Debian Codegraph @

src/Tactic/RingSolver/Core/Polynomial/Base.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Sparse polynomials in a commutative ring, encoded in Horner normal
5 -- form.
6 --
7 -- Horner normal form encodes a polynomial as a list of coefficients.
8 -- As an example take the polynomial:
9 --
10 -- 3 + 2x² + 4x⁵ + 2x⁷
11 --
12 -- Then expand it out, filling in the missing coefficients:
13 --
14 -- 3x⁰ + 0x¹ + 2x² + 0x³ + 0x⁴ + 4x⁵ + 0x⁶ + 2x⁷
15 --
16 -- And then encode that as a list:
17 --
18 -- [3, 0, 2, 0, 0, 4, 0, 2]
19 --
20 -- The representation we use here is optimised from the above. First,
21 -- we remove the zero terms, and add a "gap" index next to every
22 -- coefficient:
23 --
24 -- [(3,0),(2,1),(4,2),(2,1)]
25 --
26 -- Which can be thought of as a representation of the expression:
27 --
28 -- x⁰ * (3 + x * x¹ * (2 + x * x² * (4 + x * x¹ * (2 + x * 0))))
29 --
30 -- This is "sparse" Horner normal form.
31 --
32 -- The second optimisation deals with representing multiple variables
33 -- in a polynomial. The standard trick is to encode a polynomial in n
34 -- variables as a polynomial with coefficients in n-1 variables,
35 -- recursing until you hit 0 which is simply the type of the coefficient
36 -- itself.
37 --
38 -- We again encode "gaps" here, with the injection index. Since the
39 -- number of variables in a polynomial is contained in its type,
40 -- however, operations on this gap are type-relevant, so it's not
41 -- convenient to simply use ℕ. We use _≤′_ instead.
42 ------------------------------------------------------------------------
43
44 {-# OPTIONS --safe --cubical-compatible #-}
45
46 open import Tactic.RingSolver.Core.Polynomial.Parameters
47
48 module Tactic.RingSolver.Core.Polynomial.Base
49 {ℓ₁ ℓ₂} (coeffs : RawCoeff ℓ₁ ℓ₂) where
50
51 open RawCoeff coeffs
52
53 open import Data.Bool.Base using (Bool; true; false; T)
54 open import Data.Empty using (⊥)
55 open import Data.Fin.Base as Fin using (Fin; zero; suc)
56 open import Data.List.Kleene
57 open import Data.Nat.Base as ℕ using (ℕ; suc; zero; _≤′_; compare; ≤′-refl; ≤′-step; _<′_)
58 open import Data.Nat.Properties using (z≤′n; ≤′-trans)
59 open import Data.Nat.Induction
60 open import Data.Product.Base using (_×_; _,_; map₁; curry; uncurry)
61 open import Data.Unit.Base using (⊤; tt)
62 open import Function.Base
63 open import Relation.Nullary using (¬_; Dec; yes; no)
64
65 open import Algebra.Definitions.RawSemiring rawSemiring
66 using (_^′_)
67
68 ------------------------------------------------------------------------
69 -- Injection indices.
70 ------------------------------------------------------------------------
71
72 -- First, we define comparisons on _≤′_.
73 -- The following is analagous to Ordering and compare from
74 -- Data.Nat.Base.
75 data InjectionOrdering {n : ℕ} : ∀ {i j} (i≤n : i ≤′ n) (j≤n : j ≤′ n) → Set
76 where
77 inj-lt : ∀ {i j-1} (i≤j-1 : i ≤′ j-1) (j≤n : suc j-1 ≤′ n) →
78 InjectionOrdering (≤′-step i≤j-1 ⟨ ≤′-trans ⟩ j≤n) j≤n
79
80 inj-gt : ∀ {i-1 j} (i≤n : suc i-1 ≤′ n) (j≤i-1 : j ≤′ i-1) →
81 InjectionOrdering i≤n (≤′-step j≤i-1 ⟨ ≤′-trans ⟩ i≤n)
82
83 inj-eq : ∀ {i} (i≤n : i ≤′ n) →
84 InjectionOrdering i≤n i≤n
85
86 inj-compare : ∀ {i j n} (x : i ≤′ n) (y : j ≤′ n) → InjectionOrdering x y
87 inj-compare ≤′-refl ≤′-refl = inj-eq ≤′-refl
88 inj-compare ≤′-refl (≤′-step y) = inj-gt ≤′-refl y
89 inj-compare (≤′-step x) ≤′-refl = inj-lt x ≤′-refl
90 inj-compare (≤′-step x) (≤′-step y) = case inj-compare x y of
91 λ { (inj-lt i≤j-1 y) → inj-lt i≤j-1 (≤′-step y)
92 ; (inj-gt x j≤i-1) → inj-gt (≤′-step x) j≤i-1
93 ; (inj-eq x) → inj-eq (≤′-step x)
94 }
95
96 -- The "space" above a Fin n is the number of unique "Fin n"s greater
97 -- than or equal to it.
98 space : ∀ {n} → Fin n → ℕ
99 space f = suc (go f)
100 where
101 go : ∀ {n} → Fin n → ℕ
102 go {suc n} Fin.zero = n
103 go (Fin.suc x) = go x
104
105 space≤′n : ∀ {n} (x : Fin n) → space x ≤′ n
106 space≤′n zero = ≤′-refl
107 space≤′n (suc x) = ≤′-step (space≤′n x)
108
109 ------------------------------------------------------------------------
110 -- Definition
111 ------------------------------------------------------------------------
112
113 infixl 6 _Δ_
114 record PowInd {c} (C : Set c) : Set c where
115 constructor _Δ_
116 field
117 coeff : C
118 pow : ℕ
119 open PowInd public
120
121
122 record Poly (n : ℕ) : Set ℓ₁
123 data FlatPoly : ℕ → Set ℓ₁
124 Coeff : ℕ → Set ℓ₁
125 record NonZero (i : ℕ) : Set ℓ₁
126 Zero : ∀ {n} → Poly n → Set
127 Normalised : ∀ {i} → Coeff i + → Set
128
129 -- A Polynomial is indexed by the number of variables it contains.
130 infixl 6 _⊐_
131 record Poly n where
132 inductive
133 constructor _⊐_
134 eta-equality -- To allow matching on constructor
135 field
136 {i} : ℕ
137 flat : FlatPoly i
138 i≤n : i ≤′ n
139
140 data FlatPoly where
141 Κ : Carrier → FlatPoly zero
142 ⅀ : ∀ {n} (xs : Coeff n +) {xn : Normalised xs} → FlatPoly (suc n)
143
144
145 Coeff n = PowInd (NonZero n)
146
147 -- We disallow zeroes in the coefficient list. This condition alone
148 -- is enough to ensure a unique representation for any polynomial.
149 infixl 6 _≠0
150 record NonZero i where
151 inductive
152 constructor _≠0
153 field
154 poly : Poly i
155 .{poly≠0} : ¬ Zero poly
156
157 -- This predicate is used (in its negation) to ensure that no
158 -- coefficient is zero, preventing any trailing zeroes.
159 Zero (Κ x ⊐ _) = T (isZero x)
160 Zero (⅀ _ ⊐ _) = ⊥
161
162 -- This predicate is used to ensure that all polynomials are in
163 -- normal form: if a particular level is constant, then it can
164 -- be collapsed into the level below it.
165 Normalised (_ Δ zero & []) = ⊥
166 Normalised (_ Δ zero & ∹ _) = ⊤
167 Normalised (_ Δ suc _ & _) = ⊤
168 open NonZero public
169 open Poly public
170
171 ------------------------------------------------------------------------
172 -- Special operations
173
174 -- Decision procedure for Zero
175 zero? : ∀ {n} → (p : Poly n) → Dec (Zero p)
176 zero? (⅀ _ ⊐ _) = no id
177 zero? (Κ x ⊐ _) with isZero x
178 ... | true = yes tt
179 ... | false = no id
180 {-# INLINE zero? #-}
181
182 -- Exponentiate the first variable of a polynomial
183 infixr 8 _⍓*_ _⍓+_
184 _⍓*_ : ∀ {n} → Coeff n * → ℕ → Coeff n *
185 _⍓+_ : ∀ {n} → Coeff n + → ℕ → Coeff n +
186
187 [] ⍓* _ = []
188 (∹ xs) ⍓* i = ∹ xs ⍓+ i
189
190 coeff (head (xs ⍓+ i)) = coeff (head xs)
191 pow (head (xs ⍓+ i)) = pow (head xs) ℕ.+ i
192 tail (xs ⍓+ i) = tail xs
193
194 infixr 5 _∷↓_
195 _∷↓_ : ∀ {n} → PowInd (Poly n) → Coeff n * → Coeff n *
196 x Δ i ∷↓ xs = case zero? x of
197 λ { (yes p) → xs ⍓* suc i
198 ; (no ¬p) → ∹ _≠0 x {¬p} Δ i & xs
199 }
200 {-# INLINE _∷↓_ #-}
201
202 -- Inject a polynomial into a larger polynomial with more variables
203 _⊐↑_ : ∀ {n m} → Poly n → (suc n ≤′ m) → Poly m
204 (xs ⊐ i≤n) ⊐↑ n≤m = xs ⊐ (≤′-step i≤n ⟨ ≤′-trans ⟩ n≤m)
205 {-# INLINE _⊐↑_ #-}
206
207 infixr 4 _⊐↓_
208 _⊐↓_ : ∀ {i n} → Coeff i * → suc i ≤′ n → Poly n
209 [] ⊐↓ i≤n = Κ 0# ⊐ z≤′n
210 (∹ (x ≠0 Δ zero & [] )) ⊐↓ i≤n = x ⊐↑ i≤n
211 (∹ (x Δ zero & ∹ xs)) ⊐↓ i≤n = ⅀ (x Δ zero & ∹ xs) ⊐ i≤n
212 (∹ (x Δ suc j & xs )) ⊐↓ i≤n = ⅀ (x Δ suc j & xs) ⊐ i≤n
213 {-# INLINE _⊐↓_ #-}
214
215 ------------------------------------------------------------------------
216 -- Standard operations
217 ------------------------------------------------------------------------
218
219 ------------------------------------------------------------------------
220 -- Folds
221
222 -- These folds allow us to abstract over the proofs later: we try to
223 -- avoid using ∷↓ and ⊐↓ directly anywhere except here, so if we prove
224 -- that this fold acts the same on a normalised or non-normalised
225 -- polynomial, we can prove th same about any operation which uses it.
226
227 PolyF : ℕ → Set ℓ₁
228 PolyF i = Poly i × Coeff i *
229
230 Fold : ℕ → Set ℓ₁
231 Fold i = PolyF i → PolyF i
232
233 para : ∀ {i} → Fold i → Coeff i + → Coeff i *
234 para f (x ≠0 Δ i & []) = case f (x , []) of λ {(y , ys) → y Δ i ∷↓ ys}
235 para f (x ≠0 Δ i & ∹ xs) = case f (x , para f xs) of λ {(y , ys) → y Δ i ∷↓ ys}
236
237 poly-map : ∀ {i} → (Poly i → Poly i) → Coeff i + → Coeff i *
238 poly-map f = para (map₁ f)
239 {-# INLINE poly-map #-}
240
241 ------------------------------------------------------------------------
242 -- Addition
243
244 -- The reason the following code is so verbose is termination
245 -- checking. For instance, in the third case for ⊞-coeffs, we call a
246 -- helper function. Instead, you could conceivably use a with-block
247 -- (on ℕ.compare p q):
248 --
249 -- ⊞-coeffs ((x , p) ∷ xs) ((y , q) ∷ ys) with (ℕ.compare p q)
250 -- ... | ℕ.less p k = (x , p) ∷ ⊞-coeffs xs ((y , k) ∷ ys)
251 -- ... | ℕ.equal p = (fst~ x ⊞ fst~ y , p) ∷↓ ⊞-coeffs xs ys
252 -- ... | ℕ.greater q k = (y , q) ∷ ⊞-coeffs ((x , k) ∷ xs) ys
253 --
254 -- However, because the first and third recursive calls each rewrap
255 -- a list that was already pattern-matched on, the recursive call
256 -- does not strictly decrease the size of its argument.
257 --
258 -- Interestingly, if --cubical-compatible is turned off, we don't need
259 -- the helper function ⊞-coeffs; we could pattern match on _⊞_ directly.
260 --
261 -- _⊞_ {zero} (lift x) (lift y) = lift (x + y)
262 -- _⊞_ {suc n} [] ys = ys
263 -- _⊞_ {suc n} (x ∷ xs) [] = x ∷ xs
264 -- _⊞_ {suc n} ((x , p) ∷ xs) ((y , q) ∷ ys) = ⊞-zip (ℕ.compare p q) x xs y ys
265
266 mutual
267 infixl 6 _⊞_
268 _⊞_ : ∀ {n} → Poly n → Poly n → Poly n
269 (xs ⊐ i≤n) ⊞ (ys ⊐ j≤n) = ⊞-match (inj-compare i≤n j≤n) xs ys
270
271 ⊞-match : ∀ {i j n}
272 → {i≤n : i ≤′ n}
273 → {j≤n : j ≤′ n}
274 → InjectionOrdering i≤n j≤n
275 → FlatPoly i
276 → FlatPoly j
277 → Poly n
278 ⊞-match (inj-eq i&j≤n) (Κ x) (Κ y) = Κ (x + y) ⊐ i&j≤n
279 ⊞-match (inj-eq i&j≤n) (⅀ (x Δ i & xs)) (⅀ (y Δ j & ys)) = ⊞-zip (compare i j) x xs y ys ⊐↓ i&j≤n
280 ⊞-match (inj-lt i≤j-1 j≤n) xs (⅀ ys) = ⊞-inj i≤j-1 xs ys ⊐↓ j≤n
281 ⊞-match (inj-gt i≤n j≤i-1) (⅀ xs) ys = ⊞-inj j≤i-1 ys xs ⊐↓ i≤n
282
283 ⊞-inj : ∀ {i k}
284 → (i ≤′ k)
285 → FlatPoly i
286 → Coeff k +
287 → Coeff k *
288 ⊞-inj i≤k xs (y ⊐ j≤k ≠0 Δ zero & ys) = ⊞-match (inj-compare j≤k i≤k) y xs Δ zero ∷↓ ys
289 ⊞-inj i≤k xs (y Δ suc j & ys) = xs ⊐ i≤k Δ zero ∷↓ ∹ y Δ j & ys
290
291 ⊞-coeffs : ∀ {n} → Coeff n * → Coeff n * → Coeff n *
292 ⊞-coeffs (∹ x Δ i & xs) ys = ⊞-zip-r x i xs ys
293 ⊞-coeffs [] ys = ys
294
295 ⊞-zip : ∀ {p q n}
296 → ℕ.Ordering p q
297 → NonZero n
298 → Coeff n *
299 → NonZero n
300 → Coeff n *
301 → Coeff n *
302 ⊞-zip (ℕ.less i k) x xs y ys = ∹ x Δ i & ⊞-zip-r y k ys xs
303 ⊞-zip (ℕ.greater j k) x xs y ys = ∹ y Δ j & ⊞-zip-r x k xs ys
304 ⊞-zip (ℕ.equal i ) x xs y ys = (x .poly ⊞ y .poly) Δ i ∷↓ ⊞-coeffs xs ys
305 {-# INLINE ⊞-zip #-}
306
307 ⊞-zip-r : ∀ {n} → NonZero n → ℕ → Coeff n * → Coeff n * → Coeff n *
308 ⊞-zip-r x i xs [] = ∹ x Δ i & xs
309 ⊞-zip-r x i xs (∹ y Δ j & ys) = ⊞-zip (compare i j) x xs y ys
310
311 ------------------------------------------------------------------------
312 -- Negation
313
314 -- recurse on acc directly
315 -- https://github.com/agda/agda/issues/3190#issuecomment-416900716
316
317 ⊟-step : ∀ {n} → Acc _<′_ n → Poly n → Poly n
318 ⊟-step (acc wf) (Κ x ⊐ i≤n) = Κ (- x) ⊐ i≤n
319 ⊟-step (acc wf) (⅀ xs ⊐ i≤n) = poly-map (⊟-step (wf i≤n)) xs ⊐↓ i≤n
320
321 ⊟_ : ∀ {n} → Poly n → Poly n
322 ⊟_ = ⊟-step (<′-wellFounded _)
323 {-# INLINE ⊟_ #-}
324
325 ------------------------------------------------------------------------
326 -- Multiplication
327
328 mutual
329 ⊠-step′ : ∀ {n} → Acc _<′_ n → Poly n → Poly n → Poly n
330 ⊠-step′ a (x ⊐ i≤n) = ⊠-step a x i≤n
331
332 ⊠-step : ∀ {i n} → Acc _<′_ n → FlatPoly i → i ≤′ n → Poly n → Poly n
333 ⊠-step a (Κ x) _ = ⊠-Κ a x
334 ⊠-step a (⅀ xs) = ⊠-⅀ a xs
335
336 ⊠-Κ : ∀ {n} → Acc _<′_ n → Carrier → Poly n → Poly n
337 ⊠-Κ (acc _ ) x (Κ y ⊐ i≤n) = Κ (x * y) ⊐ i≤n
338 ⊠-Κ (acc wf) x (⅀ xs ⊐ i≤n) = ⊠-Κ-inj (wf i≤n) x xs ⊐↓ i≤n
339 {-# INLINE ⊠-Κ #-}
340
341 ⊠-⅀ : ∀ {i n} → Acc _<′_ n → Coeff i + → i <′ n → Poly n → Poly n
342 ⊠-⅀ (acc wf) xs i≤n (⅀ ys ⊐ j≤n) = ⊠-match (acc wf) (inj-compare i≤n j≤n) xs ys
343 ⊠-⅀ (acc wf) xs i≤n (Κ y ⊐ _) = ⊠-Κ-inj (wf i≤n) y xs ⊐↓ i≤n
344
345 ⊠-Κ-inj : ∀ {i} → Acc _<′_ i → Carrier → Coeff i + → Coeff i *
346 ⊠-Κ-inj a x xs = poly-map (⊠-Κ a x) (xs)
347
348 ⊠-⅀-inj : ∀ {i k}
349 → Acc _<′_ k
350 → i <′ k
351 → Coeff i +
352 → Poly k
353 → Poly k
354 ⊠-⅀-inj (acc wf) i≤k x (⅀ y ⊐ j≤k) = ⊠-match (acc wf) (inj-compare i≤k j≤k) x y
355 ⊠-⅀-inj (acc wf) i≤k x (Κ y ⊐ j≤k) = ⊠-Κ-inj (wf i≤k) y x ⊐↓ i≤k
356
357 ⊠-match : ∀ {i j n}
358 → Acc _<′_ n
359 → {i≤n : i <′ n}
360 → {j≤n : j <′ n}
361 → InjectionOrdering i≤n j≤n
362 → Coeff i +
363 → Coeff j +
364 → Poly n
365 ⊠-match (acc wf) (inj-eq i&j≤n) xs ys = ⊠-coeffs (wf i&j≤n) xs ys ⊐↓ i&j≤n
366 ⊠-match (acc wf) (inj-lt i≤j-1 j≤n) xs ys = poly-map (⊠-⅀-inj (wf j≤n) i≤j-1 xs) (ys) ⊐↓ j≤n
367 ⊠-match (acc wf) (inj-gt i≤n j≤i-1) xs ys = poly-map (⊠-⅀-inj (wf i≤n) j≤i-1 ys) (xs) ⊐↓ i≤n
368
369 ⊠-coeffs : ∀ {n} → Acc _<′_ n → Coeff n + → Coeff n + → Coeff n *
370 ⊠-coeffs a (xs) (y ≠0 Δ j & []) = poly-map (⊠-step′ a y) (xs) ⍓* j
371 ⊠-coeffs a (xs) (y ≠0 Δ j & ∹ ys) = para (⊠-cons a y ys) (xs) ⍓* j
372 {-# INLINE ⊠-coeffs #-}
373
374 ⊠-cons : ∀ {n}
375 → Acc _<′_ n
376 → Poly n
377 → Coeff n +
378 → Fold n
379 ⊠-cons a y ys (x ⊐ j≤n , xs) =
380 ⊠-step a x j≤n y , ⊞-coeffs (poly-map (⊠-step a x j≤n) ys) xs
381 {-# INLINE ⊠-cons #-}
382
383 infixl 7 _⊠_
384 _⊠_ : ∀ {n} → Poly n → Poly n → Poly n
385 _⊠_ = ⊠-step′ (<′-wellFounded _)
386 {-# INLINE _⊠_ #-}
387
388 ------------------------------------------------------------------------
389 -- Constants and variables
390
391 -- The constant polynomial
392 κ : ∀ {n} → Carrier → Poly n
393 κ x = Κ x ⊐ z≤′n
394 {-# INLINE κ #-}
395
396 -- A variable
397 ι : ∀ {n} → Fin n → Poly n
398 ι i = (κ 1# Δ 1 ∷↓ []) ⊐↓ space≤′n i
399 {-# INLINE ι #-}
400
401 ------------------------------------------------------------------------
402 -- Exponentiation
403
404 -- We try very hard to never do things like multiply by 1
405 -- unnecessarily. That's what all the weirdness here is for.
406
407 ⊡-mult : ∀ {n} → ℕ → Poly n → Poly n
408 ⊡-mult zero xs = xs
409 ⊡-mult (suc n) xs = ⊡-mult n xs ⊠ xs
410
411 _⊡_+1 : ∀ {n} → Poly n → ℕ → Poly n
412 (Κ x ⊐ i≤n) ⊡ i +1 = Κ (x ^′ suc i) ⊐ i≤n
413 (⅀ (x Δ j & []) ⊐ i≤n) ⊡ i +1 = x .poly ⊡ i +1 Δ (j ℕ.+ i ℕ.* j) ∷↓ [] ⊐↓ i≤n
414 xs@(⅀ (_ & ∹ _) ⊐ i≤n) ⊡ i +1 = ⊡-mult i xs
415
416 infixr 8 _⊡_
417 _⊡_ : ∀ {n} → Poly n → ℕ → Poly n
418 _ ⊡ zero = κ 1#
419 xs ⊡ suc i = xs ⊡ i +1
420 {-# INLINE _⊡_ #-}