Debian Codegraph @

elpi/coq-HOAS.elpi

raw
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% coq-HOAS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4
5 % This section contains the low level data types linking Coq and elpi.
6 % In particular the data type for terms and the evar_map entries (a sequent)
7 % and the entry points for tactics
8
9 % Entry point for tactics. Eg. "elpi mytactic foo 3 (f x)." becomes
10 % solve <goal> <new goals>
11 % Where [str "foo", int 3, trm (app[f,x])] is part of <goal>.
12 % The encoding of goals is described below.
13 % msolve is for tactics that operate on multiple goals (called via all: ).
14 external pred solve i:goal, o:list sealed-goal.
15 external pred msolve i:list sealed-goal, o:list sealed-goal.
16
17 % Extra arguments for tactics
18 external symbol tac : ltac1-tactic -> argument.
19
20 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
21 % Coq's terms
22 %
23 % Types of term formers
24 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
25
26 % -- terms --------------------------------------------------------------------
27 kind term type.
28
29 external symbol sort : sort -> term. % Prop, Type@{i}
30
31 % constants: inductive types, inductive constructors, definitions
32 external symbol global : gref -> term.
33 external symbol pglobal : gref -> univ-instance -> term.
34
35 % binders: to form functions, arities and local definitions
36 external symbol fun : name -> term -> (term -> term) -> term. % fun x : t =>
37 external symbol prod : name -> term -> (term -> term) -> term. % forall x : t,
38 external symbol let : name -> term -> term -> (term -> term) -> term. % let x : T := v in
39
40 % other term formers: function application, pattern matching and recursion
41 external symbol app : list term -> term. % app [hd|args]
42 external symbol match : term -> term -> list term -> term. % match t p [branch])
43 external symbol fix : name -> int -> term -> (term -> term) -> term. % fix name rno ty bo
44 external symbol mfix : int -> int -> mfix-block -> term. % mfix block-idx rno
45
46 external symbol primitive : primitive-value -> term.
47
48 kind mfix-block type.
49 external symbol mfix-ty : name -> int -> term -> (term -> mfix-block) -> mfix-block. % mfix-ty name rno ty rest
50 external symbol mfix-bo : list term -> mfix-block.
51
52 % NYI
53 %type cofix name -> term -> (term -> term) -> term. % cofix name ty bo
54
55 % Notes about (match Scrutinee TypingFunction Branches) when
56 % Inductive i A : A -> nat -> Type := K : forall a : A, i A a 0
57 % and
58 % Scrutinee be a term of type (i bool true 7)
59 %
60 % - TypingFunction has a very rigid shape that depends on i. Namely
61 % as many lambdas as indexes plus one lambda for the inductive itself
62 % where the value of the parameters are taken from the type of the scrutinee:
63 % fun `a` (indt "bool") a\
64 % fun `n` (indt "nat) n\
65 % fun `i` (app[indt "i", indt "bool", a n) i\ ..
66 % Such spine of fun cannot be omitted; else elpi cannot read the term back.
67 % See also coq.bind-ind-arity-no-let in coq-lib.elpi, that builds such spine for you,
68 % or the higher level api coq.build-match (same file) that also takes
69 % care of branches.
70 % - Branches is a list of terms, the order is the canonical one (the order
71 % of the constructors as they were declared). If the constructor has arguments
72 % (excluding the parameters) then the corresponding term shall be a Coq
73 % function. In this case
74 % fun `x` (indt "bool") x\ ..
75
76 % -- helpers ------------------------------------------------------------------
77 macro @cast T TY :- (let `cast` TY T x\x).
78
79 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 % Coq's evar_map
81 %
82 % Context and evar declaration
83 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84
85 % An evar_info (displayed as a Coq goal) is essentially a sequent:
86 %
87 % x : t
88 % y := v : x
89 % ----------
90 % p x y
91 %
92 % is coded as an Elpi query
93 %
94 % pi x1\ decl x1 `x` <t> =>
95 % pi x2\ def x2 `y` x1 <v> =>
96 % declare-evar
97 % [def x2 `y` x1 <v> , decl x1 `x` <t>]
98 % (RawEvar x1 x2) (<p> x1 x2) (Ev x1 x2)
99 %
100 % where, by default, declare-evar creates a syntactic constraint as
101 %
102 % {x1 x2} :
103 % decl x1 `x` <t>, def x2 `y` x1 <v> ?-
104 % evar (RawEvar x1 x2) (<p> x1 x2) (Ev x1 x2) /* suspended on RawEvar, Ev */
105 %
106 % When the program is over, a remaining syntactic constraint like the one above
107 % is read back and transformed into the corresponding evar_info.
108
109 external func decl term -> name, term. % Var Name Ty
110 external func def term -> name, term, term. % Var Name Ty Bo
111 external func cache term -> term.
112
113 external func declare-evar list prop, term, term, term. % Ctx RawEvar Ty Evar
114
115 :name "default-declare-evar"
116 declare-evar Ctx RawEv Ty Ev :-
117 declare_constraint (declare-evar Ctx RawEv Ty Ev) [RawEv].
118
119 % When a goal (evar _ _ _) is turned into a constraint the context is filtered
120 % to only contain decl, def, pp. For now no handling rules for this set of
121 % constraints other than one to remove a constraint
122
123 external func rm-evar term, term.
124 rm-evar (uvar as X) (uvar as Y):- !, declare_constraint (rm-evar X Y) [X,Y].
125 rm-evar _ _.
126
127 constraint declare-evar evar def decl cache rm-evar {
128
129 % Override the actual context
130 rule \ (declare-evar Ctx RawEv Ty Ev) <=> (Ctx =!=> evar RawEv Ty Ev).
131
132 rule \ (rm-evar (uvar X _) (uvar Y _)) (evar (uvar X _) _ (uvar Y _)).
133 rule \ (rm-evar (uvar X _) (uvar Y _)).
134
135 }
136
137 % The (evar R Ty E) predicate suspends when R and E are flexible,
138 % and is solved otherwise.
139 % The client may want to provide an alternative implementation of
140 % the clause "default-assign-evar", for example to typechecks that the
141 % term assigned to E has type Ty, or that the term assigned to R
142 % elaborates to a term of type Ty that gets assigned to E.
143 % In tactic mode, elpi/coq-elaborator.elpi wires things up that way.
144
145 external func evar term, term -> term. % Evar Ty RefinedSolution
146 evar (uvar as X) T S :- var S _ VL, !,
147 prune T VL, prune X VL, declare_constraint (evar X T S) [X, S].
148
149 :name "default-assign-evar"
150 evar _ _ _. % volatile, only unresolved evars are considered as evars
151
152 % To ease the creation of a context with decl and def
153 % Eg. @pi-decl `x` <t> x1\ @pi-def `y` <t> <v> y\ ...
154 macro @pi-decl N T F :- pi x\ decl x N T => F x.
155 macro @pi-def N T B F :- pi x\ def x N T B => cache x B_ => F x.
156
157 % Converts an arity to a term
158 func coq.arity->term arity -> term.
159 coq.arity->term (parameter ID _ Ty Rest) (prod Name Ty R) :-
160 coq.id->name ID Name,
161 @pi-decl Name Ty x\ coq.arity->term (Rest x) (R x).
162 coq.arity->term (arity A) A.
163
164 macro @pi-parameter ID T F :-
165 sigma N\ (coq.id->name ID N, pi x\ decl x N T => F x).
166 macro @pi-inductive ID A F :-
167 sigma N\ (coq.id->name ID N, coq.arity->term A T, pi x\ decl x N T => F x).
168
169
170 % Sometimes it can be useful to pass to Coq a term with unification variables
171 % representing "untyped holes" like an implicit argument _. In particular
172 % a unification variable may exit the so called pattern fragment (applied
173 % to distinct variables) and hence cannot be reliably mapped to Coq as an evar,
174 % but can still be considered as an implicit argument.
175 % By loading in the context get-option "HOAS:holes" tt one forces that
176 % behavior. Here a convenience macro to be put on the LHS of =>
177 macro @holes! :- get-option "HOAS:holes" tt.
178
179 % Similarly, some APIs take a term skeleton in input. In that case unification
180 % variables are totally disregarded (not even mapped to Coq evars). They are
181 % interpreted as the {{ lib:elpi.hole }} constant, which represents an implicit
182 % argument. As a consequence these APIs don't modify the input term at all, but
183 % rather return a copy. Note that if {{ lib:elpi.hole }} is used directly, then
184 % it has to be applied to all variables in scope, since Coq erases variables
185 % that are not used. For example using {{ forall x : nat, lib:elpi.hole }} as
186 % a term skeleton is equivalent to {{ nat -> lib:elpi.hole }}, while
187 % {{ forall x : nat, lib:elpi.hole x lib:elpi.hole more args }} puts x in
188 % the scope of the hole (and passes to is more args).
189
190 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191 % Coq's goals and tactic invocation
192 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
193
194 % A Coq goal is essentially a sequent, like the evar_info above, but since it
195 % has to be manipulated as first class Elpi data, it is represented in a slightly
196 % different way. For example
197 %
198 % x : t
199 % y := v : x
200 % ----------
201 % g x y
202 %
203 % is represented by the following term of type sealed-goal
204 %
205 % nabla x1\
206 % nabla x2\
207 % seal
208 % (goal
209 % [def x2 `y` x1 <v> , decl x1 `x` <t>]
210 % (RawEvar x1 x2) (<g> x1 x2) (Evar x1 x2)
211 % (Arguments x1 x2))
212
213 kind goal type.
214 kind sealed-goal type.
215 external symbol nabla : (term -> sealed-goal) -> sealed-goal.
216 external symbol seal : goal -> sealed-goal.
217
218 typeabbrev goal-ctx (list prop).
219 external symbol goal : goal-ctx -> term -> term -> term -> list argument -> goal.
220
221 % A sealed-goal closes with nabla the bound names of a
222 %
223 % (goal Ctx RawSolution Ty Solution Arguments)
224 %
225 % where Ctx is a list of decl or def and Solution is a unification variable
226 % to be assigned to a term of type Ty in order to make progress.
227 % RawSolution is used as a trigger: when a term is assigned to it, it is
228 % elaborated against Ty and the resulting term is assigned to Solution.
229 %
230 % Arguments contains data attached to the goal, which lives in its context
231 % and can be used by tactics to solve the goals.
232
233 % A tactic (an elpi predicate which makes progress on a Coq goal) is
234 % a predicate of type
235 % sealed-goal -> list sealed-goal -> prop
236 %
237 % while the main entry point for a tactic written in Elpi is solve
238 % which has type
239 % goal -> list sealed-goal -> prop
240 %
241 % The utility (coq.ltac.open T G GL) postulates all the variables bounds
242 % by nabla and loads the goal context before calling T on the unsealed
243 % goal. The invocation of a tactic with arguments
244 % 3 x "y" (h x)
245 % on the previous goal results in the following Elpi query:
246 %
247 % (pi x1\ decl x1 `x` <t> =>
248 % pi x2\ def x2 `y` x1 <v> =>
249 % declare-evar
250 % [def x2 `y` x1 <v> , decl x1 `x` <t>]
251 % (RawEvar x1 x2) (<g> x1 x2) (Evar x1 x2)),
252 % (coq.ltac.open solve
253 % (nabla x1\ nabla x2\ seal
254 % (goal
255 % [def x2 `y` x1 <v> , decl x1 `x` <t>]
256 % (RawEvar x1 x2) (<g> x1 x2) (Evar x1 x2)
257 % [int 3, str `x`, str`y`, trm (app[const `h`,x1])]))
258 % NewGoals)
259 %
260 % If the goal sequent contains other evars, then a tactic invocation is
261 % an Elpi query made of the conjunction of all the declare-evar queries
262 % corresponding to these evars and the query corresponding to the goal
263 % sequent. NewGoals can be assigned to a list of goals that should be
264 % declared as open. Omitted goals are shelved. If NewGoals is not
265 % assigned, then all unresolved evars become new goals, but the order
266 % of such goals is not specified.
267
268 % The file elpi-ltac.elpi provides a few combinators (other than coq.ltac.open)
269 % in the tradition of LCF tacticals. The main difference is that the arguments
270 % of custom written tactics must not be passed as predicate arguments but rather
271 % put in the goal they receive. Indeed these arguments can contain terms, and
272 % their bound variables cannot escape the seal. coq.ltac.set-goal-arguments
273 % can be used to put an argument from the current goal context into another
274 % goal. The coq.ltac.call utility can call Ltac1 code (written in Coq) and
275 % pass arguments via this mechanism.
276
277 % Last, since Elpi is already a logic programming language with primitive
278 % support for unification variables, most of the work of a tactic can be
279 % performed without using tacticals (which work on sealed goals) but rather
280 % in the context of the original goal. The last step is typically to call
281 % the refine utility with a term synthesized by the tactic or invoke some
282 % Ltac1 code on that term (e.g. to call vm_compute, see also the example
283 % on the reflexive tactic).
284
285 % ----- Multi goals tactics. ----
286 % Coq provides goal selectors, such as all:, to pass to a tactic more than one
287 % goal. In order to write such a tactic, Coq-Elpi provides another entry point
288 % called msolve. To be precise, if there are two goals under focus, say <g1> and
289 % <g2>, then all: elpi tac <t> runs the following query
290 %
291 % msolve [<g1>,<g2>] NewGoals ; % note the disjunction
292 % coq.ltac.all (coq.ltac.open solve) [<g1>,<g2>] NewGoals
293 %
294 % So, if msolve has no clause, Coq-Elpi will use solve on all the goals
295 % independently. If msolve has a clause, then it can manipulate the entire list
296 % of sealed goals. Note that the argument <t> is in both <g1> and <g2> but
297 % it is interpreted in both contexts independently. If both goals have a proof
298 % variable named "x" then passing (@eq_refl _ x) as <t> equips both goals with
299 % a (raw) proof that "x = x", no matter what their type is.
300
301 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302 % Declarations for Coq's API (environment read/write access, etc).
303 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304
305
306 % tt = Yes, ff = No, unspecified = No (unspecified means "_" or a variable).
307 typeabbrev opaque? bool. macro @opaque! :- tt. macro @transparent! :- ff.
308
309 %%%%%%% Attributes to be passed to APIs as in @local! => coq.something %%%%%%%%
310
311 macro @keep-alg-univs! :- get-option "coq:keepalgunivs" tt.
312
313 macro @primitive! :- get-option "coq:primitive" tt. % primitive records
314 macro @reversible! :- get-option "coq:reversible" tt. % coercions
315 macro @no-tc! :- get-option "coq:no_tc" tt. % skip typeclass inference
316
317 macro @uinstance! I :- get-option "coq:uinstance" I. % universe instance
318
319 % declaration of universe polymorphic constants
320 % The first list is the one of the universe variables being bound
321 % The first boolean is tt if this list can be extended by Coq (or it has to
322 % mention all universes actually used)
323 % The second list is the one with the constraints amond where universes
324 % The second boolean is tt if this list can be extended by Coq or it has to
325 % mention all universe constraints actually required to type check the
326 % declaration)
327 macro @udecl! Vs LV Cs LC :- get-option "coq:udecl" (upoly-decl Vs LV Cs LC).
328 macro @udecl-cumul! Vs LV Cs LC :- get-option "coq:udecl-cumul" (upoly-decl-cumul Vs LV Cs LC).
329 macro @univpoly! :- @udecl! [] tt [] tt.
330 macro @univpoly-cumul! :- @udecl-cumul! [] tt [] tt.
331
332 macro @ppwidth! N :- get-option "coq:ppwidth" N. % printing width
333 macro @ppall! :- get-option "coq:pp" "all". % printing all
334 macro @ppmost! :- get-option "coq:pp" "most". % printing most of contents
335 macro @pplevel! N :- get-option "coq:pplevel" N. % printing precedence (for parentheses)
336
337 macro @keepunivs! :- get-option "coq:keepunivs" tt. % skeletons elaboration
338 macro @dropunivs! :- get-option "coq:keepunivs" ff. % add-indt/add-const
339
340 macro @using! S :- get-option "coq:using" S. % like the #[using=S] attribute
341
342 macro @inline-at! N :- get-option "coq:inline" (coq.inline.at N). % like Inline(N)
343 macro @inline! N :- get-option "coq:inline" coq.inline.default. % like
344
345 macro @redflags! F :- get-option "coq:redflags" (F : coq.redflags). % for whd & co
346
347 % both arguments are strings eg "8.12.0" "use foo instead"
348 macro @deprecated! Since Msg :-
349 get-option "coq:deprecated" (pr Since Msg).
350
351 macro @ltacfail! N :- get-option "ltac:fail" N.
352
353 % retrocompatibility macro for Coq v8.10
354 macro @coercion! :- [coercion reversible].