Debian Codegraph @

doc/README/Tactic/RingSolver.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Examples showing how the reflective ring solver may be used.
5 ------------------------------------------------------------------------
6
7 module README.Tactic.RingSolver where
8
9 -- You can ignore this bit! We're just overloading the literals Agda uses for
10 -- numbers. This bit isn't necessary if you're just using Nats, or if you
11 -- construct your type directly. We only really do it here so that we can use
12 -- different numeric types in the same file.
13
14 open import Agda.Builtin.FromNat
15 open import Data.Nat using (ℕ)
16 open import Data.Integer using (ℤ)
17 import Data.Nat.Literals as ℕ
18 import Data.Integer.Literals as ℤ
19
20 instance
21 numberNat : Number ℕ
22 numberNat = ℕ.number
23
24 instance
25 numberInt : Number ℤ
26 numberInt = ℤ.number
27
28 ------------------------------------------------------------------------------
29 -- Imports!
30
31 open import Data.List.Base as List using (List; _∷_; [])
32 open import Relation.Binary.PropositionalEquality
33 using (subst; cong; _≡_; module ≡-Reasoning)
34 open import Data.Bool as Bool using (Bool; true; false; if_then_else_)
35 open import Data.Unit using (⊤; tt)
36
37 open import Tactic.RingSolver.Core.AlmostCommutativeRing
38 using (AlmostCommutativeRing)
39
40 ------------------------------------------------------------------------
41 -- Integer examples
42 ------------------------------------------------------------------------
43
44 module IntegerExamples where
45 open import Data.Integer hiding (_^_)
46 open import Data.Integer.Tactic.RingSolver
47
48 open AlmostCommutativeRing ring using (_^_)
49
50 -- Everything is automatic: you just ask Agda to solve it and it does!
51
52 -- Addition
53 lemma₁ : ∀ x y → x + y + 3 ≡ 2 + y + x + 1
54 lemma₁ = solve-∀
55
56 -- Multiplication
57 lemma₂ : ∀ x → x * 2 + 1 ≡ x + 1 + x
58 lemma₂ = solve-∀
59
60 -- Negation
61 lemma₃ : ∀ x y → (- x) + (- y) ≡ - (x + y)
62 lemma₃ = solve-∀
63
64 -- Subtraction
65 lemma₄ : ∀ x y → (x - y) * 2 - 2 ≡ (- 2) * y - 1 + 2 * x - 1
66 lemma₄ = solve-∀
67
68 -- Exponentiation by constant literals
69 lemma₅ : ∀ x y → (x + y) ^ 2 ≡ x ^ 2 + 2 * x * y + y ^ 2
70 lemma₅ = solve-∀
71
72 -- It can be interleaved with manual proofs as well.
73 lemma₆ : ∀ x y z → y ≡ z → x + y * 1 + 3 ≡ 2 + z + x + 1
74 lemma₆ x y z y≡z = begin
75 x + y * 1 + 3 ≡⟨ solve (x ∷ y ∷ []) ⟩
76 2 + y + x + 1 ≡⟨ cong (λ v → 2 + v + x + 1) y≡z ⟩
77 2 + z + x + 1 ∎
78 where open ≡-Reasoning
79
80 ------------------------------------------------------------------------
81 -- Natural examples
82 ------------------------------------------------------------------------
83
84 module NaturalExamples where
85 open import Data.Nat
86 open import Data.Nat.Tactic.RingSolver
87
88 -- The solver is flexible enough to work with ℕ (even though it asks
89 -- for rings!)
90 lemma₁ : ∀ x y → x + y * 1 + 3 ≡ 2 + 1 + y + x
91 lemma₁ = solve-∀
92
93 ------------------------------------------------------------------------
94 -- Checking invariants
95 ------------------------------------------------------------------------
96 -- The solver makes it easy to prove invariants, without having to
97 -- rewrite proof code every time something changes in the data
98 -- structure.
99
100 module _ {a} {A : Set a} (_≤_ : A → A → Bool) where
101 open import Data.Nat hiding (_≤_)
102 open import Data.Nat.Tactic.RingSolver
103
104 -- A Skew Heap, indexed by its size.
105 data Tree : ℕ → Set a where
106 leaf : Tree 0
107 node : ∀ {n m} → A → Tree n → Tree m → Tree (1 + n + m)
108
109 -- A substitution operator, to clean things up.
110 infixr 1 _⇒_
111 _⇒_ : ∀ {n} → Tree n → ∀ {m} → n ≡ m → Tree m
112 x ⇒ n≡m = subst Tree n≡m x
113
114 open ≡-Reasoning
115
116 _∪_ : ∀ {n m} → Tree n → Tree m → Tree (n + m)
117 leaf ∪ ys = ys
118 node {a} {b} x xl xr ∪ leaf =
119 node x xl xr ⇒ solve (a ∷ b ∷ [])
120 node {a} {b} x xl xr ∪ node {c} {d} y yl yr =
121 if x ≤ y
122 then node x (node y yl yr ∪ xr) xl ⇒ begin
123 1 + (1 + c + d + b) + a ≡⟨ solve (a ∷ b ∷ c ∷ d ∷ []) ⟩
124 1 + a + b + (1 + c + d) ∎
125 else node y (node x xl xr ∪ yr) yl ⇒ begin
126 1 + (1 + a + b + d) + c ≡⟨ solve (a ∷ b ∷ c ∷ d ∷ []) ⟩
127 1 + a + b + (1 + c + d) ∎