3 open import Level using () renaming (zero to â„“â‚€)
4 open import Data.Nat using (â„• ; zero ; suc)
5 open import Data.Maybe using (Maybe ; just ; nothing ; maybe′)
6 open import Data.Fin using (Fin ; zero ; suc)
7 open import Data.Fin.Properties using (_≟_)
8 open import Data.Vec using (Vec ; [] ; _∷_ ; _[_]≔_ ; replicate ; tabulate ; foldr ; zip ; toList) renaming (lookup to lookupVec ; map to mapV)
9 open import Data.Vec.Equality using ()
10 open import Data.Vec.Properties using (lookup∘update ; lookup∘update′)
11 open import Data.Product using (_×_ ; _,_)
12 open import Data.List.All as All using (All)
13 import Data.List.All.Properties as AllP
14 import Data.List.Any as Any
15 import Data.List.Any.Membership.Propositional
16 open import Function using (id ; _∘_ ; flip ; const)
17 open import Function.Equality using (module Î )
18 open import Function.Surjection using (module Surjection)
19 open import Relation.Nullary using (yes ; no)
20 open import Relation.Nullary.Negation using (contradiction)
21 open import Relation.Binary.Core using (Decidable)
22 open import Relation.Binary.PropositionalEquality as P using (_≡_ ; _≢_ ; _≗_)
23 open P.≡-Reasoning using (begin_ ; _≡⟨_⟩_ ; _∎)
25 open import Generic using (just-injective)
27 _∈_ : {A : Set} {n : ℕ} → A → Vec A n → Set
28 _∈_ {A} x xs = x Data.List.Any.Membership.Propositional.∈ (toList xs)
30 _∉_ : {A : Set} {n : ℕ} → A → Vec A n → Set
31 _∉_ {A} x xs = All (_≢_ x) (toList xs)
33 data Dec∈ {A : Set} {n : ℕ} (x : A) (xs : Vec A n) : Set where
34 yes-∈ : x ∈ xs → Dec∈ x xs
35 no-∉ : x ∉ xs → Dec∈ x xs
37 is-∈ : {A : Set} {n : ℕ} → Decidable (_≡_ {A = A}) → (x : A) → (xs : Vec A n) → Dec∈ x xs
38 is-∈ eq? x xs with Any.any (eq? x) (toList xs)
39 ... | yes x∈xs = yes-∈ x∈xs
40 ... | no x∉xs = no-∉ (Π._⟨$⟩_ (Surjection.to AllP.¬Any↠All¬) x∉xs)
42 FinMapMaybe : ℕ → Set → Set
43 FinMapMaybe n A = Vec (Maybe A) n
45 lookupM : {A : Set} {n : ℕ} → Fin n → FinMapMaybe n A → Maybe A
48 insert : {A : Set} {n : ℕ} → Fin n → A → FinMapMaybe n A → FinMapMaybe n A
49 insert f a m = m [ f ]≔ (just a)
51 empty : {A : Set} {n : ℕ} → FinMapMaybe n A
52 empty = replicate nothing
54 fromAscList : {A : Set} {n m : ℕ} → Vec (Fin n × A) m → FinMapMaybe n A
55 fromAscList [] = empty
56 fromAscList ((f , a) ∷ xs) = insert f a (fromAscList xs)
58 fromFunc : {A : Set} {n : ℕ} → (Fin n → A) → FinMapMaybe n A
59 fromFunc = tabulate ∘ _∘_ Maybe.just
61 reshape : {n : ℕ} {A : Set} → FinMapMaybe n A → (l : ℕ) → FinMapMaybe l A
63 reshape [] (suc l) = nothing ∷ (reshape [] l)
64 reshape (x ∷ xs) (suc l) = x ∷ (reshape xs l)
66 union : {A : Set} {n : ℕ} → FinMapMaybe n A → FinMapMaybe n A → FinMapMaybe n A
67 union m1 m2 = tabulate (λ f → maybe′ just (lookupM f m2) (lookupM f m1))
69 restrict : {A : Set} {n m : ℕ} → (Fin n → A) → Vec (Fin n) m → FinMapMaybe n A
70 restrict f is = fromAscList (zip is (mapV f is))
72 delete : {A : Set} {n : ℕ} → Fin n → FinMapMaybe n A → FinMapMaybe n A
73 delete i m = m [ i ]≔ nothing
75 delete-many : {A : Set} {n m : ℕ} → Vec (Fin n) m → FinMapMaybe n A → FinMapMaybe n A
76 delete-many = flip (foldr (const _) delete)
78 lemma-insert-same : {n : ℕ} {A : Set} → (m : FinMapMaybe n A) → (f : Fin n) → {a : A} → lookupM f m ≡ just a → m ≡ insert f a m
79 lemma-insert-same [] () p
80 lemma-insert-same {suc n} (x ∷ xs) zero p = P.cong (flip _∷_ xs) p
81 lemma-insert-same (x ∷ xs) (suc i) p = P.cong (_∷_ x) (lemma-insert-same xs i p)
83 lemma-lookupM-empty : {A : Set} {n : ℕ} → (i : Fin n) → lookupM {A} i empty ≡ nothing
84 lemma-lookupM-empty zero = P.refl
85 lemma-lookupM-empty (suc i) = lemma-lookupM-empty i
87 lemma-lookupM-restrict : {A : Set} {n m : ℕ} → (i : Fin n) → (f : Fin n → A) → (is : Vec (Fin n) m) → {a : A} → lookupM i (restrict f is) ≡ just a → f i ≡ a
88 lemma-lookupM-restrict i f [] p = contradiction (P.trans (P.sym p) (lemma-lookupM-empty i)) (λ ())
89 lemma-lookupM-restrict i f (i' ∷ is) p with i ≟ i'
90 lemma-lookupM-restrict i f (.i ∷ is) {a} p | yes P.refl = just-injective (begin
92 ≡⟨ P.sym (lookup∘update i (restrict f is) (just (f i))) ⟩
93 lookupM i (insert i (f i) (restrict f is))
96 lemma-lookupM-restrict i f (i' ∷ is) {a} p | no i≢i' = lemma-lookupM-restrict i f is (begin
97 lookupM i (restrict f is)
98 ≡⟨ P.sym (lookup∘update′ i≢i' (restrict f is) (just (f i'))) ⟩
99 lookupM i (insert i' (f i') (restrict f is))
102 lemma-lookupM-restrict-∈ : {A : Set} {n m : ℕ} → (i : Fin n) → (f : Fin n → A) → (js : Vec (Fin n) m) → i ∈ js → lookupM i (restrict f js) ≡ just (f i)
103 lemma-lookupM-restrict-∈ i f [] ()
104 lemma-lookupM-restrict-∈ i f (j ∷ js) p with i ≟ j
105 lemma-lookupM-restrict-∈ i f (.i ∷ js) p | yes P.refl = lookup∘update i (restrict f js) (just (f i))
106 lemma-lookupM-restrict-∈ i f (j ∷ js) (Any.here i≡j) | no i≢j = contradiction i≡j i≢j
107 lemma-lookupM-restrict-∈ i f (j ∷ js) (Any.there p) | no i≢j =
108 P.trans (lookup∘update′ i≢j (restrict f js) (just (f j)))
109 (lemma-lookupM-restrict-∈ i f js p)
111 lemma-lookupM-restrict-∉ : {A : Set} {n m : ℕ} → (i : Fin n) → (f : Fin n → A) → (js : Vec (Fin n) m) → i ∉ js → lookupM i (restrict f js) ≡ nothing
112 lemma-lookupM-restrict-∉ i f [] i∉[] = lemma-lookupM-empty i
113 lemma-lookupM-restrict-∉ i f (j ∷ js) i∉jjs =
114 P.trans (lookup∘update′ (All.head i∉jjs) (restrict f js) (just (f j)))
115 (lemma-lookupM-restrict-∉ i f js (All.tail i∉jjs))
117 lemma-tabulate-∘ : {n : ℕ} {A : Set} → {f g : Fin n → A} → f ≗ g → tabulate f ≡ tabulate g
118 lemma-tabulate-∘ {zero} {_} {f} {g} f≗g = P.refl
119 lemma-tabulate-∘ {suc n} {_} {f} {g} f≗g = P.cong₂ _∷_ (f≗g zero) (lemma-tabulate-∘ (f≗g ∘ suc))
121 lemma-lookupM-fromFunc : {n : ℕ} {A : Set} → (f : Fin n → A) → flip lookupM (fromFunc f) ≗ Maybe.just ∘ f
122 lemma-lookupM-fromFunc f zero = P.refl
123 lemma-lookupM-fromFunc f (suc i) = lemma-lookupM-fromFunc (f ∘ suc) i
125 lemma-lookupM-delete : {n : ℕ} {A : Set} {i j : Fin n} → (f : FinMapMaybe n A) → i ≢ j → lookupM i (delete j f) ≡ lookupM i f
126 lemma-lookupM-delete {i = zero} {j = zero} (_ ∷ _) p = contradiction P.refl p
127 lemma-lookupM-delete {i = zero} {j = suc j} (_ ∷ _) p = P.refl
128 lemma-lookupM-delete {i = suc i} {j = zero} (x ∷ xs) p = P.refl
129 lemma-lookupM-delete {i = suc i} {j = suc j} (x ∷ xs) p = lemma-lookupM-delete xs (p ∘ P.cong suc)
131 lemma-lookupM-delete-many : {n m : ℕ} {A : Set} (h : FinMapMaybe n A) → (i : Fin n) → (js : Vec (Fin n) m) → i ∉ js → lookupM i (delete-many js h) ≡ lookupM i h
132 lemma-lookupM-delete-many {n} h i [] i∉[] = P.refl
133 lemma-lookupM-delete-many {n} h i (j ∷ js) i∉jjs =
134 P.trans (lemma-lookupM-delete (delete-many js h) (All.head i∉jjs))
135 (lemma-lookupM-delete-many h i js (All.tail i∉jjs))
137 lemma-reshape-id : {n : ℕ} {A : Set} → (m : FinMapMaybe n A) → reshape m n ≡ m
138 lemma-reshape-id [] = P.refl
139 lemma-reshape-id (x ∷ xs) = P.cong (_∷_ x) (lemma-reshape-id xs)
141 lemma-disjoint-union : {n m : ℕ} {A : Set} → (f : Fin n → A) → (t : Vec (Fin n) m) → union (restrict f t) (delete-many t (fromFunc f)) ≡ fromFunc f
142 lemma-disjoint-union {n} f t = lemma-tabulate-∘ inner
143 where inner : (x : Fin n) → maybe′ just (lookupM x (delete-many t (fromFunc f))) (lookupM x (restrict f t)) ≡ just (f x)
144 inner x with is-∈ _≟_ x t
145 inner x | yes-∈ x∈t = P.cong (maybe′ just (lookupM x (delete-many t (fromFunc f)))) (lemma-lookupM-restrict-∈ x f t x∈t)
146 inner x | no-∉ x∉t = begin
147 maybe′ just (lookupM x (delete-many t (fromFunc f))) (lookupM x (restrict f t))
148 ≡⟨ P.cong₂ (maybe′ just) (lemma-lookupM-delete-many (fromFunc f) x t x∉t) (lemma-lookupM-restrict-∉ x f t x∉t) ⟩
149 maybe′ just (lookupM x (fromFunc f)) nothing
150 ≡⟨ lemma-lookupM-fromFunc f x ⟩
153 lemma-exchange-maps : {n m : ℕ} → {A : Set} → {h h′ : FinMapMaybe n A} → {P : Fin n → Set} → (∀ j → P j → lookupM j h ≡ lookupM j h′) → {is : Vec (Fin n) m} → All P (toList is) → mapV (flip lookupM h) is ≡ mapV (flip lookupM h′) is
154 lemma-exchange-maps h≈h′ {[]} All.[] = P.refl
155 lemma-exchange-maps h≈h′ {i ∷ is} (pi All.∷ pis) = P.cong₂ _∷_ (h≈h′ i pi) (lemma-exchange-maps h≈h′ pis)