@@ -13,19 +13,19 @@ environment by typing \texttt{idris prims.idr}:
14\begin{SaveVerbatim}{constprims}
14\begin{SaveVerbatim}{constprims}
16−module prims;
16+module prims
21−foo : String;
21+foo : String
22−foo = "Sausage machine";
22+foo = "Sausage machine"
24−bar : Char;
24+bar : Char
25−bar = 'Z';
25+bar = 'Z'
27−quux : Bool;
27+quux : Bool
28−quux = False;
28+quux = False
30\end{SaveVerbatim}
30\end{SaveVerbatim}
31\useverb{constprims}
31\useverb{constprims}
@@ -35,8 +35,10 @@ An \Idris{} file consists of an optional module declaration (here
35however \Idris{} programs can consist of several modules, and the definitions
35however \Idris{} programs can consist of several modules, and the definitions
36in each module each have their own namespace, as we will discuss shortly) and a
36in each module each have their own namespace, as we will discuss shortly) and a
37collection of declarations and definitions. Each definition must have a type
37collection of declarations and definitions. Each definition must have a type
38−declaration (here, \texttt{x : Int}, \texttt{foo : String}, etc). Each
38+declaration (here, \texttt{x : Int}, \texttt{foo : String}, etc).
39−component is separated by a semi-colon.
39+Indentation is significant --- a new declaration begins at the same leve
40+of indentation as the preceding declaration. Alternatively, declarations
41+may be terminated with a semicolon.
41A library module \texttt{prelude} is automatically imported by every \Idris{} program,
43A library module \texttt{prelude} is automatically imported by every \Idris{} program,
42including facilities for IO, arithmetic, data structures and various common
44including facilities for IO, arithmetic, data structures and various common
@@ -76,9 +78,9 @@ example, can be declared as follows:
77\begin{SaveVerbatim}{natlist}
79\begin{SaveVerbatim}{natlist}
79−data Nat = O | S Nat; -- Natural numbers
81+data Nat = O | S Nat -- Natural numbers
80− -- (zero and successor)
82+ -- (zero and successor)
81−data List a = Nil | (::) a (List a); -- Polymorphic lists
83+data List a = Nil | (::) a (List a) -- Polymorphic lists
83\end{SaveVerbatim}
85\end{SaveVerbatim}
84\useverb{natlist}
86\useverb{natlist}
@@ -93,7 +95,7 @@ such as this can be added using a fixity declaration, as follows:
94\begin{SaveVerbatim}{infixcons}
96\begin{SaveVerbatim}{infixcons}
96−infixr 7 :: ;
98+infixr 7 ::
98\end{SaveVerbatim}
100\end{SaveVerbatim}
99\useverb{infixcons}
101\useverb{infixcons}
@@ -124,14 +126,14 @@ defined as follows, again taken from the standard library:
124\begin{SaveVerbatim}{natfns}
126\begin{SaveVerbatim}{natfns}
126-- Unary addition
128-- Unary addition
127−plus : Nat -> Nat -> Nat;
129+plus : Nat -> Nat -> Nat
128−plus O y = y;
130+plus O y = y
129−plus (S k) y = S (plus k y);
131+plus (S k) y = S (plus k y)
131-- Unary multiplication
133-- Unary multiplication
132−mult : Nat -> Nat -> Nat;
134+mult : Nat -> Nat -> Nat
133−mult O y = O;
135+mult O y = O
134−mult (S k) y = plus y (mult k y);
136+mult (S k) y = plus y (mult k y)
136\end{SaveVerbatim}
138\end{SaveVerbatim}
137\useverb{natfns}
139\useverb{natfns}
@@ -186,11 +188,11 @@ accumulates the new, reversed list, and which does not need to be visible global
187\begin{SaveVerbatim}{revwhere}
189\begin{SaveVerbatim}{revwhere}
189−rev : List a -> List a;
191+rev : List a -> List a
190rev xs = revAcc [] xs where {
192rev xs = revAcc [] xs where {
191− revAcc : List a -> List a -> List a;
193+ revAcc : List a -> List a -> List a
192− revAcc acc [] = acc;
194+ revAcc acc [] = acc
193− revAcc acc (x :: xs) = revAcc (x :: acc) xs;
195+ revAcc acc (x :: xs) = revAcc (x :: acc) xs
196\end{SaveVerbatim}
198\end{SaveVerbatim}
@@ -207,9 +209,9 @@ behaviour, the \texttt{a} can be brought into scope as follows:
208\begin{SaveVerbatim}{revwhereb}
210\begin{SaveVerbatim}{revwhereb}
210−rev : List a -> List a;
212+rev : List a -> List a
211rev {a} xs = revAcc [] xs where {
213rev {a} xs = revAcc [] xs where {
212− revAcc : List a -> List a -> List a;
214+ revAcc : List a -> List a -> List a
@@ -228,7 +230,7 @@ we declare vectors as follows:
229data Vect : Set -> Nat -> Set where
231data Vect : Set -> Nat -> Set where
230 Nil : Vect a O
232 Nil : Vect a O
231− | (::) : a -> Vect a k -> Vect a (S k);
233+ | (::) : a -> Vect a k -> Vect a (S k)
233\end{SaveVerbatim}
235\end{SaveVerbatim}
234\useverb{vect}
236\useverb{vect}
@@ -257,9 +259,9 @@ library, appends two \tTC{Vect}s:
258\begin{SaveVerbatim}{vapp}
260\begin{SaveVerbatim}{vapp}
260−app : Vect A n -> Vect A m -> Vect A (n + m);
262+app : Vect A n -> Vect A m -> Vect A (n + m)
261−app Nil ys = ys;
263+app Nil ys = ys
262−app (x :: xs) ys = x :: app xs ys;
264+app (x :: xs) ys = x :: app xs ys
264\end{SaveVerbatim}
266\end{SaveVerbatim}
265\useverb{vapp}
267\useverb{vapp}
@@ -270,9 +272,9 @@ not hold, \Idris{} will not accept the definition. For example:
271\begin{SaveVerbatim}{vapperr}
273\begin{SaveVerbatim}{vapperr}
273−app : Vect a n -> Vect a m -> Vect a (n + m);
275+app : Vect a n -> Vect a m -> Vect a (n + m)
274−app Nil ys = ys;
276+app Nil ys = ys
275−app (x :: xs) ys = x :: app xs xs; -- BROKEN
277+app (x :: xs) ys = x :: app xs xs -- BROKEN
277\end{SaveVerbatim}
279\end{SaveVerbatim}
278\useverb{vapperr}
280\useverb{vapperr}
@@ -303,7 +305,7 @@ They are declared as follows (again, in the prelude):
304data Fin : Nat -> Set where
306data Fin : Nat -> Set where
305 fO : Fin (S k)
307 fO : Fin (S k)
306− | fS : Fin k -> Fin (S k);
308+ | fS : Fin k -> Fin (S k)
308\end{SaveVerbatim}
310\end{SaveVerbatim}
309\useverb{findecl}
311\useverb{findecl}
@@ -321,9 +323,9 @@ is defined in the prelude:
322\begin{SaveVerbatim}{vlookup}
324\begin{SaveVerbatim}{vlookup}
324−lookup : Fin n -> Vect a n -> a;
326+lookup : Fin n -> Vect a n -> a
325−lookup fO (x :: xs) = x;
327+lookup fO (x :: xs) = x
326−lookup (fS k) (x :: xs) = lookup k xs;
328+lookup (fS k) (x :: xs) = lookup k xs
328\end{SaveVerbatim}
330\end{SaveVerbatim}
329\useverb{vlookup}
331\useverb{vlookup}
@@ -384,7 +386,7 @@ declared the type of \texttt{lookup} as:
385\begin{SaveVerbatim}{vlookupn}
387\begin{SaveVerbatim}{vlookupn}
387−lookup : (i:Fin n) -> (xs:Vect a n) -> a;
389+lookup : (i:Fin n) -> (xs:Vect a n) -> a
389\end{SaveVerbatim}
391\end{SaveVerbatim}
390\useverb{vlookupn}
392\useverb{vlookupn}
@@ -406,7 +408,7 @@ definition, which defines a predicate on vectors:
407data Elem : a -> (Vect a n) -> Set where
409data Elem : a -> (Vect a n) -> Set where
408 here : {x:a} -> {xs:Vect a n} -> Elem x (x :: xs)
410 here : {x:a} -> {xs:Vect a n} -> Elem x (x :: xs)
409− | there : {x,y:a} -> {xs:Vect a n} -> Elem x xs -> Elem x (y :: xs);
411+ | there : {x,y:a} -> {xs:Vect a n} -> Elem x xs -> Elem x (y :: xs)
411\end{SaveVerbatim}
413\end{SaveVerbatim}
412\useverb{elem}
414\useverb{elem}
@@ -419,11 +421,11 @@ or \texttt{there}, in the tail of the vector. For example:
420\begin{SaveVerbatim}{testelem}
422\begin{SaveVerbatim}{testelem}
422−testVec : Vect Int 4;
424+testVec : Vect Int 4
423−testVec = 3 :: 4 :: 5 :: 6 :: VNil;
425+testVec = 3 :: 4 :: 5 :: 6 :: VNil
425−inVect : Elem 5 testVec;
427+inVect : Elem 5 testVec
426−inVect = there (there here);
428+inVect = there (there here)
428\end{SaveVerbatim}
430\end{SaveVerbatim}
429\useverb{testelem}
431\useverb{testelem}
@@ -438,7 +440,7 @@ ordering of any implicit arguments which can appear within the block:
438using (x:a, y:a, xs:Vect a n) {
440using (x:a, y:a, xs:Vect a n) {
439 data Elem : a -> (Vect a n) -> Set where
441 data Elem : a -> (Vect a n) -> Set where
440 here : Elem x (x :: xs)
442 here : Elem x (x :: xs)
441− | there : Elem x xs -> Elem x (y :: xs);
443+ | there : Elem x xs -> Elem x (y :: xs)
444\end{SaveVerbatim}
446\end{SaveVerbatim}
@@ -454,7 +456,7 @@ encapsulated in the type \texttt{IO}:
455\begin{SaveVerbatim}{ioty}
457\begin{SaveVerbatim}{ioty}
457−data IO a; -- IO operation returning a value of type a
459+data IO a -- IO operation returning a value of type a
459\end{SaveVerbatim}
461\end{SaveVerbatim}
460\useverb{ioty}
462\useverb{ioty}
@@ -467,8 +469,8 @@ program:
468\begin{SaveVerbatim}{main}
470\begin{SaveVerbatim}{main}
470−main : IO ();
472+main : IO ()
471−main = putStrLn "Hello world";
473+main = putStrLn "Hello world"
473\end{SaveVerbatim}
475\end{SaveVerbatim}
474\useverb{main}
476\useverb{main}
@@ -479,8 +481,8 @@ outputs a string without a newline:
480\begin{SaveVerbatim}{putstr}
482\begin{SaveVerbatim}{putstr}
482−putStrLn : String -> IO ();
484+putStrLn : String -> IO ()
483−putStr : String -> IO ();
485+putStr : String -> IO ()
485\end{SaveVerbatim}
487\end{SaveVerbatim}
@@ -488,7 +490,7 @@ We can also read strings from user input:
489\begin{SaveVerbatim}{getline}
491\begin{SaveVerbatim}{getline}
491−getLine : IO String;
493+getLine : IO String
493\end{SaveVerbatim}
495\end{SaveVerbatim}
494\useverb{getline}
496\useverb{getline}
@@ -499,17 +501,17 @@ writing files, including:
500\begin{SaveVerbatim}{fileops}
502\begin{SaveVerbatim}{fileops}
502−data File; -- abstract
504+data File -- abstract
503−data Mode = Read | Write | ReadWrite;
505+data Mode = Read | Write | ReadWrite
505−openFile : String -> Mode -> IO File;
507+openFile : String -> Mode -> IO File
506−closeFile : File -> IO ();
508+closeFile : File -> IO ()
508−fread : File -> IO String;
510+fread : File -> IO String
509−fwrite : File -> String -> IO ();
511+fwrite : File -> String -> IO ()
510−feof : File -> IO Bool;
512+feof : File -> IO Bool
512−readFile : String -> IO String;
514+readFile : String -> IO String
514\end{SaveVerbatim}
516\end{SaveVerbatim}
515\useverb{fileops}
517\useverb{fileops}
@@ -525,11 +527,11 @@ operations with \texttt{do} notation:
526\begin{SaveVerbatim}{greet}
528\begin{SaveVerbatim}{greet}
528−greet : IO ();
530+greet : IO ()
529−greet = do { putStr "What is your name? ";
531+greet = do { putStr "What is your name? "
530− name <- getLine;
532+ name <- getLine
531− putStrLn ("Hello " ++ name);
533+ putStrLn ("Hello " ++ name)
534\end{SaveVerbatim}
536\end{SaveVerbatim}
535\useverb{greet}
537\useverb{greet}
@@ -544,7 +546,7 @@ operation:
545\begin{SaveVerbatim}{return}
547\begin{SaveVerbatim}{return}
547−return : a -> IO a;
549+return : a -> IO a
549\end{SaveVerbatim}
551\end{SaveVerbatim}
550\useverb{return}
552\useverb{return}
@@ -566,11 +568,11 @@ We have already seen the \texttt{List} and \texttt{Vect} data types:
567\begin{SaveVerbatim}{listvec}
569\begin{SaveVerbatim}{listvec}
569−data List a = Nil | (::) a (List a);
571+data List a = Nil | (::) a (List a)
571data Vect : Set -> Nat -> Set where
573data Vect : Set -> Nat -> Set where
572 Nil : Vect a O
574 Nil : Vect a O
573− | (::) : a -> Vect a k -> Vect a (S k);
575+ | (::) : a -> Vect a k -> Vect a (S k)
575\end{SaveVerbatim}
577\end{SaveVerbatim}
576\useverb{listvec}
578\useverb{listvec}
@@ -594,13 +596,13 @@ and applies a function to every element of the list or vector.
595\begin{SaveVerbatim}{map}
597\begin{SaveVerbatim}{map}
597−map : (a -> b) -> List a -> List b;
599+map : (a -> b) -> List a -> List b
598−map f [] = [];
600+map f [] = []
599−map f (x :: xs) = f x :: map f xs;
601+map f (x :: xs) = f x :: map f xs
601−map : (a -> b) -> Vect a n -> Vect b n;
603+map : (a -> b) -> Vect a n -> Vect b n
602−map f [] = [];
604+map f [] = []
603−map f (x :: xs) = f x :: map f xs;
605+map f (x :: xs) = f x :: map f xs
605\end{SaveVerbatim}
607\end{SaveVerbatim}
606\useverb{map}
608\useverb{map}
@@ -610,11 +612,11 @@ For example, to double every element in a vector of integers:
611\begin{SaveVerbatim}{dvect}
613\begin{SaveVerbatim}{dvect}
613−intVec : Vect Int 5;
615+intVec : Vect Int 5
614−intVec = [1, 2, 3, 4, 5];
616+intVec = [1, 2, 3, 4, 5]
616−double : Int -> Int;
618+double : Int -> Int
617−double x = x * 2;
619+double x = x * 2
619\end{SaveVerbatim}
621\end{SaveVerbatim}
620\useverb{dvect}
622\useverb{dvect}
@@ -679,7 +681,7 @@ or there isn't:
680\begin{SaveVerbatim}{maybe}
682\begin{SaveVerbatim}{maybe}
682−data Maybe a = Just a | Nothing;
684+data Maybe a = Just a | Nothing
684\end{SaveVerbatim}
686\end{SaveVerbatim}
685\useverb{maybe}
687\useverb{maybe}
@@ -691,10 +693,10 @@ bounds error:
692\begin{SaveVerbatim}{lookuplist}
694\begin{SaveVerbatim}{lookuplist}
694−list_lookup : Nat -> List a -> Maybe a;
696+list_lookup : Nat -> List a -> Maybe a
695−list_lookup _ Nil = Nothing;
697+list_lookup _ Nil = Nothing
696−list_lookup O (x :: xs) = Just x;
698+list_lookup O (x :: xs) = Just x
697−list_lookup (S k) (x :: xs) = list_lookup k xs;
699+list_lookup (S k) (x :: xs) = list_lookup k xs
699\end{SaveVerbatim}
701\end{SaveVerbatim}
700\useverb{lookuplist}
702\useverb{lookuplist}
@@ -705,7 +707,7 @@ either by applying a function to the value, if there is one, or by providing a d
706\begin{SaveVerbatim}{maybefn}
708\begin{SaveVerbatim}{maybefn}
708−maybe : Maybe a -> |(default:b) -> (a -> b) -> b;
710+maybe : Maybe a -> |(default:b) -> (a -> b) -> b
710\end{SaveVerbatim}
712\end{SaveVerbatim}
711\useverb{maybefn}
713\useverb{maybefn}
@@ -724,7 +726,7 @@ Values can be paired with the following built-in data type:
725\begin{SaveVerbatim}{mkpair}
727\begin{SaveVerbatim}{mkpair}
727−data Pair a b = MkPair a b;
729+data Pair a b = MkPair a b
729\end{SaveVerbatim}
731\end{SaveVerbatim}
730\useverb{mkpair}
732\useverb{mkpair}
@@ -736,11 +738,11 @@ Tuples can contain an arbitrary number of values, represented as nested pairs:
737\begin{SaveVerbatim}{tuple}
739\begin{SaveVerbatim}{tuple}
739−fred : (String, Int);
741+fred : (String, Int)
740−fred = ("Fred", 42);
742+fred = ("Fred", 42)
742−jim : (String, Int, String);
744+jim : (String, Int, String)
743−jim = ("Jim", 25, "Cambridge");
745+jim = ("Jim", 25, "Cambridge")
745\end{SaveVerbatim}
747\end{SaveVerbatim}
746\useverb{tuple}
748\useverb{tuple}
@@ -753,7 +755,7 @@ the value of the first element:
753\begin{SaveVerbatim}{sigma}
755\begin{SaveVerbatim}{sigma}
755data Exists : (A : Set) -> (P : A -> Set) -> Set where
757data Exists : (A : Set) -> (P : A -> Set) -> Set where
756− Ex_intro : {P : A -> Set} -> (a : A) -> P a -> Exists A P;
758+ Ex_intro : {P : A -> Set} -> (a : A) -> P a -> Exists A P
758\end{SaveVerbatim}
760\end{SaveVerbatim}
759\useverb{sigma}
761\useverb{sigma}
@@ -766,8 +768,8 @@ constructs a value of this type. For example, we can pair a number with a
767\begin{SaveVerbatim}{dvec}
769\begin{SaveVerbatim}{dvec}
769−vec : (n : Nat ** Vect Int n);
771+vec : (n : Nat ** Vect Int n)
770−vec = (2 ** [3, 4]);
772+vec = (2 ** [3, 4])
772\end{SaveVerbatim}
774\end{SaveVerbatim}
773\useverb{dvec}
775\useverb{dvec}
@@ -780,8 +782,8 @@ written as:
781\begin{SaveVerbatim}{dvec1}
783\begin{SaveVerbatim}{dvec1}
783−vec : (n : Nat ** Vect Int n);
785+vec : (n : Nat ** Vect Int n)
784−vec = (_ ** [3, 4]);
786+vec = (_ ** [3, 4])
786\end{SaveVerbatim}
788\end{SaveVerbatim}
787\useverb{dvec1}
789\useverb{dvec1}
@@ -792,8 +794,8 @@ again, it can be inferred:
793\begin{SaveVerbatim}{dvec2}
795\begin{SaveVerbatim}{dvec2}
795−vec : (n ** Vect Int n);
797+vec : (n ** Vect Int n)
796−vec = (_ ** [3, 4]);
798+vec = (_ ** [3, 4])
798\end{SaveVerbatim}
800\end{SaveVerbatim}
799\useverb{dvec2}
801\useverb{dvec2}
@@ -806,7 +808,7 @@ length of the resulting vector will be:
807\begin{SaveVerbatim}{vfilter}
809\begin{SaveVerbatim}{vfilter}
809−filter : (a -> Bool) -> Vect a n -> (p ** Vect a p);
811+filter : (a -> Bool) -> Vect a n -> (p ** Vect a p)
811\end{SaveVerbatim}
813\end{SaveVerbatim}
812\useverb{vfilter}
814\useverb{vfilter}
@@ -816,7 +818,7 @@ If the \texttt{Vect} is empty, the result is easy:
817\begin{SaveVerbatim}{vfilternil}
819\begin{SaveVerbatim}{vfilternil}
819−vfilter p VNil = (_ , []);
821+vfilter p VNil = (_ , [])
821\end{SaveVerbatim}
823\end{SaveVerbatim}
822\useverb{vfilternil}
824\useverb{vfilternil}
@@ -830,7 +832,7 @@ notation. with allows pattern matching on intermediate values:
830\begin{SaveVerbatim}{vfiltercons}
832\begin{SaveVerbatim}{vfiltercons}
832filter p (x :: xs) with filter p xs {
834filter p (x :: xs) with filter p xs {
833− | (_ , xs') = if (p x) then (_ , x :: xs') else (_ , xs');
835+ | (_ , xs') = if (p x) then (_ , x :: xs') else (_ , xs')
836\end{SaveVerbatim}
838\end{SaveVerbatim}
@@ -847,9 +849,9 @@ Intermediate values can be calculated using \texttt{let} bindings:
848\begin{SaveVerbatim}{letb}
850\begin{SaveVerbatim}{letb}
850−mirror : List a -> List a;
852+mirror : List a -> List a
851mirror xs = let xs' = rev xs in
853mirror xs = let xs' = rev xs in
852− app xs xs';
854+ app xs xs'
854\end{SaveVerbatim}
856\end{SaveVerbatim}
855\useverb{letb}
857\useverb{letb}
@@ -860,11 +862,11 @@ fields from a record as follows, as well as by pattern matching at the top level
861\begin{SaveVerbatim}{letp}
863\begin{SaveVerbatim}{letp}
863−data Person = MkPerson String Int;
865+data Person = MkPerson String Int
865−showPerson : Person -> String;
867+showPerson : Person -> String
866showPerson p = let MkPerson name age = p in
868showPerson p = let MkPerson name age = p in
867− name ++ " is " ++ show age ++ " years old";
869+ name ++ " is " ++ show age ++ " years old"
869\end{SaveVerbatim}
871\end{SaveVerbatim}
870\useverb{letp}
872\useverb{letp}
@@ -891,9 +893,9 @@ follows:
892\begin{SaveVerbatim}{triples}
894\begin{SaveVerbatim}{triples}
894−pythag : Int -> List (Int, Int, Int);
896+pythag : Int -> List (Int, Int, Int)
895pythag n = [ (x, y, z) | z <- [1..n], y <- [1..z], x <- [1..y],
897pythag n = [ (x, y, z) | z <- [1..n], y <- [1..z], x <- [1..y],
896− x*x + y*y == z*z ];
898+ x*x + y*y == z*z ]
898\end{SaveVerbatim}
900\end{SaveVerbatim}
899\useverb{triples}
901\useverb{triples}
@@ -913,10 +915,10 @@ The following function, for example, splits a string into two at a given charact
914\begin{SaveVerbatim}{split}
916\begin{SaveVerbatim}{split}
916−splitAt : Char -> String -> (String, String);
918+splitAt : Char -> String -> (String, String)
917splitAt c x = case break (== c) x of {
919splitAt c x = case break (== c) x of {
918 (x, y) => (x, strTail y)
920 (x, y) => (x, strTail y)
921\end{SaveVerbatim}
923\end{SaveVerbatim}
922\useverb{split}
924\useverb{split}
@@ -934,11 +936,11 @@ looks up an index and returns a default value if the index is out of bounds:
935\begin{SaveVerbatim}{listlookup}
937\begin{SaveVerbatim}{listlookup}
937−lookup_default : Nat -> List a -> a -> a;
939+lookup_default : Nat -> List a -> a -> a
938lookup_default i xs def = case list_lookup i xs of {
940lookup_default i xs def = case list_lookup i xs of {
939 Nothing => def
941 Nothing => def
940 | Just x => x
942 | Just x => x
943\end{SaveVerbatim}
945\end{SaveVerbatim}
944\useverb{listlookup}
946\useverb{listlookup}