@@ -117,6 +117,9 @@ appropriate type parameter:
117
117
> test_repeat2 = Refl
118
118
119
119
120
+ \ todo[inline]{Explain implicits and {x= foo} syntax first? Move after the
121
+ " Supplying Type Arguments Explicitly" section? }
122
+
120
123
==== Exercise : 2 starsM (mumble_grumble)
121
124
122
125
> namespace MumbleGrumble
@@ -132,15 +135,14 @@ Consider the following two inductively defined types.
132
135
> D : Mumble -> Grumble x
133
136
> E : x -> Grumble x
134
137
135
- \ todo[inline]{Edit , Idris doesn't require type parameters to constructors}
136
138
Which of the following are well- typed elements of `Grumble x` for some type `x` ?
137
139
138
140
- `D (B A 5 )`
139
- - `D Mumble (B A 5 )`
140
- - `D Bool (B A 5 )`
141
- - `E Bool True `
142
- - `E Mumble (B C 0 )`
143
- - `E Bool (B C 0 )`
141
+ - `D (B A 5 ) {x = Mumble } `
142
+ - `D (B A 5 ) {x = Bool } `
143
+ - `E True {x = Bool } `
144
+ - `E (B C 0 ) {x = Mumble } `
145
+ - `E (B C 0 ) {x = Bool } `
144
146
- `C`
145
147
146
148
> -- FILL IN HERE
@@ -152,6 +154,9 @@ the lowercase/uppercase distinction.}
152
154
153
155
==== Type Annotation Inference
154
156
157
+ \ todo[inline]{This has already happened earlier at `repeat` , delete most of
158
+ this? }
159
+
155
160
Let's write the definition of `repeat` again, but this time we won't specify the
156
161
types of any of the arguments. Will Idris still accept it?
157
162
@@ -180,6 +185,9 @@ code).
180
185
181
186
==== Type Argument Synthesis
182
187
188
+ \todo[inline]{We should mention the `_` parameters but it won't work like this
189
+ in Idris }
190
+
183
191
To use a polymorphic function, we need to pass it one or more types in addition
184
192
to its other arguments . For example , the recursive call in the body of the
185
193
`repeat` function above must pass along the type `x_ty`. But since the second
@@ -244,20 +252,18 @@ Alternatively, we can declare an argument to be implicit when defining the
244
252
function itself , by surrounding it in curly braces instead of parens. For
245
253
example:
246
254
247
- > repeat''' : {x_ty : Type} -> (x : x_ty) -> (count : Nat) -> List x_ty
248
- > repeat''' x Z = Nil
249
- > repeat''' x (S count' ) = x :: repeat''' x count'
250
-
251
- \todo[inline]{Mention that we don't even need the curly -braced argument }
255
+ > repeat' : {x_ty : Type} -> (x : x_ty) -> (count : Nat) -> List x_ty
256
+ > repeat' x Z = Nil
257
+ > repeat' x (S count' ) = x :: repeat' x count'
252
258
253
259
(Note that we didn't even have to provide a type argument to the recursive call
254
- to `repeat''' `; indeed, it would be invalid to provide one!)
260
+ to `repeat'`; indeed, it would be invalid to provide one!)
255
261
256
- We will use the latter style whenever possible , but we will continue to use use
257
- explicit Argument declarations for Inductive constructors . The reason for this
258
- is that marking the parameter of an inductive type as implicit causes it to
259
- become implicit for the type itself , not just for its constructors. For
260
- instance, consider the following alternative definition of the `List` type:
262
+ We will use the latter style whenever possible , but we will continue to use
263
+ explicit declarations in data types . The reason for this is that marking the
264
+ parameter of an inductive type as implicit causes it to become implicit for the
265
+ type itself , not just for its constructors. For instance , consider the following
266
+ alternative definition of the `List` type:
261
267
262
268
> data List' : {x : Type} -> Type where
263
269
> Nil' : List'
@@ -268,6 +274,16 @@ including `List'` itself, we now have to write just `List'` whether we are
268
274
talking about lists of numbers or booleans or anything else , rather than `List'
269
275
Nat` or `List' Bool ` or whatever ; this is a step too far .
270
276
277
+ \todo[inline]{Added the implicit inference explanation here }
278
+ There's another step towards conciseness that we can take in Idris -- drop the
279
+ implicit argument completely in function definitions ! Idris will automatically
280
+ insert them for us when it encounters unknown variables. _Note that by convention
281
+ this will only happen for variables starting on a lowercase letter_.
282
+
283
+ > repeat'' : (x : x_ty) -> (count : Nat) -> List x_ty
284
+ > repeat'' x Z = Nil
285
+ > repeat'' x (S count' ) = x :: repeat'' x count'
286
+
271
287
Let's finish by re -implementing a few other standard list functions on our new
272
288
polymorphic lists ...
273
289
@@ -321,6 +337,9 @@ them as arguments in curly braces.
321
337
λΠ> :let mynil' = Nil {elem=Nat}
322
338
```
323
339
340
+ \todo[inline]{Describe here how to bring variables from the type into definition
341
+ scope via implicits?}
342
+
324
343
\todo[inline]{Explain that Idris has built-in notation for lists instead?}
325
344
326
345
Using argument synthesis and implicit arguments , we can define convenient
@@ -549,11 +568,12 @@ For example, if we apply `filter` to the predicate `evenb` and a list of numbers
549
568
> length_is_1 : (l : List x ) -> Bool
550
569
> length_is_1 l = beq_nat (length l ) 1
551
570
552
- \todo[inline]{Why doesn't this work ?}
571
+ \todo[inline]{Why doesn't this work without {x=Nat} ?}
553
572
554
- > --test_filter2 : filter (length_is_1) [ [1,2], [3], [4], [5,6,7], [], [8] ]
555
- > -- = [ [3], [4], [8] ]
556
- > --test_filter2 = Refl
573
+ > test_filter2 : filter (length_is_1 {x=Nat})
574
+ > [ [1,2], [3], [4], [5,6,7], [], [8] ]
575
+ > = [ [3], [4], [8] ]
576
+ > test_filter2 = Refl
557
577
558
578
We can use `filter` to give a concise version of the `countoddmembers` function
559
579
from the `Lists` chapter.
@@ -583,7 +603,7 @@ give each of these functions a name would be tedious.
583
603
Fortunately, there is a better way. We can construct a function "on the fly"
584
604
without declaring it at the top level or giving it a name .
585
605
586
- \todo[inline]{Can't use `*` here due to the tuple sugar interference }
606
+ \todo[inline]{Can't use `*` here due to the interference from our tuple sugar}
587
607
588
608
> test_anon_fun': doit3times (\n => mult n n) 2 = 256
589
609
> test_anon_fun' = Refl
@@ -595,7 +615,7 @@ Here is the `filter` example, rewritten to use an anonymous function.
595
615
596
616
> test_filter2': filter (\l => beq_nat (length l ) 1)
597
617
> [ [1,2], [3], [4], [5,6,7], [], [8] ]
598
- > = [ [3], [4], [8] ]
618
+ > = [ [3], [4], [8] ]
599
619
> test_filter2' = Refl
600
620
601
621
@@ -813,7 +833,8 @@ supply just the first. This is called _partial application_.
813
833
> test_plus3 : plus3 4 = 7
814
834
> test_plus3 = Refl
815
835
816
- \todo[inline]{Why is this broken ?}
836
+ \todo[inline]{Why is this broken ? `the (doit3times plus3 0 = 9) Refl` works in
837
+ REPL}
817
838
818
839
> -- test_plus3' : doit3times plus3 0 = 9
819
840
> -- test_plus3' = Refl
@@ -1018,7 +1039,7 @@ Multiplication:
1018
1039
1019
1040
Exponentiation:
1020
1041
1021
- \todo[inline]{Edit hint }
1042
+ \todo[inline]{Edit the hint. }
1022
1043
1023
1044
(Hint: Polymorphism plays a crucial role here . However, choosing the right type
1024
1045
to iterate over can be tricky . If you hit a "Universe inconsistency " error, try
0 commit comments