Free Types: Difference between revisions

m (Vella moved page Freetypes to Free Types)
No edit summary
Line 10: Line 10:


=== What is a Free Type ===
=== What is a Free Type ===
Free types are sum types [https://en.wikipedia.org/wiki/Tagged_union].
They make it easier to define recursive structures.
=== Syntax ===
==== ''FREETYPES'' ====
The ''FREETYPES'' machine clause contains a semicolon separated list of ''Freetype Definitions'':
<pre>
FREETYPES
  <Freetype Definition 1>;
  ...;
  <Freetype Definition N>
</pre>
==== ''Freetype Definition'' ====
Each ''Freetype Definition'' consists of an identifier (the name of the free type), followed by an equals symbol and one or more ''Freetype Constructors'' separated by commas:
<pre>
<Identifier> = <Freetype Constructor 1>, ..., <Freetype Constructor N>
</pre>
==== ''Freetype Constructor'' ====
A ''Freetype Constructor'' may be a single identifier (the name of the free type constructor) or an identifier with a single expression argument (the type of the payload data) in parentheses.
<pre>
<Identifier>
<Identifier>(<Expression>)
</pre>

Revision as of 12:25, 22 April 2024

Summary

Free types exist in Z and in the Rodin theory plugin and are supported by ProB. You can also define new free types in classical B by adding a FREETYPES clause with free type definitions separated by semicolon.

Here is a definition of an inductive type IntList for lists of integers constructed using inil and icons:

FREETYPES
  IntList = inil, icons(INTEGER*IntList)

What is a Free Type

Free types are sum types [1]. They make it easier to define recursive structures.

Syntax

FREETYPES

The FREETYPES machine clause contains a semicolon separated list of Freetype Definitions:

FREETYPES
  <Freetype Definition 1>;
  ...;
  <Freetype Definition N>

Freetype Definition

Each Freetype Definition consists of an identifier (the name of the free type), followed by an equals symbol and one or more Freetype Constructors separated by commas:

<Identifier> = <Freetype Constructor 1>, ..., <Freetype Constructor N>

Freetype Constructor

A Freetype Constructor may be a single identifier (the name of the free type constructor) or an identifier with a single expression argument (the type of the payload data) in parentheses.

<Identifier>
<Identifier>(<Expression>)