move checkInsert and related properties to CheckInsert.agda
[~helmut/bidiragda.git] / CheckInsert.agda
1 module CheckInsert where
2
3 open import Data.Nat using (â„•)
4 open import Data.Fin using (Fin)
5 open import Data.Fin.Props using (_≟_)
6 open import Data.Maybe using (Maybe ; nothing ; just)
7 open import Data.List using (List ; [] ; _∷_)
8 open import Relation.Nullary using (Dec ; yes ; no ; ¬_)
9 open import Relation.Nullary.Negation using (contradiction)
10 open import Relation.Binary.Core using (_≡_ ; refl)
11 open import Relation.Binary.PropositionalEquality using (cong ; sym ; inspect ; Reveal_is_ ; trans)
12 open Relation.Binary.PropositionalEquality.≡-Reasoning using (begin_ ; _≡⟨_⟩_ ; _∎)
13
14 open import FinMap
15
16 EqInst : Set â†’ Set
17 EqInst A = (x y : A) â†’ Dec (x â‰¡ y)
18
19 checkInsert : {A : Set} {n : â„•} â†’ EqInst A â†’ Fin n â†’ A â†’ FinMapMaybe n A â†’ Maybe (FinMapMaybe n A)
20 checkInsert eq i b m with lookupM i m
21 checkInsert eq i b m | just c with eq b c
22 checkInsert eq i b m | just .b | yes refl = just m
23 checkInsert eq i b m | just c  | no ¬p    = nothing
24 checkInsert eq i b m | nothing = just (insert i b m)
25
26 record checkInsertProof {A : Set} {n : â„•} (eq : EqInst A) (i : Fin n) (x : A) (m : FinMapMaybe n A) (P : Set) : Set where
27   field
28      same : lookupM i m â‰¡ just x â†’ P
29      new : lookupM i m â‰¡ nothing â†’ P
30      wrong : (x' : A) â†’ Â¬(x â‰¡ x') â†’ lookupM i m â‰¡ just x'  â†’ P
31
32 apply-checkInsertProof : {A P : Set} {n : â„•} â†’ (eq : EqInst A) â†’ (i : Fin n) â†’ (x : A) â†’ (m : FinMapMaybe n A) â†’ checkInsertProof eq i x m P â†’ P
33 apply-checkInsertProof eq i x m rp with lookupM i m | inspect (lookupM i) m
34 apply-checkInsertProof eq i x m rp | just x' | il with eq x x'
35 apply-checkInsertProof eq i x m rp | just .x | Reveal_is_.[_] il | yes refl = checkInsertProof.same rp il
36 apply-checkInsertProof eq i x m rp | just x' | Reveal_is_.[_] il | no x≢x' = checkInsertProof.wrong rp x' x≢x' il
37 apply-checkInsertProof eq i x m rp | nothing | Reveal_is_.[_] il = checkInsertProof.new rp il
38
39 lemma-checkInsert-same : {A : Set} {n : â„•} â†’ (eq : EqInst A) â†’ (i : Fin n) â†’ (x : A) â†’ (m : FinMapMaybe n A) â†’ lookupM i m â‰¡ just x â†’ checkInsert eq i x m â‰¡ just m
40 lemma-checkInsert-same eq i x m p with lookupM i m
41 lemma-checkInsert-same eq i x m refl | .(just x) with eq x x
42 lemma-checkInsert-same eq i x m refl | .(just x) | yes refl = refl
43 lemma-checkInsert-same eq i x m refl | .(just x) | no x≢x = contradiction refl x≢x
44
45 lemma-checkInsert-new : {A : Set} {n : â„•} â†’ (eq : EqInst A) â†’ (i : Fin n) â†’ (x : A) â†’ (m : FinMapMaybe n A) â†’ lookupM i m â‰¡ nothing â†’ checkInsert eq i x m â‰¡ just (insert i x m)
46 lemma-checkInsert-new eq i x m p with lookupM i m
47 lemma-checkInsert-new eq i x m refl | .nothing = refl
48
49 lemma-checkInsert-wrong : {A : Set} {n : â„•} â†’ (eq : EqInst A) â†’ (i : Fin n) â†’ (x : A) â†’ (m : FinMapMaybe n A) â†’ (x' : A) â†’ Â¬(x â‰¡ x') â†’ lookupM i m â‰¡ just x' â†’ checkInsert eq i x m â‰¡ nothing
50 lemma-checkInsert-wrong eq i x m x' d p with lookupM i m
51 lemma-checkInsert-wrong eq i x m x' d refl | .(just x') with eq x x'
52 lemma-checkInsert-wrong eq i x m x' d refl | .(just x') | yes q = contradiction q d
53 lemma-checkInsert-wrong eq i x m x' d refl | .(just x') | no ¬q = refl
54
55 record checkInsertEqualProof {A : Set} {n : â„•} (eq : EqInst A) (i : Fin n) (x : A) (m : FinMapMaybe n A) (e : Maybe (FinMapMaybe n A)) : Set where
56   field
57      same : lookupM i m â‰¡ just x â†’ just m â‰¡ e
58      new : lookupM i m â‰¡ nothing â†’ just (insert i x m) â‰¡ e
59      wrong : (x' : A) â†’ Â¬(x â‰¡ x') â†’ lookupM i m â‰¡ just x' â†’ nothing â‰¡ e
60
61 lift-checkInsertProof : {A : Set} {n : â„•} {eq : EqInst A} {i : Fin n} {x : A} {m : FinMapMaybe n A} {e : Maybe (FinMapMaybe n A)} â†’ checkInsertEqualProof eq i x m e â†’ checkInsertProof eq i x m (checkInsert eq i x m â‰¡ e)
62 lift-checkInsertProof {_} {_} {eq} {i} {x} {m} o = record
63   { same  = Î» p â†’ trans (lemma-checkInsert-same eq i x m p) (checkInsertEqualProof.same o p)
64   ; new   = Î» p â†’ trans (lemma-checkInsert-new eq i x m p) (checkInsertEqualProof.new o p)
65   ; wrong = Î» x' q p â†’ trans (lemma-checkInsert-wrong eq i x m x' q p) (checkInsertEqualProof.wrong o x' q p)
66   }
67
68 lemma-checkInsert-restrict : {Ï„ : Set} {n : â„•} â†’ (eq : EqInst Ï„) â†’ (f : Fin n â†’ Ï„) â†’ (i : Fin n) â†’ (is : List (Fin n)) â†’ checkInsert eq i (f i) (restrict f is) â‰¡ just (restrict f (i âˆ· is))
69 lemma-checkInsert-restrict {Ï„} eq f i is = apply-checkInsertProof eq i (f i) (restrict f is) (lift-checkInsertProof record
70   { same  = Î» lookupM≡justx â†’ cong just (lemma-insert-same (restrict f is) i (f i) lookupM≡justx)
71   ; new   = Î» lookupM≡nothing â†’ refl
72   ; wrong = Î» x' x≢x' lookupM≡justx' â†’ contradiction (lemma-lookupM-restrict i f is x' lookupM≡justx') x≢x'
73   })
74
75 lemma-lookupM-checkInsert : {A : Set} {n : â„•} â†’ (eq : EqInst A) â†’ (i j : Fin n) â†’ (x y : A) â†’ (h h' : FinMapMaybe n A) â†’ lookupM i h â‰¡ just x â†’ checkInsert eq j y h â‰¡ just h' â†’ lookupM i h' â‰¡ just x
76 lemma-lookupM-checkInsert eq i j x y h h' pl ph' with lookupM j h | inspect (lookupM j) h
77 lemma-lookupM-checkInsert eq i j x y h .(insert j y h) pl refl | nothing | pl' with i â‰Ÿ j
78 lemma-lookupM-checkInsert eq i .i x y h .(insert i y h) pl refl | nothing | Reveal_is_.[_] pl' | yes refl with begin just x â‰¡âŸ¨ sym pl âŸ© lookupM i h â‰¡âŸ¨ pl' âŸ© nothing âˆŽ
79 ... | ()
80 lemma-lookupM-checkInsert eq i j x y h .(insert j y h) pl refl | nothing | pl' | no ¬p = begin
81   lookupM i (insert j y h)
82     â‰¡âŸ¨ sym (lemma-lookupM-insert-other i j y h Â¬p) âŸ©
83   lookupM i h
84     â‰¡âŸ¨ pl âŸ©
85   just x âˆŽ
86 lemma-lookupM-checkInsert eq i j x y h h' pl ph' | just z | pl' with eq y z
87 lemma-lookupM-checkInsert eq i j x y h h' pl ph' | just .y | pl' | yes refl = begin
88   lookupM i h'
89     â‰¡âŸ¨ cong (lookupM i) (lemma-from-just (sym ph')) âŸ©
90   lookupM i h
91     â‰¡âŸ¨ pl âŸ©
92   just x âˆŽ
93 lemma-lookupM-checkInsert eq i j x y h h' pl () | just z | pl' | no ¬p