Debian Codegraph @

src/Tactic/RingSolver/Core/Polynomial/Homomorphism/Lemmas.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Lemmas for use in proving the polynomial homomorphism.
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 open import Tactic.RingSolver.Core.Polynomial.Parameters
10
11 module Tactic.RingSolver.Core.Polynomial.Homomorphism.Lemmas
12 {r₁ r₂ r₃ r₄}
13 (homo : Homomorphism r₁ r₂ r₃ r₄)
14 where
15
16 open import Data.Bool.Base using (Bool;true;false)
17 open import Data.Nat.Base as ℕ using (ℕ; suc; zero; compare; _≤′_; ≤′-step; ≤′-refl)
18 open import Data.Nat.Properties as ℕ using (≤′-trans)
19 open import Data.Vec.Base as Vec using (Vec; _∷_)
20 open import Data.Fin.Base using (Fin; zero; suc)
21 open import Data.List.Base using (_∷_; [])
22 open import Data.Unit.Base using (tt)
23 open import Data.List.Kleene
24 open import Data.Product.Base using (_,_; proj₁; proj₂; map₁; _×_)
25 open import Data.Maybe.Base using (nothing; just)
26 open import Function.Base using (_⟨_⟩_)
27 open import Level using (lift)
28 open import Relation.Nullary.Decidable.Core using (Dec; yes; no)
29 open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_)
30
31 open Homomorphism homo hiding (_^_)
32 open import Tactic.RingSolver.Core.Polynomial.Reasoning to
33 open import Tactic.RingSolver.Core.Polynomial.Base from
34 open import Tactic.RingSolver.Core.Polynomial.Semantics homo
35
36 open import Algebra.Properties.Semiring.Exp.TCOptimised semiring
37
38 ------------------------------------------------------------------------
39 -- Power lemmas
40 --
41 -- We prove some things about our odd exponentiation operator
42
43 -- First, that the optimised operator is the same as normal
44 -- exponentiation.
45 pow-opt : ∀ x ρ i → x *⟨ ρ ⟩^ i ≈ ρ ^ i * x
46 pow-opt x ρ zero = sym (*-identityˡ x)
47 pow-opt x ρ (suc i) = refl
48
49 pow-add : ∀ x y i j → y ^ (suc j) * x *⟨ y ⟩^ i ≈ x *⟨ y ⟩^ (i ℕ.+ suc j)
50 pow-add x y zero j = refl
51 pow-add x y (suc i) j = go x y i j
52 where
53 go : ∀ x y i j → (y ^ suc j) * ((y ^ suc i) * x) ≈ y ^ suc (i ℕ.+ suc j) * x
54 go x y zero j = sym (*-assoc _ _ _)
55 go x y (suc i) j = begin
56 (y ^ suc j) * (y ^ (suc i) * y * x) ≈⟨ *≫ *-assoc _ y x ⟩
57 (y ^ suc j) * (y ^ (suc i) * (y * x)) ≈⟨ go (y * x) y i j ⟩
58 y ^ suc (i ℕ.+ suc j) * (y * x) ≈⟨ sym (*-assoc _ y x) ⟩
59 y ^ suc (suc i ℕ.+ suc j) * x ∎
60
61 -- Here we show a homomorphism on exponentiation, i.e. that using the
62 -- exponentiation function on polynomials and then evaluating is the
63 -- same as evaluating and then exponentiating.
64 pow-hom : ∀ {n} i
65 → (xs : Coeff n +)
66 → ∀ ρ ρs
67 → ⅀⟦ xs ⟧ (ρ , ρs) *⟨ ρ ⟩^ i ≈ ⅀⟦ xs ⍓+ i ⟧ (ρ , ρs)
68 pow-hom zero (x Δ j & xs) ρ ρs rewrite ℕ.+-identityʳ j = refl
69 pow-hom (suc i) (x ≠0 Δ j & xs) ρ ρs =
70 begin
71 ρ ^ (suc i) * (((x , xs) ⟦∷⟧ (ρ , ρs)) *⟨ ρ ⟩^ j)
72 ≈⟨ pow-add _ ρ j i ⟩
73 (((x , xs) ⟦∷⟧ (ρ , ρs)) *⟨ ρ ⟩^ (j ℕ.+ suc i))
74 ∎
75
76 -- Proving a congruence (we don't get this for free because we're using
77 -- setoids).
78 pow-mul-cong : ∀ {x y} → x ≈ y → ∀ ρ i → x *⟨ ρ ⟩^ i ≈ y *⟨ ρ ⟩^ i
79 pow-mul-cong x≈y ρ zero = x≈y
80 pow-mul-cong x≈y ρ (suc i) = *≫ x≈y
81
82 -- Demonstrating that the proof of zeroness is correct.
83 zero-hom : ∀ {n} (p : Poly n) → Zero p → (ρs : Vec Carrier n) → 0# ≈ ⟦ p ⟧ ρs
84 zero-hom (Κ x ⊐ i≤n) p≡0 ρs = Zero-C⟶Zero-R x p≡0
85
86 -- x¹⁺ⁿ = xxⁿ
87 pow-suc : ∀ x i → x ^ suc i ≈ x * x ^ i
88 pow-suc x zero = sym (*-identityʳ _)
89 pow-suc x (suc i) = *-comm _ _
90
91 -- x¹⁺ⁿ = xⁿx
92 pow-sucʳ : ∀ x i → x ^ suc i ≈ x ^ i * x
93 pow-sucʳ x zero = sym (*-identityˡ _)
94 pow-sucʳ x (suc i) = refl
95
96 -- In the proper evaluation function, we avoid ever inserting an
97 -- unnecessary 0# like we do here. However, it is easier to prove with
98 -- the form that does insert 0#. So we write one here, and then prove
99 -- that it's equivalent to the one that adds a 0#.
100 ⅀?⟦_⟧ : ∀ {n} (xs : Coeff n *) → Carrier × Vec Carrier n → Carrier
101 ⅀?⟦ [] ⟧ _ = 0#
102 ⅀?⟦ ∹ x ⟧ = ⅀⟦ x ⟧
103
104 _⟦∷⟧?_ : ∀ {n} (x : Poly n × Coeff n *) → Carrier × Vec Carrier n → Carrier
105 (x , xs) ⟦∷⟧? (ρ , ρs) = ρ * ⅀?⟦ xs ⟧ (ρ , ρs) + ⟦ x ⟧ ρs
106
107 ⅀?-hom : ∀ {n} (xs : Coeff n +) → ∀ ρ → ⅀?⟦ ∹ xs ⟧ ρ ≈ ⅀⟦ xs ⟧ ρ
108 ⅀?-hom _ _ = refl
109
110 ⟦∷⟧?-hom : ∀ {n} (x : Poly n) → ∀ xs ρ ρs → (x , xs) ⟦∷⟧? (ρ , ρs) ≈ (x , xs) ⟦∷⟧ (ρ , ρs)
111 ⟦∷⟧?-hom x (∹ xs ) ρ ρs = refl
112 ⟦∷⟧?-hom x [] ρ ρs = (≪+ zeroʳ _) ⟨ trans ⟩ +-identityˡ _
113
114 pow′-hom : ∀ {n} i (xs : Coeff n *) → ∀ ρ ρs → ((⅀?⟦ xs ⟧ (ρ , ρs)) *⟨ ρ ⟩^ i) ≈ (⅀?⟦ xs ⍓* i ⟧ (ρ , ρs))
115 pow′-hom i (∹ xs ) ρ ρs = pow-hom i xs ρ ρs
116 pow′-hom zero [] ρ ρs = refl
117 pow′-hom (suc i) [] ρ ρs = zeroʳ _
118
119 -- Here, we show that the normalising cons is correct.
120 -- This lets us prove with respect to the non-normalising form,
121 -- especially when we're using the folds.
122 ∷↓-hom-0 : ∀ {n} (x : Poly n) → ∀ xs ρ ρs → ⅀?⟦ x Δ 0 ∷↓ xs ⟧ (ρ , ρs) ≈ (x , xs) ⟦∷⟧ (ρ , ρs)
123 ∷↓-hom-0 x xs ρ ρs with zero? x
124 ∷↓-hom-0 x xs ρ ρs | no ¬p = refl
125 ∷↓-hom-0 x [] ρ ρs | yes p = zero-hom x p ρs
126 ∷↓-hom-0 x (∹ xs ) ρ ρs | yes p =
127 begin
128 ⅀⟦ xs ⍓+ 1 ⟧ (ρ , ρs)
129 ≈⟨ sym (pow-hom 1 xs ρ ρs) ⟩
130 ρ * ⅀⟦ xs ⟧ (ρ , ρs)
131 ≈⟨ sym (+-identityʳ _) ⟨ trans ⟩ (+≫ zero-hom x p ρs) ⟩
132 ρ * ⅀⟦ xs ⟧ (ρ , ρs) + ⟦ x ⟧ ρs
133 ∎
134
135 ∷↓-hom-s : ∀ {n} (x : Poly n) → ∀ i xs ρ ρs → ⅀?⟦ x Δ suc i ∷↓ xs ⟧ (ρ , ρs) ≈ (ρ ^ suc i) * (x , xs) ⟦∷⟧ (ρ , ρs)
136 ∷↓-hom-s x i xs ρ ρs with zero? x
137 ∷↓-hom-s x i xs ρ ρs | no ¬p = refl
138 ∷↓-hom-s x i [] ρ ρs | yes p = sym ((*≫ sym (zero-hom x p ρs)) ⟨ trans ⟩ zeroʳ _)
139 ∷↓-hom-s x i (∹ xs ) ρ ρs | yes p =
140 begin
141 ⅀⟦ xs ⍓+ (suc (suc i)) ⟧ (ρ , ρs)
142 ≈⟨ sym (pow-hom (suc (suc i)) xs ρ ρs) ⟩
143 (ρ ^ suc (suc i)) * ⅀⟦ xs ⟧ (ρ , ρs)
144 ≈⟨ *-assoc _ _ _ ⟩
145 (ρ ^ suc i) * (ρ * ⅀⟦ xs ⟧ (ρ , ρs))
146 ≈⟨ *≫ (sym (+-identityʳ _) ⟨ trans ⟩ (+≫ zero-hom x p ρs)) ⟩
147 (ρ ^ suc i) * (ρ * ⅀⟦ xs ⟧ (ρ , ρs) + ⟦ x ⟧ ρs)
148 ∎
149
150 ∷↓-hom : ∀ {n}
151 → (x : Poly n)
152 → ∀ i xs ρ ρs
153 → ⅀?⟦ x Δ i ∷↓ xs ⟧ (ρ , ρs) ≈ ρ ^ i * ((x , xs) ⟦∷⟧ (ρ , ρs))
154 ∷↓-hom x zero xs ρ ρs = ∷↓-hom-0 x xs ρ ρs ⟨ trans ⟩ sym (*-identityˡ _)
155 ∷↓-hom x (suc i) xs ρ ρs = ∷↓-hom-s x i xs ρ ρs
156
157 ⟦∷⟧-hom : ∀ {n}
158 → (x : Poly n)
159 → (xs : Coeff n *)
160 → ∀ ρ ρs → (x , xs) ⟦∷⟧ (ρ , ρs) ≈ ρ * ⅀?⟦ xs ⟧ (ρ , ρs) + ⟦ x ⟧ ρs
161 ⟦∷⟧-hom x [] ρ ρs = sym ((≪+ zeroʳ _) ⟨ trans ⟩ +-identityˡ _)
162 ⟦∷⟧-hom x (∹ xs) ρ ρs = refl
163
164 -- This proves that injecting a polynomial into more variables is
165 -- correct. Basically, we show that if a polynomial doesn't care about
166 -- the first few variables, we can drop them from the input vector.
167 ⅀-⊐↑-hom : ∀ {i n m}
168 → (xs : Coeff i +)
169 → (si≤n : suc i ≤′ n)
170 → (sn≤m : suc n ≤′ m)
171 → ∀ ρ
172 → ⅀⟦ xs ⟧ (drop-1 (≤′-step si≤n ⟨ ≤′-trans ⟩ sn≤m) ρ)
173 ≈ ⅀⟦ xs ⟧ (drop-1 si≤n (proj₂ (drop-1 sn≤m ρ)))
174 ⅀-⊐↑-hom xs si≤n ≤′-refl (_ ∷ _) = refl
175 ⅀-⊐↑-hom xs si≤n (≤′-step sn≤m) (_ ∷ ρ) = ⅀-⊐↑-hom xs si≤n sn≤m ρ
176
177 ⊐↑-hom : ∀ {n m}
178 → (x : Poly n)
179 → (sn≤m : suc n ≤′ m)
180 → ∀ ρ
181 → ⟦ x ⊐↑ sn≤m ⟧ ρ ≈ ⟦ x ⟧ (proj₂ (drop-1 sn≤m ρ))
182 ⊐↑-hom (Κ x ⊐ i≤sn) _ _ = refl
183 ⊐↑-hom (⅀ xs ⊐ i≤sn) = ⅀-⊐↑-hom xs i≤sn
184
185 trans-join-coeffs-hom : ∀ {i j-1 n}
186 → (i≤j-1 : suc i ≤′ j-1)
187 → (j≤n : suc j-1 ≤′ n)
188 → (xs : Coeff i +)
189 → ∀ ρ
190 → ⅀⟦ xs ⟧ (drop-1 i≤j-1 (proj₂ (drop-1 j≤n ρ))) ≈ ⅀⟦ xs ⟧ (drop-1 (≤′-step i≤j-1 ⟨ ≤′-trans ⟩ j≤n) ρ)
191 trans-join-coeffs-hom i<j-1 ≤′-refl xs (_ ∷ _) = refl
192 trans-join-coeffs-hom i<j-1 (≤′-step j<n) xs (_ ∷ ρ) = trans-join-coeffs-hom i<j-1 j<n xs ρ
193
194 trans-join-hom : ∀ {i j-1 n}
195 → (i≤j-1 : i ≤′ j-1)
196 → (j≤n : suc j-1 ≤′ n)
197 → (x : FlatPoly i)
198 → ∀ ρ
199 → ⟦ x ⊐ i≤j-1 ⟧ (proj₂ (drop-1 j≤n ρ)) ≈ ⟦ x ⊐ (≤′-step i≤j-1 ⟨ ≤′-trans ⟩ j≤n) ⟧ ρ
200 trans-join-hom i≤j-1 j≤n (Κ x) _ = refl
201 trans-join-hom i≤j-1 j≤n (⅀ x) = trans-join-coeffs-hom i≤j-1 j≤n x
202
203 ⊐↓-hom : ∀ {n m}
204 → (xs : Coeff n *)
205 → (sn≤m : suc n ≤′ m)
206 → ∀ ρ
207 → ⟦ xs ⊐↓ sn≤m ⟧ ρ ≈ ⅀?⟦ xs ⟧ (drop-1 sn≤m ρ)
208 ⊐↓-hom [] sn≤m _ = 0-homo
209 ⊐↓-hom (∹ x₁ Δ zero & ∹ xs) sn≤m _ = refl
210 ⊐↓-hom (∹ x Δ suc j & xs ) sn≤m _ = refl
211 ⊐↓-hom (∹ _≠0 x {x≠0} Δ zero & []) sn≤m ρs =
212 let (ρ , ρs′) = drop-1 sn≤m ρs
213 in
214 begin
215 ⟦ x ⊐↑ sn≤m ⟧ ρs
216 ≈⟨ ⊐↑-hom x sn≤m ρs ⟩
217 ⟦ x ⟧ ρs′
218 ∎
219
220 drop-1⇒lookup : ∀ {n} (i : Fin n) (ρs : Vec Carrier n) →
221 proj₁ (drop-1 (space≤′n i) ρs) ≡ Vec.lookup ρs i
222 drop-1⇒lookup zero (ρ ∷ ρs) = ≡.refl
223 drop-1⇒lookup (suc i) (ρ ∷ ρs) = drop-1⇒lookup i ρs
224
225 -- The fold: this function saves us hundreds of lines of proofs in the
226 -- rest of the homomorphism proof.
227 -- Many of the functions on polynomials are defined using para: this
228 -- function allows us to prove properties of those functions (in a
229 -- foldr-fusion style) *ignoring* optimisations we have made to the
230 -- polynomial structure.
231 poly-foldR : ∀ {n} ρ ρs
232 → ([f] : Fold n)
233 → (f : Carrier → Carrier)
234 → (∀ {x y} → x ≈ y → f x ≈ f y)
235 → (∀ x y → x * f y ≈ f (x * y))
236 → (∀ y {ys} zs → ⅀?⟦ ys ⟧ (ρ , ρs) ≈ f (⅀?⟦ zs ⟧ (ρ , ρs)) → [f] (y , ys) ⟦∷⟧? (ρ , ρs) ≈ f ((y , zs) ⟦∷⟧? (ρ , ρs)) )
237 → (f 0# ≈ 0#)
238 → ∀ xs
239 → ⅀?⟦ para [f] xs ⟧ (ρ , ρs) ≈ f (⅀⟦ xs ⟧ (ρ , ρs))
240 poly-foldR ρ ρs f e cng dist step base (x ≠0 Δ suc i & []) =
241 let y,ys = f (x , [])
242 y = proj₁ y,ys
243 ys = proj₂ y,ys
244 in
245 begin
246 ⅀?⟦ y Δ suc i ∷↓ ys ⟧ (ρ , ρs)
247 ≈⟨ ∷↓-hom-s y i ys ρ ρs ⟩
248 (ρ ^ suc i) * ((y , ys) ⟦∷⟧ (ρ , ρs))
249 ≈⟨ *≫ ⟦∷⟧?-hom y ys ρ ρs ⟨
250 (ρ ^ suc i) * ((y , ys) ⟦∷⟧? (ρ , ρs))
251 ≈⟨ *≫ step x [] (sym base) ⟩
252 (ρ ^ suc i) * e ((x , []) ⟦∷⟧? (ρ , ρs))
253 ≈⟨ *≫ cng (⟦∷⟧?-hom x [] ρ ρs) ⟩
254 (ρ ^ suc i) * e ((x , []) ⟦∷⟧ (ρ , ρs))
255 ≈⟨ dist _ _ ⟩
256 e ((ρ ^ suc i) * ((x , []) ⟦∷⟧ (ρ , ρs)))
257 ∎
258 poly-foldR ρ ρs f e cng dist step base (x ≠0 Δ suc i & ∹ xs) =
259 let ys = para f xs
260 y,zs = f (x , ys)
261 y = proj₁ y,zs
262 zs = proj₂ y,zs
263 in
264 begin
265 ⅀?⟦ y Δ suc i ∷↓ zs ⟧ (ρ , ρs)
266 ≈⟨ ∷↓-hom-s y i zs ρ ρs ⟩
267 (ρ ^ suc i) * ((y , zs) ⟦∷⟧ (ρ , ρs))
268 ≈⟨ *≫ ⟦∷⟧?-hom y zs ρ ρs ⟨
269 (ρ ^ suc i) * ((y , zs) ⟦∷⟧? (ρ , ρs))
270 ≈⟨ *≫ step x (∹ xs) (poly-foldR ρ ρs f e cng dist step base xs) ⟩
271 (ρ ^ suc i) * e ((x , (∹ xs )) ⟦∷⟧? (ρ , ρs))
272 ≈⟨ *≫ cng (⟦∷⟧?-hom x (∹ xs ) ρ ρs) ⟩
273 (ρ ^ suc i) * e ((x , (∹ xs )) ⟦∷⟧ (ρ , ρs))
274 ≈⟨ dist _ _ ⟩
275 e (ρ ^ suc i * ((x , (∹ xs )) ⟦∷⟧ (ρ , ρs)))
276 ∎
277 poly-foldR ρ ρs f e cng dist step base (x ≠0 Δ ℕ.zero & []) =
278 let y,zs = f (x , [])
279 y = proj₁ y,zs
280 zs = proj₂ y,zs
281 in
282 begin
283 ⅀?⟦ y Δ ℕ.zero ∷↓ zs ⟧ (ρ , ρs)
284 ≈⟨ ∷↓-hom-0 y zs ρ ρs ⟩
285 ((y , zs) ⟦∷⟧ (ρ , ρs))
286 ≈⟨ ⟦∷⟧?-hom y zs ρ ρs ⟨
287 ((y , zs) ⟦∷⟧? (ρ , ρs))
288 ≈⟨ step x [] (sym base) ⟩
289 e ((x , []) ⟦∷⟧? (ρ , ρs))
290 ≈⟨ cng (⟦∷⟧?-hom x [] ρ ρs) ⟩
291 e ((x , []) ⟦∷⟧ (ρ , ρs))
292 ∎
293 poly-foldR ρ ρs f e cng dist step base (x ≠0 Δ ℕ.zero & (∹ xs )) =
294 let ys = para f xs
295 y,zs = f (x , ys)
296 y = proj₁ y,zs
297 zs = proj₂ y,zs
298 in
299 begin
300 ⅀?⟦ y Δ ℕ.zero ∷↓ zs ⟧ (ρ , ρs)
301 ≈⟨ ∷↓-hom-0 y zs ρ ρs ⟩
302 ((y , zs) ⟦∷⟧ (ρ , ρs))
303 ≈⟨ ⟦∷⟧?-hom y zs ρ ρs ⟨
304 ((y , zs) ⟦∷⟧? (ρ , ρs))
305 ≈⟨ step x (∹ xs ) (poly-foldR ρ ρs f e cng dist step base xs) ⟩
306 e ((x , (∹ xs )) ⟦∷⟧ (ρ , ρs))
307 ≈⟨ cng (⟦∷⟧?-hom x (∹ xs ) ρ ρs) ⟩
308 e ((x , (∹ xs )) ⟦∷⟧ (ρ , ρs))
309 ∎
310
311 poly-mapR : ∀ {n} ρ ρs
312 → ([f] : Poly n → Poly n)
313 → (f : Carrier → Carrier)
314 → (∀ {x y} → x ≈ y → f x ≈ f y)
315 → (∀ x y → x * f y ≈ f (x * y))
316 → (∀ x y → f (x + y) ≈ f x + f y)
317 → (∀ y → ⟦ [f] y ⟧ ρs ≈ f (⟦ y ⟧ ρs) )
318 → (f 0# ≈ 0#)
319 → ∀ xs
320 → ⅀?⟦ poly-map [f] xs ⟧ (ρ , ρs) ≈ f (⅀⟦ xs ⟧ (ρ , ρs))
321 poly-mapR ρ ρs [f] f cng *-dist +-dist step′ base xs = poly-foldR ρ ρs (map₁ [f]) f cng *-dist step base xs
322 where
323 step : ∀ y {ys} zs → ⅀?⟦ ys ⟧ (ρ , ρs) ≈ f (⅀?⟦ zs ⟧ (ρ , ρs)) →(map₁ [f] (y , ys) ⟦∷⟧? (ρ , ρs)) ≈ f ((y , zs) ⟦∷⟧? (ρ , ρs))
324 step y {ys} zs ys≋zs =
325 begin
326 map₁ [f] (y , ys) ⟦∷⟧? (ρ , ρs)
327 ≡⟨⟩
328 ρ * ⅀?⟦ ys ⟧ (ρ , ρs) + ⟦ [f] y ⟧ ρs
329 ≈⟨ ((*≫ ys≋zs) ⟨ trans ⟩ *-dist ρ _) ⟨ +-cong ⟩ step′ y ⟩
330 f (ρ * ⅀?⟦ zs ⟧ (ρ , ρs)) + f (⟦ y ⟧ ρs)
331 ≈⟨ sym (+-dist _ _) ⟩
332 f ((y , zs) ⟦∷⟧? (ρ , ρs))
333 ∎