Skip to content

Commit da5e89c

Browse files
committed
Maps: tweak whitespace
1 parent 0e19b18 commit da5e89c

File tree

1 file changed

+38
-20
lines changed

1 file changed

+38
-20
lines changed

src/Maps.lidr

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
= Maps: Total and Partial Maps
22

33
> module Maps
4+
>
45

56
Maps (or dictionaries) are ubiquitous data structures both generally and in the
67
theory of programming languages in particular; we're going to need them in many
@@ -52,9 +53,10 @@ equality comparison function for \idr{Id} and its fundamental property.
5253

5354
> data Id : Type where
5455
> MkId : String -> Id
55-
56+
>
5657
> beq_id : (x1, x2 : Id) -> Bool
5758
> beq_id (MkId n1) (MkId n2) = decAsBool $ decEq n1 n2
59+
>
5860

5961
\todo[inline]{Edit}
6062

@@ -68,8 +70,9 @@ present purposes you can think of it as just a fancy \idr{Bool}.)
6870

6971
> beq_id_refl : (x : Id) -> True = beq_id x x
7072
> beq_id_refl (MkId n) with (decEq n n)
71-
> beq_id_refl _ | Yes _ = Refl
73+
> beq_id_refl _ | Yes _ = Refl
7274
> beq_id_refl _ | No contra = absurd $ contra Refl
75+
>
7376

7477
The following useful property of \idr{beq_id} follows from an analogous lemma
7578
about strings:
@@ -78,8 +81,9 @@ about strings:
7881

7982
> iff : {p,q : Type} -> Type
8083
> iff {p} {q} = (p -> q, q -> p)
81-
84+
>
8285
> syntax [p] "<->" [q] = iff {p} {q}
86+
>
8387

8488
\todo[inline]{Somehow this doesn't work if put under \idr{where} as usual}
8589

@@ -106,10 +110,11 @@ for now}
106110
> not_true_and_false : (b = False) -> Not (b = True)
107111
> not_true_and_false {b=False} _ Refl impossible
108112
> not_true_and_false {b=True} Refl _ impossible
113+
>
109114
> not_true_is_false : Not (b = True) -> b = False
110115
> not_true_is_false {b=False} h = Refl
111-
> not_true_is_false {b=True} h = absurd $ h Refl
112-
116+
> not_true_is_false {b=True} h = absurd $ h Refl
117+
>
113118
> beq_id_false_iff : (beq_id x y = False) <-> Not (x = y)
114119
> beq_id_false_iff = (to, fro)
115120
> where
@@ -137,6 +142,7 @@ return a default value when we look up a key that is not present in the map.
137142

138143
> total_map : Type -> Type
139144
> total_map a = Id -> a
145+
>
140146

141147
Intuitively, a total map over an element type \idr{a} is just a function that
142148
can be used to look up \idr{Id}s, yielding \idr{a}s.
@@ -146,6 +152,7 @@ this map always returns the default element when applied to any id.
146152

147153
> t_empty : (v : a) -> total_map a
148154
> t_empty v = \_ => v
155+
>
149156

150157
We can also write this as:
151158

@@ -159,6 +166,7 @@ More interesting is the \idr{update} function, which (as before) takes a map
159166

160167
> t_update : (x : Id) -> (v : a) -> (m : total_map a) -> total_map a
161168
> t_update x v m = \x' => if beq_id x x' then v else m x'
169+
>
162170

