@@ -250,17 +250,65 @@ So, for example, the following definitions are legal:
250 d y = c (y + 1 + z y)
250 d y = c (y + 1 + z y)
251 where z w = y + w
251 where z w = y + w
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:
262+.. code-block:: idris
265+ main = putStrLn ?greeting
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``:
274+ --------------------------------------
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``:
280+.. code-block:: idris
284+ even (S k) = ?even_rhs
286+We can check the type of ``even_rhs`` and see the expected return type,
287+and the type of the variable ``k``:
293+ --------------------------------------
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
254Dependent Types
301Dependent Types
255===============
302===============
257First Class Types
306First Class Types
258-----------------
307-----------------
260−In Idris, types are a first class language construct, meaning that they
309+In Idris, types are first class, meaning that they can be computed and
261−can be computed and manipulated (and passed to functions) just like any
310+manipulated (and passed to functions) just like any other language construct.
262−other language construct. For example, we could write a function which
311+For example, we could write a function which computes a type:
265.. code-block:: idris
313.. code-block:: idris
@@ -350,19 +398,22 @@ following:
353− $ idris vbroken.idr --check
401+ $ idris VBroken.idr --check
402+ VBroken.idr:9:23-25:
354− vbroken.idr:9:23:When elaborating right hand side of Vect.++:
403+ When checking right hand side of Vect.++ with expected type
355− When elaborating an application of constructor Vect.:::
404+ Vect (S k + m) a
356− Type mismatch between
357− Vect (k + k) a (Type of xs ++ xs)
359− Vect (plus k m) a (Expected type)
406+ When checking an application of constructor Vect.:::
407+ Type mismatch between
408+ Vect (k + k) a (Type of xs ++ xs)
410+ Vect (plus k m) a (Expected type)
361− Specifically:
412+ Specifically:
362− Type mismatch between
413+ Type mismatch between
363− plus k k
414+ plus k k
365− plus k m
416+ plus k m
368This error message suggests that there is a length mismatch between
419This error message suggests that there is a length mismatch between
@@ -656,9 +707,9 @@ not always the best approach. Consider the following function:
657.. code-block:: idris
708.. code-block:: idris
659− ifThenElse : Bool -> a -> a -> a;
710+ ifThenElse : Bool -> a -> a -> a
660− ifThenElse True t e = t;
711+ ifThenElse True t e = t
661− ifThenElse False t e = e;
712+ ifThenElse False t e = e
663This function uses one of the ``t`` or ``e`` arguments, but not both
714This function uses one of the ``t`` or ``e`` arguments, but not both
664(in fact, this is used to implement the ``if...then...else`` construct
715(in fact, this is used to implement the ``if...then...else`` construct
@@ -681,19 +732,19 @@ without any explicit use of ``Force`` or ``Delay``:
682.. code-block:: idris
733.. code-block:: idris
684− ifThenElse : Bool -> Lazy a -> Lazy a -> a;
735+ ifThenElse : Bool -> Lazy a -> Lazy a -> a
685− ifThenElse True t e = t;
736+ ifThenElse True t e = t
686− ifThenElse False t e = e;
737+ ifThenElse False t e = e
688Codata Types
739Codata Types
689============
740============
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
742+Codata types allow us to define infinite data structures by marking recursive
743+arguments as potentially infinite. For
693−constructor arguments of type ``T`` are transformed into a coinductive parameter
744+a codata type ``T``, each of its constructor arguments of type ``T`` are transformed
694−``Inf T``. This makes each of the ``T`` arguments lazy, and allows infinite data
745+into an argument of type ``Inf T``. This makes each of the ``T`` arguments
695−structures of type ``T`` to be built. One example of a codata type is Stream,
746+lazy, and allows infinite data structures of type ``T`` to be built. One
696−which is defined as follows.
747+example of a codata type is Stream, which is defined as follows.
698.. code-block:: idris
749.. code-block:: idris
@@ -778,7 +829,8 @@ Useful Data Types
778=================
829=================
780Idris includes a number of useful data types and library functions
831Idris 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
782describes a few of these. The functions described here are imported
834describes a few of these. The functions described here are imported
783automatically by every Idris program, as part of ``Prelude.idr``.
835automatically by every Idris program, as part of ``Prelude.idr``.
@@ -836,7 +888,7 @@ the vector:
839− *usefultypes> show (map double intVec)
891+ *UsefulTypes> show (map double intVec)
840 "[2, 4, 6, 8, 10]" : String
892 "[2, 4, 6, 8, 10]" : String
842For more details of the functions available on ``List`` and
894For more details of the functions available on ``List`` and
@@ -850,9 +902,7 @@ For more details of the functions available on ``List`` and
851- ``libs/base/Data/VectType.idr``
903- ``libs/base/Data/VectType.idr``
853−Functions include filtering, appending, reversing, and so on. Also
905+Functions include filtering, appending, reversing, and so on.
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!
857Aside: Anonymous functions and operator sections
907Aside: Anonymous functions and operator sections
858~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
908~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -862,7 +912,7 @@ would be to use an anonymous function:
865− *usefultypes> show (map (\x => x * 2) intVec)
915+ *UsefulTypes> show (map (\x => x * 2) intVec)
866 "[2, 4, 6, 8, 10]" : String
916 "[2, 4, 6, 8, 10]" : String
868The notation ``\x => val`` constructs an anonymous function which takes
918The 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:
877− *usefultypes> show (map (* 2) intVec)
927+ *UsefulTypes> show (map (* 2) intVec)
878 "[2, 4, 6, 8, 10]" : String
928 "[2, 4, 6, 8, 10]" : String
880``(*2)`` is shorthand for a function which multiplies a number
930``(*2)`` is shorthand for a function which multiplies a number
@@ -938,19 +988,18 @@ contain an arbitrary number of values, represented as nested pairs:
941− *usefultypes> fst jim
991+ *UsefulTypes> fst jim
942 "Jim" : String
992 "Jim" : String
943− *usefultypes> snd jim
993+ *UsefulTypes> snd jim
944 (25, "Cambridge") : (Int, String)
994 (25, "Cambridge") : (Int, String)
945− *usefultypes> jim == ("Jim", (25, "Cambridge"))
995+ *UsefulTypes> jim == ("Jim", (25, "Cambridge"))
946 True : Bool
996 True : Bool
948Dependent Pairs
998Dependent Pairs
949---------------
999---------------
951Dependent pairs allow the type of the second element of a pair to depend
1001Dependent 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
1002+on the value of the first element.
955.. code-block:: idris
1004.. code-block:: idris
@@ -1021,6 +1070,7 @@ intermediate values:
1022We will see more on ``with`` notation later.
1071We will see more on ``with`` notation later.
1073+Dependent pairs are sometimes referred to as “sigma types”.
@@ -1052,11 +1102,11 @@ can be used to access the field values:
1055− *record> firstName fred
1105+ *Record> firstName fred
1056 "Fred" : String
1106 "Fred" : String
1057− *record> age fred
1107+ *Record> age fred
1058 30 : Int
1108 30 : Int
1059− *record> :t firstName
1109+ *Record> :t firstName
1060 firstName : Person -> String
1110 firstName : Person -> String
1062We can also use the field names to update a record (or, more
1112We can also use the field names to update a record (or, more
@@ -1065,9 +1115,9 @@ updated):
1066.. code-block:: bash
1116.. code-block:: bash
1068− *record> record { firstName = "Jim" } fred
1118+ *Record> record { firstName = "Jim" } fred
1069 MkPerson "Jim" "Joe" "Bloggs" 30 : Person
1119 MkPerson "Jim" "Joe" "Bloggs" 30 : Person
1070− *record> record { firstName = "Jim", age $= (+ 1) } fred
1120+ *Record> record { firstName = "Jim", age $= (+ 1) } fred
1071 MkPerson "Jim" "Joe" "Bloggs" 31 : Person
1121 MkPerson "Jim" "Joe" "Bloggs" 31 : Person
1073The syntax ``record { field = val, ... }`` generates a function which
1123The syntax ``record { field = val, ... }`` generates a function which
@@ -1098,9 +1148,15 @@ length because it will not affect the type of the record:
1101− *record> addStudent fred (ClassInfo [] "CS")
1151+ *Record> addStudent fred (ClassInfo [] "CS")
1102 ClassInfo [MkPerson "Fred" "Joe" "Bloggs" 30] "CS" : Class
1152 ClassInfo [MkPerson "Fred" "Joe" "Bloggs" 30] "CS" : Class
1154+We could also use ``$=`` to define ``addStudent`` more concisely:
1156+.. code-block:: idris
1158+ addStudent' : Person -> Class -> Class
1159+ addStudent' p c = record { students $= (p ::) } c
1105Nested record update
1161Nested record update
1106~~~~~~~~~~~~~~~~~~~~
1162~~~~~~~~~~~~~~~~~~~~
@@ -1123,12 +1179,13 @@ can also be accessed with the following syntax:
1124 record { a->b->c } x
1180 record { a->b->c } x
1182+The ``$=`` notation is also valid for nested record updates.
1127Dependent Records
1184Dependent Records
1128-----------------
1185-----------------
1130−Records can also be dependent on values. Records *parameters*, which
1187+Records can also be dependent on values. Records have *parameters*, which
1131−are not subject to field updates. The parameters appear as arguments
1188+cannot be updated like the other fields. The parameters appear as arguments
1132to the resulting type, and are written following the record type
1189to the resulting type, and are written following the record type
1133name. For example, a pair type could be defined as follows:
1190name. For example, a pair type could be defined as follows:
@@ -1140,10 +1197,9 @@ name. For example, a pair type could be defined as follows:
1143−Using the class record from the original introduction to records. The
1200+Using the ``class`` record from earlier, the size of the class can be
1144−size of the class can be restricted using a ``Vect`` and the size
1201+restricted using a ``Vect`` and the size included in the type by parameterising
1145−promoted to the type level by parameterising the record with the size.
1146−For example:
1202+the record with the size. For example:
1149.. code-block:: idris
1205.. code-block:: idris
@@ -1258,9 +1314,9 @@ we get a default value:
1261− *usefultypes> lookup_default 2 [3,4,5,6] (-1)
1317+ *UsefulTypes> lookup_default 2 [3,4,5,6] (-1)
1262 5 : Integer
1318 5 : Integer
1263− *usefultypes> lookup_default 4 [3,4,5,6] (-1)
1319+ *UsefulTypes> lookup_default 4 [3,4,5,6] (-1)
1264 -1 : Integer
1320 -1 : Integer
1266**Restrictions:** The ``case`` construct is intended for simple
1322**Restrictions:** The ``case`` construct is intended for simple
@@ -1278,75 +1334,25 @@ matching ``let`` and lambda bindings. It will *only* work if:
1337+Idris distinguishes between *total* and *partial* functions.
1281−A *total* function is a function that terminates for all possible
1338+A total function is a function that either:
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
1288− Idris> :t Prelude.List.head
1289− head : (l : List a) -> {auto ok : NonEmpty l} -> a
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:
1294−.. code-block:: idris
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
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:
1306−.. code-block:: idris
1310− main : IO ()
1340++ Terminates for all possible inputs, or
1311− main = do
1341++ Produces a non-empty, finite, prefix of a possibly infinite result
1312− let x : Integer = head [1,2,3]
1315−And will print ``1``. However, the same code with ``head []`` won't compile:
1319− test.idr:5:26:When checking right hand side of main with expected type
1322− When checking argument ok to function Prelude.List.head:
1323− Can't find a value of type
1326−We can't bind and print ``x`` because ``head []`` doesn't type check.
1328−However, we might imagine a function, ``unsafeHead``, that is identical to
1343+If a function is total, we can consider its type a precise description of what
1329−Idris' ``head`` function except that it is *not* total: it will error out
1344+that function will do. For example, if we have a function with a return
1330−at runtime if called on an empty list. (This is similar to the behavior of
1345+type of ``String`` we know something different, depending on whether or not
1331−Haskell's ``head`` function.) ``unsafeHead`` might look like this:
1346+it's total:
1333−.. code-block:: idris
1335− -- Unsafe head example!
1336− unsafeHead : List a -> a
1337− unsafeHead (x::xs) = x
1339−And although it typechecks and compiles, it will not reduce (that is, evaluation
1340−of the function will cause it to change):
1344− unsafe> the Integer $ unsafeHead [1, 2, 3]
1348++ If it's total, it will return a value of type ``String`` in finite time
1345− 1 : Integer
1349++ If it's partial, then as long as it doesn't crash or enter an infinite loop,
1346− unsafe> the Integer $ unsafeHead []
1350+ it will return a ``String``.
1347− unsafeHead [] : Integer
1349−Functions that are not total are known as *partial functions*. As
1352+Idris makes this distinction so that it knows which functions are safe to
1350−mentioned in the note about ``mutual`` blocks, non-total definitions
1353+evaluate while type checking (as we've seen with :ref:`sect-fctypes`). After all,
1351−aren't reduced when type checking because they are not well-defined
1354+if it tries to evaluate a function during type checking which doesn't
1352−for all possible inputs.
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