@@ -250,17 +250,65 @@ So, for example, the following definitions are legal:
250250 d y = c (y + 1 + z y)
251251 where z w = y + w
252252
253+.. _sect-holes:
254+
255+Holes
256+-----
257+
258+Idris programs can contain *holes* which stand for incomplete parts of
259+programs. For example, we could leave a hole for the greeting in our
260+"Hello world" program:
261+
262+.. code-block:: idris
263+
264+ main : IO ()
265+ main = putStrLn ?greeting
266+
267+The syntax ``?greeting`` introduces a hole, which stands for a part of
268+a program which is not yet written. This is a valid Idris program, and you
269+can check the type of ``greeting``:
270+
271+::
272+
273+ *Hello> :t greeting
274+ --------------------------------------
275+ greeting : String
276+
277+Checking the type of a hole also shows the types of any variables in scope.
278+For example, given an incomplete definition of ``even``:
279+
280+.. code-block:: idris
281+
282+ even : Nat -> Bool
283+ even Z = True
284+ even (S k) = ?even_rhs
285+
286+We can check the type of ``even_rhs`` and see the expected return type,
287+and the type of the variable ``k``:
288+
289+::
290+
291+ *Even> :t even_rhs
292+ k : Nat
293+ --------------------------------------
294+ even_rhs : Bool
295+
296+Holes are useful because they help us write functions *incrementally*.
297+Rather than writing an entire function in one go, we can leave some parts
298+unwritten and use Idris to tell us what is necessary to complete the
299+definition.
253300
254301Dependent Types
255302===============
256303
304+.. _sect-fctypes:
305+
257306First Class Types
258307-----------------
259308
260−In Idris, types are a first class language construct, meaning that they
261−can be computed and manipulated (and passed to functions) just like any
262−other language construct. For example, we could write a function which
263−computes a type:
309+In Idris, types are first class, meaning that they can be computed and
310+manipulated (and passed to functions) just like any other language construct.
311+For example, we could write a function which computes a type:
264312
265313.. code-block:: idris
266314
@@ -350,19 +398,22 @@ following:
350398
351399::
352400
353− $ idris vbroken.idr --check
354− vbroken.idr:9:23:When elaborating right hand side of Vect.++:
355− When elaborating an application of constructor Vect.:::
356− Type mismatch between
357− Vect (k + k) a (Type of xs ++ xs)
358− and
359− Vect (plus k m) a (Expected type)
401+ $ idris VBroken.idr --check
402+ VBroken.idr:9:23-25:
403+ When checking right hand side of Vect.++ with expected type
404+ Vect (S k + m) a
360405
361− Specifically:
362− Type mismatch between
363− plus k k
364− and
365− plus k m
406+ When checking an application of constructor Vect.:::
407+ Type mismatch between
408+ Vect (k + k) a (Type of xs ++ xs)
409+ and
410+ Vect (plus k m) a (Expected type)
411+
412+ Specifically:
413+ Type mismatch between
414+ plus k k
415+ and
416+ plus k m
366417
367418
368419This error message suggests that there is a length mismatch between
@@ -656,9 +707,9 @@ not always the best approach. Consider the following function:
656707
657708.. code-block:: idris
658709
659− ifThenElse : Bool -> a -> a -> a;
660− ifThenElse True t e = t;
661− ifThenElse False t e = e;
710+ ifThenElse : Bool -> a -> a -> a
711+ ifThenElse True t e = t
712+ ifThenElse False t e = e
662713
663714This function uses one of the ``t`` or ``e`` arguments, but not both
664715(in fact, this is used to implement the ``if...then...else`` construct
@@ -681,19 +732,19 @@ without any explicit use of ``Force`` or ``Delay``:
681732
682733.. code-block:: idris
683734
684− ifThenElse : Bool -> Lazy a -> Lazy a -> a;
685− ifThenElse True t e = t;
686− ifThenElse False t e = e;
735+ ifThenElse : Bool -> Lazy a -> Lazy a -> a
736+ ifThenElse True t e = t
737+ ifThenElse False t e = e
687738
688739Codata Types
689740============
690741
691−Codata types are like regular data types, except that they allow for us to
692−define infinite data structures. More precisely, for a type ``T``, each of its
693−constructor arguments of type ``T`` are transformed into a coinductive parameter
694−``Inf T``. This makes each of the ``T`` arguments lazy, and allows infinite data
695−structures of type ``T`` to be built. One example of a codata type is Stream,
696−which is defined as follows.
742+Codata types allow us to define infinite data structures by marking recursive
743+arguments as potentially infinite. For
744+a codata type ``T``, each of its constructor arguments of type ``T`` are transformed
745+into an argument of type ``Inf T``. This makes each of the ``T`` arguments
746+lazy, and allows infinite data structures of type ``T`` to be built. One
747+example of a codata type is Stream, which is defined as follows.
697748
698749.. code-block:: idris
699750
@@ -778,7 +829,8 @@ Useful Data Types
778829=================
779830
780831Idris includes a number of useful data types and library functions
781−(see the ``libs/`` directory in the distribution). This chapter
832+(see the ``libs/`` directory in the distribution, and the
833+`documentation <https://www.idris-lang.org/documentation/>`_). This section
782834describes a few of these. The functions described here are imported
783835automatically by every Idris program, as part of ``Prelude.idr``.
784836
@@ -836,7 +888,7 @@ the vector:
836888
837889::
838890
839− *usefultypes> show (map double intVec)
891+ *UsefulTypes> show (map double intVec)
840892 "[2, 4, 6, 8, 10]" : String
841893
842894For more details of the functions available on ``List`` and
@@ -850,9 +902,7 @@ For more details of the functions available on ``List`` and
850902
851903- ``libs/base/Data/VectType.idr``
852904
853−Functions include filtering, appending, reversing, and so on. Also
854−remember that Idris is still in development, so if you don’t see
855−the function you need, please feel free to add it and submit a patch!
905+Functions include filtering, appending, reversing, and so on.
856906
857907Aside: Anonymous functions and operator sections
858908~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -862,7 +912,7 @@ would be to use an anonymous function:
862912
863913::
864914
865− *usefultypes> show (map (\x => x * 2) intVec)
915+ *UsefulTypes> show (map (\x => x * 2) intVec)
866916 "[2, 4, 6, 8, 10]" : String
867917
868918The notation ``\x => val`` constructs an anonymous function which takes
@@ -874,7 +924,7 @@ e.g. ``\(x, y) => x + y``. We could also use an operator section:
874924
875925::
876926
877− *usefultypes> show (map (* 2) intVec)
927+ *UsefulTypes> show (map (* 2) intVec)
878928 "[2, 4, 6, 8, 10]" : String
879929
880930``(*2)`` is shorthand for a function which multiplies a number
@@ -938,19 +988,18 @@ contain an arbitrary number of values, represented as nested pairs:
938988
939989::
940990
941− *usefultypes> fst jim
991+ *UsefulTypes> fst jim
942992 "Jim" : String
943− *usefultypes> snd jim
993+ *UsefulTypes> snd jim
944994 (25, "Cambridge") : (Int, String)
945− *usefultypes> jim == ("Jim", (25, "Cambridge"))
995+ *UsefulTypes> jim == ("Jim", (25, "Cambridge"))
946996 True : Bool
947997
948998Dependent Pairs
949999---------------
9501000
9511001Dependent pairs allow the type of the second element of a pair to depend
952−on the value of the first element. Traditionally, these are referred to
953−as “sigma types”:
1002+on the value of the first element.
9541003
9551004.. code-block:: idris
9561005
@@ -1021,6 +1070,7 @@ intermediate values:
10211070
10221071We will see more on ``with`` notation later.
10231072
1073+Dependent pairs are sometimes referred to as “sigma types”.
10241074
10251075Records
10261076-------
@@ -1052,11 +1102,11 @@ can be used to access the field values:
10521102
10531103::
10541104
1055− *record> firstName fred
1105+ *Record> firstName fred
10561106 "Fred" : String
1057− *record> age fred
1107+ *Record> age fred
10581108 30 : Int
1059− *record> :t firstName
1109+ *Record> :t firstName
10601110 firstName : Person -> String
10611111
10621112We can also use the field names to update a record (or, more
@@ -1065,9 +1115,9 @@ updated):
10651115
10661116.. code-block:: bash
10671117
1068− *record> record { firstName = "Jim" } fred
1118+ *Record> record { firstName = "Jim" } fred
10691119 MkPerson "Jim" "Joe" "Bloggs" 30 : Person
1070− *record> record { firstName = "Jim", age $= (+ 1) } fred
1120+ *Record> record { firstName = "Jim", age $= (+ 1) } fred
10711121 MkPerson "Jim" "Joe" "Bloggs" 31 : Person
10721122
10731123The syntax ``record { field = val, ... }`` generates a function which
@@ -1098,9 +1148,15 @@ length because it will not affect the type of the record:
10981148
10991149::
11001150
1101− *record> addStudent fred (ClassInfo [] "CS")
1151+ *Record> addStudent fred (ClassInfo [] "CS")
11021152 ClassInfo [MkPerson "Fred" "Joe" "Bloggs" 30] "CS" : Class
11031153
1154+We could also use ``$=`` to define ``addStudent`` more concisely:
1155+
1156+.. code-block:: idris
1157+
1158+ addStudent' : Person -> Class -> Class
1159+ addStudent' p c = record { students $= (p ::) } c
11041160
11051161Nested record update
11061162~~~~~~~~~~~~~~~~~~~~
@@ -1123,12 +1179,13 @@ can also be accessed with the following syntax:
11231179
11241180 record { a->b->c } x
11251181
1182+The ``$=`` notation is also valid for nested record updates.
11261183
11271184Dependent Records
11281185-----------------
11291186
1130−Records can also be dependent on values. Records *parameters*, which
1131−are not subject to field updates. The parameters appear as arguments
1187+Records can also be dependent on values. Records have *parameters*, which
1188+cannot be updated like the other fields. The parameters appear as arguments
11321189to the resulting type, and are written following the record type
11331190name. For example, a pair type could be defined as follows:
11341191
@@ -1140,10 +1197,9 @@ name. For example, a pair type could be defined as follows:
11401197 snd : b
11411198
11421199
1143−Using the class record from the original introduction to records. The
1144−size of the class can be restricted using a ``Vect`` and the size
1145−promoted to the type level by parameterising the record with the size.
1146−For example:
1200+Using the ``class`` record from earlier, the size of the class can be
1201+restricted using a ``Vect`` and the size included in the type by parameterising
1202+the record with the size. For example:
11471203
11481204
11491205.. code-block:: idris
@@ -1258,9 +1314,9 @@ we get a default value:
12581314
12591315::
12601316
1261− *usefultypes> lookup_default 2 [3,4,5,6] (-1)
1317+ *UsefulTypes> lookup_default 2 [3,4,5,6] (-1)
12621318 5 : Integer
1263− *usefultypes> lookup_default 4 [3,4,5,6] (-1)
1319+ *UsefulTypes> lookup_default 4 [3,4,5,6] (-1)
12641320 -1 : Integer
12651321
12661322**Restrictions:** The ``case`` construct is intended for simple
@@ -1278,75 +1334,25 @@ matching ``let`` and lambda bindings. It will *only* work if:
12781334Totality
12791335========
12801336
1281−A *total* function is a function that terminates for all possible
1282−inputs, or one that is guaranteed to produce some output before making
1283−a recursive call. For example, Idris' ``head`` function is total for
1284−all lists:
1285−
1286−::
1287−
1288− Idris> :t Prelude.List.head
1289− head : (l : List a) -> {auto ok : NonEmpty l} -> a
1290−
1291−The ``{auto ok : NonEmpty l}`` tells us that Idris won't compile if we
1292−try to call ``head`` on an empty list. The implementation is as follows:
1293−
1294−.. code-block:: idris
1295−
1296− ||| Get the first element of a non-empty list
1297− ||| @ ok proof that the list is non-empty
1298− head : (l : List a) -> {auto ok : NonEmpty l} -> a
1299− head [] {ok=IsNonEmpty} impossible
1300− head (x::xs) {ok=p} = x
1301−
1302−(Note that this implementation is in contrast to Haskell's ``head``,
1303−which is *not* total and will fail at runtime rather than compile time.)
1304−The following Idris code will compile:
1305−
1306−.. code-block:: idris
1307−
1308− module Main
1337+Idris distinguishes between *total* and *partial* functions.
1338+A total function is a function that either:
13091339
1310− main : IO ()
1311− main = do
1312− let x : Integer = head [1,2,3]
1313− print x
1314−
1315−And will print ``1``. However, the same code with ``head []`` won't compile:
1316−
1317−::
1318−
1319− test.idr:5:26:When checking right hand side of main with expected type
1320− IO ()
1321−
1322− When checking argument ok to function Prelude.List.head:
1323− Can't find a value of type
1324− NonEmpty []
1325−
1326−We can't bind and print ``x`` because ``head []`` doesn't type check.
1340++ Terminates for all possible inputs, or
1341++ Produces a non-empty, finite, prefix of a possibly infinite result
13271342
1328−However, we might imagine a function, ``unsafeHead``, that is identical to
1329−Idris' ``head`` function except that it is *not* total: it will error out
1330−at runtime if called on an empty list. (This is similar to the behavior of
1331−Haskell's ``head`` function.) ``unsafeHead`` might look like this:
1332−
1333−.. code-block:: idris
1334−
1335− -- Unsafe head example!
1336− unsafeHead : List a -> a
1337− unsafeHead (x::xs) = x
1338−
1339−And although it typechecks and compiles, it will not reduce (that is, evaluation
1340−of the function will cause it to change):
1341−
1342−::
1343+If a function is total, we can consider its type a precise description of what
1344+that function will do. For example, if we have a function with a return
1345+type of ``String`` we know something different, depending on whether or not
1346+it's total:
13431347
1344− unsafe> the Integer $ unsafeHead [1, 2, 3]
1345− 1 : Integer
1346− unsafe> the Integer $ unsafeHead []
1347− unsafeHead [] : Integer
1348++ 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,
1350+ it will return a ``String``.
13481351
1349−Functions that are not total are known as *partial functions*. As
1350−mentioned in the note about ``mutual`` blocks, non-total definitions
1351−aren't reduced when type checking because they are not well-defined
1352−for all possible inputs.
1352+Idris 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,
1354+if it tries to evaluate a function during type checking which doesn't
1355+terminate, then type checking won't terminate!
1356+Therefore, only total functions will be evaluated during type checking.
1357+Partial functions can still be used in types, but will not be evaluated
1358+further.