@@ -7,13 +7,13 @@ Types and Functions
7Primitive Types
7Primitive Types
8---------------
8---------------
10−``Idris`` defines several primitive types: ``Int``, ``Integer`` and
10+Idris defines several primitive types: ``Int``, ``Integer`` and
11``Float`` for numeric operations, ``Char`` and ``String`` for text
11``Float`` for numeric operations, ``Char`` and ``String`` for text
12manipulation, and ``Ptr`` which represents foreign pointers. There are
12manipulation, and ``Ptr`` which represents foreign pointers. There are
13also several data types declared in the library, including ``Bool``,
13also several data types declared in the library, including ``Bool``,
14with values ``True`` and ``False``. We can declare some constants with
14with values ``True`` and ``False``. We can declare some constants with
15these types. Enter the following into a file ``prims.idr`` and load it
15these types. Enter the following into a file ``prims.idr`` and load it
16−into the ``Idris`` interactive environment by typing ``idris
16+into the Idris interactive environment by typing ``idris
17prims.idr``:
17prims.idr``:
19.. code-block:: idris
19.. code-block:: idris
@@ -32,9 +32,9 @@ prims.idr``:
32 quux : Bool
32 quux : Bool
33 quux = False
33 quux = False
35−An ``Idris`` file consists of an optional module declaration (here
35+An Idris file consists of an optional module declaration (here
36``module prims``) followed by an optional list of imports (none here,
36``module prims``) followed by an optional list of imports (none here,
37−however ``Idris`` programs can consist of several modules, and the
37+however Idris programs can consist of several modules, and the
38definitions in each module each have their own namespace, as we will
38definitions in each module each have their own namespace, as we will
39discuss in Section :ref:`sect-namespaces`) and a collection of
39discuss in Section :ref:`sect-namespaces`) and a collection of
40declarations and definitions. The order of definitions is significant
40declarations and definitions. The order of definitions is significant
@@ -46,7 +46,7 @@ declaration. Alternatively, declarations may be terminated with a
48A library module ``prelude`` is automatically imported by every
48A library module ``prelude`` is automatically imported by every
49−``Idris`` program, including facilities for IO, arithmetic, data
49+Idris program, including facilities for IO, arithmetic, data
50structures and various common functions. The prelude defines several
50structures and various common functions. The prelude defines several
51arithmetic and comparison operators, which we can use at the prompt.
51arithmetic and comparison operators, which we can use at the prompt.
52Evaluating things at the prompt gives an answer, and the type of the
52Evaluating things at the prompt gives an answer, and the type of the
@@ -108,7 +108,7 @@ Functions
110Functions are implemented by pattern matching, again using a similar
110Functions are implemented by pattern matching, again using a similar
111−syntax to Haskell. The main difference is that ``Idris`` requires type
111+syntax to Haskell. The main difference is that Idris requires type
112declarations for all functions, using a single colon ``:`` (rather
112declarations for all functions, using a single colon ``:`` (rather
113than Haskell’s double colon ``::``). Some natural number arithmetic
113than Haskell’s double colon ``::``). Some natural number arithmetic
114functions can be defined as follows, again taken from the standard
114functions can be defined as follows, again taken from the standard
@@ -133,7 +133,7 @@ names must begin with a capital letter or not. Function names
133(``plus`` and ``mult`` above), data constructors (``Z``, ``S``,
133(``plus`` and ``mult`` above), data constructors (``Z``, ``S``,
134``Nil`` and ``::``) and type constructors (``Nat`` and ``List``) are
134``Nil`` and ``::``) and type constructors (``Nat`` and ``List``) are
135all part of the same namespace. We can test these functions at the
135all part of the same namespace. We can test these functions at the
136−``Idris`` prompt:
136+Idris prompt:
@@ -144,10 +144,10 @@ all part of the same namespace. We can test these functions at the
147− ``Idris`` automatically desugars the ``Nat`` representation into a
147+ Idris automatically desugars the ``Nat`` representation into a
148 more human readable format. The result of ``plus (S (S Z)) (S (S Z))``
148 more human readable format. The result of ``plus (S (S Z)) (S (S Z))``
149 is actually ``(S (S (S (S Z))))`` which is the Integer 4. This can be
149 is actually ``(S (S (S (S Z))))`` which is the Integer 4. This can be
150− checked at the ``Idris`` prompt:
150+ checked at the Idris prompt:
@@ -169,7 +169,7 @@ computers have perfectly good integer arithmetic built in. The reason
169is primarily that unary numbers have a very convenient structure which
169is primarily that unary numbers have a very convenient structure which
170is easy to reason about, and easy to relate to other data structures
170is easy to reason about, and easy to relate to other data structures
171as we will see later. Nevertheless, we do not want this convenience to
171as we will see later. Nevertheless, we do not want this convenience to
172−be at the expense of efficiency. Fortunately, ``Idris`` knows about
172+be at the expense of efficiency. Fortunately, Idris knows about
173the relationship between ``Nat`` (and similarly structured types) and
173the relationship between ``Nat`` (and similarly structured types) and
174numbers. This means it can optimise the representation, and functions
174numbers. This means it can optimise the representation, and functions
175such as ``plus`` and ``mult``.
175such as ``plus`` and ``mult``.
@@ -248,7 +248,7 @@ Vectors
249A standard example of a dependent type is the type of “lists with
249A standard example of a dependent type is the type of “lists with
250length”, conventionally called vectors in the dependent type
250length”, conventionally called vectors in the dependent type
251−literature. They are available as part of the ``Idris`` library, by
251+literature. They are available as part of the Idris library, by
252importing ``Data.Vect``, or we can declare them as follows:
252importing ``Data.Vect``, or we can declare them as follows:
254.. code-block:: idris
254.. code-block:: idris
@@ -258,7 +258,7 @@ importing ``Data.Vect``, or we can declare them as follows:
258 (::) : a -> Vect k a -> Vect (S k) a
258 (::) : a -> Vect k a -> Vect (S k) a
260Note that we have used the same constructor names as for ``List``.
260Note that we have used the same constructor names as for ``List``.
261−Ad-hoc name overloading such as this is accepted by ``Idris``,
261+Ad-hoc name overloading such as this is accepted by Idris,
262provided that the names are declared in different namespaces (in
262provided that the names are declared in different namespaces (in
263practice, normally in different modules). Ambiguous constructor names
263practice, normally in different modules). Ambiguous constructor names
264can normally be resolved from context.
264can normally be resolved from context.
@@ -289,7 +289,7 @@ defined as follows, appends two ``Vect``:
290The type of ``(++)`` states that the resulting vector’s length will be
290The type of ``(++)`` states that the resulting vector’s length will be
291the sum of the input lengths. If we get the definition wrong in such a
291the sum of the input lengths. If we get the definition wrong in such a
292−way that this does not hold, ``Idris`` will not accept the definition.
292+way that this does not hold, Idris will not accept the definition.
293For example:
293For example:
295.. code-block:: idris
295.. code-block:: idris
@@ -298,7 +298,7 @@ For example:
298 (++) Nil ys = ys
298 (++) Nil ys = ys
299 (++) (x :: xs) ys = x :: xs ++ xs -- BROKEN
299 (++) (x :: xs) ys = x :: xs ++ xs -- BROKEN
301−When run through the ``Idris`` type checker, this results in the
301+When run through the Idris type checker, this results in the
302following:
302following:
@@ -325,7 +325,7 @@ The Finite Sets
325~~~~~~~~~~~~~~~
325~~~~~~~~~~~~~~~
327Finite sets, as the name suggests, are sets with a finite number of
327Finite sets, as the name suggests, are sets with a finite number of
328−elements. They are available as part of the ``Idris`` library, by
328+elements. They are available as part of the Idris library, by
329importing ``Data.Fin``, or can be declared as follows:
329importing ``Data.Fin``, or can be declared as follows:
331.. code-block:: idris
331.. code-block:: idris
@@ -480,9 +480,9 @@ I/O
481Computer programs are of little use if they do not interact with the
481Computer programs are of little use if they do not interact with the
482user or the system in some way. The difficulty in a pure language such
482user or the system in some way. The difficulty in a pure language such
483−as ``Idris`` — that is, a language where expressions do not have
483+as Idris — that is, a language where expressions do not have
484side-effects — is that I/O is inherently side-effecting. Therefore in
484side-effects — is that I/O is inherently side-effecting. Therefore in
485−``Idris``, such interactions are encapsulated in the type ``IO``:
485+Idris, such interactions are encapsulated in the type ``IO``:
487.. code-block:: idris
487.. code-block:: idris
@@ -567,7 +567,7 @@ Laziness
569Normally, arguments to functions are evaluated before the function
569Normally, arguments to functions are evaluated before the function
570−itself (that is, ``Idris`` uses *eager* evaluation). However, this is
570+itself (that is, Idris uses *eager* evaluation). However, this is
571not always the best approach. Consider the following function:
571not always the best approach. Consider the following function:
573.. code-block:: idris
573.. code-block:: idris
@@ -579,7 +579,7 @@ not always the best approach. Consider the following function:
579This function uses one of the ``t`` or ``e`` arguments, but not both
579This function uses one of the ``t`` or ``e`` arguments, but not both
580(in fact, this is used to implement the ``if...then...else`` construct
580(in fact, this is used to implement the ``if...then...else`` construct
581as we will see later. We would prefer if *only* the argument which was
581as we will see later. We would prefer if *only* the argument which was
582−used was evaluated. To achieve this, ``Idris`` provides a ``Lazy``
582+used was evaluated. To achieve this, Idris provides a ``Lazy``
583data type, which allows evaluation to be suspended:
583data type, which allows evaluation to be suspended:
585.. code-block:: idris
585.. code-block:: idris
@@ -590,7 +590,7 @@ data type, which allows evaluation to be suspended:
590 Force : Lazy a -> a
590 Force : Lazy a -> a
592A value of type ``Lazy a`` is unevaluated until it is forced by
592A value of type ``Lazy a`` is unevaluated until it is forced by
593−``Force``. The ``Idris`` type checker knows about the ``Lazy`` type,
593+``Force``. The Idris type checker knows about the ``Lazy`` type,
594and inserts conversions where necessary between ``Lazy a`` and ``a``,
594and inserts conversions where necessary between ``Lazy a`` and ``a``,
595and vice versa. We can therefore write ``boolCase`` as follows,
595and vice versa. We can therefore write ``boolCase`` as follows,
596without any explicit use of ``Force`` or ``Delay``:
596without any explicit use of ``Force`` or ``Delay``:
@@ -604,10 +604,10 @@ without any explicit use of ``Force`` or ``Delay``:
604Useful Data Types
604Useful Data Types
605-----------------
605-----------------
607−``Idris`` includes a number of useful data types and library functions
607+Idris includes a number of useful data types and library functions
608(see the ``libs/`` directory in the distribution). This chapter
608(see the ``libs/`` directory in the distribution). This chapter
609describes a few of these. The functions described here are imported
609describes a few of these. The functions described here are imported
610−automatically by every ``Idris`` program, as part of ``Prelude.idr``.
610+automatically by every Idris program, as part of ``Prelude.idr``.
612``List`` and ``Vect``
612``List`` and ``Vect``
613~~~~~~~~~~~~~~~~~~~~~
613~~~~~~~~~~~~~~~~~~~~~
@@ -679,7 +679,7 @@ directory. For more details of the functions available on ``List`` and
679- ``libs/base/Data/VectType.idr``
679- ``libs/base/Data/VectType.idr``
681Functions include filtering, appending, reversing, and so on. Also
681Functions include filtering, appending, reversing, and so on. Also
682−remember that ``Idris`` is still in development, so if you don’t see
682+remember that Idris is still in development, so if you don’t see
683the function you need, please feel free to add it and submit a patch!
683the function you need, please feel free to add it and submit a patch!
685Aside: Anonymous functions and operator sections
685Aside: Anonymous functions and operator sections
@@ -877,7 +877,7 @@ pattern matching at the top level:
877List comprehensions
877List comprehensions
878~~~~~~~~~~~~~~~~~~~
878~~~~~~~~~~~~~~~~~~~
880−``Idris`` provides *comprehension* notation as a convenient shorthand
880+Idris provides *comprehension* notation as a convenient shorthand
881for building lists. The general form is:
881for building lists. The general form is:
@@ -958,10 +958,10 @@ matching ``let`` and lambda bindings. It will *only* work if:
958Dependent Records
958Dependent Records
959-----------------
959-----------------
961−*Records* are data types which collect several values (the record’s
961+*Records* are data types which collect several values (the record's
962−*fields*) together. ``Idris`` provides syntax for defining records and
962+*fields*) together. Idris provides syntax for defining records and
963*automatically generating field access and update functions. For
963*automatically generating field access and update functions. For
964−*example, we can represent a person’s name and age in a record:
964+*example, we can represent a person's name and age in a record:
966.. code-block:: idris
966.. code-block:: idris
@@ -1029,7 +1029,7 @@ length because it will not affect the type of the record:
1029Nested record update
1029Nested record update
1030~~~~~~~~~~~~~~~~~~~~
1030~~~~~~~~~~~~~~~~~~~~
1032−``Idris`` also provides a convenient syntax for accessing and updating
1032+Idris also provides a convenient syntax for accessing and updating
1033nested records. For example, if a field is accessible with the
1033nested records. For example, if a field is accessible with the
1034expression ``c (b (a x))``, it can be updated using the following
1034expression ``c (b (a x))``, it can be updated using the following