|
| 1 | +{-# OPTIONS --without-K --safe #-} |
| 2 | +module README.Tactic.Rewrite where |
| 3 | + |
| 4 | +open import Data.Nat |
| 5 | +open import Data.Nat.Properties |
| 6 | + |
| 7 | +open import Relation.Binary.PropositionalEquality as Eq |
| 8 | +import Relation.Binary.Reasoning.Preorder as PR |
| 9 | + |
| 10 | +open import Tactic.Rewrite using (cong!) |
| 11 | + |
| 12 | +---------------------------------------------------------------------- |
| 13 | +-- Usage |
| 14 | +---------------------------------------------------------------------- |
| 15 | + |
| 16 | +-- When performing large equational reasoning proofs, it's quite |
| 17 | +-- common to have to construct sophisticated lambdas to pass |
| 18 | +-- into 'cong'. This can be extremely tedious, and can bog down |
| 19 | +-- large proofs in piles of boilerplate. The 'cong!' tactic |
| 20 | +-- simplifies this process by synthesizing the appropriate call |
| 21 | +-- to 'cong' by inspecting both sides of the goal. |
| 22 | +-- |
| 23 | +-- This is best demonstrated with a small example. Consider |
| 24 | +-- the following proof: |
| 25 | + |
| 26 | +verbose-example : ∀ m n → m ≡ n → suc (suc (m + 0)) + m ≡ suc (suc n) + (n + 0) |
| 27 | +verbose-example m n eq = |
| 28 | + let open Eq.≡-Reasoning in |
| 29 | + begin |
| 30 | + suc (suc (m + 0)) + m |
| 31 | + ≡⟨ cong (λ ϕ → suc (suc (ϕ + m))) (+-identityʳ m) ⟩ |
| 32 | + suc (suc m) + m |
| 33 | + ≡⟨ cong (λ ϕ → suc (suc (ϕ + ϕ))) eq ⟩ |
| 34 | + suc (suc n) + n |
| 35 | + ≡˘⟨ cong (λ ϕ → suc (suc (n + ϕ))) (+-identityʳ n) ⟩ |
| 36 | + suc (suc n) + (n + 0) |
| 37 | + ∎ |
| 38 | + |
| 39 | +-- The calls to 'cong' add a lot of boilerplate, and also |
| 40 | +-- clutter up the proof, making it more difficult to read. |
| 41 | +-- We can simplify this by using 'cong!' to deduce those |
| 42 | +-- lambdas for us. |
| 43 | + |
| 44 | +succinct-example : ∀ m n → m ≡ n → suc (suc (m + 0)) + m ≡ suc (suc n) + (n + 0) |
| 45 | +succinct-example m n eq = |
| 46 | + let open Eq.≡-Reasoning in |
| 47 | + begin |
| 48 | + suc (suc (m + 0)) + m |
| 49 | + ≡⟨ cong! (+-identityʳ m) ⟩ |
| 50 | + suc (suc m) + m |
| 51 | + ≡⟨ cong! eq ⟩ |
| 52 | + suc (suc n) + n |
| 53 | + ≡˘⟨ cong! (+-identityʳ n) ⟩ |
| 54 | + suc (suc n) + (n + 0) |
| 55 | + ∎ |
| 56 | + |
| 57 | +---------------------------------------------------------------------- |
| 58 | +-- Limitations |
| 59 | +---------------------------------------------------------------------- |
| 60 | + |
| 61 | +-- The 'cong!' tactic can handle simple cases, but will |
| 62 | +-- struggle when presented with equality proofs like |
| 63 | +-- 'm + n ≡ n + m' or 'm + (n + o) ≡ (m + n) + o'. |
| 64 | +-- |
| 65 | +-- The reason behind this is that this tactic operates by simple |
| 66 | +-- anti-unification; it examines both sides of the equality goal |
| 67 | +-- to deduce where to generalize. When presented with two sides |
| 68 | +-- of an equality like 'm + n ≡ n + m', it will anti-unify to |
| 69 | +-- 'ϕ + ϕ', which is too specific. |
| 70 | + |
| 71 | +---------------------------------------------------------------------- |
| 72 | +-- Unit Tests |
| 73 | +---------------------------------------------------------------------- |
| 74 | + |
| 75 | +module LiteralTests |
| 76 | + (assumption : 48 ≡ 42) |
| 77 | + (f : ℕ → ℕ → ℕ → ℕ) |
| 78 | + where |
| 79 | + |
| 80 | + test₁ : 40 + 2 ≡ 42 |
| 81 | + test₁ = cong! refl |
| 82 | + |
| 83 | + test₂ : 48 ≡ 42 → 42 ≡ 48 |
| 84 | + test₂ eq = cong! (sym eq) |
| 85 | + |
| 86 | + test₃ : (f : ℕ → ℕ) → f 48 ≡ f 42 |
| 87 | + test₃ f = cong! assumption |
| 88 | + |
| 89 | + test₄ : (f : ℕ → ℕ → ℕ) → f 48 48 ≡ f 42 42 |
| 90 | + test₄ f = cong! assumption |
| 91 | + |
| 92 | + test₅ : f 48 45 48 ≡ f 42 45 42 |
| 93 | + test₅ = cong! assumption |
| 94 | + |
| 95 | +module LambdaTests |
| 96 | + (assumption : 48 ≡ 42) |
| 97 | + where |
| 98 | + |
| 99 | + test₁ : (λ x → x + 48) ≡ (λ x → x + 42) |
| 100 | + test₁ = cong! assumption |
| 101 | + |
| 102 | + test₂ : (λ x y z → x + (y + 48 + z)) ≡ (λ x y z → x + (y + 42 + z)) |
| 103 | + test₂ = cong! assumption |
| 104 | + |
| 105 | +module HigherOrderTests |
| 106 | + (f g : ℕ → ℕ) |
| 107 | + where |
| 108 | + |
| 109 | + test₁ : f ≡ g → ∀ n → f n ≡ g n |
| 110 | + test₁ eq n = cong! eq |
| 111 | + |
| 112 | + test₂ : f ≡ g → ∀ n → f (f (f n)) ≡ g (g (g n)) |
| 113 | + test₂ eq n = cong! eq |
| 114 | + |
| 115 | +module EquationalReasoningTests where |
| 116 | + |
| 117 | + test₁ : ∀ m n → m ≡ n → suc (suc (m + 0)) + m ≡ suc (suc n) + (n + 0) |
| 118 | + test₁ m n eq = |
| 119 | + let open Eq.≡-Reasoning in |
| 120 | + begin |
| 121 | + suc (suc (m + 0)) + m |
| 122 | + ≡⟨ cong! (+-identityʳ m) ⟩ |
| 123 | + suc (suc m) + m |
| 124 | + ≡⟨ cong! eq ⟩ |
| 125 | + suc (suc n) + n |
| 126 | + ≡˘⟨ cong! (+-identityʳ n) ⟩ |
| 127 | + suc (suc n) + (n + 0) |
| 128 | + ∎ |
| 129 | + |
| 130 | + test₂ : ∀ m n → m ≡ n → suc (m + m) ≤ suc (suc (n + n)) |
| 131 | + test₂ m n eq = |
| 132 | + let open PR ≤-preorder in |
| 133 | + begin |
| 134 | + suc (m + m) |
| 135 | + ≡⟨ cong! eq ⟩ |
| 136 | + suc (n + n) |
| 137 | + ∼⟨ ≤-step ≤-refl ⟩ |
| 138 | + suc (suc (n + n)) |
| 139 | + ∎ |
0 commit comments