Source file 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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
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
type reduction_tactic_error =
InvalidAbstraction of env * Evd.evar_map * EConstr.constr * (env * Type_errors.type_error)
exception ReductionTacticError of reduction_tactic_error
exception Elimconst
let subst_evaluable_reference subst =
Evaluable.map (fun id -> id) (Mod_subst.subst_constant subst)
(Mod_subst.subst_proj_repr subst)
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 ?loc r = Loc.raise ?loc (NotEvaluableRef r)
let is_evaluable_const env cst =
is_transparent env (Evaluable.EvalConstRef cst) && evaluable_constant cst env
let is_evaluable_var env id =
is_transparent env (Evaluable.EvalVarRef id) && evaluable_named id env
let is_evaluable_projection env p =
is_transparent env (Evaluable.EvalProjectionRef p)
let is_evaluable env = function
| Evaluable.EvalConstRef cst -> is_evaluable_const env cst
| Evaluable.EvalVarRef id -> is_evaluable_var env id
| Evaluable.EvalProjectionRef p -> is_evaluable_projection env p
let value_of_evaluable_ref env evref u =
match evref with
| Evaluable.EvalConstRef con ->
let u = Unsafe.to_instance u in
EConstr.of_constr (constant_value_in env (con, u))
| Evaluable.EvalVarRef id ->
env |> lookup_named id |> NamedDecl.get_value |> Option.get
| Evaluable.EvalProjectionRef _ ->
assert false
let soft_evaluable_of_global_reference ?loc = function
| GlobRef.ConstRef cst ->
begin
match Structures.PrimitiveProjections.find_opt cst with
| None -> Evaluable.EvalConstRef cst
| Some p -> Evaluable.EvalProjectionRef p
end
| GlobRef.VarRef id -> Evaluable.EvalVarRef id
| r -> error_not_evaluable ?loc r
let evaluable_of_global_reference env = function
| GlobRef.ConstRef cst when is_evaluable_const env cst ->
begin
match Structures.PrimitiveProjections.find_opt cst with
| None -> Evaluable.EvalConstRef cst
| Some p -> Evaluable.EvalProjectionRef p
end
| GlobRef.VarRef id when is_evaluable_var env id -> Evaluable.EvalVarRef id
| r -> error_not_evaluable r
let global_of_evaluable_reference = function
| Evaluable.EvalConstRef cst -> GlobRef.ConstRef cst
| Evaluable.EvalVarRef id -> GlobRef.VarRef id
| Evaluable.EvalProjectionRef p -> GlobRef.ConstRef (Projection.Repr.constant p)
type evaluable_reference =
| EvalConst of Constant.t
| EvalVar of Id.t
| EvalRel of int
| EvalEvar of EConstr.existential
let evaluable_reference_eq env sigma r1 r2 = match r1, r2 with
| EvalConst c1, EvalConst c2 -> QConstant.equal env 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))
| (EvalConst _ | EvalVar _ | EvalRel _ | EvalEvar _), _ -> 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) && Structures.PrimitiveProjections.is_transparent_constant ts cst
| Var id -> is_evaluable env (EvalVarRef id) && TransparentState.is_transparent_variable ts id
| Rel _ -> true
| Evar _ -> false
| _ -> 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.")
module CacheTable = Hashtbl.Make(struct
type t = Constant.t * UVars.Instance.t
let equal (c,u) (c',u') =
Constant.UserOrd.equal c c' && UVars.Instance.equal u u'
let hash (c,u) =
Hashset.Combine.combine (Constant.UserOrd.hash c) (UVars.Instance.hash u)
end)
let reference_opt_value cache env sigma eval u =
match eval with
| EvalConst cst ->
let u = EInstance.kind sigma u in
let cu = (cst, u) in
begin match CacheTable.find_opt cache cu with
| Some v -> v
| None ->
let v = Option.map EConstr.of_constr (constant_opt_value_in env cu) in
CacheTable.add cache cu v;
v
end
| 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 cache env sigma c u =
match reference_opt_value cache env sigma c u with
| None -> raise NotEvaluable
| Some d -> d
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_elimination =
| EliminationFix of fix_evaluation_data
| EliminationCases of constr * int
| EliminationProj of constr * int
| NotAnElimination of constr
| NotAnEliminationConstant
type constant_coelimination =
| CoEliminationCoFix of fix_evaluation_data
| CoEliminationConstruct of constr
| CoEliminationPrimitive of constr
| NotACoElimination of constr
| NotACoEliminationConstant
type simpl_infos = {
constant_body_cache : EConstr.t option CacheTable.t;
elim_cache : constant_elimination CacheTable.t;
coelim_cache : constant_coelimination CacheTable.t;
red_behavior : ReductionBehaviour.Db.t;
main_reds : RedFlags.reds;
construct_reds : RedFlags.reds;
}
let make_simpl_infos (red_behavior, main_reds, construct_reds) = {
constant_body_cache = CacheTable.create 12;
elim_cache = CacheTable.create 12;
coelim_cache = CacheTable.create 12;
red_behavior;
main_reds;
construct_reds;
}
let compute_constant_reversibility sigma labs args fix =
let nlam = List.length labs in
let nargs = List.length args in
if nargs > nlam then
raise Elimconst;
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
(k, snd (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;
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 labs args minarg refs ((lv,i),_ as fix) =
let li, nlam, nargs = compute_constant_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
{
trigger_min_args = max minarg nlam;
refolding_target = ref;
refolding_data;
}
else
{
trigger_min_args = max minarg (nlam - nargs + k + 1);
refolding_target = ref;
refolding_data;
}
let check_cofix_reversibility env sigma ref u labs args minarg refs (i,_ as cofix) =
let li, nlam, nargs = compute_constant_reversibility sigma labs args (mkCoFix cofix) in
let refolding_data = {
refolding_names = refs;
refolding_wrapper_data = li;
expected_args = nlam;
} in
{
trigger_min_args = max minarg nlam;
refolding_target = ref;
refolding_data;
}
let compute_recursive_wrapper infos env sigma ref u =
try match reference_opt_value infos.constant_body_cache 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 infos.main_reds env sigma ccl in
Some (labs, l)
with Not_found -> None
let invert_recursive_names infos env sigma ref u names i =
let labs, l =
match compute_recursive_wrapper infos 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
| 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_recursive_wrapper infos 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 (fun (_,t) (_,t') -> eq_constr t t') 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 =
RedFlags.(red_add_transparent (red_sub allowed_reds fDELTA) TransparentState.empty)
let compute_constant_elimination infos env sigma ref u =
let allowed_reds_no_delta = deactivate_delta infos.main_reds in
let rec srec env all_abs lastref lastu onlyproj c stk =
let c', args = whd_stack_gen allowed_reds_no_delta env sigma c in
match EConstr.kind sigma c' with
| Lambda (id,t,g) ->
assert (List.is_empty args && Stack.is_empty stk);
let open Context.Rel.Declaration in
srec (push_rel (LocalAssum (id,t)) env) ((id,t)::all_abs) lastref lastu onlyproj g Stack.empty
| Fix ((lv,i),(names,_,_) as fix) when not onlyproj ->
let n_all_abs = List.length all_abs in
let nbfix = Array.length lv in
(if nbfix = 1 then
let refs = [|Some (ref,u)|] in
try EliminationFix (check_fix_reversibility env sigma ref u all_abs args n_all_abs refs fix)
with Elimconst -> NotAnEliminationConstant
else
let last_labs, last_args, names = invert_recursive_names infos env sigma lastref lastu names i in
try EliminationFix (check_fix_reversibility env sigma lastref lastu last_labs last_args n_all_abs names fix)
with Elimconst -> NotAnEliminationConstant)
| Case (_,_,_,_,_,d,_) when isRel sigma d && not onlyproj ->
EliminationCases (it_mkLambda (Stack.zip sigma (c',Stack.append_app_list args stk)) all_abs, List.length all_abs)
| Case (ci,u,pms,p,iv,d,lf) -> srec env all_abs lastref lastu true d Stack.(Case (mkCaseStk (ci,u,pms,p,iv,lf)) :: append_app_list args stk)
| Proj (p, _, d) when isRel sigma d ->
EliminationProj (it_mkLambda (Stack.zip sigma (c',Stack.append_app_list args stk)) all_abs, List.length all_abs)
| _ when isTransparentEvalRef env sigma (RedFlags.red_transparent infos.main_reds) c' ->
let ref, u = destEvalRefU sigma c' in
(match reference_opt_value infos.constant_body_cache env sigma ref u with
| None -> NotAnEliminationConstant
| Some c -> srec env all_abs ref u onlyproj (applist (c, args)) stk)
| _ -> NotAnEliminationConstant
in
match reference_opt_value infos.constant_body_cache env sigma ref u with
| None -> NotAnEliminationConstant
| Some c -> match srec env [] ref u false c Stack.empty with NotAnEliminationConstant -> NotAnElimination c | e -> e
let compute_constant_coelimination infos env sigma ref u =
let allowed_reds_no_delta = deactivate_delta infos.construct_reds in
let rec srec env all_abs lastref lastu c =
let c', args = whd_stack_gen allowed_reds_no_delta env sigma c in
match EConstr.kind sigma c' with
| Lambda (id,t,g) ->
assert (List.is_empty args);
let open Context.Rel.Declaration in
srec (push_rel (LocalAssum (id,t)) env) ((id,t)::all_abs) lastref lastu g
| Construct _ ->
let c = it_mkLambda (applist (c', args)) all_abs in
CoEliminationConstruct c
| Int _ | Float _ | String _ | Array _ ->
let c = it_mkLambda (applist (c', args)) all_abs in
CoEliminationPrimitive c
| CoFix (i,(names,_,_) as cofix) ->
let n_all_abs = List.length all_abs in
let nbfix = Array.length names in
(if nbfix = 1 then
let refs = [|Some (ref,u)|] in
try CoEliminationCoFix (check_cofix_reversibility env sigma ref u all_abs args n_all_abs refs cofix)
with Elimconst -> NotACoEliminationConstant
else
let last_labs, last_args, names = invert_recursive_names infos env sigma lastref lastu names i in
try CoEliminationCoFix (check_cofix_reversibility env sigma lastref lastu last_labs last_args n_all_abs names cofix)
with Elimconst -> NotACoEliminationConstant)
| _ when isTransparentEvalRef env sigma (RedFlags.red_transparent infos.construct_reds) c' ->
let ref, u = destEvalRefU sigma c' in
(match reference_opt_value infos.constant_body_cache env sigma ref u with
| None -> NotACoEliminationConstant
| Some c -> srec env all_abs ref u (applist (c, args)))
| _ -> NotACoEliminationConstant
in
match reference_opt_value infos.constant_body_cache env sigma ref u with
| None -> NotACoEliminationConstant
| Some c -> match srec env [] ref u c with NotACoEliminationConstant -> NotACoElimination c | e -> e
let compute_reference_elimination infos env sigma ref u =
match ref with
| EvalConst cst as ref ->
let cu = cst, EInstance.kind sigma u in
(match CacheTable.find_opt infos.elim_cache cu with
| Some v -> v
| None ->
let v = compute_constant_elimination infos env sigma ref u in
CacheTable.add infos.elim_cache cu v;
v)
| ref -> compute_constant_elimination infos env sigma ref u
let compute_reference_coelimination infos env sigma ref u =
match ref with
| EvalConst cst as ref ->
let cu = cst, EInstance.kind sigma u in
(match CacheTable.find_opt infos.coelim_cache cu with
| Some v -> v
| None ->
let v = compute_constant_coelimination infos env sigma ref u in
CacheTable.add infos.coelim_cache cu v;
v)
| ref -> compute_constant_coelimination infos env sigma ref u
let xname = Name Namegen.default_dependent_ident
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
Vars.lift k v.(i-k-1)
else
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_rec env sigma f nbodies mk_rec contract body =
match f with
| None -> contract ()
| 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 ->
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 -> mk_rec i
| Some (ref, u) ->
let body = applist (mkEvalRef ref u, la) in
List.fold_left_i (fun 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 ERelevance.relevant in
mkLambda_with_eta sigma x tij' c)
1 body (List.rev lv)
in
let lbodies = List.init nbodies make_Fi in
let c = substl_with_function (List.rev lbodies) sigma (nf_beta env sigma body) in
nf_beta env sigma c
let contract_fix env sigma f ((recindices,bodynum),(_names,_types,bodies as typedbodies) as fixp) =
contract_rec env sigma f
(Array.length bodies)
(fun i -> mkFix((recindices,i),typedbodies))
(fun () -> contract_fix sigma fixp)
bodies.(bodynum)
let contract_cofix env sigma f (bodynum,(_names,_types,bodies as typedbodies) as cofixp) =
contract_rec env sigma f
(Array.length bodies)
(fun i -> mkCoFix(i,typedbodies))
(fun () -> contract_cofix sigma cofixp)
bodies.(bodynum)
let reducible_construct sigma c = match EConstr.kind sigma c with
| Construct _ | CoFix _
| Int _ | Float _ | String _ | Array _ -> true
| _ -> 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, r, c) when not (Projection.unfolded p) ->
if is_evaluable env (EvalProjectionRef (Projection.repr p)) then
Some (mkProj (Projection.unfold p, r, 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 behavior = function
| EvalVar _ | EvalRel _ | EvalEvar _ -> None
| EvalConst c -> ReductionBehaviour.get_from_db behavior 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 contract_projection env sigma f (p,r) ~npars (hd, args) =
match EConstr.kind sigma hd with
| Construct _ ->
let proj_narg = npars + Projection.arg p in
Reduced (List.nth args proj_narg)
| CoFix cofix ->
let cofix_def = contract_cofix env sigma f cofix in
Reduced (mkProj (p, r, applist(cofix_def, args)))
| _ -> 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 _ | Meta _ -> 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
let make_reds env behavior =
let open RedFlags in
let open ReductionBehaviour.Db in
let simpl_never = all_never_unfold behavior in
let transparent_state = Conv_oracle.get_transp_state (Environ.oracle env) in
let transparent_state_never =
{ transparent_state with
tr_cst = Cpred.diff transparent_state.tr_cst simpl_never
}
in
let reds = no_red in
let reds = red_add reds fDELTA in
let reds = red_add reds fZETA in
let reds = red_add reds fBETA in
let reds = red_add_transparent reds transparent_state in
let reds_never = red_add_transparent reds transparent_state_never in
behavior, reds_never, reds
let rec descend cache env sigma target (ref,u) args =
let c = reference_value cache env sigma ref u in
if evaluable_reference_eq env sigma ref target then
(c,args)
else
let c', lrest = whd_betalet_stack env sigma (applist (c, args)) in
descend cache env sigma target (destEvalRefU sigma c') lrest
let make_simpl_reds env =
make_reds env (ReductionBehaviour.Db.get ())
let make_hnf_reds env =
make_reds env ReductionBehaviour.Db.empty
let rec red_elim_const infos env sigma ref u largs =
let open ReductionBehaviour in
let nargs = List.length largs in
let* largs, unfold_anyway, unfold_nonelim, nocase =
match recargs infos.red_behavior 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 infos env sigma largs recargs in
Reduced (params,
false,
false,
false)
| Some (UnfoldWhenNoMatch { recargs; nargs = None }) ->
let* params = reduce_params infos 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 infos 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 infos env sigma largs recargs in
Reduced (params,
is_empty && nargs >= n,
not is_empty && nargs >= n,
true)
in
let ans = match compute_reference_elimination infos env sigma ref u with
| EliminationCases (c,n) when nargs >= n ->
let c', stack = whd_nothing_for_iota env sigma (c, largs) in
let* ans = reduce_case infos env sigma (EConstr.destCase sigma c') in
Reduced ((ans, stack), nocase)
| EliminationProj (c,n) when nargs >= n ->
let c', stack = whd_nothing_for_iota env sigma (c, largs) in
let* ans = reduce_nested_projection infos env sigma c' in
Reduced ((ans, stack), nocase)
| EliminationFix {trigger_min_args; refolding_target; refolding_data}
when nargs >= trigger_min_args ->
let (_, midargs as s) = descend infos.constant_body_cache env sigma refolding_target (ref,u) largs in
let d, stack = whd_nothing_for_iota env sigma s in
let f = refolding_data, midargs in
let* c = reduce_fix infos env sigma (Some f) (destFix sigma d) stack in
Reduced (c, nocase)
| NotAnElimination c when unfold_nonelim ->
Reduced ((whd_betaiotazeta env sigma (applist (c, largs)), []), nocase)
| _ -> NotReducible
in
match ans with
| NotReducible when unfold_anyway ->
let c = reference_value infos.constant_body_cache env sigma ref u in
Reduced ((whd_betaiotazeta env sigma (applist (c, largs)), []), nocase)
| _ -> ans
and reduce_params infos 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 infos env sigma arg in
match EConstr.kind sigma (fst rarg) with
| Construct _ | Int _ | Float _ | String _ | Array _ ->
redp (List.assign stack i (applist rarg)) l
| _ -> NotReducible
in
redp stack l
and whd_simpl_stack infos 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
| Cast (c,_,_) -> redrec (c, stack)
| Case (ci,u,pms,p,iv,c,lf) ->
begin match reduce_case infos env sigma (ci,u,pms,p,iv,c,lf) with
| Reduced c -> redrec (c, stack)
| NotReducible -> s'
end
| Fix fix ->
begin match reduce_fix infos env sigma None fix stack with
| Reduced s' -> redrec s'
| NotReducible -> s'
end
| Proj (p, r, c) ->
let ans =
let unf = Projection.unfolded p in
if unf || is_evaluable env (EvalProjectionRef (Projection.repr p)) then
let npars = Projection.npars p in
match unf, ReductionBehaviour.get_from_db infos.red_behavior (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 infos env sigma stack l' in
let* c = reduce_projection infos env sigma (p,r) ~npars c in
Reduced (c, stack)
| _ ->
let* c = reduce_projection infos env sigma (p,r) ~npars c in
Reduced (c, 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 infos 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
| Const (cst, _) when is_symbol env cst ->
whd_all env sigma (applist s'), []
| _ ->
match match_eval_ref env sigma x stack with
| Some (ref, u) ->
let ans =
let* sapp, nocase = red_elim_const infos 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 infos env sigma f fix stack =
match fix_recarg fix stack with
| None -> NotReducible
| Some (recargnum,recarg) ->
let* (recarg'hd,_ as recarg') = whd_construct_stack infos env sigma recarg in
match EConstr.kind sigma recarg'hd with
| Construct _ ->
let stack' = List.assign stack recargnum (applist recarg') in
Reduced (contract_fix env sigma f fix, stack')
| _ -> NotReducible
and reduce_nested_projection infos env sigma c =
let rec redrec c =
match EConstr.kind sigma c with
| Proj (p, r, c) ->
let c' = match redrec c with NotReducible -> c | Reduced c -> c in
let npars = Projection.npars p in
reduce_projection infos env sigma (p,r) ~npars c'
| 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 reduce_case infos env sigma p with
| Reduced c -> Reduced c
| NotReducible -> Reduced (mkCase p)
end
| _ -> NotReducible
in redrec c
and reduce_projection infos env sigma p ~npars c =
let* f, s = whd_construct infos env sigma (c, []) in
contract_projection env sigma f p ~npars s
and reduce_case infos env sigma (ci, u, pms, p, iv, c, lf) =
let* f, (hd, args) = whd_construct infos env sigma (c, []) in
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))
| CoFix (bodynum,(names,_,_) as cofix) ->
let cofix_def = contract_cofix env sigma f cofix in
Reduced (mkCase (ci, u, pms, p, iv, applist(cofix_def, args), lf))
| Int _ | Float _ | String _ | Array _ -> NotReducible
| _ -> assert false
and whd_construct_stack infos env sigma s =
let* _, s = whd_construct infos env sigma (s, []) in
Reduced s
and whd_construct infos env sigma c =
let (constr, cargs) =
let construct_infos = { infos with red_behavior = ReductionBehaviour.Db.empty; main_reds = infos.construct_reds } in
whd_simpl_stack construct_infos env sigma c in
match match_eval_ref env sigma constr cargs with
| Some (ref, u) ->
(match compute_reference_coelimination infos env sigma ref u with
| CoEliminationConstruct c -> Reduced (None, whd_stack_gen infos.main_reds env sigma (applist (c, cargs)))
| CoEliminationPrimitive c -> Reduced (None, whd_stack_gen infos.main_reds env sigma (applist (c, cargs)))
| CoEliminationCoFix {refolding_target; refolding_data} ->
let (_, midargs as s) = descend infos.constant_body_cache env sigma refolding_target (ref,u) cargs in
let s = whd_nothing_for_iota env sigma s in
let f = refolding_data, midargs in
Reduced (Some f, s)
| NotACoElimination c ->
whd_construct infos env sigma (c, cargs)
| NotACoEliminationConstant -> NotReducible)
| None ->
if reducible_construct sigma constr then Reduced (None, (constr, cargs))
else NotReducible
let try_red_product env sigma c =
let simpfun c = clos_norm_flags RedFlags.betaiotazeta env sigma c in
let cache = CacheTable.create 12 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, r, c) ->
let* c' =
match EConstr.kind sigma c with
| Construct _ | CoFix _ -> Reduced c
| _ -> redrec env c
in
let npars = Projection.npars p in
let* c = contract_projection env sigma None (p,r) ~npars (whd_betaiotazeta_stack env sigma c') in
Reduced (simpfun c)
| _ ->
(match match_eval_ref env sigma x [] with
| Some (ref, u) ->
(match reference_opt_value cache 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 -> Some c
| NotReducible -> None
let whd_simpl_orelse_delta_but_fix env sigma c =
let infos = make_simpl_infos (make_hnf_reds env) in
let rec redrec s =
let (constr, stack as s') = whd_simpl_stack infos 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
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
let whd_simpl_with_reds infos env sigma c =
applist (whd_simpl_stack infos env sigma (c, []))
let whd_simpl env sigma x =
let infos = make_simpl_infos (make_simpl_reds env) in
whd_simpl_with_reds infos env sigma x
let simpl env sigma c =
let infos = make_simpl_infos (make_simpl_reds env) in
let rec strongrec env t =
map_constr_with_full_binders env sigma push_rel strongrec env
(whd_simpl_with_reds infos env sigma t) in
strongrec env c
let matches_head env sigma c t =
let t, l = decompose_app sigma t in
match EConstr.kind sigma t, Array.is_empty l with
| Proj (p, _, _), _ -> Constr_matching.matches env sigma c (mkConstU (Projection.constant p, EInstance.empty))
| _, false -> Constr_matching.matches env sigma c t
| _ -> raise Constr_matching.PatternMatchingFailure
(** FIXME: Specific function to handle projections: it ignores what happens on the
parameters. This is a temporary fix while rewrite etc... are not up to equivalence
of the projection and its eta expanded form.
*)
let change_map_constr_with_binders_left_to_right g f (env, l as acc) sigma c =
match EConstr.kind sigma c with
| Proj (p, r, v) ->
let t = Retyping.expand_projection env sigma p v [] in
let hdf, al = destApp sigma t in
let a = al.(Array.length al - 1) in
let app = (mkApp (hdf, Array.sub al 0 (Array.length al - 1))) in
let app' = f acc app in
let a' = f acc a in
let hdf', _ = decompose_app sigma app' in
if hdf' == hdf then
mkProj (p, r, a')
else mkApp (app', [| a' |])
| _ -> map_constr_with_binders_left_to_right env sigma g f acc c
let e_contextually byhead (occs,c) f = begin fun env sigma t ->
let count = ref (Locusops.initialize_occurrence_counter occs) in
let evd = ref sigma in
let rec traverse nested (env,c as envc) t =
if Locusops.occurrences_done !count then t
else
try
let subst =
if byhead then matches_head env sigma c t
else Constr_matching.matches env sigma c t in
let ok, count' = Locusops.update_occurrence_counter !count in count := count';
if ok then begin
if Option.has_some nested then
user_err Pp.(str "The subterm at occurrence " ++ int (Option.get nested) ++ str " overlaps with the subterm at occurrence " ++ int (Locusops.current_occurrence !count) ++ str ".");
if Locusops.more_specific_occurrences !count then
ignore (traverse_below (Some (Locusops.current_occurrence !count)) envc t);
let (evm, t) = (f subst) env !evd t in
(evd := evm; t)
end
else
traverse_below nested envc t
with Constr_matching.PatternMatchingFailure ->
traverse_below nested envc t
and traverse_below nested envc t =
match EConstr.kind !evd t with
| App (f,l) when byhead -> mkApp (f, Array.map_left (traverse nested envc) l)
| Proj (p,r,c) when byhead -> mkProj (p,r,traverse nested envc c)
| _ ->
change_map_constr_with_binders_left_to_right
(fun d (env,c) -> (push_rel d env, Patternops.lift_pattern 1 c))
(traverse nested) envc sigma t
in
let t' = traverse None (env,c) t in
Locusops.check_used_occurrences !count;
(!evd, t')
end
let contextually byhead occs f env sigma t =
let f' subst env sigma t = sigma, f subst env sigma t in
snd (e_contextually byhead occs f' env sigma t)
let match_constr_evaluable_ref env sigma c evref =
match EConstr.kind sigma c, evref with
| Const (c,u), Evaluable.EvalConstRef c' when QConstant.equal env c c' -> Some u
| Proj (p,_,_), Evaluable.EvalProjectionRef p' when QProjection.Repr.equal env (Projection.repr p) p' -> Some EInstance.empty
| Var id, Evaluable.EvalVarRef id' when Id.equal id id' -> Some EInstance.empty
| _, _ -> None
let substlin env sigma evalref occs c =
let count = ref (Locusops.initialize_occurrence_counter occs) in
let value u = value_of_evaluable_ref env evalref u in
let rec substrec () c =
if Locusops.occurrences_done !count then c
else
match match_constr_evaluable_ref env sigma c evalref with
| Some u ->
let ok, count' = Locusops.update_occurrence_counter !count in
count := count';
if ok then value u else c
| None ->
map_constr_with_binders_left_to_right env sigma
(fun _ () -> ())
substrec () c
in
let t' = substrec () c in
Locusops.check_used_occurrences !count;
(Locusops.current_occurrence !count, t')
let string_of_evaluable_ref env = function
| Evaluable.EvalVarRef id -> Id.to_string id
| Evaluable.EvalConstRef kn ->
Libnames.string_of_qualid
(Nametab.shortest_qualid_of_global (vars_of_env env) (GlobRef.ConstRef kn))
| Evaluable.EvalProjectionRef p ->
Projection.Repr.to_string p
let unfold_side_flags = RedFlags.[fBETA;fMATCH;fFIX;fCOFIX;fZETA]
let unfold_side_red = RedFlags.(mkflags [fBETA;fMATCH;fFIX;fCOFIX;fZETA])
let unfold_red kn =
let open RedFlags in
let flags = fDELTA :: unfold_side_flags in
let flags = match kn with
| Evaluable.EvalVarRef id -> fVAR id :: flags
| Evaluable.EvalConstRef sp ->
begin
match Structures.PrimitiveProjections.find_opt sp with
| None -> fCONST sp :: flags
| Some p -> fCONST sp :: fPROJ p :: flags
end
| Evaluable.EvalProjectionRef p ->
fPROJ p :: fCONST (Projection.Repr.constant p) :: flags
in
mkflags flags
let unfold env sigma name c =
if is_evaluable env name then
clos_norm_flags (unfold_red name) env sigma c
else
user_err Pp.(str (string_of_evaluable_ref env name^" is opaque."))
let unfoldoccs env sigma (occs,name) c =
let open Locus in
match occs with
| NoOccurrences -> c
| AllOccurrences -> unfold env sigma name c
| OnlyOccurrences _ | AllOccurrencesBut _ | AtLeastOneOccurrence ->
let (occ,uc) = substlin env sigma name occs c in
if Int.equal occ 0 then
user_err Pp.(str ((string_of_evaluable_ref env name)^" does not occur."));
nf_betaiotazeta env sigma uc
let unfoldn loccname env sigma c =
List.fold_left (fun c occname -> unfoldoccs env sigma occname c) c loccname
let fold_one_com com env sigma c =
let rcom = match red_product env sigma com with
| None -> user_err Pp.(str "No head constant to reduce.")
| Some c -> c
in
let a = subst_term sigma (clos_norm_flags unfold_side_red env sigma rcom) c in
if not (EConstr.eq_constr sigma a c) then
Vars.subst1 com a
else
let a = subst_term sigma rcom c in
Vars.subst1 com a
let fold_commands cl env sigma c =
List.fold_right (fun com c -> fold_one_com com env sigma c) (List.rev cl) c
let cbv_norm_flags flags ~strong env sigma t =
Cbv.(cbv_norm (create_cbv_infos flags ~strong env sigma) t)
let cbv_beta = cbv_norm_flags RedFlags.beta ~strong:true
let cbv_betaiota = cbv_norm_flags RedFlags.betaiota ~strong:true
let cbv_betadeltaiota env sigma = cbv_norm_flags RedFlags.all env sigma ~strong:true
let whd_cbv_betadeltaiota env sigma = cbv_norm_flags RedFlags.all env sigma ~strong:false
let whd_compute = whd_cbv_betadeltaiota
let compute = cbv_betadeltaiota
let abstract_scheme env (locc,a) (c, sigma) =
let ta = Retyping.get_type_of env sigma a in
let r = Retyping.relevance_of_term env sigma a in
let sigma, ta = Evarsolve.refresh_universes ~onlyalg:true (Some false) env sigma ta in
let na = Namegen.named_hd env sigma ta Anonymous in
let na = make_annot na r in
if occur_meta sigma ta then user_err Pp.(str "Cannot find a type for the generalisation.");
if occur_meta sigma a then
mkLambda (na,ta,c), sigma
else
let c', sigma = Find_subterm.subst_closed_term_occ env sigma (Locus.AtOccs locc) a c in
mkLambda (na,ta,c'), sigma
let pattern_occs loccs_trm = begin fun env sigma c ->
let abstr_trm, sigma = List.fold_right (abstract_scheme env) loccs_trm (c,sigma) in
try
let sigma, _ = Typing.type_of env sigma abstr_trm in
(sigma, applist(abstr_trm, List.map snd loccs_trm))
with Type_errors.TypeError (env',t) ->
raise (ReductionTacticError (InvalidAbstraction (env,sigma,abstr_trm,(env',t))))
end
let check_privacy env ind =
let spec = Inductive.lookup_mind_specif env ind in
if Inductive.is_private spec then
user_err Pp.(str "case analysis on a private type.")
let reduce_to_ind_gen allow_product env sigma t =
let rec elimrec env t l =
let t = hnf_constr0 env sigma t in
match EConstr.kind sigma (fst (decompose_app sigma t)) with
| Ind (ind, _ as indu) ->
let t = nf_betaiota env sigma t in
check_privacy env ind; (Some indu, it_mkProd_or_LetIn t l)
| Prod (n,ty,t') ->
let open Context.Rel.Declaration in
if allow_product then
let ty = nf_betaiota env sigma ty in
elimrec (push_rel (LocalAssum (n,ty)) env) t' ((LocalAssum (n,ty))::l)
else
None, it_mkProd_or_LetIn t l
| _ ->
let t' = whd_all env sigma t in
match EConstr.kind sigma (fst (decompose_app sigma t')) with
| Ind (ind, _ as indu) -> check_privacy env ind; (Some indu, it_mkProd_or_LetIn t' l)
| _ -> None, it_mkProd_or_LetIn t l
in
elimrec env t []
let reduce_to_quantified_ind env sigma c =
match reduce_to_ind_gen true env sigma c with
| None, _ -> user_err Pp.(str"Not an inductive definition.")
| Some i, t -> i, t
let reduce_to_atomic_ind env sigma c =
match reduce_to_ind_gen false env sigma c with
| None, _ -> user_err Pp.(str"Not an inductive definition.")
| Some i, t -> i, t
let eval_to_quantified_ind env sigma t =
let rec elimrec env t =
let t = hnf_constr0 env sigma t in
match EConstr.kind sigma (fst (decompose_app sigma t)) with
| Ind (ind, _ as indu) ->
let () = check_privacy env ind in
indu
| Prod (n,ty,t') ->
elimrec (push_rel (Context.Rel.Declaration.LocalAssum (n,ty)) env) t'
| _ ->
let t' = whd_all env sigma t in
match EConstr.kind sigma (fst (decompose_app sigma t')) with
| Ind (ind, _ as indu) -> check_privacy env ind; indu
| _ -> user_err Pp.(str"Not an inductive product.")
in
elimrec env t
let find_hnf_rectype env sigma t =
let ind,t = reduce_to_atomic_ind env sigma t in
ind, snd (decompose_app_list sigma t)
exception NotStepReducible
let one_step_reduce env sigma c =
let infos = make_simpl_infos (ReductionBehaviour.Db.empty, RedFlags.betadeltazeta, RedFlags.betadeltazeta) in
let rec redrec (x, stack) =
match EConstr.kind sigma x with
| Lambda (n,t,c) ->
(match stack with
| [] -> raise NotStepReducible
| a :: rest -> (Vars.subst1 a c, rest))
| App (f,cl) -> redrec (f, (Array.to_list cl)@stack)
| LetIn (_,f,_,cl) -> (Vars.subst1 f cl,stack)
| Cast (c,_,_) -> redrec (c,stack)
| Case (ci,u,pms,p,iv,c,lf) ->
begin match reduce_case infos env sigma (ci,u,pms,p,iv,c,lf) with
| Reduced c -> (c, stack)
| NotReducible -> raise NotStepReducible
end
| Fix fix ->
begin match reduce_fix infos env sigma None fix stack with
| Reduced s' -> s'
| NotReducible -> raise NotStepReducible
end
| _ when isEvalRef env sigma x ->
let ref,u = destEvalRefU sigma x in
begin match red_elim_const infos env sigma ref u stack with
| Reduced (c, _) -> c
| NotReducible ->
match reference_opt_value infos.constant_body_cache env sigma ref u with
| Some d -> (d, stack)
| None -> raise NotStepReducible
end
| _ -> raise NotStepReducible
in
applist (redrec (c,[]))
let error_cannot_recognize ref =
user_err
Pp.(str "Cannot recognize a statement based on " ++
Nametab.pr_global_env Id.Set.empty ref ++ str".")
let reduce_to_ref_gen allow_failure allow_product env sigma ref t =
match ref with
| GlobRef.IndRef mind' ->
let (i,t) = reduce_to_ind_gen allow_product env sigma t in
if allow_failure then t else
(match i with
| Some (mind,u) when QInd.equal env mind mind' -> t
| _ -> error_cannot_recognize ref)
| _ ->
let rec elimrec env t l =
let c, _ = decompose_app sigma t in
match EConstr.kind sigma c with
| Prod (n,ty,t') ->
if allow_product then
let open Context.Rel.Declaration in
elimrec (push_rel (LocalAssum (n,ty)) env) t' ((LocalAssum (n,ty))::l)
else if allow_failure then
it_mkProd_or_LetIn t l
else
error_cannot_recognize ref
| _ ->
if isRefX env sigma ref c
then it_mkProd_or_LetIn t l
else
try
let t' = nf_betaiota env sigma (one_step_reduce env sigma t) in
elimrec env t' l
with NotStepReducible ->
if allow_failure then
it_mkProd_or_LetIn t l
else
error_cannot_recognize ref
in
elimrec env t []
let reduce_to_quantified_ref ?(allow_failure=false) = reduce_to_ref_gen allow_failure true
let reduce_to_atomic_ref ?(allow_failure=false) = reduce_to_ref_gen allow_failure false