@@ -176,6 +176,7 @@ In order to load Coq-Elpi use `From elpi Require Import elpi`.
176176- ` Elpi Db <dbname> <code> ` creates a Db (a program that is accumulated into
177177 other programs). ` <code> ` is the initial contents of the Db, including the
178178 type declaration of its constituting predicates.
179+ It understands the ` #[phase] ` attribute, see [ synterp-vs-interp] ( README.md#separation-of-parsing-from-execution-of-vernacular-commands ) .
179180- ` Elpi Program <qname> <code> ` lower level primitive letting one crate a
180181 command/tactic with a custom preamble ` <code> ` .
181182
@@ -186,19 +187,26 @@ In order to load Coq-Elpi use `From elpi Require Import elpi`.
186187 a no op if the Coq version is matched (or not) by the given regular expression.
187188 File names are relative to the directory mapped to ` <loadpath> ` ; if more than
188189 one such directory exists, the ` <filename> ` must exists only once.
190+ It understands the ` #[phase] ` attribute, see [ synterp-vs-interp] ( README.md#separation-of-parsing-from-execution-of-vernacular-commands )
189191- ` Elpi Typecheck [<qname>] ` typechecks the current program (or ` <qname> ` if
190192 specified).
193+ It understands the ` #[phase] ` attribute, see [ synterp-vs-interp] ( README.md#separation-of-parsing-from-execution-of-vernacular-commands )
191194- ` Elpi Debug <string> ` sets the variable ` <string> ` , relevant for conditional
192195 clause compilation (the ` :if VARIABLE ` clause attribute).
196+ It understands the ` #[phase] ` attribute, see [ synterp-vs-interp] ( README.md#separation-of-parsing-from-execution-of-vernacular-commands )
193197- ` Elpi Trace [[<start> <stop>] <predicate-filter>*|Off] ` enable/disable
194198 tracing, eventually limiting it to a specific range of execution steps or
195199 predicate names.
200+ It understands the ` #[phase] ` attribute, see [ synterp-vs-interp] ( README.md#separation-of-parsing-from-execution-of-vernacular-commands )
201+ - ` Elpi Trace Browser ` enable/disable
202+ tracing for Elpi's [ trace browser] ( ) .
196203- ` Elpi Bound Steps <number> ` limits the number of steps an Elpi program can
197204 make.
198205- ` Elpi Print <qname> [<string> <filter>*] ` prints the program ` <qname> ` to an
199206 HTML file named ` <qname>.html ` and a text file called ` <qname>.txt `
200207 (or ` <string> ` if provided) filtering out clauses whose file or clause-name
201208 matches ` <filter> ` .
209+ It understands the ` #[phase] ` attribute, see [ synterp-vs-interp] ( README.md#separation-of-parsing-from-execution-of-vernacular-commands )
202210
203211where:
204212
@@ -216,6 +224,86 @@ where:
216224
217225</p ></details >
218226
227+ #### Separation of parsing from execution of vernacular commands
228+
229+ <details ><summary >(click to expand)</summary >
230+
231+ Since version 8.18 Coq has separate parsing and execution phases,
232+ respectively called synterp and interp.
233+
234+ Since Coq has an extensible grammar the parsing phase is not entirely
235+ performed by the parser: after parsing one sentence Coq evaluates its
236+ synterp action. The synterp actions of a command like ` Import A. ` are
237+ the subset of its effect which affect parsing, like enabling a notation.
238+ Later, during the execution phase Coq evaluates the its
239+ interp action, which includes effects like putting lemma names in scope or
240+ enables type class instances etc.
241+
242+ Being able to parse an entire document quickly,
243+ without actually executing any sentence, is important for developing reactive
244+ user interfaces, but requires some extra work when defining new commands,
245+ in particular to separate their synterp actions from their interp ones.
246+ Each command defined with Coq-Elpi is split into two programs,
247+ one running during the parsing phase and the other one during the execution
248+ phase.
249+
250+ ##### Declaration of synterp actions
251+
252+ Each ` Elpi Command ` internally declares two programs with the same name.
253+ One to be run while the Coq document is parsed, the synterp-command,
254+ and the other one while it is executed, the interp command.
255+ ` Elpi Accumulate ` , by default, adds code to the interp-command.
256+ The ` #[phase] ` attribute can be used to accumulate code to the synterp-command
257+ or to both commands. ` Elpi Typecheck ` checks both commands.
258+
259+ Each ` Elpi Db ` internally declares one db, by default for the interp phase.
260+ The ` #[phase] ` attribute can be used crate a database for the synterp phase,
261+ or for both phases. Note that databases for the two phases are distinct, no
262+ data is shared among them. In particular the ` coq.elpi.accumulate* ` API exists
263+ in both phases and only acts on data bases for the current phase.
264+
265+ ##### The alignment of phases
266+
267+ All synterp actions, i.e. calls to APIs dealing with modules and sections
268+ like begin/end-module or import/export, have to happen at * both* synterp and
269+ interp time and * in the same order* .
270+
271+ In order to do so, the synterp-command may need to communicate data to the
272+ corresponding interp-command. There are two ways for doing so.
273+
274+ The first one is to use, as the main entry points, the following ones:
275+ ```
276+ pred main-synterp i:list argument, o:any.
277+ pred main-interp i:list argument, i:any.
278+ ```
279+ Unlike ` main ` the former outputs a datum while the latter receives it in input.
280+ During the synterp phase the API ` coq.synterp-actions ` lists the actions
281+ performed so far. An excerpt from the [ coq-builtin-synterp] ( coq-builtin-synterp.elpi ) file:
282+ ```
283+ % Action executed during the parsing phase (aka synterp)
284+ kind synterp-action type.
285+ type begin-module id -> synterp-action.
286+ type end-module modpath -> synterp-action.
287+ ```
288+ The synterp-command can output data of that type, but also any other data it
289+ wishes.
290+
291+ The second way to communicate data is implicit, but limited to synterp actions.
292+ During the interp phase commands can use the ` coq.next-synterp-action ` API to
293+ peek into the list of actions yet to be performed.
294+ Once an action is performed, the API reveals the next one. See also the
295+ related utilities ` coq.replay-synterp-action ` and
296+ ` coq.replay-all-missing-synterp-actions ` .
297+
298+ ##### Syntax of the ` #[phase] ` attribute
299+
300+ - ` #[phase="ph"] ` where ` "ph" ` can be ` "parsing" ` ,
301+ ` "execution" ` or ` "both" `
302+ - ` #[synterp] ` is a shorthand for ` #[phase="parsing"] `
303+ - ` #[interp] ` is a shorthand for ` #[phase="execution] `
304+
305+ </p ></details >
306+
219307#### Invocation of Elpi code
220308
221309<details ><summary >(click to expand)</summary >
@@ -227,7 +315,8 @@ where:
227315 program passing a possible empty list of arguments and the current goal. This
228316 is how you invoke a tactic.
229317
230- - ` Elpi Export <qname> ` makes it possible to invoke command ` <qname> ` without
318+ - ` Elpi Export <qname> [As <other-qname>] ` makes it possible to invoke
319+ command ` <qname> ` (or ` <other-qname> ` if given) without
231320 the ` Elpi ` prefix or invoke tactic ` <qname> ` in the middle of a term just
232321 writing ` <qname> args ` instead of ` ltac:(elpi <qname> args) ` . Note that in
233322 the case of tactics, all arguments are considered to be terms.
@@ -327,6 +416,9 @@ Arguments of type `uconstr` are passed raw.
327416
328417- ` Elpi Query [<qname>] <code> ` runs ` <code> ` in the current program (or in
329418 ` <qname> ` if specified).
419+ - ` Elpi Query [<qname>] <synterp-code> <interp-code> ` runs
420+ ` <synterp-code> ` in the current (synterp) program (or in
421+ ` <qname> ` if specified) and ` <interp-code> ` in the current program (or ` <qname> ` ).
330422- ` elpi query [<qname>] <string> <argument>* ` runs the ` <string> ` predicate
331423 (that must have the same signature of the default predicate ` solve ` ).
332424
@@ -393,12 +485,13 @@ see [coq-builtin](coq-builtin.elpi).
393485
394486- [ coq-builtin] ( coq-builtin.elpi ) documents the HOAS encoding of Coq terms
395487 and the API to access Coq
488+ - [ coq-builtin-synterp] ( coq-builtin-synterp.elpi ) documents APIs to interact with Coq at parsing time
396489- [ elpi-buitin] ( elpi-builtin.elpi ) documents Elpi's standard library, you may
397490 look here for list processing code
398491- [ coq-lib] ( elpi/coq-lib.elpi ) provides some utilities to manipulate Coq terms;
399492 it is an addendum to coq-builtin
400- - [ elpi-command-template] ( elpi/elpi-command-template.elpi ) provides the pre-loaded code for
401- ` Elpi Command `
493+ - [ elpi-command-template] ( elpi/elpi-command-template.elpi ) provides the pre-loaded code for ` Elpi Command ` (execution phase)
494+ - [ elpi-command-template-synterp ] ( elpi/elpi-command-template-synterp.elpi ) provides the pre-loaded code for ` Elpi Command ` (parsing phase)
402495- [ elpi-tactic-template] ( elpi/elpi-tactic-template.elpi ) provides the pre-loaded code for ` Elpi Tactic `
403496
404497#### Organization of the repository
0 commit comments