Debian Codegraph @

src/Tactic/MonoidSolver.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Reflection-based solver for monoid equalities
5 ------------------------------------------------------------------------
6 --
7 -- This solver automates the construction of proofs of equivalences
8 -- between monoid expressions.
9 -- When called like so:
10 --
11 -- proof : ∀ x y z → (x ∙ y) ∙ z ≈ x ∙ (y ∙ z) ∙ ε
12 -- proof x y z = solve mon
13 --
14 -- The following diagram describes what happens under the hood:
15 --
16 -- ┌▸x ∙ (y ∙ (z ∙ ε)) ════ x ∙ (y ∙ (z ∙ ε))◂┐
17 -- │ ║ ║ │
18 -- │ ║ ║ │
19 -- [_⇓] ║ ║ [_⇓]
20 -- ╱ ║ ║ ╲
21 -- ╱ ║ ║ ╲
22 -- (x ∙′ y) ∙′ z homo homo x ∙′ (y ∙′ z) ∙′ ε′
23 -- ▴ ╲ ║ ║ ╱ ▴
24 -- │ ╲ ║ ║ ╱ │
25 -- │ [_↓] ║ ║ [_↓] │
26 -- │ │ ║ ║ │ │
27 -- │ │ ║ ║ │ │
28 -- │ └───▸(x ∙ y) ∙ z x ∙ (y ∙ z) ∙ ε◂─┘ │
29 -- │ │ │ │
30 -- │ │ │ │
31 -- └────reflection────┘ └───reflection──────┘
32 --
33 -- The actual output—the proof constructed by the solver—is represented
34 -- by the double-lined path (══).
35 --
36 -- We start at the bottom, with our two expressions.
37 -- Through reflection, we convert these two expressions to their AST
38 -- representations, in the Expr type.
39 -- We then can evaluate the AST in two ways: one simply gives us back
40 -- the two expressions we put in ([_↓]), and the other normalises
41 -- ([_⇓]).
42 -- We use the homo function to prove equivalence between these two
43 -- forms: joining up these two proofs gives us the desired overall
44 -- proof.
45
46 -- Note: What's going on with the Monoid parameter?
47 --
48 -- This module is not parameterised over a monoid, which is contrary
49 -- to what you might expect. Instead, we take the monoid record as an
50 -- argument to the solve macro, and then pass it around as an
51 -- argument wherever we need it.
52 --
53 -- We need to get the monoid record at the call site, not the import
54 -- site, to ensure that it's consistent with the rest of the context.
55 -- For instance, if we wanted to produce `x ∙ y` using the monoid record
56 -- as imported, we would run into problems:
57 -- * If we tried to just reflect on the expression itself
58 -- (quoteTerm (x ∙ y)) we would likely get some de Bruijn indices
59 -- wrong (in x and y), and ∙ might not even be in scope where the
60 -- user wants us to solve! If they're solving an expression like
61 -- x + (y + z), they can pass in the +-0-monoid, but don't have to
62 -- open it themselves.
63 -- * If instead we tried to construct a term which accesses the _∙_
64 -- field on the reflection of the record, we'd run into similar
65 -- problems again. While the record is a parameter for us, it might
66 -- not be for the user.
67 -- Basically, we need the Monoid we're looking at to be exactly the
68 -- same as the one the user is looking at, and in order to do that we
69 -- quote it at the call site.
70
71 {-# OPTIONS --cubical-compatible --safe #-}
72
73 module Tactic.MonoidSolver where
74
75 open import Algebra
76 open import Function.Base using (_⟨_⟩_)
77
78 open import Data.Bool.Base as Bool using (Bool; _∨_; if_then_else_)
79 open import Data.Maybe.Base as Maybe using (Maybe; just; nothing; maybe)
80 open import Data.List.Base as List using (List; _∷_; [])
81 open import Data.Nat.Base as ℕ using (ℕ; suc; zero)
82 open import Data.Product.Base as Product using (_×_; _,_)
83
84 open import Reflection.AST
85 open import Reflection.AST.Term
86 open import Reflection.AST.Argument
87 import Reflection.AST.Name as Name
88 open import Reflection.TCM
89 open import Reflection.TCM.Syntax
90
91 import Relation.Binary.Reasoning.Setoid as ≈-Reasoning
92
93 ------------------------------------------------------------------------
94 -- The Expr type with homomorphism proofs
95 ------------------------------------------------------------------------
96
97 infixl 7 _∙′_
98 data Expr {a} (A : Set a) : Set a where
99 _∙′_ : Expr A → Expr A → Expr A
100 ε′ : Expr A
101 [_↑] : A → Expr A
102
103 module _ {m₁ m₂} (monoid : Monoid m₁ m₂) where
104
105 open Monoid monoid
106 open ≈-Reasoning setoid
107
108 -- Convert the AST to an expression (i.e. evaluate it) without
109 -- normalising.
110 [_↓] : Expr Carrier → Carrier
111 [ x ∙′ y ↓] = [ x ↓] ∙ [ y ↓]
112 [ ε′ ↓] = ε
113 [ [ x ↑] ↓] = x
114
115 -- Convert an AST to an expression (i.e. evaluate it) while
116 -- normalising.
117 --
118 -- This first function actually converts an AST to the Cayley
119 -- representation of the underlying monoid.
120 -- This obeys the monoid laws up to beta-eta equality, which is the
121 -- property which gives us the "normalising" behaviour we want.
122 [_⇓]′ : Expr Carrier → Carrier → Carrier
123 [ x ∙′ y ⇓]′ z = [ x ⇓]′ ([ y ⇓]′ z)
124 [ ε′ ⇓]′ y = y
125 [ [ x ↑] ⇓]′ y = x ∙ y
126
127 [_⇓] : Expr Carrier → Carrier
128 [ x ⇓] = [ x ⇓]′ ε
129
130 homo′ : ∀ x y → [ x ⇓] ∙ y ≈ [ x ⇓]′ y
131 homo′ ε′ y = identityˡ y
132 homo′ [ x ↑] y = ∙-congʳ (identityʳ x)
133 homo′ (x ∙′ y) z = begin
134 [ x ∙′ y ⇓] ∙ z ≡⟨⟩
135 [ x ⇓]′ [ y ⇓] ∙ z ≈⟨ ∙-congʳ (homo′ x [ y ⇓]) ⟨
136 ([ x ⇓] ∙ [ y ⇓]) ∙ z ≈⟨ assoc [ x ⇓] [ y ⇓] z ⟩
137 [ x ⇓] ∙ ([ y ⇓] ∙ z) ≈⟨ ∙-congˡ (homo′ y z) ⟩
138 [ x ⇓] ∙ ([ y ⇓]′ z) ≈⟨ homo′ x ([ y ⇓]′ z) ⟩
139 [ x ⇓]′ ([ y ⇓]′ z) ∎
140
141 homo : ∀ x → [ x ⇓] ≈ [ x ↓]
142 homo ε′ = refl
143 homo [ x ↑] = identityʳ x
144 homo (x ∙′ y) = begin
145 [ x ∙′ y ⇓] ≡⟨⟩
146 [ x ⇓]′ [ y ⇓] ≈⟨ homo′ x [ y ⇓] ⟨
147 [ x ⇓] ∙ [ y ⇓] ≈⟨ ∙-cong (homo x) (homo y) ⟩
148 [ x ↓] ∙ [ y ↓] ∎
149
150 ------------------------------------------------------------------------
151 -- Helpers for reflection
152 ------------------------------------------------------------------------
153
154 getArgs : Term → Maybe (Term × Term)
155 getArgs (def _ xs) = go xs
156 where
157 go : List (Arg Term) → Maybe (Term × Term)
158 go (vArg x ∷ vArg y ∷ []) = just (x , y)
159 go (x ∷ xs) = go xs
160 go _ = nothing
161 getArgs _ = nothing
162
163 ------------------------------------------------------------------------
164 -- Getting monoid names
165 ------------------------------------------------------------------------
166
167 -- We try to be flexible here, by matching two kinds of names.
168 -- The first is the field accessor for the monoid record itself.
169 -- However, users will likely want to use the solver with
170 -- expressions like:
171 --
172 -- xs ++ (ys ++ zs) ≡ (xs ++ ys) ++ zs
173 --
174 -- So we also evaluate the field accessor to find functions like ++.
175
176 record MonoidNames : Set where
177 field
178 is-∙ : Name → Bool
179 is-ε : Name → Bool
180
181 buildMatcher : Name → Maybe Name → Name → Bool
182 buildMatcher n nothing x = n Name.≡ᵇ x
183 buildMatcher n (just m) x = n Name.≡ᵇ x ∨ m Name.≡ᵇ x
184
185 findMonoidNames : Term → TC MonoidNames
186 findMonoidNames mon = do
187 ∙-altName ← normalise (def (quote Monoid._∙_) (2 ⋯⟅∷⟆ mon ⟨∷⟩ []))
188 ε-altName ← normalise (def (quote Monoid.ε) (2 ⋯⟅∷⟆ mon ⟨∷⟩ []))
189 pure record
190 { is-∙ = buildMatcher (quote Monoid._∙_) (getName ∙-altName)
191 ; is-ε = buildMatcher (quote Monoid.ε) (getName ε-altName)
192 }
193
194 ------------------------------------------------------------------------
195 -- Building Expr
196 ------------------------------------------------------------------------
197
198 -- We now define a function that takes an AST representing the LHS
199 -- or RHS of the equation to solve and converts it into an AST
200 -- respresenting the corresponding Expr.
201
202 ″ε″ : Term
203 ″ε″ = quote ε′ ⟨ con ⟩ []
204
205 [_↑]′ : Term → Term
206 [ t ↑]′ = quote [_↑] ⟨ con ⟩ (t ⟨∷⟩ [])
207
208 module _ (names : MonoidNames) where
209
210 open MonoidNames names
211
212 mutual
213 ″∙″ : List (Arg Term) → Term
214 ″∙″ (x ⟨∷⟩ y ⟨∷⟩ []) = quote _∙′_ ⟨ con ⟩ buildExpr x ⟨∷⟩ buildExpr y ⟨∷⟩ []
215 ″∙″ (x ∷ xs) = ″∙″ xs
216 ″∙″ _ = unknown
217
218 buildExpr : Term → Term
219 buildExpr t@(def n xs) =
220 if is-∙ n
221 then ″∙″ xs
222 else if is-ε n
223 then ″ε″
224 else
225 [ t ↑]′
226 buildExpr t@(con n xs) =
227 if is-∙ n
228 then ″∙″ xs
229 else if is-ε n
230 then ″ε″
231 else [ t ↑]′
232 buildExpr t = quote [_↑] ⟨ con ⟩ (t ⟨∷⟩ [])
233
234 ------------------------------------------------------------------------
235 -- Constructing the solution
236 ------------------------------------------------------------------------
237
238 -- This function joins up the two homomorphism proofs. It constructs
239 -- a proof of the following form:
240 --
241 -- trans (sym (homo x)) (homo y)
242 --
243 -- where x and y are the Expr representations of each side of the
244 -- goal equation.
245
246 constructSoln : Term → MonoidNames → Term → Term → Term
247 constructSoln mon names lhs rhs =
248 quote Monoid.trans ⟨ def ⟩ 2 ⋯⟅∷⟆ mon ⟨∷⟩
249 (quote Monoid.sym ⟨ def ⟩ 2 ⋯⟅∷⟆ mon ⟨∷⟩
250 (quote homo ⟨ def ⟩ 2 ⋯⟅∷⟆ mon ⟨∷⟩ buildExpr names lhs ⟨∷⟩ []) ⟨∷⟩ [])
251 ⟨∷⟩
252 (quote homo ⟨ def ⟩ 2 ⋯⟅∷⟆ mon ⟨∷⟩ buildExpr names rhs ⟨∷⟩ []) ⟨∷⟩
253 []
254
255 ------------------------------------------------------------------------
256 -- Macro
257 ------------------------------------------------------------------------
258
259 solve-macro : Term → Term → TC _
260 solve-macro mon hole = do
261 hole′ ← inferType hole >>= normalise
262 names ← findMonoidNames mon
263 just (lhs , rhs) ← pure (getArgs hole′)
264 where nothing → typeError (termErr hole′ ∷ [])
265 let soln = constructSoln mon names lhs rhs
266 unify hole soln
267
268 macro
269 solve : Term → Term → TC _
270 solve = solve-macro