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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
(* $Id$ *)
open Pp
open Util
open Options
open Generic
open Term
open Constant
open Inductive
open Reduction
open Tacred
open Declare
open Names
open Coqast
open Ast
open Library
open Libobject
open Astterm
let mkCastC(c,t) = ope("CAST",[c;t])
let mkLambdaC(x,a,b) = ope("LAMBDA",[a;slam(Some (string_of_id x),b)])
let mkLambdaCit = List.fold_right (fun (x,a) b -> mkLambdaC(x,a,b))
let mkProdC (x,a,b) = ope("PROD",[a;slam(Some (string_of_id x),b)])
let mkProdCit = List.fold_right (fun (x,a) b -> mkProdC(x,a,b))
(* Commands of the interface *)
(* 1| Constant definitions *)
let constant_entry_of_com com =
let sigma = Evd.empty in
let env = Global.env() in
match com with
| Node(_,"CAST",[_;t]) ->
{ const_entry_body = Cooked (constr_of_com sigma env com);
const_entry_type = Some (constr_of_com1 true sigma env t) }
| _ ->
{ const_entry_body = Cooked (constr_of_com sigma env com);
const_entry_type = None }
let definition_body ident n com =
let ce = constant_entry_of_com com in
declare_constant ident (ce,n);
if is_verbose() then message ((string_of_id ident) ^ " is defined")
let red_constant_entry ce = function
| None -> ce
| Some red ->
let body = match ce.const_entry_body with
| Cooked c -> c
| Recipe _ -> assert false
in
{ const_entry_body =
Cooked (reduction_of_redexp red (Global.env()) Evd.empty body);
const_entry_type =
ce.const_entry_type }
let definition_body_red ident n com red_option =
let ce = constant_entry_of_com com in
let ce' = red_constant_entry ce red_option in
declare_constant ident (ce',n);
if is_verbose() then message ((string_of_id ident) ^ " is defined")
let syntax_definition ident com =
let c = raw_constr_of_com Evd.empty (Global.context()) com in
Syntax_def.declare_syntactic_definition ident c;
if is_verbose() then
message ((string_of_id ident) ^ " is now a syntax macro")
(***TODO
let abstraction_definition ident arity com =
let c = raw_constr_of_compattern Evd.empty (Global.env()) com in
machine_abstraction ident arity c
***)
(* 2| Variable definitions *)
let parameter_def_var ident c =
let c = constr_of_com1 true Evd.empty (Global.env()) c in
declare_parameter (id_of_string ident) c
let hypothesis_def_var is_refining ident n c =
let warning () =
mSGERRNL [< 'sTR"Warning: "; 'sTR ident;
'sTR" is declared as a parameter";
'sTR" because it is at a global level" >]
in
match n with
| NeverDischarge ->
warning();
parameter_def_var ident c
| DischargeAt disch_sp ->
if Lib.is_section_p disch_sp then begin
declare_variable (id_of_string ident)
(constr_of_com1 true Evd.empty (Global.env()) c,n,false);
if is_refining then
mSGERRNL [< 'sTR"Warning: Variable "; 'sTR ident;
'sTR" is not visible from current goals" >]
end else begin
warning();
parameter_def_var ident c
end
(* 3| Mutual Inductive definitions *)
let minductive_message = function
| [] -> anomaly "no inductive definition"
| [x] -> [< print_id x; 'sTR " is defined">]
| l -> hOV 0 [< prlist_with_sep pr_coma print_id l;
'sPC; 'sTR "are defined">]
let recursive_message = function
| [] -> anomaly "no recursive definition"
| [x] -> [< print_id x; 'sTR " is recursively defined">]
| l -> hOV 0 [< prlist_with_sep pr_coma print_id l;
'sPC; 'sTR "are recursively defined">]
let corecursive_message = function
| [] -> anomaly "no corecursive definition"
| [x] -> [< print_id x; 'sTR " is corecursively defined">]
| l -> hOV 0 [< prlist_with_sep pr_coma print_id l;
'sPC; 'sTR "are corecursively defined">]
let build_mutual lparams lnamearconstrs finite =
let allnames =
List.fold_left
(fun acc (id,_,l) -> id::(List.map fst l)@acc) [] lnamearconstrs in
if not (list_distinct allnames) then
error "Two inductive objects have the same name";
let lrecnames = List.map (fun (x,_,_)->x) lnamearconstrs
and nparams = List.length lparams
and sigma = Evd.empty
and env0 = Global.env() in
let fs = States.freeze() in
try
let mispecvec =
let (ind_sign,arityl) =
List.fold_left
(fun (env,arl) (recname,arityc,_) ->
let arity = type_of_com env (mkProdCit lparams arityc) in
let env' = Environ.push_var (recname,arity) env in
declare_variable recname (arity.body,NeverDischarge,false);
(env', (arity::arl)))
(env0,[]) lnamearconstrs
in
List.map2
(fun ar (name,_,lname_constr) ->
let consconstrl =
List.map
(fun (_,constr) -> constr_of_com sigma ind_sign
(mkProdCit lparams constr))
lname_constr
in
(name, ar.body, List.map fst lname_constr,
put_DLAMSV_subst (List.rev lrecnames) (Array.of_list consconstrl)))
(List.rev arityl) lnamearconstrs
in
let mie = {
mind_entry_nparams = nparams;
mind_entry_finite = finite;
mind_entry_inds = mispecvec }
in
States.unfreeze fs;
let sp = declare_mind mie in
if is_verbose() then pPNL(minductive_message lrecnames);
for i = 0 to List.length mispecvec - 1 do
declare_eliminations sp i
done
with e ->
States.unfreeze fs; raise e
(* try to find non recursive definitions *)
let list_chop_hd i l = match list_chop i l with
| (l1,x::l2) -> (l1,x,l2)
| _ -> assert false
let collect_non_rec =
let rec searchrec lnonrec lnamerec ldefrec larrec nrec =
try
let i =
list_try_find_i
(fun i f ->
if List.for_all (fun def -> not (occur_var f def)) ldefrec
then i else failwith "try_find_i")
0 lnamerec
in
let (lf1,f,lf2) = list_chop_hd i lnamerec in
let (ldef1,def,ldef2) = list_chop_hd i ldefrec in
let (lar1,ar,lar2) = list_chop_hd i larrec in
let newlnv =
try
match list_chop i nrec with
| (lnv1,_::lnv2) -> (lnv1@lnv2)
| _ -> [] (* nrec=[] for cofixpoints *)
with Failure "list_chop" -> []
in
searchrec ((f,DOP2(Cast,def,body_of_type ar))::lnonrec)
(lf1@lf2) (ldef1@ldef2) (lar1@lar2) newlnv
with Failure "try_find_i" ->
(lnonrec, (lnamerec,ldefrec,larrec,nrec))
in
searchrec []
let build_recursive lnameargsardef =
let lrecnames = List.map (fun (f,_,_,_) -> f) lnameargsardef
and sigma = Evd.empty
and env0 = Global.env()
and nv = Array.of_list (List.map (fun (_,la,_,_) -> (List.length la) -1)
lnameargsardef)
in
let fs = States.freeze() in
let (rec_sign,arityl) =
try
List.fold_left
(fun (env,arl) (recname,lparams,arityc,_) ->
let arity = type_of_com env (mkProdCit lparams arityc) in
declare_variable recname (arity.body,NeverDischarge,false);
(Environ.push_var (recname,arity) env, (arity::arl)))
(env0,[]) lnameargsardef
with e ->
States.unfreeze fs; raise e
in
let recdef =
try
List.map (fun (_,lparams,arityc,def) ->
constr_of_com sigma rec_sign
(mkLambdaCit lparams (mkCastC(def,arityc))))
lnameargsardef
with e ->
States.unfreeze fs; raise e
in
States.unfreeze fs;
let (lnonrec,(lnamerec,ldefrec,larrec,nvrec)) =
collect_non_rec lrecnames recdef (List.rev arityl) (Array.to_list nv) in
let n = NeverDischarge in
if lnamerec <> [] then begin
let recvec = [|put_DLAMSV_subst (List.rev lnamerec)
(Array.of_list ldefrec)|] in
let varrec = Array.of_list larrec in
let rec declare i = function
| fi::lf ->
let ce =
{ const_entry_body =
Cooked (mkFixDlam (Array.of_list nvrec) i varrec recvec);
const_entry_type = None }
in
declare_constant fi (ce, n);
declare (i+1) lf
| _ -> ()
in
(* declare the recursive definitions *)
declare 0 lnamerec;
if is_verbose() then pPNL(recursive_message lnamerec)
end;
(* The others are declared as normal definitions *)
let var_subst id = (id,make_substituend (global_reference CCI id)) in
let _ =
List.fold_left
(fun subst (f,def) ->
let ce = { const_entry_body = Cooked (Generic.replace_vars subst def);
const_entry_type = None } in
declare_constant f (ce,n);
warning ((string_of_id f)^" is non-recursively defined");
(var_subst f) :: subst)
(List.map var_subst lnamerec)
lnonrec
in
()
let build_corecursive lnameardef =
let lrecnames = List.map (fun (f,_,_) -> f) lnameardef
and sigma = Evd.empty
and env0 = Global.env() in
let fs = States.freeze() in
let (rec_sign,arityl) =
try
List.fold_left
(fun (env,arl) (recname,arityc,_) ->
let arity = type_of_com env0 arityc in
declare_variable recname (arity.body,NeverDischarge,false);
(Environ.push_var (recname,arity) env, (arity::arl)))
(env0,[]) lnameardef
with e ->
States.unfreeze fs; raise e
in
let recdef =
try
List.map (fun (_,arityc,def) ->
constr_of_com sigma rec_sign
(mkCastC(def,arityc)))
lnameardef
with e ->
States.unfreeze fs; raise e
in
States.unfreeze fs;
let (lnonrec,(lnamerec,ldefrec,larrec,_)) =
collect_non_rec lrecnames recdef (List.rev arityl) [] in
let n = NeverDischarge in
if lnamerec <> [] then begin
let recvec =
if lnamerec = [] then
[||]
else
[|put_DLAMSV_subst (List.rev lnamerec) (Array.of_list ldefrec)|]
in
let rec declare i = function
| fi::lf ->
let ce =
{ const_entry_body =
Cooked (mkCoFixDlam i (Array.of_list larrec) recvec);
const_entry_type = None }
in
declare_constant fi (ce,n);
declare (i+1) lf
| _ -> ()
in
declare 0 lnamerec;
if is_verbose() then pPNL(corecursive_message lnamerec)
end;
let var_subst id =
(id,make_substituend (global_reference CCI id)) in
let _ =
List.fold_left
(fun subst (f,def) ->
let ce = { const_entry_body = Cooked (Generic.replace_vars subst def);
const_entry_type = None } in
declare_constant f (ce,n);
warning ((string_of_id f)^" is non-recursively defined");
(var_subst f) :: subst)
(List.map var_subst lnamerec)
lnonrec
in
()
let build_scheme lnamedepindsort =
let lrecnames = List.map (fun (f,_,_,_) -> f) lnamedepindsort
and sigma = Evd.empty
and env0 = Global.env() in
let lrecspec =
List.map (fun (_,dep,ind,sort) ->
let indc = constr_of_com sigma env0 ind
and s = destSort (constr_of_com sigma env0 sort) in
(indc,dep,s)) lnamedepindsort
in
let n = NeverDischarge in
let listdecl = Indrec.build_indrec env0 sigma lrecspec in
let rec declare i = function
| fi::lf ->
let ce =
{ const_entry_body = Cooked listdecl.(i); const_entry_type = None }
in
declare_constant fi (ce,n);
declare (i+1) lf
| _ -> ()
in
declare 0 lrecnames;
if is_verbose() then pPNL(recursive_message lrecnames)
|