forked from coq/coq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tacred.ml
1361 lines (1212 loc) · 51.4 KB
/
tacred.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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(************************************************************************)
(* * The Coq Proof Assistant / The Coq Development Team *)
(* v * Copyright INRIA, CNRS and contributors *)
(* <O___,, * (see version control and CREDITS file for authors & dates) *)
(* \VV/ **************************************************************)
(* // * This file is distributed under the terms of the *)
(* * GNU Lesser General Public License Version 2.1 *)
(* * (see LICENSE file for the text of the license) *)
(************************************************************************)
open CErrors
open Util
open Names
open Constr
open Context
open Termops
open Environ
open EConstr
open Reductionops
module RelDecl = Context.Rel.Declaration
module NamedDecl = Context.Named.Declaration
(* Errors *)
type reduction_tactic_error =
InvalidAbstraction of env * Evd.evar_map * EConstr.constr * (env * Type_errors.type_error)
exception ReductionTacticError of reduction_tactic_error
(* Evaluable reference *)
exception Elimconst
type evaluable_global_reference =
| EvalVarRef of Id.t
| EvalConstRef of Constant.t
(* Better to have it here that in closure, since used in grammar.cma *)
let eq_egr e1 e2 = match e1, e2 with
EvalConstRef con1, EvalConstRef con2 -> Constant.CanOrd.equal con1 con2
| EvalVarRef id1, EvalVarRef id2 -> Id.equal id1 id2
| _, _ -> false
(* Here the semantics is completely unclear.
What does "Hint Unfold t" means when "t" is a parameter?
Does the user mean "Unfold X.t" or does she mean "Unfold y"
where X.t is later on instantiated with y? I choose the first
interpretation (i.e. an evaluable reference is never expanded). *)
let subst_evaluable_reference subst = function
| EvalVarRef id -> EvalVarRef id
| EvalConstRef kn -> EvalConstRef (Mod_subst.subst_constant subst kn)
exception NotEvaluableRef of GlobRef.t
let () = CErrors.register_handler (function
| NotEvaluableRef r ->
Some Pp.(str "Cannot coerce" ++ spc () ++ Nametab.pr_global_env Id.Set.empty r ++
spc () ++ str "to an evaluable reference.")
| _ -> None)
let error_not_evaluable r = raise (NotEvaluableRef r)
let is_evaluable_const env cst =
is_transparent env (ConstKey cst) && evaluable_constant cst env
let is_evaluable_var env id =
is_transparent env (VarKey id) && evaluable_named id env
let is_evaluable env = function
| EvalConstRef cst -> is_evaluable_const env cst
| EvalVarRef id -> is_evaluable_var env id
let value_of_evaluable_ref env evref u =
match evref with
| EvalConstRef con ->
let u = Unsafe.to_instance u in
EConstr.of_constr (constant_value_in env (con, u))
| EvalVarRef id -> env |> lookup_named id |> NamedDecl.get_value |> Option.get
let evaluable_of_global_reference env = function
| GlobRef.ConstRef cst when is_evaluable_const env cst -> EvalConstRef cst
| GlobRef.VarRef id when is_evaluable_var env id -> EvalVarRef id
| r -> error_not_evaluable r
let global_of_evaluable_reference = function
| EvalConstRef cst -> GlobRef.ConstRef cst
| EvalVarRef id -> GlobRef.VarRef id
type evaluable_reference =
| EvalConst of Constant.t
| EvalVar of Id.t
| EvalRel of int
| EvalEvar of EConstr.existential
let evaluable_reference_eq sigma r1 r2 = match r1, r2 with
| EvalConst c1, EvalConst c2 -> Constant.CanOrd.equal c1 c2
| EvalVar id1, EvalVar id2 -> Id.equal id1 id2
| EvalRel i1, EvalRel i2 -> Int.equal i1 i2
| EvalEvar (e1, ctx1), EvalEvar (e2, ctx2) ->
EConstr.eq_constr sigma (mkEvar (e1, ctx1)) (mkEvar (e2, ctx2))
| _ -> false
let mkEvalRef ref u =
match ref with
| EvalConst cst -> mkConstU (cst,u)
| EvalVar id -> mkVar id
| EvalRel n -> mkRel n
| EvalEvar ev -> EConstr.mkEvar ev
let isEvalRef env sigma c = match EConstr.kind sigma c with
| Const (sp,_) -> is_evaluable env (EvalConstRef sp)
| Var id -> is_evaluable env (EvalVarRef id)
| Rel _ | Evar _ -> true
| _ -> false
let isTransparentEvalRef env sigma ts c = match EConstr.kind sigma c with
| Const (cst,_) -> is_evaluable env (EvalConstRef cst) && TransparentState.is_transparent_constant ts cst
| Var id -> is_evaluable env (EvalVarRef id) && TransparentState.is_transparent_variable ts id
| Rel _ -> true
| Evar _ -> false (* undefined *)
| _ -> false
let destEvalRefU sigma c = match EConstr.kind sigma c with
| Const (cst,u) -> EvalConst cst, u
| Var id -> (EvalVar id, EInstance.empty)
| Rel n -> (EvalRel n, EInstance.empty)
| Evar ev -> (EvalEvar ev, EInstance.empty)
| _ -> anomaly (Pp.str "Not an unfoldable reference.")
let reference_opt_value env sigma eval u =
match eval with
| EvalConst cst ->
let u = EInstance.kind sigma u in
Option.map EConstr.of_constr (constant_opt_value_in env (cst,u))
| EvalVar id ->
env |> lookup_named id |> NamedDecl.get_value
| EvalRel n ->
env |> lookup_rel n |> RelDecl.get_value |> Option.map (Vars.lift n)
| EvalEvar ev ->
match EConstr.kind sigma (mkEvar ev) with
| Evar _ -> None
| c -> Some (EConstr.of_kind c)
exception NotEvaluable
let reference_value env sigma c u =
match reference_opt_value env sigma c u with
| None -> raise NotEvaluable
| Some d -> d
(************************************************************************)
(* Reduction of constants hiding a fixpoint (e.g. for "simpl" tactic). *)
(* One reuses the name of the function after reduction of the fixpoint *)
type fix_refolding = {
refolding_names : (evaluable_reference * EInstance.t) option array;
refolding_wrapper_data : (int * constr) list;
expected_args : int;
}
type fix_evaluation_data = {
trigger_min_args : int;
refolding_target : evaluable_reference;
refolding_data : fix_refolding;
}
type constant_evaluation =
| EliminationFix of fix_evaluation_data
| EliminationCases of int
| EliminationProj of int
| NotAnElimination
(* We use a cache registered as a global table *)
type frozen = constant_evaluation Cmap.t
let eval_table = Summary.ref (Cmap.empty : frozen) ~name:"evaluation"
(* [compute_consteval] determines whether f is an "elimination constant"
either [yn:Tn]..[y1:T1](match yi with f1..fk end g1 ..gp)
or [yn:Tn]..[y1:T1](Fix(m0,..) yi1..yip)
with yi1..yip distinct variables among the yi, not occurring in t
In the second case, [check_fix_reversibility [T1;...;Tn] args fix]
checks that [args] is a subset of disjoint variables in y1..yn (a necessary
condition for reversibility). Assuming a constant f_m naming
Fix(m,..), with f := f_m0, it also returns for each m the relevant
information ([i1,Ti1;..;ip,Tip],n) in order to compute an
equivalent g_m of Fix(m,..) such that
g_m := [xp:Tip']..[x1:Ti1'](f_m a1..an)
== [xp:Tip']..[x1:Ti1'](Fix(f|t) yi1..yip)
with a_k:=y_k if k<>i_j and (but only in the case m_0), a_k:=args_k
otherwise, as well as Tij':=Tij[x1..xi(j-1) <- a1..ai(j-1)]
Note that the types Tk, when no i_j=k, must not be dependent on
the xp..x1.
*)
let compute_fix_reversibility sigma labs args fix =
let nlam = List.length labs in
let nargs = List.length args in
if nargs > nlam then
(* Necessary non-linear, thus not reversible *)
raise Elimconst;
(* Check that arguments are bound by the lambdas, up to a
substitution, and that they do not occur elsewhere *)
let typed_reversible_args =
List.map
(function d -> match EConstr.kind sigma d with
| Rel k ->
if Vars.noccurn sigma k fix && k <= nlam then
(* Bound in labs and occurring only in args *)
(k, List.nth labs (k-1))
else
raise Elimconst
| _ ->
raise Elimconst) args in
let reversible_rels = List.map fst typed_reversible_args in
if not (List.distinct_f Int.compare reversible_rels) then
raise Elimconst;
(* Lambda's that are not used should not depend on those that are
used and that will thus be different in the recursive calls *)
List.iteri (fun i t_i ->
if not (Int.List.mem (i+1) reversible_rels) then
let fvs = List.map ((+) (i+1)) (Int.Set.elements (free_rels sigma t_i)) in
match List.intersect Int.equal fvs reversible_rels with
| [] -> ()
| _ -> raise Elimconst)
labs;
typed_reversible_args, nlam, nargs
let check_fix_reversibility env sigma ref u minarg labs args refs ((lv,i),_ as fix) =
let li, nlam, nargs = compute_fix_reversibility sigma labs args (mkFix fix) in
let k = lv.(i) in
let refolding_data = {
refolding_names = refs;
refolding_wrapper_data = li;
expected_args = nlam;
} in
if k < nargs then
(* Such an optimisation would need eta-expansion
let p = destRel (List.nth args k) in
EliminationFix (n-p+1,(li,n))
*)
EliminationFix {
trigger_min_args = max minarg nlam;
refolding_target = ref;
refolding_data;
}
else
EliminationFix {
trigger_min_args = max minarg (nlam - nargs + k + 1);
refolding_target = ref;
refolding_data;
}
let compute_fix_wrapper allowed_reds env sigma ref u =
try match reference_opt_value env sigma ref u with
| None -> None
| Some c ->
let labs, ccl = whd_decompose_lambda env sigma c in
let c, l = whd_stack_gen allowed_reds env sigma ccl in
let labs = List.map snd labs in
assert (isFix sigma c);
Some (labs, l)
with Not_found (* Undefined ref *) -> None
(* Heuristic to look if global names are associated to other
components of a mutual fixpoint *)
let invert_names allowed_reds env sigma ref u names i =
let labs, l =
match compute_fix_wrapper allowed_reds env sigma ref u with
| None -> assert false
| Some (labs, l) -> labs, l in
let make_name j =
if Int.equal i j then Some (ref, u) else
match names.(j).binder_name with
| Anonymous -> None (* should not happen *)
| Name id ->
let refi = match ref with
| EvalRel _ | EvalEvar _ -> None
| EvalVar id' -> Some (EvalVar id)
| EvalConst kn ->
let kn = Constant.change_label kn (Label.of_id id) in
if Environ.mem_constant kn env then Some (EvalConst kn) else None
in
match refi with
| None -> None
| Some ref ->
match compute_fix_wrapper allowed_reds env sigma ref u with
| None -> None
| Some (labs', l') ->
let eq_constr c1 c2 = EConstr.eq_constr sigma c1 c2 in
if List.equal eq_constr labs' labs &&
List.equal eq_constr l l' then Some (ref, u)
else None in
labs, l, Array.init (Array.length names) make_name
let deactivate_delta allowed_reds =
(* Act both on Delta and transparent state as not all reduction functions work the same *)
RedFlags.(red_add_transparent (red_sub allowed_reds fDELTA) TransparentState.empty)
(* [compute_consteval] expands and refolds an arbitrary long sequence
of reversible constants for unary fixpoints but consider the last
constant before revealing a Fix if the latter is mutually defined *)
let compute_consteval allowed_reds env sigma ref u =
let allowed_reds_no_delta = deactivate_delta allowed_reds in
let rec srec env n labs lastref lastu onlyproj c =
let c',l = whd_stack_gen allowed_reds_no_delta env sigma c in
match EConstr.kind sigma c' with
| Lambda (id,t,g) when List.is_empty l && not onlyproj ->
let open Context.Rel.Declaration in
srec (push_rel (LocalAssum (id,t)) env) (n+1) (t::labs) lastref lastu onlyproj g
| Fix ((lv,i),(names,_,_) as fix) when not onlyproj ->
let nbfix = Array.length lv in
(if nbfix = 1 then
let names = [|Some (ref,u)|] in
try check_fix_reversibility env sigma ref u n labs l names fix
with Elimconst -> NotAnElimination
else
let labs', l', names = invert_names allowed_reds env sigma lastref lastu names i in
try check_fix_reversibility env sigma lastref lastu n labs' l' names fix
with Elimconst -> NotAnElimination)
| Case (_,_,_,_,_,d,_) when isRel sigma d && not onlyproj -> EliminationCases n
| Case (_,_,_,_,_,d,_) -> srec env n labs lastref lastu true d
| Proj (p, d) when isRel sigma d -> EliminationProj n
| _ when isTransparentEvalRef env sigma (RedFlags.red_transparent allowed_reds) c' ->
(* Forget all \'s and args and do as if we had started with c' *)
let ref, u = destEvalRefU sigma c' in
(match reference_opt_value env sigma ref u with
| None -> NotAnElimination (* e.g. if a rel *)
| Some c -> srec env n labs ref u onlyproj (applist (c,l)))
| _ -> NotAnElimination
in
match reference_opt_value env sigma ref u with
| None -> NotAnElimination
| Some c -> srec env 0 [] ref u false c
let reference_eval allowed_reds env sigma ref u =
match ref with
| EvalConst cst as ref when EInstance.is_empty u ->
(try
Cmap.find cst !eval_table
with Not_found -> begin
let v = compute_consteval allowed_reds env sigma ref u in
eval_table := Cmap.add cst v !eval_table;
v
end)
| ref -> compute_consteval allowed_reds env sigma ref u
(* If f is bound to EliminationFix (n',refs,infos), then n' is the minimal
number of args for triggering the reduction and infos is
([(yi1,Ti1);...;(yip,Tip)],n) indicating that f converts
to some [y1:T1,...,yn:Tn](Fix(..) yip .. yi1) where the y_{i_j} consist in a
disjoint subset of the yi, i.e. 1 <= ij <= n and the ij are disjoint (in
particular, p <= n).
f is applied to largs := arg1 .. argn and we need for recursive
calls to build the function
g := [xp:Tip',...,x1:Ti1'](f a1 ... an)
s.t. any (Fix(..) u1 ... up) can be re-expanded to (g u1 ... up)
This is made possible by setting
a_k:=x_j if k=i_j for some j
a_k:=arg_k otherwise
The type Tij' is Tij[yi(j-1)..y1 <- ai(j-1)..a1]
In the case of a mutual fix and f is the m-th component, this is
the same for the components different from m except that for the
f_l associated to component l, and f_l is convertible to
[y1:U1,...,yn:Un](Fix(..,l,..) yip .. yi1), we need i_j to be a
bijection (since we have no more arg_k at our disposal to fill a
position k not in the image of i_j).
*)
let xname = Name Namegen.default_dependent_ident
(* [f] is convertible to [Fix(recindices,bodynum),bodyvect)]:
do so that the reduction uses this extra information *)
let substl_with_function subst sigma constr =
let v = Array.of_list subst in
let rec subst_total k c = match EConstr.kind sigma c with
| Rel i when k < i ->
if i <= k + Array.length v then
(* A recursive call *)
Vars.lift k v.(i-k-1)
else
(* A variable bound beyond the scope of the fix *)
mkRel (i - Array.length v)
| _ ->
map_with_binders sigma succ subst_total k c in
subst_total 0 constr
type 'a fix_reduction_result = NotReducible | Reduced of 'a
let[@ocaml.inline] (let*) m f = match m with
| NotReducible -> NotReducible
| Reduced x -> f x
let mkLambda_with_eta sigma x t c =
let f, args = decompose_app_list sigma c in
if List.is_empty args then mkLambda (x, t, c)
else
let b, args = List.sep_last args in
if isRelN sigma 1 b then applist (f, List.map (Vars.lift (-1)) args)
else mkLambda (x, t, c)
let contract_fix env sigma f
((recindices,bodynum),(_names,_types,bodies as typedbodies) as fixp) = match f with
| None -> contract_fix sigma fixp
| Some f ->
let {refolding_names; refolding_wrapper_data = lv; expected_args = n}, largs = f in
let lu = List.firstn n largs in
let p = List.length lv in
let lyi = List.map fst lv in
let la =
List.map_i (fun q aq ->
(* k from the comment is q+1 *)
try mkRel (p+1-(List.index Int.equal (n-q) lyi))
with Not_found -> Vars.lift p aq)
0 lu
in
let make_Fi i = match refolding_names.(i) with
| None -> mkFix((recindices,i),typedbodies)
| Some (ref, u) ->
let body = applist (mkEvalRef ref u, la) in
List.fold_left_i (fun q (* j = n+1-q *) c (ij,tij) ->
let subst = List.map (Vars.lift (-q)) (List.firstn (n-ij) la) in
let tij' = Vars.substl (List.rev subst) tij in
let x = make_annot xname Sorts.Relevant in (* TODO relevance *)
mkLambda_with_eta sigma x tij' c)
1 body (List.rev lv)
in
let nbodies = Array.length recindices in
let lbodies = List.init nbodies make_Fi in
let c = substl_with_function (List.rev lbodies) sigma (nf_beta env sigma bodies.(bodynum)) in
nf_beta env sigma c
let contract_cofix env sigma f
(bodynum,(names,_,bodies as typedbodies) as fixp) args = match f with
| None -> contract_cofix sigma fixp
| Some f ->
let make_Fi i =
let cofix = mkCoFix (i,typedbodies) in
match f with
| EvalConst kn, u ->
begin
if Int.equal i bodynum then mkConstU (kn, u)
else match names.(i).binder_name with
| Anonymous -> cofix
| Name id ->
(* In case of a call to another component of a block of
mutual inductive, try to reuse the global name if
the block was indeed initially built as a global
definition *)
let kn = Constant.change_label kn (Label.of_id id) in
let cst = (kn, EInstance.kind sigma u) in
try match constant_opt_value_in env cst with
| None -> cofix
(* TODO: check kn is correct *)
| Some _ -> mkConstU (kn, u)
with Not_found -> cofix
end
| _ ->
cofix in
let nbodies = Array.length bodies in
let subbodies = List.init nbodies make_Fi in
substl_with_function (List.rev subbodies)
sigma (nf_beta env sigma bodies.(bodynum))
let reducible_construct sigma c = match EConstr.kind sigma c with
| Construct _ | CoFix _ (* reduced by case *)
| Int _ | Float _ | Array _ (* reduced by primitives *) -> true
| _ -> false
let reduce_mind_case env sigma f (ci, u, pms, p, iv, (hd, args), lf) =
match EConstr.kind sigma hd with
| Construct ((_, i as cstr),u) ->
let real_cargs = List.skipn ci.ci_npar args in
let br = lf.(i - 1) in
let ctx = EConstr.expand_branch env sigma u pms cstr br in
let br = it_mkLambda_or_LetIn (snd br) ctx in
Reduced (applist (br, real_cargs))
(* TODO, consider the case of lambdas in front of the CoFix ?? *)
| CoFix (bodynum,(names,_,_) as cofix) ->
let cofix_def = contract_cofix env sigma f cofix args in
Reduced (mkCase (ci, u, pms, p, iv, applist(cofix_def, args), lf))
| Int _ | Float _ | Array _ -> NotReducible
| _ -> assert false
let match_eval_ref env sigma constr stack =
match EConstr.kind sigma constr with
| Const (sp, u) ->
reduction_effect_hook env sigma sp
(lazy (EConstr.to_constr sigma (applist (constr,stack))));
if is_evaluable env (EvalConstRef sp) then Some (EvalConst sp, u) else None
| Var id when is_evaluable env (EvalVarRef id) -> Some (EvalVar id, EInstance.empty)
| Rel i -> Some (EvalRel i, EInstance.empty)
| Evar ev -> Some (EvalEvar ev, EInstance.empty)
| _ -> None
let match_eval_ref_value env sigma constr stack =
match EConstr.kind sigma constr with
| Const (sp, u) ->
reduction_effect_hook env sigma sp
(lazy (EConstr.to_constr sigma (applist (constr,stack))));
if is_evaluable env (EvalConstRef sp) then
let u = EInstance.kind sigma u in
Some (EConstr.of_constr (constant_value_in env (sp, u)))
else
None
| Proj (p, c) when not (Projection.unfolded p) ->
if is_evaluable env (EvalConstRef (Projection.constant p)) then
Some (mkProj (Projection.unfold p, c))
else None
| Var id when is_evaluable env (EvalVarRef id) ->
env |> lookup_named id |> NamedDecl.get_value
| Rel n ->
env |> lookup_rel n |> RelDecl.get_value |> Option.map (Vars.lift n)
| _ -> None
let push_app sigma (hd, stk as p) = match EConstr.kind sigma hd with
| App (hd, args) ->
(hd, Array.fold_right (fun x accu -> x :: accu) args stk)
| _ -> p
let recargs = function
| EvalVar _ | EvalRel _ | EvalEvar _ -> None
| EvalConst c -> ReductionBehaviour.get c
let fix_recarg ((recindices,bodynum),_) stack =
assert (0 <= bodynum && bodynum < Array.length recindices);
let recargnum = Array.get recindices bodynum in
try
Some (recargnum, List.nth stack recargnum)
with Failure _ ->
None
let reduce_projection env sigma p ~npars (recarg'hd,stack') stack =
(match EConstr.kind sigma recarg'hd with
| Construct _ ->
let proj_narg = npars + Projection.arg p in
Reduced (List.nth stack' proj_narg, stack)
| _ -> NotReducible)
let rec beta_applist sigma accu c stk = match EConstr.kind sigma c, stk with
| Lambda (_, _, c), arg :: stk -> beta_applist sigma (arg :: accu) c stk
| _ -> Vars.substl accu c, stk
let whd_nothing_for_iota env sigma s =
let rec whrec (x, stack as s) =
match EConstr.kind sigma x with
| Rel n ->
let open Context.Rel.Declaration in
(match lookup_rel n env with
| LocalDef (_,body,_) -> whrec (Vars.lift n body, stack)
| _ -> s)
| Var id ->
let open Context.Named.Declaration in
(match lookup_named id env with
| LocalDef (_,body,_) -> whrec (body, stack)
| _ -> s)
| Evar ev -> s
| Meta ev ->
(try whrec (Evd.meta_value sigma ev, stack)
with Not_found -> s)
| Const (const, u) ->
let u = EInstance.kind sigma u in
(match constant_opt_value_in env (const, u) with
| Some body -> whrec (EConstr.of_constr body, stack)
| None -> s)
| LetIn (_,b,_,c) -> whrec (beta_applist sigma [b] c stack)
| Cast (c,_,_) -> whrec (c, stack)
| App (f,cl) -> whrec (f, Array.fold_right (fun c accu -> c :: accu) cl stack)
| Lambda (na,t,c) ->
(match stack with
| a :: stack -> whrec (beta_applist sigma [a] c stack)
| _ -> s)
| x -> s
in
whrec s
(* The reductions that should be performed as part of the simpl tactic,
excluding symbols that have the NeverUnfold flag. *)
let make_simpl_reds env =
let open RedFlags in
let open ReductionBehaviour in
let simpl_never = all_never_unfold () in
let transparent_state = Conv_oracle.get_transp_state (Environ.oracle env) in
let transparent_state =
{ transparent_state with
tr_cst = Cpred.diff transparent_state.tr_cst simpl_never
}
in
let reds = no_red in
let reds = red_add_transparent reds transparent_state in
let reds = red_add reds fDELTA in
let reds = red_add reds fZETA in
let reds = red_add reds fBETA in
reds
(* [red_elim_const] contracts iota/fix/cofix redexes hidden behind
constants by keeping the name of the constants in the recursive calls;
it fails if no redex is around *)
let rec red_elim_const allowed_reds env sigma ref u largs =
let open ReductionBehaviour in
let nargs = List.length largs in
let* largs, unfold_anyway, unfold_nonelim, nocase =
match recargs ref with
| None -> Reduced (largs, false, false, false)
| Some NeverUnfold -> NotReducible
| Some (UnfoldWhen { nargs = Some n } | UnfoldWhenNoMatch { nargs = Some n })
when nargs < n -> NotReducible
| Some (UnfoldWhen { recargs = x::l } | UnfoldWhenNoMatch { recargs = x::l })
when nargs <= List.fold_left max x l -> NotReducible
| Some (UnfoldWhen { recargs; nargs = None }) ->
let* params = reduce_params allowed_reds env sigma largs recargs in
Reduced (params,
false,
false,
false)
| Some (UnfoldWhenNoMatch { recargs; nargs = None }) ->
let* params = reduce_params allowed_reds env sigma largs recargs in
Reduced (params,
false,
false,
true)
| Some (UnfoldWhen { recargs; nargs = Some n }) ->
let is_empty = List.is_empty recargs in
let* params = reduce_params allowed_reds env sigma largs recargs in
Reduced (params,
is_empty && nargs >= n,
not is_empty && nargs >= n,
false)
| Some (UnfoldWhenNoMatch { recargs; nargs = Some n }) ->
let is_empty = List.is_empty recargs in
let* params = reduce_params allowed_reds env sigma largs recargs in
Reduced (params,
is_empty && nargs >= n,
not is_empty && nargs >= n,
true)
in
let ans = match reference_eval allowed_reds env sigma ref u with
| EliminationCases n when nargs >= n ->
let c = reference_value env sigma ref u in
let c', lrest = whd_nothing_for_iota env sigma (c, largs) in
let* ans = special_red_case allowed_reds env sigma (EConstr.destCase sigma c') in
Reduced ((ans, lrest), nocase)
| EliminationProj n when nargs >= n ->
let c = reference_value env sigma ref u in
let c', lrest = whd_nothing_for_iota env sigma (c, largs) in
let* ans = reduce_proj allowed_reds env sigma c' in
Reduced ((ans, lrest), nocase)
| EliminationFix {trigger_min_args; refolding_target; refolding_data}
when nargs >= trigger_min_args ->
let rec descend (ref,u) args =
let c = reference_value env sigma ref u in
if evaluable_reference_eq sigma ref refolding_target then
(c,args)
else
let c', lrest = whd_betalet_stack env sigma (applist(c,args)) in
descend (destEvalRefU sigma c') lrest in
let (_, midargs as s) = descend (ref,u) largs in
let d, lrest = whd_nothing_for_iota env sigma s in
let f = refolding_data, midargs in
let* (c, rest) = reduce_fix allowed_reds env sigma (Some f) (destFix sigma d) lrest in
Reduced ((c, rest), nocase)
| NotAnElimination when unfold_nonelim ->
let c = reference_value env sigma ref u in
Reduced ((whd_betaiotazeta env sigma (applist (c, largs)), []), nocase)
| _ -> NotReducible
in
match ans with
| NotReducible when unfold_anyway ->
let c = reference_value env sigma ref u in
Reduced ((whd_betaiotazeta env sigma (applist (c, largs)), []), nocase)
| _ -> ans
and reduce_params allowed_reds env sigma stack l =
let len = List.length stack in
let rec redp stack l = match l with
| [] -> Reduced stack
| i :: l ->
if len <= i then NotReducible
else
let arg = List.nth stack i in
let* rarg = whd_construct_stack allowed_reds env sigma arg in
match EConstr.kind sigma (fst rarg) with
| Construct _ | Int _ | Float _ | Array _ ->
redp (List.assign stack i (applist rarg)) l
| _ -> NotReducible
in
redp stack l
(* reduce to whd normal form or to an applied constant that does not hide
a reducible iota/fix/cofix redex (the "simpl" tactic) *)
and whd_simpl_stack allowed_reds env sigma =
let rec redrec s =
let s' = push_app sigma s in
let (x, stack) = s' in
match EConstr.kind sigma x with
| Lambda (na,t,c) ->
(match stack with
| [] -> s'
| a :: rest -> redrec (beta_applist sigma [a] c rest))
| LetIn (n,b,t,c) -> redrec (Vars.substl [b] c, stack)
| App (f,cl) -> assert false (* see push_app above *)
| Cast (c,_,_) -> redrec (c, stack)
| Case (ci,u,pms,p,iv,c,lf) ->
begin match special_red_case allowed_reds env sigma (ci,u,pms,p,iv,c,lf) with
| Reduced c -> redrec (c, stack)
| NotReducible -> s'
end
| Fix fix ->
begin match reduce_fix allowed_reds env sigma None fix stack with
| Reduced s' -> redrec s'
| NotReducible -> s'
end
| Proj (p, c) ->
let ans =
let unf = Projection.unfolded p in
if unf || is_evaluable env (EvalConstRef (Projection.constant p)) then
let npars = Projection.npars p in
match unf, ReductionBehaviour.get (Projection.constant p) with
| false, Some NeverUnfold -> NotReducible
| false, Some (UnfoldWhen { recargs } | UnfoldWhenNoMatch { recargs })
when not (List.is_empty recargs) ->
let l' = List.map_filter (fun i ->
let idx = (i - (npars + 1)) in
if idx < 0 then None else Some idx) recargs in
let* stack = reduce_params allowed_reds env sigma stack l' in
let* r = whd_construct_stack allowed_reds env sigma c in
reduce_projection env sigma p ~npars r stack
| _ ->
let* r = whd_construct_stack allowed_reds env sigma c in
reduce_projection env sigma p ~npars r stack
else NotReducible
in
begin match ans with
| Reduced s' -> redrec s'
| NotReducible -> s'
end
| Const (cst, _) when is_primitive env cst ->
let ans =
let args =
List.map_filter_i (fun i a ->
match a with CPrimitives.Kwhnf -> Some i | _ -> None)
(CPrimitives.kind (Option.get (get_primitive env cst))) in
let* stack = reduce_params allowed_reds env sigma stack args in
Reduced (whd_const cst env sigma (applist (x, stack)), [])
in
begin match ans with
| Reduced s' -> s'
| NotReducible -> s'
end
| _ ->
match match_eval_ref env sigma x stack with
| Some (ref, u) ->
let ans =
let* sapp, nocase = red_elim_const allowed_reds env sigma ref u stack in
let hd, _ as s'' = redrec sapp in
let rec is_case x = match EConstr.kind sigma x with
| Lambda (_,_, x) | LetIn (_,_,_, x) | Cast (x, _,_) -> is_case x
| App (hd, _) -> is_case hd
| Case _ -> true
| _ -> false in
if nocase && is_case hd then NotReducible else Reduced s''
in
begin match ans with
| Reduced s' -> s'
| NotReducible -> s'
end
| None -> s'
in
redrec
and reduce_fix allowed_reds env sigma f fix stack =
match fix_recarg fix stack with
| None -> NotReducible
| Some (recargnum,recarg) ->
let* (recarg'hd,_ as recarg') =
whd_construct_stack allowed_reds env sigma recarg in
let stack' = List.assign stack recargnum (applist recarg') in
(match EConstr.kind sigma recarg'hd with
| Construct _ -> Reduced (contract_fix env sigma f fix, stack')
| _ -> NotReducible)
and reduce_proj allowed_reds env sigma c =
let rec redrec s =
match EConstr.kind sigma s with
| Proj (proj, c) ->
let c' = match redrec c with NotReducible -> c | Reduced c -> c in
let* (constr, cargs) = whd_construct_stack allowed_reds env sigma c' in
(match EConstr.kind sigma constr with
| Construct _ ->
let proj_narg = Projection.npars proj + Projection.arg proj in
Reduced (List.nth cargs proj_narg)
| _ -> NotReducible)
| Case (n,u,pms,p,iv,c,brs) ->
let* c' = redrec c in
let p = (n,u,pms,p,iv,c',brs) in
begin match special_red_case allowed_reds env sigma p with
| Reduced c -> Reduced c
| NotReducible -> Reduced (mkCase p)
end
| _ -> NotReducible
in redrec c
and special_red_case allowed_reds env sigma (ci, u, pms, p, iv, c, lf) =
let* f, head, args = whd_construct allowed_reds env sigma (c, []) in
reduce_mind_case env sigma f (ci, u, pms, p, iv, (head, args), lf)
and whd_construct_stack allowed_reds env sigma s =
let* _, head, args = whd_construct allowed_reds env sigma (s, []) in
Reduced (head, args)
(* reduce until finding an applied constructor (or primitive value) or fail *)
and whd_construct allowed_reds env sigma s =
let (constr, cargs) = whd_simpl_stack allowed_reds env sigma s in
match match_eval_ref env sigma constr cargs with
| Some (ref, u) ->
(match reference_opt_value env sigma ref u with
| None -> NotReducible
| Some gvalue ->
if reducible_construct sigma gvalue then Reduced (Some (ref, u), gvalue, cargs)
else whd_construct allowed_reds env sigma (gvalue, cargs))
| None ->
if reducible_construct sigma constr then Reduced (None, constr, cargs)
else NotReducible
(************************************************************************)
(* Special Purpose Reduction Strategies *)
(* Red reduction tactic: one step of delta reduction + full
beta-iota-fix-cofix-zeta-cast at the head of the conclusion of a
sequence of products; fails if no delta redex is around
*)
let try_red_product env sigma c =
let simpfun c = clos_norm_flags RedFlags.betaiotazeta env sigma c in
let rec redrec env x =
let x = whd_betaiota env sigma x in
match EConstr.kind sigma x with
| App (f,l) ->
(match EConstr.kind sigma f with
| Fix fix ->
(match fix_recarg fix (Array.to_list l) with
| None -> NotReducible
| Some (recargnum,recarg) ->
let* recarg' = redrec env recarg in
let l = Array.copy l in
let () = Array.set l recargnum recarg' in
Reduced (simpfun (mkApp (f, l))))
| _ ->
let* r = redrec env f in
Reduced (simpfun (mkApp (r, l))))
| Cast (c,_,_) -> redrec env c
| Prod (x,a,b) ->
let open Context.Rel.Declaration in
let* b = redrec (push_rel (LocalAssum (x, a)) env) b in
Reduced (mkProd (x, a, b))
| LetIn (x,a,b,t) -> redrec env (Vars.subst1 a t)
| Case (ci,u,pms,p,iv,d,lf) ->
let* d = redrec env d in
Reduced (simpfun (mkCase (ci,u,pms,p,iv,d,lf)))
| Proj (p, c) ->
let* c' =
match EConstr.kind sigma c with
| Construct _ -> Reduced c
| _ -> redrec env c
in
let npars = Projection.npars p in
let* s = reduce_projection env sigma p ~npars (whd_betaiotazeta_stack env sigma c') [] in
Reduced (simpfun (applist s))
| _ ->
(match match_eval_ref env sigma x [] with
| Some (ref, u) ->
(* TO DO: re-fold fixpoints after expansion *)
(* to get true one-step reductions *)
(match reference_opt_value env sigma ref u with
| None -> NotReducible
| Some c -> Reduced c)
| _ -> NotReducible)
in redrec env c
let red_product env sigma c = match try_red_product env sigma c with
| Reduced c -> c
| NotReducible -> user_err Pp.(str "No head constant to reduce.")
(*
(* This old version of hnf uses betadeltaiota instead of itself (resp
whd_construct_state) to reduce the argument of Case (resp Fix);
The new version uses the "simpl" strategy instead. For instance,
Variable n:nat.
Eval hnf in match (plus (S n) O) with S n => n | _ => O end.
returned
(fix plus (n m : nat) {struct n} : nat :=
match n with
| O => m
| S p => S (plus p m)
end) n 0
while the new version returns (plus n O)
*)
let whd_simpl_orelse_delta_but_fix_old env sigma c =
let whd_all = whd_all_state env sigma in
let rec redrec (x, stack as s) =
match kind_of_term x with
| Lambda (na,t,c) ->
(match decomp_stack stack with
| None -> s
| Some (a,rest) -> stacklam redrec [a] c rest)
| LetIn (n,b,t,c) -> stacklam redrec [b] c stack
| App (f,cl) -> redrec (f, append_stack cl stack)
| Cast (c,_,_) -> redrec (c, stack)
| Case (ci,p,d,lf) ->
(try
redrec (special_red_case env sigma whd_all (ci,p,d,lf), stack)
with Redelimination ->
s)
| Fix fix ->
(match reduce_fix whd_all fix stack with
| Reduced s' -> redrec s'
| NotReducible -> s)
| _ when isEvalRef env x ->
let ref = destEvalRef x in
(try
redrec (red_elim_const env sigma ref stack)
with Redelimination ->
match reference_opt_value env sigma ref with
| Some c ->
(match kind_of_term (strip_lam c) with
| CoFix _ | Fix _ -> s
| _ -> redrec (c, stack))
| None -> s)
| _ -> s
in app_stack (redrec (c, empty_stack))
*)
(* Same as [whd_simpl] but also reduces constants that do not hide a
reducible fix, but does this reduction of constants only until it
immediately hides a non reducible fix or a cofix *)
let whd_simpl_orelse_delta_but_fix env sigma c =
let reds = make_simpl_reds env in
let rec redrec s =
let (constr, stack as s') = whd_simpl_stack reds env sigma s in
match match_eval_ref_value env sigma constr stack with
| Some c ->
(match EConstr.kind sigma (snd (decompose_lambda sigma c)) with
| CoFix _ | Fix _ -> s'
| Proj (p,t) when
(match EConstr.kind sigma constr with
| Const (c', _) -> QConstant.equal env (Projection.constant p) c'
| _ -> false) ->
let npars = Projection.npars p in
if List.length stack <= npars then
(* Do not show the eta-expanded form *)
s'
else redrec (c, stack)
| _ -> redrec (c, stack))
| None -> s'
in
applist (redrec c)
let hnf_constr0 env sigma c =
whd_simpl_orelse_delta_but_fix env sigma (c, [])
let hnf_constr env sigma c =
let c = whd_simpl_orelse_delta_but_fix env sigma (c, []) in
clos_norm_flags RedFlags.betaiota env sigma c
(* The "simpl" reduction tactic *)
let whd_simpl_with_reds allowed_reds env sigma c =
applist (whd_simpl_stack allowed_reds env sigma (c, []))
let whd_simpl env sigma x = whd_simpl_with_reds (make_simpl_reds env) env sigma x