0 | module Goop.Theroy.BisectableHistory
5 | import Goop.Theroy.Types
21 | record Item (a : Type) (output : Type) (item_digest : output) where
23 | {auto 0 impl : Digest output a}
25 | {auto witness : item_digest = digest @{impl} item}
39 | digestEq : {d1, d2 : output} -> d1 = d2
40 | -> (item1 : Item a output d1) -> (item2 : Item a output d2)
42 | digestEq prf item1 item2 = believe_me $
the (1 = 1) Refl
58 | data Set : (output : Type) -> (n : Nat) -> (a : Type) -> Type
64 | data NotInSet : (item : Item a output d)
65 | -> (set : Set output n a)
70 | contains : {auto cmp : (x, y : output) -> Dec (x = y)}
71 | -> Set output n a -> output -> Bool
83 | data Set : (output : Type) -> (n : Nat) -> (a : Type) -> Type where
84 | Nil : Set output 0 a
85 | (::) : {digest_value : output} -> (x : Item a output digest_value)
86 | -> (xs : Set output n a) -> {auto 0 not_in_rest : NotInSet x xs}
87 | -> Set output (S n) a
90 | data NotInSet : (item : Item a output d)
91 | -> (set : Set output n a)
93 | NotInHere : {digest_value : output} -> (x : Item a output digest_value)
94 | -> (xs : Set output n a)
95 | -> {auto cmp : (x, y : output) -> Dec (x = y)}
96 | -> So (Basics.not $
Set.contains xs digest_value)
100 | contains {cmp} [] y = False
101 | contains {cmp} ((::) {digest_value} x xs) y =
102 | case cmp digest_value y of
104 | No _ => contains xs y
110 | namespace SetMonoid
117 | (++) : Digest output a => {m, n : Nat}
118 | -> (lhs : Set output n a) -> (rhs : Set output m a)
119 | -> (n' : Nat ** Set output n' a)
120 | (++) [] rhs = (
_ ** rhs)
121 | (++) @{impl} ((::) {digest_value} x xs) rhs =
122 | let (
n' ** rest)
= xs ++ rhs
123 | _ = outputDecEq @{impl}
124 | in case choose $
contains rest digest_value of
125 | Left _ => (
n' ** rest)
127 | let _ = NotInHere x rest
128 | in (
_ ** x :: rest)
133 | record AnySet (output : Type) (a : Type) where
134 | constructor MkAnySet
136 | inner : Set output inner_n a
140 | Digest output a => Semigroup (AnySet output a) where
142 | let (
_ ** res)
= x.inner ++ y.inner
147 | Semigroup (AnySet output a) => Monoid (AnySet output a) where
148 | neutral = MkAnySet Nil
157 | contents : Set output n a -> LazyList output
159 | contents ((::) {digest_value} x xs) =
160 | digest_value :: contents xs
164 | size : Set output n a -> Nat
166 | size (x :: xs) = S (size xs)
170 | sizeCorrect : (xs : Set output n a) -> size xs = n
171 | sizeCorrect [] = Refl
172 | sizeCorrect (x :: xs) = rewrite sizeCorrect xs in Refl
176 | merkleValue : Digest output a => Set output n a -> output
177 | merkleValue @{impl} x = merkle @{impl} (contents x)
181 | merkleValueEq : Digest output a => (x, y : Set output n a)
182 | -> (merkleValue x = merkleValue y)
184 | merkleValueEq x y prf = believe_me $
the (1 = 1) Refl
203 | data History : (output : Type) -> (merkle_val : output) -> (n : Nat) -> (a : Type) -> Type
207 | contents : History output mv n a -> LazyList (AnySet output a)
211 | set : Digest output a => History output mv n a -> AnySet output a
220 | data Entry : (output : Type) -> (digest_value : output) -> (a : Type) -> Type
224 | set : Digest output a => Entry output dv a -> AnySet output a
233 | data History : (output : Type) -> (merkle_val : output) -> (n : Nat) -> (a : Type) -> Type where
234 | Nil : {auto i : Digest output a} -> History output (merkle @{i} []) 0 a
235 | (::) : {auto i : Digest output a}
236 | -> {digest_x : output} -> (x : Entry output digest_x a)
237 | -> {merkle_xs : output} -> (xs : History output merkle_xs n a)
238 | -> History output (merkle @{i} [digest_x, merkle_xs]) (S n) a
242 | contents (x :: xs) =
243 | set x :: contents xs
247 | foldr (<+>) neutral (contents hs)
251 | merkleValue : (hs : History output mv n a) -> output
252 | merkleValue (Nil {i}) = merkle @{i} []
253 | merkleValue ((::) {i} {digest_x} x {merkle_xs} xs) = merkle @{i} [digest_x, merkle_xs]
257 | merkleValueCorrect : (hs : History output mv n a) -> merkleValue hs = mv
258 | merkleValueCorrect [] = Refl
259 | merkleValueCorrect (x :: xs) = Refl
263 | merkleValueEq : mv1 = mv2
264 | -> (x : History output mv1 n a)
265 | -> (y : History output mv2 n a)
267 | merkleValueEq prf x y = believe_me $
the (1 = 1) Refl
271 | heads : (hs : History output mv n a) -> Vect n output
273 | heads orig@(x :: xs) = merkleValue orig :: heads xs
278 | data Entry : (output : Type) -> (digest_value : output) -> (a : Type) -> Type where
279 | IncludeSet : {auto i : Digest output a}
280 | -> {merkle_value : output} -> {n : Nat} -> (x : Set output n a)
281 | -> {auto witness : merkle_value = merkleValue x}
282 | -> Entry output merkle_value a
283 | IncludeHistory : {merkle_value : _} -> {n : Nat} -> (hs : History output merkle_value n a)
284 | -> Entry output merkle_value a
288 | set (IncludeSet x) = MkAnySet x
289 | set (IncludeHistory hs) = set hs
293 | digestValue : (e : Entry output digest_value a) -> output
294 | digestValue (IncludeSet x) = digest_value
295 | digestValue (IncludeHistory hs) = digest_value
299 | digestValueCorrect : (e : Entry output digest_value a) -> digestValue e = digest_value
300 | digestValueCorrect (IncludeSet x) = Refl
301 | digestValueCorrect (IncludeHistory hs) = Refl
305 | digestValueEq : mv1 = mv2
306 | -> (x : Entry output mv1 a)
307 | -> (y : Entry output mv2 a)
309 | digestValueEq prf x y = believe_me $
the (1 = 1) Refl
322 | data Contains : (mv_target : output) -> (n : Nat) -> (hs : History output mv_head m a)
324 | Here : {auto i : Digest output a}
325 | -> (head : Entry output dv_head a)
326 | -> (0 tail : History output mv_tail m a)
327 | -> Contains (merkleValue (head :: tail)) 0 (head :: tail)
328 | There : {auto i : Digest output a}
329 | -> (0 head : Entry output dv_head a)
330 | -> (0 tail : History output mv_tail m a)
331 | -> (rest : Contains mv_target n tail)
332 | -> Contains mv_target (S n) (head :: tail)
336 | Digest output a => Uninhabited (Contains mv_target n (the (History output _ _ a) [])) where
337 | uninhabited (Here head tail
) impossible
338 | uninhabited (There head tail rest
) impossible
342 | decContains : (mv_target : output) -> (hs : History output mv_head m a)
343 | -> Dec (n : Nat ** Contains mv_target n hs)
344 | decContains mv_target [] = No $
\(_ ** oops) => uninhabited oops
345 | decContains mv_target ((::) @{i} x xs) =
346 | case outputDecEq @{i} mv_target (merkleValue (x :: xs)) of
347 | Yes prf => Yes (
0 ** rewrite prf in Here x xs)
349 | case decContains mv_target xs of
350 | Yes (
n' ** rest)
=>
351 | Yes (
S n' ** There x xs rest)
352 | No not_there => No $
\(
n' ** oops)
=>
354 | (Here x xs) => not_here Refl
355 | (There x xs rest) => not_there (
_ ** rest)
359 | length : Contains mv_target n hs -> Nat
360 | length (Here head tail) = 0
361 | length (There head tail rest) = S (length rest)
365 | lengthCorrect : (c : Contains mv_target n hs) -> length c = n
366 | lengthCorrect (Here head tail) = Refl
367 | lengthCorrect (There head tail rest) = rewrite lengthCorrect rest in Refl
372 | seek : (hs : History output mv_head m a) -> Contains mv_target n hs
373 | -> History output mv_target (m `minus` n) a
374 | seek [] c = absurd c
377 | Here _ _ => x :: xs
378 | There _ _ rest => seek xs rest