@@ -23,7 +23,17 @@ Which declare and define the datatype as the names suggest.
23 :height: 246px
23 :height: 246px
24 :alt: diagram illustrating data structures associated with declareDatatype defineDatatype.
24 :alt: diagram illustrating data structures associated with declareDatatype defineDatatype.
26−Here is a simple example constructing a boolean type:
26+.. list-table::
28+ * - As a first example, the following boolean-like type can be constructed. When the compiler has run it will be available to us as if we had compiled it in the usual way:
29+ - .. code-block:: idris
32+ data Two : Type where
36+This was generated by the following code:
28.. code-block:: idris
38.. code-block:: idris
@@ -42,15 +52,7 @@ Here is a simple example constructing a boolean type:
43.. list-table::
53.. list-table::
45− * - This type is now available to us as if we had compiled it in the usual way:
46− - .. code-block:: idris
49− data Two : Type where
53− * - The constructors are T and F:
55+ * - The constructors T and F can be called as would be expected:
54 - .. code-block:: idris
56 - .. code-block:: idris
@@ -76,7 +78,23 @@ Which declare and define the function as the names suggest.
76 :height: 246px
78 :height: 246px
77 :alt: diagram illustrating data structures associated with declareDatatype defineDatatype.
79 :alt: diagram illustrating data structures associated with declareDatatype defineDatatype.
81+Note: FunClause - holds a 'let' clause, the left hand side (lhs) and right hand side (rhs) must have the same type. TT does not have 'where' clauses because they are desugared out.
79−We can now add some function definitions to the above datatype:
85+ * - Some function definitions can now be added to the above datatype. This is what they will look like:
86+ - .. code-block:: idris
97+This was generated with the following code:
81.. code-block:: idris
99.. code-block:: idris
@@ -98,18 +116,6 @@ We can now add some function definitions to the above datatype:
99.. list-table::
117.. list-table::
101− * - Here are the functions:
102− - .. code-block:: idris
113 * - This is what happens when we call the functions:
119 * - This is what happens when we call the functions:
114 - .. code-block:: idris
120 - .. code-block:: idris
@@ -118,7 +124,21 @@ We can now add some function definitions to the above datatype:
118 λΠ> perm2 T
124 λΠ> perm2 T
119 F T : Two
125 F T : Two
127+So far these datatypes and functions could have been written, statically, in the usual way. However, it is possible to imagine situations where we may need a lot of functions to be generated automatically at compile time. For example, if we extend this Boolean datatype to a datatype with more simple constructors (a finite set), we could generate a function for every possible permutation of that datatype back to itself.
129+A Different Example which has Type Parameters
130+---------------------------------------------
121−Here is an example with parameters:
134+ * - Here is an example of a datatype with type parameters:
135+ - .. code-block:: idris
137+ data N : Nat -> Type where
139+ MkN' : (x : Nat) -> N (S x)
141+This was produced by the following code:
123.. code-block:: idris
143.. code-block:: idris
@@ -138,15 +158,7 @@ Here is an example with parameters:
139 %runElab addData
159 %runElab addData
141−So this declares and defines the following data structure 'N' with a constructor 'MkN' which can have an implicit or an explicit Nat argument.
161+So this declares and defines the following data structure 'N' with a constructor 'MkN' which can have an implicit or an explicit Nat argument. Which can be used like this:
143−.. code-block:: idris
145− data N : Nat -> Type where
147− MkN' : (x : Nat) -> N (S x)
149−Which can be used like this:
151.. code-block:: idris
163.. code-block:: idris