~ubuntu-branches/ubuntu/utopic/agda-stdlib/utopic-proposed

« back to all changes in this revision

Viewing changes to src/Algebra/RingSolver.agda

  • Committer: Bazaar Package Importer
  • Author(s): Iain Lane
  • Date: 2011-05-24 10:26:15 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110524102615-9tontf43bq9ym690
Tags: 0.5-1
* [9251e0b] Imported Upstream version 0.5
* [ba20206] Make package architecture independent (Closes: #573807, #598708,
  #599958)
* [2c82171] Add watch file
* [0518fa6] No longer need procps | hurd BD as we no longer have the ticker
* [daf2445] Don't use upstream's make install — handled ourselves by
  dh_install
* [1b86533] Update watchfile to point at new location
* [637f47d] Remove debian/gbp.conf as we are no longer building for exp
* [cc88671] Require Agda 2.2.10
* [e99dab5] Set maximum stack size to 1G to prevent overflows in the build
* [251cd1d] Run the test suite manually
* [a7db697] Set the variable in the emacs loading script properly
* [540ca3f] Improve short description
* [6b3e794] Add dependencies on compatible versions of the Agda library
* [7127678] Standards-Version bump to 3.9.2, no changes required
* [9d0ae30] Update to use ghc instead of ghc6
* [3f6879a] Set Maintainer to my d.o email address

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
-- Solver for commutative ring or semiring equalities
3
3
------------------------------------------------------------------------
4
4
 
 
5
{-# OPTIONS --universe-polymorphism #-}
 
6
 
5
7
-- Uses ideas from the Coq ring tactic. See "Proving Equalities in a
6
8
-- Commutative Ring Done Right in Coq" by Grégoire and Mahboubi. The
7
9
-- code below is not optimised like theirs, though.
10
12
open import Algebra.RingSolver.AlmostCommutativeRing
11
13
 
12
14
module Algebra.RingSolver
13
 
  (coeff : RawRing)            -- Coefficient "ring".
14
 
  (r : AlmostCommutativeRing)  -- Main "ring".
15
 
  (morphism : coeff -Raw-AlmostCommutative⟶ r)
 
15
  {r₁ r₂ r₃}
 
16
  (Coeff : RawRing r₁)               -- Coefficient "ring".
 
17
  (R : AlmostCommutativeRing r₂ r₃)  -- Main "ring".
 
18
  (morphism : Coeff -Raw-AlmostCommutative⟶ R)
16
19
  where
17
20
 
18
 
import Algebra.RingSolver.Lemmas as L; open L coeff r morphism
19
 
private module C = RawRing coeff
20
 
open AlmostCommutativeRing r hiding (zero)
 
21
import Algebra.RingSolver.Lemmas as L; open L Coeff R morphism
 
22
private module C = RawRing Coeff
 
23
open AlmostCommutativeRing R hiding (zero)
21
24
import Algebra.FunctionProperties as P; open P _≈_
22
25
open import Algebra.Morphism
23
26
open _-Raw-AlmostCommutative⟶_ morphism renaming (⟦_⟧ to ⟦_⟧')
30
33
open import Data.Nat using (ℕ; suc; zero) renaming (_+_ to _ℕ-+_)
31
34
open import Data.Fin as Fin using (Fin; zero; suc)
32
35
open import Data.Vec
33
 
open import Data.Function hiding (_∶_)
 
36
open import Function hiding (type-signature)
 
37
open import Level
34
38
 
35
39
infix  9 _↑ :-_ -‿NF_
36
40
infixr 9 _:^_ _^-NF_ _:↑_
48
52
 
49
53
-- The polynomials are indexed over the number of variables.
50
54
 
51
 
data Polynomial (m : ℕ) : Set where
 
55
data Polynomial (m : ℕ) : Set r₁ where
52
56
  op   : (o : Op) (p₁ : Polynomial m) (p₂ : Polynomial m) → Polynomial m
53
57
  con  : (c : C.Carrier) → Polynomial m
54
58
  var  : (x : Fin m) → Polynomial m
79
83
⟦ p :^ n ⟧     ρ = ⟦ p ⟧ ρ ^ n
80
84
⟦ :- p ⟧       ρ = - ⟦ p ⟧ ρ
81
85
 
 
86
-- Equality.
 
87
 
 
88
_≛_ : ∀ {n} → Polynomial n → Polynomial n → Set _
 
89
p₁ ≛ p₂ = ∀ {ρ} → ⟦ p₁ ⟧ ρ ≈ ⟦ p₂ ⟧ ρ
 
90
 
82
91
private
83
92
 
84
 
  -- Equality.
85
 
 
86
 
  _≛_ : ∀ {n} → Polynomial n → Polynomial n → Set
87
 
  p₁ ≛ p₂ = ∀ {ρ} → ⟦ p₁ ⟧ ρ ≈ ⟦ p₂ ⟧ ρ
88
 
 
89
93
  -- Reindexing.
90
94
 
91
95
  _:↑_ : ∀ {n} → Polynomial n → (m : ℕ) → Polynomial (m ℕ-+ n)
98
102
------------------------------------------------------------------------
99
103
-- Normal forms of polynomials
100
104
 
101
 
private
102
 
 
103
 
  -- The normal forms (Horner forms) are indexed over
104
 
  -- * the number of variables in the polynomial, and
105
 
  -- * an equivalent polynomial.
106
 
 
107
 
  data Normal : (n : ℕ) → Polynomial n → Set where
108
 
    con   : (c : C.Carrier) → Normal 0 (con c)
109
 
    _↑    : ∀ {n p'} (p : Normal n p') → Normal (suc n) (p' :↑ 1)
110
 
    _*x+_ : ∀ {n p' c'} (p : Normal (suc n) p') (c : Normal n c') →
111
 
            Normal (suc n) (p' :* var zero :+ c' :↑ 1)
112
 
    _∶_   : ∀ {n p₁ p₂} (p : Normal n p₁) (eq : p₁ ≛ p₂) → Normal n p₂
113
 
 
114
 
  ⟦_⟧-NF : ∀ {n p} → Normal n p → Vec Carrier n → Carrier
115
 
  ⟦ p ∶ _   ⟧-NF ρ       = ⟦ p ⟧-NF ρ
116
 
  ⟦ con c   ⟧-NF ρ       = ⟦ c ⟧'
117
 
  ⟦ p ↑     ⟧-NF (x ∷ ρ) = ⟦ p ⟧-NF ρ
118
 
  ⟦ p *x+ c ⟧-NF (x ∷ ρ) = (⟦ p ⟧-NF (x ∷ ρ) * x) + ⟦ c ⟧-NF ρ
 
105
-- The normal forms (Horner forms) are indexed over
 
106
-- * the number of variables in the polynomial, and
 
107
-- * an equivalent polynomial.
 
108
 
 
109
data Normal : (n : ℕ) → Polynomial n → Set (r₁ ⊔ r₂ ⊔ r₃) where
 
110
  con   : (c : C.Carrier) → Normal 0 (con c)
 
111
  _↑    : ∀ {n p'} (p : Normal n p') → Normal (suc n) (p' :↑ 1)
 
112
  _*x+_ : ∀ {n p' c'} (p : Normal (suc n) p') (c : Normal n c') →
 
113
          Normal (suc n) (p' :* var zero :+ c' :↑ 1)
 
114
  _∶_   : ∀ {n p₁ p₂} (p : Normal n p₁) (eq : p₁ ≛ p₂) → Normal n p₂
 
115
 
 
116
⟦_⟧-Normal : ∀ {n p} → Normal n p → Vec Carrier n → Carrier
 
117
⟦ p ∶ _   ⟧-Normal ρ       = ⟦ p ⟧-Normal ρ
 
118
⟦ con c   ⟧-Normal ρ       = ⟦ c ⟧'
 
119
⟦ p ↑     ⟧-Normal (x ∷ ρ) = ⟦ p ⟧-Normal ρ
 
120
⟦ p *x+ c ⟧-Normal (x ∷ ρ) = (⟦ p ⟧-Normal (x ∷ ρ) * x) + ⟦ c ⟧-Normal ρ
119
121
 
120
122
------------------------------------------------------------------------
121
123
-- Normalisation
184
186
  normaliseOp [+] = _+-NF_
185
187
  normaliseOp [*] = _*-NF_
186
188
 
187
 
  normalise : ∀ {n} (p : Polynomial n) → Normal n p
188
 
  normalise (op o p₁ p₂) = normalise p₁ ⟨ normaliseOp o ⟩ normalise p₂
189
 
  normalise (con c)      = con-NF c
190
 
  normalise (var i)      = var-NF i
191
 
  normalise (p :^ n)     = normalise p ^-NF n
192
 
  normalise (:- p)       = -‿NF normalise p
 
189
normalise : ∀ {n} (p : Polynomial n) → Normal n p
 
190
normalise (op o p₁ p₂) = normalise p₁ ⟨ normaliseOp o ⟩ normalise p₂
 
191
normalise (con c)      = con-NF c
 
192
normalise (var i)      = var-NF i
 
193
normalise (p :^ n)     = normalise p ^-NF n
 
194
normalise (:- p)       = -‿NF normalise p
193
195
 
194
196
⟦_⟧↓ : ∀ {n} → Polynomial n → Vec Carrier n → Carrier
195
 
⟦ p ⟧↓ ρ = ⟦ normalise p ⟧-NF ρ
 
197
⟦ p ⟧↓ ρ = ⟦ normalise p ⟧-Normal ρ
196
198
 
197
199
------------------------------------------------------------------------
198
200
-- Correctness
213
215
  raise-sem (:- p)       ρ = -‿cong (raise-sem p ρ)
214
216
 
215
217
  nf-sound : ∀ {n p} (nf : Normal n p) ρ →
216
 
             ⟦ nf ⟧-NF ρ ≈ ⟦ p ⟧ ρ
 
218
             ⟦ nf ⟧-Normal ρ ≈ ⟦ p ⟧ ρ
217
219
  nf-sound (nf ∶ eq)         ρ       = nf-sound nf ρ ⟨ trans ⟩ eq
218
220
  nf-sound (con c)           ρ       = refl
219
221
  nf-sound (_↑ {p' = p'} nf) (x ∷ ρ) =