@@ -1,3 +1,5 @@
1+.. _sect-typefuns:
2+
13===================
24Types and Functions
35===================
@@ -11,8 +13,8 @@ manipulation, and ``Ptr`` which represents foreign pointers. There are
1113also several data types declared in the library, including ``Bool``,
1214with values ``True`` and ``False``. We can declare some constants with
1315these types. Enter the following into a file ``prims.idr`` and load it
14−into the ``Idris`` interactive environment by typing
15−``idris prims.idr``:
16+into the ``Idris`` interactive environment by typing ``idris
17+prims.idr``:
1618
1719.. code-block:: idris
1820
@@ -34,13 +36,14 @@ An ``Idris`` file consists of an optional module declaration (here
3436``module prims``) followed by an optional list of imports (none here,
3537however ``Idris`` programs can consist of several modules, and the
3638definitions in each module each have their own namespace, as we will
37−discuss in Section :ref:`sect-namespaces`) and a collection of declarations
38−and definitions. The order of definitions is significant — functions and
39−data types must be defined before use. Each definition must have a type
40−declaration, for example see ``x : Int``, ``foo : String``, from the
41−above listing. Indentation is significant — a new declaration begins at
42−the same level of indentation as the preceding declaration.
43−Alternatively, declarations may be terminated with a semicolon.
39+discuss in Section :ref:`sect-namespaces`) and a collection of
40+declarations and definitions. The order of definitions is significant
41+— functions and data types must be defined before use. Each definition
42+must have a type declaration, for example see ``x : Int``, ``foo :
43+String``, from the above listing. Indentation is significant — a new
44+declaration begins at the same level of indentation as the preceding
45+declaration. Alternatively, declarations may be terminated with a
46+semicolon.
4447
4548A library module ``prelude`` is automatically imported by every
4649``Idris`` program, including facilities for IO, arithmetic, data
@@ -56,10 +59,10 @@ answer. For example:
5659 *prims> x == 6*6+6
5760 True : Bool
5861
59−All of the usual arithmetic and comparison operators are defined for the
60−primitive types. They are overloaded using type classes, as we will
61−discuss in Section :ref:`sect-classes` and can be extended to work on user
62−defined types. Boolean expressions can be tested with the
62+All of the usual arithmetic and comparison operators are defined for
63+the primitive types. They are overloaded using type classes, as we
64+will discuss in Section :ref:`sect-classes` and can be extended to
65+work on user defined types. Boolean expressions can be tested with the
6366``if...then...else`` construct:
6467
6568::
@@ -71,8 +74,8 @@ Data Types
7174----------
7275
7376Data 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
75−as follows:
77+similar syntax. Natural numbers and lists, for example, can be
78+declared as follows:
7679
7780.. code-block:: idris
7881
@@ -81,19 +84,21 @@ as follows:
8184 data List a = Nil | (::) a (List a) -- Polymorphic lists
8285
8386The above declarations are taken from the standard library. Unary
84−natural numbers can be either zero (``Z``), or the successor of another
85−natural number (``S k``). Lists can either be empty (``Nil``) or a value
86−added to the front of another list (``x :: xs``). In the declaration for
87−``List``, we used an infix operator ``::``. New operators such as this
88−can be added using a fixity declaration, as follows:
87+natural numbers can be either zero (``Z``), or the successor of
88+another natural number (``S k``). Lists can either be empty (``Nil``)
89+or a value added to the front of another list (``x :: xs``). In the
90+declaration for ``List``, we used an infix operator ``::``. New
91+operators such as this can be added using a fixity declaration, as
92+follows:
8993
9094::
9195
9296 infixr 10 ::
9397
9498Functions, data constructors and type constructors may all be given
95−infix operators as names. They may be used in prefix form if enclosed in
96−brackets, e.g. \ ``(::)``. Infix operators can use any of the symbols:
99+infix operators as names. They may be used in prefix form if enclosed
100+in brackets, e.g. ``(::)``. Infix operators can use any of the
101+symbols:
97102
98103::
99104
@@ -104,9 +109,10 @@ Functions
104109
105110Functions are implemented by pattern matching, again using a similar
106111syntax to Haskell. The main difference is that ``Idris`` requires type
107−declarations for all functions, using a single colon ``:`` (rather than
108−Haskell’s double colon ``::``). Some natural number arithmetic functions
109−can be defined as follows, again taken from the standard library:
112+declarations for all functions, using a single colon ``:`` (rather
113+than Haskell’s double colon ``::``). Some natural number arithmetic
114+functions can be defined as follows, again taken from the standard
115+library:
110116
111117.. code-block:: idris
112118
@@ -123,10 +129,11 @@ can be defined as follows, again taken from the standard library:
123129The standard arithmetic operators ``+`` and ``*`` are also overloaded
124130for use by ``Nat``, and are implemented using the above functions.
125131Unlike Haskell, there is no restriction on whether types and function
126−names must begin with a capital letter or not. Function names (``plus``
127−and ``mult`` above), data constructors (``Z``, ``S``, ``Nil`` and
128−``::``) and type constructors (``Nat`` and ``List``) are all part of the
129−same namespace. We can test these functions at the ``Idris`` prompt:
132+names must begin with a capital letter or not. Function names
133+(``plus`` and ``mult`` above), data constructors (``Z``, ``S``,
134+``Nil`` and ``::``) and type constructors (``Nat`` and ``List``) are
135+all part of the same namespace. We can test these functions at the
136+``Idris`` prompt:
130137
131138::
132139
@@ -135,10 +142,12 @@ same namespace. We can test these functions at the ``Idris`` prompt:
135142 Idris> mult (S (S (S Z))) (plus (S (S Z)) (S (S Z)))
136143 12 : Nat
137144
138−**Note:** ``Idris`` automatically desugars the ``Nat`` representation
139−into a more human readable format. The result of
140−``plus (S (S Z)) (S (S Z))`` is actually ``(S (S (S (S Z))))`` which is
141−the Integer 4. This can be checked at the ``Idris`` prompt:
145+.. note::
146+
147+ ``Idris`` automatically desugars the ``Nat`` representation into a
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
150+ checked at the ``Idris`` prompt:
142151
143152::
144153
@@ -156,12 +165,12 @@ type classes, meaning that we can also test the functions as follows:
156165 12 : Nat
157166
158167You 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
160−primarily that unary numbers have a very convenient structure which is
161−easy to reason about, and easy to relate to other data structures as we
162−will see later. Nevertheless, we do not want this convenience to be at
163−the expense of efficiency. Fortunately, ``Idris`` knows about the
164−relationship between ``Nat`` (and similarly structured types) and
168+computers have perfectly good integer arithmetic built in. The reason
169+is primarily that unary numbers have a very convenient structure which
170+is easy to reason about, and easy to relate to other data structures
171+as we will see later. Nevertheless, we do not want this convenience to
172+be at the expense of efficiency. Fortunately, ``Idris`` knows about
173+the relationship between ``Nat`` (and similarly structured types) and
165174numbers. This means it can optimise the representation, and functions
166175such as ``plus`` and ``mult``.
167176
@@ -188,7 +197,7 @@ indented further than the outer function.
188197visible in the ``where`` clause (unless they have been redefined, such
189198as ``xs`` here). A name which appears only in the type will be in scope
190199in 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.
192201
193202As well as functions, ``where`` blocks can include local data
194203declarations, such as the following where ``MyLT`` is not accessible
@@ -210,9 +219,9 @@ In general, functions defined in a ``where`` clause need a type
210219declaration just like any top level function. However, the type
211220declaration for a function ``f`` *can* be omitted if:
212221
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
214223
215−- The type of ``f`` can be completely determined from its first
224+- The type of ``f`` can be completely determined from its first
216225 application
217226
218227So, for example, the following definitions are legal:
@@ -238,9 +247,9 @@ Vectors
238247~~~~~~~
239248
240249A standard example of a dependent type is the type of “lists with
241−length”, conventionally called vectors in the dependent type literature.
242−They are available as part of the ``Idris`` library, by importing
243−``Data.Vect``, or we can declare them as follows:
250+length”, conventionally called vectors in the dependent type
251+literature. They are available as part of the ``Idris`` library, by
252+importing ``Data.Vect``, or we can declare them as follows:
244253
245254.. code-block:: idris
246255
@@ -249,28 +258,28 @@ They are available as part of the ``Idris`` library, by importing
249258 (::) : a -> Vect k a -> Vect (S k) a
250259
251260Note that we have used the same constructor names as for ``List``.
252−Ad-hoc name overloading such as this is accepted by ``Idris``, provided
253−that the names are declared in different namespaces (in practice,
254−normally in different modules). Ambiguous constructor names can normally
255−be resolved from context.
261+Ad-hoc name overloading such as this is accepted by ``Idris``,
262+provided that the names are declared in different namespaces (in
263+practice, normally in different modules). Ambiguous constructor names
264+can normally be resolved from context.
256265
257266This declares a family of types, and so the form of the declaration is
258−rather different from the simple type declarations above. We explicitly
259−state the type of the type constructor ``Vect`` — it takes a ``Nat`` and
260−a type as an argument, where ``Type`` stands for the type of types. We
261−say that ``Vect`` is *indexed* over ``Nat`` and *parameterised* by
262−``Type``. Each constructor targets a different part of the family of
263−types. ``Nil`` can only be used to construct vectors with zero length,
264−and ``::`` to construct vectors with non-zero length. In the type of
265−``::``, we state explicitly that an element of type ``a`` and a tail of
266−type ``Vect k a`` (i.e., a vector of length ``k``) combine to make a
267−vector of length ``S k``.
267+rather different from the simple type declarations above. We
268+explicitly state the type of the type constructor ``Vect`` — it takes
269+a ``Nat`` and a type as an argument, where ``Type`` stands for the
270+type of types. We say that ``Vect`` is *indexed* over ``Nat`` and
271+*parameterised* by ``Type``. Each constructor targets a different part
272+of the family of types. ``Nil`` can only be used to construct vectors
273+with zero length, and ``::`` to construct vectors with non-zero
274+length. In the type of ``::``, we state explicitly that an element of
275+type ``a`` and a tail of type ``Vect k a`` (i.e., a vector of length
276+``k``) combine to make a vector of length ``S k``.
268277
269278We can define functions on dependent types such as ``Vect`` in the same
270279way as on simple types such as ``List`` and ``Nat`` above, by pattern
271280matching. The type of a function over ``Vect`` will describe what
272281happens to the lengths of the vectors involved. For example, ``++``,
273−defined as follows, appends two ``Vect``\ s:
282+defined as follows, appends two ``Vect``:
274283
275284.. code-block:: idris
276285
@@ -308,9 +317,9 @@ following:
308317 with
309318 plus k m
310319
311−This error message suggests that there is a length mismatch between two
312−vectors — we needed a vector of length ``k + m``, but provided a vector
313−of length ``k + k``.
320+This error message suggests that there is a length mismatch between
321+two vectors — we needed a vector of length ``k + m``, but provided a
322+vector of length ``k + k``.
314323
315324The Finite Sets
316325~~~~~~~~~~~~~~~
@@ -332,9 +341,9 @@ of elements in the set. Obviously we can’t construct an element of an
332341empty set, so neither constructor targets ``Fin Z``.
333342
334343A useful application of the ``Fin`` family is to represent bounded
335−natural numbers. Since the first ``n`` natural numbers form a finite set
336−of ``n`` elements, we can treat ``Fin n`` as the set of natural numbers
337−bounded by ``n``.
344+natural 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``.
338347
339348For example, the following function which looks up an element in a
340349``Vect``, by a bounded index given as a ``Fin n``, is defined in the
@@ -347,15 +356,16 @@ prelude:
347356 index (FS k) (x :: xs) = index k xs
348357
349358This 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
351−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.
359+location is bounded by the length of the vector (``n`` in each case),
360+so there is no need for a run-time bounds check. The type checker
361+guarantees that the location is no larger than the length of the
362+vector.
353363
354−Note also that there is no case for ``Nil`` here. This is because it is
355−impossible. Since there is no element of ``Fin Z``, and the location is
356−a ``Fin n``, then ``n`` can not be ``Z``. As a result, attempting to
357−look up an element in an empty vector would give a compile time type
358−error, since it would force ``n`` to be ``Z``.
364+Note also that there is no case for ``Nil`` here. This is because it
365+is impossible. Since there is no element of ``Fin Z``, and the
366+location is a ``Fin n``, then ``n`` can not be ``Z``. As a result,
367+attempting to look up an element in an empty vector would give a
368+compile time type error, since it would force ``n`` to be ``Z``.
359369
360370Implicit Arguments
361371~~~~~~~~~~~~~~~~~~
@@ -376,13 +386,14 @@ names, ``n`` and ``a``, which are not declared explicitly. These are
376386
377387 index : {a:Type} -> {n:Nat} -> Fin n -> Vect n a -> a
378388
379−Implicit arguments, given in braces ``{}`` in the type declaration, are
380−not given in applications of ``index``; their values can be inferred
381−from the types of the ``Fin n`` and ``Vect n a`` arguments. Any name
382−with a which appears as a parameter or index in a type declaration, but
383−which is otherwise free, will be automatically bound as an implicit
384−argument. Implicit arguments can still be given explicitly in
385−applications, using ``{a=value}`` and ``{n=value}``, for example:
389+Implicit arguments, given in braces ``{}`` in the type declaration,
390+are not given in applications of ``index``; their values can be
391+inferred from the types of the ``Fin n`` and ``Vect n a``
392+arguments. Any name with a which appears as a parameter or index in a
393+type declaration, but which is otherwise free, will be automatically
394+bound as an implicit argument. Implicit arguments can still be given
395+explicitly in applications, using ``{a=value}`` and ``{n=value}``, for
396+example:
386397
387398.. code-block:: idris
388399
@@ -403,10 +414,10 @@ clear.
403414~~~~~~~~~~~~~~~~~~~~
404415
405416Sometimes it is useful to provide types of implicit arguments,
406−particularly where there is a dependency ordering, or where the implicit
407−arguments themselves have dependencies. For example, we may wish to
408−state the types of the implicit arguments in the following definition,
409−which defines a predicate on vectors:
417+particularly where there is a dependency ordering, or where the
418+implicit arguments themselves have dependencies. For example, we may
419+wish to state the types of the implicit arguments in the following
420+definition, which defines a predicate on vectors:
410421
411422.. code-block:: idris
412423
@@ -414,10 +425,10 @@ which defines a predicate on vectors:
414425 Here : {x:a} -> {xs:Vect n a} -> Elem x (x :: xs)
415426 There : {x,y:a} -> {xs:Vect n a} -> Elem x xs -> Elem x (y :: xs)
416427
417−An instance of ``Elem x xs`` states that ``x`` is an element of ``xs``.
418−We can construct such a predicate if the required element is ``Here``,
419−at the head of the vector, or ``There``, in the tail of the vector. For
420−example:
428+An instance of ``Elem x xs`` states that ``x`` is an element of
429+``xs``. We can construct such a predicate if the required element is
430+``Here``, at the head of the vector, or ``There``, in the tail of the
431+vector. For example:
421432
422433.. code-block:: idris
423434
@@ -429,8 +440,8 @@ example:
429440
430441If the same implicit arguments are being used a lot, it can make a
431442definition difficult to read. To avoid this problem, a ``using`` block
432−gives the types and ordering of any implicit arguments which can appear
433−within the block:
443+gives the types and ordering of any implicit arguments which can
444+appear within the block:
434445
435446.. code-block:: idris
436447
@@ -445,8 +456,8 @@ Note: Declaration Order and ``mutual`` blocks
445456In general, functions and data types must be defined before use, since
446457dependent types allow functions to appear as part of types, and their
447458reduction behaviour to affect type checking. However, this restriction
448−can be relaxed by using a ``mutual`` block, which allows data types and
449−functions to be defined simultaneously:
459+can be relaxed by using a ``mutual`` block, which allows data types
460+and functions to be defined simultaneously:
450461
451462.. code-block:: idris
452463
@@ -461,7 +472,8 @@ functions to be defined simultaneously:
461472
462473In a ``mutual`` block, first all of the type declarations are added,
463474then 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
476+block.
465477
466478I/O
467479---
@@ -477,18 +489,18 @@ side-effects — is that I/O is inherently side-effecting. Therefore in
477489 data IO a -- IO operation returning a value of type a
478490
479491We’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
481−execute them. The resulting operations are executed externally, by the
482−run-time system. We’ve already seen one IO program:
492+describes what the I/O operations to be executed are, rather than how
493+to execute them. The resulting operations are executed externally, by
494+the run-time system. We’ve already seen one IO program:
483495
484496.. code-block:: idris
485497
486498 main : IO ()
487499 main = putStrLn "Hello world"
488500
489−The type of ``putStrLn`` explains that it takes a string, and returns an
490−element of the unit type ``()`` via an I/O action. There is a variant
491−``putStr`` which outputs a string without a newline:
501+The type of ``putStrLn`` explains that it takes a string, and returns
502+an element of the unit type ``()`` via an I/O action. There is a
503+variant ``putStr`` which outputs a string without a newline:
492504
493505.. code-block:: idris
494506
@@ -501,8 +513,8 @@ We can also read strings from user input:
501513
502514 getLine : IO String
503515
504−A number of other I/O operations are defined in the prelude, for example
505−for reading and writing files, including:
516+A number of other I/O operations are defined in the prelude, for
517+example for reading and writing files, including:
506518
507519.. code-block:: idris
508520
@@ -523,10 +535,10 @@ for reading and writing files, including:
523535“``do``” notation
524536-----------------
525537
526−I/O programs will typically need to sequence actions, feeding the output
527−of one computation into the input of the next. ``IO`` is an abstract
528−type, however, so we can’t access the result of a computation directly.
529−Instead, we sequence operations with ``do`` notation:
538+I/O programs will typically need to sequence actions, feeding the
539+output of one computation into the input of the next. ``IO`` is an
540+abstract type, however, so we can’t access the result of a computation
541+directly. Instead, we sequence operations with ``do`` notation:
530542
531543.. code-block:: idris
532544
@@ -538,16 +550,16 @@ Instead, we sequence operations with ``do`` notation:
538550The syntax ``x <- iovalue`` executes the I/O operation ``iovalue``, of
539551type ``IO a``, and puts the result, of type ``a`` into the variable
540552``x``. In this case, ``getLine`` returns an ``IO String``, so ``name``
541−has type ``String``. Indentation is significant — each statement in the
542−do block must begin in the same column. The ``return`` operation allows
543−us to inject a value directly into an IO operation:
553+has type ``String``. Indentation is significant — each statement in
554+the do block must begin in the same column. The ``return`` operation
555+allows us to inject a value directly into an IO operation:
544556
545557.. code-block:: idris
546558
547559 return : a -> IO a
548560
549−As we will see later, ``do`` notation is more general than this, and can
550−be overloaded.
561+As we will see later, ``do`` notation is more general than this, and
562+can be overloaded.
551563
552564.. _sect-lazy:
553565
@@ -564,11 +576,11 @@ not always the best approach. Consider the following function:
564576 boolCase True t e = t;
565577 boolCase False t e = e;
566578
567−This function uses one of the ``t`` or ``e`` arguments, but not both (in
568−fact, this is used to implement the ``if...then...else`` construct as we
569−will see later. We would prefer if *only* the argument which was used
570−was evaluated. To achieve this, ``Idris`` provides a ``Lazy`` data type,
571−which allows evaluation to be suspended:
579+This 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
581+as we will see later. We would prefer if *only* the argument which was
582+used was evaluated. To achieve this, ``Idris`` provides a ``Lazy``
583+data type, which allows evaluation to be suspended:
572584
573585.. code-block:: idris
574586
@@ -578,10 +590,10 @@ which allows evaluation to be suspended:
578590 Force : Lazy a -> a
579591
580592A value of type ``Lazy a`` is unevaluated until it is forced by
581−``Force``. The ``Idris`` type checker knows about the ``Lazy`` type, and
582−inserts conversions where necessary between ``Lazy a`` and ``a``, and
583−vice versa. We can therefore write ``boolCase`` as follows, without any
584−explicit use of ``Force`` or ``Delay``:
593+``Force``. The ``Idris`` type checker knows about the ``Lazy`` type,
594+and inserts conversions where necessary between ``Lazy a`` and ``a``,
595+and vice versa. We can therefore write ``boolCase`` as follows,
596+without any explicit use of ``Force`` or ``Delay``:
585597
586598.. code-block:: idris
587599
@@ -611,19 +623,19 @@ We have already seen the ``List`` and ``Vect`` data types:
611623 (::) : a -> Vect k a -> Vect (S k) a
612624
613625Note that the constructor names are the same for each — constructor
614−names (in fact, names in general) can be overloaded, provided that they
615−are declared in different namespaces (see Section :ref:`sect-namespaces`),
616−and will typically be resolved according to their type. As syntactic
617−sugar, any type with the constructor names ``Nil`` and ``::`` can be
618−written in list form. For example:
626+names (in fact, names in general) can be overloaded, provided that
627+they are declared in different namespaces (see Section
628+:ref:`sect-namespaces`), and will typically be resolved according to
629+their type. As syntactic sugar, any type with the constructor names
630+``Nil`` and ``::`` can be written in list form. For example:
619631
620632- ``[]`` means ``Nil``
621633
622634- ``[1,2,3]`` means ``1 :: 2 :: 3 :: Nil``
623635
624636The library also defines a number of functions for manipulating these
625−types. ``map`` is overloaded both for ``List`` and ``Vect`` and applies
626−a function to every element of the list or vector.
637+types. ``map`` is overloaded both for ``List`` and ``Vect`` and
638+applies a function to every element of the list or vector.
627639
628640.. code-block:: idris
629641
@@ -667,8 +679,8 @@ directory. For more details of the functions available on ``List`` and
667679- ``libs/base/Data/VectType.idr``
668680
669681Functions include filtering, appending, reversing, and so on. Also
670−remember that ``Idris`` is still in development, so if you don’t see the
671−function you need, please feel free to add it and submit a patch!
682+remember that ``Idris`` is still in development, so if you don’t see
683+the function you need, please feel free to add it and submit a patch!
672684
673685Aside: Anonymous functions and operator sections
674686~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -684,18 +696,18 @@ would be to use an anonymous function:
684696The notation ``\x => val`` constructs an anonymous function which takes
685697one argument, ``x`` and returns the expression ``val``. Anonymous
686698functions may take several arguments, separated by commas,
687−e.g. \ ``\x, y, z => val``. Arguments may also be given explicit types,
688−e.g. \ ``\x : Int => x * 2``, and can pattern match,
689−e.g. \ ``\(x, y) => x + y``. We could also use an operator section:
699+e.g. ``\x, y, z => val``. Arguments may also be given explicit types,
700+e.g. ``\x : Int => x * 2``, and can pattern match,
701+e.g. ``\(x, y) => x + y``. We could also use an operator section:
690702
691703::
692704
693705 *usefultypes> show (map (* 2) intVec)
694706 "[2, 4, 6, 8, 10]" : String
695707
696−``(*2)`` is shorthand for a function which multiplies a number by 2. It
697−expands to ``\x => x * 2``. Similarly, ``(2*)`` would expand to
698−``\x => 2 * x``.
708+``(*2)`` is shorthand for a function which multiplies a number
709+by 2. It expands to ``\x => x * 2``. Similarly, ``(2*)`` would expand
710+to ``\x => 2 * x``.
699711
700712Maybe
701713~~~~~
@@ -707,9 +719,9 @@ given type, or there isn’t:
707719
708720 data Maybe a = Just a | Nothing
709721
710−``Maybe`` is one way of giving a type to an operation that may fail. For
711−example, looking something up in a ``List`` (rather than a vector) may
712−result in an out of bounds error:
722+``Maybe`` is one way of giving a type to an operation that may
723+fail. For example, looking something up in a ``List`` (rather than a
724+vector) may result in an out of bounds error:
713725
714726.. code-block:: idris
715727
@@ -740,9 +752,9 @@ Values can be paired with the following built-in data type:
740752
741753 data Pair a b = MkPair a b
742754
743−As syntactic sugar, we can write ``(a, b)`` which, according to context,
744−means either ``Pair a b`` or ``MkPair a b``. Tuples can contain an
745−arbitrary number of values, represented as nested pairs:
755+As syntactic sugar, we can write ``(a, b)`` which, according to
756+context, means either ``Pair a b`` or ``MkPair a b``. Tuples can
757+contain an arbitrary number of values, represented as nested pairs:
746758
747759.. code-block:: idris
748760
@@ -766,8 +778,8 @@ as “sigma types”:
766778
767779Again, there is syntactic sugar for this. ``(a : A ** P)`` is the type
768780of 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
770−a number with a ``Vect`` of a particular length.
781+``( a ** p )`` constructs a value of this type. For example, we can
782+pair a number with a ``Vect`` of a particular length.
771783
772784.. code-block:: idris
773785
@@ -783,27 +795,28 @@ equivalent.
783795 vec = MkSigma 2 [3, 4]
784796
785797The 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
787−of values which we expect the type checker to fill in, so the above
788−definition could also be written as:
798+from the length of the vector. We can write an underscore ``_`` in
799+place of values which we expect the type checker to fill in, so the
800+above definition could also be written as:
789801
790802.. code-block:: idris
791803
792804 vec : (n : Nat ** Vect n Int)
793805 vec = (_ ** [3, 4])
794806
795−We might also prefer to omit the type of the first element of the pair,
796−since, again, it can be inferred:
807+We might also prefer to omit the type of the first element of the
808+pair, since, again, it can be inferred:
797809
798810.. code-block:: idris
799811
800812 vec : (n ** Vect n Int)
801813 vec = (_ ** [3, 4])
802814
803−One use for dependent pairs is to return values of dependent types where
804−the index is not necessarily known in advance. For example, if we filter
805−elements out of a ``Vect`` according to some predicate, we will not know
806−in advance what the length of the resulting vector will be:
815+One use for dependent pairs is to return values of dependent types
816+where the index is not necessarily known in advance. For example, if
817+we filter elements out of a ``Vect`` according to some predicate, we
818+will not know in advance what the length of the resulting vector will
819+be:
807820
808821.. code-block:: idris
809822
@@ -815,9 +828,9 @@ If the ``Vect`` is empty, the result is easy:
815828
816829 filter p Nil = (_ ** [])
817830
818−In the ``::`` case, we need to inspect the result of a recursive call to
819−``filter`` to extract the length and the vector from the result. To do
820−this, we use ``with`` notation, which allows pattern matching on
831+In the ``::`` case, we need to inspect the result of a recursive call
832+to ``filter`` to extract the length and the vector from the result. To
833+do this, we use ``with`` notation, which allows pattern matching on
821834intermediate values:
822835
823836.. code-block:: idris
@@ -849,9 +862,9 @@ Intermediate values can be calculated using ``let`` bindings:
849862 splitAt c x = case break (== c) x of
850863 (x, y) => (x, strTail y)
851864
852−We can do simple pattern matching in ``let`` bindings too. For example,
853−we can extract fields from a record as follows, as well as by pattern
854−matching at the top level:
865+We can do simple pattern matching in ``let`` bindings too. For
866+example, we can extract fields from a record as follows, as well as by
867+pattern matching at the top level:
855868
856869.. code-block:: idris
857870
@@ -872,9 +885,9 @@ for building lists. The general form is:
872885 [ expression | qualifiers ]
873886
874887This generates the list of values produced by evaluating the
875−``expression``, according to the conditions given by the comma separated
876−``qualifiers``. For example, we can build a list of Pythagorean triples
877−as follows:
888+``expression``, according to the conditions given by the comma
889+separated ``qualifiers``. For example, we can build a list of
890+Pythagorean triples as follows:
878891
879892.. code-block:: idris
880893
@@ -884,16 +897,16 @@ as follows:
884897
885898The ``[a..b]`` notation is another shorthand which builds a list of
886899numbers between ``a`` and ``b``. Alternatively ``[a,b..c]`` builds a
887−list of numbers between ``a`` and ``c`` with the increment specified by
888−the difference between ``a`` and ``b``. This works for any numeric type,
889−using the ``count`` function from the prelude.
900+list of numbers between ``a`` and ``c`` with the increment specified
901+by the difference between ``a`` and ``b``. This works for any numeric
902+type, using the ``count`` function from the prelude.
890903
891904``case`` expressions
892905~~~~~~~~~~~~~~~~~~~~
893906
894907Another way of inspecting intermediate values of *simple* types is to
895−use a ``case`` expression. The following function, for example, splits a
896−string into two at a given character:
908+use a ``case`` expression. The following function, for example, splits
909+a string into two at a given character:
897910
898911.. code-block:: idris
899912
@@ -907,10 +920,11 @@ deconstruct the pair it returns, and remove the first character of the
907920second string.
908921
909922A ``case`` expression can match several cases, for example, to inspect
910−an intermediate value of type ``Maybe a``. Recall ``list_lookup`` which
911−looks up an index in a list, returning ``Nothing`` if the index is out
912−of bounds. We can use this to write ``lookup_default``, which looks up
913−an index and returns a default value if the index is out of bounds:
923+an intermediate value of type ``Maybe a``. Recall ``list_lookup``
924+which looks up an index in a list, returning ``Nothing`` if the index
925+is out of bounds. We can use this to write ``lookup_default``, which
926+looks up an index and returns a default value if the index is out of
927+bounds:
914928
915929.. code-block:: idris
916930
@@ -919,8 +933,8 @@ an index and returns a default value if the index is out of bounds:
919933 Nothing => def
920934 Just x => x
921935
922−If the index is in bounds, we get the value at that index, otherwise we
923−get a default value:
936+If the index is in bounds, we get the value at that index, otherwise
937+we get a default value:
924938
925939::
926940
@@ -929,15 +943,15 @@ get a default value:
929943 *usefultypes> lookup_default 4 [3,4,5,6] (-1)
930944 -1 : Integer
931945
932−**Restrictions:** The ``case`` construct is intended for simple analysis
933−of intermediate expressions to avoid the need to write auxiliary
934−functions, and is also used internally to implement pattern matching
935−``let`` and lambda bindings. It will *only* work if:
946+**Restrictions:** The ``case`` construct is intended for simple
947+analysis of intermediate expressions to avoid the need to write
948+auxiliary functions, and is also used internally to implement pattern
949+matching ``let`` and lambda bindings. It will *only* work if:
936950
937951- Each branch *matches* a value of the same type, and *returns* a value
938952 of the same type.
939953
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
941955 can be determined *without* type checking the ``case``-expression
942956 itself.
943957
@@ -946,8 +960,8 @@ Dependent Records
946960
947961*Records* are data types which collect several values (the record’s
948962*fields*) together. ``Idris`` provides syntax for defining records and
949−automatically generating field access and update functions. For example,
950−we can represent a person’s name and age in a record:
963+*automatically generating field access and update functions. For
964+*example, we can represent a person’s name and age in a record:
951965
952966.. code-block:: idris
953967
@@ -958,10 +972,11 @@ we can represent a person’s name and age in a record:
958972 fred : Person
959973 fred = MkPerson "Fred" 30
960974
961−Record declarations are like ``data`` declarations, except that they are
962−introduced by the ``record`` keyword, and can only have one constructor.
963−The names of the binders in the constructor type (``name`` and ``age``)
964−here are the field names, which we can use to access the field values:
975+Record declarations are like ``data`` declarations, except that they
976+are introduced by the ``record`` keyword, and can only have one
977+constructor. The names of the binders in the constructor type
978+(``name`` and ``age``) here are the field names, which we can use to
979+access the field values:
965980
966981::
967982
@@ -972,8 +987,8 @@ here are the field names, which we can use to access the field values:
972987 *record> :t name
973988 name : Person -> String
974989
975−We can also use the field names to update a record (or, more precisely,
976−produce a new record with the given fields updated).
990+We can also use the field names to update a record (or, more
991+precisely, produce a new record with the given fields updated).
977992
978993.. code-block:: bash
979994
@@ -1024,9 +1039,9 @@ syntax:
10241039 record { a->b->c = val } x
10251040
10261041This 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.
1028−``record { a->b->c = val }`` itself has a function type. Symmetrically,
1029−the field can also be accessed with the following syntax:
1042+``a->b->c`` set to ``x``. The syntax is first class, i.e. ``record {
1043+a->b->c = val }`` itself has a function type. Symmetrically, the field
1044+can also be accessed with the following syntax:
10301045
10311046.. code-block:: idris
10321047