diff options
-rw-r--r-- | Bidir.agda | 92 | ||||
-rw-r--r-- | Precond.agda | 71 |
2 files changed, 111 insertions, 52 deletions
@@ -4,14 +4,13 @@ open import Data.Nat using (ℕ) open import Data.Fin using (Fin) open import Data.Fin.Props using (_≟_) open import Data.Maybe using (Maybe ; nothing ; just ; maybe′) -open import Data.List using (List ; [] ; _∷_ ; map ; length) -open import Data.List.Properties using (map-cong ; ∷-injective) renaming (map-compose to map-∘) +open import Data.List using (List) open import Data.List.Any using (Any ; any ; here ; there) open import Data.List.All using (All) open Data.List.Any.Membership-≡ using (_∈_ ; _∉_) -open import Data.Vec using (toList ; fromList ; tabulate) renaming (lookup to lookupVec ; _∷_ to _∷V_) -open import Data.Vec.Properties using (tabulate-∘ ; lookup∘tabulate) -open import Data.Product using (∃ ; _,_ ; proj₁ ; proj₂) +open import Data.Vec using (Vec ; [] ; _∷_ ; toList ; fromList ; map ; tabulate) renaming (lookup to lookupVec) +open import Data.Vec.Properties using (tabulate-∘ ; lookup∘tabulate ; map-cong ; map-∘) +open import Data.Product using (∃ ; _×_ ; _,_ ; proj₁ ; proj₂) open import Data.Empty using (⊥-elim) open import Function using (id ; _∘_ ; flip) open import Relation.Nullary using (yes ; no ; ¬_) @@ -25,22 +24,22 @@ open import CheckInsert open import BFF using (_>>=_ ; fmap) -open BFF.ListBFF using (assoc ; enumerate ; denumerate ; bff) +open BFF.VecBFF using (assoc ; enumerate ; denumerate ; bff) -lemma-1 : {τ : Set} {n : ℕ} → (eq : EqInst τ) → (f : Fin n → τ) → (is : List (Fin n)) → assoc eq is (map f is) ≡ just (restrict f is) +lemma-1 : {τ : Set} {m n : ℕ} → (eq : EqInst τ) → (f : Fin n → τ) → (is : Vec (Fin n) m) → assoc eq is (map f is) ≡ just (restrict f (toList is)) lemma-1 eq f [] = refl lemma-1 eq f (i ∷ is′) = begin assoc eq (i ∷ is′) (map f (i ∷ is′)) ≡⟨ refl ⟩ assoc eq is′ (map f is′) >>= checkInsert eq i (f i) ≡⟨ cong (λ m → m >>= checkInsert eq i (f i)) (lemma-1 eq f is′) ⟩ - just (restrict f is′) >>= (checkInsert eq i (f i)) + just (restrict f (toList is′)) >>= (checkInsert eq i (f i)) ≡⟨ refl ⟩ - checkInsert eq i (f i) (restrict f is′) - ≡⟨ lemma-checkInsert-restrict eq f i is′ ⟩ - just (restrict f (i ∷ is′)) ∎ + checkInsert eq i (f i) (restrict f (toList is′)) + ≡⟨ lemma-checkInsert-restrict eq f i (toList is′) ⟩ + just (restrict f (toList (i ∷ is′))) ∎ -lemma-lookupM-assoc : {A : Set} {n : ℕ} → (eq : EqInst A) → (i : Fin n) → (is : List (Fin n)) → (x : A) → (xs : List A) → (h : FinMapMaybe n A) → assoc eq (i ∷ is) (x ∷ xs) ≡ just h → lookupM i h ≡ just x +lemma-lookupM-assoc : {A : Set} {m n : ℕ} → (eq : EqInst A) → (i : Fin n) → (is : Vec (Fin n) m) → (x : A) → (xs : Vec A m) → (h : FinMapMaybe n A) → assoc eq (i ∷ is) (x ∷ xs) ≡ just h → lookupM i h ≡ just x lemma-lookupM-assoc eq i is x xs h p with assoc eq is xs lemma-lookupM-assoc eq i is x xs h () | nothing lemma-lookupM-assoc eq i is x xs h p | just h' = apply-checkInsertProof eq i x h' record @@ -59,15 +58,13 @@ lemma-lookupM-assoc eq i is x xs h p | just h' = apply-checkInsertProof eq i ; wrong = λ x' x≢x' lookupM≡justx' → lemma-just≢nothing (trans (sym p) (lemma-checkInsert-wrong eq i x h' x' x≢x' lookupM≡justx')) } -lemma-∉-lookupM-assoc : {A : Set} {n : ℕ} → (eq : EqInst A) → (i : Fin n) → (is : List (Fin n)) → (xs : List A) → (h : FinMapMaybe n A) → assoc eq is xs ≡ just h → (i ∉ is) → lookupM i h ≡ nothing +lemma-∉-lookupM-assoc : {A : Set} {m n : ℕ} → (eq : EqInst A) → (i : Fin n) → (is : Vec (Fin n) m) → (xs : Vec A m) → (h : FinMapMaybe n A) → assoc eq is xs ≡ just h → (i ∉ toList is) → lookupM i h ≡ nothing lemma-∉-lookupM-assoc eq i [] [] h ph i∉is = begin lookupM i h ≡⟨ cong (lookupM i) (sym (lemma-from-just ph)) ⟩ lookupM i empty ≡⟨ lemma-lookupM-empty i ⟩ nothing ∎ -lemma-∉-lookupM-assoc eq i [] (x' ∷ xs') h () i∉is -lemma-∉-lookupM-assoc eq i (i' ∷ is') [] h () i∉is lemma-∉-lookupM-assoc eq i (i' ∷ is') (x' ∷ xs') h ph i∉is with assoc eq is' xs' | inspect (assoc eq is') xs' lemma-∉-lookupM-assoc eq i (i' ∷ is') (x' ∷ xs') h () i∉is | nothing | Reveal_is_.[_] ph' lemma-∉-lookupM-assoc eq i (i' ∷ is') (x' ∷ xs') h ph i∉is | just h' | Reveal_is_.[_] ph' = apply-checkInsertProof eq i' x' h' record { @@ -91,10 +88,8 @@ lemma-∉-lookupM-assoc eq i (i' ∷ is') (x' ∷ xs') h ph i∉is | just h' | R _in-domain-of_ : {n : ℕ} {A : Set} → (is : List (Fin n)) → (FinMapMaybe n A) → Set _in-domain-of_ is h = All (λ i → ∃ λ x → lookupM i h ≡ just x) is -lemma-assoc-domain : {n : ℕ} {A : Set} → (eq : EqInst A) → (is : List (Fin n)) → (xs : List A) → (h : FinMapMaybe n A) → assoc eq is xs ≡ just h → is in-domain-of h -lemma-assoc-domain eq [] [] h ph = Data.List.All.[] -lemma-assoc-domain eq [] (x' ∷ xs') h () -lemma-assoc-domain eq (i' ∷ is') [] h () +lemma-assoc-domain : {m n : ℕ} {A : Set} → (eq : EqInst A) → (is : Vec (Fin n) m) → (xs : Vec A m) → (h : FinMapMaybe n A) → assoc eq is xs ≡ just h → (toList is) in-domain-of h +lemma-assoc-domain eq [] [] h ph = Data.List.All.[] lemma-assoc-domain eq (i' ∷ is') (x' ∷ xs') h ph with assoc eq is' xs' | inspect (assoc eq is') xs' lemma-assoc-domain eq (i' ∷ is') (x' ∷ xs') h () | nothing | ph' lemma-assoc-domain eq (i' ∷ is') (x' ∷ xs') h ph | just h' | Reveal_is_.[_] ph' = apply-checkInsertProof eq i' x' h' record { @@ -109,7 +104,7 @@ lemma-assoc-domain eq (i' ∷ is') (x' ∷ xs') h ph | just h' | Reveal_is_.[_] ; wrong = λ x'' x'≢x'' lookupM-i'-h'≡just-x'' → lemma-just≢nothing (trans (sym ph) (lemma-checkInsert-wrong eq i' x' h' x'' x'≢x'' lookupM-i'-h'≡just-x'')) } -lemma-map-lookupM-insert : {n : ℕ} {A : Set} → (eq : EqInst A) → (i : Fin n) → (is : List (Fin n)) → (x : A) → (h : FinMapMaybe n A) → ¬(i ∈ is) → is in-domain-of h → map (flip lookupM (insert i x h)) is ≡ map (flip lookupM h) is +lemma-map-lookupM-insert : {m n : ℕ} {A : Set} → (eq : EqInst A) → (i : Fin n) → (is : Vec (Fin n) m) → (x : A) → (h : FinMapMaybe n A) → ¬(i ∈ (toList is)) → (toList is) in-domain-of h → map (flip lookupM (insert i x h)) is ≡ map (flip lookupM h) is lemma-map-lookupM-insert eq i [] x h i∉is ph = refl lemma-map-lookupM-insert eq i (i' ∷ is') x h i∉is ph = begin lookupM i' (insert i x h) ∷ map (flip lookupM (insert i x h)) is' @@ -118,11 +113,9 @@ lemma-map-lookupM-insert eq i (i' ∷ is') x h i∉is ph = begin ≡⟨ cong (_∷_ (lookupM i' h)) (lemma-map-lookupM-insert eq i is' x h (i∉is ∘ there) (Data.List.All.tail ph)) ⟩ lookupM i' h ∷ map (flip lookupM h) is' ∎ -lemma-map-lookupM-assoc : {n : ℕ} {A : Set} → (eq : EqInst A) → (i : Fin n) → (is : List (Fin n)) → (x : A) → (xs : List A) → (h : FinMapMaybe n A) → (h' : FinMapMaybe n A) → assoc eq is xs ≡ just h' → checkInsert eq i x h' ≡ just h → map (flip lookupM h) is ≡ map (flip lookupM h') is +lemma-map-lookupM-assoc : {m n : ℕ} {A : Set} → (eq : EqInst A) → (i : Fin n) → (is : Vec (Fin n) m) → (x : A) → (xs : Vec A m) → (h : FinMapMaybe n A) → (h' : FinMapMaybe n A) → assoc eq is xs ≡ just h' → checkInsert eq i x h' ≡ just h → map (flip lookupM h) is ≡ map (flip lookupM h') is lemma-map-lookupM-assoc eq i [] x [] h h' ph' ph = refl -lemma-map-lookupM-assoc eq i [] x (x' ∷ xs') h h' () ph -lemma-map-lookupM-assoc eq i (i' ∷ is') x [] h h' () ph -lemma-map-lookupM-assoc eq i (i' ∷ is') x (x' ∷ xs') h h' ph' ph with any (_≟_ i) (i' ∷ is') +lemma-map-lookupM-assoc eq i (i' ∷ is') x (x' ∷ xs') h h' ph' ph with any (_≟_ i) (toList (i' ∷ is')) lemma-map-lookupM-assoc eq i (i' ∷ is') x (x' ∷ xs') h h' ph' ph | yes p with Data.List.All.lookup (lemma-assoc-domain eq (i' ∷ is') (x' ∷ xs') h' ph') p lemma-map-lookupM-assoc eq i (i' ∷ is') x (x' ∷ xs') h h' ph' ph | yes p | (x'' , p') with lookupM i h' lemma-map-lookupM-assoc eq i (i' ∷ is') x (x' ∷ xs') h h' ph' ph | yes p | (x'' , refl) | .(just x'') with eq x x'' @@ -135,10 +128,8 @@ lemma-map-lookupM-assoc eq i (i' ∷ is') x (x' ∷ xs') h h' ph' ph | no ¬p re ≡⟨ lemma-map-lookupM-insert eq i (i' ∷ is') x h' ¬p (lemma-assoc-domain eq (i' ∷ is') (x' ∷ xs') h' ph') ⟩ map (flip lookupM h') (i' ∷ is') ∎ -lemma-2 : {τ : Set} {n : ℕ} → (eq : EqInst τ) → (is : List (Fin n)) → (v : List τ) → (h : FinMapMaybe n τ) → assoc eq is v ≡ just h → map (flip lookupM h) is ≡ map just v +lemma-2 : {τ : Set} {m n : ℕ} → (eq : EqInst τ) → (is : Vec (Fin n) m) → (v : Vec τ m) → (h : FinMapMaybe n τ) → assoc eq is v ≡ just h → map (flip lookupM h) is ≡ map just v lemma-2 eq [] [] h p = refl -lemma-2 eq [] (x ∷ xs) h () -lemma-2 eq (x ∷ xs) [] h () lemma-2 eq (i ∷ is) (x ∷ xs) h p with assoc eq is xs | inspect (assoc eq is) xs lemma-2 eq (i ∷ is) (x ∷ xs) h () | nothing | _ lemma-2 eq (i ∷ is) (x ∷ xs) h p | just h' | Reveal_is_.[_] ir = begin @@ -160,28 +151,24 @@ lemma-2 eq (i ∷ is) (x ∷ xs) h p | just h' | Reveal_is_.[_] ir = begin map just (x ∷ xs) ∎ postulate - free-theorem-list-list : {β γ : Set} → (get : {α : Set} → List α → List α) → (f : β → γ) → get ∘ map f ≗ map f ∘ get + free-theorem-list-list : {β γ : Set} → {getlen : ℕ → ℕ} → (get : {α : Set} {n : ℕ} → Vec α n → Vec α (getlen n)) → (f : β → γ) → {m : ℕ } → (v : Vec β m) → get (map f v) ≡ map f (get v) -toList-map-commutes : {A B : Set} {n : ℕ} → (f : A → B) → (v : Data.Vec.Vec A n) → (toList (Data.Vec.map f v)) ≡ map f (toList v) -toList-map-commutes f Data.Vec.[] = refl -toList-map-commutes f (x ∷V xs) = cong (_∷_ (f x)) (toList-map-commutes f xs) - -lemma-map-denumerate-enumerate : {A : Set} → (as : List A) → map (denumerate as) (enumerate as) ≡ as -lemma-map-denumerate-enumerate [] = refl +lemma-map-denumerate-enumerate : {m : ℕ} {A : Set} → (as : Vec A m) → map (denumerate as) (enumerate as) ≡ as +lemma-map-denumerate-enumerate [] = refl lemma-map-denumerate-enumerate (a ∷ as) = cong (_∷_ a) (begin - map (flip lookupVec (a ∷V (fromList as))) (toList (tabulate Fin.suc)) - ≡⟨ cong (map (flip lookupVec (a ∷V (fromList as))) ∘ toList) (tabulate-∘ Fin.suc id) ⟩ - map (flip lookupVec (a ∷V (fromList as))) (toList (Data.Vec.map Fin.suc (tabulate id))) - ≡⟨ cong (map (flip lookupVec (a ∷V fromList as))) (toList-map-commutes Data.Fin.suc (tabulate id)) ⟩ - map (flip lookupVec (a ∷V fromList as)) (map Fin.suc (enumerate as)) - ≡⟨ sym (map-∘ (enumerate as)) ⟩ - map (flip lookupVec (a ∷V (fromList as)) ∘ Fin.suc) (enumerate as) + map (flip lookupVec (a ∷ as)) (tabulate Fin.suc) + ≡⟨ cong (map (flip lookupVec (a ∷ as))) (tabulate-∘ Fin.suc id) ⟩ + map (flip lookupVec (a ∷ as)) (map Fin.suc (tabulate id)) + ≡⟨ refl ⟩ + map (flip lookupVec (a ∷ as)) (map Fin.suc (enumerate as)) + ≡⟨ sym (map-∘ _ _ (enumerate as)) ⟩ + map (flip lookupVec (a ∷ as) ∘ Fin.suc) (enumerate as) ≡⟨ refl ⟩ map (denumerate as) (enumerate as) ≡⟨ lemma-map-denumerate-enumerate as ⟩ as ∎) -theorem-1 : (get : {α : Set} → List α → List α) → {τ : Set} → (eq : EqInst τ) → (s : List τ) → bff get eq s (get s) ≡ just s +theorem-1 : {getlen : ℕ → ℕ} → (get : {α : Set} {n : ℕ} → Vec α n → Vec α (getlen n)) → {m : ℕ} {τ : Set} → (eq : EqInst τ) → (s : Vec τ m) → bff get eq s (get s) ≡ just s theorem-1 get eq s = begin bff get eq s (get s) ≡⟨ cong (bff get eq s ∘ get) (sym (lemma-map-denumerate-enumerate s)) ⟩ @@ -191,10 +178,10 @@ theorem-1 get eq s = begin ≡⟨ refl ⟩ fmap (flip map (enumerate s) ∘ flip lookup) (fmap (flip union (fromFunc (denumerate s))) (assoc eq (get (enumerate s)) (map (denumerate s) (get (enumerate s))))) ≡⟨ cong (fmap (flip map (enumerate s) ∘ flip lookup) ∘ fmap (flip union (fromFunc (denumerate s)))) (lemma-1 eq (denumerate s) (get (enumerate s))) ⟩ - fmap (flip map (enumerate s) ∘ flip lookup) (fmap (flip union (fromFunc (flip lookupVec (fromList s)))) (just (restrict (denumerate s) (get (enumerate s))))) + fmap (flip map (enumerate s) ∘ flip lookup) (fmap (flip union (fromFunc (flip lookupVec s))) (just (restrict (denumerate s) (toList (get (enumerate s)))))) ≡⟨ refl ⟩ - just ((flip map (enumerate s) ∘ flip lookup) (union (restrict (denumerate s) (get (enumerate s))) (fromFunc (denumerate s)))) - ≡⟨ cong just (cong (flip map (enumerate s) ∘ flip lookup) (lemma-union-restrict (denumerate s) (get (enumerate s)))) ⟩ + just ((flip map (enumerate s) ∘ flip lookup) (union (restrict (denumerate s) (toList (get (enumerate s)))) (fromFunc (denumerate s)))) + ≡⟨ cong just (cong (flip map (enumerate s) ∘ flip lookup) (lemma-union-restrict (denumerate s) (toList (get (enumerate s))))) ⟩ just ((flip map (enumerate s) ∘ flip lookup) (fromFunc (denumerate s))) ≡⟨ refl ⟩ just (map (flip lookup (fromFunc (denumerate s))) (enumerate s)) @@ -207,14 +194,15 @@ lemma-fmap-just : {A B : Set} → {f : A → B} {b : B} → (ma : Maybe A) → f lemma-fmap-just (just x) fmap-f-ma≡just-b = x , refl lemma-fmap-just nothing () -lemma-from-map-just : {A : Set} → {xs ys : List A} → map Maybe.just xs ≡ map Maybe.just ys → xs ≡ ys -lemma-from-map-just {xs = []} {ys = []} p = refl -lemma-from-map-just {xs = []} {ys = y ∷ ys'} () -lemma-from-map-just {xs = x ∷ xs'} {ys = []} () -lemma-from-map-just {xs = x ∷ xs'} {ys = y ∷ ys'} p with ∷-injective p +∷-injective : {A : Set} {n : ℕ} {x y : A} {xs ys : Vec A n} → (x ∷ xs) ≡ (y ∷ ys) → x ≡ y × xs ≡ ys +∷-injective refl = refl , refl + +lemma-from-map-just : {A : Set} {n : ℕ} → {xs ys : Vec A n} → map Maybe.just xs ≡ map Maybe.just ys → xs ≡ ys +lemma-from-map-just {xs = []} {ys = []} p = refl +lemma-from-map-just {xs = x ∷ xs'} {ys = y ∷ ys'} p with ∷-injective p lemma-from-map-just {xs = x ∷ xs'} {ys = .x ∷ ys'} p | refl , p' = cong (_∷_ x) (lemma-from-map-just p') -lemma-union-not-used : {n : ℕ} {A : Set} (h : FinMapMaybe n A) → (h' : FinMap n A) → (is : List (Fin n)) → is in-domain-of h → map just (map (flip lookup (union h h')) is) ≡ map (flip lookupM h) is +lemma-union-not-used : {m n : ℕ} {A : Set} (h : FinMapMaybe n A) → (h' : FinMap n A) → (is : Vec (Fin n) m) → (toList is) in-domain-of h → map just (map (flip lookup (union h h')) is) ≡ map (flip lookupM h) is lemma-union-not-used h h' [] p = refl lemma-union-not-used h h' (i ∷ is') p with Data.List.All.head p lemma-union-not-used h h' (i ∷ is') p | x , lookupM-i-h≡just-x = begin @@ -233,7 +221,7 @@ lemma-union-not-used h h' (i ∷ is') p | x , lookupM-i-h≡just-x = begin ≡⟨ cong (_∷_ (lookupM i h)) (lemma-union-not-used h h' is' (Data.List.All.tail p)) ⟩ lookupM i h ∷ map (flip lookupM h) is' ∎ -theorem-2 : (get : {α : Set} → List α → List α) → {τ : Set} → (eq : EqInst τ) → (v s u : List τ) → bff get eq s v ≡ just u → get u ≡ v +theorem-2 : {getlen : ℕ → ℕ} (get : {α : Set} {n : ℕ} → Vec α n → Vec α (getlen n)) → {τ : Set} {m : ℕ} → (eq : EqInst τ) → (v : Vec τ (getlen m)) → (s u : Vec τ m) → bff get eq s v ≡ just u → get u ≡ v theorem-2 get eq v s u p with lemma-fmap-just (assoc eq (get (enumerate s)) v) (proj₂ (lemma-fmap-just (fmap (flip union (fromFunc (denumerate s))) (assoc eq (get (enumerate s)) v)) p)) theorem-2 get eq v s u p | h , ph = begin get u diff --git a/Precond.agda b/Precond.agda new file mode 100644 index 0000000..f856c57 --- /dev/null +++ b/Precond.agda @@ -0,0 +1,71 @@ +module Precond where + +open import Data.Nat using (ℕ) renaming (zero to nzero ; suc to nsuc) +open import Data.Fin using (Fin ; zero ; suc) +open import Data.Vec using (Vec ; [] ; _∷_ ; map ; lookup ; toList) +open import Data.List.Any using (here ; there) +open Data.List.Any.Membership-≡ using (_∉_) +open import Data.Maybe using (just) +open import Data.Product using (∃ ; _,_) +open import Function using (flip ; _∘_) +open import Relation.Binary.Core using (_≡_ ; _≢_) +open import Relation.Binary.PropositionalEquality using (refl ; cong) +open Relation.Binary.PropositionalEquality.≡-Reasoning using (begin_ ; _≡⟨_⟩_ ; _∎) + +open import FinMap using (FinMap ; FinMapMaybe ; union ; fromFunc ; empty ; insert) +open import CheckInsert using (EqInst ; checkInsert ; lemma-checkInsert-new) +open import BFF using (fmap ; _>>=_) +open import Bidir using (lemma-∉-lookupM-assoc) + +open BFF.VecBFF using (assoc ; enumerate ; denumerate ; bff) + +assoc-enough : {getlen : ℕ → ℕ} (get : {A : Set} {n : ℕ} → Vec A n → Vec A (getlen n)) → {B : Set} {m : ℕ} → (eq : EqInst B) → (s : Vec B m) → (v : Vec B (getlen m)) → (h : FinMapMaybe m B) → assoc eq (get (enumerate s)) v ≡ just h → ∃ λ u → bff get eq s v ≡ just u +assoc-enough get {B} {m} eq s v h p = map (flip lookup (union h g)) s′ , (begin + bff get eq s v + ≡⟨ refl ⟩ + fmap (flip map s′ ∘ flip lookup) (fmap (flip union g) (assoc eq (get s′) v)) + ≡⟨ cong (fmap (flip map s′ ∘ flip lookup)) (cong (fmap (flip union g)) p) ⟩ + fmap (flip map s′ ∘ flip lookup) (fmap (flip union g) (just h)) + ≡⟨ refl ⟩ + just (map (flip lookup (union h g)) s′) ∎) + where s′ : Vec (Fin m) m + s′ = enumerate s + g : FinMap m B + g = fromFunc (denumerate s) + +all-different : {A : Set} {n : ℕ} → Vec A n → Set +all-different {_} {n} v = (i : Fin n) → (j : Fin n) → i ≢ j → lookup i v ≢ lookup j v + +suc-≡ : {n : ℕ} {i j : Fin n} → (suc i ≡ suc j) → i ≡ j +suc-≡ refl = refl + +different-swap : {A : Set} {n : ℕ} → (a b : A) → (v : Vec A n) → all-different (a ∷ b ∷ v) → all-different (b ∷ a ∷ v) +different-swap a b v p zero zero i≢j li≡lj = i≢j refl +different-swap a b v p zero (suc zero) i≢j li≡lj = p (suc zero) zero (λ ()) li≡lj +different-swap a b v p zero (suc (suc j)) i≢j li≡lj = p (suc zero) (suc (suc j)) (λ ()) li≡lj +different-swap a b v p (suc zero) zero i≢j li≡lj = p zero (suc zero) (λ ()) li≡lj +different-swap a b v p (suc zero) (suc zero) i≢j li≡lj = i≢j refl +different-swap a b v p (suc zero) (suc (suc j)) i≢j li≡lj = p zero (suc (suc j)) (λ ()) li≡lj +different-swap a b v p (suc (suc i)) zero i≢j li≡lj = p (suc (suc i)) (suc zero) (λ ()) li≡lj +different-swap a b v p (suc (suc i)) (suc zero) i≢j li≡lj = p (suc (suc i)) zero (λ ()) li≡lj +different-swap a b v p (suc (suc i)) (suc (suc j)) i≢j li≡lj = p (suc (suc i)) (suc (suc j)) i≢j li≡lj + +different-drop : {A : Set} {n : ℕ} → (a : A) → (v : Vec A n) → all-different (a ∷ v) → all-different v +different-drop a v p i j i≢j = p (suc i) (suc j) (i≢j ∘ suc-≡) + +different-∉ : {A : Set} {n : ℕ} → (x : A) (xs : Vec A n) → all-different (x ∷ xs) → x ∉ (toList xs) +different-∉ x [] p () +different-∉ x (y ∷ ys) p (here px) = p zero (suc zero) (λ ()) px +different-∉ x (y ∷ ys) p (there pxs) = different-∉ x ys (different-drop y (x ∷ ys) (different-swap x y ys p)) pxs + +different-assoc : {B : Set} {m n : ℕ} → (eq : EqInst B) → (u : Vec (Fin n) m) → (v : Vec B m) → all-different u → ∃ λ h → assoc eq u v ≡ just h +different-assoc eq [] [] p = empty , refl +different-assoc eq (u ∷ us) (v ∷ vs) p with different-assoc eq us vs (λ i j i≢j → p (suc i) (suc j) (i≢j ∘ suc-≡)) +different-assoc eq (u ∷ us) (v ∷ vs) p | h , p' = insert u v h , (begin + assoc eq (u ∷ us) (v ∷ vs) + ≡⟨ refl ⟩ + assoc eq us vs >>= checkInsert eq u v + ≡⟨ cong (flip _>>=_ (checkInsert eq u v)) p' ⟩ + checkInsert eq u v h + ≡⟨ lemma-checkInsert-new eq u v h (lemma-∉-lookupM-assoc eq u us vs h p' (different-∉ u us p)) ⟩ + just (insert u v h) ∎)
\ No newline at end of file |