| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- A solver that uses reflection to automatically obtain and solve |
| 5 |
-- equations over rings. |
| 6 |
------------------------------------------------------------------------ |
| 7 |
|
| 8 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 9 |
|
| 10 |
module Tactic.RingSolver where |
| 11 |
|
| 12 |
open import Algebra |
| 13 |
open import Data.Fin.Base as Fin using (Fin) |
| 14 |
open import Data.Vec.Base as Vec using (Vec; _∷_; []) |
| 15 |
open import Data.List.Base as List using (List; _∷_; []) |
| 16 |
open import Data.Maybe.Base as Maybe using (Maybe; just; nothing; fromMaybe) |
| 17 |
open import Data.Nat.Base using (ℕ; suc; zero; _<ᵇ_) |
| 18 |
open import Data.Bool.Base using (Bool; if_then_else_; true; false) |
| 19 |
open import Data.Unit.Base using (⊤) |
| 20 |
open import Data.String.Base as String using (String; _++_; parens) |
| 21 |
open import Data.Product.Base using (_,_; proj₁) |
| 22 |
open import Function.Base |
| 23 |
|
| 24 |
open import Reflection |
| 25 |
open import Reflection.AST.Argument |
| 26 |
open import Reflection.AST.Term as Term |
| 27 |
open import Reflection.AST.AlphaEquality |
| 28 |
open import Reflection.AST.Name as Name |
| 29 |
open import Reflection.TCM.Syntax |
| 30 |
open import Data.Nat.Reflection |
| 31 |
open import Data.List.Reflection |
| 32 |
import Data.Vec.Reflection as Vec |
| 33 |
|
| 34 |
open import Tactic.RingSolver.NonReflective renaming (solve to solver) |
| 35 |
open import Tactic.RingSolver.Core.AlmostCommutativeRing |
| 36 |
open import Tactic.RingSolver.Core.NatSet as NatSet |
| 37 |
|
| 38 |
open AlmostCommutativeRing |
| 39 |
|
| 40 |
------------------------------------------------------------------------ |
| 41 |
-- Utilities |
| 42 |
|
| 43 |
private |
| 44 |
VarMap : Set |
| 45 |
VarMap = ℕ → Maybe Term |
| 46 |
|
| 47 |
getVisible : Arg Term → Maybe Term |
| 48 |
getVisible (arg (arg-info visible _) x) = just x |
| 49 |
getVisible _ = nothing |
| 50 |
|
| 51 |
getVisibleArgs : ∀ n → Term → Maybe (Vec Term n) |
| 52 |
getVisibleArgs n (def _ xs) = Maybe.map Vec.reverse |
| 53 |
(List.foldl f c (List.mapMaybe getVisible xs) n) |
| 54 |
where |
| 55 |
f : (∀ n → Maybe (Vec Term n)) → Term → ∀ n → Maybe (Vec Term n) |
| 56 |
f xs x zero = just [] |
| 57 |
f xs x (suc n) = Maybe.map (x ∷_) (xs n) |
| 58 |
|
| 59 |
c : ∀ n → Maybe (Vec Term n) |
| 60 |
c zero = just [] |
| 61 |
c (suc _ ) = nothing |
| 62 |
getVisibleArgs _ _ = nothing |
| 63 |
|
| 64 |
curriedTerm : NatSet → Term |
| 65 |
curriedTerm = List.foldr go Vec.`[] ∘ NatSet.toList |
| 66 |
where |
| 67 |
go : ℕ → Term → Term |
| 68 |
go x xs = var x [] Vec.`∷ xs |
| 69 |
|
| 70 |
------------------------------------------------------------------------ |
| 71 |
-- Reflection utilities for rings |
| 72 |
|
| 73 |
`AlmostCommutativeRing : Term |
| 74 |
`AlmostCommutativeRing = def (quote AlmostCommutativeRing) (2 ⋯⟨∷⟩ []) |
| 75 |
|
| 76 |
record RingOperatorTerms : Set where |
| 77 |
constructor add⇒_mul⇒_pow⇒_neg⇒_sub⇒_ |
| 78 |
field |
| 79 |
add mul pow neg sub : Term |
| 80 |
|
| 81 |
checkIsRing : Term → TC Term |
| 82 |
checkIsRing ring = checkType ring `AlmostCommutativeRing |
| 83 |
|
| 84 |
module RingReflection (`ring : Term) where |
| 85 |
|
| 86 |
-- Takes the name of a function that takes the ring as it's first |
| 87 |
-- explicit argument and the terms of it's arguments and inserts |
| 88 |
-- the required ring arguments |
| 89 |
-- e.g. "_+_" $ʳ xs = "_+_ {_} {_} ring xs" |
| 90 |
infixr 6 _$ʳ_ |
| 91 |
_$ʳ_ : Name → Args Term → Term |
| 92 |
nm $ʳ args = def nm (2 ⋯⟅∷⟆ `ring ⟨∷⟩ args) |
| 93 |
|
| 94 |
`Carrier : Term |
| 95 |
`Carrier = quote Carrier $ʳ [] |
| 96 |
|
| 97 |
`refl : Term |
| 98 |
`refl = quote refl $ʳ (1 ⋯⟅∷⟆ []) |
| 99 |
|
| 100 |
`sym : Term → Term |
| 101 |
`sym x≈y = quote sym $ʳ (2 ⋯⟅∷⟆ x≈y ⟨∷⟩ []) |
| 102 |
|
| 103 |
`trans : Term → Term → Term |
| 104 |
`trans x≈y y≈z = quote trans $ʳ (3 ⋯⟅∷⟆ x≈y ⟨∷⟩ y≈z ⟨∷⟩ []) |
| 105 |
|
| 106 |
-- Normalises each of the fields of the ring operator so we can |
| 107 |
-- compare the result against the normalised definitions we come |
| 108 |
-- across when converting the term passed to the macro. |
| 109 |
getRingOperatorTerms : TC RingOperatorTerms |
| 110 |
getRingOperatorTerms = ⦇ |
| 111 |
add⇒ normalise (quote _+_ $ʳ []) |
| 112 |
mul⇒ normalise (quote _*_ $ʳ []) |
| 113 |
pow⇒ normalise (quote _^_ $ʳ []) |
| 114 |
neg⇒ normalise (quote (-_) $ʳ []) |
| 115 |
sub⇒ normalise (quote _-_ $ʳ []) |
| 116 |
⦈ |
| 117 |
|
| 118 |
------------------------------------------------------------------------ |
| 119 |
-- Reflection utilities for ring solver |
| 120 |
|
| 121 |
module RingSolverReflection (ring : Term) (numberOfVariables : ℕ) where |
| 122 |
open RingReflection ring |
| 123 |
|
| 124 |
`numberOfVariables : Term |
| 125 |
`numberOfVariables = toTerm numberOfVariables |
| 126 |
|
| 127 |
-- This function applies the hidden arguments that the constructors |
| 128 |
-- that Expr needs. The first is the universe level, the second is the |
| 129 |
-- type it contains, and the third is the number of variables it's |
| 130 |
-- indexed by. All three of these could likely be inferred, but to |
| 131 |
-- make things easier we supply the third because we know it. |
| 132 |
infix -1 _$ᵉ_ |
| 133 |
_$ᵉ_ : Name → List (Arg Term) → Term |
| 134 |
e $ᵉ xs = con e (1 ⋯⟅∷⟆ `Carrier ⟅∷⟆ `numberOfVariables ⟅∷⟆ xs) |
| 135 |
|
| 136 |
-- A constant expression. |
| 137 |
`Κ : Term → Term |
| 138 |
`Κ x = quote Κ $ᵉ (x ⟨∷⟩ []) |
| 139 |
|
| 140 |
`I : Term → Term |
| 141 |
`I x = quote Ι $ᵉ (x ⟨∷⟩ []) |
| 142 |
|
| 143 |
infixl 6 _`⊜_ |
| 144 |
|
| 145 |
_`⊜_ : Term → Term → Term |
| 146 |
x `⊜ y = quote _⊜_ $ʳ (`numberOfVariables ⟅∷⟆ x ⟨∷⟩ y ⟨∷⟩ []) |
| 147 |
|
| 148 |
`correct : Term → Term → Term |
| 149 |
`correct x ρ = quote Ops.correct $ʳ (1 ⋯⟅∷⟆ x ⟨∷⟩ ρ ⟨∷⟩ []) |
| 150 |
|
| 151 |
`solver : Term → Term → Term |
| 152 |
`solver `f `eq = quote solver $ʳ (`numberOfVariables ⟨∷⟩ `f ⟨∷⟩ `eq ⟨∷⟩ []) |
| 153 |
|
| 154 |
-- Converts the raw terms provided by the macro into the `Expr`s |
| 155 |
-- used internally by the solver. |
| 156 |
-- |
| 157 |
-- When trying to figure out the shape of an expression, one of |
| 158 |
-- the difficult tasks is recognizing where constants in the |
| 159 |
-- underlying ring are used. If we were only dealing with ℕ, we |
| 160 |
-- might look for its constructors: however, we want to deal with |
| 161 |
-- arbitrary types which implement AlmostCommutativeRing. If the |
| 162 |
-- Term type contained type information we might be able to |
| 163 |
-- recognize it there, but it doesn't. |
| 164 |
-- |
| 165 |
-- We're in luck, though, because all other cases in the following |
| 166 |
-- function *are* recognizable. As a result, the "catch-all" case |
| 167 |
-- will just assume that it has a constant expression. |
| 168 |
convertTerm : RingOperatorTerms → VarMap → Term → TC Term |
| 169 |
convertTerm operatorTerms varMap = convert |
| 170 |
where |
| 171 |
open RingOperatorTerms operatorTerms |
| 172 |
|
| 173 |
mutual |
| 174 |
convert : Term → TC Term |
| 175 |
-- First try and match directly against the fields |
| 176 |
convert (def (quote _+_) xs) = convertOp₂ (quote _⊕_) xs |
| 177 |
convert (def (quote _*_) xs) = convertOp₂ (quote _⊗_) xs |
| 178 |
convert (def (quote -_) xs) = convertOp₁ (quote ⊝_) xs |
| 179 |
convert (def (quote _^_) xs) = convertExp xs |
| 180 |
convert (def (quote _-_) xs) = convertSub xs |
| 181 |
-- Other definitions the underlying implementation of the ring's fields |
| 182 |
convert (def nm xs) = convertUnknownName nm xs |
| 183 |
-- Variables |
| 184 |
convert v@(var x _) = pure $ fromMaybe (`Κ v) (varMap x) |
| 185 |
-- Special case to recognise "suc" for naturals |
| 186 |
convert (`suc x) = convertSuc x |
| 187 |
-- Otherwise we're forced to treat it as a constant |
| 188 |
convert t = pure $ `Κ t |
| 189 |
|
| 190 |
-- Application of a ring operator often doesn't have a type as |
| 191 |
-- simple as "Carrier → Carrier → Carrier": there may be hidden |
| 192 |
-- arguments, etc. Here, we do our best to handle those cases, |
| 193 |
-- by just taking the last two explicit arguments. |
| 194 |
convertOp₂ : Name → Args Term → TC Term |
| 195 |
convertOp₂ nm (x ⟨∷⟩ y ⟨∷⟩ []) = do |
| 196 |
x' ← convert x |
| 197 |
y' ← convert y |
| 198 |
pure (nm $ᵉ (x' ⟨∷⟩ y' ⟨∷⟩ [])) |
| 199 |
convertOp₂ nm (x ∷ xs) = convertOp₂ nm xs |
| 200 |
convertOp₂ _ _ = pure unknown |
| 201 |
|
| 202 |
convertOp₁ : Name → Args Term → TC Term |
| 203 |
convertOp₁ nm (x ⟨∷⟩ []) = do |
| 204 |
x' ← convert x |
| 205 |
pure (nm $ᵉ (x' ⟨∷⟩ [])) |
| 206 |
convertOp₁ nm (x ∷ xs) = convertOp₁ nm xs |
| 207 |
convertOp₁ _ _ = pure unknown |
| 208 |
|
| 209 |
convertExp : Args Term → TC Term |
| 210 |
convertExp (x ⟨∷⟩ y ⟨∷⟩ []) = do |
| 211 |
x' ← convert x |
| 212 |
pure (quote _⊛_ $ᵉ (x' ⟨∷⟩ y ⟨∷⟩ [])) |
| 213 |
convertExp (x ∷ xs) = convertExp xs |
| 214 |
convertExp _ = pure unknown |
| 215 |
|
| 216 |
convertSub : Args Term → TC Term |
| 217 |
convertSub (x ⟨∷⟩ y ⟨∷⟩ []) = do |
| 218 |
x' ← convert x |
| 219 |
-y' ← convertOp₁ (quote (⊝_)) (y ⟨∷⟩ []) |
| 220 |
pure (quote _⊕_ $ᵉ x' ⟨∷⟩ -y' ⟨∷⟩ []) |
| 221 |
convertSub (x ∷ xs) = convertSub xs |
| 222 |
convertSub _ = pure unknown |
| 223 |
|
| 224 |
convertUnknownName : Name → Args Term → TC Term |
| 225 |
convertUnknownName nm xs = do |
| 226 |
nameTerm ← normalise (def nm []) |
| 227 |
if (nameTerm =α= add) then convertOp₂ (quote _⊕_) xs else |
| 228 |
if (nameTerm =α= mul) then convertOp₂ (quote _⊗_) xs else |
| 229 |
if (nameTerm =α= neg) then convertOp₁ (quote ⊝_) xs else |
| 230 |
if (nameTerm =α= pow) then convertExp xs else |
| 231 |
if (nameTerm =α= sub) then convertSub xs else |
| 232 |
pure (`Κ (def nm xs)) |
| 233 |
|
| 234 |
convertSuc : Term → TC Term |
| 235 |
convertSuc x = do x' ← convert x; pure (quote _⊕_ $ᵉ (`Κ (toTerm 1) ⟨∷⟩ x' ⟨∷⟩ [])) |
| 236 |
|
| 237 |
------------------------------------------------------------------------ |
| 238 |
-- Macros |
| 239 |
------------------------------------------------------------------------ |
| 240 |
-- Quantified macro |
| 241 |
|
| 242 |
open RingReflection |
| 243 |
open RingSolverReflection |
| 244 |
|
| 245 |
malformedForallTypeError : ∀ {a} {A : Set a} → Term → TC A |
| 246 |
malformedForallTypeError found = typeError |
| 247 |
( strErr "Malformed call to solve." |
| 248 |
∷ strErr "Expected target type to be like: ∀ x y → x + y ≈ y + x." |
| 249 |
∷ strErr "Instead: " |
| 250 |
∷ termErr found |
| 251 |
∷ []) |
| 252 |
|
| 253 |
quantifiedVarMap : ℕ → VarMap |
| 254 |
quantifiedVarMap numVars i = |
| 255 |
if i <ᵇ numVars |
| 256 |
then just (var i []) |
| 257 |
else nothing |
| 258 |
|
| 259 |
constructCallToSolver : Term → RingOperatorTerms → List String → Term → Term → TC Term |
| 260 |
constructCallToSolver `ring opNames variables `lhs `rhs = do |
| 261 |
`lhsExpr ← conv `lhs |
| 262 |
`rhsExpr ← conv `rhs |
| 263 |
|
| 264 |
pure $ `solver `ring numVars |
| 265 |
(prependVLams variables (_`⊜_ `ring numVars `lhsExpr `rhsExpr)) |
| 266 |
(prependHLams variables (`refl `ring)) |
| 267 |
where |
| 268 |
numVars : ℕ |
| 269 |
numVars = List.length variables |
| 270 |
|
| 271 |
conv : Term → TC Term |
| 272 |
conv = convertTerm `ring numVars opNames (quantifiedVarMap numVars) |
| 273 |
|
| 274 |
-- This is the main macro which solves for equations in which the |
| 275 |
-- variables are universally quantified over: |
| 276 |
-- |
| 277 |
-- lemma : ∀ x y → x + y ≈ y + x |
| 278 |
-- lemma = solve-∀ ring |
| 279 |
-- |
| 280 |
-- where ring is your implementation of AlmostCommutativeRing. |
| 281 |
-- (Find some example implementations in |
| 282 |
-- Polynomial.Solver.Ring.AlmostCommutativeRing.Instances). |
| 283 |
solve-∀-macro : Name → Term → TC ⊤ |
| 284 |
solve-∀-macro ring hole = do |
| 285 |
`ring ← checkIsRing (def ring []) |
| 286 |
commitTC |
| 287 |
operatorTerms ← getRingOperatorTerms `ring |
| 288 |
|
| 289 |
-- Obtain and sanitise the goal type |
| 290 |
`hole ← inferType hole >>= reduce |
| 291 |
let variablesAndTypes , equation = stripPis `hole |
| 292 |
|
| 293 |
let variables = List.map proj₁ variablesAndTypes |
| 294 |
just (lhs ∷ rhs ∷ []) ← pure (getVisibleArgs 2 equation) |
| 295 |
where nothing → malformedForallTypeError `hole |
| 296 |
|
| 297 |
solverCall ← constructCallToSolver `ring operatorTerms variables lhs rhs |
| 298 |
unify hole solverCall |
| 299 |
|
| 300 |
macro |
| 301 |
solve-∀ : Name → Term → TC ⊤ |
| 302 |
solve-∀ = solve-∀-macro |
| 303 |
|
| 304 |
------------------------------------------------------------------------ |
| 305 |
-- Unquantified macro |
| 306 |
|
| 307 |
malformedArgumentListError : ∀ {a} {A : Set a} → Term → TC A |
| 308 |
malformedArgumentListError found = typeError |
| 309 |
( strErr "Malformed call to solve." |
| 310 |
∷ strErr "First argument should be a list of free variables." |
| 311 |
∷ strErr "Instead: " |
| 312 |
∷ termErr found |
| 313 |
∷ []) |
| 314 |
|
| 315 |
malformedGoalError : ∀ {a} {A : Set a} → Term → TC A |
| 316 |
malformedGoalError found = typeError |
| 317 |
( strErr "Malformed call to solve." |
| 318 |
∷ strErr "Goal type should be of the form: LHS ≈ RHS" |
| 319 |
∷ strErr "Instead: " |
| 320 |
∷ termErr found |
| 321 |
∷ []) |
| 322 |
|
| 323 |
checkIsListOfVariables : Term → Term → TC Term |
| 324 |
checkIsListOfVariables `ring `xs = checkType `xs (`List (`Carrier `ring)) >>= normalise |
| 325 |
|
| 326 |
-- Extracts the deBruijn indices from a list of variables |
| 327 |
getVariableIndices : Term → Maybe NatSet |
| 328 |
getVariableIndices = go [] |
| 329 |
where |
| 330 |
go : NatSet → Term → Maybe NatSet |
| 331 |
go t (var i [] `∷` xs) = go (insert i t) xs |
| 332 |
go t `[]` = just t |
| 333 |
go _ _ = nothing |
| 334 |
|
| 335 |
constructSolution : Term → RingOperatorTerms → NatSet → Term → Term → TC Term |
| 336 |
constructSolution `ring opTerms variables `lhs `rhs = do |
| 337 |
`lhsExpr ← conv `lhs |
| 338 |
`rhsExpr ← conv `rhs |
| 339 |
pure $ `trans `ring (`sym `ring `lhsExpr) `rhsExpr |
| 340 |
where |
| 341 |
numVars = List.length variables |
| 342 |
|
| 343 |
varMap : VarMap |
| 344 |
varMap i = Maybe.map (λ x → `I `ring numVars (toFinTerm x)) (lookup variables i) |
| 345 |
|
| 346 |
ρ : Term |
| 347 |
ρ = curriedTerm variables |
| 348 |
|
| 349 |
conv = λ t → do |
| 350 |
t' ← convertTerm `ring numVars opTerms varMap t |
| 351 |
pure $ `correct `ring numVars t' ρ |
| 352 |
|
| 353 |
-- Use this macro when you want to solve something *under* a lambda. |
| 354 |
-- For example: say you have a long proof, and you just want the solver |
| 355 |
-- to deal with an intermediate step. Call it like so: |
| 356 |
-- |
| 357 |
-- lemma₃ : ∀ x y → x + y * 1 + 3 ≈ 2 + 1 + y + x |
| 358 |
-- lemma₃ x y = begin |
| 359 |
-- x + y * 1 + 3 ≈⟨ +-comm x (y * 1) ⟨ +-cong ⟩ refl ⟩ |
| 360 |
-- y * 1 + x + 3 ≈⟨ solve (x ∷ y ∷ []) Int.ring ⟩ |
| 361 |
-- 3 + y + x ≡⟨ refl ⟩ |
| 362 |
-- 2 + 1 + y + x ∎ |
| 363 |
-- |
| 364 |
-- The first argument is the free variables, and the second is the |
| 365 |
-- ring implementation (as before). |
| 366 |
solve-macro : Term → Name → Term → TC ⊤ |
| 367 |
solve-macro variables ring hole = do |
| 368 |
`ring ← checkIsRing (def ring []) |
| 369 |
commitTC |
| 370 |
operatorTerms ← getRingOperatorTerms `ring |
| 371 |
|
| 372 |
-- Obtain and sanitise the list of variables |
| 373 |
listOfVariables′ ← checkIsListOfVariables `ring variables |
| 374 |
commitTC |
| 375 |
just variableIndices ← pure (getVariableIndices listOfVariables′) |
| 376 |
where nothing → malformedArgumentListError listOfVariables′ |
| 377 |
|
| 378 |
-- Obtain and santise the goal type |
| 379 |
hole′ ← inferType hole >>= reduce |
| 380 |
just (lhs ∷ rhs ∷ []) ← pure (getVisibleArgs 2 hole′) |
| 381 |
where nothing → malformedGoalError hole′ |
| 382 |
|
| 383 |
solution ← constructSolution `ring operatorTerms variableIndices lhs rhs |
| 384 |
unify hole solution |
| 385 |
|
| 386 |
macro |
| 387 |
solve : Term → Name → Term → TC ⊤ |
| 388 |
solve = solve-macro |