@@ -1,3 +1,5 @@
1===================
3===================
2Types and Functions
4Types and Functions
3===================
5===================
@@ -11,8 +13,8 @@ manipulation, and ``Ptr`` which represents foreign pointers. There are
11also several data types declared in the library, including ``Bool``,
13also several data types declared in the library, including ``Bool``,
12with values ``True`` and ``False``. We can declare some constants with
14with values ``True`` and ``False``. We can declare some constants with
13these 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
14−into the ``Idris`` interactive environment by typing
16+into the ``Idris`` interactive environment by typing ``idris
15−``idris prims.idr``:
17+prims.idr``:
17.. code-block:: idris
19.. code-block:: idris
@@ -34,13 +36,14 @@ An ``Idris`` file consists of an optional module declaration (here
34``module prims``) followed by an optional list of imports (none here,
36``module prims``) followed by an optional list of imports (none here,
35however ``Idris`` programs can consist of several modules, and the
37however ``Idris`` programs can consist of several modules, and the
36definitions in each module each have their own namespace, as we will
38definitions in each module each have their own namespace, as we will
37−discuss in Section :ref:`sect-namespaces`) and a collection of declarations
39+discuss in Section :ref:`sect-namespaces`) and a collection of
38−and definitions. The order of definitions is significant — functions and
40+declarations and definitions. The order of definitions is significant
39−data types must be defined before use. Each definition must have a type
41+— functions and data types must be defined before use. Each definition
40−declaration, for example see ``x : Int``, ``foo : String``, from the
42+must have a type declaration, for example see ``x : Int``, ``foo :
41−above listing. Indentation is significant — a new declaration begins at
43+String``, from the above listing. Indentation is significant — a new
42−the same level of indentation as the preceding declaration.
44+declaration begins at the same level of indentation as the preceding
43−Alternatively, declarations may be terminated with a semicolon.
45+declaration. Alternatively, declarations may be terminated with a
45A library module ``prelude`` is automatically imported by every
48A library module ``prelude`` is automatically imported by every
46``Idris`` program, including facilities for IO, arithmetic, data
49``Idris`` program, including facilities for IO, arithmetic, data
@@ -56,10 +59,10 @@ answer. For example:
56 *prims> x == 6*6+6
59 *prims> x == 6*6+6
57 True : Bool
60 True : Bool
59−All of the usual arithmetic and comparison operators are defined for the
62+All of the usual arithmetic and comparison operators are defined for
60−primitive types. They are overloaded using type classes, as we will
63+the primitive types. They are overloaded using type classes, as we
61−discuss in Section :ref:`sect-classes` and can be extended to work on user
64+will discuss in Section :ref:`sect-classes` and can be extended to
62−defined types. Boolean expressions can be tested with the
65+work on user defined types. Boolean expressions can be tested with the
63``if...then...else`` construct:
66``if...then...else`` construct:
@@ -71,8 +74,8 @@ Data Types
73Data 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
74−similar syntax. Natural numbers and lists, for example, can be declared
77+similar syntax. Natural numbers and lists, for example, can be
75−as follows:
78+declared as follows:
77.. code-block:: idris
80.. code-block:: idris
@@ -81,19 +84,21 @@ as follows:
81 data List a = Nil | (::) a (List a) -- Polymorphic lists
84 data List a = Nil | (::) a (List a) -- Polymorphic lists
83The above declarations are taken from the standard library. Unary
86The above declarations are taken from the standard library. Unary
84−natural numbers can be either zero (``Z``), or the successor of another
87+natural numbers can be either zero (``Z``), or the successor of
85−natural number (``S k``). Lists can either be empty (``Nil``) or a value
88+another natural number (``S k``). Lists can either be empty (``Nil``)
86−added to the front of another list (``x :: xs``). In the declaration for
89+or a value added to the front of another list (``x :: xs``). In the
87−``List``, we used an infix operator ``::``. New operators such as this
90+declaration for ``List``, we used an infix operator ``::``. New
88−can be added using a fixity declaration, as follows:
91+operators such as this can be added using a fixity declaration, as
92 infixr 10 ::
96 infixr 10 ::
94Functions, data constructors and type constructors may all be given
98Functions, data constructors and type constructors may all be given
95−infix operators as names. They may be used in prefix form if enclosed in
99+infix operators as names. They may be used in prefix form if enclosed
96−brackets, e.g. \ ``(::)``. Infix operators can use any of the symbols:
100+in brackets, e.g. ``(::)``. Infix operators can use any of the
@@ -104,9 +109,10 @@ Functions
105Functions are implemented by pattern matching, again using a similar
110Functions are implemented by pattern matching, again using a similar
106syntax to Haskell. The main difference is that ``Idris`` requires type
111syntax to Haskell. The main difference is that ``Idris`` requires type
107−declarations for all functions, using a single colon ``:`` (rather than
112+declarations for all functions, using a single colon ``:`` (rather
108−Haskell’s double colon ``::``). Some natural number arithmetic functions
113+than Haskell’s double colon ``::``). Some natural number arithmetic
109−can be defined as follows, again taken from the standard library:
114+functions can be defined as follows, again taken from the standard
111.. code-block:: idris
117.. code-block:: idris
@@ -123,10 +129,11 @@ can be defined as follows, again taken from the standard library:
123The standard arithmetic operators ``+`` and ``*`` are also overloaded
129The standard arithmetic operators ``+`` and ``*`` are also overloaded
124for use by ``Nat``, and are implemented using the above functions.
130for use by ``Nat``, and are implemented using the above functions.
125Unlike Haskell, there is no restriction on whether types and function
131Unlike Haskell, there is no restriction on whether types and function
126−names must begin with a capital letter or not. Function names (``plus``
132+names must begin with a capital letter or not. Function names
127−and ``mult`` above), data constructors (``Z``, ``S``, ``Nil`` and
133+(``plus`` and ``mult`` above), data constructors (``Z``, ``S``,
128−``::``) and type constructors (``Nat`` and ``List``) are all part of the
134+``Nil`` and ``::``) and type constructors (``Nat`` and ``List``) are
129−same namespace. We can test these functions at the ``Idris`` prompt:
135+all part of the same namespace. We can test these functions at the
@@ -135,10 +142,12 @@ same namespace. We can test these functions at the ``Idris`` prompt:
135 Idris> mult (S (S (S Z))) (plus (S (S Z)) (S (S Z)))
142 Idris> mult (S (S (S Z))) (plus (S (S Z)) (S (S Z)))
138−**Note:** ``Idris`` automatically desugars the ``Nat`` representation
147+ ``Idris`` automatically desugars the ``Nat`` representation into a
139−into a more human readable format. The result of
148+ more human readable format. The result of ``plus (S (S Z)) (S (S Z))``
140−``plus (S (S Z)) (S (S Z))`` is actually ``(S (S (S (S Z))))`` which is
149+ is actually ``(S (S (S (S Z))))`` which is the Integer 4. This can be
141−the Integer 4. This can be checked at the ``Idris`` prompt:
150+ checked at the ``Idris`` prompt:
@@ -156,12 +165,12 @@ type classes, meaning that we can also test the functions as follows:
158You may wonder, by the way, why we have unary natural numbers when our
167You may wonder, by the way, why we have unary natural numbers when our
159−computers have perfectly good integer arithmetic built in. The reason is
168+computers have perfectly good integer arithmetic built in. The reason
160−primarily that unary numbers have a very convenient structure which is
169+is primarily that unary numbers have a very convenient structure which
161−easy to reason about, and easy to relate to other data structures as we
170+is easy to reason about, and easy to relate to other data structures
162−will see later. Nevertheless, we do not want this convenience to be at
171+as we will see later. Nevertheless, we do not want this convenience to
163−the expense of efficiency. Fortunately, ``Idris`` knows about the
172+be at the expense of efficiency. Fortunately, ``Idris`` knows about
164−relationship between ``Nat`` (and similarly structured types) and
173+the relationship between ``Nat`` (and similarly structured types) and
165numbers. This means it can optimise the representation, and functions
174numbers. This means it can optimise the representation, and functions
166such as ``plus`` and ``mult``.
175such as ``plus`` and ``mult``.
@@ -188,7 +197,7 @@ indented further than the outer function.
188visible in the ``where`` clause (unless they have been redefined, such
197visible in the ``where`` clause (unless they have been redefined, such
189as ``xs`` here). A name which appears only in the type will be in scope
198as ``xs`` here). A name which appears only in the type will be in scope
190in the ``where`` clause if it is a *parameter* to one of the types,
199in the ``where`` clause if it is a *parameter* to one of the types,
191−i.e. it is fixed across the entire structure.
200+i.e. it is fixed across the entire structure.
193As well as functions, ``where`` blocks can include local data
202As well as functions, ``where`` blocks can include local data
194declarations, such as the following where ``MyLT`` is not accessible
203declarations, such as the following where ``MyLT`` is not accessible
@@ -210,9 +219,9 @@ In general, functions defined in a ``where`` clause need a type
210declaration just like any top level function. However, the type
219declaration just like any top level function. However, the type
211declaration for a function ``f`` *can* be omitted if:
220declaration for a function ``f`` *can* be omitted if:
213−- ``f`` appears in the right hand side of the top level definition
222+- ``f`` appears in the right hand side of the top level definition
215−- The type of ``f`` can be completely determined from its first
224+- The type of ``f`` can be completely determined from its first
216 application
225 application
218So, for example, the following definitions are legal:
227So, for example, the following definitions are legal:
@@ -238,9 +247,9 @@ Vectors
240A 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
241−length”, conventionally called vectors in the dependent type literature.
250+length”, conventionally called vectors in the dependent type
242−They are available as part of the ``Idris`` library, by importing
251+literature. They are available as part of the ``Idris`` library, by
243−``Data.Vect``, or we can declare them as follows:
252+importing ``Data.Vect``, or we can declare them as follows:
245.. code-block:: idris
254.. code-block:: idris
@@ -249,28 +258,28 @@ They are available as part of the ``Idris`` library, by importing
249 (::) : a -> Vect k a -> Vect (S k) a
258 (::) : a -> Vect k a -> Vect (S k) a
251Note that we have used the same constructor names as for ``List``.
260Note that we have used the same constructor names as for ``List``.
252−Ad-hoc name overloading such as this is accepted by ``Idris``, provided
261+Ad-hoc name overloading such as this is accepted by ``Idris``,
253−that the names are declared in different namespaces (in practice,
262+provided that the names are declared in different namespaces (in
254−normally in different modules). Ambiguous constructor names can normally
263+practice, normally in different modules). Ambiguous constructor names
255−be resolved from context.
264+can normally be resolved from context.
257This declares a family of types, and so the form of the declaration is
266This declares a family of types, and so the form of the declaration is
258−rather different from the simple type declarations above. We explicitly
267+rather different from the simple type declarations above. We
259−state the type of the type constructor ``Vect`` — it takes a ``Nat`` and
268+explicitly state the type of the type constructor ``Vect`` — it takes
260−a type as an argument, where ``Type`` stands for the type of types. We
269+a ``Nat`` and a type as an argument, where ``Type`` stands for the
261−say that ``Vect`` is *indexed* over ``Nat`` and *parameterised* by
270+type of types. We say that ``Vect`` is *indexed* over ``Nat`` and
262−``Type``. Each constructor targets a different part of the family of
271+*parameterised* by ``Type``. Each constructor targets a different part
263−types. ``Nil`` can only be used to construct vectors with zero length,
272+of the family of types. ``Nil`` can only be used to construct vectors
264−and ``::`` to construct vectors with non-zero length. In the type of
273+with zero length, and ``::`` to construct vectors with non-zero
265−``::``, we state explicitly that an element of type ``a`` and a tail of
274+length. In the type of ``::``, we state explicitly that an element of
266−type ``Vect k a`` (i.e., a vector of length ``k``) combine to make a
275+type ``a`` and a tail of type ``Vect k a`` (i.e., a vector of length
267−vector of length ``S k``.
276+``k``) combine to make a vector of length ``S k``.
269We can define functions on dependent types such as ``Vect`` in the same
278We can define functions on dependent types such as ``Vect`` in the same
270way as on simple types such as ``List`` and ``Nat`` above, by pattern
279way as on simple types such as ``List`` and ``Nat`` above, by pattern
271matching. The type of a function over ``Vect`` will describe what
280matching. The type of a function over ``Vect`` will describe what
272happens to the lengths of the vectors involved. For example, ``++``,
281happens to the lengths of the vectors involved. For example, ``++``,
273−defined as follows, appends two ``Vect``\ s:
282+defined as follows, appends two ``Vect``:
275.. code-block:: idris
284.. code-block:: idris
@@ -308,9 +317,9 @@ following:
311−This error message suggests that there is a length mismatch between two
320+This error message suggests that there is a length mismatch between
312−vectors — we needed a vector of length ``k + m``, but provided a vector
321+two vectors — we needed a vector of length ``k + m``, but provided a
313−of length ``k + k``.
322+vector of length ``k + k``.
315The Finite Sets
324The Finite Sets
316~~~~~~~~~~~~~~~
325~~~~~~~~~~~~~~~
@@ -332,9 +341,9 @@ of elements in the set. Obviously we can’t construct an element of an
332empty set, so neither constructor targets ``Fin Z``.
341empty set, so neither constructor targets ``Fin Z``.
334A useful application of the ``Fin`` family is to represent bounded
343A useful application of the ``Fin`` family is to represent bounded
335−natural numbers. Since the first ``n`` natural numbers form a finite set
344+natural numbers. Since the first ``n`` natural numbers form a finite
336−of ``n`` elements, we can treat ``Fin n`` as the set of natural numbers
345+set of ``n`` elements, we can treat ``Fin n`` as the set of natural
337−bounded by ``n``.
346+numbers bounded by ``n``.
339For example, the following function which looks up an element in a
348For example, the following function which looks up an element in a
340``Vect``, by a bounded index given as a ``Fin n``, is defined in the
349``Vect``, by a bounded index given as a ``Fin n``, is defined in the
@@ -347,15 +356,16 @@ prelude:
347 index (FS k) (x :: xs) = index k xs
356 index (FS k) (x :: xs) = index k xs
349This function looks up a value at a given location in a vector. The
358This function looks up a value at a given location in a vector. The
350−location is bounded by the length of the vector (``n`` in each case), so
359+location is bounded by the length of the vector (``n`` in each case),
351−there is no need for a run-time bounds check. The type checker
360+so there is no need for a run-time bounds check. The type checker
352−guarantees that the location is no larger than the length of the vector.
361+guarantees that the location is no larger than the length of the
354−Note also that there is no case for ``Nil`` here. This is because it is
364+Note also that there is no case for ``Nil`` here. This is because it
355−impossible. Since there is no element of ``Fin Z``, and the location is
365+is impossible. Since there is no element of ``Fin Z``, and the
356−a ``Fin n``, then ``n`` can not be ``Z``. As a result, attempting to
366+location is a ``Fin n``, then ``n`` can not be ``Z``. As a result,
357−look up an element in an empty vector would give a compile time type
367+attempting to look up an element in an empty vector would give a
358−error, since it would force ``n`` to be ``Z``.
368+compile time type error, since it would force ``n`` to be ``Z``.
360Implicit Arguments
370Implicit Arguments
361~~~~~~~~~~~~~~~~~~
371~~~~~~~~~~~~~~~~~~
@@ -376,13 +386,14 @@ names, ``n`` and ``a``, which are not declared explicitly. These are
377 index : {a:Type} -> {n:Nat} -> Fin n -> Vect n a -> a
387 index : {a:Type} -> {n:Nat} -> Fin n -> Vect n a -> a
379−Implicit arguments, given in braces ``{}`` in the type declaration, are
389+Implicit arguments, given in braces ``{}`` in the type declaration,
380−not given in applications of ``index``; their values can be inferred
390+are not given in applications of ``index``; their values can be
381−from the types of the ``Fin n`` and ``Vect n a`` arguments. Any name
391+inferred from the types of the ``Fin n`` and ``Vect n a``
382−with a which appears as a parameter or index in a type declaration, but
392+arguments. Any name with a which appears as a parameter or index in a
383−which is otherwise free, will be automatically bound as an implicit
393+type declaration, but which is otherwise free, will be automatically
384−argument. Implicit arguments can still be given explicitly in
394+bound as an implicit argument. Implicit arguments can still be given
385−applications, using ``{a=value}`` and ``{n=value}``, for example:
395+explicitly in applications, using ``{a=value}`` and ``{n=value}``, for
387.. code-block:: idris
398.. code-block:: idris
@@ -403,10 +414,10 @@ clear.
403~~~~~~~~~~~~~~~~~~~~
414~~~~~~~~~~~~~~~~~~~~
405Sometimes it is useful to provide types of implicit arguments,
416Sometimes it is useful to provide types of implicit arguments,
406−particularly where there is a dependency ordering, or where the implicit
417+particularly where there is a dependency ordering, or where the
407−arguments themselves have dependencies. For example, we may wish to
418+implicit arguments themselves have dependencies. For example, we may
408−state the types of the implicit arguments in the following definition,
419+wish to state the types of the implicit arguments in the following
409−which defines a predicate on vectors:
420+definition, which defines a predicate on vectors:
411.. code-block:: idris
422.. code-block:: idris
@@ -414,10 +425,10 @@ which defines a predicate on vectors:
414 Here : {x:a} -> {xs:Vect n a} -> Elem x (x :: xs)
425 Here : {x:a} -> {xs:Vect n a} -> Elem x (x :: xs)
415 There : {x,y:a} -> {xs:Vect n a} -> Elem x xs -> Elem x (y :: xs)
426 There : {x,y:a} -> {xs:Vect n a} -> Elem x xs -> Elem x (y :: xs)
417−An instance of ``Elem x xs`` states that ``x`` is an element of ``xs``.
428+An instance of ``Elem x xs`` states that ``x`` is an element of
418−We can construct such a predicate if the required element is ``Here``,
429+``xs``. We can construct such a predicate if the required element is
419−at the head of the vector, or ``There``, in the tail of the vector. For
430+``Here``, at the head of the vector, or ``There``, in the tail of the
420−example:
431+vector. For example:
422.. code-block:: idris
433.. code-block:: idris
@@ -429,8 +440,8 @@ example:
430If the same implicit arguments are being used a lot, it can make a
441If the same implicit arguments are being used a lot, it can make a
431definition difficult to read. To avoid this problem, a ``using`` block
442definition difficult to read. To avoid this problem, a ``using`` block
432−gives the types and ordering of any implicit arguments which can appear
443+gives the types and ordering of any implicit arguments which can
433−within the block:
444+appear within the block:
435.. code-block:: idris
446.. code-block:: idris
@@ -445,8 +456,8 @@ Note: Declaration Order and ``mutual`` blocks
445In general, functions and data types must be defined before use, since
456In general, functions and data types must be defined before use, since
446dependent types allow functions to appear as part of types, and their
457dependent types allow functions to appear as part of types, and their
447reduction behaviour to affect type checking. However, this restriction
458reduction behaviour to affect type checking. However, this restriction
448−can be relaxed by using a ``mutual`` block, which allows data types and
459+can be relaxed by using a ``mutual`` block, which allows data types
449−functions to be defined simultaneously:
460+and functions to be defined simultaneously:
451.. code-block:: idris
462.. code-block:: idris
@@ -461,7 +472,8 @@ functions to be defined simultaneously:
462In a ``mutual`` block, first all of the type declarations are added,
473In a ``mutual`` block, first all of the type declarations are added,
463then the function bodies. As a result, none of the function types can
474then the function bodies. As a result, none of the function types can
464−depend on the reduction behaviour of any of the functions in the block.
475+depend on the reduction behaviour of any of the functions in the
@@ -477,18 +489,18 @@ side-effects — is that I/O is inherently side-effecting. Therefore in
477 data IO a -- IO operation returning a value of type a
489 data IO a -- IO operation returning a value of type a
479We’ll leave the definition of ``IO`` abstract, but effectively it
491We’ll leave the definition of ``IO`` abstract, but effectively it
480−describes what the I/O operations to be executed are, rather than how to
492+describes what the I/O operations to be executed are, rather than how
481−execute them. The resulting operations are executed externally, by the
493+to execute them. The resulting operations are executed externally, by
482−run-time system. We’ve already seen one IO program:
494+the run-time system. We’ve already seen one IO program:
484.. code-block:: idris
496.. code-block:: idris
486 main : IO ()
498 main : IO ()
487 main = putStrLn "Hello world"
499 main = putStrLn "Hello world"
489−The type of ``putStrLn`` explains that it takes a string, and returns an
501+The type of ``putStrLn`` explains that it takes a string, and returns
490−element of the unit type ``()`` via an I/O action. There is a variant
502+an element of the unit type ``()`` via an I/O action. There is a
491−``putStr`` which outputs a string without a newline:
503+variant ``putStr`` which outputs a string without a newline:
493.. code-block:: idris
505.. code-block:: idris
@@ -501,8 +513,8 @@ We can also read strings from user input:
502 getLine : IO String
514 getLine : IO String
504−A number of other I/O operations are defined in the prelude, for example
516+A number of other I/O operations are defined in the prelude, for
505−for reading and writing files, including:
517+example for reading and writing files, including:
507.. code-block:: idris
519.. code-block:: idris
@@ -523,10 +535,10 @@ for reading and writing files, including:
523“``do``” notation
535“``do``” notation
524-----------------
536-----------------
526−I/O programs will typically need to sequence actions, feeding the output
538+I/O programs will typically need to sequence actions, feeding the
527−of one computation into the input of the next. ``IO`` is an abstract
539+output of one computation into the input of the next. ``IO`` is an
528−type, however, so we can’t access the result of a computation directly.
540+abstract type, however, so we can’t access the result of a computation
529−Instead, we sequence operations with ``do`` notation:
541+directly. Instead, we sequence operations with ``do`` notation:
531.. code-block:: idris
543.. code-block:: idris
@@ -538,16 +550,16 @@ Instead, we sequence operations with ``do`` notation:
538The syntax ``x <- iovalue`` executes the I/O operation ``iovalue``, of
550The syntax ``x <- iovalue`` executes the I/O operation ``iovalue``, of
539type ``IO a``, and puts the result, of type ``a`` into the variable
551type ``IO a``, and puts the result, of type ``a`` into the variable
540``x``. In this case, ``getLine`` returns an ``IO String``, so ``name``
552``x``. In this case, ``getLine`` returns an ``IO String``, so ``name``
541−has type ``String``. Indentation is significant — each statement in the
553+has type ``String``. Indentation is significant — each statement in
542−do block must begin in the same column. The ``return`` operation allows
554+the do block must begin in the same column. The ``return`` operation
543−us to inject a value directly into an IO operation:
555+allows us to inject a value directly into an IO operation:
545.. code-block:: idris
557.. code-block:: idris
547 return : a -> IO a
559 return : a -> IO a
549−As we will see later, ``do`` notation is more general than this, and can
561+As we will see later, ``do`` notation is more general than this, and
550−be overloaded.
562+can be overloaded.
552.. _sect-lazy:
564.. _sect-lazy:
@@ -564,11 +576,11 @@ not always the best approach. Consider the following function:
564 boolCase True t e = t;
576 boolCase True t e = t;
565 boolCase False t e = e;
577 boolCase False t e = e;
567−This function uses one of the ``t`` or ``e`` arguments, but not both (in
579+This function uses one of the ``t`` or ``e`` arguments, but not both
568−fact, this is used to implement the ``if...then...else`` construct as we
580+(in fact, this is used to implement the ``if...then...else`` construct
569−will see later. We would prefer if *only* the argument which was used
581+as we will see later. We would prefer if *only* the argument which was
570−was evaluated. To achieve this, ``Idris`` provides a ``Lazy`` data type,
582+used was evaluated. To achieve this, ``Idris`` provides a ``Lazy``
571−which allows evaluation to be suspended:
583+data type, which allows evaluation to be suspended:
573.. code-block:: idris
585.. code-block:: idris
@@ -578,10 +590,10 @@ which allows evaluation to be suspended:
578 Force : Lazy a -> a
590 Force : Lazy a -> a
580A 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
581−``Force``. The ``Idris`` type checker knows about the ``Lazy`` type, and
593+``Force``. The ``Idris`` type checker knows about the ``Lazy`` type,
582−inserts conversions where necessary between ``Lazy a`` and ``a``, and
594+and inserts conversions where necessary between ``Lazy a`` and ``a``,
583−vice versa. We can therefore write ``boolCase`` as follows, without any
595+and vice versa. We can therefore write ``boolCase`` as follows,
584−explicit use of ``Force`` or ``Delay``:
596+without any explicit use of ``Force`` or ``Delay``:
586.. code-block:: idris
598.. code-block:: idris
@@ -611,19 +623,19 @@ We have already seen the ``List`` and ``Vect`` data types:
611 (::) : a -> Vect k a -> Vect (S k) a
623 (::) : a -> Vect k a -> Vect (S k) a
613Note that the constructor names are the same for each — constructor
625Note that the constructor names are the same for each — constructor
614−names (in fact, names in general) can be overloaded, provided that they
626+names (in fact, names in general) can be overloaded, provided that
615−are declared in different namespaces (see Section :ref:`sect-namespaces`),
627+they are declared in different namespaces (see Section
616−and will typically be resolved according to their type. As syntactic
628+:ref:`sect-namespaces`), and will typically be resolved according to
617−sugar, any type with the constructor names ``Nil`` and ``::`` can be
629+their type. As syntactic sugar, any type with the constructor names
618−written in list form. For example:
630+``Nil`` and ``::`` can be written in list form. For example:
620- ``[]`` means ``Nil``
632- ``[]`` means ``Nil``
622- ``[1,2,3]`` means ``1 :: 2 :: 3 :: Nil``
634- ``[1,2,3]`` means ``1 :: 2 :: 3 :: Nil``
624The library also defines a number of functions for manipulating these
636The library also defines a number of functions for manipulating these
625−types. ``map`` is overloaded both for ``List`` and ``Vect`` and applies
637+types. ``map`` is overloaded both for ``List`` and ``Vect`` and
626−a function to every element of the list or vector.
638+applies a function to every element of the list or vector.
628.. code-block:: idris
640.. code-block:: idris
@@ -667,8 +679,8 @@ directory. For more details of the functions available on ``List`` and
667- ``libs/base/Data/VectType.idr``
679- ``libs/base/Data/VectType.idr``
669Functions include filtering, appending, reversing, and so on. Also
681Functions include filtering, appending, reversing, and so on. Also
670−remember that ``Idris`` is still in development, so if you don’t see the
682+remember that ``Idris`` is still in development, so if you don’t see
671−function you need, please feel free to add it and submit a patch!
683+the function you need, please feel free to add it and submit a patch!
673Aside: Anonymous functions and operator sections
685Aside: Anonymous functions and operator sections
674~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
686~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -684,18 +696,18 @@ would be to use an anonymous function:
684The notation ``\x => val`` constructs an anonymous function which takes
696The notation ``\x => val`` constructs an anonymous function which takes
685one argument, ``x`` and returns the expression ``val``. Anonymous
697one argument, ``x`` and returns the expression ``val``. Anonymous
686functions may take several arguments, separated by commas,
698functions may take several arguments, separated by commas,
687−e.g. \ ``\x, y, z => val``. Arguments may also be given explicit types,
699+e.g. ``\x, y, z => val``. Arguments may also be given explicit types,
688−e.g. \ ``\x : Int => x * 2``, and can pattern match,
700+e.g. ``\x : Int => x * 2``, and can pattern match,
689−e.g. \ ``\(x, y) => x + y``. We could also use an operator section:
701+e.g. ``\(x, y) => x + y``. We could also use an operator section:
693 *usefultypes> show (map (* 2) intVec)
705 *usefultypes> show (map (* 2) intVec)
694 "[2, 4, 6, 8, 10]" : String
706 "[2, 4, 6, 8, 10]" : String
696−``(*2)`` is shorthand for a function which multiplies a number by 2. It
708+``(*2)`` is shorthand for a function which multiplies a number
697−expands to ``\x => x * 2``. Similarly, ``(2*)`` would expand to
709+by 2. It expands to ``\x => x * 2``. Similarly, ``(2*)`` would expand
698−``\x => 2 * x``.
710+to ``\x => 2 * x``.
@@ -707,9 +719,9 @@ given type, or there isn’t:
708 data Maybe a = Just a | Nothing
720 data Maybe a = Just a | Nothing
710−``Maybe`` is one way of giving a type to an operation that may fail. For
722+``Maybe`` is one way of giving a type to an operation that may
711−example, looking something up in a ``List`` (rather than a vector) may
723+fail. For example, looking something up in a ``List`` (rather than a
712−result in an out of bounds error:
724+vector) may result in an out of bounds error:
714.. code-block:: idris
726.. code-block:: idris
@@ -740,9 +752,9 @@ Values can be paired with the following built-in data type:
741 data Pair a b = MkPair a b
753 data Pair a b = MkPair a b
743−As syntactic sugar, we can write ``(a, b)`` which, according to context,
755+As syntactic sugar, we can write ``(a, b)`` which, according to
744−means either ``Pair a b`` or ``MkPair a b``. Tuples can contain an
756+context, means either ``Pair a b`` or ``MkPair a b``. Tuples can
745−arbitrary number of values, represented as nested pairs:
757+contain an arbitrary number of values, represented as nested pairs:
747.. code-block:: idris
759.. code-block:: idris
@@ -766,8 +778,8 @@ as “sigma types”:
767Again, there is syntactic sugar for this. ``(a : A ** P)`` is the type
779Again, there is syntactic sugar for this. ``(a : A ** P)`` is the type
768of a pair of A and P, where the name ``a`` can occur inside ``P``.
780of a pair of A and P, where the name ``a`` can occur inside ``P``.
769−``( a ** p )`` constructs a value of this type. For example, we can pair
781+``( a ** p )`` constructs a value of this type. For example, we can
770−a number with a ``Vect`` of a particular length.
782+pair a number with a ``Vect`` of a particular length.
772.. code-block:: idris
784.. code-block:: idris
@@ -783,27 +795,28 @@ equivalent.
783 vec = MkSigma 2 [3, 4]
795 vec = MkSigma 2 [3, 4]
785The type checker could of course infer the value of the first element
797The type checker could of course infer the value of the first element
786−from the length of the vector. We can write an underscore ``_`` in place
798+from the length of the vector. We can write an underscore ``_`` in
787−of values which we expect the type checker to fill in, so the above
799+place of values which we expect the type checker to fill in, so the
788−definition could also be written as:
800+above definition could also be written as:
790.. code-block:: idris
802.. code-block:: idris
792 vec : (n : Nat ** Vect n Int)
804 vec : (n : Nat ** Vect n Int)
793 vec = (_ ** [3, 4])
805 vec = (_ ** [3, 4])
795−We might also prefer to omit the type of the first element of the pair,
807+We might also prefer to omit the type of the first element of the
796−since, again, it can be inferred:
808+pair, since, again, it can be inferred:
798.. code-block:: idris
810.. code-block:: idris
800 vec : (n ** Vect n Int)
812 vec : (n ** Vect n Int)
801 vec = (_ ** [3, 4])
813 vec = (_ ** [3, 4])
803−One use for dependent pairs is to return values of dependent types where
815+One use for dependent pairs is to return values of dependent types
804−the index is not necessarily known in advance. For example, if we filter
816+where the index is not necessarily known in advance. For example, if
805−elements out of a ``Vect`` according to some predicate, we will not know
817+we filter elements out of a ``Vect`` according to some predicate, we
806−in advance what the length of the resulting vector will be:
818+will not know in advance what the length of the resulting vector will
808.. code-block:: idris
821.. code-block:: idris
@@ -815,9 +828,9 @@ If the ``Vect`` is empty, the result is easy:
816 filter p Nil = (_ ** [])
829 filter p Nil = (_ ** [])
818−In the ``::`` case, we need to inspect the result of a recursive call to
831+In the ``::`` case, we need to inspect the result of a recursive call
819−``filter`` to extract the length and the vector from the result. To do
832+to ``filter`` to extract the length and the vector from the result. To
820−this, we use ``with`` notation, which allows pattern matching on
833+do this, we use ``with`` notation, which allows pattern matching on
821intermediate values:
834intermediate values:
823.. code-block:: idris
836.. code-block:: idris
@@ -849,9 +862,9 @@ Intermediate values can be calculated using ``let`` bindings:
849 splitAt c x = case break (== c) x of
862 splitAt c x = case break (== c) x of
850 (x, y) => (x, strTail y)
863 (x, y) => (x, strTail y)
852−We can do simple pattern matching in ``let`` bindings too. For example,
865+We can do simple pattern matching in ``let`` bindings too. For
853−we can extract fields from a record as follows, as well as by pattern
866+example, we can extract fields from a record as follows, as well as by
854−matching at the top level:
867+pattern matching at the top level:
856.. code-block:: idris
869.. code-block:: idris
@@ -872,9 +885,9 @@ for building lists. The general form is:
872 [ expression | qualifiers ]
885 [ expression | qualifiers ]
874This generates the list of values produced by evaluating the
887This generates the list of values produced by evaluating the
875−``expression``, according to the conditions given by the comma separated
888+``expression``, according to the conditions given by the comma
876−``qualifiers``. For example, we can build a list of Pythagorean triples
889+separated ``qualifiers``. For example, we can build a list of
877−as follows:
890+Pythagorean triples as follows:
879.. code-block:: idris
892.. code-block:: idris
@@ -884,16 +897,16 @@ as follows:
885The ``[a..b]`` notation is another shorthand which builds a list of
898The ``[a..b]`` notation is another shorthand which builds a list of
886numbers between ``a`` and ``b``. Alternatively ``[a,b..c]`` builds a
899numbers between ``a`` and ``b``. Alternatively ``[a,b..c]`` builds a
887−list of numbers between ``a`` and ``c`` with the increment specified by
900+list of numbers between ``a`` and ``c`` with the increment specified
888−the difference between ``a`` and ``b``. This works for any numeric type,
901+by the difference between ``a`` and ``b``. This works for any numeric
889−using the ``count`` function from the prelude.
902+type, using the ``count`` function from the prelude.
891``case`` expressions
904``case`` expressions
892~~~~~~~~~~~~~~~~~~~~
905~~~~~~~~~~~~~~~~~~~~
894Another way of inspecting intermediate values of *simple* types is to
907Another way of inspecting intermediate values of *simple* types is to
895−use a ``case`` expression. The following function, for example, splits a
908+use a ``case`` expression. The following function, for example, splits
896−string into two at a given character:
909+a string into two at a given character:
898.. code-block:: idris
911.. code-block:: idris
@@ -907,10 +920,11 @@ deconstruct the pair it returns, and remove the first character of the
907second string.
920second string.
909A ``case`` expression can match several cases, for example, to inspect
922A ``case`` expression can match several cases, for example, to inspect
910−an intermediate value of type ``Maybe a``. Recall ``list_lookup`` which
923+an intermediate value of type ``Maybe a``. Recall ``list_lookup``
911−looks up an index in a list, returning ``Nothing`` if the index is out
924+which looks up an index in a list, returning ``Nothing`` if the index
912−of bounds. We can use this to write ``lookup_default``, which looks up
925+is out of bounds. We can use this to write ``lookup_default``, which
913−an index and returns a default value if the index is out of bounds:
926+looks up an index and returns a default value if the index is out of
915.. code-block:: idris
929.. code-block:: idris
@@ -919,8 +933,8 @@ an index and returns a default value if the index is out of bounds:
919 Nothing => def
933 Nothing => def
920 Just x => x
934 Just x => x
922−If the index is in bounds, we get the value at that index, otherwise we
936+If the index is in bounds, we get the value at that index, otherwise
923−get a default value:
937+we get a default value:
@@ -929,15 +943,15 @@ get a default value:
929 *usefultypes> lookup_default 4 [3,4,5,6] (-1)
943 *usefultypes> lookup_default 4 [3,4,5,6] (-1)
930 -1 : Integer
944 -1 : Integer
932−**Restrictions:** The ``case`` construct is intended for simple analysis
946+**Restrictions:** The ``case`` construct is intended for simple
933−of intermediate expressions to avoid the need to write auxiliary
947+analysis of intermediate expressions to avoid the need to write
934−functions, and is also used internally to implement pattern matching
948+auxiliary functions, and is also used internally to implement pattern
935−``let`` and lambda bindings. It will *only* work if:
949+matching ``let`` and lambda bindings. It will *only* work if:
937- Each branch *matches* a value of the same type, and *returns* a value
951- Each branch *matches* a value of the same type, and *returns* a value
938 of the same type.
952 of the same type.
940−- The type of the result is “known”. i.e. the type of the expression
954+- The type of the result is “known”. i.e. the type of the expression
941 can be determined *without* type checking the ``case``-expression
955 can be determined *without* type checking the ``case``-expression
@@ -946,8 +960,8 @@ Dependent Records
947*Records* are data types which collect several values (the record’s
961*Records* are data types which collect several values (the record’s
948*fields*) together. ``Idris`` provides syntax for defining records and
962*fields*) together. ``Idris`` provides syntax for defining records and
949−automatically generating field access and update functions. For example,
963+*automatically generating field access and update functions. For
950−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:
952.. code-block:: idris
966.. code-block:: idris
@@ -958,10 +972,11 @@ we can represent a person’s name and age in a record:
958 fred : Person
972 fred : Person
959 fred = MkPerson "Fred" 30
973 fred = MkPerson "Fred" 30
961−Record declarations are like ``data`` declarations, except that they are
975+Record declarations are like ``data`` declarations, except that they
962−introduced by the ``record`` keyword, and can only have one constructor.
976+are introduced by the ``record`` keyword, and can only have one
963−The names of the binders in the constructor type (``name`` and ``age``)
977+constructor. The names of the binders in the constructor type
964−here are the field names, which we can use to access the field values:
978+(``name`` and ``age``) here are the field names, which we can use to
979+access the field values:
@@ -972,8 +987,8 @@ here are the field names, which we can use to access the field values:
972 *record> :t name
987 *record> :t name
973 name : Person -> String
988 name : Person -> String
975−We can also use the field names to update a record (or, more precisely,
990+We can also use the field names to update a record (or, more
976−produce a new record with the given fields updated).
991+precisely, produce a new record with the given fields updated).
978.. code-block:: bash
993.. code-block:: bash
@@ -1024,9 +1039,9 @@ syntax:
1024 record { a->b->c = val } x
1039 record { a->b->c = val } x
1026This returns a new record, with the field accessed by the path
1041This returns a new record, with the field accessed by the path
1027−``a->b->c`` set to ``x``. The syntax is first class, i.e.
1042+``a->b->c`` set to ``x``. The syntax is first class, i.e. ``record {
1028−``record { a->b->c = val }`` itself has a function type. Symmetrically,
1043+a->b->c = val }`` itself has a function type. Symmetrically, the field
1029−the field can also be accessed with the following syntax:
1044+can also be accessed with the following syntax:
1031.. code-block:: idris
1046.. code-block:: idris