Debian Codegraph @

src/Algebra/Solver/Ring.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Old solver for commutative ring or semiring equalities
5 ------------------------------------------------------------------------
6
7 -- Uses ideas from the Coq ring tactic. See "Proving Equalities in a
8 -- Commutative Ring Done Right in Coq" by Grégoire and Mahboubi. The
9 -- code below is not optimised like theirs, though (in particular, our
10 -- Horner normal forms are not sparse).
11 --
12 -- At first the `WeaklyDecidable` type may at first glance look useless
13 -- as there is no guarantee that it doesn't always return `nothing`.
14 -- However the implementation of it affects the power of the solver. The
15 -- more equalities it returns, the more expressions the ring solver can
16 -- solve.
17
18 {-# OPTIONS --cubical-compatible --safe #-}
19
20 open import Algebra.Bundles
21 open import Algebra.Solver.Ring.AlmostCommutativeRing
22 open import Relation.Binary.Definitions using (WeaklyDecidable)
23
24 module Algebra.Solver.Ring
25 {r₁ r₂ r₃ r₄}
26 (Coeff : RawRing r₁ r₄) -- Coefficient "ring".
27 (R : AlmostCommutativeRing r₂ r₃) -- Main "ring".
28 (morphism : Coeff -Raw-AlmostCommutative⟶ R)
29 (_coeff≟_ : WeaklyDecidable (Induced-equivalence morphism))
30 where
31
32 open import Algebra.Core
33 open import Algebra.Solver.Ring.Lemmas Coeff R morphism
34 private module C = RawRing Coeff
35 open AlmostCommutativeRing R
36 renaming (zero to *-zero; zeroˡ to *-zeroˡ; zeroʳ to *-zeroʳ)
37 open import Algebra.Definitions _≈_
38 open import Algebra.Morphism
39 open _-Raw-AlmostCommutative⟶_ morphism renaming (⟦_⟧ to ⟦_⟧′)
40 open import Algebra.Properties.Semiring.Exp semiring
41
42 open import Relation.Nullary.Decidable using (yes; no)
43 open import Relation.Binary.Reasoning.Setoid setoid
44 import Relation.Binary.PropositionalEquality.Core as ≡
45 import Relation.Binary.Reflection as Reflection
46
47 open import Data.Nat.Base using (ℕ; suc; zero)
48 open import Data.Fin.Base using (Fin; zero; suc)
49 open import Data.Vec.Base using (Vec; []; _∷_; lookup)
50 open import Data.Maybe.Base using (just; nothing)
51 open import Function.Base using (_⟨_⟩_; _$_)
52 open import Level using (_⊔_)
53
54 infix 9 :-_ -H_ -N_
55 infixr 9 _:×_ _:^_ _^N_
56 infix 8 _*x+_ _*x+HN_ _*x+H_
57 infixl 8 _:*_ _*N_ _*H_ _*NH_ _*HN_
58 infixl 7 _:+_ _:-_ _+H_ _+N_
59 infix 4 _≈H_ _≈N_
60
61 private
62 variable
63 n : ℕ
64
65
66 ------------------------------------------------------------------------
67 -- Polynomials
68
69 data Op : Set where
70 [+] : Op
71 [*] : Op
72
73 -- The polynomials are indexed by the number of variables.
74
75 data Polynomial (m : ℕ) : Set r₁ where
76 op : (o : Op) (p₁ : Polynomial m) (p₂ : Polynomial m) → Polynomial m
77 con : (c : C.Carrier) → Polynomial m
78 var : (x : Fin m) → Polynomial m
79 _:^_ : (p : Polynomial m) (n : ℕ) → Polynomial m
80 :-_ : (p : Polynomial m) → Polynomial m
81
82 -- Short-hand notation.
83
84 _:+_ : Polynomial n → Polynomial n → Polynomial n
85 _:+_ = op [+]
86
87 _:*_ : Polynomial n → Polynomial n → Polynomial n
88 _:*_ = op [*]
89
90 _:-_ : Polynomial n → Polynomial n → Polynomial n
91 x :- y = x :+ :- y
92
93 _:×_ : ℕ → Polynomial n → Polynomial n
94 zero :× p = con C.0#
95 suc m :× p = p :+ m :× p
96
97 -- Semantics.
98
99 sem : Op → Op₂ Carrier
100 sem [+] = _+_
101 sem [*] = _*_
102
103 -- An environment contains one value for every variable.
104
105 Env : ℕ → Set _
106 Env = Vec Carrier
107
108 ⟦_⟧ : Polynomial n → Env n → Carrier
109 ⟦ op o p₁ p₂ ⟧ ρ = ⟦ p₁ ⟧ ρ ⟨ sem o ⟩ ⟦ p₂ ⟧ ρ
110 ⟦ con c ⟧ ρ = ⟦ c ⟧′
111 ⟦ var x ⟧ ρ = lookup ρ x
112 ⟦ p :^ n ⟧ ρ = ⟦ p ⟧ ρ ^ n
113 ⟦ :- p ⟧ ρ = - ⟦ p ⟧ ρ
114
115 ------------------------------------------------------------------------
116 -- Normal forms of polynomials
117
118 -- A univariate polynomial of degree d,
119 --
120 -- p = a_d x^d + a_{d-1}x^{d-1} + … + a_0,
121 --
122 -- is represented in Horner normal form by
123 --
124 -- p = ((a_d x + a_{d-1})x + …)x + a_0.
125 --
126 -- Note that Horner normal forms can be represented as lists, with the
127 -- empty list standing for the zero polynomial of degree "-1".
128 --
129 -- Given this representation of univariate polynomials over an
130 -- arbitrary ring, polynomials in any number of variables over the
131 -- ring C can be represented via the isomorphisms
132 --
133 -- C[] ≅ C
134 --
135 -- and
136 --
137 -- C[X_0,...X_{n+1}] ≅ C[X_0,...,X_n][X_{n+1}].
138
139 mutual
140
141 -- The polynomial representations are indexed by the polynomial's
142 -- degree.
143
144 data HNF : ℕ → Set r₁ where
145 ∅ : HNF (suc n)
146 _*x+_ : HNF (suc n) → Normal n → HNF (suc n)
147
148 data Normal : ℕ → Set r₁ where
149 con : C.Carrier → Normal zero
150 poly : HNF (suc n) → Normal (suc n)
151
152 -- Note that the data types above do /not/ ensure uniqueness of
153 -- normal forms: the zero polynomial of degree one can be
154 -- represented using both ∅ and ∅ *x+ con C.0#.
155
156 mutual
157
158 -- Semantics.
159
160 ⟦_⟧H : HNF (suc n) → Env (suc n) → Carrier
161 ⟦ ∅ ⟧H _ = 0#
162 ⟦ p *x+ c ⟧H (x ∷ ρ) = ⟦ p ⟧H (x ∷ ρ) * x + ⟦ c ⟧N ρ
163
164 ⟦_⟧N : Normal n → Env n → Carrier
165 ⟦ con c ⟧N _ = ⟦ c ⟧′
166 ⟦ poly p ⟧N ρ = ⟦ p ⟧H ρ
167
168 ------------------------------------------------------------------------
169 -- Equality and decidability
170
171 mutual
172
173 -- Equality.
174
175 data _≈H_ : HNF n → HNF n → Set (r₁ ⊔ r₃) where
176 ∅ : _≈H_ {suc n} ∅ ∅
177 _*x+_ : ∀ {n} {p₁ p₂ : HNF (suc n)} {c₁ c₂ : Normal n} →
178 p₁ ≈H p₂ → c₁ ≈N c₂ → (p₁ *x+ c₁) ≈H (p₂ *x+ c₂)
179
180 data _≈N_ : Normal n → Normal n → Set (r₁ ⊔ r₃) where
181 con : ∀ {c₁ c₂} → ⟦ c₁ ⟧′ ≈ ⟦ c₂ ⟧′ → con c₁ ≈N con c₂
182 poly : ∀ {n} {p₁ p₂ : HNF (suc n)} → p₁ ≈H p₂ → poly p₁ ≈N poly p₂
183
184 mutual
185
186 -- Equality is weakly decidable.
187
188 infix 4 _≟H_ _≟N_
189
190 _≟H_ : WeaklyDecidable (_≈H_ {n = n})
191 ∅ ≟H ∅ = just ∅
192 ∅ ≟H (_ *x+ _) = nothing
193 (_ *x+ _) ≟H ∅ = nothing
194 (p₁ *x+ c₁) ≟H (p₂ *x+ c₂) with p₁ ≟H p₂ | c₁ ≟N c₂
195 ... | just p₁≈p₂ | just c₁≈c₂ = just (p₁≈p₂ *x+ c₁≈c₂)
196 ... | _ | nothing = nothing
197 ... | nothing | _ = nothing
198
199 _≟N_ : WeaklyDecidable (_≈N_ {n = n})
200 con c₁ ≟N con c₂ with c₁ coeff≟ c₂
201 ... | just c₁≈c₂ = just (con c₁≈c₂)
202 ... | nothing = nothing
203 poly p₁ ≟N poly p₂ with p₁ ≟H p₂
204 ... | just p₁≈p₂ = just (poly p₁≈p₂)
205 ... | nothing = nothing
206
207 mutual
208
209 -- The semantics respect the equality relations defined above.
210
211 ⟦_⟧H-cong : {p₁ p₂ : HNF (suc n)} →
212 p₁ ≈H p₂ → ∀ ρ → ⟦ p₁ ⟧H ρ ≈ ⟦ p₂ ⟧H ρ
213 ⟦ ∅ ⟧H-cong _ = refl
214 ⟦ p₁≈p₂ *x+ c₁≈c₂ ⟧H-cong (x ∷ ρ) =
215 (⟦ p₁≈p₂ ⟧H-cong (x ∷ ρ) ⟨ *-cong ⟩ refl)
216 ⟨ +-cong ⟩
217 ⟦ c₁≈c₂ ⟧N-cong ρ
218
219 ⟦_⟧N-cong : {p₁ p₂ : Normal n} →
220 p₁ ≈N p₂ → ∀ ρ → ⟦ p₁ ⟧N ρ ≈ ⟦ p₂ ⟧N ρ
221 ⟦ con c₁≈c₂ ⟧N-cong _ = c₁≈c₂
222 ⟦ poly p₁≈p₂ ⟧N-cong ρ = ⟦ p₁≈p₂ ⟧H-cong ρ
223
224 ------------------------------------------------------------------------
225 -- Ring operations on Horner normal forms
226
227 -- Zero.
228
229 0H : HNF (suc n)
230 0H = ∅
231
232 0N : Normal n
233 0N {zero} = con C.0#
234 0N {suc n} = poly 0H
235
236 mutual
237
238 -- One.
239
240 1H : HNF (suc n)
241 1H {n} = ∅ *x+ 1N {n}
242
243 1N : Normal n
244 1N {zero} = con C.1#
245 1N {suc n} = poly 1H
246
247 -- A simplifying variant of _*x+_.
248
249 _*x+HN_ : HNF (suc n) → Normal n → HNF (suc n)
250 (p *x+ c′) *x+HN c = (p *x+ c′) *x+ c
251 ∅ *x+HN c with c ≟N 0N
252 ... | just c≈0 = ∅
253 ... | nothing = ∅ *x+ c
254
255 mutual
256
257 -- Addition.
258
259 _+H_ : HNF (suc n) → HNF (suc n) → HNF (suc n)
260 ∅ +H p = p
261 p +H ∅ = p
262 (p₁ *x+ c₁) +H (p₂ *x+ c₂) = (p₁ +H p₂) *x+HN (c₁ +N c₂)
263
264 _+N_ : Normal n → Normal n → Normal n
265 con c₁ +N con c₂ = con (c₁ C.+ c₂)
266 poly p₁ +N poly p₂ = poly (p₁ +H p₂)
267
268 -- Multiplication.
269
270 _*x+H_ : HNF (suc n) → HNF (suc n) → HNF (suc n)
271 p₁ *x+H (p₂ *x+ c) = (p₁ +H p₂) *x+HN c
272 ∅ *x+H ∅ = ∅
273 (p₁ *x+ c) *x+H ∅ = (p₁ *x+ c) *x+ 0N
274
275 mutual
276
277 _*NH_ : Normal n → HNF (suc n) → HNF (suc n)
278 c *NH ∅ = ∅
279 c *NH (p *x+ c′) with c ≟N 0N
280 ... | just c≈0 = ∅
281 ... | nothing = (c *NH p) *x+ (c *N c′)
282
283 _*HN_ : HNF (suc n) → Normal n → HNF (suc n)
284 ∅ *HN c = ∅
285 (p *x+ c′) *HN c with c ≟N 0N
286 ... | just c≈0 = ∅
287 ... | nothing = (p *HN c) *x+ (c′ *N c)
288
289 _*H_ : HNF (suc n) → HNF (suc n) → HNF (suc n)
290 ∅ *H _ = ∅
291 (_ *x+ _) *H ∅ = ∅
292 (p₁ *x+ c₁) *H (p₂ *x+ c₂) =
293 ((p₁ *H p₂) *x+H (p₁ *HN c₂ +H c₁ *NH p₂)) *x+HN (c₁ *N c₂)
294
295 _*N_ : Normal n → Normal n → Normal n
296 con c₁ *N con c₂ = con (c₁ C.* c₂)
297 poly p₁ *N poly p₂ = poly (p₁ *H p₂)
298
299 -- Exponentiation.
300
301 _^N_ : Normal n → ℕ → Normal n
302 p ^N zero = 1N
303 p ^N suc n = p *N (p ^N n)
304
305 mutual
306
307 -- Negation.
308
309 -H_ : HNF (suc n) → HNF (suc n)
310 -H p = (-N 1N) *NH p
311
312 -N_ : Normal n → Normal n
313 -N con c = con (C.- c)
314 -N poly p = poly (-H p)
315
316 ------------------------------------------------------------------------
317 -- Normalisation
318
319 normalise-con : C.Carrier → Normal n
320 normalise-con {zero} c = con c
321 normalise-con {suc n} c = poly (∅ *x+HN normalise-con c)
322
323 normalise-var : Fin n → Normal n
324 normalise-var zero = poly ((∅ *x+ 1N) *x+ 0N)
325 normalise-var (suc i) = poly (∅ *x+HN normalise-var i)
326
327 normalise : Polynomial n → Normal n
328 normalise (op [+] t₁ t₂) = normalise t₁ +N normalise t₂
329 normalise (op [*] t₁ t₂) = normalise t₁ *N normalise t₂
330 normalise (con c) = normalise-con c
331 normalise (var i) = normalise-var i
332 normalise (t :^ k) = normalise t ^N k
333 normalise (:- t) = -N normalise t
334
335 -- Evaluation after normalisation.
336
337 ⟦_⟧↓ : Polynomial n → Env n → Carrier
338 ⟦ p ⟧↓ ρ = ⟦ normalise p ⟧N ρ
339
340 ------------------------------------------------------------------------
341 -- Homomorphism lemmas
342
343 0N-homo : ∀ {n} ρ → ⟦ 0N {n} ⟧N ρ ≈ 0#
344 0N-homo [] = 0-homo
345 0N-homo (x ∷ ρ) = refl
346
347 -- If c is equal to 0N, then c is semantically equal to 0#.
348
349 0≈⟦0⟧ : ∀ {n} {c : Normal n} → c ≈N 0N → ∀ ρ → 0# ≈ ⟦ c ⟧N ρ
350 0≈⟦0⟧ {c = c} c≈0 ρ = sym (begin
351 ⟦ c ⟧N ρ ≈⟨ ⟦ c≈0 ⟧N-cong ρ ⟩
352 ⟦ 0N ⟧N ρ ≈⟨ 0N-homo ρ ⟩
353 0# ∎)
354
355 1N-homo : ∀ {n} ρ → ⟦ 1N {n} ⟧N ρ ≈ 1#
356 1N-homo [] = 1-homo
357 1N-homo (x ∷ ρ) = begin
358 0# * x + ⟦ 1N ⟧N ρ ≈⟨ refl ⟨ +-cong ⟩ 1N-homo ρ ⟩
359 0# * x + 1# ≈⟨ lemma₆ _ _ ⟩
360 1# ∎
361
362 -- _*x+HN_ is equal to _*x+_.
363
364 *x+HN≈*x+ : ∀ {n} (p : HNF (suc n)) (c : Normal n) →
365 ∀ ρ → ⟦ p *x+HN c ⟧H ρ ≈ ⟦ p *x+ c ⟧H ρ
366 *x+HN≈*x+ (p *x+ c′) c ρ = refl
367 *x+HN≈*x+ ∅ c (x ∷ ρ) with c ≟N 0N
368 ... | just c≈0 = begin
369 0# ≈⟨ 0≈⟦0⟧ c≈0 ρ ⟩
370 ⟦ c ⟧N ρ ≈⟨ sym $ lemma₆ _ _ ⟩
371 0# * x + ⟦ c ⟧N ρ ∎
372 ... | nothing = refl
373
374 ∅*x+HN-homo : ∀ {n} (c : Normal n) x ρ →
375 ⟦ ∅ *x+HN c ⟧H (x ∷ ρ) ≈ ⟦ c ⟧N ρ
376 ∅*x+HN-homo c x ρ with c ≟N 0N
377 ... | just c≈0 = 0≈⟦0⟧ c≈0 ρ
378 ... | nothing = lemma₆ _ _
379
380 mutual
381
382 +H-homo : ∀ {n} (p₁ p₂ : HNF (suc n)) →
383 ∀ ρ → ⟦ p₁ +H p₂ ⟧H ρ ≈ ⟦ p₁ ⟧H ρ + ⟦ p₂ ⟧H ρ
384 +H-homo ∅ p₂ ρ = sym (+-identityˡ _)
385 +H-homo (p₁ *x+ x₁) ∅ ρ = sym (+-identityʳ _)
386 +H-homo (p₁ *x+ c₁) (p₂ *x+ c₂) (x ∷ ρ) = begin
387 ⟦ (p₁ +H p₂) *x+HN (c₁ +N c₂) ⟧H (x ∷ ρ) ≈⟨ *x+HN≈*x+ (p₁ +H p₂) (c₁ +N c₂) (x ∷ ρ) ⟩
388
389 ⟦ p₁ +H p₂ ⟧H (x ∷ ρ) * x + ⟦ c₁ +N c₂ ⟧N ρ ≈⟨ (+H-homo p₁ p₂ (x ∷ ρ) ⟨ *-cong ⟩ refl) ⟨ +-cong ⟩ +N-homo c₁ c₂ ρ ⟩
390
391 (⟦ p₁ ⟧H (x ∷ ρ) + ⟦ p₂ ⟧H (x ∷ ρ)) * x + (⟦ c₁ ⟧N ρ + ⟦ c₂ ⟧N ρ) ≈⟨ lemma₁ _ _ _ _ _ ⟩
392
393 (⟦ p₁ ⟧H (x ∷ ρ) * x + ⟦ c₁ ⟧N ρ) +
394 (⟦ p₂ ⟧H (x ∷ ρ) * x + ⟦ c₂ ⟧N ρ) ∎
395
396 +N-homo : ∀ {n} (p₁ p₂ : Normal n) →
397 ∀ ρ → ⟦ p₁ +N p₂ ⟧N ρ ≈ ⟦ p₁ ⟧N ρ + ⟦ p₂ ⟧N ρ
398 +N-homo (con c₁) (con c₂) _ = +-homo _ _
399 +N-homo (poly p₁) (poly p₂) ρ = +H-homo p₁ p₂ ρ
400
401 *x+H-homo :
402 ∀ {n} (p₁ p₂ : HNF (suc n)) x ρ →
403 ⟦ p₁ *x+H p₂ ⟧H (x ∷ ρ) ≈
404 ⟦ p₁ ⟧H (x ∷ ρ) * x + ⟦ p₂ ⟧H (x ∷ ρ)
405 *x+H-homo ∅ ∅ _ _ = sym $ lemma₆ _ _
406 *x+H-homo (p *x+ c) ∅ x ρ = begin
407 ⟦ p *x+ c ⟧H (x ∷ ρ) * x + ⟦ 0N ⟧N ρ ≈⟨ refl ⟨ +-cong ⟩ 0N-homo ρ ⟩
408 ⟦ p *x+ c ⟧H (x ∷ ρ) * x + 0# ∎
409 *x+H-homo p₁ (p₂ *x+ c₂) x ρ = begin
410 ⟦ (p₁ +H p₂) *x+HN c₂ ⟧H (x ∷ ρ) ≈⟨ *x+HN≈*x+ (p₁ +H p₂) c₂ (x ∷ ρ) ⟩
411 ⟦ p₁ +H p₂ ⟧H (x ∷ ρ) * x + ⟦ c₂ ⟧N ρ ≈⟨ (+H-homo p₁ p₂ (x ∷ ρ) ⟨ *-cong ⟩ refl) ⟨ +-cong ⟩ refl ⟩
412 (⟦ p₁ ⟧H (x ∷ ρ) + ⟦ p₂ ⟧H (x ∷ ρ)) * x + ⟦ c₂ ⟧N ρ ≈⟨ lemma₀ _ _ _ _ ⟩
413 ⟦ p₁ ⟧H (x ∷ ρ) * x + (⟦ p₂ ⟧H (x ∷ ρ) * x + ⟦ c₂ ⟧N ρ) ∎
414
415 mutual
416
417 *NH-homo :
418 ∀ {n} (c : Normal n) (p : HNF (suc n)) x ρ →
419 ⟦ c *NH p ⟧H (x ∷ ρ) ≈ ⟦ c ⟧N ρ * ⟦ p ⟧H (x ∷ ρ)
420 *NH-homo c ∅ x ρ = sym (*-zeroʳ _)
421 *NH-homo c (p *x+ c′) x ρ with c ≟N 0N
422 ... | just c≈0 = begin
423 0# ≈⟨ sym (*-zeroˡ _) ⟩
424 0# * (⟦ p ⟧H (x ∷ ρ) * x + ⟦ c′ ⟧N ρ) ≈⟨ 0≈⟦0⟧ c≈0 ρ ⟨ *-cong ⟩ refl ⟩
425 ⟦ c ⟧N ρ * (⟦ p ⟧H (x ∷ ρ) * x + ⟦ c′ ⟧N ρ) ∎
426 ... | nothing = begin
427 ⟦ c *NH p ⟧H (x ∷ ρ) * x + ⟦ c *N c′ ⟧N ρ ≈⟨ (*NH-homo c p x ρ ⟨ *-cong ⟩ refl) ⟨ +-cong ⟩ *N-homo c c′ ρ ⟩
428 (⟦ c ⟧N ρ * ⟦ p ⟧H (x ∷ ρ)) * x + (⟦ c ⟧N ρ * ⟦ c′ ⟧N ρ) ≈⟨ lemma₃ _ _ _ _ ⟩
429 ⟦ c ⟧N ρ * (⟦ p ⟧H (x ∷ ρ) * x + ⟦ c′ ⟧N ρ) ∎
430
431 *HN-homo :
432 ∀ {n} (p : HNF (suc n)) (c : Normal n) x ρ →
433 ⟦ p *HN c ⟧H (x ∷ ρ) ≈ ⟦ p ⟧H (x ∷ ρ) * ⟦ c ⟧N ρ
434 *HN-homo ∅ c x ρ = sym (*-zeroˡ _)
435 *HN-homo (p *x+ c′) c x ρ with c ≟N 0N
436 ... | just c≈0 = begin
437 0# ≈⟨ sym (*-zeroʳ _) ⟩
438 (⟦ p ⟧H (x ∷ ρ) * x + ⟦ c′ ⟧N ρ) * 0# ≈⟨ refl ⟨ *-cong ⟩ 0≈⟦0⟧ c≈0 ρ ⟩
439 (⟦ p ⟧H (x ∷ ρ) * x + ⟦ c′ ⟧N ρ) * ⟦ c ⟧N ρ ∎
440 ... | nothing = begin
441 ⟦ p *HN c ⟧H (x ∷ ρ) * x + ⟦ c′ *N c ⟧N ρ ≈⟨ (*HN-homo p c x ρ ⟨ *-cong ⟩ refl) ⟨ +-cong ⟩ *N-homo c′ c ρ ⟩
442 (⟦ p ⟧H (x ∷ ρ) * ⟦ c ⟧N ρ) * x + (⟦ c′ ⟧N ρ * ⟦ c ⟧N ρ) ≈⟨ lemma₂ _ _ _ _ ⟩
443 (⟦ p ⟧H (x ∷ ρ) * x + ⟦ c′ ⟧N ρ) * ⟦ c ⟧N ρ ∎
444
445 *H-homo : ∀ {n} (p₁ p₂ : HNF (suc n)) →
446 ∀ ρ → ⟦ p₁ *H p₂ ⟧H ρ ≈ ⟦ p₁ ⟧H ρ * ⟦ p₂ ⟧H ρ
447 *H-homo ∅ p₂ ρ = sym $ *-zeroˡ _
448 *H-homo (p₁ *x+ c₁) ∅ ρ = sym $ *-zeroʳ _
449 *H-homo (p₁ *x+ c₁) (p₂ *x+ c₂) (x ∷ ρ) = begin
450 ⟦ ((p₁ *H p₂) *x+H ((p₁ *HN c₂) +H (c₁ *NH p₂))) *x+HN
451 (c₁ *N c₂) ⟧H (x ∷ ρ) ≈⟨ *x+HN≈*x+ ((p₁ *H p₂) *x+H ((p₁ *HN c₂) +H (c₁ *NH p₂)))
452 (c₁ *N c₂) (x ∷ ρ) ⟩
453 ⟦ (p₁ *H p₂) *x+H
454 ((p₁ *HN c₂) +H (c₁ *NH p₂)) ⟧H (x ∷ ρ) * x +
455 ⟦ c₁ *N c₂ ⟧N ρ ≈⟨ (*x+H-homo (p₁ *H p₂) ((p₁ *HN c₂) +H (c₁ *NH p₂)) x ρ
456 ⟨ *-cong ⟩
457 refl)
458 ⟨ +-cong ⟩
459 *N-homo c₁ c₂ ρ ⟩
460 (⟦ p₁ *H p₂ ⟧H (x ∷ ρ) * x +
461 ⟦ (p₁ *HN c₂) +H (c₁ *NH p₂) ⟧H (x ∷ ρ)) * x +
462 ⟦ c₁ ⟧N ρ * ⟦ c₂ ⟧N ρ ≈⟨ (((*H-homo p₁ p₂ (x ∷ ρ) ⟨ *-cong ⟩ refl)
463 ⟨ +-cong ⟩
464 (+H-homo (p₁ *HN c₂) (c₁ *NH p₂) (x ∷ ρ)))
465 ⟨ *-cong ⟩
466 refl)
467 ⟨ +-cong ⟩
468 refl ⟩
469 (⟦ p₁ ⟧H (x ∷ ρ) * ⟦ p₂ ⟧H (x ∷ ρ) * x +
470 (⟦ p₁ *HN c₂ ⟧H (x ∷ ρ) + ⟦ c₁ *NH p₂ ⟧H (x ∷ ρ))) * x +
471 ⟦ c₁ ⟧N ρ * ⟦ c₂ ⟧N ρ ≈⟨ ((refl ⟨ +-cong ⟩ (*HN-homo p₁ c₂ x ρ ⟨ +-cong ⟩ *NH-homo c₁ p₂ x ρ))
472 ⟨ *-cong ⟩
473 refl)
474 ⟨ +-cong ⟩
475 refl ⟩
476 (⟦ p₁ ⟧H (x ∷ ρ) * ⟦ p₂ ⟧H (x ∷ ρ) * x +
477 (⟦ p₁ ⟧H (x ∷ ρ) * ⟦ c₂ ⟧N ρ + ⟦ c₁ ⟧N ρ * ⟦ p₂ ⟧H (x ∷ ρ))) * x +
478 (⟦ c₁ ⟧N ρ * ⟦ c₂ ⟧N ρ) ≈⟨ lemma₄ _ _ _ _ _ ⟩
479
480 (⟦ p₁ ⟧H (x ∷ ρ) * x + ⟦ c₁ ⟧N ρ) *
481 (⟦ p₂ ⟧H (x ∷ ρ) * x + ⟦ c₂ ⟧N ρ) ∎
482
483 *N-homo : ∀ {n} (p₁ p₂ : Normal n) →
484 ∀ ρ → ⟦ p₁ *N p₂ ⟧N ρ ≈ ⟦ p₁ ⟧N ρ * ⟦ p₂ ⟧N ρ
485 *N-homo (con c₁) (con c₂) _ = *-homo _ _
486 *N-homo (poly p₁) (poly p₂) ρ = *H-homo p₁ p₂ ρ
487
488 ^N-homo : ∀ {n} (p : Normal n) (k : ℕ) →
489 ∀ ρ → ⟦ p ^N k ⟧N ρ ≈ ⟦ p ⟧N ρ ^ k
490 ^N-homo p zero ρ = 1N-homo ρ
491 ^N-homo p (suc k) ρ = begin
492 ⟦ p *N (p ^N k) ⟧N ρ ≈⟨ *N-homo p (p ^N k) ρ ⟩
493 ⟦ p ⟧N ρ * ⟦ p ^N k ⟧N ρ ≈⟨ refl ⟨ *-cong ⟩ ^N-homo p k ρ ⟩
494 ⟦ p ⟧N ρ * (⟦ p ⟧N ρ ^ k) ∎
495
496 mutual
497
498 -H‿-homo : ∀ {n} (p : HNF (suc n)) →
499 ∀ ρ → ⟦ -H p ⟧H ρ ≈ - ⟦ p ⟧H ρ
500 -H‿-homo p (x ∷ ρ) = begin
501 ⟦ (-N 1N) *NH p ⟧H (x ∷ ρ) ≈⟨ *NH-homo (-N 1N) p x ρ ⟩
502 ⟦ -N 1N ⟧N ρ * ⟦ p ⟧H (x ∷ ρ) ≈⟨ trans (-N‿-homo 1N ρ) (-‿cong (1N-homo ρ)) ⟨ *-cong ⟩ refl ⟩
503 - 1# * ⟦ p ⟧H (x ∷ ρ) ≈⟨ lemma₇ _ ⟩
504 - ⟦ p ⟧H (x ∷ ρ) ∎
505
506 -N‿-homo : ∀ {n} (p : Normal n) →
507 ∀ ρ → ⟦ -N p ⟧N ρ ≈ - ⟦ p ⟧N ρ
508 -N‿-homo (con c) _ = -‿homo _
509 -N‿-homo (poly p) ρ = -H‿-homo p ρ
510
511 ------------------------------------------------------------------------
512 -- Correctness
513
514 correct-con : ∀ {n} (c : C.Carrier) (ρ : Env n) →
515 ⟦ normalise-con c ⟧N ρ ≈ ⟦ c ⟧′
516 correct-con c [] = refl
517 correct-con c (x ∷ ρ) = begin
518 ⟦ ∅ *x+HN normalise-con c ⟧H (x ∷ ρ) ≈⟨ ∅*x+HN-homo (normalise-con c) x ρ ⟩
519 ⟦ normalise-con c ⟧N ρ ≈⟨ correct-con c ρ ⟩
520 ⟦ c ⟧′ ∎
521
522 correct-var : ∀ {n} (i : Fin n) →
523 ∀ ρ → ⟦ normalise-var i ⟧N ρ ≈ lookup ρ i
524 correct-var (suc i) (x ∷ ρ) = begin
525 ⟦ ∅ *x+HN normalise-var i ⟧H (x ∷ ρ) ≈⟨ ∅*x+HN-homo (normalise-var i) x ρ ⟩
526 ⟦ normalise-var i ⟧N ρ ≈⟨ correct-var i ρ ⟩
527 lookup ρ i ∎
528 correct-var zero (x ∷ ρ) = begin
529 (0# * x + ⟦ 1N ⟧N ρ) * x + ⟦ 0N ⟧N ρ ≈⟨ ((refl ⟨ +-cong ⟩ 1N-homo ρ) ⟨ *-cong ⟩ refl) ⟨ +-cong ⟩ 0N-homo ρ ⟩
530 (0# * x + 1#) * x + 0# ≈⟨ lemma₅ _ ⟩
531 x ∎
532
533 correct : ∀ {n} (p : Polynomial n) → ∀ ρ → ⟦ p ⟧↓ ρ ≈ ⟦ p ⟧ ρ
534 correct (op [+] p₁ p₂) ρ = begin
535 ⟦ normalise p₁ +N normalise p₂ ⟧N ρ ≈⟨ +N-homo (normalise p₁) (normalise p₂) ρ ⟩
536 ⟦ p₁ ⟧↓ ρ + ⟦ p₂ ⟧↓ ρ ≈⟨ correct p₁ ρ ⟨ +-cong ⟩ correct p₂ ρ ⟩
537 ⟦ p₁ ⟧ ρ + ⟦ p₂ ⟧ ρ ∎
538 correct (op [*] p₁ p₂) ρ = begin
539 ⟦ normalise p₁ *N normalise p₂ ⟧N ρ ≈⟨ *N-homo (normalise p₁) (normalise p₂) ρ ⟩
540 ⟦ p₁ ⟧↓ ρ * ⟦ p₂ ⟧↓ ρ ≈⟨ correct p₁ ρ ⟨ *-cong ⟩ correct p₂ ρ ⟩
541 ⟦ p₁ ⟧ ρ * ⟦ p₂ ⟧ ρ ∎
542 correct (con c) ρ = correct-con c ρ
543 correct (var i) ρ = correct-var i ρ
544 correct (p :^ k) ρ = begin
545 ⟦ normalise p ^N k ⟧N ρ ≈⟨ ^N-homo (normalise p) k ρ ⟩
546 ⟦ p ⟧↓ ρ ^ k ≈⟨ correct p ρ ⟨ ^-cong ⟩ ≡.refl {x = k} ⟩
547 ⟦ p ⟧ ρ ^ k ∎
548 correct (:- p) ρ = begin
549 ⟦ -N normalise p ⟧N ρ ≈⟨ -N‿-homo (normalise p) ρ ⟩
550 - ⟦ p ⟧↓ ρ ≈⟨ -‿cong (correct p ρ) ⟩
551 - ⟦ p ⟧ ρ ∎
552
553 ------------------------------------------------------------------------
554 -- "Tactic.
555
556 open Reflection setoid var ⟦_⟧ ⟦_⟧↓ correct public
557 using (prove; solve) renaming (_⊜_ to _:=_)
558
559 -- For examples of how solve and _:=_ can be used to
560 -- semi-automatically prove ring equalities, see, for instance,
561 -- Data.Digit or Data.Nat.DivMod.