163171
This definition is a nice example of higher-order programming: \idr{t_update}
164172
takes a _function_ \idr{m} and yields a new function \idr{\x' => ...} that
@@ -174,21 +182,23 @@ this:
174182
> examplemap = t_update (MkId "foo") False $
175183
> t_update (MkId "bar") True $
176184
> t_empty False
185+
>
177186

178187
This completes the definition of total maps. Note that we don't need to define a
179188
\idr{find} operation because it is just function application!
180189

181190
> update_example1 : examplemap (MkId "baz") = False
182191
> update_example1 = Refl
183-
192+
>
184193
> update_example2 : examplemap (MkId "foo") = False
185194
> update_example2 = Refl
186-
195+
>
187196
> update_example3 : examplemap (MkId "quux") = False
188197
> update_example3 = Refl
189-
198+
>
190199
> update_example4 : examplemap (MkId "bar") = True
191200
> update_example4 = Refl
201+
>
192202

193203
To use maps in later chapters, we'll need several fundamental facts about how
194204
they behave. Even if you don't work the following exercises, make sure you
@@ -202,6 +212,7 @@ First, the empty map returns its default element for all keys:
202212

203213
> t_apply_empty : t_empty v x = v
204214
> t_apply_empty = ?t_apply_empty_rhs
215+
>
205216

206217
$\square$
207218

@@ -214,6 +225,7 @@ then look up \idr{x} in the map resulting from the \idr{update}, we get back
214225

215226
> t_update_eq : (t_update x v m) x = v
216227
> t_update_eq = ?t_update_eq_rhs
228+
>
217229

218230
$\square$
219231

@@ -226,6 +238,7 @@ a _different_ key \idr{x2} in the resulting map, we get the same result that
226238

227239
> t_update_neq : Not (x1 = x2) -> (t_update x1 v m) x2 = m x2
228240
> t_update_neq neq = ?t_update_neq_rhs
241+
>
229242

230243
$\square$
231244

@@ -239,6 +252,7 @@ simpler map obtained by performing just the second \idr{update} on \idr{m}:
239252

240253
> t_update_shadow : t_update x v2 $ t_update x v1 m = t_update x v2 m
241254
> t_update_shadow = ?t_update_shadow_rhs
255+
>
242256

243257
$\square$
244258

@@ -258,9 +272,10 @@ following:
258272
> data Reflect : Type -> Bool -> Type where
259273
> ReflectT : (p : Type) -> Reflect p True
260274
> ReflectF : (p : Type) -> (Not p) -> Reflect p False
261-
275+
>
262276
> beq_idP : Reflect (x = y) (beq_id x y)
263277
> beq_idP = ?beq_idP_rhs
278+
>
264279

265280
$\square$
266281

@@ -279,6 +294,7 @@ the following theorem, which states that if we update a map to assign key
279294

280295
> t_update_same : t_update x (m x) m = m
281296
> t_update_same = ?t_update_same_rhs
297+
>
282298

283299
$\square$
284300

@@ -292,6 +308,7 @@ we do the updates.
292308
> t_update_permute : Not (x2 = x1) -> t_update x1 v1 $ t_update x2 v2 m
293309
> = t_update x2 v2 $ t_update x1 v1 m
294310
> t_update_permute neq = ?t_update_permute_rhs
311+
>
295312

296313
$\square$
297314

@@ -304,40 +321,41 @@ a} and default element \idr{Nothing}.
304321

305322
> partial_map : Type -> Type
306323
> partial_map a = total_map (Maybe a)
307-
324+
>
308325
> empty : partial_map a
309326
> empty = t_empty Nothing
310-
327+
>
311328
> update : (x : Id) -> (v : a) -> (m : partial_map a) -> partial_map a
312329
> update x v m = t_update x (Just v) m
330+
>
313331

314332
We now straightforwardly lift all of the basic lemmas about total maps to
315333
partial maps.
316334

317335
> apply_empty : empty {a} x = Nothing {a}
318336
> apply_empty = Refl
319-
337+
>
320338
> update_eq : (update x v m) x = Just v
321339
> update_eq {x} {v} {m} =
322340
> rewrite t_update_eq {x} {v=Just v} {m} in
323-
> Refl
324-
341+
> Refl
342+
>
325343
> update_neq : Not (x2 = x1) -> (update x2 v m) x1 = m x1
326344
> update_neq {x1} {x2} {v} {m} neq =
327345
> rewrite t_update_neq neq {x1=x2} {x2=x1} {v=Just v} {m} in
328-
> Refl
329-
346+
> Refl
347+
>
330348
> update_shadow : update x v2 $ update x v1 m = update x v2 m
331349
> update_shadow {x} {v1} {v2} {m} =
332350
> rewrite t_update_shadow {x} {v1=Just v1} {v2=Just v2} {m} in
333-
> Refl
334-
351+
> Refl
352+
>
335353
> update_same : m x = Just v -> update x v m = m
336354
> update_same {x} {m} {v} prf =
337355
> rewrite sym prf in
338356
> rewrite t_update_same {x} {m} in
339-
> Refl
340-
357+
> Refl
358+
>
341359
> update_permute : Not (x2 = x1) -> update x1 v1 $ update x2 v2 m
342360
> = update x2 v2 $ update x1 v1 m
343361
> update_permute {x1} {x2} {v1} {v2} {m} neq =

0 commit comments

Comments
 (0)