Debian Codegraph @

src/Tactic/RingSolver/NonReflective.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- An implementation of the ring solver that requires you to manually
5 -- pass the equation you wish to solve.
6 ------------------------------------------------------------------------
7
8 -- You'll probably want to use `Tactic.RingSolver` instead which uses
9 -- reflection to automatically extract the equation.
10
11 {-# OPTIONS --cubical-compatible --safe #-}
12
13 open import Tactic.RingSolver.Core.AlmostCommutativeRing
14
15 module Tactic.RingSolver.NonReflective
16 {ℓ₁ ℓ₂} (ring : AlmostCommutativeRing ℓ₁ ℓ₂)
17 (let open AlmostCommutativeRing ring)
18 where
19
20 open import Algebra.Morphism
21 open import Function.Base using (id; _⟨_⟩_)
22 open import Data.Bool.Base using (Bool; true; false; T; if_then_else_)
23 open import Data.Maybe.Base
24 open import Data.Nat.Base using (ℕ)
25 open import Data.Product.Base using (_×_; proj₁; proj₂; _,_)
26 open import Data.Vec.Base using (Vec)
27 open import Data.Vec.N-ary
28
29 open import Tactic.RingSolver.Core.Polynomial.Parameters
30 open import Tactic.RingSolver.Core.Expression public
31 open import Algebra.Properties.Semiring.Exp.TCOptimised semiring
32
33 module Ops where
34 zero-homo : ∀ x → T (is-just (0≟ x)) → 0# ≈ x
35 zero-homo x _ with just p ← 0≟ x = p
36
37 homo : Homomorphism ℓ₁ ℓ₂ ℓ₁ ℓ₂
38 homo = record
39 { from = record
40 { rawRing = AlmostCommutativeRing.rawRing ring
41 ; isZero = λ x → is-just (0≟ x)
42 }
43 ; to = record
44 { isAlmostCommutativeRing = record
45 { isCommutativeSemiring = isCommutativeSemiring
46 ; -‿cong = -‿cong
47 ; -‿*-distribˡ = -‿*-distribˡ
48 ; -‿+-comm = -‿+-comm
49 }
50 }
51 ; morphism = -raw-almostCommutative⟶ ring
52 ; Zero-C⟶Zero-R = zero-homo
53 }
54 open Eval rawRing id public
55
56 open import Tactic.RingSolver.Core.Polynomial.Base (Homomorphism.from homo)
57
58 norm : ∀ {n} → Expr Carrier n → Poly n
59 norm (Κ x) = κ x
60 norm (Ι x) = ι x
61 norm (x ⊕ y) = norm x ⊞ norm y
62 norm (x ⊗ y) = norm x ⊠ norm y
63 norm (⊝ x) = ⊟ norm x
64 norm (x ⊛ i) = norm x ⊡ i
65
66 ⟦_⇓⟧ : ∀ {n} → Expr Carrier n → Vec Carrier n → Carrier
67 ⟦ expr ⇓⟧ = ⟦ norm expr ⟧ₚ where
68
69 open import Tactic.RingSolver.Core.Polynomial.Semantics homo
70 renaming (⟦_⟧ to ⟦_⟧ₚ)
71
72 correct : ∀ {n} (expr : Expr Carrier n) ρ → ⟦ expr ⇓⟧ ρ ≈ ⟦ expr ⟧ ρ
73 correct {n = n} = go
74 where
75 open import Tactic.RingSolver.Core.Polynomial.Homomorphism homo
76
77 go : ∀ (expr : Expr Carrier n) ρ → ⟦ expr ⇓⟧ ρ ≈ ⟦ expr ⟧ ρ
78 go (Κ x) ρ = κ-hom x ρ
79 go (Ι x) ρ = ι-hom x ρ
80 go (x ⊕ y) ρ = ⊞-hom (norm x) (norm y) ρ ⟨ trans ⟩ (go x ρ ⟨ +-cong ⟩ go y ρ)
81 go (x ⊗ y) ρ = ⊠-hom (norm x) (norm y) ρ ⟨ trans ⟩ (go x ρ ⟨ *-cong ⟩ go y ρ)
82 go (⊝ x) ρ = ⊟-hom (norm x) ρ ⟨ trans ⟩ -‿cong (go x ρ)
83 go (x ⊛ i) ρ = ⊡-hom (norm x) i ρ ⟨ trans ⟩ ^-congˡ i (go x ρ)
84
85 open import Relation.Binary.Reflection setoid Ι ⟦_⟧ ⟦_⇓⟧ correct public
86
87 solve : ∀ (n : ℕ) →
88 (f : N-ary n (Expr Carrier n) (Expr Carrier n × Expr Carrier n)) →
89 Eqʰ n _≈_ (curryⁿ (Ops.⟦_⇓⟧ (proj₁ (Ops.close n f)))) (curryⁿ (Ops.⟦_⇓⟧ (proj₂ (Ops.close n f)))) →
90 Eq n _≈_ (curryⁿ (Ops.⟦_⟧ (proj₁ (Ops.close n f)))) (curryⁿ (Ops.⟦_⟧ (proj₂ (Ops.close n f))))
91 solve = Ops.solve
92 {-# INLINE solve #-}
93
94 infixl 6 _⊜_
95 _⊜_ : ∀ {n : ℕ} →
96 Expr Carrier n →
97 Expr Carrier n →
98 Expr Carrier n × Expr Carrier n
99 _⊜_ = _,_
100 {-# INLINE _⊜_ #-}