Debian Codegraph @

doc/README/Tactic/Cong.agda

raw
1 {-# OPTIONS --cubical-compatible --safe #-}
2
3 module README.Tactic.Cong where
4
5 open import Data.Nat
6 open import Data.Nat.DivMod
7 open import Data.Nat.Properties
8
9 open import Relation.Binary.PropositionalEquality
10 using (_≡_; refl; sym; cong; module ≡-Reasoning)
11
12 open import Tactic.Cong using (cong! ; ⌞_⌟)
13
14 ----------------------------------------------------------------------
15 -- Usage
16 ----------------------------------------------------------------------
17
18 -- When performing large equational reasoning proofs, it's quite
19 -- common to have to construct sophisticated lambdas to pass
20 -- into 'cong'. This can be extremely tedious, and can bog down
21 -- large proofs in piles of boilerplate. The 'cong!' tactic
22 -- simplifies this process by synthesizing the appropriate call
23 -- to 'cong' by inspecting both sides of the goal.
24 --
25 -- This is best demonstrated with a small example. Consider
26 -- the following proof:
27
28 verbose-example : ∀ m n → m ≡ n → suc (suc (m + 0)) + m ≡ suc (suc n) + (n + 0)
29 verbose-example m n eq =
30 let open ≡-Reasoning in
31 begin
32 suc (suc (m + 0)) + m
33 ≡⟨ cong (λ ϕ → suc (suc (ϕ + m))) (+-identityʳ m) ⟩
34 suc (suc m) + m
35 ≡⟨ cong (λ ϕ → suc (suc (ϕ + ϕ))) eq ⟩
36 suc (suc n) + n
37 ≡⟨ cong (λ ϕ → suc (suc (n + ϕ))) (+-identityʳ n) ⟨
38 suc (suc n) + (n + 0)
39 ∎
40
41 -- The calls to 'cong' add a lot of boilerplate, and also
42 -- clutter up the proof, making it more difficult to read.
43 -- We can simplify this by using 'cong!' to deduce those
44 -- lambdas for us.
45
46 succinct-example : ∀ m n → m ≡ n → suc (suc (m + 0)) + m ≡ suc (suc n) + (n + 0)
47 succinct-example m n eq =
48 let open ≡-Reasoning in
49 begin
50 suc (suc (m + 0)) + m
51 ≡⟨ cong! (+-identityʳ m) ⟩
52 suc (suc m) + m
53 ≡⟨ cong! eq ⟩
54 suc (suc n) + n
55 ≡⟨ cong! (+-identityʳ n) ⟨
56 suc (suc n) + (n + 0)
57 ∎
58
59 ----------------------------------------------------------------------
60 -- Explicit markings
61 ----------------------------------------------------------------------
62
63 -- The 'cong!' tactic can handle simple cases, but will
64 -- struggle when presented with equality proofs like
65 -- 'm + n ≡ n + m' or 'm + (n + o) ≡ (m + n) + o'.
66 --
67 -- The reason behind this is that this tactic operates by simple
68 -- anti-unification; it examines both sides of the equality goal
69 -- to deduce where to generalize. When presented with two sides
70 -- of an equality like 'm + n ≡ n + m', it will anti-unify to
71 -- 'ϕ + ϕ', which is too specific.
72 --
73 -- In cases like these, you may explicitly mark the subterms to
74 -- be generalized by wrapping them in the marker function, ⌞_⌟.
75
76 marker-example₁ : ∀ m n o p → m + n + (o + p) ≡ n + m + (p + o)
77 marker-example₁ m n o p =
78 let open ≡-Reasoning in
79 begin
80 ⌞ m + n ⌟ + (o + p)
81 ≡⟨ cong! (+-comm m n) ⟩
82 n + m + ⌞ o + p ⌟
83 ≡⟨ cong! (+-comm p o) ⟨
84 n + m + (p + o)
85 ∎
86
87 marker-example₂ : ∀ m n → m + n + (m + n) ≡ n + m + (n + m)
88 marker-example₂ m n =
89 let open ≤-Reasoning in
90 begin-equality
91 ⌞ m + n ⌟ + ⌞ m + n ⌟
92 ≡⟨ cong! (+-comm m n) ⟩
93 n + m + (n + m)
94 ∎
95
96 ----------------------------------------------------------------------
97 -- Unit Tests
98 ----------------------------------------------------------------------
99
100 module LiteralTests
101 (assumption : 48 ≡ 42)
102 (f : ℕ → ℕ → ℕ → ℕ)
103 where
104
105 test₁ : 40 + 2 ≡ 42
106 test₁ = cong! refl
107
108 test₂ : 48 ≡ 42 → 42 ≡ 48
109 test₂ eq = cong! (sym eq)
110
111 test₃ : (f : ℕ → ℕ) → f 48 ≡ f 42
112 test₃ f = cong! assumption
113
114 test₄ : (f : ℕ → ℕ → ℕ) → f 48 48 ≡ f 42 42
115 test₄ f = cong! assumption
116
117 test₅ : f 48 45 48 ≡ f 42 45 42
118 test₅ = cong! assumption
119
120 module LambdaTests
121 (assumption : 48 ≡ 42)
122 where
123
124 test₁ : (λ x → x + 48) ≡ (λ x → x + 42)
125 test₁ = cong! assumption
126
127 test₂ : (λ x y z → x + (y + 48 + z)) ≡ (λ x y z → x + (y + 42 + z))
128 test₂ = cong! assumption
129
130 module HigherOrderTests
131 (f g : ℕ → ℕ)
132 where
133
134 test₁ : f ≡ g → ∀ n → f n ≡ g n
135 test₁ eq n = cong! eq
136
137 test₂ : f ≡ g → ∀ n → f (f (f n)) ≡ g (g (g n))
138 test₂ eq n = cong! eq
139
140 module EquationalReasoningTests where
141
142 test₁ : ∀ m n → m ≡ n → suc (suc (m + 0)) + m ≡ suc (suc n) + (n + 0)
143 test₁ m n eq =
144 let open ≡-Reasoning in
145 begin
146 suc (suc (m + 0)) + m
147 ≡⟨ cong! (+-identityʳ m) ⟩
148 suc (suc m) + m
149 ≡⟨ cong! eq ⟩
150 suc (suc n) + n
151 ≡⟨ cong! (+-identityʳ n) ⟨
152 suc (suc n) + (n + 0)
153 ∎
154
155 test₂ : ∀ m n → m ≡ n → suc (m + m) ≤ suc (suc (n + n))
156 test₂ m n eq =
157 let open ≤-Reasoning in
158 begin
159 suc (m + m)
160 ≡⟨ cong! eq ⟩
161 suc (n + n)
162 ≤⟨ n≤1+n _ ⟩
163 suc (suc (n + n))
164 ∎
165
166 module MetaTests where
167
168 test₁ : ∀ m n o → .⦃ _ : NonZero o ⦄ → (m + n) / o ≡ (n + m) / o
169 test₁ m n o =
170 let open ≤-Reasoning in
171 begin-equality
172 ⌞ m + n ⌟ / o
173 ≡⟨ cong! (+-comm m n) ⟩
174 (n + m) / o
175 ∎
176
177 test₂ : ∀ m n o p q r → .⦃ _ : NonZero o ⦄ → .⦃ _ : NonZero p ⦄ →
178 .⦃ _ : NonZero q ⦄ → p ≡ q ^ r → (m + n) % o % p ≡ (n + m) % o % p
179 test₂ m n o p q r eq =
180 let
181 open ≤-Reasoning
182 instance q^r≢0 = m^n≢0 q r
183 in
184 begin-equality
185 (m + n) % o % p
186 ≡⟨ %-congʳ eq ⟩
187 ⌞ m + n ⌟ % o % q ^ r
188 ≡⟨ cong! (+-comm m n) ⟩
189 ⌞ n + m ⌟ % o % q ^ r
190 ≡⟨ cong! (+-comm m n) ⟨
191 ⌞ m + n ⌟ % o % q ^ r
192 ≡⟨ cong! (+-comm m n) ⟩
193 (n + m) % o % q ^ r
194 ≡⟨ %-congʳ eq ⟨
195 (n + m) % o % p
196 ∎