| 1 |
(* Generates a module containing all the derived constants. |
| 2 |
|
| 3 |
license: GNU Lesser General Public License Version 2.1 or later |
| 4 |
------------------------------------------------------------------------- *) |
| 5 |
|
| 6 |
|
| 7 |
(* since non-uniform inductive parameters are rarely used and the inference |
| 8 |
code from the kernel is not easily accessible, we require the user to |
| 9 |
be explicit about them, eg Inductive foo U1 U2 | NU1 NU2 := ... *) |
| 10 |
#[export] Set Uniform Inductive Parameters. |
| 11 |
|
| 12 |
(** The derive command |
| 13 |
The derive command can be invoked in two ways. |
| 14 |
- [derive <inductive-type> <prefix>] |
| 15 |
- [derive Inductive <declaration>] |
| 16 |
[derive Record <declaration>] |
| 17 |
|
| 18 |
The first command runs all the derivations on an alerady declared inductive |
| 19 |
type named [<inductive-type>] and all generated constants are named after the |
| 20 |
prefix [<prefix>] (by default the inductive type name followed by [_]). Example: |
| 21 |
|
| 22 |
<< |
| 23 |
derive nat. (* default prefix nat_ *) |
| 24 |
derive nat my_nat_stuff_. |
| 25 |
>> |
| 26 |
|
| 27 |
The second command takes as argument an inductive type declaration, it |
| 28 |
creates a module named after the inductive type and puts inside id both |
| 29 |
the inductive types and the output of the derivations. Example: |
| 30 |
|
| 31 |
<< |
| 32 |
derive Inductive tickle A := stop | more : A -> tickle-> tickle. |
| 33 |
>> |
| 34 |
|
| 35 |
is equivalent to |
| 36 |
|
| 37 |
<< |
| 38 |
Module tickle. |
| 39 |
Inductive tickle A := stop | more : A -> tickle-> tickle. |
| 40 |
derive tickle "". |
| 41 |
End tickle. |
| 42 |
Notation tickle := tickle.tickle. |
| 43 |
Notation stop := tickle.stop. |
| 44 |
Notation more := tickle.more. |
| 45 |
>> |
| 46 |
|
| 47 |
Both commands honor the [#[verbose]] attribute. If set they print all |
| 48 |
the derivations that are run, and if they fail or succeed. |
| 49 |
|
| 50 |
A derivation d can be skipped by using the [#[skip(d)]] attribute. |
| 51 |
A derivation different from d can be skipped [#[only(d)]] attribute. |
| 52 |
|
| 53 |
*) |
| 54 |
|
| 55 |
From elpi.apps.derive.elpi Extra Dependency "derive_hook.elpi" as derive_hook. |
| 56 |
From elpi.apps.derive.elpi Extra Dependency "derive_synterp_hook.elpi" as derive_synterp_hook. |
| 57 |
From elpi.apps.derive.elpi Extra Dependency "derive.elpi" as derive. |
| 58 |
From elpi.apps.derive.elpi Extra Dependency "derive_synterp.elpi" as derive_synterp. |
| 59 |
|
| 60 |
From elpi Require Import elpi. |
| 61 |
|
| 62 |
Elpi Command derive. |
| 63 |
|
| 64 |
#[phase="both"] |
| 65 |
Elpi Accumulate lp:{{ |
| 66 |
% runs P in a context where Coq #[attributes] are parsed |
| 67 |
pred with-attributes i:prop. |
| 68 |
with-attributes P :- |
| 69 |
attributes A, |
| 70 |
coq.parse-attributes A [ |
| 71 |
att "verbose" bool, |
| 72 |
att "only" attmap, |
| 73 |
att "recursive" bool, |
| 74 |
att "prefix" string, |
| 75 |
att "module" string, |
| 76 |
att "no_alias" bool, |
| 77 |
] Opts, !, |
| 78 |
Opts => P. |
| 79 |
|
| 80 |
pred get_name i:indt-decl, o:string. |
| 81 |
get_name (parameter _ _ _ F) N :- pi p\ get_name (F p) N. |
| 82 |
get_name (inductive N _ _ _) N. |
| 83 |
get_name (minductive N _ _ _) N. |
| 84 |
get_name (record N _ _ _) N. |
| 85 |
}}. |
| 86 |
|
| 87 |
#[synterp] Elpi Accumulate File derive_synterp_hook. |
| 88 |
#[synterp] Elpi Accumulate File derive_synterp. |
| 89 |
#[synterp] Elpi Accumulate lp:{{ |
| 90 |
main [str TypeName] :- !, |
| 91 |
with-attributes (derive.main TypeName). |
| 92 |
|
| 93 |
main [indt-decl D] :- !, |
| 94 |
get_name D TypeName, |
| 95 |
with-attributes (derive.main TypeName). |
| 96 |
|
| 97 |
main _. |
| 98 |
}}. |
| 99 |
|
| 100 |
Elpi Accumulate File derive_hook. |
| 101 |
Elpi Accumulate File derive. |
| 102 |
Elpi Accumulate lp:{{ |
| 103 |
main [str I] :- !, |
| 104 |
coq.locate I GR, |
| 105 |
with-attributes (derive.main GR _). |
| 106 |
|
| 107 |
main [indt-decl D] :- !, |
| 108 |
get_name D TypeName, |
| 109 |
with-attributes (derive.decl+main TypeName D). |
| 110 |
|
| 111 |
main _ :- usage. |
| 112 |
|
| 113 |
usage :- |
| 114 |
coq.error "Usage: derive <inductive type/definition>\n\tderive Inductive name Params : Arity := Constructors.". |
| 115 |
}}. |
| 116 |
|
| 117 |
|
| 118 |
Elpi Export derive. |