1
{-# OPTIONS --allow-unsolved-metas --no-termination-check
22
----------------------------------------------------------------------
26
-- If this were Coq then the invariant should be a Prop. Similar
27
-- remarks apply to some definitions below. Since I have to write
28
-- the supporting library myself I can't be bothered to
29
-- distinguish Set and Prop right now though.
31
data BagType (a : Datoid) : Set where
32
bt : (pairs : List (Pair Pos.Pos (El a)))
33
-> NoDuplicates a (map snd pairs)
36
list : {a : Datoid} -> BagType a -> List (Pair Pos.Pos (El a))
39
contents : {a : Datoid} -> BagType a -> List (El a)
40
contents b = map snd (list b)
42
invariant : {a : Datoid} -> (b : BagType a)
43
-> NoDuplicates a (contents b)
44
invariant (bt _ i) = i
47
elemDatoid : Datoid -> Datoid
48
elemDatoid a = pairDatoid Pos.posDatoid a
50
BagEq : (a : Datoid) -> BagType a -> BagType a -> Set
51
BagEq a b1 b2 = rel' (Permutation (elemDatoid a)) (list b1) (list b2)
53
eqRefl : {a : Datoid} -> (x : BagType a) -> BagEq a x x
54
eqRefl {a} x = refl (Permutation (elemDatoid a)) {list x}
56
eqSym : {a : Datoid} -> (x y : BagType a)
57
-> BagEq a x y -> BagEq a y x
58
eqSym {a} x y = sym (Permutation (elemDatoid a)) {list x} {list y}
60
eqTrans : {a : Datoid} -> (x y z : BagType a)
61
-> BagEq a x y -> BagEq a y z -> BagEq a x z
62
eqTrans {a} x y z = trans (Permutation (elemDatoid a))
63
{list x} {list y} {list z}
65
eqDec : {a : Datoid} -> (x y : BagType a)
66
-> Either (BagEq a x y) _
67
eqDec {a} x y = decRel (Permutation (elemDatoid a)) (list x) (list y)
69
BagEquiv : (a : Datoid) -> DecidableEquiv (BagType a)
70
BagEquiv a = decEquiv (equiv (BagEq a) eqRefl eqSym eqTrans) (dec eqDec)
72
Bag : Datoid -> Datoid
73
Bag a = datoid (BagType a) (BagEquiv a)
75
----------------------------------------------------------------------
78
empty : {a : Datoid} -> El (Bag a)
82
data LookupResult (a : Datoid) (x : El a) (b : El (Bag a)) : Set where
85
-> Not (member a x (contents b'))
86
-> ({y : El a} -> Not (member a y (contents b))
87
-> Not (member a y (contents b')))
90
lookup1 : {a : Datoid}
94
-> (nyb' : Not (member a y (contents b')))
96
-> Either (datoidRel a x y) _
97
-> LookupResult a x b'
98
-> LookupResult a x (bt (pair n y :: list b')
99
(pair nyb' (invariant b')))
100
lookup1 n y b' nyb' x (left xy) _ =
102
(contrapositive (memberPreservesEq xy (contents b')) nyb')
103
(\{y'} ny'b -> snd (notDistribIn ny'b))
104
lookup1 {a} n y b' nyb' x (right nxy)
105
(lr n' (bt b'' ndb'') nxb'' nmPres) =
106
lr n' (bt (pair n y :: b'') (pair (nmPres nyb') ndb''))
107
(notDistribOut {datoidRel a x y} nxy nxb'')
108
(\{y'} ny'b -> notDistribOut (fst (notDistribIn ny'b))
109
(nmPres (snd (notDistribIn ny'b))))
115
-> LookupResult a x b
116
lookup2 x (bt nil nd) = lr zero (bt nil nd) (not id) (\{_} _ -> not id)
117
lookup2 {a} x (bt (pair n y :: b) (pair nyb ndb)) =
118
lookup1 n y (bt b ndb) nyb x
119
(decRel (datoidEq a) x y)
120
(lookup2 x (bt b ndb))
122
lookup3 : {a : Datoid} -> {x : El a} -> {b : El (Bag a)}
123
-> LookupResult a x b -> Pair Nat (El (Bag a))
124
lookup3 (lr n b _ _) = pair n b
126
lookup : {a : Datoid} -> El a -> El (Bag a) -> Pair Nat (El (Bag a))
127
lookup x b = lookup3 (lookup2 x b)
130
insert' : {a : Datoid} -> (x : El a) -> {b : El (Bag a)}
131
-> LookupResult a x b -> El (Bag a)
132
insert' x (lr n (bt b ndb) nxb _) =
133
bt (pair (Pos.suc' n) x :: b) (pair nxb ndb)
135
insert : {a : Datoid} -> El a -> El (Bag a) -> El (Bag a)
136
insert x b = insert' x (lookup2 x b)
145
-> (nxb : Not (member a x (contents b)))
148
(bt (pair Pos.one x :: list b) (pair nxb (invariant b)))
155
-> (nxb : Not (member a x (contents b)))
157
(insert x (bt (pair n x :: list b)
158
(pair nxb (invariant b))))
159
(bt (pair (Pos.suc n) x :: list b)
160
(pair nxb (invariant b)))
162
----------------------------------------------------------------------
165
data Traverse (a : Datoid) : Set where
167
Insert : (x : El a) -> (b : El (Bag a)) -> Traverse a
169
run : {a : Datoid} -> Traverse a -> El (Bag a)
171
run (Insert x b) = insert x b
175
traverse : {a : Datoid} -> El (Bag a) -> Traverse a
176
traverse (bt nil _) = Empty
177
traverse {a} (bt (pair n x :: b) (pair nxb ndb)) = traverse' (Pos.pred n)
180
traverse' : Maybe Pos.Pos -> Traverse a
181
traverse' Nothing = Insert x (bt b ndb)
182
traverse' (Just n) = Insert x (bt (pair n x :: b) (pair nxb ndb))
185
: {a : Datoid} -> (b : El (Bag a))
186
-> datoidRel (Bag a) (run (traverse b)) b
187
traverseTraverses {a} (bt nil unit) = dRefl (Bag a) {empty}
188
traverseTraverses {a} (bt (pair n x :: b) (pair nxb ndb)) =
189
tT (Pos.pred n) (Pos.predOK n)
193
subst' : {a : Datoid} -> (P : El a -> Set) -> (x y : El a)
194
-> datoidRel a x y -> P x -> P y
196
tT : (predN : Maybe Pos.Pos)
199
(run (traverse (bt (pair n x :: b) (pair nxb ndb))))
200
(bt (pair n x :: b) (pair nxb ndb))
201
tT Nothing (Pos.ok eq) = ?
202
-- subst' (\p -> datoidRel (Bag a)
203
-- (run (traverse (bt (pair p x :: b) (pair nxb ndb))))
204
-- (bt (pair p x :: b) (pair nxb ndb)))
205
-- Pos.one n eq (insertLemma1 x (bt b ndb) nxb)
208
-- data Pred (p : Pos) (mP : Maybe Pos) : Set where
209
-- ok : datoidRel posDatoid (sucPred mP) p -> Pred p mP
210
-- insert x (bt b ndb) == bt (pair n x :: b) (pair nxb ndb)
211
tT (Just n) (Pos.ok eq) = ? -- insertLemma2 n x (bt b ndb) nxb
212
-- insert x (bt (pair n x :: b) (pair nxb btb) ==
213
-- bt (pair (suc n) x :: b) (pair nxb btb)
217
-> (P : El (Bag a) -> Set)
218
-> Respects (Bag a) P
220
-> ((x : El a) -> (b : El (Bag a)) -> P b -> P (insert x b))
223
bagElim {a} P Prespects e i b =
224
bagElim' b (traverse b) (traverseTraverses b)
230
-> datoidRel (Bag a) (run t) b
232
bagElim' b Empty eq = subst Prespects empty b eq e
233
bagElim' b (Insert x b') eq =
234
subst Prespects (insert x b') b eq
235
(i x b' (bagElim' b' (traverse b') (traverseTraverses b')))
237
----------------------------------------------------------------------
238
-- Respect and equality preservation lemmas
241
insertPreservesRespect
243
-> (P : El (Bag a) -> Set)
245
-> Respects (Bag a) P
246
-> Respects (Bag a) (\b -> P (insert x b))
248
lookupPreservesRespect
250
-> (P : El (Bag a) -> Set)
252
-> Respects (Bag a) P
253
-> Respects (Bag a) (\b -> P (snd $ lookup x b))
255
-- This doesn't type check without John Major equality or some
256
-- ugly substitutions...
257
-- bagElimPreservesEquality
259
-- -> (P : El (Bag a) -> Set)
260
-- -> (r : Respects (Bag a) P)
262
-- -> (i : (x : El a) -> (b : El (Bag a)) -> P b -> P (insert x b))
263
-- -> ( (x1 x2 : El a) -> (b1 b2 : El (Bag a))
264
-- -> (p1 : P b1) -> (p2 : P b2)
265
-- -> (eqX : datoidRel a x1 x2) -> (eqB : datoidRel (Bag a) b1 b2)
266
-- -> i x1 b1 p1 =^= i x2 b2 p2
268
-- -> (b1 b2 : El (Bag a))
269
-- -> datoidRel (Bag a) b1 b2
270
-- -> bagElim P r e i b1 =^= bagElim P r e i b2