1
1
= Maps : Total and Partial Maps
2
2
3
+ > module Maps
4
+
3
5
Maps (or dictionaries) are ubiquitous data structures both generally and in the
4
6
theory of programming languages in particular; we're going to need them in many
5
7
places in the coming chapters. They also make a nice case study using ideas
@@ -10,7 +12,7 @@ streamline proofs (from `IndProp`).
10
12
We'll define two flavors of maps: _total_ maps, which include a "default"
11
13
element to be returned when a key being looked up doesn't exist, and _partial_
12
14
maps, which return a \idr{Maybe } to indicate success or failure. The latter is
13
- defined in terms of the former, using \idr{Maybe } as the default element.
15
+ defined in terms of the former, using \idr{Nothing } as the default element.
14
16
15
17
16
18
= = The Idris Standard Library
@@ -19,24 +21,26 @@ defined in terms of the former, using \idr{Maybe} as the default element.
19
21
20
22
One small digression before we get to maps.
21
23
22
- Unlike the chapters we have seen so far, this one does not Require Import the
24
+ Unlike the chapters we have seen so far, this one does not ` Require Import ` the
23
25
chapter before it (and , transitively, all the earlier chapters). Instead , in
24
26
this chapter and from now, on we're going to import the definitions and theorems
25
27
we need directly from Idris's standard library stuff. You should not notice much
26
28
difference, though, because we've been careful to name our own definitions and
27
29
theorems the same as their counterparts in the standard library, wherever they
28
30
overlap.
29
31
32
+ `` `coq
30
33
Require Import Idris . Arith . Arith .
31
34
Require Import Idris . Bool . Bool .
32
35
Require Import Idris . Strings . String .
33
36
Require Import Idris . Logic . FunctionalExtensionality .
37
+ `` `
34
38
35
39
Documentation for the standard library can be found at
36
40
http : //Idris.inria.fr/library/.
37
41
38
- The Search command is a good way to look for theorems involving objects of
39
- specific types. Take a minute now to experiment with it.
42
+ The \ idr{ : search} command is a good way to look for theorems involving objects
43
+ of specific types. Take a minute now to experiment with it.
40
44
41
45
42
46
== Identifiers
@@ -70,13 +74,15 @@ present purposes you can think of it as just a fancy \idr{Bool}.)
70
74
The following useful property of \ idr{beq_id} follows from an analogous lemma
71
75
about strings :
72
76
73
- \ todo[inline]{Copypasted ` <-> ` for now}
77
+ \ todo[inline]{Copied \ idr{ <-> } for now}
74
78
75
79
> iff : {p,q : Type } -> Type
76
80
> iff {p} {q} = (p -> q, q -> p)
77
81
78
82
> syntax [p] " <->" [q] = iff {p} {q}
79
83
84
+ \ todo[inline]{Somehow this doesn't work if put under \ idr{where } as usual}
85
+
80
86
> bto : (beq_id x y = True) -> x = y
81
87
> bto {x= MkId n1} {y= MkId n2} prf with (decEq n1 n2)
82
88
> bto Refl | (Yes eq) = cong {f= MkId } eq
@@ -94,9 +100,10 @@ about strings:
94
100
95
101
Similarly :
96
102
97
- \ todo[inline]{Copypasted here for now}
103
+ \ todo[inline]{Copied and inlined \ idr{not_true_iff_false} from \ idr{Logic } here
104
+ for now}
98
105
99
- > not_true_and_false : (b = False) -> (b = True) -> Void
106
+ > not_true_and_false : (b = False) -> Not (b = True)
100
107
> not_true_and_false {b= False } _ Refl impossible
101
108
> not_true_and_false {b= True } Refl _ impossible
102
109
> not_true_is_false : Not (b = True) -> b = False
@@ -140,6 +147,12 @@ this map always returns the default element when applied to any id.
140
147
> t_empty : (v : a) -> total_map a
141
148
> t_empty v = \ _ => v
142
149
150
+ We can also write this as :
151
+
152
+ `` `idris
153
+ t_empty = const
154
+ `` `
155
+
143
156
More interesting is the \ idr{update} function, which (as before) takes a map
144
157
\ idr{m}, a key \ idr{x}, and a value \ idr{v} and returns a new map that takes
145
158
\ idr{x} to \ idr{v} and takes every other key to whatever \ idr{m} does.
@@ -155,7 +168,7 @@ For example, we can build a map taking \idr{Id}s to \idr{Bool}s, where \idr{Id
155
168
3 } is mapped to \ idr{True } and every other key is mapped to \ idr{False }, like
156
169
this :
157
170
158
- \ todo[inline]{Seems like an inconsistency in the book here}
171
+ \ todo[inline]{Seems like a wrong description in the book here}
159
172
160
173
> examplemap : total_map Bool
161
174
> examplemap = t_update (MkId " foo" ) False $
@@ -208,7 +221,7 @@ $\square$
208
221
==== Exercise : 2 stars, optional (t_update_neq)
209
222
210
223
On the other hand, if we update a map \ idr{m} at a key \ idr{x1} and then look up
211
- a different key \ idr{x2} in the resulting map , we get the same result that
224
+ a _different_ key \ idr{x2} in the resulting map , we get the same result that
212
225
\ idr{m} would have given :
213
226
214
227
> t_update_neq : Not (x1 = x2) -> (t_update x1 v m) x2 = m x2
@@ -240,6 +253,8 @@ boolean function \idr{beq_id}.
240
253
Use the proof of \ idr{beq_natP} in chapter `IndProp` as a template to prove the
241
254
following :
242
255
256
+ \ todo[inline]{Copied \ idr{Reflect } for now}
257
+
243
258
> data Reflect : Type -> Bool -> Type where
244
259
> ReflectT : (p : Type ) -> Reflect p True
245
260
> ReflectF : (p : Type ) -> (Not p) -> Reflect p False
0 commit comments