Source file generalize.ml
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
open Pp
open Util
open Names
open Constr
open Context
open Termops
open Environ
open EConstr
open Vars
open Find_subterm
open Namegen
open Locus
open Proofview.Notations
open Context.Named.Declaration
module NamedDecl = Context.Named.Declaration
exception AlreadyUsed of Id.t
let error ?loc e =
Loc.raise ?loc e
exception Unhandled
let wrap_unhandled f e =
try Some (f e)
with Unhandled -> None
let tactic_interp_error_handler = function
| AlreadyUsed id ->
Id.print id ++ str " is already used."
| _ -> raise Unhandled
let _ = CErrors.register_handler (wrap_unhandled tactic_interp_error_handler)
let fresh_id_in_env avoid id env =
let avoid' = ids_of_named_context_val (named_context_val env) in
let avoid = if Id.Set.is_empty avoid then avoid' else Id.Set.union avoid' avoid in
next_ident_away_in_goal (Global.env ()) id avoid
let bring_hyps hyps =
if List.is_empty hyps then Tacticals.tclIDTAC
else
let hyps = List.rev hyps in
Proofview.Goal.enter begin fun gl ->
let env = Proofview.Goal.env gl in
let sigma = Proofview.Goal.sigma gl in
let concl = Tacmach.pf_concl gl in
let newcl = it_mkNamedProd_or_LetIn sigma concl hyps in
let args = Context.Named.instance mkVar hyps in
Refine.refine ~typecheck:false begin fun sigma ->
let (sigma, ev) =
Evarutil.new_evar env sigma ~principal:true newcl in
(sigma, mkApp (ev, args))
end
end
let revert hyps =
Proofview.Goal.enter begin fun gl ->
let ctx = List.map (fun id -> Tacmach.pf_get_hyp id gl) hyps in
(bring_hyps ctx) <*> (Tactics.clear hyps)
end
let generalized_name env sigma c t ids cl = function
| Name id as na ->
if Id.List.mem id ids then
error (AlreadyUsed id);
na
| Anonymous ->
match EConstr.kind sigma c with
| Var id ->
Name id
| _ ->
if noccurn sigma 1 cl then Anonymous else
named_hd env sigma t Anonymous
let generalize_goal_gen env sigma ids i ((occs,c,b),na) t cl =
let open Context.Rel.Declaration in
let decls,cl = decompose_prod_n_decls sigma i cl in
let dummy_prod = it_mkProd_or_LetIn mkProp decls in
let newdecls,_ =
let arity = Array.length (snd (EConstr.decompose_app sigma c)) in
let cache = ref Int.Map.empty in
let eq sigma k t =
let c =
try Int.Map.find k !cache
with Not_found ->
let c = EConstr.Vars.lift k c in
let () = cache := Int.Map.add k c !cache in
c
in
EConstr.eq_constr_nounivs sigma c t
in
decompose_prod_n_decls sigma i (replace_term_gen sigma eq arity (mkRel 1) dummy_prod)
in
let cl',sigma' = subst_closed_term_occ env sigma (AtOccs occs) c (it_mkProd_or_LetIn cl newdecls) in
let na = generalized_name env sigma c t ids cl' na in
let r = Retyping.relevance_of_type env sigma t in
let decl = match b with
| None -> LocalAssum (make_annot na r,t)
| Some b -> LocalDef (make_annot na r,b,t)
in
mkProd_or_LetIn decl cl', sigma'
let generalize_goal gl i ((occs,c,b),na as o) (cl,sigma) =
let open Tacmach in
let env = pf_env gl in
let ids = pf_ids_of_hyps gl in
let sigma, t = Typing.type_of env sigma c in
generalize_goal_gen env sigma ids i o t cl
let generalize_dep ?(with_let=false) c =
let open Tacmach in
let open Tacticals in
Proofview.Goal.enter begin fun gl ->
let env = pf_env gl in
let sign = named_context_val env in
let sigma = project gl in
let init_ids = ids_of_named_context (Global.named_context()) in
let seek (d:named_declaration) (toquant:named_context) =
if List.exists (fun d' -> occur_var_in_decl env sigma (NamedDecl.get_id d') d) toquant
|| dependent_in_decl sigma c d then
d::toquant
else
toquant in
let to_quantify = Context.Named.fold_outside seek (named_context_of_val sign) ~init:[] in
let qhyps = List.map NamedDecl.get_id to_quantify in
let tothin = List.filter (fun id -> not (Id.List.mem id init_ids)) qhyps in
let tothin' =
match EConstr.kind sigma c with
| Var id when mem_named_context_val id sign && not (Id.List.mem id init_ids)
-> tothin@[id]
| _ -> tothin
in
let cl' = it_mkNamedProd_or_LetIn sigma (pf_concl gl) to_quantify in
let is_var, body = match EConstr.kind sigma c with
| Var id ->
let body = NamedDecl.get_value (pf_get_hyp id gl) in
let is_var = Option.is_empty body && not (List.mem id init_ids) in
if with_let then is_var, body else is_var, None
| _ -> false, None
in
let cl'',evd = generalize_goal gl 0 ((AllOccurrences,c,body),Anonymous)
(cl',project gl) in
let evd =
if is_var then evd
else fst (Typing.type_of env evd cl'')
in
let args = Array.to_list (Context.Named.instance mkVar to_quantify) in
tclTHENLIST
[ Proofview.Unsafe.tclEVARS evd;
Tactics.apply_type ~typecheck:false cl'' (if Option.is_empty body then c::args else args);
Tactics.clear tothin']
end
(** *)
let generalize_gen_let lconstr = Proofview.Goal.enter begin fun gl ->
let env = Proofview.Goal.env gl in
let newcl, evd =
List.fold_right_i (generalize_goal gl) 0 lconstr
(Tacmach.pf_concl gl,Tacmach.project gl)
in
let (evd, _) = Typing.type_of env evd newcl in
let map ((_, c, b),_) = if Option.is_empty b then Some c else None in
Proofview.tclTHEN (Proofview.Unsafe.tclEVARS evd)
(Tactics.apply_type ~typecheck:false newcl (List.map_filter map lconstr))
end
let new_generalize_gen_let lconstr =
Proofview.Goal.enter begin fun gl ->
let sigma = Proofview.Goal.sigma gl in
let concl = Proofview.Goal.concl gl in
let env = Proofview.Goal.env gl in
let ids = Tacmach.pf_ids_of_hyps gl in
let newcl, sigma, args =
List.fold_right_i
(fun i ((_,c,b),_ as o) (cl, sigma, args) ->
let sigma, t = Typing.type_of env sigma c in
let args = if Option.is_empty b then c :: args else args in
let cl, sigma = generalize_goal_gen env sigma ids i o t cl in
(cl, sigma, args))
0 lconstr (concl, sigma, [])
in
Proofview.tclTHEN (Proofview.Unsafe.tclEVARS sigma)
(Refine.refine ~typecheck:false begin fun sigma ->
let (sigma, ev) = Evarutil.new_evar env sigma ~principal:true newcl in
(sigma, applist (ev, args))
end)
end
let generalize_gen lconstr =
generalize_gen_let (List.map (fun (occs_c,na) ->
let (occs,c) = Redexpr.out_with_occurrences occs_c in
(occs,c,None),na) lconstr)
let new_generalize_gen lconstr =
new_generalize_gen_let (List.map (fun ((occs,c),na) ->
(occs,c,None),na) lconstr)
let generalize l =
new_generalize_gen_let (List.map (fun c -> ((AllOccurrences,c,None),Anonymous)) l)
let coq_eq env sigma = Evd.fresh_global env sigma Coqlib.(lib_ref "core.eq.type")
let coq_eq_refl env sigma = Evd.fresh_global env sigma Coqlib.(lib_ref "core.eq.refl")
let coq_heq_ref = lazy (Coqlib.lib_ref "core.JMeq.type")
let coq_heq env sigma = Evd.fresh_global env sigma (Lazy.force coq_heq_ref)
let coq_heq_refl env sigma = Evd.fresh_global env sigma (Coqlib.lib_ref "core.JMeq.refl")
let mkEq env sigma t x y =
let sigma, eq = coq_eq env sigma in
sigma, mkApp (eq, [| t; x; y |])
let mkRefl env sigma t x =
let sigma, refl = coq_eq_refl env sigma in
sigma, mkApp (refl, [| t; x |])
let mkHEq env sigma t x u y =
let sigma, c = coq_heq env sigma in
sigma, mkApp (c,[| t; x; u; y |])
let mkHRefl env sigma t x =
let sigma, c = coq_heq_refl env sigma in
sigma, mkApp (c, [| t; x |])
let lift_togethern n l =
let l', _ =
List.fold_right
(fun x (acc, n) ->
(lift n x :: acc, succ n))
l ([], n)
in l'
let lift_list l = List.map (lift 1) l
let ids_of_constr env sigma ?(all=false) vars c =
let rec aux vars c =
match EConstr.kind sigma c with
| Var id -> Id.Set.add id vars
| App (f, args) ->
(match EConstr.kind sigma f with
| Construct ((ind,_),_)
| Ind (ind,_) ->
let (mib,mip) = Inductive.lookup_mind_specif env ind in
Array.fold_left_from
(if all then 0 else mib.Declarations.mind_nparams)
aux vars args
| _ -> EConstr.fold sigma aux vars c)
| _ -> EConstr.fold sigma aux vars c
in aux vars c
let decompose_indapp env sigma f args =
match EConstr.kind sigma f with
| Construct ((ind,_),_)
| Ind (ind,_) ->
let (mib,mip) = Inductive.lookup_mind_specif env ind in
let first = mib.Declarations.mind_nparams_rec in
let pars, args = Array.chop first args in
mkApp (f, pars), args
| _ -> f, args
let mk_term_eq homogeneous env sigma ty t ty' t' =
if homogeneous then
let sigma, eq = mkEq env sigma ty t t' in
let sigma, refl = mkRefl env sigma ty' t' in
sigma, (eq, refl)
else
let sigma, heq = mkHEq env sigma ty t ty' t' in
let sigma, hrefl = mkHRefl env sigma ty' t' in
sigma, (heq, hrefl)
let make_abstract_generalize env id typ concl dep ctx body c eqs args refls =
let open Context.Rel.Declaration in
Refine.refine ~typecheck:true begin fun sigma ->
let eqslen = List.length eqs in
let sigma, abshypeq, abshypt =
if dep then
let ty = lift 1 c in
let homogeneous = Reductionops.is_conv env sigma ty typ in
let sigma, (eq, refl) =
mk_term_eq homogeneous (push_rel_context ctx env) sigma ty (mkRel 1) typ (mkVar id) in
sigma, mkProd (make_annot Anonymous Sorts.Relevant, eq, lift 1 concl), [| refl |]
else sigma, concl, [||]
in
let eqs = lift_togethern 1 eqs in
let abseqs = it_mkProd_or_LetIn (lift eqslen abshypeq)
(List.map (fun x -> LocalAssum (make_annot Anonymous Sorts.Relevant, x)) eqs)
in
let r = Sorts.Relevant in
let decl = match body with
| None -> LocalAssum (make_annot (Name id) r, c)
| Some body -> LocalDef (make_annot (Name id) r, body, c)
in
let genarg = mkProd_or_LetIn decl abseqs in
let genctyp = it_mkProd_or_LetIn genarg ctx in
let (sigma, genc) = Evarutil.new_evar env sigma ~principal:true genctyp in
let instc = mkApp (genc, Array.of_list args) in
let instc = Option.cata (fun _ -> instc) (mkApp (instc, [| mkVar id |])) body in
let appeqs = mkApp (instc, Array.of_list refls) in
(sigma, mkApp (appeqs, abshypt))
end
let hyps_of_vars env sigma sign nogen hyps =
if Id.Set.is_empty hyps then []
else
let (_,lh) =
Context.Named.fold_inside
(fun (hs,hl) d ->
let x = NamedDecl.get_id d in
if Id.Set.mem x nogen then (hs,hl)
else if Id.Set.mem x hs then (hs,x::hl)
else
let xvars = global_vars_set_of_decl env sigma d in
if not (Id.Set.is_empty (Id.Set.diff xvars hs)) then
(Id.Set.add x hs, x :: hl)
else (hs, hl))
~init:(hyps,[])
sign
in lh
exception Seen
let linear env sigma vars args =
let seen = ref vars in
try
Array.iter (fun i ->
let rels = ids_of_constr env sigma ~all:true Id.Set.empty i in
let seen' =
Id.Set.fold (fun id acc ->
if Id.Set.mem id acc then raise Seen
else Id.Set.add id acc)
rels !seen
in seen := seen')
args;
true
with Seen -> false
let is_defined_variable env id =
env |> lookup_named id |> is_local_def
let abstract_args gl generalize_vars dep id defined f args =
let open Context.Rel.Declaration in
let sigma = Tacmach.project gl in
let env = Tacmach.pf_env gl in
let concl = Tacmach.pf_concl gl in
let hyps = Proofview.Goal.hyps gl in
let dep = dep || local_occur_var sigma id concl in
let avoid = ref Id.Set.empty in
let get_id name =
let id = fresh_id_in_env !avoid (match name with Name n -> n | Anonymous -> Id.of_string "gen_x") env in
avoid := Id.Set.add id !avoid; id
in
let aux (sigma, prod, ctx, ctxenv, c, args, eqs, refls, nongenvars, vars) arg =
let name, ty_relevance, ty, arity =
let rel, c = Reductionops.whd_decompose_prod_n env sigma 1 prod in
let ({binder_name=na;binder_relevance=r},t) = List.hd rel in
na, r, t, c
in
let argty = Retyping.get_type_of env sigma arg in
let sigma, ty = Evarsolve.refresh_universes (Some true) env sigma ty in
let lenctx = List.length ctx in
let liftargty = lift lenctx argty in
let leq = constr_cmp ctxenv sigma Conversion.CUMUL liftargty ty in
match EConstr.kind sigma arg with
| Var id when not (is_defined_variable env id) && leq && not (Id.Set.mem id nongenvars) ->
(sigma, subst1 arg arity, ctx, ctxenv, mkApp (c, [|arg|]), args, eqs, refls,
Id.Set.add id nongenvars, Id.Set.remove id vars)
| _ ->
let name = get_id name in
let decl = LocalAssum (make_annot (Name name) ty_relevance, ty) in
let ctx = decl :: ctx in
let c' = mkApp (lift 1 c, [|mkRel 1|]) in
let args = arg :: args in
let liftarg = lift (List.length ctx) arg in
let sigma, eq, refl =
if leq then
let sigma, eq = mkEq env sigma (lift 1 ty) (mkRel 1) liftarg in
let sigma, refl = mkRefl env sigma (lift (-lenctx) ty) arg in
sigma, eq, refl
else
let sigma, eq = mkHEq env sigma (lift 1 ty) (mkRel 1) liftargty liftarg in
let sigma, refl = mkHRefl env sigma argty arg in
sigma, eq, refl
in
let eqs = eq :: lift_list eqs in
let refls = refl :: refls in
let argvars = ids_of_constr env sigma vars arg in
(sigma, arity, ctx, push_rel decl ctxenv, c', args, eqs, refls,
nongenvars, Id.Set.union argvars vars)
in
let f', args' = decompose_indapp env sigma f args in
let dogen, f', args' =
let parvars = ids_of_constr env sigma ~all:true Id.Set.empty f' in
if not (linear env sigma parvars args') then true, f, args
else
match Array.findi (fun i x -> not (isVar sigma x) || is_defined_variable env (destVar sigma x)) args' with
| None -> false, f', args'
| Some nonvar ->
let before, after = Array.chop nonvar args' in
true, mkApp (f', before), after
in
if dogen then
let tyf' = Retyping.get_type_of env sigma f' in
let sigma, arity, ctx, ctxenv, c', args, eqs, refls, nogen, vars =
Array.fold_left aux (sigma, tyf',[],env,f',[],[],[],Id.Set.empty,Id.Set.empty) args'
in
let args, refls = List.rev args, List.rev refls in
let vars =
if generalize_vars then
let nogen = Id.Set.add id nogen in
hyps_of_vars env sigma hyps nogen vars
else []
in
let body, c' =
if defined then Some c', Retyping.get_type_of ctxenv sigma c'
else None, c'
in
let typ = Tacmach.pf_get_hyp_typ id gl in
let tac = make_abstract_generalize env id typ concl dep ctx body c' eqs args refls in
let tac = Proofview.Unsafe.tclEVARS sigma <*> tac in
Some (tac, dep, succ (List.length ctx), vars)
else None
let abstract_generalize ?(generalize_vars=true) ?(force_dep=false) id =
let open Context.Named.Declaration in
Proofview.Goal.enter begin fun gl ->
Coqlib.(check_required_library jmeq_module_name);
let sigma = Tacmach.project gl in
let (f, args, def, id, oldid) =
let oldid = Tacmach.pf_get_new_id id gl in
match Tacmach.pf_get_hyp id gl with
| LocalAssum (_,t) -> let f, args = decompose_app sigma t in
(f, args, false, id, oldid)
| LocalDef (_,t,_) ->
let f, args = decompose_app sigma t in
(f, args, true, id, oldid)
in
if Array.is_empty args then Proofview.tclUNIT ()
else
let newc = abstract_args gl generalize_vars force_dep id def f args in
match newc with
| None -> Proofview.tclUNIT ()
| Some (tac, dep, n, vars) ->
let tac =
if dep then
Tacticals.tclTHENLIST [
tac;
Tactics.rename_hyp [(id, oldid)]; Tacticals.tclDO n Tactics.intro;
generalize_dep ~with_let:true (mkVar oldid)]
else Tacticals.tclTHENLIST [
tac;
Tactics.clear [id];
Tacticals.tclDO n Tactics.intro]
in
if List.is_empty vars then tac
else Tacticals.tclTHEN tac
(Tacticals.tclFIRST
[revert vars ;
Tacticals.tclMAP (fun id ->
Tacticals.tclTRY (generalize_dep ~with_let:true (mkVar id))) vars])
end