@@ -1,11 +1,11 @@
11.. _sect-typefuns:
22
3−===================
3+*******************
44Types and Functions
5−===================
5+*******************
66
77Primitive Types
8−---------------
8+===============
99
1010Idris defines several primitive types: ``Int``, ``Integer`` and
1111``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
7171 "The answer!" : String
7272
7373Data Types
74−----------
74+==========
7575
7676Data types are declared in a similar way to Haskell data types, with a
7777similar syntax. Natural numbers and lists, for example, can be
@@ -105,7 +105,7 @@ symbols:
105105 :+-*/=_.?|&><!@$%^~.
106106
107107Functions
108−---------
108+=========
109109
110110Functions are implemented by pattern matching, again using a similar
111111syntax to Haskell. The main difference is that Idris requires type
@@ -175,7 +175,7 @@ numbers. This means it can optimise the representation, and functions
175175such as ``plus`` and ``mult``.
176176
177177``where`` clauses
178−~~~~~~~~~~~~~~~~~
178+-----------------
179179
180180Functions can also be defined *locally* using ``where`` clauses. For
181181example, to define a function which reverses a list, we can use an
@@ -241,10 +241,10 @@ So, for example, the following definitions are legal:
241241 where z w = y + w
242242
243243Dependent Types
244−---------------
244+===============
245245
246246Vectors
247−~~~~~~~
247+-------
248248
249249A standard example of a dependent type is the type of “lists with
250250length”, conventionally called vectors in the dependent type
@@ -322,7 +322,7 @@ two vectors — we needed a vector of length ``k + m``, but provided a
322322vector of length ``k + k``.
323323
324324The Finite Sets
325−~~~~~~~~~~~~~~~
325+---------------
326326
327327Finite sets, as the name suggests, are sets with a finite number of
328328elements. They are available as part of the Idris library, by
@@ -334,16 +334,25 @@ importing ``Data.Fin``, or can be declared as follows:
334334 FZ : Fin (S k)
335335 FS : Fin k -> Fin (S k)
336336
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.
343+
344+Let us look at the constructors in greater detail.
345+
337346``FZ`` is the zeroth element of a finite set with ``S k`` elements;
338347``FS n`` is the ``n+1``\ th element of a finite set with ``S k``
339348elements. ``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
341−empty set, so neither constructor targets ``Fin Z``.
349+of elements in the set. Since we can’t construct an element of an
350+empty set, neither constructor targets ``Fin Z``.
342351
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
344353natural 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
346−numbers bounded by ``n``.
354+set of ``n`` elements, we can treat ``Fin n`` as the set of integers
355+greater than or equal to zero and less than ``n``.
347356
348357For example, the following function which looks up an element in a
349358``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
359368location is bounded by the length of the vector (``n`` in each case),
360369so there is no need for a run-time bounds check. The type checker
361370guarantees that the location is no larger than the length of the
362−vector.
371+vector, and of course no less than zero.
363372
364373Note also that there is no case for ``Nil`` here. This is because it
365374is 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
368377compile time type error, since it would force ``n`` to be ``Z``.
369378
370379Implicit Arguments
371−~~~~~~~~~~~~~~~~~~
380+------------------
372381
373382Let us take a closer look at the type of ``index``:
374383
@@ -411,7 +420,7 @@ help document a function by making the purpose of an argument more
411420clear.
412421
413422“``using``” notation
414−~~~~~~~~~~~~~~~~~~~~
423+--------------------
415424
416425Sometimes it is useful to provide types of implicit arguments,
417426particularly where there is a dependency ordering, or where the
@@ -451,7 +460,7 @@ appear within the block:
451460 There : Elem x xs -> Elem x (y :: xs)
452461
453462Note: Declaration Order and ``mutual`` blocks
454−~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
463+---------------------------------------------
455464
456465In general, functions and data types must be defined before use, since
457466dependent 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
476485block.
477486
478487I/O
479−---
488+===
480489
481490Computer programs are of little use if they do not interact with the
482491user or the system in some way. The difficulty in a pure language such
@@ -533,7 +542,7 @@ example for reading and writing files, including:
533542.. _sect-do:
534543
535544“``do``” notation
536−-----------------
545+=================
537546
538547I/O programs will typically need to sequence actions, feeding the
539548output of one computation into the input of the next. ``IO`` is an
@@ -564,7 +573,7 @@ can be overloaded.
564573.. _sect-lazy:
565574
566575Laziness
567−--------
576+========
568577
569578Normally, arguments to functions are evaluated before the function
570579itself (that is, Idris uses *eager* evaluation). However, this is
@@ -602,7 +611,7 @@ without any explicit use of ``Force`` or ``Delay``:
602611 boolCase False t e = e;
603612
604613Useful Data Types
605−-----------------
614+=================
606615
607616Idris includes a number of useful data types and library functions
608617(see the ``libs/`` directory in the distribution). This chapter
@@ -610,7 +619,7 @@ describes a few of these. The functions described here are imported
610619automatically by every Idris program, as part of ``Prelude.idr``.
611620
612621``List`` and ``Vect``
613−~~~~~~~~~~~~~~~~~~~~~
622+---------------------
614623
615624We have already seen the ``List`` and ``Vect`` data types:
616625
@@ -683,7 +692,7 @@ remember that Idris is still in development, so if you don’t see
683692the function you need, please feel free to add it and submit a patch!
684693
685694Aside: Anonymous functions and operator sections
686−~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
695+------------------------------------------------
687696
688697There are actually neater ways to write the above expression. One way
689698would be to use an anonymous function:
@@ -710,7 +719,7 @@ by 2. It expands to ``\x => x * 2``. Similarly, ``(2*)`` would expand
710719to ``\x => 2 * x``.
711720
712721Maybe
713−~~~~~
722+-----
714723
715724``Maybe`` describes an optional value. Either there is a value of the
716725given type, or there isn’t:
@@ -744,7 +753,7 @@ simply ``b``. Since the default value might not be used, we mark it as
744753discarding it would be wasteful.
745754
746755Tuples and Dependent Pairs
747−~~~~~~~~~~~~~~~~~~~~~~~~~~
756+--------------------------
748757
749758Values can be paired with the following built-in data type:
750759
@@ -765,7 +774,7 @@ contain an arbitrary number of values, represented as nested pairs:
765774 jim = ("Jim", 25, "Cambridge")
766775
767776Dependent Pairs
768−~~~~~~~~~~~~~~~
777+---------------
769778
770779Dependent pairs allow the type of the second element of a pair to depend
771780on the value of the first element. Traditionally, these are referred to
@@ -843,10 +852,10 @@ We will see more on ``with`` notation later.
843852.. _sect-more-expr:
844853
845854More Expressions
846−----------------
855+================
847856
848857``let`` bindings
849−~~~~~~~~~~~~~~~~
858+----------------
850859
851860Intermediate values can be calculated using ``let`` bindings:
852861
@@ -875,7 +884,7 @@ pattern matching at the top level:
875884 name ++ " is " ++ show age ++ " years old"
876885
877886List comprehensions
878−~~~~~~~~~~~~~~~~~~~
887+-------------------
879888
880889Idris provides *comprehension* notation as a convenient shorthand
881890for building lists. The general form is:
@@ -902,7 +911,7 @@ by the difference between ``a`` and ``b``. This works for any numeric
902911type, using the ``count`` function from the prelude.
903912
904913``case`` expressions
905−~~~~~~~~~~~~~~~~~~~~
914+--------------------
906915
907916Another way of inspecting intermediate values of *simple* types is to
908917use a ``case`` expression. The following function, for example, splits
@@ -956,7 +965,7 @@ matching ``let`` and lambda bindings. It will *only* work if:
956965 itself.
957966
958967Dependent Records
959−-----------------
968+=================
960969
961970*Records* are data types which collect several values (the record's
962971*fields*) together. Idris provides syntax for defining records and
@@ -1027,7 +1036,7 @@ length because it will not affect the type of the record:
10271036 : Class
10281037
10291038Nested record update
1030−~~~~~~~~~~~~~~~~~~~~
1039+--------------------
10311040
10321041Idris also provides a convenient syntax for accessing and updating
10331042nested records. For example, if a field is accessible with the