| 1 |
(*| |
| 2 |
|
| 3 |
Tutorial on the HOAS for Coq terms |
| 4 |
********************************** |
| 5 |
|
| 6 |
:author: Enrico Tassi |
| 7 |
|
| 8 |
.. include:: ../etc/tutorial_style.rst |
| 9 |
|
| 10 |
.. |
| 11 |
Elpi is an extension language that comes as a library |
| 12 |
to be embedded into host applications such as Coq. |
| 13 |
|
| 14 |
Elpi is a variant of λProlog enriched with constraints. |
| 15 |
λProlog is a programming language designed to make it easy |
| 16 |
to manipulate abstract syntax trees containing binders. |
| 17 |
Elpi extends λProlog with programming constructs that are |
| 18 |
designed to make it easy to manipulate abstract syntax trees |
| 19 |
containing metavariables (also called unification variables, or |
| 20 |
evars in the Coq jargon). |
| 21 |
|
| 22 |
This software, "coq-elpi", is a Coq plugin embedding Elpi and |
| 23 |
exposing to the extension language Coq specific data types (e.g. terms) |
| 24 |
and API (e.g. to declare a new inductive type). |
| 25 |
|
| 26 |
In order to get proper syntax highlighting using VSCode please install the |
| 27 |
"gares.coq-elpi-lang" extension. In CoqIDE please chose "coq-elpi" in |
| 28 |
Edit -> Preferences -> Colors. |
| 29 |
|
| 30 |
|
| 31 |
This tutorial focuses on the integration of Elpi within Coq, in particular |
| 32 |
it describes how Coq terms are exposed to Elpi programs and how Coq APIs can |
| 33 |
be called. |
| 34 |
|
| 35 |
This tutorial assumes the reader is familiar with Elpi and HOAS; if it is not |
| 36 |
the case, please take a look at the |
| 37 |
`Elpi tutorial <https://lpcic.github.io/coq-elpi/tutorial_elpi_lang.html>`_ |
| 38 |
first. |
| 39 |
|
| 40 |
.. contents:: |
| 41 |
|
| 42 |
================ |
| 43 |
HOAS for Gallina |
| 44 |
================ |
| 45 |
|
| 46 |
|*) |
| 47 |
|
| 48 |
Set Warnings "-elpi.linear-variable". (* .none *) |
| 49 |
From elpi Require Import elpi. (* .none *) |
| 50 |
|
| 51 |
Elpi Command tutorial_HOAS. (* .none *) |
| 52 |
|
| 53 |
(*| |
| 54 |
|
| 55 |
The full syntax of Coq terms can be found in |
| 56 |
`coq-builtin.elpi <https://github.com/LPCIC/coq-elpi/blob/master/builtin-doc/coq-builtin.elpi>`_ |
| 57 |
together with a detailed documentation of the encoding of contexts and the |
| 58 |
APIs one can use to interact with Coq. This tutorial, and the two more |
| 59 |
that focus on commands and tactics, are a gentle introduction to all that. |
| 60 |
|
| 61 |
We defer to later quotations and antiquotations: syntactic features that |
| 62 |
let one write terms in Coq's native syntax. Here we focus on the abstract |
| 63 |
syntax tree of Coq terms. |
| 64 |
|
| 65 |
----------------------- |
| 66 |
Constructor :e:`global` |
| 67 |
----------------------- |
| 68 |
|
| 69 |
Let's start with the :type:`gref` data type (for global reference). |
| 70 |
|
| 71 |
.. code:: elpi |
| 72 |
|
| 73 |
type const constant -> gref. |
| 74 |
type indt inductive -> gref. |
| 75 |
type indc constructor -> gref. |
| 76 |
|
| 77 |
:type:`constant`, :type:`inductive` and :type:`constructor` are Coq specific |
| 78 |
data types that are opaque to Elpi. Still the :type:`gref` data type lets you |
| 79 |
see what these names point to (a constant, and inductive type or a |
| 80 |
constructor). |
| 81 |
|
| 82 |
.. note:: keyword :e:`external symbol` |
| 83 |
|
| 84 |
Starting with Elpi 3.0 symbols like :e:`const`, :e:`indt` and |
| 85 |
:e:`indc` are declared using the keyword :e:`external symbol`, |
| 86 |
rather than :e:`type`, since they are coming from Rocq. |
| 87 |
|
| 88 |
The :builtin:`coq.locate` API resolves a string to a :type:`gref`. |
| 89 |
|
| 90 |
|*) |
| 91 |
|
| 92 |
Elpi Query lp:{{ |
| 93 |
|
| 94 |
coq.locate "nat" GRnat, |
| 95 |
coq.locate "S" GRs, |
| 96 |
coq.locate "plus" GRplus |
| 97 |
|
| 98 |
}}. |
| 99 |
|
| 100 |
(*| |
| 101 |
|
| 102 |
The :e:`coq.env.*` family of APIs lets one access the |
| 103 |
environment of well typed Coq terms that have a global name. |
| 104 |
|
| 105 |
|*) |
| 106 |
|
| 107 |
Definition x := 2. |
| 108 |
|
| 109 |
Elpi Query lp:{{ |
| 110 |
|
| 111 |
coq.locate "x" GR, |
| 112 |
|
| 113 |
% all global references have a type |
| 114 |
coq.env.typeof GR Ty, |
| 115 |
|
| 116 |
% destruct GR to obtain its constant part C |
| 117 |
GR = const C, |
| 118 |
|
| 119 |
% constants may have a body, do have a type |
| 120 |
coq.env.const C (some Bo) TyC |
| 121 |
|
| 122 |
}}. |
| 123 |
|
| 124 |
(*| |
| 125 |
|
| 126 |
An expression like :e:`indt «nat»` is not a Coq term (or better a type) yet. |
| 127 |
|
| 128 |
The :constructor:`global` term constructor turns a :type:`gref` into an |
| 129 |
actual :type:`term`. |
| 130 |
|
| 131 |
.. code:: elpi |
| 132 |
|
| 133 |
type global gref -> term. |
| 134 |
|
| 135 |
---------------------------------- |
| 136 |
Constructors :e:`app` and :e:`fun` |
| 137 |
---------------------------------- |
| 138 |
|
| 139 |
The :constructor:`app` term constructor takes a list of terms and builds |
| 140 |
the (n-ary) application. The first term is the head, while the others |
| 141 |
are the arguments. |
| 142 |
|
| 143 |
For example :e:`app [global (indc «S»), global (indc «O»)]` is |
| 144 |
the representation of `1`. |
| 145 |
|
| 146 |
.. code:: elpi |
| 147 |
|
| 148 |
type app list term -> term. |
| 149 |
|
| 150 |
Let's move to binders! |
| 151 |
|
| 152 |
|*) |
| 153 |
|
| 154 |
Definition f := fun x : nat => x. |
| 155 |
|
| 156 |
Elpi Query lp:{{ |
| 157 |
|
| 158 |
coq.locate "f" (const C), |
| 159 |
coq.env.const C (some Bo) _ |
| 160 |
|
| 161 |
}}. |
| 162 |
|
| 163 |
(*| |
| 164 |
|
| 165 |
The :constructor:`fun` constructor carries a pretty printing hint ```x```, |
| 166 |
the type of the bound variable `nat` and a function describing the body: |
| 167 |
|
| 168 |
.. code:: elpi |
| 169 |
|
| 170 |
type fun name -> term -> (term -> term) -> term. |
| 171 |
|
| 172 |
.. note:: :type:`name` is just for pretty printing: in spite of carrying |
| 173 |
a value in the Coq world, it has no content in Elpi (like the unit type) |
| 174 |
|
| 175 |
Elpi terms of type :type:`name` are just identifiers |
| 176 |
written between ````` (backticks). |
| 177 |
|
| 178 |
.. coq:: |
| 179 |
|
| 180 |
Elpi Query lp:{{ |
| 181 |
|
| 182 |
fun `foo` T B = fun `bar` T B % names don't matter |
| 183 |
|
| 184 |
}}. |
| 185 |
|
| 186 |
API such as :builtin:`coq.name-suffix` lets one craft a family of |
| 187 |
names starting from one, eg ``coq.name-suffix `H` 1 N`` sets :e:`N` |
| 188 |
to ```H1```. |
| 189 |
|
| 190 |
------------------------------------ |
| 191 |
Constructors :e:`fix` and :e:`match` |
| 192 |
------------------------------------ |
| 193 |
|
| 194 |
The other binders :constructor:`prod` (Coq's `forall`, AKA `Π`) and :constructor:`let` are similar, |
| 195 |
so let's rather focus on :constructor:`fix` here. |
| 196 |
|
| 197 |
|*) |
| 198 |
|
| 199 |
Elpi Query lp:{{ |
| 200 |
|
| 201 |
coq.locate "plus" (const C), |
| 202 |
coq.env.const C (some Bo) _ |
| 203 |
|
| 204 |
}}. |
| 205 |
|
| 206 |
(*| |
| 207 |
|
| 208 |
The :constructor:`fix` constructor carries a pretty printing hint, |
| 209 |
the number of the recursive argument (starting at :e:`0`), the type |
| 210 |
of the recursive function and finally the body where the recursive |
| 211 |
call is represented via a bound variable |
| 212 |
|
| 213 |
.. code:: elpi |
| 214 |
|
| 215 |
type fix name -> int -> term -> (term -> term) -> term. |
| 216 |
|
| 217 |
A :constructor:`match` constructor carries the term being inspected, |
| 218 |
the return clause |
| 219 |
and a list of branches. Each branch is a Coq function expecting in input |
| 220 |
the arguments of the corresponding constructor. The order follows the |
| 221 |
order of the constructors in the inductive type declaration. |
| 222 |
|
| 223 |
.. code:: elpi |
| 224 |
|
| 225 |
type match term -> term -> list term -> term. |
| 226 |
|
| 227 |
The return clause is represented as a Coq function expecting in input |
| 228 |
the indexes of the inductive type, the inspected term and generating the |
| 229 |
type of the branches. |
| 230 |
|
| 231 |
|*) |
| 232 |
|
| 233 |
Definition m (h : 0 = 1 ) P : P 0 -> P 1 := |
| 234 |
match h as e in eq _ x return P 0 -> P x |
| 235 |
with eq_refl => fun (p : P 0) => p end. |
| 236 |
|
| 237 |
Elpi Query lp:{{ |
| 238 |
|
| 239 |
coq.locate "m" (const C), |
| 240 |
coq.env.const C (some (fun _ _ h\ fun _ _ p\ match _ (RT h p) _)) _, |
| 241 |
coq.say "The return type of m is:" RT |
| 242 |
|
| 243 |
}}. |
| 244 |
|
| 245 |
|
| 246 |
(*| |
| 247 |
|
| 248 |
--------------------- |
| 249 |
Constructor :e:`sort` |
| 250 |
--------------------- |
| 251 |
|
| 252 |
The last term constructor worth discussing is :constructor:`sort`. |
| 253 |
|
| 254 |
.. code:: elpi |
| 255 |
|
| 256 |
type sort universe -> term. |
| 257 |
type prop universe. |
| 258 |
type typ univ -> universe. |
| 259 |
|
| 260 |
The opaque type :type:`univ` is a universe level variable. Elpi holds a store of |
| 261 |
constraints among these variables and provides APIs named :e:`coq.univ.*` to |
| 262 |
impose constraints. |
| 263 |
|
| 264 |
|*) |
| 265 |
|
| 266 |
Elpi Query lp:{{ |
| 267 |
|
| 268 |
coq.sort.sup U U1, |
| 269 |
coq.say U "<" U1, |
| 270 |
% This constraint can't be allowed in the store! |
| 271 |
not(coq.sort.leq U1 U) |
| 272 |
|
| 273 |
}}. |
| 274 |
|
| 275 |
(*| |
| 276 |
|
| 277 |
.. note:: the user is not expected to declare universe constraints by hand |
| 278 |
|
| 279 |
The type checking primitives update the store of constraints |
| 280 |
automatically and put Coq universe variables in place of Elpi's unification |
| 281 |
variables (:e:`U` and :e:`V` below). |
| 282 |
|
| 283 |
Let's play a bit more with universe constraints using the |
| 284 |
:builtin:`coq.typecheck` API: |
| 285 |
|
| 286 |
|*) |
| 287 |
|
| 288 |
Elpi Query lp:{{ |
| 289 |
|
| 290 |
ID = (fun `x` (sort (typ U)) x\ x), |
| 291 |
A = (sort (typ U)), % the same U as before |
| 292 |
B = (sort (typ V)), |
| 293 |
coq.say "(id b) is:" (app [ID, B]), |
| 294 |
|
| 295 |
% error, since U : U is not valid |
| 296 |
coq.typecheck (app [ID, A]) T (error ErrMsg), |
| 297 |
coq.say "(id a) is illtyped:" ErrMsg, |
| 298 |
|
| 299 |
% ok, since V : U is possible |
| 300 |
coq.typecheck (app [ID, B]) T ok, |
| 301 |
|
| 302 |
% remark: U and V are now Coq's univ with constraints |
| 303 |
coq.say "after typing (id b) is:" (app [ID, B]) ":" T, |
| 304 |
coq.univ.print |
| 305 |
|
| 306 |
}}. |
| 307 |
|
| 308 |
(*| |
| 309 |
|
| 310 |
The :stdtype:`diagnostic` data type is used by :builtin:`coq.typecheck` to |
| 311 |
tell if the term is well typed. The constructor :e:`ok` signals success, while |
| 312 |
:e:`error` carries an error message. In case of success universe constraints |
| 313 |
are added to the store. |
| 314 |
|
| 315 |
============================= |
| 316 |
Quotations and Antiquotations |
| 317 |
============================= |
| 318 |
|
| 319 |
Writing Gallina terms as we did so far is surely possible but very verbose |
| 320 |
and unhandy. Elpi provides a system of quotations and antiquotations to |
| 321 |
let one take advantage of the Coq parser to write terms. |
| 322 |
|
| 323 |
The antiquotation, from Coq to Elpi, is written `lp:{{ ... }}` and we have |
| 324 |
been using it since the beginning of the tutorial. The quotation from |
| 325 |
Elpi to Coq is written :e:`{{:coq ... }}` or also just :e:`{{ ... }}` since |
| 326 |
the `:coq` is the default quotation (Coq has no default quotation, hence you always need |
| 327 |
to write `lp:` there). |
| 328 |
|
| 329 |
|*) |
| 330 |
|
| 331 |
Elpi Query lp:{{ |
| 332 |
|
| 333 |
% the ":coq" flag is optional |
| 334 |
coq.say {{:coq 1 + 2 }} "=" {{ 1 + 2 }} |
| 335 |
|
| 336 |
}}. |
| 337 |
|
| 338 |
(*| |
| 339 |
|
| 340 |
Of course quotations can nest. |
| 341 |
|
| 342 |
|*) |
| 343 |
|
| 344 |
Elpi Query lp:{{ |
| 345 |
|
| 346 |
coq.locate "S" S, |
| 347 |
coq.say {{ 1 + lp:{{ app[global S, {{ 0 }} ] }} }} |
| 348 |
% elpi.... coq.. elpi........... coq elpi coq |
| 349 |
|
| 350 |
}}. |
| 351 |
|
| 352 |
(*| |
| 353 |
|
| 354 |
One rule governs bound variables: |
| 355 |
|
| 356 |
.. important:: |
| 357 |
|
| 358 |
if a variable is bound in a language, Coq or Elpi, |
| 359 |
then the variable is only visible in that language (not in the other one). |
| 360 |
|
| 361 |
The following example is horrible but proves this point. In real code |
| 362 |
you are encouraged to pick appropriate names for your variables, avoiding |
| 363 |
gratuitous (visual) clashes. |
| 364 |
|
| 365 |
|*) |
| 366 |
|
| 367 |
Elpi Query lp:{{ |
| 368 |
|
| 369 |
coq.say (fun `x` {{nat}} x\ {{ fun x : nat => x + lp:{{ x }} }}) |
| 370 |
% e c c e |
| 371 |
}}. |
| 372 |
|
| 373 |
(*| |
| 374 |
|
| 375 |
A commodity quotation without parentheses let's one quote identifiers |
| 376 |
omitting the curly braces. |
| 377 |
That is `lp:{{ ident }}` can be written just `lp:ident`. |
| 378 |
|
| 379 |
|*) |
| 380 |
|
| 381 |
|
| 382 |
Elpi Query lp:{{ |
| 383 |
|
| 384 |
coq.say (fun `x` {{nat}} x\ {{ fun x : nat => x + lp:x }}) |
| 385 |
% e c c e |
| 386 |
}}. |
| 387 |
|
| 388 |
(*| |
| 389 |
|
| 390 |
It is quite frequent to put Coq variables in the scope of an Elpi |
| 391 |
unification variable, and this can be done by simply writing |
| 392 |
`lp:(X a b)` which is a shorthand for `lp:{{ X {{ a }} {{ b }} }}`. |
| 393 |
|
| 394 |
.. warning:: writing `lp:X a b` (without parentheses) would result in a |
| 395 |
Coq application, not an Elpi one |
| 396 |
|
| 397 |
Let's play a bit with these shorthands: |
| 398 |
|
| 399 |
|*) |
| 400 |
|
| 401 |
Elpi Query lp:{{ |
| 402 |
|
| 403 |
X = (x\y\ {{ lp:y + lp:x }}), % x and y live in Elpi |
| 404 |
|
| 405 |
coq.say {{ fun a b : nat => lp:(X a b) }} % a and b live in Coq |
| 406 |
|
| 407 |
}}. |
| 408 |
|
| 409 |
(*| |
| 410 |
|
| 411 |
Another commodity quotation lets one access the coqlib |
| 412 |
feature introduced in Coq 8.10. |
| 413 |
|
| 414 |
Rocqlib gives you an indirection between your code and the actual name |
| 415 |
of constants. |
| 416 |
|
| 417 |
|*) |
| 418 |
|
| 419 |
Register Corelib.Init.Datatypes.nat as my.N. |
| 420 |
Register Corelib.Init.Logic.eq as my.eq. |
| 421 |
|
| 422 |
Elpi Query lp:{{ |
| 423 |
|
| 424 |
coq.say {{ fun a b : lib:my.N => lib:@my.eq lib:my.N a b }} |
| 425 |
|
| 426 |
}}. |
| 427 |
|
| 428 |
(*| |
| 429 |
|
| 430 |
.. note:: The (optional) `@` in `lib:@some.name` disables implicit arguments. |
| 431 |
|
| 432 |
The `{{:gref .. }}` quotation lets one build the gref data type, instead of the |
| 433 |
term one. It supports `lib:` as well. |
| 434 |
|
| 435 |
|*) |
| 436 |
|
| 437 |
Elpi Query lp:{{ |
| 438 |
|
| 439 |
coq.say {{:gref nat }}, |
| 440 |
coq.say {{:gref lib:my.N }}. |
| 441 |
|
| 442 |
}}. |
| 443 |
|
| 444 |
(*| |
| 445 |
|
| 446 |
The last thing to keep in mind when using quotations is that implicit |
| 447 |
arguments are inserted (according to the `Arguments` setting in Coq) |
| 448 |
but not synthesized automatically. |
| 449 |
|
| 450 |
It is the job of the type checker or elaborator to synthesize them. |
| 451 |
We shall see more on this in the section on `holes`_. |
| 452 |
|
| 453 |
|*) |
| 454 |
|
| 455 |
Elpi Query lp:{{ |
| 456 |
|
| 457 |
T = (fun `ax` {{nat}} a\ {{ fun b : nat => lp:a = b }}), |
| 458 |
coq.say "before:" T, |
| 459 |
coq.typecheck T _ ok, |
| 460 |
coq.say "after:" T |
| 461 |
|
| 462 |
}}. |
| 463 |
|
| 464 |
(*| |
| 465 |
|
| 466 |
=========== |
| 467 |
The context |
| 468 |
=========== |
| 469 |
|
| 470 |
The context of Elpi (the hypothetical program made of rules loaded |
| 471 |
via :e:`=>`) is taken into account by the Coq APIs. In particular every time |
| 472 |
a bound variable is crossed, the programmer *must* load in the context a |
| 473 |
rule attaching to that variable a type. There are a few facilities to |
| 474 |
do that, but let's first see what happens if one forgets it. |
| 475 |
|
| 476 |
|*) |
| 477 |
|
| 478 |
Fail Elpi Query lp:{{ |
| 479 |
|
| 480 |
T = {{ fun x : nat => x + 1 }}, |
| 481 |
coq.typecheck T _ ok, |
| 482 |
T = fun _ _ Bo, |
| 483 |
pi x\ |
| 484 |
coq.typecheck (Bo x) _ _ |
| 485 |
|
| 486 |
}}. (* .fails *) |
| 487 |
|
| 488 |
(*| |
| 489 |
|
| 490 |
This fatal error says that :e:`x` in :e:`(Bo x)` is unknown to Coq. |
| 491 |
It is |
| 492 |
a variable postulated in Elpi, but it's type, `nat`, was lost. There |
| 493 |
is nothing wrong per se in using :e:`pi x\ ` as we did if we don't call Coq |
| 494 |
APIs under it. But if we do, we have to record the type of :e:`x` somewhere. |
| 495 |
|
| 496 |
In some sense Elpi's way of traversing a binder is similar to a Zipper. |
| 497 |
The context of Elpi must record the part of the Zipper context that is |
| 498 |
relevant for binders. |
| 499 |
|
| 500 |
The two predicates :builtin:`decl` and :builtin:`def` are used |
| 501 |
for that purpose: |
| 502 |
|
| 503 |
.. code:: elpi |
| 504 |
|
| 505 |
func decl term -> name, term. % Var Name Ty |
| 506 |
func def term -> name, term, term. % Var Name Ty Bo |
| 507 |
|
| 508 |
where :e:`def` is used to cross a :e:`let`. |
| 509 |
|
| 510 |
|*) |
| 511 |
|
| 512 |
Elpi Query lp:{{ |
| 513 |
|
| 514 |
T = {{ fun x : nat => x + 1 }}, |
| 515 |
coq.typecheck T _ ok, |
| 516 |
T = fun N Ty Bo, |
| 517 |
pi x\ |
| 518 |
decl x N Ty ==> |
| 519 |
coq.typecheck (Bo x) _ ok |
| 520 |
|
| 521 |
}}. |
| 522 |
|
| 523 |
(*| |
| 524 |
|
| 525 |
In order to ease this task, Rocq-Elpi provides a few commodity macros such as |
| 526 |
`@pi-decl`: |
| 527 |
|
| 528 |
.. code:: elpi |
| 529 |
|
| 530 |
macro @pi-decl N T F :- pi x\ decl x N T ==> F x. |
| 531 |
|
| 532 |
.. note:: the precedence of lambda abstraction :e:`x\ ` lets you write the |
| 533 |
following code without parentheses for :e:`F`. |
| 534 |
|
| 535 |
|*) |
| 536 |
|
| 537 |
Elpi Query lp:{{ |
| 538 |
|
| 539 |
T = {{ fun x : nat => x + 1 }}, |
| 540 |
coq.typecheck T _ ok, |
| 541 |
T = fun N Ty Bo, |
| 542 |
@pi-decl N Ty x\ |
| 543 |
coq.typecheck (Bo x) _ ok |
| 544 |
|
| 545 |
}}. |
| 546 |
|
| 547 |
(*| |
| 548 |
|
| 549 |
.. tip:: :e:`@pi-decl N Ty x\ ` takes arguments in the same order of :constructor:`fun` and |
| 550 |
:constructor:`prod`, while |
| 551 |
:e:`@pi-def N Ty Bo x\ ` takes arguments in the same order of :constructor:`let`. |
| 552 |
|
| 553 |
.. _holes: |
| 554 |
|
| 555 |
========================== |
| 556 |
Holes (implicit arguments) |
| 557 |
========================== |
| 558 |
|
| 559 |
An "Evar" (Coq slang for existentially quantified meta variable) is |
| 560 |
represented as a Elpi unification variable and a typing constraint. |
| 561 |
|
| 562 |
|*) |
| 563 |
|
| 564 |
Elpi Query lp:{{ |
| 565 |
|
| 566 |
T = {{ _ }}, |
| 567 |
coq.say "raw T =" T, |
| 568 |
coq.sigma.print, |
| 569 |
coq.say "--------------------------------", |
| 570 |
coq.typecheck T {{ nat }} ok, |
| 571 |
coq.sigma.print |
| 572 |
|
| 573 |
}}. |
| 574 |
|
| 575 |
(*| |
| 576 |
|
| 577 |
Before the call to :builtin:`coq.typecheck`, :builtin:`coq.sigma.print` |
| 578 |
prints nothing interesting, while after the call it also prints the following |
| 579 |
syntactic constraint: |
| 580 |
|
| 581 |
.. mquote:: .s(Elpi).msg(suspended on X0) |
| 582 |
:language: elpi |
| 583 |
|
| 584 |
which indicates that the hole :e:`X0` is linked to a Coq evar |
| 585 |
and is expected to have type `nat`. |
| 586 |
|
| 587 |
Now the bijective mapping from Coq evars to Elpi's unification variables |
| 588 |
is not empty anymore: |
| 589 |
|
| 590 |
.. mquote:: .s(Elpi).msg{Rocq-Elpi mapping:*[?]X11 <-> X0*} |
| 591 |
:language: text |
| 592 |
|
| 593 |
Note that Coq's evar identifiers are of the form `?X<n>`, while the Elpi ones |
| 594 |
have no leading `?`. The Coq Evar map says that `?X11` has type `nat`: |
| 595 |
|
| 596 |
.. mquote:: .s(Elpi).msg{EVARS:*[?]X11==[[] |- nat[]]*} |
| 597 |
:language: text |
| 598 |
|
| 599 |
The intuition is that Coq's Evar map (AKA sigma or evd), which assigns |
| 600 |
typing judgement to evars, is represented with Elpi constraints which carry |
| 601 |
the same piece of info. |
| 602 |
|
| 603 |
Naked Elpi unification variables, when passed to Coq's API, are |
| 604 |
automatically linked to a Coq evar. We postpone the explanation of the |
| 605 |
difference "raw" and "elab" unification variables to the chapter about |
| 606 |
tactics, here the second copy of :e:`X0` in the evar constraint plays |
| 607 |
no role. |
| 608 |
|
| 609 |
Now, what about the typing context? |
| 610 |
|
| 611 |
|*) |
| 612 |
|
| 613 |
Elpi Query lp:{{ |
| 614 |
|
| 615 |
T = {{ fun x : nat => x + _ }}, |
| 616 |
coq.say "raw T =" T, |
| 617 |
T = fun N Ty Bo, |
| 618 |
@pi-decl N Ty x\ |
| 619 |
coq.typecheck (Bo x) {{ nat }} ok, |
| 620 |
coq.sigma.print. |
| 621 |
|
| 622 |
}}. |
| 623 |
|
| 624 |
(*| |
| 625 |
|
| 626 |
In the value of raw :e:`T` we can see that the hole in `x + _`, which occurs under the |
| 627 |
binder :e:`c0\ `, is represented by an Elpi unification variable :e:`X0 c0`, that |
| 628 |
means that :e:`X0` sees :e:`c0` (:e:`c0` is in the scope of :e:`X0`). |
| 629 |
|
| 630 |
The constraint is this time a bit more complex. Let's dissect it: |
| 631 |
|
| 632 |
.. mquote:: .s(Elpi).msg(suspended on X0) |
| 633 |
:language: elpi |
| 634 |
|
| 635 |
Here `{...}` is the set of names (not necessarily minimized) used in the |
| 636 |
constraint, while `?-` separates the assumptions (the context) from the |
| 637 |
conclusion (the suspended goal). |
| 638 |
|
| 639 |
The mapping between Coq and Elpi is: |
| 640 |
|
| 641 |
.. mquote:: .s(Elpi).msg{Rocq-Elpi mapping:*[?]X13 <-> X0*} |
| 642 |
:language: text |
| 643 |
|
| 644 |
where `?X13` can be found in Coq's sigma: |
| 645 |
|
| 646 |
.. mquote:: .s(Elpi).msg{EVARS:*[?]X13==[[]x |- nat[]]*} |
| 647 |
:language: text |
| 648 |
|
| 649 |
As expected both Elpi's constraint and Coq's evar map record a context |
| 650 |
with a variable :e:`x` (of type `nat`) which is in the scope of the hole. |
| 651 |
|
| 652 |
Unless one is writing a tactic, Elpi's constraints are just used to |
| 653 |
represent the evar map. When a term is assigned to a variable |
| 654 |
the corresponding constraint is dropped. When one is writing a tactic, |
| 655 |
things are wired up so that assigning a term to an Elpi variable |
| 656 |
representing an evar resumes a type checking goal to ensure the term has |
| 657 |
the expected type. |
| 658 |
We will explain this in detail in the tutorial about tactics. |
| 659 |
|
| 660 |
---------------------------- |
| 661 |
Outside the pattern fragment |
| 662 |
---------------------------- |
| 663 |
|
| 664 |
This encoding of evars is such that the programmer does not need to care |
| 665 |
much about them: no need to carry around an assignment/typing map like the |
| 666 |
Evar map, no need to declare new variables there, etc. The programmer |
| 667 |
can freely call Coq API passing an Elpi term containing holes. |
| 668 |
|
| 669 |
There is one limitation, though. The rest of this tutorial describes it |
| 670 |
and introduces a few APIs and options to deal with it. |
| 671 |
|
| 672 |
The limitation is that the automatic declaration and mapping |
| 673 |
does not work in all situations. In particular it only works for Elpi |
| 674 |
unification variables which are in the pattern fragment, which mean |
| 675 |
that they are applied only to distinct names (bound variables). |
| 676 |
|
| 677 |
This is the case for all the `{{ _ }}` one writes inside quotations, for |
| 678 |
example, but it is not hard to craft a term outside this fragment. |
| 679 |
In particular we can use Elpi's substitution (function application) to |
| 680 |
put an arbitrary term in place of a bound variable. |
| 681 |
|
| 682 |
|*) |
| 683 |
|
| 684 |
Fail Elpi Query lp:{{ |
| 685 |
|
| 686 |
T = {{ fun x : nat => x + _ }}, |
| 687 |
% remark the hole sees x |
| 688 |
T = fun N Ty Bo, |
| 689 |
% 1 is the offending term we put in place of x |
| 690 |
Bo1 = Bo {{ 1 }}, |
| 691 |
% Bo1 is outside the pattern fragment |
| 692 |
coq.say "Bo1 (not in pattern fragment) =" Bo1, |
| 693 |
% boom |
| 694 |
coq.typecheck Bo1 {{ nat }} ok. |
| 695 |
|
| 696 |
}}. (* .fails *) |
| 697 |
|
| 698 |
(*| |
| 699 |
|
| 700 |
This snippet fails hard, with the following message: |
| 701 |
|
| 702 |
.. mquote:: .s(Elpi).msg(Flexible term outside) |
| 703 |
:language: elpi |
| 704 |
|
| 705 |
Indeed :e:`Bo1` contains a term outside the pattern fragment, |
| 706 |
the second argument of `plus`, which is obtained by replacing |
| 707 |
:e:`c0` with `{{ 1 }}` in :e:`X0 c0`. |
| 708 |
|
| 709 |
While programming Coq extensions in Elpi, it may happen that we want to |
| 710 |
use a Coq term as a syntax tree (with holes) and we need to apply |
| 711 |
substitutions to it but we don't really care about the scope of holes. |
| 712 |
We would like these holes to stay `{{ _ }}` (a fresh hole which sees the |
| 713 |
entire context of bound variables). In some sense, we would like `{{ _ }}` |
| 714 |
to be a special dummy constant, to be turned into an actual hole on the |
| 715 |
fly when needed. |
| 716 |
|
| 717 |
This use case is perfectly legitimate and is supported by all APIs taking |
| 718 |
terms in input thanks to the :macro:`@holes!` option. |
| 719 |
|
| 720 |
|*) |
| 721 |
|
| 722 |
Elpi Query lp:{{ |
| 723 |
|
| 724 |
T = {{ fun x : nat => x + _ }}, |
| 725 |
T = fun N Ty Bo, |
| 726 |
Bo1 = Bo {{ 1 }}, |
| 727 |
coq.say "Bo1 before =" Bo1, |
| 728 |
% by loading this rule in the context, we set |
| 729 |
% the option for the APIs called under it |
| 730 |
(@holes! ==> coq.typecheck Bo1 {{ nat }} ok), |
| 731 |
coq.say "Bo1 after =" Bo1. |
| 732 |
|
| 733 |
}}. |
| 734 |
|
| 735 |
(*| |
| 736 |
|
| 737 |
Note that after the call to :builtin:`coq.typecheck`, :e:`X0` is assigned the |
| 738 |
term :e:`_\ X1`, that means that the offending argument has been pruned |
| 739 |
(discarded). |
| 740 |
|
| 741 |
.. note:: All APIs taking a term support the :macro:`@holes!` option. |
| 742 |
|
| 743 |
In addition to the :macro:`@holes!` option, there is a class of APIs which can |
| 744 |
deal with terms outside the pattern fragment. These APIs take in input a term |
| 745 |
*skeleton*. A skeleton is not modified in place, as :builtin:`coq.typecheck` |
| 746 |
does with its first argument, but is rather elaborated to a term related to it. |
| 747 |
|
| 748 |
In some sense APIs taking a skeleton are more powerful, because they can |
| 749 |
modify the structure of the term, eg. insert a coercions, but are less |
| 750 |
precise, in the sense that the relation between the input and the output |
| 751 |
terms is not straightforward (it's not unification). |
| 752 |
|
| 753 |
|*) |
| 754 |
|
| 755 |
Coercion nat2bool n := match n with O => false | _ => true end. |
| 756 |
Open Scope bool_scope. |
| 757 |
|
| 758 |
Elpi Query lp:{{ |
| 759 |
|
| 760 |
T = {{ fun x : nat => x && _ }}, |
| 761 |
T = fun N Ty Bo, |
| 762 |
Bo1 = Bo {{ 1 }}, |
| 763 |
coq.elaborate-skeleton Bo1 {{ bool }} Bo2 ok |
| 764 |
|
| 765 |
}}. |
| 766 |
|
| 767 |
(*| |
| 768 |
|
| 769 |
Here :e:`Bo2` is obtained by taking :e:`Bo1`, considering all |
| 770 |
unification variables as holes and all `{{ Type }}` levels as fresh |
| 771 |
(the are none in this example), and running Coq's elaborator on it. |
| 772 |
|
| 773 |
The result is a term with a similar structure (skeleton), but a coercion |
| 774 |
is inserted to make :e:`x` fit as a boolean value, and a fresh hole :e:`X1` is |
| 775 |
put in place of the term :e:`X0 (app [global (indc «S»), global (indc «O»)])` |
| 776 |
which is left untouched. |
| 777 |
|
| 778 |
Skeletons and their APIs are described in more details in the tutorial |
| 779 |
on commands. |
| 780 |
|
| 781 |
That is all for this tutorial. You can continue by reading the tutorial |
| 782 |
about |
| 783 |
`commands <https://lpcic.github.io/coq-elpi/tutorial_coq_elpi_command.html>`_ |
| 784 |
or the one about |
| 785 |
`tactics <https://lpcic.github.io/coq-elpi/tutorial_coq_elpi_tactic.html>`_. |
| 786 |
|
| 787 |
|*) |