@@ -13,8 +13,7 @@ manipulation, and ``Ptr`` which represents foreign pointers. There are
13also several data types declared in the library, including ``Bool``,
13also several data types declared in the library, including ``Bool``,
14with values ``True`` and ``False``. We can declare some constants with
14with values ``True`` and ``False``. We can declare some constants with
15these types. Enter the following into a file ``Prims.idr`` and load it
15these types. Enter the following into a file ``Prims.idr`` and load it
16−into the Idris interactive environment by typing ``idris
16+into the Idris interactive environment by typing ``idris Prims.idr``:
19.. code-block:: idris
18.. code-block:: idris
@@ -38,7 +37,7 @@ collection of declarations and definitions. In this example no imports
38have been specified. However Idris programs can consist of several
37have been specified. However Idris programs can consist of several
39modules and the definitions in each module each have their own
38modules and the definitions in each module each have their own
40namespace. This is discussed further in Section
39namespace. This is discussed further in Section
41−:ref:`sect-namespaces`). When writing Idris programs both the order in which
40+:ref:`sect-namespaces`. When writing Idris programs both the order in which
42definitions are given and indentation are significant. Functions and
41definitions are given and indentation are significant. Functions and
43data types must be defined before use, incidentally each definition must
42data types must be defined before use, incidentally each definition must
44have a type declaration, for example see ``x : Int``, ``foo :
43have a type declaration, for example see ``x : Int``, ``foo :
@@ -343,7 +342,7 @@ Vectors
344A standard example of a dependent data type is the type of “lists with
343A standard example of a dependent data type is the type of “lists with
345length”, conventionally called vectors in the dependent type
344length”, conventionally called vectors in the dependent type
346−literature. They are available as part of the Idris library, by
345+literature. They are available as part of the Idris library, by
347importing ``Data.Vect``, or we can declare them as follows:
346importing ``Data.Vect``, or we can declare them as follows:
349.. code-block:: idris
348.. code-block:: idris
@@ -521,8 +520,8 @@ help document a function by making the purpose of an argument more
523Furthermore, ``{}`` can be used to pattern match on the left hand side, i.e.
522Furthermore, ``{}`` can be used to pattern match on the left hand side, i.e.
524−``{var = pat}`` gets an implicit variable and attempts to pattern match on "pat";
523+``{var = pat}`` gets an implicit variable and attempts to pattern match on “pat”;
525−For example :
524+For example:
527.. code-block:: idris
526.. code-block:: idris
@@ -559,7 +558,6 @@ vector. For example:
559 inVect : IsElem 5 Main.testVec
558 inVect : IsElem 5 Main.testVec
560 inVect = There (There Here)
559 inVect = There (There Here)
563.. important:: Implicit Arguments and Scope
561.. important:: Implicit Arguments and Scope
565 Within the type signature the typechecker will treat all variables
563 Within the type signature the typechecker will treat all variables
@@ -569,7 +567,6 @@ vector. For example:
569 ``testVec``. In the example above, we have assumed that the code
567 ``testVec``. In the example above, we have assumed that the code
570 lives within the ``Main`` module.
568 lives within the ``Main`` module.
573If the same implicit arguments are being used a lot, it can make a
570If the same implicit arguments are being used a lot, it can make a
574definition difficult to read. To avoid this problem, a ``using`` block
571definition difficult to read. To avoid this problem, a ``using`` block
575gives the types and ordering of any implicit arguments which can
572gives the types and ordering of any implicit arguments which can
@@ -582,15 +579,13 @@ appear within the block:
582 Here : IsElem x (x :: xs)
579 Here : IsElem x (x :: xs)
583 There : IsElem x xs -> IsElem x (y :: xs)
580 There : IsElem x xs -> IsElem x (y :: xs)
587Note: Declaration Order and ``mutual`` blocks
582Note: Declaration Order and ``mutual`` blocks
588~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
583~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
590In general, functions and data types must be defined before use, since
585In general, functions and data types must be defined before use, since
591dependent types allow functions to appear as part of types, and type
586dependent types allow functions to appear as part of types, and type
592checking can rely on how particular functions are defined (though this
587checking can rely on how particular functions are defined (though this
593−is only true of total functions; see Section :ref:`sect-totality`)).
588+is only true of total functions; see Section :ref:`sect-totality`).
594However, this restriction can be relaxed by using a ``mutual`` block,
589However, this restriction can be relaxed by using a ``mutual`` block,
595which allows data types and functions to be defined simultaneously:
590which allows data types and functions to be defined simultaneously:
@@ -673,7 +668,7 @@ Note that several of these return ``Either``, since they may fail.
673I/O programs will typically need to sequence actions, feeding the
668I/O programs will typically need to sequence actions, feeding the
674output of one computation into the input of the next. ``IO`` is an
669output of one computation into the input of the next. ``IO`` is an
675abstract type, however, so we can’t access the result of a computation
670abstract type, however, so we can’t access the result of a computation
676−directly. Instead, we sequence operations with ``do`` notation:
671+directly. Instead, we sequence operations with ``do`` notation:
678.. code-block:: idris
673.. code-block:: idris
@@ -713,7 +708,7 @@ not always the best approach. Consider the following function:
714This function uses one of the ``t`` or ``e`` arguments, but not both
709This function uses one of the ``t`` or ``e`` arguments, but not both
715(in fact, this is used to implement the ``if...then...else`` construct
710(in fact, this is used to implement the ``if...then...else`` construct
716−as we will see later. We would prefer if *only* the argument which was
711+as we will see later). We would prefer if *only* the argument which was
717used was evaluated. To achieve this, Idris provides a ``Lazy``
712used was evaluated. To achieve this, Idris provides a ``Lazy``
718data type, which allows evaluation to be suspended:
713data type, which allows evaluation to be suspended:
@@ -797,7 +792,7 @@ infinite loop and cause a stack overflow.
797To fix this we must add explicit ``Inf`` declarations to the constructor
792To fix this we must add explicit ``Inf`` declarations to the constructor
798parameter types, since codata will not add it to constructor parameters of a
793parameter types, since codata will not add it to constructor parameters of a
799**different** type from the one being defined. For example, the following
794**different** type from the one being defined. For example, the following
800−outputs "1".
795+outputs ``1``.
802.. code-block:: idris
797.. code-block:: idris
@@ -902,7 +897,7 @@ For more details of the functions available on ``List`` and
903- ``libs/base/Data/VectType.idr``
898- ``libs/base/Data/VectType.idr``
905−Functions include filtering, appending, reversing, and so on.
900+Functions include filtering, appending, reversing, and so on.
907Aside: Anonymous functions and operator sections
902Aside: Anonymous functions and operator sections
908~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
903~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -999,7 +994,7 @@ Dependent Pairs
999---------------
994---------------
1001Dependent pairs allow the type of the second element of a pair to depend
996Dependent pairs allow the type of the second element of a pair to depend
1002−on the value of the first element.
997+on the value of the first element:
1004.. code-block:: idris
999.. code-block:: idris
@@ -1009,7 +1004,7 @@ on the value of the first element.
1009Again, there is syntactic sugar for this. ``(a : A ** P)`` is the type
1004Again, there is syntactic sugar for this. ``(a : A ** P)`` is the type
1010of a pair of A and P, where the name ``a`` can occur inside ``P``.
1005of a pair of A and P, where the name ``a`` can occur inside ``P``.
1011``( a ** p )`` constructs a value of this type. For example, we can
1006``( a ** p )`` constructs a value of this type. For example, we can
1012−pair a number with a ``Vect`` of a particular length.
1007+pair a number with a ``Vect`` of a particular length:
1014.. code-block:: idris
1009.. code-block:: idris
@@ -1017,7 +1012,7 @@ pair a number with a ``Vect`` of a particular length.
1017 vec = (2 ** [3, 4])
1012 vec = (2 ** [3, 4])
1019If you like, you can write it out the long way, the two are precisely
1014If you like, you can write it out the long way, the two are precisely
1020−equivalent.
1015+equivalent:
1022.. code-block:: idris
1017.. code-block:: idris
@@ -1070,7 +1065,7 @@ intermediate values:
1071We will see more on ``with`` notation later.
1066We will see more on ``with`` notation later.
1073−Dependent pairs are sometimes referred to as “sigma types”.
1068+Dependent pairs are sometimes referred to as “Sigma types”.
@@ -1080,7 +1075,7 @@ Records
1080automatically generating field access and update functions. Unlike
1075automatically generating field access and update functions. Unlike
1081the syntax used for data structures, records in Idris follow a
1076the syntax used for data structures, records in Idris follow a
1082different syntax to that seen with Haskell. For example, we can
1077different syntax to that seen with Haskell. For example, we can
1083−represent a person's name and age in a record:
1078+represent a person’s name and age in a record:
1085.. code-block:: idris
1080.. code-block:: idris
@@ -1092,12 +1087,11 @@ represent a person's name and age in a record:
1092 fred : Person
1087 fred : Person
1093 fred = MkPerson "Fred" "Joe" "Bloggs" 30
1088 fred = MkPerson "Fred" "Joe" "Bloggs" 30
1096The constructor name is provided using the ``constructor`` keyword,
1090The constructor name is provided using the ``constructor`` keyword,
1097and the *fields* are then given which are in an indented block
1091and the *fields* are then given which are in an indented block
1098following the `where` keyword (here, ``firstName``, ``middleName``,
1092following the `where` keyword (here, ``firstName``, ``middleName``,
1099``lastName``, and ``age``). You can declare multiple fields on a
1093``lastName``, and ``age``). You can declare multiple fields on a
1100−single line, provided that they have the same type. The field names
1094+single line, provided that they have the same type. The field names
1101can be used to access the field values:
1095can be used to access the field values:
@@ -1171,7 +1165,7 @@ syntax:
1171 record { a->b->c = val } x
1165 record { a->b->c = val } x
1173This returns a new record, with the field accessed by the path
1167This returns a new record, with the field accessed by the path
1174−``a->b->c`` set to ``val``. The syntax is first class, i.e. ``record {
1168+``a->b->c`` set to ``val``. The syntax is first class, i.e. ``record {
1175a->b->c = val }`` itself has a function type. Symmetrically, the field
1169a->b->c = val }`` itself has a function type. Symmetrically, the field
1176can also be accessed with the following syntax:
1170can also be accessed with the following syntax:
@@ -1196,12 +1190,10 @@ name. For example, a pair type could be defined as follows:
1200Using the ``class`` record from earlier, the size of the class can be
1193Using the ``class`` record from earlier, the size of the class can be
1201restricted using a ``Vect`` and the size included in the type by parameterising
1194restricted using a ``Vect`` and the size included in the type by parameterising
1202the record with the size. For example:
1195the record with the size. For example:
1205.. code-block:: idris
1197.. code-block:: idris
1207 record SizedClass (size : Nat) where
1199 record SizedClass (size : Nat) where
@@ -1214,7 +1206,7 @@ function from earlier, since that would change the size of the class. A
1214function to add a student must now specify in the type that the
1206function to add a student must now specify in the type that the
1215size of the class has been increased by one. As the size is specified
1207size of the class has been increased by one. As the size is specified
1216using natural numbers, the new value can be incremented using the
1208using natural numbers, the new value can be incremented using the
1217−``S`` constructor.
1209+``S`` constructor:
1219.. code-block:: idris
1211.. code-block:: idris
@@ -1327,7 +1319,7 @@ matching ``let`` and lambda bindings. It will *only* work if:
1327- Each branch *matches* a value of the same type, and *returns* a
1319- Each branch *matches* a value of the same type, and *returns* a
1328 value of the same type.
1320 value of the same type.
1330−- The type of the result is "known". i.e. the type of the expression
1322+- The type of the result is “known”. i.e. the type of the expression
1331 can be determined *without* type checking the ``case``-expression
1323 can be determined *without* type checking the ``case``-expression
@@ -1337,7 +1329,7 @@ Totality
1337Idris distinguishes between *total* and *partial* functions.
1329Idris distinguishes between *total* and *partial* functions.
1338A total function is a function that either:
1330A total function is a function that either:
1340−+ Terminates for all possible inputs, or
1332++ Terminates for all possible inputs, or
1341+ Produces a non-empty, finite, prefix of a possibly infinite result
1333+ Produces a non-empty, finite, prefix of a possibly infinite result
1343If a function is total, we can consider its type a precise description of what
1335If a function is total, we can consider its type a precise description of what
@@ -1345,12 +1337,12 @@ that function will do. For example, if we have a function with a return
1345type of ``String`` we know something different, depending on whether or not
1337type of ``String`` we know something different, depending on whether or not
1346it's total:
1338it's total:
1348−+ If it's total, it will return a value of type ``String`` in finite time
1340++ If it's total, it will return a value of type ``String`` in finite time;
1349+ If it's partial, then as long as it doesn't crash or enter an infinite loop,
1341+ If it's partial, then as long as it doesn't crash or enter an infinite loop,
1350 it will return a ``String``.
1342 it will return a ``String``.
1352Idris makes this distinction so that it knows which functions are safe to
1344Idris makes this distinction so that it knows which functions are safe to
1353−evaluate while type checking (as we've seen with :ref:`sect-fctypes`). After all,
1345+evaluate while type checking (as we've seen with :ref:`sect-fctypes`). After all,
1354if it tries to evaluate a function during type checking which doesn't
1346if it tries to evaluate a function during type checking which doesn't
1355terminate, then type checking won't terminate!
1347terminate, then type checking won't terminate!
1356Therefore, only total functions will be evaluated during type checking.
1348Therefore, only total functions will be evaluated during type checking.