@@ -11,8 +11,7 @@ We can declare some constants with these types. Enter the following
1111into a file \texttt{prims.idr} and load it into the \Idris{} interactive
1212environment by typing \texttt{idris prims.idr}:
1313
14−\begin{SaveVerbatim}{constprims}
15−
14+\begin{code}
1615module prims
1716
1817x : Int
@@ -26,9 +25,7 @@ bar = 'Z'
2625
2726quux : Bool
2827quux = False
29−
30−\end{SaveVerbatim}
31−\useverb{constprims}
28+\end{code}
3229
3330\noindent
3431An \Idris{} file consists of an optional module declaration (here
@@ -50,15 +47,12 @@ functions. The prelude defines several arithmetic and comparison operators,
5047which we can use at the prompt. Evaluating things at the prompt gives an
5148answer, and the type of the answer. For example:
5249
53−\begin{SaveVerbatim}{promptprim}
54−
50+\begin{lstlisting}
5551*prims> 6*6+6
565242 : Int
5753*prims> x == 6*6+6
5854True : Bool
59−
60−\end{SaveVerbatim}
61−\useverb{promptprim}
55+\end{lstlisting}
6256
6357\noindent
6458All of the usual arithmetic and comparison operators are defined for the primitive
@@ -66,27 +60,21 @@ types. They are overloaded using type classes, as we will discuss in Section
6660\ref{sec:classes} and can be extended to work on user defined types.
6761Boolean expressions can be tested with the \texttt{if...then...else} construct:
6862
69−\begin{SaveVerbatim}{ifthenelse}
70−
63+\begin{lstlisting}
7164*prims> if x == 6 * 6 + 6 then "The answer!" else "Not the answer"
7265"The answer!" : String
73−
74−\end{SaveVerbatim}
75−\useverb{ifthenelse}
66+\end{lstlisting}
7667
7768\subsection{Data Types}
7869
7970Data types are declared in a similar way to Haskell data types, with a similar
8071syntax. Natural numbers and lists, for example, can be declared as follows:
8172
82−\begin{SaveVerbatim}{natlist}
83−
73+\begin{code}
8474data Nat = Z | S Nat -- Natural numbers
8575 -- (zero and successor)
8676data List a = Nil | (::) a (List a) -- Polymorphic lists
87−
88−\end{SaveVerbatim}
89−\useverb{natlist}
77+\end{code}
9078
9179\noindent
9280The above declarations are taken from the standard library. Unary natural
@@ -97,28 +85,18 @@ or a value added to the front of another list (\texttt{x :: xs}).
9785In the declaration for \tTC{List}, we used an infix operator \tDC{::}. New operators
9886such as this can be added using a fixity declaration, as follows:
9987
100−\begin{SaveVerbatim}{infixcons}
101−
88+\begin{code}
10289infixr 10 ::
103−
104−\end{SaveVerbatim}
105−\useverb{infixcons}
90+\end{code}
10691
10792\noindent
10893Functions, data constructors and type constuctors may all be given infix
10994operators as names. They may be used in prefix form if enclosed in brackets,
11095e.g. \tDC{(::)}. Infix operators can use any of the symbols:
11196
112−\begin{SaveVerbatim}{infixsyms}
113−
97+\begin{lstlisting}
11498:+-*/=_.?|&><!@$%^~.
115−
116−\end{SaveVerbatim}
117−\useverb{infixsyms}
118−
119−\begin{SaveVerbatim}{argh}
120−$
121−\end{SaveVerbatim}
99+\end{lstlisting}
122100
123101\subsection{Functions}
124102
@@ -128,8 +106,7 @@ functions, using a single colon \texttt{:} (rather than
128106Haskell's double colon \texttt{::}). Some natural number arithmetic functions can be
129107defined as follows, again taken from the standard library:
130108
131−\begin{SaveVerbatim}{natfns}
132−
109+\begin{code}
133110-- Unary addition
134111plus : Nat -> Nat -> Nat
135112plus Z y = y
@@ -139,9 +116,7 @@ plus (S k) y = S (plus k y)
139116mult : Nat -> Nat -> Nat
140117mult Z y = Z
141118mult (S k) y = plus y (mult k y)
142−
143−\end{SaveVerbatim}
144−\useverb{natfns}
119+\end{code}
145120
146121\noindent
147122The standard arithmetic operators \texttt{+} and \texttt{*} are also overloaded
@@ -154,29 +129,23 @@ all part of the same namespace.
154129
155130We can test these functions at the \Idris{} prompt:
156131
157−\begin{SaveVerbatim}{fntest}
158−
132+\begin{lstlisting}
159133Idris> plus (S (S Z)) (S (S Z))
160134S (S (S (S Z))) : Nat
161135Idris> mult (S (S (S Z))) (plus (S (S Z)) (S (S Z)))
162136S (S (S (S (S (S (S (S (S (S (S (S Z))))))))))) : Nat
163−
164−\end{SaveVerbatim}
165−\useverb{fntest}
137+\end{lstlisting}
166138
167139\noindent
168140Like arithmetic operations, integer literals are also overloaded using type classes,
169141meaning that we can also test the functions as follows:
170142
171−\begin{SaveVerbatim}{fntest}
172−
143+\begin{lstlisting}
173144Idris> plus 2 2
174145S (S (S (S Z))) : Nat
175146Idris> mult 3 (plus 2 2)
176147S (S (S (S (S (S (S (S (S (S (S (S Z))))))))))) : Nat
177−
178−\end{SaveVerbatim}
179−\useverb{fntest}
148+\end{lstlisting}
180149
181150\noindent
182151You may wonder, by the way, why we have unary natural numbers when our
@@ -194,16 +163,13 @@ Functions can also be defined \emph{locally} using \texttt{where} clauses. For e
194163to define a function which reverses a list, we can use an auxiliary function which
195164accumulates the new, reversed list, and which does not need to be visible globally:
196165
197−\begin{SaveVerbatim}{revwhere}
198−
166+\begin{code}
199167reverse : List a -> List a
200168reverse xs = revAcc [] xs where
201169 revAcc : List a -> List a -> List a
202170 revAcc acc [] = acc
203171 revAcc acc (x :: xs) = revAcc (x :: acc) xs
204−
205−\end{SaveVerbatim}
206−\useverb{revwhere}
172+\end{code}
207173
208174\noindent
209175Indentation is significant --- functions in the \texttt{where} block must be indented
@@ -220,8 +186,7 @@ As well as functions, \texttt{where} blocks can include local data
220186declarations, such as the following where \texttt{MyLT} is not accessible outside
221187the definition of \texttt{foo}:
222188
223−\begin{SaveVerbatim}{wheredata}
224−
189+\begin{code}
225190foo : Int -> Int
226191foo x = case isLT of
227192 Yes => x*2
@@ -231,9 +196,7 @@ foo x = case isLT of
231196
232197 isLT : MyLT
233198 isLT = if x < 20 then Yes else No
234−
235−\end{SaveVerbatim}
236−\useverb{wheredata}
199+\end{code}
237200
238201\noindent
239202In general, functions defined in a \texttt{where} clause need a type declaration
@@ -249,8 +212,7 @@ application
249212\noindent
250213So, for example, the following definitions are legal:
251214
252−\begin{SaveVerbatim}{whereinfer}
253−
215+\begin{code}
254216even : Nat -> Bool
255217even Z = True
256218even (S k) = odd k where
@@ -262,10 +224,7 @@ test = [c (S 1), c Z, d (S Z)]
262224 where c x = 42 + x
263225 d y = c (y + 1 + z y)
264226 where z w = y + w
265−
266−\end{SaveVerbatim}
267−\useverb{whereinfer}
268−
227+\end{code}
269228
270229\subsection{Dependent Types}
271230
@@ -276,14 +235,11 @@ conventionally called vectors in the dependent type literature. In \Idris{},
276235we declare vectors as follows\footnote{Note that prior to \Idris{} 0.9.9,
277236the order of the arguments to \texttt{Vect} was reversed.}:
278237
279−\begin{SaveVerbatim}{vect}
280−
238+\begin{code}
281239data Vect : Nat -> Type -> Type where
282240 Nil : Vect Z a
283241 (::) : a -> Vect k a -> Vect (S k) a
284−
285−\end{SaveVerbatim}
286−\useverb{vect}
242+\end{code}
287243
288244\noindent
289245Note that we have used the same constructor names as for \tTC{List}. Ad-hoc
@@ -308,42 +264,31 @@ The type of a function over \tTC{Vect} will describe what happens to the
308264lengths of the vectors involved. For example, \tFN{++}, defined in the
309265library, appends two \tTC{Vect}s:
310266
311−\begin{SaveVerbatim}{vapp}
312−
267+\begin{code}
313268(++) : Vect n a -> Vect m a -> Vect (n + m) a
314269(++) Nil ys = ys
315270(++) (x :: xs) ys = x :: xs ++ ys
316−
317−\end{SaveVerbatim}
318−\useverb{vapp}
271+\end{code}
319272
320273\noindent
321274The type of \tFN{(++)} states that the resulting vector's length will be the sum of
322275the input lengths. If we get the definition wrong in such a way that this does
323276not hold, \Idris{} will not accept the definition. For example:
324277
325−\begin{SaveVerbatim}{vapperr}
326−
278+\begin{code}
327279(++) : Vect n a -> Vect m a -> Vect (n + m) a
328280(++) Nil ys = ys
329281(++) (x :: xs) ys = x :: xs ++ xs -- BROKEN
282+\end{code}
330283
331−\end{SaveVerbatim}
332−\useverb{vapperr}
333−
334−\begin{SaveVerbatim}{vapperrmsg}
335−
284+\begin{lstlisting}
336285$ idris vbroken.idr --check
337286vbroken.idr:3:Can't unify Vect n a with Vect m a
338287
339288Specifically:
340289 Can't unify n with m
290+\end{lstlisting}
341291
342−\end{SaveVerbatim}
343−\begin{SaveVerbatim}{bleh}
344−$
345−\end{SaveVerbatim}
346−\useverb{vapperrmsg}
347292
348293\noindent
349294This error message suggests that there is a length mismatch between two vectors
@@ -357,14 +302,11 @@ of length \texttt{n}.
357302Finite sets, as the name suggests, are sets with a finite number of elements.
358303They are declared as follows (again, in the prelude):
359304
360−\begin{SaveVerbatim}{findecl}
361−
305+\begin{code}
362306data Fin : Nat -> Type where
363307 fZ : Fin (S k)
364308 fS : Fin k -> Fin (S k)
365−
366−\end{SaveVerbatim}
367−\useverb{findecl}
309+\end{code}
368310
369311\noindent
370312\tDC{fZ} is the zeroth element of a finite set with \texttt{S k} elements;
@@ -382,14 +324,11 @@ numbers bounded by \tTC{n}.
382324For example, the following function which looks up an element in a \tTC{Vect},
383325by a bounded index given as a \tTC{Fin n}, is defined in the prelude:
384326
385−\begin{SaveVerbatim}{vindex}
386−
327+\begin{code}
387328index : Fin n -> Vect n a -> a
388329index fZ (x :: xs) = x
389330index (fS k) (x :: xs) = index k xs
390−
391−\end{SaveVerbatim}
392−\useverb{vindex}
331+\end{code}
393332
394333\noindent
395334This function looks up a value at a given location in a vector. The location is
@@ -407,12 +346,9 @@ since it would force \texttt{n} to be \tDC{Z}.
407346
408347Let us take a closer look at the type of \texttt{index}:
409348
410−\begin{SaveVerbatim}{vindexty}
411−
349+\begin{code}
412350index : Fin n -> Vect n a -> a
413−
414−\end{SaveVerbatim}
415−\useverb{vindexty}
351+\end{code}
416352
417353\noindent
418354It takes two arguments, an element of the finite set of \texttt{n} elements, and a vector
@@ -420,12 +356,9 @@ with \texttt{n} elements of type \texttt{a}. But there are also two names,
420356\texttt{n} and \texttt{a}, which are not declared explictly. These are \emph{implicit}
421357arguments to \texttt{index}. We could also write the type of \texttt{index} as:
422358
423−\begin{SaveVerbatim}{vindeximpty}
424−
359+\begin{code}
425360index : {a:Type} -> {n:Nat} -> Fin n -> Vect n a -> a
426−
427−\end{SaveVerbatim}
428−\useverb{vindeximpty}
361+\end{code}
429362
430363\noindent
431364Implicit arguments, given in braces \texttt{\{\}} in the type declaration, are
@@ -437,23 +370,17 @@ free, will be automatically bound as an implicit argument. Implicit arguments
437370can still be given explicitly in applications, using \texttt{\{a=value\}} and
438371\texttt{\{n=value\}}, for example:
439372
440−\begin{SaveVerbatim}{vindexexp}
441−
373+\begin{code}
442374index {a=Int} {n=2} fZ (2 :: 3 :: Nil)
443−
444−\end{SaveVerbatim}
445−\useverb{vindexexp}
375+\end{code}
446376
447377\noindent
448378In fact, any argument, implicit or explicit, may be given a name. We could have
449379declared the type of \texttt{index} as:
450380
451−\begin{SaveVerbatim}{vindexn}
452−
381+\begin{code}
453382index : (i:Fin n) -> (xs:Vect n a) -> a
454−
455−\end{SaveVerbatim}
456−\useverb{vindexn}
383+\end{code}
457384
458385\noindent
459386It is a matter of taste whether you want to do this --- sometimes it can help
@@ -468,14 +395,11 @@ before being used --- or if an implicit argument has a complex type. For example
468395we will need to state the types of the implicit arguments in the following
469396definition, which defines a predicate on vectors:
470397
471−\begin{SaveVerbatim}{elem}
472−
398+\begin{code}
473399data Elem : a -> Vect n a -> Type where
474400 here : {x:a} -> {xs:Vect n a} -> Elem x (x :: xs)
475401 there : {x,y:a} -> {xs:Vect n a} -> Elem x xs -> Elem x (y :: xs)
476−
477−\end{SaveVerbatim}
478−\useverb{elem}
402+\end{code}
479403
480404\noindent
481405An instance of \texttt{Elem x xs} states that \texttt{x} is an element of
@@ -483,31 +407,26 @@ An instance of \texttt{Elem x xs} states that \texttt{x} is an element of
483407such a predicate if the required element is \texttt{here}, at the head of the vector,
484408or \texttt{there}, in the tail of the vector. For example:
485409
486−\begin{SaveVerbatim}{testelem}
487−
410+\begin{code}
488411testVec : Vect 4 Int
489412testVec = 3 :: 4 :: 5 :: 6 :: Nil
490413
491414inVect : Elem 5 testVec
492415inVect = there (there here)
493−
494−\end{SaveVerbatim}
495−\useverb{testelem}
416+\end{code}
496417
497418\noindent
498419If the same implicit arguments are being used a lot, it can make a definition
499420difficult to read. To avoid this problem, a \texttt{using} block gives the types and
500421ordering of any implicit arguments which can appear within the block:
501422
502−\begin{SaveVerbatim}{elemusing}
503−
423+\begin{code}
504424using (x:a, y:a, xs:Vect n a)
505425 data Elem : a -> Vect n a -> Type where
506426 here : Elem x (x :: xs)
507427 there : Elem x xs -> Elem x (y :: xs)
428+\end{code}
508429
509−\end{SaveVerbatim}
510−\useverb{elemusing}
511430
512431\subsubsection*{Note: Declaration Order and \texttt{mutual} blocks}
513432
@@ -517,8 +436,7 @@ reduction behaviour to affect type checking. However, this
517436restriction can be relaxed by using a \texttt{mutual} block, which allows
518437data types and functions to be defined simultaneously:
519438
520−\begin{SaveVerbatim}{mutual}
521−
439+\begin{code}
522440mutual
523441 even : Nat -> Bool
524442 even Z = True
@@ -527,9 +445,8 @@ mutual
527445 odd : Nat -> Bool
528446 odd Z = False
529447 odd (S k) = even k
448+\end{code}
530449
531−\end{SaveVerbatim}
532−\useverb{mutual}
533450
534451\noindent
535452In a \texttt{mutual} block, first all of the type declarations are added,
@@ -544,12 +461,10 @@ that is, a language where expressions do not have side-effects --- is that I/O
544461is inherently side-effecting. Therefore in \Idris{}, such interactions are
545462encapsulated in the type \texttt{IO}:
546463
547−\begin{SaveVerbatim}{ioty}
548−
464+\begin{code}
549465data IO a -- IO operation returning a value of type a
466+\end{code}
550467
551−\end{SaveVerbatim}
552−\useverb{ioty}
553468
554469\noindent
555470We'll leave the definition of \texttt{IO} abstract, but effectively it describes what
@@ -558,41 +473,34 @@ resulting operations are executed externally,
558473by the run-time system. We've already seen one IO
559474program:
560475
561−\begin{SaveVerbatim}{main}
562−
476+\begin{code}
563477main : IO ()
564478main = putStrLn "Hello world"
479+\end{code}
565480
566−\end{SaveVerbatim}
567−\useverb{main}
568481
569482\noindent
570483The type of \texttt{putStrLn} explains that it takes a string, and returns an
571484element of the unit type () via an I/O action. There is a variant \texttt{putStr} which
572485outputs a string without a newline:
573486
574−\begin{SaveVerbatim}{putstr}
575−
487+\begin{code}
576488putStrLn : String -> IO ()
577489putStr : String -> IO ()
578−
579−\end{SaveVerbatim}
490+\end{code}
580491
581492We can also read strings from user input:
582493
583−\begin{SaveVerbatim}{getline}
584−
494+\begin{code}
585495getLine : IO String
496+\end{code}
586497
587−\end{SaveVerbatim}
588−\useverb{getline}
589498
590499\noindent
591500A number of other I/O operations are defined in the prelude, for example for reading and
592501writing files, including:
593502
594−\begin{SaveVerbatim}{fileops}
595−
503+\begin{code}
596504data File -- abstract
597505data Mode = Read | Write | ReadWrite
598506
@@ -604,9 +512,8 @@ fwrite : File -> String -> IO ()
604512feof : File -> IO Bool
605513
606514readFile : String -> IO String
515+\end{code}
607516
608−\end{SaveVerbatim}
609−\useverb{fileops}
610517
611518\subsection{``\texttt{do}'' notation}
612519
@@ -617,15 +524,13 @@ computation into the input of the next. \texttt{IO} is an abstract type, however
617524can't access the result of a computation directly. Instead, we sequence
618525operations with \texttt{do} notation:
619526
620−\begin{SaveVerbatim}{greet}
621−
527+\begin{code}
622528greet : IO ()
623529greet = do putStr "What is your name? "
624530 name <- getLine
625531 putStrLn ("Hello " ++ name)
532+\end{code}
626533
627−\end{SaveVerbatim}
628−\useverb{greet}
629534
630535\noindent
631536The syntax \texttt{x <- iovalue} executes the I/O operation \texttt{iovalue}, of type
@@ -637,12 +542,10 @@ statement in the do block must begin in the same column.
637542The \texttt{return} operation allows us to inject a value directly into an IO
638543operation:
639544
640−\begin{SaveVerbatim}{return}
641−
545+\begin{code}
642546return : a -> IO a
547+\end{code}
643548
644−\end{SaveVerbatim}
645−\useverb{return}
646549
647550\noindent
648551As we will see later, \texttt{do} notation is more general than this, and can be
@@ -659,16 +562,14 @@ part of \texttt{prelude.idr}.
659562
660563We have already seen the \texttt{List} and \texttt{Vect} data types:
661564
662−\begin{SaveVerbatim}{listvec}
663−
565+\begin{code}
664566data List a = Nil | (::) a (List a)
665567
666568data Vect : Nat -> Type -> Type where
667569 Nil : Vect Z a
668570 (::) : a -> Vect k a -> Vect (S k) a
571+\end{code}
669572
670−\end{SaveVerbatim}
671−\useverb{listvec}
672573
673574\noindent
674575Note that the constructor names are the same for each --- constructor names (in
@@ -687,8 +588,7 @@ The library also defines a number of functions for manipulating these types.
687588\texttt{map} is overloaded both for \texttt{List} and \texttt{Vect}
688589and applies a function to every element of the list or vector.
689590
690−\begin{SaveVerbatim}{map}
691−
591+\begin{code}
692592map : (a -> b) -> List a -> List b
693593map f [] = []
694594map f (x :: xs) = f x :: map f xs
@@ -696,34 +596,29 @@ map f (x :: xs) = f x :: map f xs
696596map : (a -> b) -> Vect n a -> Vect n b
697597map f [] = []
698598map f (x :: xs) = f x :: map f xs
599+\end{code}
699600
700−\end{SaveVerbatim}
701−\useverb{map}
702601
703602\noindent
704603For example, to double every element in a vector of integers:
705604
706−\begin{SaveVerbatim}{dvect}
707−
605+\begin{code}
708606intVec : Vect 5 Int
709607intVec = [1, 2, 3, 4, 5]
710608
711609double : Int -> Int
712610double x = x * 2
611+\end{code}
713612
714−\end{SaveVerbatim}
715−\useverb{dvect}
716613
717614\noindent
718615You'll find these examples in \texttt{usefultypes.idr} in the \texttt{examples/} directory:
719616
720−\begin{SaveVerbatim}{rundvect}
721−
617+\begin{lstlisting}
722618*usefultypes> show (map double intVec)
723619"[2, 4, 6, 8, 10]" : String
620+\end{lstlisting}
724621
725−\end{SaveVerbatim}
726−\useverb{rundvect}
727622
728623\noindent
729624For more details of the functions available on \texttt{List} and \texttt{Vect},
@@ -737,13 +632,11 @@ need, please feel free to add it and submit a patch!
737632There are actually neater ways to write the above expression. One way would be
738633to use an anonymous function:
739634
740−\begin{SaveVerbatim}{lam1}
741−
635+\begin{lstlisting}
742636*usefultypes> show (map (\x => x * 2) intVec)
743637"[2, 4, 6, 8, 10]" : String
638+\end{lstlisting}
744639
745−\end{SaveVerbatim}
746−\useverb{lam1}
747640
748641\noindent
749642The notation \texttt{$\backslash$x => val} constructs an anonymous function
@@ -754,13 +647,11 @@ types, e.g. \texttt{$\backslash$x : Int => x * 2}, and can pattern match,
754647e.g. \texttt{$\backslash$ (x, y) => x + y}.
755648We could also use an operator section:
756649
757−\begin{SaveVerbatim}{sec1}
758−
650+\begin{code}
759651*usefultypes> show (map (* 2) intVec)
760652"[2, 4, 6, 8, 10]" : String
653+\end{code}
761654
762−\end{SaveVerbatim}
763−\useverb{sec1}
764655
765656\noindent
766657\texttt{(*2)} is shorthand for a function which multiplies a number by 2. It expands to
@@ -772,38 +663,32 @@ Similarly, \texttt{(2*)} would expand to \texttt{$\backslash$x => 2 * x}.
772663\texttt{Maybe} describes an optional value. Either there is a value of the given type,
773664or there isn't:
774665
775−\begin{SaveVerbatim}{maybe}
776−
666+\begin{code}
777667data Maybe a = Just a | Nothing
668+\end{code}
778669
779−\end{SaveVerbatim}
780−\useverb{maybe}
781670
782671\noindent
783672\texttt{Maybe} is one way of giving a type to an operation that may fail. For example,
784673looking something up in a \texttt{List} (rather than a vector) may result in an out of
785674bounds error:
786675
787−\begin{SaveVerbatim}{lookuplist}
788−
676+\begin{code}
789677list_lookup : Nat -> List a -> Maybe a
790678list_lookup _ Nil = Nothing
791679list_lookup Z (x :: xs) = Just x
792680list_lookup (S k) (x :: xs) = list_lookup k xs
681+\end{code}
793682
794−\end{SaveVerbatim}
795−\useverb{lookuplist}
796683
797684\noindent
798685The \texttt{maybe} function is used to process values of type \texttt{Maybe},
799686either by applying a function to the value, if there is one, or by providing a default value:
800687
801−\begin{SaveVerbatim}{maybefn}
802−
688+\begin{code}
803689maybe : Maybe a -> |(default:b) -> (a -> b) -> b
690+\end{code}
804691
805−\end{SaveVerbatim}
806−\useverb{maybefn}
807692
808693\noindent
809694The vertical bar $\mid$ before the default value is a laziness annotation. Normally
@@ -817,41 +702,35 @@ needed.
817702
818703Values can be paired with the following built-in data type:
819704
820−\begin{SaveVerbatim}{mkpair}
821−
705+\begin{code}
822706data Pair a b = MkPair a b
707+\end{code}
823708
824−\end{SaveVerbatim}
825−\useverb{mkpair}
826709
827710\noindent
828711As syntactic sugar, we can write \texttt{(a, b)} which, according to context,
829712means either \texttt{Pair a b} or \texttt{MkPair a b}.
830713Tuples can contain an arbitrary number of values, represented as nested pairs:
831714
832−\begin{SaveVerbatim}{tuple}
833−
715+\begin{code}
834716fred : (String, Int)
835717fred = ("Fred", 42)
836718
837719jim : (String, Int, String)
838720jim = ("Jim", 25, "Cambridge")
721+\end{code}
839722
840−\end{SaveVerbatim}
841−\useverb{tuple}
842723
843724\subsubsection*{Dependent Pairs}
844725
845726Dependent pairs allow the type of the second element of a pair to depend on
846727the value of the first element:
847728
848−\begin{SaveVerbatim}{sigma}
849−
729+\begin{code}
850730data Exists : (A : Type) -> (P : A -> Type) -> Type where
851731 Ex_intro : {P : A -> Type} -> (a : A) -> P a -> Exists A P
732+\end{code}
852733
853−\end{SaveVerbatim}
854−\useverb{sigma}
855734
856735\noindent
857736Again, there is syntactic sugar for this. \texttt{(a : A ** P)} is the type of a pair of
@@ -859,13 +738,11 @@ A and P, where the name \texttt{a} can occur inside P. \texttt{( a ** p )}
859738constructs a value of this type. For example, we can pair a number with a
860739\texttt{Vect} of a particular length.
861740
862−\begin{SaveVerbatim}{dvec}
863−
741+\begin{code}
864742vec : (n : Nat ** Vect n Int)
865743vec = (2 ** [3, 4])
744+\end{code}
866745
867−\end{SaveVerbatim}
868−\useverb{dvec}
869746
870747\noindent
871748The type checker could of course infer the value of the first element from the
@@ -873,25 +750,21 @@ length of the vector. We can write an underscore \texttt{\_} in place of values
873750expect the type checker to fill in, so the above definition could also be
874751written as:
875752
876−\begin{SaveVerbatim}{dvec1}
877−
753+\begin{code}
878754vec : (n : Nat ** Vect n Int)
879755vec = (_ ** [3, 4])
756+\end{code}
880757
881−\end{SaveVerbatim}
882−\useverb{dvec1}
883758
884759\noindent
885760We might also prefer to omit the type of the first element of the pair, since,
886761again, it can be inferred:
887762
888−\begin{SaveVerbatim}{dvec2}
889−
763+\begin{code}
890764vec : (n ** Vect n Int)
891765vec = (_ ** [3, 4])
766+\end{code}
892767
893−\end{SaveVerbatim}
894−\useverb{dvec2}
895768
896769\noindent
897770One use for dependent pairs is to return values of dependent types where the
@@ -899,22 +772,18 @@ index is not necessarily known in advance. For example, if we filter elements
899772out of a \texttt{Vect} according to some predicate, we will not know in advance what the
900773length of the resulting vector will be:
901774
902−\begin{SaveVerbatim}{vfilter}
903−
775+\begin{code}
904776filter : (a -> Bool) -> Vect n a -> (p ** Vect p a)
777+\end{code}
905778
906−\end{SaveVerbatim}
907−\useverb{vfilter}
908779
909780\noindent
910781If the \texttt{Vect} is empty, the result is easy:
911782
912−\begin{SaveVerbatim}{vfilternil}
913−
783+\begin{code}
914784filter p Nil = (_ ** [])
785+\end{code}
915786
916−\end{SaveVerbatim}
917−\useverb{vfilternil}
918787
919788\noindent
920789In the \texttt{::} case, we need to inspect the result of a recursive call to
@@ -922,13 +791,10 @@ In the \texttt{::} case, we need to inspect the result of a recursive call to
922791extract the length and the vector from the result. To do this, we use \texttt{with}
923792notation, which allows pattern matching on intermediate values:
924793
925−\begin{SaveVerbatim}{vfiltercons}
926−
794+\begin{code}
927795filter p (x :: xs) with (filter p xs)
928796 | ( _ ** xs' ) = if (p x) then ( _ ** x :: xs' ) else ( _ ** xs' )
929−
930−\end{SaveVerbatim}
931−\useverb{vfiltercons}
797+\end{code}
932798
933799\noindent
934800We will see more on \texttt{with} notation later.
@@ -938,13 +804,11 @@ We will see more on \texttt{with} notation later.
938804The \texttt{so} data type is a predicate on \texttt{Bool} which guarantees that the
939805value is true:
940806
941−\begin{SaveVerbatim}{sotype}
942−
807+\begin{code}
943808data so : Bool -> Type where
944809 oh : so True
945−
946−\end{SaveVerbatim}
947−\useverb{sotype}
810+\end{code}
811+
948812
949813\noindent
950814This is most useful for providing a static guarantee that a dynamic check has been made.
@@ -952,16 +816,14 @@ For example, we might provide a safe interface to a function which draws a pixel
952816on a graphical display as follows, where \texttt{so (inBounds x y)} guarantees that
953817the point \texttt{(x,y)} is within the bounds of a 640x480 window:
954818
955−\begin{SaveVerbatim}{soexample}
956−
819+\begin{code}
957820inBounds : Int -> Int -> Bool
958821inBounds x y = x >= 0 && x < 640 && y >= 0 && y < 480
959822
960823drawPoint : (x : Int) -> (y : Int) -> so (inBounds x y) -> IO ()
961824drawPoint x y p = unsafeDrawPoint x y
962−
963−\end{SaveVerbatim}
964−\useverb{soexample}
825+\end{code}
826+
965827
966828\subsection{More Expressions}
967829
@@ -969,29 +831,25 @@ drawPoint x y p = unsafeDrawPoint x y
969831
970832Intermediate values can be calculated using \texttt{let} bindings:
971833
972−\begin{SaveVerbatim}{letb}
973−
834+\begin{code}
974835mirror : List a -> List a
975836mirror xs = let xs' = rev xs in
976837 xs ++ xs'
977−
978−\end{SaveVerbatim}
979−\useverb{letb}
838+\end{code}
839+
980840
981841\noindent
982842We can do simple pattern matching in \texttt{let} bindings too. For example, we can extract
983843fields from a record as follows, as well as by pattern matching at the top level:
984844
985−\begin{SaveVerbatim}{letp}
986−
845+\begin{code}
987846data Person = MkPerson String Int
988847
989848showPerson : Person -> String
990849showPerson p = let MkPerson name age = p in
991850 name ++ " is " ++ show age ++ " years old"
992−
993−\end{SaveVerbatim}
994−\useverb{letp}
851+\end{code}
852+
995853
996854\subsubsection*{List comprehensions}
997855
@@ -1000,12 +858,10 @@ showPerson p = let MkPerson name age = p in
1000858\Idris{} provides \emph{comprehension} notation as a convenient shorthand for
1001859building lists. The general form is:
1002860
1003−\begin{SaveVerbatim}{listcomp}
1004−
861+\begin{lstlisting}
1005862[ expression | qualifiers ]
1006−
1007−\end{SaveVerbatim}
1008−\useverb{listcomp}
863+\end{lstlisting}
864+
1009865
1010866\noindent
1011867This generates the list of values produced by evaluating the
@@ -1013,14 +869,12 @@ This generates the list of values produced by evaluating the
1013869\texttt{qualifiers}. For example, we can build a list of Pythagorean triples as
1014870follows:
1015871
1016−\begin{SaveVerbatim}{triples}
1017−
872+\begin{code}
1018873pythag : Int -> List (Int, Int, Int)
1019874pythag n = [ (x, y, z) | z <- [1..n], y <- [1..z], x <- [1..y],
1020875 x*x + y*y == z*z ]
1021−
1022−\end{SaveVerbatim}
1023−\useverb{triples}
876+\end{code}
877+
1024878
1025879\noindent
1026880The \texttt{[a..b]} notation is another shorthand which builds a list of
@@ -1035,14 +889,12 @@ Another way of inspecting intermediate values of \emph{simple} types
1035889is to use a \texttt{case} expression.
1036890The following function, for example, splits a string into two at a given character:
1037891
1038−\begin{SaveVerbatim}{split}
1039−
892+\begin{code}
1040893splitAt : Char -> String -> (String, String)
1041894splitAt c x = case break (== c) x of
1042895 (x, y) => (x, strTail y)
1043−
1044−\end{SaveVerbatim}
1045−\useverb{split}
896+\end{code}
897+
1046898
1047899\noindent
1048900\texttt{break} is a library function which breaks a string into a pair of strings
@@ -1055,29 +907,25 @@ looks up an index in a list, returning \texttt{Nothing} if the index is out
1055907of bounds. We can use this to write \texttt{lookup\_default}, which
1056908looks up an index and returns a default value if the index is out of bounds:
1057909
1058−\begin{SaveVerbatim}{listlookup}
1059−
910+\begin{code}
1060911lookup_default : Nat -> List a -> a -> a
1061912lookup_default i xs def = case list_lookup i xs of
1062913 Nothing => def
1063914 Just x => x
1064−
1065−\end{SaveVerbatim}
1066−\useverb{listlookup}
915+\end{code}
916+
1067917
1068918\noindent
1069919If the index is in bounds, we get the value at that index, otherwise we get
1070920a default value:
1071921
1072−\begin{SaveVerbatim}{deftest}
1073−
922+\begin{lstlisting}
1074923*usefultypes> lookup_default 2 [3,4,5,6] (-1)
10759245 : Int
1076925*usefultypes> lookup_default 4 [3,4,5,6] (-1)
1077926-1 : Int
1078−
1079−\end{SaveVerbatim}
1080−\useverb{deftest}
927+\end{lstlisting}
928+
1081929
1082930\noindent
1083931\textbf{Restrictions:} The \texttt{case} construct is intended for simple analysis
@@ -1099,17 +947,15 @@ determined \emph{without} type checking the \texttt{case}-expression itself.
1099947automatically generating field access and update functions. For example, we
1100948can represent a person's name and age in a record:
1101949
1102−\begin{SaveVerbatim}{recsimpl}
1103−
950+\begin{code}
1104951record Person : Type where
1105952 MkPerson : (name : String) ->
1106953 (age : Int) -> Person
1107954
1108955fred : Person
1109956fred = MkPerson "Fred" 30
1110−
1111−\end{SaveVerbatim}
1112−\useverb{recsimpl}
957+\end{code}
958+
1113959
1114960\noindent
1115961Record declarations are like \texttt{data} declarations, except that they are
@@ -1117,31 +963,25 @@ introduced by the \texttt{record} keyword, and can only have one constructor.
1117963The names of the binders in the constructor type (\texttt{name} and \texttt{age})
1118964here are the field names, which we can use to access the field values:
1119965
1120−\begin{SaveVerbatim}{recaccess}
1121−
966+\begin{lstlisting}
1122967*record> name fred
1123968"Fred" : String
1124969*record> age fred
112597030 : Int
1126971*record> :t name
1127972name : Person -> String
1128−
1129−\end{SaveVerbatim}
1130−\useverb{recaccess}
973+\end{lstlisting}
1131974
1132975\noindent
1133976We can also use the field names to update a record (or, more precisely, produce a
1134977new record with the given fields updates).
1135978
1136−\begin{SaveVerbatim}{recupdate}
1137−
979+\begin{lstlisting}
1138980*record> record { name = "Jim" } fred
1139981MkPerson "Jim" 30 : Person
1140982*record> record { name = "Jim", age = 20 } fred
1141983MkPerson "Jim" 20 : Person
1142−
1143−\end{SaveVerbatim}
1144−\useverb{recupdate}
984+\end{lstlisting}
1145985
1146986\noindent
1147987The syntax \texttt{record \{ field = val, ... \}} generates a function which updates
@@ -1152,29 +992,26 @@ Updates are allowed to change the type of a field,
1152992provided that the result is well-typed, and the result does not affect the type of
1153993the record as a whole. For example:
1154994
1155−\begin{SaveVerbatim}{deprec}
1156−
995+\begin{code}
1157996record Class : Type where
1158997 ClassInfo : (students : Vect n Person) ->
1159998 (className : String) ->
1160999 Class
1161−
1162−\end{SaveVerbatim}
1163−\useverb{deprec}
1000+\end{code}
11641001
11651002\noindent
11661003It is safe to update the \texttt{students} field to a vector of a different length
11671004because it will not affect the type of the record:
11681005
1169−\begin{SaveVerbatim}{addstu}
1170−
1006+\begin{code}
11711007addStudent : Person -> Class -> Class
11721008addStudent p c = record { students = p :: students c } c
1009+\end{code}
11731010
1011+\begin{lstlisting}
11741012*record> addStudent fred (ClassInfo [] "CS")
11751013ClassInfo (prelude.vect.:: (MkPerson "Fred" 30) (prelude.vect.Nil)) "CS"
11761014 : Class
1015+\end{lstlisting}
11771016
1178−\end{SaveVerbatim}
1179−\useverb{addstu}
11801017