Skip to content

Commit e8adef6

Browse files
committed
Document builtins by running a program.
1 parent 1ebfe9e commit e8adef6

8 files changed

+3870
-3845
lines changed

builtin-doc/coq-builtin-synterp.elpi

Lines changed: 386 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,386 @@
1+
2+
3+
% -- Misc ---------------------------------------------------------
4+
5+
% [coq.info ...] Prints an info message
6+
external type coq.info variadic any prop.
7+
8+
% [coq.notice ...] Prints a notice message
9+
external type coq.notice variadic any prop.
10+
11+
% [coq.say ...] Prints a notice message
12+
external type coq.say variadic any prop.
13+
14+
% [coq.warn ...] Prints a generic warning message
15+
external type coq.warn variadic any prop.
16+
17+
% [coq.warning Category Name ...]
18+
% Prints a warning message with a Name and Category which can be used
19+
% to silence this warning or turn it into an error. See coqc -w command
20+
% line option
21+
external type coq.warning string -> string -> variadic any prop.
22+
23+
% [coq.error ...] Prints and *aborts* the program. It is a fatal error for
24+
% Elpi and Ltac
25+
external type coq.error variadic any prop.
26+
27+
% [coq.version VersionString Major Minor Patch] Fetches the version of Coq,
28+
% as a string and as 3 numbers
29+
external pred coq.version o:string, o:int, o:int, o:int.
30+
31+
32+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
33+
%%%%%%%%%%%%%%%%%%%%%%%%%%%% coq-arg-HOAS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35+
36+
% This section contains the low level data types linking Coq and elpi.
37+
% In particular the entry points for commands
38+
39+
40+
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41+
% Entry points
42+
%
43+
% Command and tactic invocation
44+
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
45+
46+
% Entry point for commands. Eg. "#[att=true] Elpi mycommand foo 3 (f x)." becomes
47+
% main [str "foo", int 3, trm (app[f,x])]
48+
% in a context where
49+
% attributes [attribute "att" (leaf "true")]
50+
% holds. The encoding of terms is described below.
51+
% See also the coq.parse-attributes utility.
52+
pred main i:list argument.
53+
pred main-interp i:list argument, i:any.
54+
pred main-synterp i:list argument, o:any.
55+
pred usage.
56+
pred attributes o:list attribute.
57+
58+
% see coq-lib.elpi for coq.parse-attributes generating the options below
59+
type get-option string -> A -> prop.
60+
61+
% The data type of arguments (for commands or tactics)
62+
kind argument type.
63+
type int int -> argument. % Eg. 1 -2.
64+
type str string -> argument. % Eg. x "y" z.w. or any Coq keyword/symbol
65+
type trm term -> argument. % Eg. (t).
66+
67+
% Extra arguments for commands. [Definition], [Axiom], [Record] and [Context]
68+
% take precedence over the [str] argument above (when not "quoted").
69+
%
70+
% Eg. Record or Inductive
71+
type indt-decl indt-decl -> argument.
72+
% Eg. #[universes(polymorphic,...)] Record or Inductive
73+
type upoly-indt-decl indt-decl -> upoly-decl -> argument.
74+
type upoly-indt-decl indt-decl -> upoly-decl-cumul -> argument.
75+
% Eg. Definition or Axiom (when the body is none)
76+
type const-decl id -> option term -> arity -> argument.
77+
% Eg. #[universes(polymorphic,...)] Definition or Axiom
78+
type upoly-const-decl id -> option term -> arity -> upoly-decl -> argument.
79+
% Eg. Context A (b : A).
80+
type ctx-decl context-decl -> argument.
81+
82+
% Declaration of inductive types %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83+
84+
kind indt-decl type.
85+
kind indc-decl type.
86+
kind record-decl type.
87+
88+
% An arity is written, in Coq syntax, as:
89+
% (x : T1) .. (xn : Tn) : S1 -> ... -> Sn -> U
90+
% This syntax is used, for example, in the type of an inductive type or
91+
% in the type of constructors. We call the abstractions on the left of ":"
92+
% "parameters" while we call the type following the ":" (proper) arity.
93+
94+
% Note: in some contexts, like the type of an inductive type constructor,
95+
% Coq makes no distinction between these two writings
96+
% (xn : Tn) : forall y1 : S1, ... and (xn : Tn) (y1 : S1) : ...
97+
% while Elpi is a bit more restrictive, since it understands user directives
98+
% such as the implicit status of an arguments (eg, using {} instead of () around
99+
% the binder), only on parameters.
100+
% Moreover parameters carry the name given by the user as an "id", while binders
101+
% in terms only carry it as a "name", an irrelevant pretty pringintg hint (see
102+
% also the HOAS of terms). A user command can hence only use the names of
103+
% parameters, and not the names of "forall" quantified variables in the arity.
104+
%
105+
% See also the arity->term predicate in coq-lib.elpi
106+
107+
type parameter id -> implicit_kind -> term -> (term -> arity) -> arity.
108+
type arity term -> arity.
109+
110+
type parameter id -> implicit_kind -> term -> (term -> indt-decl) -> indt-decl.
111+
type inductive id -> bool -> arity -> (term -> list indc-decl) -> indt-decl. % tt means inductive, ff coinductive
112+
type record id -> term -> id -> record-decl -> indt-decl.
113+
114+
type constructor id -> arity -> indc-decl.
115+
116+
type field field-attributes -> id -> term -> (term -> record-decl) -> record-decl.
117+
type end-record record-decl.
118+
119+
% Example.
120+
% Remark that A is a regular parameter; y is a non-uniform parameter and t
121+
% also features an index of type bool.
122+
%
123+
% Inductive t (A : Type) | (y : nat) : bool -> Type :=
124+
% | K1 (x : A) {n : nat} : S n = y -> t A n true -> t A y true
125+
% | K2 : t A y false
126+
%
127+
% is written
128+
%
129+
% (parameter "A" explicit {{ Type }} a\
130+
% inductive "t" tt (parameter "y" explicit {{ nat }} _\
131+
% arity {{ bool -> Type }})
132+
% t\
133+
% [ constructor "K1"
134+
% (parameter "y" explicit {{ nat }} y\
135+
% (parameter "x" explicit a x\
136+
% (parameter "n" maximal {{ nat }} n\
137+
% arity {{ S lp:n = lp:y -> lp:t lp:n true -> lp:t lp:y true }})))
138+
% , constructor "K2"
139+
% (parameter "y" explicit {{ nat }} y\
140+
% arity {{ lp:t lp:y false }}) ])
141+
%
142+
% Remark that the uniform parameters are not passed to occurrences of t, since
143+
% they never change, while non-uniform parameters are both abstracted
144+
% in each constructor type and passed as arguments to t.
145+
%
146+
% The coq.typecheck-indt-decl API can be used to fill in implicit arguments
147+
% an infer universe constraints in the declaration above (e.g. the hidden
148+
% argument of "=" in the arity of K1).
149+
%
150+
% Note: when and inductive type declaration is passed as an argument to an
151+
% Elpi command non uniform parameters must be separated from the uniform ones
152+
% with a | (a syntax introduced in Coq 8.12 and accepted by coq-elpi since
153+
% version 1.4, in Coq this separator is optional, but not in Elpi).
154+
155+
% Context declaration (used as an argument to Elpi commands)
156+
kind context-decl type.
157+
% Eg. (x : T) or (x := B), body is optional, type may be a variable
158+
type context-item id -> implicit_kind -> term -> option term -> (term -> context-decl) -> context-decl.
159+
type context-end context-decl.
160+
161+
typeabbrev field-attributes (list field-attribute).
162+
163+
macro @global! :- get-option "coq:locality" "global".
164+
macro @local! :- get-option "coq:locality" "local".
165+
166+
167+
% Coq terms are not visible at synterp time, they are always holes
168+
169+
kind term type.
170+
171+
% -- Parsing time APIs
172+
% ----------------------------------------------------
173+
174+
% [id] is a name that matters, we piggy back on Elpi's strings.
175+
% Note: [name] is a name that does not matter.
176+
typeabbrev id string.
177+
178+
179+
% Name of a module /*E*/
180+
typeabbrev modpath (ctype "modpath").
181+
182+
183+
% Name of a module type /*E*/
184+
typeabbrev modtypath (ctype "modtypath").
185+
186+
187+
% [coq.locate-module ModName ModPath] locates a module. It's a fatal error
188+
% if ModName cannot be located. *E*
189+
external pred coq.locate-module i:id, o:modpath.
190+
191+
% [coq.locate-module-type ModName ModPath] locates a module. It's a fatal
192+
% error if ModName cannot be located. *E*
193+
external pred coq.locate-module-type i:id, o:modtypath.
194+
195+
196+
kind located type.
197+
type loc-modpath modpath -> located.
198+
type loc-modtypath modtypath -> located.
199+
200+
201+
% [coq.locate-all Name Located] finds all possible meanings of a string.
202+
% Does not fail.
203+
external pred coq.locate-all i:id, o:list located.
204+
205+
% Coq Module inline directive
206+
kind coq.inline type.
207+
type coq.inline.no coq.inline. % Coq's [no inline] (aka !)
208+
type coq.inline.default coq.inline. % The default, can be omitted
209+
type coq.inline.at int -> coq.inline. % Coq's [inline at <num>]
210+
211+
external pred coq.env.begin-module-functor % Starts a functor *E*
212+
i:id, % The name of the functor
213+
i:option modtypath, % Its module type
214+
i:list (pair id modtypath). % Parameters of the functor
215+
216+
217+
pred coq.env.begin-module i:id, i:option modtypath.
218+
coq.env.begin-module Name MP :-
219+
coq.env.begin-module-functor Name MP [].
220+
221+
222+
% [coq.env.end-module ModPath] end the current module that becomes known as
223+
% ModPath *E*
224+
external pred coq.env.end-module o:modpath.
225+
226+
external pred coq.env.begin-module-type-functor % Starts a module type functor *E*
227+
i:id, % The name of the functor
228+
i:list (pair id modtypath). % The parameters of the functor
229+
230+
231+
pred coq.env.begin-module-type i:id.
232+
coq.env.begin-module-type Name :-
233+
coq.env.begin-module-type-functor Name [].
234+
235+
236+
% [coq.env.end-module-type ModTyPath] end the current module type that
237+
% becomes known as ModPath *E*
238+
external pred coq.env.end-module-type o:modtypath.
239+
240+
external pred coq.env.apply-module-functor % Applies a functor *E*
241+
i:id, % The name of the new module
242+
i:option modtypath, % Its module type
243+
i:modpath, % The functor being applied
244+
i:list modpath, % Its arguments
245+
i:coq.inline, % Arguments inlining
246+
o:modpath. % The modpath of the new module
247+
248+
external pred coq.env.apply-module-type-functor % Applies a type functor *E*
249+
i:id, % The name of the new module type
250+
i:modtypath, % The functor
251+
i:list modpath, % Its arguments
252+
i:coq.inline, % Arguments inlining
253+
o:modtypath. % The modtypath of the new module type
254+
255+
% [coq.env.include-module ModPath Inline] is like the vernacular Include,
256+
% Inline can be omitted *E*
257+
external pred coq.env.include-module i:modpath, i:coq.inline.
258+
259+
% [coq.env.include-module-type ModTyPath Inline] is like the vernacular
260+
% Include Type, Inline can be omitted *E*
261+
external pred coq.env.include-module-type i:modtypath, i:coq.inline.
262+
263+
% [coq.env.import-module ModPath] is like the vernacular Import *E*
264+
external pred coq.env.import-module i:modpath.
265+
266+
% [coq.env.export-module ModPath] is like the vernacular Export *E*
267+
external pred coq.env.export-module i:modpath.
268+
269+
% [coq.env.begin-section Name] starts a section named Name *E*
270+
external pred coq.env.begin-section i:id.
271+
272+
% [coq.env.end-section] end the current section *E*
273+
external pred coq.env.end-section .
274+
275+
% [coq.modpath->path MP FullPath] extract the full kernel name, each
276+
% component is a separate list item
277+
external pred coq.modpath->path i:modpath, o:list string.
278+
279+
% [coq.modtypath->path MTP FullPath] extract the full kernel name, each
280+
% component is a separate list item
281+
external pred coq.modtypath->path i:modtypath, o:list string.
282+
283+
% [coq.modpath->library MP LibraryPath] extract the enclosing module which
284+
% can be Required
285+
external pred coq.modpath->library i:modpath, o:modpath.
286+
287+
% [coq.modtypath->library MTP LibraryPath] extract the enclosing module
288+
% which can be Required
289+
external pred coq.modtypath->library i:modtypath, o:modpath.
290+
291+
% [coq.env.current-path Path] lists the current module path
292+
external pred coq.env.current-path o:list string.
293+
294+
% [coq.env.current-section-path Path] lists the current section path
295+
external pred coq.env.current-section-path o:list string.
296+
297+
% clauses
298+
%
299+
% A clause like
300+
% :name "foo" :before "bar" foo X Y :- bar X Z, baz Z Y
301+
% is represented as
302+
% clause "foo" (before "bar") (pi x y z\ foo x y :- bar x z, baz z y)
303+
% that is exactly what one would load in the context using =>.
304+
%
305+
% The name and the grafting specification can be left unspecified.
306+
kind clause type.
307+
type clause id -> grafting -> prop -> clause.
308+
309+
% Specify if the clause has to be grafted before, grafted after or replace
310+
% a named clause
311+
kind grafting type.
312+
type before id -> grafting.
313+
type after id -> grafting.
314+
type replace id -> grafting.
315+
316+
% Specify to which module the clause should be attached to
317+
kind scope type.
318+
type execution-site scope. % The module inside which the Elpi program is run
319+
type current scope. % The module being defined (see begin/end-module)
320+
type library scope. % The outermost module (carrying the file name)
321+
322+
323+
% see coq.elpi.accumulate-clauses
324+
pred coq.elpi.accumulate i:scope, i:id, i:clause.
325+
coq.elpi.accumulate S N C :- coq.elpi.accumulate-clauses S N [C].
326+
327+
328+
% [coq.elpi.accumulate-clauses Scope DbName Clauses]
329+
% Declare that, once the program is over, the given clauses has to be
330+
% added to the given db (see Elpi Db).
331+
% Clauses usually belong to Coq modules: the Scope argument lets one
332+
% select which module:
333+
% - execution site (default) is the module in which the pogram is
334+
% invoked
335+
% - current is the module currently being constructed (see
336+
% begin/end-module)
337+
% - library is the current file (the module that is named after the file)
338+
% The clauses are visible as soon as the enclosing module is
339+
% Imported.
340+
% Clauses cannot be accumulated inside functors.
341+
% Supported attributes:
342+
% - @local! (default: false, discard at the end of section or module)
343+
% - @global! (default: false, always active, only if Scope is
344+
% execution-site, discouraged)
345+
external pred coq.elpi.accumulate-clauses i:scope, i:id, i:list clause.
346+
347+
% Action executed during the parsing phase (aka synterp)
348+
kind synterp-action type.
349+
type begin-module id -> synterp-action.
350+
type begin-module-type id -> synterp-action.
351+
type begin-section id -> synterp-action.
352+
type end-module modpath -> synterp-action.
353+
type end-module-type modtypath -> synterp-action.
354+
type end-section synterp-action.
355+
type apply-module-functor id -> synterp-action.
356+
type apply-module-type-functor id -> synterp-action.
357+
type include-module modpath -> synterp-action.
358+
type include-module-type modtypath -> synterp-action.
359+
type import-module modpath -> synterp-action.
360+
type export-module modpath -> synterp-action.
361+
362+
% [coq.synterp-actions A] Get the list of actions performed during the
363+
% parsing phase (aka synterp) up to now.
364+
external pred coq.synterp-actions o:list synterp-action.
365+
366+
% [coq.begin-synterp-group ID Group] Create and open a new synterp action
367+
% group with the given name.
368+
external pred coq.begin-synterp-group i:id, o:group.
369+
370+
% [coq.end-synterp-group Group] End the synterp action group Group. Group
371+
% must refer to the most recently openned group.
372+
external pred coq.end-synterp-group i:group.
373+
374+
% Generic attribute value
375+
kind attribute-value type.
376+
type leaf-str string -> attribute-value.
377+
type leaf-loc loc -> attribute-value.
378+
type node list attribute -> attribute-value.
379+
380+
% Generic attribute
381+
kind attribute type.
382+
type attribute string -> attribute-value -> attribute.
383+
384+
385+
386+

0 commit comments

Comments
 (0)