@@ -1,11 +1,11 @@
1.. _sect-typefuns:
1.. _sect-typefuns:
3−===================
3+*******************
4Types and Functions
4Types and Functions
5−===================
5+*******************
7Primitive Types
7Primitive Types
8−---------------
8+===============
10Idris defines several primitive types: ``Int``, ``Integer`` and
10Idris 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
@@ -71,7 +71,7 @@ work on user defined types. Boolean expressions can be tested with the
71 "The answer!" : String
71 "The answer!" : String
74−----------
74+==========
76Data types are declared in a similar way to Haskell data types, with a
76Data types are declared in a similar way to Haskell data types, with a
77similar syntax. Natural numbers and lists, for example, can be
77similar syntax. Natural numbers and lists, for example, can be
@@ -105,7 +105,7 @@ symbols:
105 :+-*/=_.?|&><!@$%^~.
105 :+-*/=_.?|&><!@$%^~.
108−---------
108+=========
110Functions are implemented by pattern matching, again using a similar
110Functions are implemented by pattern matching, again using a similar
111syntax to Haskell. The main difference is that Idris requires type
111syntax to Haskell. The main difference is that Idris requires type
@@ -175,7 +175,7 @@ numbers. This means it can optimise the representation, and functions
175such as ``plus`` and ``mult``.
175such as ``plus`` and ``mult``.
177``where`` clauses
177``where`` clauses
178−~~~~~~~~~~~~~~~~~
178+-----------------
180Functions can also be defined *locally* using ``where`` clauses. For
180Functions can also be defined *locally* using ``where`` clauses. For
181example, to define a function which reverses a list, we can use an
181example, to define a function which reverses a list, we can use an
@@ -241,10 +241,10 @@ So, for example, the following definitions are legal:
241 where z w = y + w
241 where z w = y + w
243Dependent Types
243Dependent Types
244−---------------
244+===============
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
@@ -322,7 +322,7 @@ two vectors — we needed a vector of length ``k + m``, but provided a
322vector of length ``k + k``.
322vector of length ``k + k``.
324The Finite Sets
324The 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
328elements. They are available as part of the Idris library, by
328elements. They are available as part of the Idris library, by
@@ -334,16 +334,25 @@ importing ``Data.Fin``, or can be declared as follows:
334 FZ : Fin (S k)
334 FZ : Fin (S k)
335 FS : Fin k -> Fin (S k)
335 FS : Fin k -> Fin (S k)
337+From the signature, we can see that this is a type constructor that takes a ``Nat``, and produces a type.
338+So this is not a set in the sense of a collection that is a container of objects,
339+rather it is the canonical set of unnamed elements, as in "the set of 5 elements," for example.
340+Effectively, it is a type that captures integers that fall into the range of zero to ``(n - 1)`` where
341+``n`` is the argument used to instantiate the ``Fin`` type.
342+For example, ``Fin 5`` can be thought of as the type of integers between 0 and 4.
344+Let us look at the constructors in greater detail.
337``FZ`` is the zeroth element of a finite set with ``S k`` elements;
346``FZ`` is the zeroth element of a finite set with ``S k`` elements;
338``FS n`` is the ``n+1``\ th element of a finite set with ``S k``
347``FS n`` is the ``n+1``\ th element of a finite set with ``S k``
339elements. ``Fin`` is indexed by a ``Nat``, which represents the number
348elements. ``Fin`` is indexed by a ``Nat``, which represents the number
340−of elements in the set. Obviously we can’t construct an element of an
349+of elements in the set. Since we can’t construct an element of an
341−empty set, so neither constructor targets ``Fin Z``.
350+empty set, neither constructor targets ``Fin Z``.
343−A useful application of the ``Fin`` family is to represent bounded
352+As mentioned above, a useful application of the ``Fin`` family is to represent bounded
344natural numbers. Since the first ``n`` natural numbers form a finite
353natural numbers. Since the first ``n`` natural numbers form a finite
345−set of ``n`` elements, we can treat ``Fin n`` as the set of natural
354+set of ``n`` elements, we can treat ``Fin n`` as the set of integers
346−numbers bounded by ``n``.
355+greater than or equal to zero and less than ``n``.
348For example, the following function which looks up an element in a
357For example, the following function which looks up an element in a
349``Vect``, by a bounded index given as a ``Fin n``, is defined in the
358``Vect``, by a bounded index given as a ``Fin n``, is defined in the
@@ -359,7 +368,7 @@ This function looks up a value at a given location in a vector. The
359location is bounded by the length of the vector (``n`` in each case),
368location is bounded by the length of the vector (``n`` in each case),
360so there is no need for a run-time bounds check. The type checker
369so there is no need for a run-time bounds check. The type checker
361guarantees that the location is no larger than the length of the
370guarantees that the location is no larger than the length of the
362−vector.
371+vector, and of course no less than zero.
364Note also that there is no case for ``Nil`` here. This is because it
373Note also that there is no case for ``Nil`` here. This is because it
365is impossible. Since there is no element of ``Fin Z``, and the
374is impossible. Since there is no element of ``Fin Z``, and the
@@ -368,7 +377,7 @@ attempting to look up an element in an empty vector would give a
368compile time type error, since it would force ``n`` to be ``Z``.
377compile time type error, since it would force ``n`` to be ``Z``.
370Implicit Arguments
379Implicit Arguments
371−~~~~~~~~~~~~~~~~~~
380+------------------
373Let us take a closer look at the type of ``index``:
382Let us take a closer look at the type of ``index``:
@@ -411,7 +420,7 @@ help document a function by making the purpose of an argument more
413“``using``” notation
422“``using``” notation
414−~~~~~~~~~~~~~~~~~~~~
423+--------------------
416Sometimes it is useful to provide types of implicit arguments,
425Sometimes it is useful to provide types of implicit arguments,
417particularly where there is a dependency ordering, or where the
426particularly where there is a dependency ordering, or where the
@@ -451,7 +460,7 @@ appear within the block:
451 There : Elem x xs -> Elem x (y :: xs)
460 There : Elem x xs -> Elem x (y :: xs)
453Note: Declaration Order and ``mutual`` blocks
462Note: Declaration Order and ``mutual`` blocks
454−~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
463+---------------------------------------------
456In general, functions and data types must be defined before use, since
465In general, functions and data types must be defined before use, since
457dependent types allow functions to appear as part of types, and their
466dependent types allow functions to appear as part of types, and their
@@ -476,7 +485,7 @@ depend on the reduction behaviour of any of the functions in the
481Computer programs are of little use if they do not interact with the
490Computer 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
491user or the system in some way. The difficulty in a pure language such
@@ -533,7 +542,7 @@ example for reading and writing files, including:
533.. _sect-do:
542.. _sect-do:
535“``do``” notation
544“``do``” notation
536−-----------------
545+=================
538I/O programs will typically need to sequence actions, feeding the
547I/O programs will typically need to sequence actions, feeding the
539output of one computation into the input of the next. ``IO`` is an
548output of one computation into the input of the next. ``IO`` is an
@@ -564,7 +573,7 @@ can be overloaded.
564.. _sect-lazy:
573.. _sect-lazy:
569Normally, arguments to functions are evaluated before the function
578Normally, arguments to functions are evaluated before the function
570itself (that is, Idris uses *eager* evaluation). However, this is
579itself (that is, Idris uses *eager* evaluation). However, this is
@@ -602,7 +611,7 @@ without any explicit use of ``Force`` or ``Delay``:
602 boolCase False t e = e;
611 boolCase False t e = e;
604Useful Data Types
613Useful Data Types
605−-----------------
614+=================
607Idris includes a number of useful data types and library functions
616Idris includes a number of useful data types and library functions
608(see the ``libs/`` directory in the distribution). This chapter
617(see the ``libs/`` directory in the distribution). This chapter
@@ -610,7 +619,7 @@ describes a few of these. The functions described here are imported
610automatically by every Idris program, as part of ``Prelude.idr``.
619automatically by every Idris program, as part of ``Prelude.idr``.
612``List`` and ``Vect``
621``List`` and ``Vect``
613−~~~~~~~~~~~~~~~~~~~~~
622+---------------------
615We have already seen the ``List`` and ``Vect`` data types:
624We have already seen the ``List`` and ``Vect`` data types:
@@ -683,7 +692,7 @@ 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!
692the function you need, please feel free to add it and submit a patch!
685Aside: Anonymous functions and operator sections
694Aside: Anonymous functions and operator sections
686−~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
695+------------------------------------------------
688There are actually neater ways to write the above expression. One way
697There are actually neater ways to write the above expression. One way
689would be to use an anonymous function:
698would be to use an anonymous function:
@@ -710,7 +719,7 @@ by 2. It expands to ``\x => x * 2``. Similarly, ``(2*)`` would expand
710to ``\x => 2 * x``.
719to ``\x => 2 * x``.
715``Maybe`` describes an optional value. Either there is a value of the
724``Maybe`` describes an optional value. Either there is a value of the
716given type, or there isn’t:
725given type, or there isn’t:
@@ -744,7 +753,7 @@ simply ``b``. Since the default value might not be used, we mark it as
744discarding it would be wasteful.
753discarding it would be wasteful.
746Tuples and Dependent Pairs
755Tuples and Dependent Pairs
747−~~~~~~~~~~~~~~~~~~~~~~~~~~
756+--------------------------
749Values can be paired with the following built-in data type:
758Values can be paired with the following built-in data type:
@@ -765,7 +774,7 @@ contain an arbitrary number of values, represented as nested pairs:
765 jim = ("Jim", 25, "Cambridge")
774 jim = ("Jim", 25, "Cambridge")
767Dependent Pairs
776Dependent Pairs
768−~~~~~~~~~~~~~~~
777+---------------
770Dependent pairs allow the type of the second element of a pair to depend
779Dependent pairs allow the type of the second element of a pair to depend
771on the value of the first element. Traditionally, these are referred to
780on the value of the first element. Traditionally, these are referred to
@@ -843,10 +852,10 @@ We will see more on ``with`` notation later.
843.. _sect-more-expr:
852.. _sect-more-expr:
845More Expressions
854More Expressions
846−----------------
855+================
848``let`` bindings
857``let`` bindings
849−~~~~~~~~~~~~~~~~
858+----------------
851Intermediate values can be calculated using ``let`` bindings:
860Intermediate values can be calculated using ``let`` bindings:
@@ -875,7 +884,7 @@ pattern matching at the top level:
875 name ++ " is " ++ show age ++ " years old"
884 name ++ " is " ++ show age ++ " years old"
877List comprehensions
886List comprehensions
878−~~~~~~~~~~~~~~~~~~~
887+-------------------
880Idris provides *comprehension* notation as a convenient shorthand
889Idris provides *comprehension* notation as a convenient shorthand
881for building lists. The general form is:
890for building lists. The general form is:
@@ -902,7 +911,7 @@ by the difference between ``a`` and ``b``. This works for any numeric
902type, using the ``count`` function from the prelude.
911type, using the ``count`` function from the prelude.
904``case`` expressions
913``case`` expressions
905−~~~~~~~~~~~~~~~~~~~~
914+--------------------
907Another way of inspecting intermediate values of *simple* types is to
916Another way of inspecting intermediate values of *simple* types is to
908use a ``case`` expression. The following function, for example, splits
917use a ``case`` expression. The following function, for example, splits
@@ -956,7 +965,7 @@ matching ``let`` and lambda bindings. It will *only* work if:
958Dependent Records
967Dependent Records
959−-----------------
968+=================
961*Records* are data types which collect several values (the record's
970*Records* are data types which collect several values (the record's
962*fields*) together. Idris provides syntax for defining records and
971*fields*) together. Idris provides syntax for defining records and
@@ -1027,7 +1036,7 @@ length because it will not affect the type of the record:
1029Nested record update
1038Nested record update
1030−~~~~~~~~~~~~~~~~~~~~
1039+--------------------
1032Idris also provides a convenient syntax for accessing and updating
1041Idris also provides a convenient syntax for accessing and updating
1033nested records. For example, if a field is accessible with the
1042nested records. For example, if a field is accessible with the