| 1 | [](https://github.com/LPCIC/coq-elpi/actions/workflows/ci.yml) |
| 2 | [](https://github.com/LPCIC/coq-elpi/actions/workflows/nix-action-rocq-9.0.yml) |
| 3 | [](https://github.com/LPCIC/coq-elpi/actions/workflows/nix-action-rocq-9.1.yml) |
| 4 | [](https://github.com/LPCIC/coq-elpi/actions/workflows/nix-action-rocq-9.2.yml) |
| 5 | [](https://github.com/LPCIC/coq-elpi/actions/workflows/nix-action-rocq-master.yml) |
| 6 | [](https://github.com/LPCIC/coq-elpi/actions/workflows/doc.yml) |
| 7 | [](https://coq.zulipchat.com/#narrow/stream/253928-Elpi-users.20.26.20devs) |
| 8 | <img align="right" src="https://github.com/LPCIC/coq-elpi/raw/master/etc/rocqy-elpi.png" alt="Rocq-Elpi logo" width="35%" /> |
| 9 | |
| 10 | ### Rocq-Elpi |
| 11 | |
| 12 | [Rocq](https://github.com/coq/coq) plugin embedding [Elpi](https://github.com/LPCIC/elpi). |
| 13 | |
| 14 | [Elpi](https://github.com/LPCIC/elpi) provides an easy-to-embed implementation |
| 15 | of a dialect of λProlog, a programming language well suited to manipulate |
| 16 | abstract syntax trees containing binders and unification variables. |
| 17 | |
| 18 | Rocq-Elpi provides a Rocq plugin that lets one define new commands and tactics in |
| 19 | Elpi. For that purpose it provides an embedding of Rocq's terms into λProlog |
| 20 | using the Higher-Order Abstract Syntax approach |
| 21 | ([HOAS](https://en.wikipedia.org/wiki/Higher-order_abstract_syntax)). |
| 22 | |
| 23 | Rocq-Elpi also exports to Elpi a comprehensive set of Rocq's primitives, |
| 24 | so that one can print a message, access the environment of theorems and data |
| 25 | types, define a new constant, declare implicit arguments, type classes |
| 26 | instances, and so on. For convenience it also provides quotations |
| 27 | and anti-quotations for Rocq's syntax, so that one can write `{{ nat -> lp:X }}` |
| 28 | in the middle of an Elpi program instead of the equivalent AST. |
| 29 | |
| 30 | Finally Rocq-Elpi provides an FFI to bind OCaml libraries. For |
| 31 | example [apps/json](apps/json) provides access to external data |
| 32 | in json format via the Yojson library, and [apps/xml](apps/xml) does the same |
| 33 | for xml-light. |
| 34 | |
| 35 | ## What is the purpose of all that |
| 36 | In the short term, provide an extension language for Rocq well suited to |
| 37 | manipulate terms containing binders. One can already use Elpi to implement |
| 38 | commands and tactics. |
| 39 | |
| 40 | As ongoing research we are looking forward to express algorithms like higher |
| 41 | order unification and type inference, and to provide an alternative |
| 42 | elaborator for Rocq. |
| 43 | |
| 44 | ## Installation |
| 45 | |
| 46 | The simplest way is to use [OPAM](http://opam.ocaml.org/) and type |
| 47 | ``` |
| 48 | opam repo add rocq-released https://rocq-prover.org/opam/released |
| 49 | opam install rocq-elpi |
| 50 | opam install rocq-elpi-json # example of optional plugin |
| 51 | ``` |
| 52 | |
| 53 | ### Editor Setup |
| 54 | |
| 55 | The recommended user interface is [VSRocq](https://github.com/rocq-prover/vsrocq/). |
| 56 | We provide an [extension for vscode](https://github.com/LPCIC/coq-elpi-lang) in the |
| 57 | market place, just look for Elpi. The extension provides syntax hilighting |
| 58 | for both languages even when they are nested via quotations and antiquotations. |
| 59 | |
| 60 | <details><summary>Other editors (click to expand)</summary><p> |
| 61 | |
| 62 | At the time of writing Proof General does not handle quotations correctly, see ProofGeneral/PG#437. |
| 63 | In particular `Elpi Accumulate lp:{{ .... }}.` is used in tutorials to mix Rocq and Elpi code |
| 64 | without escaping. Rocq-Elpi also accepts `Elpi Accumulate " .... ".` but strings part of the |
| 65 | Elpi code needs to be escaped. Finally, for non-tutorial material, one can always put |
| 66 | the code in an external file declared with `From some.load.path Extra Dependency "filename" as f.` |
| 67 | and use `Elpi Accumulate File f.`. |
| 68 | |
| 69 | RocqIDE does handle quotations. The installation process puts |
| 70 | [coq-elpi.lang](etc/coq-elpi.lang) |
| 71 | in a place where RocqIDE can find it. Then you can select `coq-elpi` |
| 72 | from the menu `Edit -> Preferences -> Colors`. |
| 73 | |
| 74 | For Vim users, [Coqtail](https://github.com/whonore/Coqtail) provides syntax |
| 75 | highlighting and handles quotations. |
| 76 | |
| 77 | </p></details> |
| 78 | |
| 79 | <details><summary>Development version (click to expand)</summary><p> |
| 80 | |
| 81 | To install the development version one can type |
| 82 | ``` |
| 83 | opam pin add rocq-elpi https://github.com/LPCIC/coq-elpi.git |
| 84 | ``` |
| 85 | One can also clone this repository and type `make`, but check you have |
| 86 | all the dependencies installed first (see [rocq-elpi.opam](rocq-elpi.opam)). |
| 87 | |
| 88 | We recommend to look at the [CI setup](.github/workflows) for |
| 89 | ocaml versions being tested. Also, we recommend to install `dot-merlin-reader` |
| 90 | and `ocaml-lsp-server` (version 1.15). |
| 91 | |
| 92 | </p></details> |
| 93 | |
| 94 | ## Documentation |
| 95 | |
| 96 | ### Tutorials |
| 97 | |
| 98 | - [The Elpi programming language](https://lpcic.github.io/coq-elpi/tutorial_elpi_lang.html) is an Elpi |
| 99 | tutorial, there is nothing Rocq specific in there even if the tutorial uses Rocq |
| 100 | to step trough the various examples. If you never heard of λProlog or HOAS |
| 101 | based languages (like Twelf or Beluga) then you are strongly encouraged to |
| 102 | read this tutorial and have a look at |
| 103 | [λProlog's home page](http://www.lix.polytechnique.fr/Labo/Dale.Miller/lProlog/) |
| 104 | for additional documentation. Even if you are familiar with λProlog or HOAS it |
| 105 | may be worth reading the last sections since they focus on Elpi specific |
| 106 | features. Last but not least it covers common pitfalls for people with a |
| 107 | background in functional programming and the tracing mechanisms (useful for |
| 108 | debugging). |
| 109 | - [HOAS of Rocq terms](https://lpcic.github.io/coq-elpi/tutorial_coq_elpi_HOAS.html) focuses on how |
| 110 | Rocq terms are represented in Elpi, how to inspect them and call Rocq APIs under |
| 111 | a context of binders, and finally how holes ("evars" in Rocq slang) are |
| 112 | represented. It assumes the reader is familiar with Elpi. |
| 113 | - [Writing commands in Elpi](https://lpcic.github.io/coq-elpi/tutorial_coq_elpi_command.html) focuses on how to |
| 114 | write commands, in particular how to store a state across calls via so called |
| 115 | DBs and how to handled command arguments. It assumes the reader is familiar |
| 116 | with Elpi and the HOAS of Rocq terms. |
| 117 | - [Writing tactics in Elpi](https://lpcic.github.io/coq-elpi/tutorial_coq_elpi_tactic.html) describes how goals |
| 118 | and tactics are represented, how to handle tactic arguments and finally how |
| 119 | to define tactic notations. It assumes the reader is familiar with Elpi and |
| 120 | the HOAS of Rocq terms. |
| 121 | - [Elpi: rule-based meta-language for Rocq](https://www.youtube.com/watch?v=XjkpA5rVxkM) |
| 122 | video recording of the keynote at RocqPL25 ([slides & demo files](https://www-sop.inria.fr/members/Enrico.Tassi/coqpl2025/)). |
| 123 | - [Rocq-Elpi in 20 minutes](https://youtu.be/m60rHnvCJ2o) |
| 124 | video recording of a talk given at the Rocq Users and Developers Workshop 2020. |
| 125 | |
| 126 | ### Small examples (proofs of concept) |
| 127 | |
| 128 | - [reification](examples/example_reflexive_tactic.v) is the typical use |
| 129 | case for meta programs: reading the syntax of terms into an inductive |
| 130 | representing a sub language on which some decision procedure can be |
| 131 | implemented |
| 132 | - [data bases](examples/example_data_base.v) shows how Elpi programs |
| 133 | can store data and reuse it across multiple runs |
| 134 | - [record expansion](examples/example_record_expansion.v) sketches a |
| 135 | program to unpack records in a definition: it replaces an abstraction over a |
| 136 | records with abstractions over all of its components |
| 137 | - [record to sigma](examples/example_record_to_sigma.v) sketches a |
| 138 | program that de-sugars a record type to iterated sigma types |
| 139 | - [fuzzer](examples/example_fuzzer.v) sketches a |
| 140 | program to alter an inductive type while preserving its well typedness. It |
| 141 | makes nothing useful per se, but shows how to map a term and call the type |
| 142 | checker deep inside it. |
| 143 | - [tactics](examples/example_curry_howard_tactics.v) show how to create |
| 144 | simple tactics by using (proof) terms and the elaborator of Rocq |
| 145 | - [generalize](examples/example_generalize.v) show how to abstract |
| 146 | subterms out (one way to skin the cat, there are many) |
| 147 | - [abs_evars](examples/example_abs_evars.v) show how to close a term |
| 148 | containing holes (evars) with binders |
| 149 | - [record import](examples/example_import_projections.v) gives short names |
| 150 | to record projections applied to the given record instance. |
| 151 | - [reduction surgery](examples/example_reduction_surgery.v) implements |
| 152 | a tactic fine tuning cbv with a list of allowed unfoldings taken from a |
| 153 | module. |
| 154 | - [open terms](examples/example_open_terms.v) implements |
| 155 | a tactic like `replace` that receives terms containing free variables, i.e. |
| 156 | variables bound in the goal but not in the proof context. |
| 157 | - [builtins](examples/example_plugin/) adds builtin predicates |
| 158 | (hence implemented in OCaml) to Elpi via a Rocq plugin |
| 159 | |
| 160 | ### Applications written in Rocq-Elpi |
| 161 | |
| 162 | - [Derive](apps/derive/examples/usage.v) shows how to |
| 163 | obtain proved equality tests and a few extra gadgets out of |
| 164 | inductive type declarations. See the [README](apps/derive/README.md) |
| 165 | for the list of derivations. It comes bundled with Rocq-Elpi. |
| 166 | - [Locker](apps/locker) lets one hide the computational contents of definitions |
| 167 | via modules or opaque locks. It comes bundled with Rocq-Elpi. |
| 168 | - [Hierarchy Builder](https://github.com/math-comp/hierarchy-builder) is a |
| 169 | Rocq extension to declare hierarchies of algebraic structures. |
| 170 | - [Algebra Tactics](https://github.com/math-comp/algebra-tactics/) is a |
| 171 | port of the `ring` and `field` tactics to the Mathematical Components |
| 172 | library. |
| 173 | - [Trakt](https://github.com/ecranceMERCE/trakt) is a generic goal |
| 174 | preprocessing tool for proof automation tactics in Rocq. |
| 175 | - [Namespace Emulation System](apps/NES/examples/usage_NES.v) implements |
| 176 | most of the features of namespaces (on top of Rocq's modules). |
| 177 | - [Dx](https://gitlab.univ-lille.fr/samuel.hym/dx) uses elpi to generate |
| 178 | an intermediate representation of Rocq terms, to be later tranformed into |
| 179 | C. |
| 180 | - [Coercion](apps/coercion) enable to program coercions in Elpi. |
| 181 | It comes bundled with Rocq-Elpi. |
| 182 | - [Record builder](apps/rbuild) implements fancy record build/update syntax. |
| 183 | It comes bundled with Rocq-Elpi. |
| 184 | |
| 185 | ### Quick Reference |
| 186 | |
| 187 | In order to load Rocq-Elpi use `From elpi Require Import elpi`. |
| 188 | |
| 189 | #### Vernacular commands |
| 190 | |
| 191 | <details><summary>(click to expand)</summary> |
| 192 | |
| 193 | - `Elpi Command <qname>` creates command named `<qname>` containing the preamble |
| 194 | [elpi-command](elpi/elpi-command-template.elpi). |
| 195 | - `Elpi Tactic <qname>` creates a tactic `<qname>` containing the preamble |
| 196 | [elpi-tactic](elpi/elpi-tactic-template.elpi). |
| 197 | - `Elpi Db <dbname> <code>` creates a Db (a program that is accumulated into |
| 198 | other programs). `<code>` is the initial contents of the Db, including the |
| 199 | type declaration of its constituting predicates. |
| 200 | It understands the `#[phase]` attribute, see [synterp-vs-interp](README.md#separation-of-parsing-from-execution-of-vernacular-commands). |
| 201 | - `Elpi Program <qname> <code>` lower level primitive letting one crate a |
| 202 | command/tactic with a custom preamble `<code>`. |
| 203 | - `From some.load.path Extra Dependency <filename> as <fname>` declares `<fname>` |
| 204 | as a piece of code that can be accumulated via `Elpi Accumulate File`. |
| 205 | The content is given in the external file `<filename>` to be found in |
| 206 | the Rocq load path `some.load.path`. |
| 207 | - `Elpi File <fname> <code>.` declares `<fname>` |
| 208 | as a piece of code that can be accumulated via `Elpi Accumulate File`. |
| 209 | This time the code is given in the .v file. |
| 210 | It understands the `#[phase]` attribute, see [synterp-vs-interp](README.md#separation-of-parsing-from-execution-of-vernacular-commands). |
| 211 | - `Elpi Accumulate [<dbname>|<qname>] [<code>|File [Signature] <fname>|Db [Header] <dbname>]` |
| 212 | adds code to the current program (or `<dbname>` or `<qname>` if specified). |
| 213 | The code can be verbatim, from a file or a Db. |
| 214 | File names `<fname>` must have been previously declared with |
| 215 | `Extra Dependency` or `Elpi File`. |
| 216 | Accumulating `File Signature <fname>` only adds the signautre declarations |
| 217 | (kinds, types, modes, type abbreviations) from `<fname>` skipping the code |
| 218 | (clauses/rules). |
| 219 | Accumulating `Db Header <dbname>`, instead of `Db <dbname>`, accumulates |
| 220 | only the first chunk of code associated with Db, typically the type |
| 221 | declaration of the predicates that live in the Db. When defining a command |
| 222 | or tactic it can be useful to first accumulate the Db header, then some |
| 223 | code (possibly calling the predicate living in the Db), and finally |
| 224 | accumulating the (full) Db. |
| 225 | Note that when a command is executed it may need to be (partially) |
| 226 | recompiled, e.g. if the Db was updated. In this case all the code accumulated |
| 227 | after the Db (but not after its header) may need to be recompiled. Hence |
| 228 | we recommend to accumulate Dbs last. |
| 229 | It understands the `#[skip="rex"]` and `#[only="rex"]` which make the command |
| 230 | a no op if the Rocq version is matched (or not) by the given regular expression. |
| 231 | It understands the `#[phase]` attribute, see [synterp-vs-interp](README.md#separation-of-parsing-from-execution-of-vernacular-commands). |
| 232 | It understands the `#[local]`, `#[global]`, and `#[superglobal]` scope attributes, |
| 233 | although only when accumulating to a `<dbname>` (all accumulations to a program |
| 234 | are `#[superglobal]`). Default accumulation to db is the equivalent of `#[export]`. |
| 235 | See the Rocq reference manual for the meaning of these scopes. |
| 236 | - `Elpi Typecheck [<qname>]` typechecks the current program (or `<qname>` if |
| 237 | specified). |
| 238 | It understands the `#[phase]` attribute, see [synterp-vs-interp](README.md#separation-of-parsing-from-execution-of-vernacular-commands) |
| 239 | - `Elpi Debug <string>` sets the variable `<string>`, relevant for conditional |
| 240 | clause compilation (the `:if VARIABLE` clause attribute). |
| 241 | It understands the `#[phase]` attribute, see [synterp-vs-interp](README.md#separation-of-parsing-from-execution-of-vernacular-commands) |
| 242 | - `Elpi Trace [[<start> <stop>] <predicate-filter>*|Off]` enable/disable |
| 243 | tracing, eventually limiting it to a specific range of execution steps or |
| 244 | predicate names. |
| 245 | It understands the `#[phase]` attribute, see [synterp-vs-interp](README.md#separation-of-parsing-from-execution-of-vernacular-commands) |
| 246 | - `Elpi Trace Browser` enable/disable |
| 247 | tracing for Elpi's [trace browser](). |
| 248 | - `Elpi Bound Steps <number>` limits the number of steps an Elpi program can |
| 249 | make. |
| 250 | - `Elpi Print <qname> [<string> <filter>*]` prints the program `<qname>` to |
| 251 | a text file called `<qname>.txt` (or `<string>` if provided) filtering out |
| 252 | clauses whose file or clause-name matches `<filter>`. |
| 253 | It understands the `#[phase]` attribute, see [synterp-vs-interp](README.md#separation-of-parsing-from-execution-of-vernacular-commands) |
| 254 | |
| 255 | where: |
| 256 | |
| 257 | - `<qname>` is a qualified Rocq name, e.g. `derive.eq` or `my_program`. |
| 258 | - `<dbname>` is like `<qname>` but lives in a different namespace. By convention |
| 259 | `<dbname>` ends in `.db`, e.g. `derive.eq.db`. |
| 260 | - `<code>` is verbatim Elpi code, either `lp:{{ ... }}` or `" ... "` (in the |
| 261 | latter case, strings delimiters need to be escaped following Rocq rules, e.g. |
| 262 | `lp:{{ coq.say "hello!" }}` becomes `" coq.say ""hello!"" "`). |
| 263 | - `<filename>` is a string containing the path of an external file, e.g. |
| 264 | `"this_file.elpi"`. |
| 265 | - `<fname>` is a qualified Rocq name, eg `foo.elpi` (note that `Extra Dependency` |
| 266 | only allows simple identifiers). |
| 267 | - `<start>` and `<stop>` are numbers, e.g. `17 24`. |
| 268 | - `<predicate-filter>` is a regexp against which the predicate name is matched, |
| 269 | e.g. `"derive.*"`. |
| 270 | |
| 271 | </p></details> |
| 272 | |
| 273 | #### Separation of parsing from execution of vernacular commands |
| 274 | |
| 275 | <details><summary>(click to expand)</summary> |
| 276 | |
| 277 | Since version 8.18 Rocq has separate parsing and execution phases, |
| 278 | respectively called synterp and interp. |
| 279 | |
| 280 | Since Rocq has an extensible grammar the parsing phase is not entirely |
| 281 | performed by the parser: after parsing one sentence Rocq evaluates its |
| 282 | synterp action. The synterp actions of a command like `Import A.` are |
| 283 | the subset of its effect which affect parsing, like enabling a notation. |
| 284 | Later, during the execution phase Rocq evaluates the its |
| 285 | interp action, which includes effects like putting lemma names in scope or |
| 286 | enables type class instances etc. |
| 287 | |
| 288 | Being able to parse an entire document quickly, |
| 289 | without actually executing any sentence, is important for developing reactive |
| 290 | user interfaces, but requires some extra work when defining new commands, |
| 291 | in particular to separate their synterp actions from their interp ones. |
| 292 | Each command defined with Rocq-Elpi is split into two programs, |
| 293 | one running during the parsing phase and the other one during the execution |
| 294 | phase. |
| 295 | |
| 296 | ##### Declaration of synterp actions |
| 297 | |
| 298 | Each `Elpi Command` internally declares two programs with the same name. |
| 299 | One to be run while the Rocq document is parsed, the synterp-command, |
| 300 | and the other one while it is executed, the interp command. |
| 301 | `Elpi Accumulate`, by default, adds code to the interp-command. |
| 302 | The `#[phase]` attribute can be used to accumulate code to the synterp-command |
| 303 | or to both commands. `Elpi Typecheck` checks both commands. |
| 304 | |
| 305 | Each `Elpi Db` internally declares one db, by default for the interp phase. |
| 306 | The `#[phase]` attribute can be used crate a database for the synterp phase, |
| 307 | or for both phases. Note that databases for the two phases are distinct, no |
| 308 | data is shared among them. In particular the `coq.elpi.accumulate*` API exists |
| 309 | in both phases and only acts on data bases for the current phase. |
| 310 | |
| 311 | ##### The alignment of phases |
| 312 | |
| 313 | All synterp actions, i.e. calls to APIs dealing with modules and sections |
| 314 | like begin/end-module or import/export, have to happen at *both* synterp and |
| 315 | interp time and *in the same order*. |
| 316 | |
| 317 | In order to do so, the synterp-command may need to communicate data to the |
| 318 | corresponding interp-command. There are two ways for doing so. |
| 319 | |
| 320 | The first one is to use, as the main entry points, the following ones: |
| 321 | ``` |
| 322 | pred main-synterp i:list argument, o:any. |
| 323 | pred main-interp i:list argument, i:any. |
| 324 | ``` |
| 325 | Unlike `main` the former outputs a datum while the latter receives it in input. |
| 326 | During the synterp phase the API `coq.synterp-actions` lists the actions |
| 327 | performed so far. An excerpt from the [coq-builtin-synterp](builtin-doc/coq-builtin-synterp.elpi) file: |
| 328 | ``` |
| 329 | % Action executed during the parsing phase (aka synterp) |
| 330 | kind synterp-action type. |
| 331 | type begin-module id -> synterp-action. |
| 332 | type end-module modpath -> synterp-action. |
| 333 | ``` |
| 334 | The synterp-command can output data of that type, but also any other data it |
| 335 | wishes. |
| 336 | |
| 337 | The second way to communicate data is implicit, but limited to synterp actions. |
| 338 | Such synterp actions can be recorded into (nested) groups whose structure is |
| 339 | declared using well-bracketed calls to predicates `coq.begin-synterp-group` |
| 340 | and `coq.end-synterp-group` in the synterp phase. In the interp phase, one can |
| 341 | then use predicate `coq.replay-synterp-action-group` to replay all the synterp |
| 342 | actions of the group with the given name at once. |
| 343 | |
| 344 | In the case where one wishes to interleave code between the actions of a given |
| 345 | group, it is also possible to match the synterp group structure at interp, via |
| 346 | `coq.begin-synterp-group` and `coq.end-synterp-group`. Individual actions that |
| 347 | are contained in the group then need to be replayed individually. |
| 348 | |
| 349 | One can use `coq.replay-next-synterp-actions` to replay all synterp actions |
| 350 | until the next beginning/end of a synterp group. However, this is discouraged |
| 351 | in favour of using groups explicitly, as this is more modular. Code that used |
| 352 | to rely on the now-removed `coq.replay-all-missing-synterp-actions` predicate |
| 353 | can rely on `coq.replay-next-synterp-actions` instead, but this is discouraged |
| 354 | in favour of using groups explicitly) |
| 355 | |
| 356 | ##### Syntax of the `#[phase]` attribute |
| 357 | |
| 358 | - `#[phase="ph"]` where `"ph"` can be `"parsing"`, |
| 359 | `"execution"` or `"both"` |
| 360 | - `#[synterp]` is a shorthand for `#[phase="parsing"]` |
| 361 | - `#[interp]` is a shorthand for `#[phase="execution]` |
| 362 | |
| 363 | </p></details> |
| 364 | |
| 365 | #### Invocation of Elpi code |
| 366 | |
| 367 | <details><summary>(click to expand)</summary> |
| 368 | |
| 369 | - `Elpi <qname> <argument>*.` invokes the `main` predicate of the `<qname>` |
| 370 | program passing a possible empty list of arguments. This is how you invoke a |
| 371 | command. |
| 372 | - `elpi <qname> <argument>*.` invokes the `solve` predicate of the `<qname>` |
| 373 | program passing a possible empty list of arguments and the current goal. This |
| 374 | is how you invoke a tactic. |
| 375 | |
| 376 | - `Elpi Export <qname> [As <other-qname>]` makes it possible to invoke |
| 377 | command `<qname>` (or `<other-qname>` if given) without |
| 378 | the `Elpi` prefix or invoke tactic `<qname>` in the middle of a term just |
| 379 | writing `<qname> args` instead of `ltac:(elpi <qname> args)`. Note that in |
| 380 | the case of tactics, all arguments are considered to be terms. |
| 381 | Moreover, remember that one can use `Tactic Notation` to give the tactic a |
| 382 | better syntax and a shorter name when used in the middle of a proof script. |
| 383 | Commands can declare the behavior of starting/ending a proof by, respectively, |
| 384 | using `#[proof="begin"] Elpi Export ..` and `#[proof="end"] Elpi Export ..`. |
| 385 | Starting the proof can depend on the presence of an attribute, for example |
| 386 | `#[proof(begin_if="interactive")] Elpi Export foo` will make |
| 387 | `foo` not open a proof, while `#[interactive] foo` will open a proof. |
| 388 | See also the `main-interp-proof` and `main-interp-qed` entry points in |
| 389 | [coq-builtin](builtin-doc/coq-builtin.elpi). |
| 390 | |
| 391 | where `<argument>` can be: |
| 392 | |
| 393 | - a number, e.g. `3`, represented in Elpi as `(int 3)` |
| 394 | - a string, e.g. `"foo"` or `bar.baz`, represented in Elpi as `(str "foo")` and |
| 395 | `(str "bar.baz")`. Rocq keywords and symbols are recognized as strings, |
| 396 | eg `=>` requires no quotes. Quotes are necessary if the string contains |
| 397 | a space or a character that is not accepted for qualified identifiers or |
| 398 | if the string is `Definition`, `Axiom`, `Record`, `Structure`, `Inductive`, |
| 399 | `CoInductive`, `Variant` or `Context`. |
| 400 | - a term, e.g. `(3)` or `(f x)`, represented in Elpi as `(trm ...)`. Note that |
| 401 | terms always require parentheses, that is `3` is a number while `(3)` is a Rocq |
| 402 | term and depending on the context could be a natural number |
| 403 | (i.e. `S (S (S O))`) or a `Z` or ... See also the section Terms as arguments |
| 404 | down below, and the syntax for Ltac variables down below. |
| 405 | |
| 406 | Commands also accept the following arguments (the syntax is as close as possible |
| 407 | to the Rocq one: [...] means optional, * means 0 or more). See the `argument` |
| 408 | data type in `coq-builtin.elpi` for their HOAS encoding. See also the section |
| 409 | Terms as arguments down below. |
| 410 | |
| 411 | - `Definition` _name_ _binder_* [`:` _term_] `:=` _term_ |
| 412 | - `Axiom` _name_ `:` _term_ |
| 413 | - [ `Record` | `Structure` ] _name_ _binder_* [`:` _sort_] `:=` [_name_] `{` _name_ `:` _term_ `;` * `}` |
| 414 | - [ `Inductive` | `CoInductive` | `Variant` ] _name_ _binder_* [`|` _binder_*] [`:` _term_] `:=` `|` _name_ _binder_* `:` _term_ * |
| 415 | - `Context` _binder_* |
| 416 | |
| 417 | ##### Ltac Variables |
| 418 | |
| 419 | Tactics also accept Ltac variables as follows: |
| 420 | - `ltac_string:(v)` (for `v` of type `string` or `ident`) |
| 421 | - `ltac_int:(v)` (for `v` of type `int` or `integer`) |
| 422 | - `ltac_term:(v)` (for `v` of type `constr` or `open_constr` or `uconstr` or `hyp`) |
| 423 | - `ltac_open_term:(v)` (for `v` of type `uconstr`) |
| 424 | - `ltac_(string|int|term|open_term)_list:(v)` (for `v` of type `list` of ...) |
| 425 | - `ltac_tactic:(t)` (for `t` of type `tactic`) |
| 426 | - `ltac_attributes:(v)` (for `v` of type `attributes`) |
| 427 | For example: |
| 428 | ```coq |
| 429 | Tactic Notation "tac" string(X) ident(Y) int(Z) hyp(T) constr_list(L) simple_intropattern_list(P) uconstr(U) tactic(TA) := |
| 430 | elpi tac ltac_string:(X) ltac_string:(Y) ltac_int:(Z) ltac_term:(T) ltac_term_list:(L) ltac_tactic:(intros P) ltac_open_term:(U) ltac_tactic:(TA). |
| 431 | ``` |
| 432 | lets one write `tac "a" b 3 H t1 t2 t3 [|m] u ta` in any Ltac context. |
| 433 | Arguments are first interpreted by Ltac according to the types declared |
| 434 | in the tactic notation and then injected in the corresponding Elpi argument. |
| 435 | For example `H` must be an existing hypothesis, since it is typed with |
| 436 | the `hyp` Ltac type, but in Elpi it will appear as a term, eg `trm c0`. |
| 437 | Similarly `t1`, `t2` and `t3` are checked to be well typed and to contain no |
| 438 | unresolved implicit arguments, since this is what the `constr` Ltac type means |
| 439 | If they were typed as `open_constr` or `uconstr`, the last or both checks would |
| 440 | be respectively skipped. In any case they are passed to the Elpi code as `trm ...`. |
| 441 | Both `"a"` and `b` are passed to Elpi as `str ...`. |
| 442 | Argument `U` flagged as `ltac_open_term` can mention free variables. The Elpi |
| 443 | tactic receives `open-trm N F` where `N` is the number of free variables in `U` |
| 444 | and `F` is `fun x1 => ... fun xN => U`. |
| 445 | Argument `TA` is received as `tac T` where `T` is an (opaque) tactic that can |
| 446 | be called via the `coq.ltac.*` APIs. |
| 447 | Finally, `ltac_term:(T)` and `(T)` are *not* synonyms: but the former must be used |
| 448 | when defining tactic notations, the latter when invoking elpi tactics directly. |
| 449 | ``` `(T)``` can be used to pass an open term to `elpi tactic ...`. |
| 450 | |
| 451 | ##### Attributes |
| 452 | |
| 453 | Attributes are supported in both commands and tactics. Examples: |
| 454 | - `#[ att ] Elpi cmd` |
| 455 | - `#[ att ] cmd` for a command `cmd` exported via `Elpi Export cmd` |
| 456 | - `#[ att ] elpi tac` |
| 457 | - `Tactic Notation ... attributes(A) ... := ltac_attributes:(A) elpi tac`. |
| 458 | Due to a parsing conflict in Rocq grammar, at the time of writing this code: |
| 459 | ```coq |
| 460 | Tactic Notation "#[" attributes(A) "]" "tac" := |
| 461 | ltac_attributes:(A) elpi tac. |
| 462 | ``` |
| 463 | has the following limitation: |
| 464 | - `#[ att ] tac.` does not parse |
| 465 | - `(#[ att ] tac).` works |
| 466 | - `idtac; #[ att ] tac.` works |
| 467 | |
| 468 | ##### Terms as arguments |
| 469 | |
| 470 | Since version 1.15, terms passed to Elpi commands code via `(term)` or via a |
| 471 | declaration (like `Record`, `Inductive` ...) are in elaborated format by |
| 472 | default. This means that all Rocq notational facilities are available, like |
| 473 | deep pattern matching, or tactics in terms. |
| 474 | One can use the attribute `#[arguments(raw)]` to declare a command which instead |
| 475 | takes arguments in raw format. In that case, notations are unfolded, |
| 476 | implicit arguments are expanded (holes `_` are added) and lexical analysis is |
| 477 | performed (global names and bound names are identified, holes are applied |
| 478 | to bound names in scope), but deep pattern matching or tactics in terms are not |
| 479 | supported, and in particular type checking/inference is not performed. |
| 480 | Once can use the `coq.typecheck` or `coq.elaborate-skeleton` APIs |
| 481 | to fill in implicit arguments and insert coercions on raw terms. |
| 482 | |
| 483 | Terms passed to Elpi tactics via tactic notations can be forced to be elaborated |
| 484 | beforehand by declaring the parameters to be of type `constr` or `open_constr`. |
| 485 | Arguments of type `uconstr` are passed raw. |
| 486 | |
| 487 | ##### Testing/debugging: |
| 488 | |
| 489 | - `Elpi Query [<qname>] <code>` runs `<code>` in the current program (or in |
| 490 | `<qname>` if specified). |
| 491 | - `Elpi Query [<qname>] <synterp-code> <interp-code>` runs |
| 492 | `<synterp-code>` in the current (synterp) program (or in |
| 493 | `<qname>` if specified) and `<interp-code>` in the current program (or `<qname>`). |
| 494 | - `elpi query [<qname>] <string> <argument>*` runs the `<string>` predicate |
| 495 | (that must have the same signature of the default predicate `solve`). |
| 496 | |
| 497 | </p></details> |
| 498 | |
| 499 | #### Supported features of Gallina (core calculus of Rocq) |
| 500 | |
| 501 | <details><summary>(click to expand)</summary> |
| 502 | |
| 503 | - [x] functional core (fun, forall, match, application, let-in, sorts) |
| 504 | - [x] evars (unification variables) |
| 505 | - [x] single Inductive and CoInductive types (including parameters, non-uniform |
| 506 | parameters, indexes) |
| 507 | - [ ] mutual Inductive and CoInductive types |
| 508 | - [x] fixpoints |
| 509 | - [ ] mutual fixpoints |
| 510 | - [ ] cofixpoints |
| 511 | - [x] primitive records |
| 512 | - [x] primitive projections |
| 513 | - [x] primitive integers |
| 514 | - [x] primitive floats |
| 515 | - [ ] primitive arrays |
| 516 | - [x] universe polymorphism |
| 517 | - [x] modules |
| 518 | - [x] module types |
| 519 | - [x] functor application |
| 520 | - [x] functor definition |
| 521 | |
| 522 | </p></details> |
| 523 | |
| 524 | #### Supported features of Gallina's extensions (extra logical features, APIs) |
| 525 | |
| 526 | <details><summary>(click to expand)</summary> |
| 527 | |
| 528 | Checked boxes are available, unchecked boxes are planned, missing items are not |
| 529 | planned. This is a high level list, for the details |
| 530 | see [coq-builtin](builtin-doc/coq-builtin.elpi). |
| 531 | |
| 532 | - [x] i/o: messages, warnings, errors, Rocq version |
| 533 | - [x] logical environment: read, write, locate |
| 534 | + [x] dependencies between objects |
| 535 | - [x] type classes database: read, write |
| 536 | + [ ] take over resolution |
| 537 | - [x] canonical structures database: read, write |
| 538 | + [ ] take over resolution |
| 539 | - [x] coercions database: read, write |
| 540 | - [x] sections: open, close |
| 541 | - [x] scope management: import, export |
| 542 | - [x] hints: mode, opaque, resolve, strategy |
| 543 | - [x] arguments: implicit, name, scope, simpl |
| 544 | - [x] abbreviations: read, write, locate |
| 545 | - [x] typing and elaboration |
| 546 | - [x] unification |
| 547 | - [x] reduction: `lazy`, `cbv`, `vm`, `native` |
| 548 | - [x] flags for `lazy` and `cbv` |
| 549 | - [x] ltac1: bridge to call ltac1 code, mono and multi-goal tactics |
| 550 | - [x] option system: get, set, add |
| 551 | - [x] pretty printer: boxes, printing width |
| 552 | - [x] attributes: read |
| 553 | |
| 554 | </p></details> |
| 555 | |
| 556 | #### Relevant files |
| 557 | |
| 558 | - [coq-builtin](builtin-doc/coq-builtin.elpi) documents the HOAS encoding of Rocq terms |
| 559 | and the API to access Rocq |
| 560 | - [coq-builtin-synterp](builtin-doc/coq-builtin-synterp.elpi) documents APIs to interact with Rocq at parsing time |
| 561 | - [elpi-buitin](builtin-doc/elpi-builtin.elpi) documents Elpi's standard library, you may |
| 562 | look here for list processing code |
| 563 | - [coq-lib](elpi/coq-lib.elpi) provides some utilities to manipulate Rocq terms; |
| 564 | it is an addendum to coq-builtin |
| 565 | - [elpi-command-template](elpi/elpi-command-template.elpi) provides the pre-loaded code for `Elpi Command` (execution phase) and `Elpi Tactic` |
| 566 | - [elpi-command-template-synterp](elpi/elpi-command-template-synterp.elpi) provides the pre-loaded code for `Elpi Command` (parsing phase) |
| 567 | - [elpi-tactic-template](elpi/elpi-tactic-template.elpi) provides the pre-loaded code for `Elpi Tactic` (note tactics also load [elpi-command-template](elpi/elpi-command-template.elpi)) |
| 568 | |
| 569 | #### Organization of the repository |
| 570 | |
| 571 | The code of the Rocq plugin is at the root of the repository in the [src](src/), |
| 572 | [elpi](elpi/) and [theories](theories/) directories. |
| 573 | |
| 574 | The [apps](apps/) directory contains client applications written in Rocq-Elpi. |
| 575 | |
| 576 | #### License |
| 577 | |
| 578 | Rocq-Elpi is free software under the LGPL v2.1 license or any later version. |
| 579 |