| 1 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%% coq-arg-HOAS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 4 |
|
| 5 |
% This section contains the low level data types linking Coq and elpi. |
| 6 |
% In particular the entry points for commands |
| 7 |
|
| 8 |
|
| 9 |
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 10 |
% Entry points |
| 11 |
% |
| 12 |
% Command and tactic invocation |
| 13 |
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 14 |
|
| 15 |
% Entry point for commands. Eg. "#[att=true] Elpi mycommand foo 3 (f x)." becomes |
| 16 |
% main [str "foo", int 3, trm (app[f,x])] |
| 17 |
% in a context where |
| 18 |
% attributes [attribute "att" (leaf "true")] |
| 19 |
% holds. The encoding of terms is described below. |
| 20 |
% See also the coq.parse-attributes utility. |
| 21 |
external pred main i:list argument. |
| 22 |
|
| 23 |
% Commands with a non-trivial parsing phase can thread data between |
| 24 |
% from the parsing phase to the interp one in an untyped way using the |
| 25 |
% following two entrypoints |
| 26 |
external pred main-interp i:list argument, i:any. |
| 27 |
external pred main-synterp i:list argument, o:any. |
| 28 |
|
| 29 |
% Commands starting/closing proofs have to use the following entrypoints. |
| 30 |
% The minimal working snippet is: |
| 31 |
% |
| 32 |
% main-interp-proof [trm P] _ P GL _ :- |
| 33 |
% coq.typecheck P _ ok, coq.ltac.collect-goals P GL _. |
| 34 |
% main-interp-qed _ _ P _GL _ :- |
| 35 |
% coq.env.add-const "it" P _ _ _. |
| 36 |
% |
| 37 |
% Where GL are the goals to prove, and _GL (unused here) are the same goals |
| 38 |
% with their current proof. The term P can be used as the root proof term, |
| 39 |
% where GL are missing pieces. One is not obliged to use P as is, i.e. he |
| 40 |
% can inspect _GL and declare each proof independenlty, or combine these proofs |
| 41 |
% as he likes and ignore P completely. When a main-interp-qed is used to end |
| 42 |
% a proof stated by a standard Rocq Lemma/Definition statement, the data from |
| 43 |
% the proof opening command is the lemma name, _GL is the empty list and P the |
| 44 |
% proof term. |
| 45 |
external pred main-interp-proof |
| 46 |
i:list argument, % arguments passed to the command |
| 47 |
i:any, % data from the parsing phase (defaults to _) |
| 48 |
o:term, % proof data for the corresponding proof-closing command |
| 49 |
o:list sealed-goal, % the sequents to prove |
| 50 |
o:any. % any data for the corresponding proof-closing command |
| 51 |
external pred main-interp-qed |
| 52 |
i:list argument, % arguments passed to the command |
| 53 |
i:any, % data from the parsing phase (defaults to _) |
| 54 |
i:term, % proof data from the corresponding proof-opening command |
| 55 |
i:list sealed-goal, % the sequents from the corresponding proof-opening command, but with proofs |
| 56 |
i:any. % any data from the corresponding proof-opening command |
| 57 |
|
| 58 |
pred usage. |
| 59 |
external func attributes -> list attribute. |
| 60 |
|
| 61 |
% see coq-lib.elpi for coq.parse-attributes generating the options below |
| 62 |
external pred get-option o:string, o:A. |
| 63 |
|
| 64 |
% The data type of arguments (for commands or tactics) |
| 65 |
kind argument type. |
| 66 |
external symbol int : int -> argument. % Eg. 1 -2. |
| 67 |
external symbol str : string -> argument. % Eg. x "y" z.w. or any Coq keyword/symbol |
| 68 |
external symbol trm : term -> argument. % Eg. (t). |
| 69 |
external symbol open-trm : int -> term -> argument. |
| 70 |
|
| 71 |
% Extra arguments for commands. [Definition], [Axiom], [Record] and [Context] |
| 72 |
% take precedence over the [str] argument above (when not "quoted"). |
| 73 |
% |
| 74 |
% Eg. Record or Inductive |
| 75 |
external symbol indt-decl : indt-decl -> argument. |
| 76 |
% Eg. #[universes(polymorphic,...)] Record or Inductive |
| 77 |
external symbol upoly-indt-decl : indt-decl -> upoly-decl -> argument. |
| 78 |
% Eg. Definition or Axiom (when the body is none) |
| 79 |
external symbol const-decl : id -> option term -> arity -> argument. |
| 80 |
% Eg. #[universes(polymorphic,...)] Definition or Axiom |
| 81 |
external symbol upoly-const-decl : id -> option term -> arity -> upoly-decl -> argument. |
| 82 |
% Eg. Context A (b : A). |
| 83 |
external symbol ctx-decl : context-decl -> argument. |
| 84 |
|
| 85 |
% Declaration of inductive types %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 86 |
|
| 87 |
kind indt-decl type. |
| 88 |
kind indc-decl type. |
| 89 |
kind record-decl type. |
| 90 |
|
| 91 |
% An arity is written, in Coq syntax, as: |
| 92 |
% (x : T1) .. (xn : Tn) : S1 -> ... -> Sn -> U |
| 93 |
% This syntax is used, for example, in the type of an inductive type or |
| 94 |
% in the type of constructors. We call the abstractions on the left of ":" |
| 95 |
% "parameters" while we call the type following the ":" (proper) arity. |
| 96 |
|
| 97 |
% Note: in some contexts, like the type of an inductive type constructor, |
| 98 |
% Coq makes no distinction between these two writings |
| 99 |
% (xn : Tn) : forall y1 : S1, ... and (xn : Tn) (y1 : S1) : ... |
| 100 |
% while Elpi is a bit more restrictive, since it understands user directives |
| 101 |
% such as the implicit status of an arguments (eg, using {} instead of () around |
| 102 |
% the binder), only on parameters. |
| 103 |
% Moreover parameters carry the name given by the user as an "id", while binders |
| 104 |
% in terms only carry it as a "name", an irrelevant pretty pringintg hint (see |
| 105 |
% also the HOAS of terms). A user command can hence only use the names of |
| 106 |
% parameters, and not the names of "forall" quantified variables in the arity. |
| 107 |
% |
| 108 |
% See also the arity->term predicate in coq-lib.elpi |
| 109 |
|
| 110 |
kind arity type. |
| 111 |
external symbol parameter : id -> implicit_kind -> term -> (term -> arity) -> arity = "1". |
| 112 |
external symbol arity : term -> arity. |
| 113 |
|
| 114 |
external symbol parameter : id -> implicit_kind -> term -> (term -> indt-decl) -> indt-decl = "2". |
| 115 |
external symbol inductive : id -> bool -> arity -> (term -> list indc-decl) -> indt-decl. % tt means inductive, ff coinductive |
| 116 |
external symbol minductive : id -> bool -> arity -> (term -> indt-decl) -> indt-decl. % one type of a mutual block |
| 117 |
external symbol mblock : list (list indc-decl) -> indt-decl. % constructor blocks for minductive declarations |
| 118 |
external symbol record : id -> term -> id -> record-decl -> indt-decl. |
| 119 |
|
| 120 |
external symbol constructor : id -> arity -> indc-decl. |
| 121 |
|
| 122 |
external symbol field : field-attributes -> id -> term -> (term -> record-decl) -> record-decl. |
| 123 |
external symbol end-record : record-decl. |
| 124 |
|
| 125 |
% Example. |
| 126 |
% Remark that A is a regular parameter; y is a non-uniform parameter and t |
| 127 |
% also features an index of type bool. |
| 128 |
% |
| 129 |
% Inductive t (A : Type) | (y : nat) : bool -> Type := |
| 130 |
% | K1 (x : A) {n : nat} : S n = y -> t A n true -> t A y true |
| 131 |
% | K2 : t A y false |
| 132 |
% |
| 133 |
% is written |
| 134 |
% |
| 135 |
% (parameter "A" explicit {{ Type }} a\ |
| 136 |
% inductive "t" tt (parameter "y" explicit {{ nat }} _\ |
| 137 |
% arity {{ bool -> Type }}) |
| 138 |
% t\ |
| 139 |
% [ constructor "K1" |
| 140 |
% (parameter "y" explicit {{ nat }} y\ |
| 141 |
% (parameter "x" explicit a x\ |
| 142 |
% (parameter "n" maximal {{ nat }} n\ |
| 143 |
% arity {{ S lp:n = lp:y -> lp:t lp:n true -> lp:t lp:y true }}))) |
| 144 |
% , constructor "K2" |
| 145 |
% (parameter "y" explicit {{ nat }} y\ |
| 146 |
% arity {{ lp:t lp:y false }}) ]) |
| 147 |
% |
| 148 |
% Remark that the uniform parameters are not passed to occurrences of t, since |
| 149 |
% they never change, while non-uniform parameters are both abstracted |
| 150 |
% in each constructor type and passed as arguments to t. |
| 151 |
% |
| 152 |
% The coq.typecheck-indt-decl API can be used to fill in implicit arguments |
| 153 |
% an infer universe constraints in the declaration above (e.g. the hidden |
| 154 |
% argument of "=" in the arity of K1). |
| 155 |
% |
| 156 |
% Note: when and inductive type declaration is passed as an argument to an |
| 157 |
% Elpi command non uniform parameters must be separated from the uniform ones |
| 158 |
% with a | (a syntax introduced in Coq 8.12 and accepted by rocq-elpi since |
| 159 |
% version 1.4, in Coq this separator is optional, but not in Elpi). |
| 160 |
|
| 161 |
% Context declaration (used as an argument to Elpi commands) |
| 162 |
kind context-decl type. |
| 163 |
% Eg. (x : T) or (x := B), body is optional, type may be a variable |
| 164 |
external symbol context-item : id -> implicit_kind -> term -> option term -> (term -> context-decl) -> context-decl. |
| 165 |
external symbol context-end : context-decl. |
| 166 |
|
| 167 |
typeabbrev field-attributes (list field-attribute). |
| 168 |
|
| 169 |
macro @global! :- get-option "coq:locality" "global". |
| 170 |
macro @local! :- get-option "coq:locality" "local". |