| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- A simple tactic for used to automatically compute the function |
| 5 |
-- argument to cong. |
| 6 |
-- |
| 7 |
-- The main use for this tactic is getting a similar experience to |
| 8 |
-- 'rewrite' during equational reasoning. This allows us to write very |
| 9 |
-- succinct proofs: |
| 10 |
-- |
| 11 |
-- example : ∀ m n → m ≡ n → suc (suc (m + 0)) + m ≡ suc (suc n) + (n + 0) |
| 12 |
-- example m n eq = begin |
| 13 |
-- suc (suc (m + 0)) + m |
| 14 |
-- ≡⟨ cong! (+-identityʳ m) ⟩ |
| 15 |
-- suc (suc m) + m |
| 16 |
-- ≡⟨ cong! eq ⟩ |
| 17 |
-- suc (suc n) + n |
| 18 |
-- ≡⟨ cong! (+-identityʳ n) ⟨ |
| 19 |
-- suc (suc n) + (n + 0) |
| 20 |
-- ∎ |
| 21 |
-- |
| 22 |
-- Please see README.Tactic.Cong for more details. |
| 23 |
------------------------------------------------------------------------ |
| 24 |
|
| 25 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 26 |
|
| 27 |
module Tactic.Cong where |
| 28 |
|
| 29 |
open import Data.Bool.Base using (true; false; if_then_else_; _∧_) |
| 30 |
open import Data.Char.Base as Char using (toℕ) |
| 31 |
open import Data.Float.Base as Float using (_≡ᵇ_) |
| 32 |
open import Data.List.Base as List using ([]; _∷_) |
| 33 |
open import Data.Maybe.Base as Maybe using (Maybe; just; nothing) |
| 34 |
open import Data.Nat.Base as ℕ using (ℕ; zero; suc; _≡ᵇ_; _+_) |
| 35 |
open import Data.Unit.Base using (⊤) |
| 36 |
open import Data.Word64.Base as Word64 using (toℕ) |
| 37 |
open import Data.Product.Base using (_×_; map₁; _,_) |
| 38 |
open import Function.Base using (_$_; flip; case_of_) |
| 39 |
open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl; cong) |
| 40 |
open import Relation.Nullary.Decidable.Core using (yes; no) |
| 41 |
|
| 42 |
-- 'Data.String.Properties' defines this via 'Dec', so let's use the |
| 43 |
-- builtin for maximum speed. |
| 44 |
import Agda.Builtin.String as String renaming (primStringEquality to _≡ᵇ_) |
| 45 |
|
| 46 |
open import Reflection |
| 47 |
open import Reflection.AST.Abstraction |
| 48 |
open import Reflection.AST.AlphaEquality as Alpha |
| 49 |
open import Reflection.AST.Argument as Arg |
| 50 |
open import Reflection.AST.Argument.Information as ArgInfo |
| 51 |
open import Reflection.AST.Argument.Visibility as Visibility |
| 52 |
open import Reflection.AST.Literal as Literal |
| 53 |
open import Reflection.AST.Meta as Meta |
| 54 |
open import Reflection.AST.Name as Name |
| 55 |
open import Reflection.AST.Term as Term |
| 56 |
import Reflection.AST.Traversal as Traversal |
| 57 |
|
| 58 |
open import Reflection.TCM.Syntax |
| 59 |
open import Reflection.TCM.Utilities |
| 60 |
|
| 61 |
-- Marker to keep anti-unification from descending into the wrapped |
| 62 |
-- subterm. |
| 63 |
-- |
| 64 |
-- For instance, anti-unification of ⌞ a + b ⌟ + c and b + a + c |
| 65 |
-- yields λ ϕ → ϕ + c, as opposed to λ ϕ → ϕ + ϕ + c without ⌞_⌟. |
| 66 |
-- |
| 67 |
-- The marker is only visible to the cong! tactic, which inhibits |
| 68 |
-- normalisation. Anywhere else, ⌞ a + b ⌟ reduces to a + b. |
| 69 |
-- |
| 70 |
-- Thus, proving ⌞ a + b ⌟ + c ≡ b + a + c via cong! (+-comm a b) |
| 71 |
-- also proves a + b + c ≡ b + a + c. |
| 72 |
⌞_⌟ : ∀ {a} {A : Set a} → A → A |
| 73 |
⌞_⌟ x = x |
| 74 |
|
| 75 |
------------------------------------------------------------------------ |
| 76 |
-- Utilities |
| 77 |
------------------------------------------------------------------------ |
| 78 |
|
| 79 |
private |
| 80 |
-- Descend past a variable. |
| 81 |
varDescend : ℕ → ℕ → ℕ |
| 82 |
varDescend ϕ x = if ϕ ℕ.≤ᵇ x then suc x else x |
| 83 |
|
| 84 |
-- Descend a variable underneath pattern variables. |
| 85 |
patternDescend : ℕ → Pattern → Pattern × ℕ |
| 86 |
patternsDescend : ℕ → Args Pattern → Args Pattern × ℕ |
| 87 |
|
| 88 |
patternDescend ϕ (con c ps) = map₁ (con c) (patternsDescend ϕ ps) |
| 89 |
patternDescend ϕ (dot t) = dot t , ϕ |
| 90 |
patternDescend ϕ (var x) = var (varDescend ϕ x) , suc ϕ |
| 91 |
patternDescend ϕ (lit l) = lit l , ϕ |
| 92 |
patternDescend ϕ (proj f) = proj f , ϕ |
| 93 |
patternDescend ϕ (absurd x) = absurd (varDescend ϕ x) , suc ϕ |
| 94 |
|
| 95 |
patternsDescend ϕ ((arg i p) ∷ ps) = |
| 96 |
let (p' , ϕ') = patternDescend ϕ p |
| 97 |
(ps' , ϕ'') = patternsDescend ϕ' ps |
| 98 |
in (arg i p ∷ ps' , ϕ'') |
| 99 |
patternsDescend ϕ [] = |
| 100 |
[] , ϕ |
| 101 |
|
| 102 |
-- Construct an error when the goal is not 'x ≡ y' for some 'x' and 'y'. |
| 103 |
notEqualityError : ∀ {A : Set} Term → TC A |
| 104 |
notEqualityError goal = typeError (strErr "Cannot rewrite a goal that is not equality: " ∷ termErr goal ∷ []) |
| 105 |
|
| 106 |
unificationError : ∀ {A : Set} → TC Term → TC Term → TC A |
| 107 |
unificationError term symTerm = do |
| 108 |
term' ← term |
| 109 |
symTerm' ← symTerm |
| 110 |
-- Don't show the same term twice. |
| 111 |
let symErr = case term' Term.≟ symTerm' of λ where |
| 112 |
(yes _) → [] |
| 113 |
(no _) → strErr "\n" ∷ termErr symTerm' ∷ [] |
| 114 |
typeError (strErr "cong! failed, tried:\n" ∷ termErr term' ∷ symErr) |
| 115 |
|
| 116 |
record EqualityGoal : Set where |
| 117 |
constructor equals |
| 118 |
field |
| 119 |
level : Term |
| 120 |
type : Term |
| 121 |
lhs : Term |
| 122 |
rhs : Term |
| 123 |
|
| 124 |
destructEqualityGoal : Term → TC EqualityGoal |
| 125 |
destructEqualityGoal goal@(def (quote _≡_) (lvl ∷ tp ∷ lhs ∷ rhs ∷ [])) = |
| 126 |
pure $ equals (unArg lvl) (unArg tp) (unArg lhs) (unArg rhs) |
| 127 |
destructEqualityGoal (meta m args) = |
| 128 |
blockOnMeta m |
| 129 |
destructEqualityGoal goal = |
| 130 |
notEqualityError goal |
| 131 |
|
| 132 |
-- Helper for constructing applications of 'cong' |
| 133 |
`cong : ∀ {a} {A : Set a} {x y : A} → EqualityGoal → Term → x ≡ y → TC Term |
| 134 |
`cong {a = a} {A = A} {x = x} {y = y} eqGoal f x≡y = do |
| 135 |
-- NOTE: We apply all implicit arguments here to ensure that using |
| 136 |
-- equality proofs with implicits don't lead to unsolved metavariable |
| 137 |
-- errors. |
| 138 |
let open EqualityGoal eqGoal |
| 139 |
eq ← quoteTC x≡y |
| 140 |
`a ← quoteTC a |
| 141 |
`A ← quoteTC A |
| 142 |
`x ← quoteTC x |
| 143 |
`y ← quoteTC y |
| 144 |
pure $ def (quote cong) $ `a ⟅∷⟆ `A ⟅∷⟆ level ⟅∷⟆ type ⟅∷⟆ vLam "ϕ" f ⟨∷⟩ `x ⟅∷⟆ `y ⟅∷⟆ eq ⟨∷⟩ [] |
| 145 |
|
| 146 |
------------------------------------------------------------------------ |
| 147 |
-- Anti-Unification |
| 148 |
-- |
| 149 |
-- The core idea of the tactic is that we can compute the input |
| 150 |
-- to 'cong' by syntactically anti-unifying both sides of the |
| 151 |
-- equality, and then using that to construct a lambda |
| 152 |
-- where all the differences are replaced by the lambda-abstracted |
| 153 |
-- variable. |
| 154 |
-- |
| 155 |
-- For instance, the two terms 'suc (m + (m + 0)) + (m + 0)' and |
| 156 |
-- 'suc (m + m) + (m + 0)' would anti unify to 'suc (m + _) + (m + 0)' |
| 157 |
-- which we can then use to construct the lambda 'λ ϕ → suc (m + ϕ) + (m + 0)'. |
| 158 |
------------------------------------------------------------------------ |
| 159 |
|
| 160 |
private |
| 161 |
antiUnify : ℕ → Term → Term → Term |
| 162 |
antiUnifyArgs : ℕ → Args Term → Args Term → Maybe (Args Term) |
| 163 |
antiUnifyClauses : ℕ → Clauses → Clauses → Maybe Clauses |
| 164 |
antiUnifyClause : ℕ → Clause → Clause → Maybe Clause |
| 165 |
|
| 166 |
pattern apply-⌞⌟ t = (def (quote ⌞_⌟) (_ ∷ _ ∷ arg _ t ∷ [])) |
| 167 |
|
| 168 |
antiUnify ϕ (var x args) (var y args') with x ℕ.≡ᵇ y | antiUnifyArgs ϕ args args' |
| 169 |
... | _ | nothing = var ϕ [] |
| 170 |
... | false | just uargs = var ϕ uargs |
| 171 |
... | true | just uargs = var (varDescend ϕ x) uargs |
| 172 |
antiUnify ϕ (con c args) (con c' args') with c Name.≡ᵇ c' | antiUnifyArgs ϕ args args' |
| 173 |
... | _ | nothing = var ϕ [] |
| 174 |
... | false | just uargs = var ϕ [] |
| 175 |
... | true | just uargs = con c uargs |
| 176 |
antiUnify ϕ (def f args) (apply-⌞⌟ t) = antiUnify ϕ (def f args) t |
| 177 |
antiUnify ϕ (def f args) (def f' args') with f Name.≡ᵇ f' | antiUnifyArgs ϕ args args' |
| 178 |
... | _ | nothing = var ϕ [] |
| 179 |
... | false | just uargs = var ϕ [] |
| 180 |
... | true | just uargs = def f uargs |
| 181 |
antiUnify ϕ (lam v (abs s t)) (lam _ (abs _ t')) = |
| 182 |
lam v (abs s (antiUnify (suc ϕ) t t')) |
| 183 |
antiUnify ϕ (pat-lam cs args) (pat-lam cs' args') with antiUnifyClauses ϕ cs cs' | antiUnifyArgs ϕ args args' |
| 184 |
... | nothing | _ = var ϕ [] |
| 185 |
... | _ | nothing = var ϕ [] |
| 186 |
... | just ucs | just uargs = pat-lam ucs uargs |
| 187 |
antiUnify ϕ (Π[ s ∶ arg i a ] b) (Π[ _ ∶ arg _ a' ] b') = |
| 188 |
Π[ s ∶ arg i (antiUnify ϕ a a') ] antiUnify (suc ϕ) b b' |
| 189 |
antiUnify ϕ (sort (set t)) (sort (set t')) = |
| 190 |
sort (set (antiUnify ϕ t t')) |
| 191 |
antiUnify ϕ (sort (lit n)) (sort (lit n')) with n ℕ.≡ᵇ n' |
| 192 |
... | true = sort (lit n) |
| 193 |
... | false = var ϕ [] |
| 194 |
antiUnify ϕ (sort (propLit n)) (sort (propLit n')) with n ℕ.≡ᵇ n' |
| 195 |
... | true = sort (propLit n) |
| 196 |
... | false = var ϕ [] |
| 197 |
antiUnify ϕ (sort (inf n)) (sort (inf n')) with n ℕ.≡ᵇ n' |
| 198 |
... | true = sort (inf n) |
| 199 |
... | false = var ϕ [] |
| 200 |
antiUnify ϕ (sort unknown) (sort unknown) = |
| 201 |
sort unknown |
| 202 |
antiUnify ϕ (lit l) (lit l') with l Literal.≡ᵇ l' |
| 203 |
... | true = lit l |
| 204 |
... | false = var ϕ [] |
| 205 |
antiUnify ϕ (meta x args) (meta x' args') with x Meta.≡ᵇ x' | antiUnifyArgs ϕ args args' |
| 206 |
... | _ | nothing = var ϕ [] |
| 207 |
... | false | _ = var ϕ [] |
| 208 |
... | true | just uargs = meta x uargs |
| 209 |
antiUnify ϕ unknown unknown = unknown |
| 210 |
antiUnify ϕ _ _ = var ϕ [] |
| 211 |
|
| 212 |
antiUnifyArgs ϕ (arg i t ∷ args) (arg _ t' ∷ args') = |
| 213 |
Maybe.map (arg i (antiUnify ϕ t t') ∷_) (antiUnifyArgs ϕ args args') |
| 214 |
antiUnifyArgs ϕ [] [] = |
| 215 |
just [] |
| 216 |
antiUnifyArgs ϕ _ _ = |
| 217 |
nothing |
| 218 |
|
| 219 |
antiUnifyClause ϕ (clause Γ pats t) (clause Δ pats' t') = |
| 220 |
Maybe.when ((Γ =α= Δ) ∧ (pats =α= pats')) |
| 221 |
let (upats , ϕ') = patternsDescend ϕ pats |
| 222 |
in clause Γ upats (antiUnify ϕ' t t') |
| 223 |
antiUnifyClause ϕ (absurd-clause Γ pats) (absurd-clause Δ pats') = |
| 224 |
Maybe.when ((Γ =α= Δ) ∧ (pats =α= pats')) $ |
| 225 |
absurd-clause Γ pats |
| 226 |
antiUnifyClause ϕ _ _ = |
| 227 |
nothing |
| 228 |
|
| 229 |
antiUnifyClauses ϕ (c ∷ cs) (c' ∷ cs') = |
| 230 |
Maybe.ap (Maybe.map _∷_ (antiUnifyClause ϕ c c')) (antiUnifyClauses ϕ cs cs') |
| 231 |
antiUnifyClauses ϕ _ _ = |
| 232 |
just [] |
| 233 |
|
| 234 |
|
| 235 |
------------------------------------------------------------------------ |
| 236 |
-- Rewriting |
| 237 |
------------------------------------------------------------------------ |
| 238 |
|
| 239 |
macro |
| 240 |
cong! : ∀ {a} {A : Set a} {x y : A} → x ≡ y → Term → TC ⊤ |
| 241 |
cong! x≡y hole = |
| 242 |
-- NOTE: We avoid doing normalisation here as this tactic |
| 243 |
-- is mainly meant for equational reasoning. In that context, |
| 244 |
-- the endpoints of the equality are already specified in |
| 245 |
-- the form that the -- programmer expects them to be in, |
| 246 |
-- so normalising buys us nothing. |
| 247 |
withNormalisation false $ do |
| 248 |
goal ← inferType hole |
| 249 |
eqGoal ← destructEqualityGoal goal |
| 250 |
let makeTerm = λ lhs rhs → `cong eqGoal (antiUnify 0 lhs rhs) x≡y |
| 251 |
let lhs = EqualityGoal.lhs eqGoal |
| 252 |
let rhs = EqualityGoal.rhs eqGoal |
| 253 |
let term = makeTerm lhs rhs |
| 254 |
let symTerm = makeTerm rhs lhs |
| 255 |
let uni = _>>= flip unify hole |
| 256 |
-- When using ⌞_⌟ with ≡⟨ ... ⟨, (uni term) fails and |
| 257 |
-- (uni symTerm) succeeds. |
| 258 |
catchTC (uni term) $ |
| 259 |
catchTC (uni symTerm) $ do |
| 260 |
-- If we failed because of unresolved metas, restart. |
| 261 |
blockOnMetas goal |
| 262 |
-- If we failed for a different reason, show an error. |
| 263 |
unificationError term symTerm |