1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
(************************************************************************)
(* v * The Coq Proof Assistant / The Coq Development Team *)
(* <O___,, * INRIA - CNRS - LIX - LRI - PPS - Copyright 1999-2017 *)
(* \VV/ **************************************************************)
(* // * This file is distributed under the terms of the *)
(* * GNU Lesser General Public License Version 2.1 *)
(************************************************************************)
open Q_util
let loc = Ploc.dummy
IFDEF STRICT THEN
let ploc_vala x = Ploc.VaVal x
ELSE
let ploc_vala x = x
END
let declare_str_items loc l =
MLast.StDcl (loc, ploc_vala l) (* correspond to <:str_item< declare $list:l'$ end >> *)
let mk_extraarg loc s = <:expr< $lid:"wit_"^s$ >>
let rec make_wit loc = function
| ListArgType t -> <:expr< Genarg.wit_list $make_wit loc t$ >>
| OptArgType t -> <:expr< Genarg.wit_opt $make_wit loc t$ >>
| PairArgType (t1,t2) ->
<:expr< Genarg.wit_pair $make_wit loc t1$ $make_wit loc t2$ >>
| ExtraArgType s -> mk_extraarg loc s
let is_self s = function
| ExtraArgType s' -> s = s'
| _ -> false
let make_rawwit loc arg = <:expr< Genarg.rawwit $make_wit loc arg$ >>
let make_globwit loc arg = <:expr< Genarg.glbwit $make_wit loc arg$ >>
let make_topwit loc arg = <:expr< Genarg.topwit $make_wit loc arg$ >>
let make_act loc act pil =
let rec make = function
| [] -> <:expr< (fun loc -> $act$) >>
| ExtNonTerminal (_, None) :: tl -> <:expr< (fun $lid:"_"$ -> $make tl$) >>
| ExtNonTerminal (_, Some p) :: tl -> <:expr< (fun $lid:p$ -> $make tl$) >>
| ExtTerminal _ :: tl ->
<:expr< (fun _ -> $make tl$) >> in
make (List.rev pil)
let make_prod_item = function
| ExtTerminal s -> <:expr< Extend.Atoken (CLexer.terminal $mlexpr_of_string s$) >>
| ExtNonTerminal (g, _) ->
let base s = <:expr< $lid:s$ >> in
mlexpr_of_prod_entry_key base g
let rec make_prod = function
| [] -> <:expr< Extend.Stop >>
| item :: prods -> <:expr< Extend.Next $make_prod prods$ $make_prod_item item$ >>
let make_rule loc (prods,act) =
<:expr< Extend.Rule $make_prod (List.rev prods)$ $make_act loc act prods$ >>
let is_ident x = function
| <:expr< $lid:s$ >> -> (s : string) = x
| _ -> false
let make_extend loc s cl wit = match cl with
| [[ExtNonTerminal (Uentry e, Some id)], act] when is_ident id act ->
(** Special handling of identity arguments by not redeclaring an entry *)
<:str_item<
value $lid:s$ =
let () = Pcoq.register_grammar $wit$ $lid:e$ in
$lid:e$
>>
| _ ->
let se = mlexpr_of_string s in
let rules = mlexpr_of_list (make_rule loc) (List.rev cl) in
<:str_item<
value $lid:s$ =
let $lid:s$ = Pcoq.create_generic_entry Pcoq.utactic $se$ (Genarg.rawwit $wit$) in
let () = Pcoq.grammar_extend $lid:s$ None (None, [(None, None, $rules$)]) in
$lid:s$ >>
let warning_redundant prefix s =
Printf.eprintf "Redundant [%sTYPED AS] clause in [ARGUMENT EXTEND %s].\n%!" prefix s
let get_type prefix s = function
| None -> None
| Some typ ->
if is_self s typ then
let () = warning_redundant prefix s in None
else Some typ
let check_type prefix s = function
| None -> ()
| Some _ -> warning_redundant prefix s
let declare_tactic_argument loc s (typ, f, g, h) cl =
let se = mlexpr_of_string s in
let rawtyp, rawpr, globtyp, globpr, typ, pr = match typ with
| `Uniform (typ, pr) ->
let typ = get_type "" s typ in
typ, pr, typ, pr, typ, pr
| `Specialized (a, rpr, c, gpr, e, tpr) ->
(** Check that we actually need the TYPED AS arguments *)
let rawtyp = get_type "RAW_" s a in
let glbtyp = get_type "GLOB_" s c in
let toptyp = get_type "" s e in
let () = match g with None -> () | Some _ -> check_type "RAW_" s rawtyp in
let () = match f, h with Some _, Some _ -> check_type "GLOB_" s glbtyp | _ -> () in
rawtyp, rpr, glbtyp, gpr, toptyp, tpr
in
let glob = match g with
| None ->
begin match rawtyp with
| None -> <:expr< fun ist v -> (ist, v) >>
| Some rawtyp ->
<:expr< fun ist v ->
let ans = out_gen $make_globwit loc rawtyp$
(Tacintern.intern_genarg ist
(Genarg.in_gen $make_rawwit loc rawtyp$ v)) in
(ist, ans) >>
end
| Some f ->
<:expr< fun ist v -> (ist, $lid:f$ ist v) >>
in
let interp = match f with
| None ->
begin match globtyp with
| None ->
let typ = match globtyp with None -> ExtraArgType s | Some typ -> typ in
<:expr< fun ist v -> Ftactic.return (Geninterp.Val.inject (Geninterp.val_tag $make_topwit loc typ$) v) >>
| Some globtyp ->
<:expr< fun ist x ->
Tacinterp.interp_genarg ist (Genarg.in_gen $make_globwit loc globtyp$ x) >>
end
| Some f ->
(** Compatibility layer, TODO: remove me *)
let typ = match globtyp with None -> ExtraArgType s | Some typ -> typ in
<:expr<
let f = $lid:f$ in
fun ist v -> Ftactic.nf_enter (fun gl ->
let (sigma, v) = Tacmach.New.of_old (fun gl -> f ist gl v) gl in
let v = Geninterp.Val.inject (Geninterp.val_tag $make_topwit loc typ$) v in
Proofview.tclTHEN (Proofview.Unsafe.tclEVARS sigma) (Ftactic.return v)
)
>> in
let subst = match h with
| None ->
begin match globtyp with
| None -> <:expr< fun s v -> v >>
| Some globtyp ->
<:expr< fun s x ->
out_gen $make_globwit loc globtyp$
(Tacsubst.subst_genarg s
(Genarg.in_gen $make_globwit loc globtyp$ x)) >>
end
| Some f -> <:expr< $lid:f$>> in
let dyn = match typ with
| None -> <:expr< None >>
| Some typ -> <:expr< Some (Geninterp.val_tag $make_topwit loc typ$) >>
in
let wit = <:expr< $lid:"wit_"^s$ >> in
declare_str_items loc
[ <:str_item< value ($lid:"wit_"^s$) = Genarg.make0 $se$ >>;
<:str_item< Genintern.register_intern0 $wit$ $glob$ >>;
<:str_item< Genintern.register_subst0 $wit$ $subst$ >>;
<:str_item< Geninterp.register_interp0 $wit$ $interp$ >>;
<:str_item< Geninterp.register_val0 $wit$ $dyn$ >>;
make_extend loc s cl wit;
<:str_item< do {
Pptactic.declare_extra_genarg_pprule
$wit$ $lid:rawpr$ $lid:globpr$ $lid:pr$;
Tacentries.create_ltac_quotation $se$
(fun (loc, v) -> Tacexpr.TacGeneric (Genarg.in_gen (Genarg.rawwit $wit$) v))
($lid:s$, None)
} >> ]
let declare_vernac_argument loc s pr cl =
let se = mlexpr_of_string s in
let wit = <:expr< $lid:"wit_"^s$ >> in
let pr_rules = match pr with
| None -> <:expr< fun _ _ _ _ -> Pp.str $str:"[No printer for "^s^"]"$ >>
| Some pr -> <:expr< fun _ _ _ -> $lid:pr$ >> in
declare_str_items loc
[ <:str_item<
value ($lid:"wit_"^s$ : Genarg.genarg_type 'a unit unit) =
Genarg.create_arg $se$ >>;
make_extend loc s cl wit;
<:str_item< do {
Pptactic.declare_extra_genarg_pprule $wit$
$pr_rules$
(fun _ _ _ _ -> CErrors.anomaly (Pp.str "vernac argument needs not globwit printer."))
(fun _ _ _ _ -> CErrors.anomaly (Pp.str "vernac argument needs not wit printer.")) }
>> ]
open Pcaml
EXTEND
GLOBAL: str_item;
str_item:
[ [ "ARGUMENT"; "EXTEND"; s = entry_name;
header = argextend_header;
OPT "|"; l = LIST1 argrule SEP "|";
"END" ->
declare_tactic_argument loc s header l
| "VERNAC"; "ARGUMENT"; "EXTEND"; s = entry_name;
pr = OPT ["PRINTED"; "BY"; pr = LIDENT -> pr];
OPT "|"; l = LIST1 argrule SEP "|";
"END" ->
declare_vernac_argument loc s pr l ] ]
;
argextend_specialized:
[ [ rawtyp = OPT [ "RAW_TYPED"; "AS"; rawtyp = argtype -> rawtyp ];
"RAW_PRINTED"; "BY"; rawpr = LIDENT;
globtyp = OPT [ "GLOB_TYPED"; "AS"; globtyp = argtype -> globtyp ];
"GLOB_PRINTED"; "BY"; globpr = LIDENT ->
(rawtyp, rawpr, globtyp, globpr) ] ]
;
argextend_header:
[ [ typ = OPT [ "TYPED"; "AS"; typ = argtype -> typ ];
"PRINTED"; "BY"; pr = LIDENT;
f = OPT [ "INTERPRETED"; "BY"; f = LIDENT -> f ];
g = OPT [ "GLOBALIZED"; "BY"; f = LIDENT -> f ];
h = OPT [ "SUBSTITUTED"; "BY"; f = LIDENT -> f ];
special = OPT argextend_specialized ->
let repr = match special with
| None -> `Uniform (typ, pr)
| Some (rtyp, rpr, gtyp, gpr) -> `Specialized (rtyp, rpr, gtyp, gpr, typ, pr)
in
(repr, f, g, h) ] ]
;
argtype:
[ "2"
[ e1 = argtype; "*"; e2 = argtype -> PairArgType (e1, e2) ]
| "1"
[ e = argtype; LIDENT "list" -> ListArgType e
| e = argtype; LIDENT "option" -> OptArgType e ]
| "0"
[ e = LIDENT ->
let e = parse_user_entry e "" in
type_of_user_symbol e
| "("; e = argtype; ")" -> e ] ]
;
argrule:
[ [ "["; l = LIST0 genarg; "]"; "->"; "["; e = Pcaml.expr; "]" -> (l,e) ] ]
;
genarg:
[ [ e = LIDENT; "("; s = LIDENT; ")" ->
let e = parse_user_entry e "" in
ExtNonTerminal (e, Some s)
| e = LIDENT; "("; s = LIDENT; ","; sep = STRING; ")" ->
let e = parse_user_entry e sep in
ExtNonTerminal (e, Some s)
| e = LIDENT ->
let e = parse_user_entry e "" in
ExtNonTerminal (e, None)
| s = STRING -> ExtTerminal s
] ]
;
entry_name:
[ [ s = LIDENT -> s
| UIDENT -> failwith "Argument entry names must be lowercase"
] ]
;
END
|