Source file cbv.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
open Util
open Names
open Constr
open Vars
open CClosure
open Esubst
type cbv_value =
| VAL of int * constr
| STACK of int * cbv_value * cbv_stack
| CBN of constr * cbv_value subs
| LAM of int * (Name.t Context.binder_annot * constr) list * constr * cbv_value subs
| FIXP of fixpoint * cbv_value subs * cbv_value array
| COFIXP of cofixpoint * cbv_value subs * cbv_value array
| CONSTR of constructor Univ.puniverses * cbv_value array
| PRIMITIVE of CPrimitives.t * pconstant * cbv_value array
| ARRAY of Univ.Instance.t * cbv_value Parray.t * cbv_value
and cbv_stack =
| TOP
| APP of cbv_value array * cbv_stack
| CASE of Univ.Instance.t * constr array * case_return * case_branch array * Constr.case_invert * case_info * cbv_value subs * cbv_stack
| PROJ of Projection.t * cbv_stack
let rec shift_value n = function
| VAL (k,t) -> VAL (k+n,t)
| STACK(k,v,stk) -> STACK(k+n,v,stk)
| CBN (t,s) -> CBN(t,subs_shft(n,s))
| LAM (nlams,ctxt,b,s) -> LAM (nlams,ctxt,b,subs_shft (n,s))
| FIXP (fix,s,args) ->
FIXP (fix,subs_shft (n,s), Array.map (shift_value n) args)
| COFIXP (cofix,s,args) ->
COFIXP (cofix,subs_shft (n,s), Array.map (shift_value n) args)
| CONSTR (c,args) ->
CONSTR (c, Array.map (shift_value n) args)
| PRIMITIVE(op,c,args) ->
PRIMITIVE(op,c,Array.map (shift_value n) args)
| ARRAY (u,t,ty) ->
ARRAY(u, Parray.map (shift_value n) t, shift_value n ty)
let shift_value n v =
if Int.equal n 0 then v else shift_value n v
let rec mk_fix_subs make_body n env i =
if Int.equal i n then env
else mk_fix_subs make_body n (subs_cons (make_body i) env) (i + 1)
let contract_fixp env ((reci,i),(_,_,bds as bodies)) =
let make_body j = FIXP(((reci,j),bodies), env, [||]) in
let n = Array.length bds in
mk_fix_subs make_body n env 0, bds.(i)
let contract_cofixp env (i,(_,_,bds as bodies)) =
let make_body j = COFIXP((j,bodies), env, [||]) in
let n = Array.length bds in
mk_fix_subs make_body n env 0, bds.(i)
let make_constr_ref n k t =
match k with
| RelKey p -> mkRel (n+p)
| VarKey id -> t
| ConstKey cst -> t
let stack_app appl stack =
if Int.equal (Array.length appl) 0 then stack else
match stack with
| APP(args,stk) -> APP(Array.append appl args,stk)
| _ -> APP(appl, stack)
let rec stack_concat stk1 stk2 =
match stk1 with
TOP -> stk2
| APP(v,stk1') -> APP(v,stack_concat stk1' stk2)
| CASE(u,pms,c,b,iv,i,s,stk1') -> CASE(u,pms,c,b,iv,i,s,stack_concat stk1' stk2)
| PROJ (p,stk1') -> PROJ (p,stack_concat stk1' stk2)
let mkSTACK = function
v, TOP -> v
| STACK(0,v0,stk0), stk -> STACK(0,v0,stack_concat stk0 stk)
| v,stk -> STACK(0,v,stk)
type cbv_infos = {
env : Environ.env;
tab : (cbv_value, Empty.t) Declarations.constant_def KeyTable.t;
reds : RedFlags.reds;
sigma : Evd.evar_map
}
open RedFlags
let red_set_ref flags = function
| RelKey _ -> red_set flags fDELTA
| VarKey id -> red_set flags (fVAR id)
| ConstKey (sp,_) -> red_set flags (fCONST sp)
let strip_appl head stack =
match head with
| FIXP (fix,env,app) -> (FIXP(fix,env,[||]), stack_app app stack)
| COFIXP (cofix,env,app) -> (COFIXP(cofix,env,[||]), stack_app app stack)
| CONSTR (c,app) -> (CONSTR(c,[||]), stack_app app stack)
| PRIMITIVE(op,c,app) -> (PRIMITIVE(op,c,[||]), stack_app app stack)
| VAL _ | STACK _ | CBN _ | LAM _ | ARRAY _ -> (head, stack)
let fixp_reducible flgs ((reci,i),_) stk =
if red_set flgs fFIX then
match stk with
| APP(appl,_) ->
Array.length appl > reci.(i) &&
(match appl.(reci.(i)) with
CONSTR _ -> true
| _ -> false)
| _ -> false
else
false
let cofixp_reducible flgs _ stk =
if red_set flgs fCOFIX then
match stk with
| (CASE _ | PROJ _ | APP(_,CASE _) | APP(_,PROJ _)) -> true
| _ -> false
else
false
let debug_cbv = CDebug.create ~name:"Cbv" ()
open Primred
module VNativeEntries =
struct
type elem = cbv_value
type args = cbv_value array
type evd = unit
type uinstance = Univ.Instance.t
let get = Array.get
let get_int () e =
match e with
| VAL(_, ci) ->
(match kind ci with
| Int i -> i
| _ -> raise Primred.NativeDestKO)
| _ -> raise Primred.NativeDestKO
let get_float () e =
match e with
| VAL(_, cf) ->
(match kind cf with
| Float f -> f
| _ -> raise Primred.NativeDestKO)
| _ -> raise Primred.NativeDestKO
let get_parray () e =
match e with
| ARRAY(_u,t,_ty) -> t
| _ -> raise Primred.NativeDestKO
let mkInt env i = VAL(0, mkInt i)
let mkFloat env f = VAL(0, mkFloat f)
let mkBool env b =
let (ct,cf) = get_bool_constructors env in
CONSTR(Univ.in_punivs (if b then ct else cf), [||])
let int_ty env = VAL(0, mkConst @@ get_int_type env)
let float_ty env = VAL(0, mkConst @@ get_float_type env)
let mkCarry env b e =
let (c0,c1) = get_carry_constructors env in
CONSTR(Univ.in_punivs (if b then c1 else c0), [|int_ty env;e|])
let mkIntPair env e1 e2 =
let int_ty = int_ty env in
let c = get_pair_constructor env in
CONSTR(Univ.in_punivs c, [|int_ty;int_ty;e1;e2|])
let mkFloatIntPair env f i =
let float_ty = float_ty env in
let int_ty = int_ty env in
let c = get_pair_constructor env in
CONSTR(Univ.in_punivs c, [|float_ty;int_ty;f;i|])
let mkLt env =
let (_eq,lt,_gt) = get_cmp_constructors env in
CONSTR(Univ.in_punivs lt, [||])
let mkEq env =
let (eq,_lt,_gt) = get_cmp_constructors env in
CONSTR(Univ.in_punivs eq, [||])
let mkGt env =
let (_eq,_lt,gt) = get_cmp_constructors env in
CONSTR(Univ.in_punivs gt, [||])
let mkFLt env =
let (_eq,lt,_gt,_nc) = get_f_cmp_constructors env in
CONSTR(Univ.in_punivs lt, [||])
let mkFEq env =
let (eq,_lt,_gt,_nc) = get_f_cmp_constructors env in
CONSTR(Univ.in_punivs eq, [||])
let mkFGt env =
let (_eq,_lt,gt,_nc) = get_f_cmp_constructors env in
CONSTR(Univ.in_punivs gt, [||])
let mkFNotComparable env =
let (_eq,_lt,_gt,nc) = get_f_cmp_constructors env in
CONSTR(Univ.in_punivs nc, [||])
let mkPNormal env =
let (pNormal,_nNormal,_pSubn,_nSubn,_pZero,_nZero,_pInf,_nInf,_nan) =
get_f_class_constructors env in
CONSTR(Univ.in_punivs pNormal, [||])
let mkNNormal env =
let (_pNormal,nNormal,_pSubn,_nSubn,_pZero,_nZero,_pInf,_nInf,_nan) =
get_f_class_constructors env in
CONSTR(Univ.in_punivs nNormal, [||])
let mkPSubn env =
let (_pNormal,_nNormal,pSubn,_nSubn,_pZero,_nZero,_pInf,_nInf,_nan) =
get_f_class_constructors env in
CONSTR(Univ.in_punivs pSubn, [||])
let mkNSubn env =
let (_pNormal,_nNormal,_pSubn,nSubn,_pZero,_nZero,_pInf,_nInf,_nan) =
get_f_class_constructors env in
CONSTR(Univ.in_punivs nSubn, [||])
let mkPZero env =
let (_pNormal,_nNormal,_pSubn,_nSubn,pZero,_nZero,_pInf,_nInf,_nan) =
get_f_class_constructors env in
CONSTR(Univ.in_punivs pZero, [||])
let mkNZero env =
let (_pNormal,_nNormal,_pSubn,_nSubn,_pZero,nZero,_pInf,_nInf,_nan) =
get_f_class_constructors env in
CONSTR(Univ.in_punivs nZero, [||])
let mkPInf env =
let (_pNormal,_nNormal,_pSubn,_nSubn,_pZero,_nZero,pInf,_nInf,_nan) =
get_f_class_constructors env in
CONSTR(Univ.in_punivs pInf, [||])
let mkNInf env =
let (_pNormal,_nNormal,_pSubn,_nSubn,_pZero,_nZero,_pInf,nInf,_nan) =
get_f_class_constructors env in
CONSTR(Univ.in_punivs nInf, [||])
let mkNaN env =
let (_pNormal,_nNormal,_pSubn,_nSubn,_pZero,_nZero,_pInf,_nInf,nan) =
get_f_class_constructors env in
CONSTR(Univ.in_punivs nan, [||])
let mkArray env u t ty =
ARRAY (u,t,ty)
end
module VredNative = RedNative(VNativeEntries)
let debug_pr_key = function
| ConstKey (sp,_) -> Names.Constant.print sp
| VarKey id -> Names.Id.print id
| RelKey n -> Pp.(str "REL_" ++ int n)
let rec reify_stack t = function
| TOP -> t
| APP (args,st) ->
reify_stack (mkApp(t,Array.map reify_value args)) st
| CASE (u,pms,ty,br,iv,ci,env,st) ->
reify_stack
(mkCase (ci, u, pms, ty, iv, t,br))
st
| PROJ (p, st) ->
reify_stack (mkProj (p, t)) st
and reify_value = function
| VAL (n,t) -> lift n t
| STACK (0,v,stk) ->
reify_stack (reify_value v) stk
| STACK (n,v,stk) ->
lift n (reify_stack (reify_value v) stk)
| CBN(t,env) ->
apply_env env t
| LAM (k,ctxt,b,env) ->
apply_env env @@
List.fold_left (fun c (n,t) ->
mkLambda (n, t, c)) b ctxt
| FIXP ((lij,fix),env,args) ->
let fix = mkFix (lij, fix) in
mkApp (apply_env env fix, Array.map reify_value args)
| COFIXP ((j,cofix),env,args) ->
let cofix = mkCoFix (j, cofix) in
mkApp (apply_env env cofix, Array.map reify_value args)
| CONSTR (c,args) ->
mkApp(mkConstructU c, Array.map reify_value args)
| PRIMITIVE(op,c,args) ->
mkApp(mkConstU c, Array.map reify_value args)
| ARRAY (u,t,ty) ->
let t, def = Parray.to_array t in
mkArray(u, Array.map reify_value t, reify_value def, reify_value ty)
and apply_env env t =
match kind t with
| Rel i ->
begin match expand_rel i env with
| Inl (k, v) ->
reify_value (shift_value k v)
| Inr (k,_) ->
mkRel k
end
| _ ->
map_with_binders subs_lift apply_env env t
let rec strip_app = function
| APP (args,st) -> APP (args,strip_app st)
| s -> TOP
let rec subs_consn v i n s =
if Int.equal i n then s
else subs_consn v (i + 1) n (subs_cons v.(i) s)
let expand_branch env u pms (ind, i) br =
let open Declarations in
let nas, _br = br.(i - 1) in
let (mib, mip) = Inductive.lookup_mind_specif env ind in
let paramdecl = Vars.subst_instance_context u mib.mind_params_ctxt in
let paramsubst = Vars.subst_of_rel_context_instance paramdecl (Array.to_list pms) in
let subst = paramsubst @ Inductive.ind_subst (fst ind) mib u in
let (ctx, _) = mip.mind_nf_lc.(i - 1) in
let (ctx, _) = List.chop mip.mind_consnrealdecls.(i - 1) ctx in
Inductive.instantiate_context u subst nas ctx
let cbv_subst_of_rel_context_instance mkclos sign args env =
let rec aux subst sign l =
let open Context.Rel.Declaration in
match sign, l with
| LocalAssum _ :: sign', a::args' -> aux (subs_cons a subst) sign' args'
| LocalDef (_,c,_)::sign', args' ->
aux (subs_cons (mkclos subst c) subst) sign' args'
| [], [] -> subst
| _ -> CErrors.anomaly (Pp.str "Instance and signature do not match.")
in aux env (List.rev sign) (Array.to_list args)
let rec norm_head info env t stack =
match kind t with
| App (head,args) ->
let nargs = Array.map (cbv_stack_term info TOP env) args in
norm_head info env head (stack_app nargs stack)
| Case (ci,u,pms,p,iv,c,v) -> norm_head info env c (CASE(u,pms,p,v,iv,ci,env,stack))
| Cast (ct,_,_) -> norm_head info env ct stack
| Proj (p, c) ->
let p' =
if red_set info.reds (fCONST (Projection.constant p))
&& red_set info.reds fBETA
then Projection.unfold p
else p
in
norm_head info env c (PROJ (p', stack))
| Rel i ->
(match expand_rel i env with
| Inl (0,v) -> strip_appl v stack
| Inl (n,v) -> strip_appl (shift_value n v) stack
| Inr (n,None) -> (VAL(0, mkRel n), stack)
| Inr (n,Some p) -> norm_head_ref (n-p) info env stack (RelKey p) t)
| Var id -> norm_head_ref 0 info env stack (VarKey id) t
| Const sp ->
Reductionops.reduction_effect_hook info.env info.sigma
(fst sp) (lazy (reify_stack t (strip_app stack)));
norm_head_ref 0 info env stack (ConstKey sp) t
| LetIn (_, b, _, c) ->
if red_set info.reds fZETA then
let env' = subs_cons (cbv_stack_term info TOP env b) env in
norm_head info env' c stack
else
(CBN(t,env), stack)
| Evar ev ->
(match Reductionops.safe_evar_value info.sigma ev with
Some c -> norm_head info env c stack
| None ->
let e, xs = ev in
let xs' = List.map (apply_env env) xs in
(VAL(0, mkEvar (e,xs')), stack))
| Lambda _ ->
let ctxt,b = Term.decompose_lam t in
(LAM(List.length ctxt, List.rev ctxt,b,env), stack)
| Fix fix -> (FIXP(fix,env,[||]), stack)
| CoFix cofix -> (COFIXP(cofix,env,[||]), stack)
| Construct c -> (CONSTR(c, [||]), stack)
| Array(u,t,def,ty) ->
let ty = cbv_stack_term info TOP env ty in
let len = Array.length t in
let t =
Parray.init (Uint63.of_int len)
(fun i -> cbv_stack_term info TOP env t.(i))
(cbv_stack_term info TOP env def) in
(ARRAY (u,t,ty), stack)
| (Sort _ | Meta _ | Ind _ | Int _ | Float _) -> (VAL(0, t), stack)
| Prod _ -> (CBN(t,env), stack)
and norm_head_ref k info env stack normt t =
if red_set_ref info.reds normt then
match cbv_value_cache info normt with
| Declarations.Def body ->
debug_cbv (fun () -> Pp.(str "Unfolding " ++ debug_pr_key normt));
strip_appl (shift_value k body) stack
| Declarations.Primitive op ->
let c = match normt with
| ConstKey c -> c
| RelKey _ | VarKey _ -> assert false
in
(PRIMITIVE(op,c,[||]),stack)
| Declarations.OpaqueDef _ | Declarations.Undef _ ->
debug_cbv (fun () -> Pp.(str "Not unfolding " ++ debug_pr_key normt));
(VAL(0,make_constr_ref k normt t),stack)
else
begin
debug_cbv (fun () -> Pp.(str "Not unfolding " ++ debug_pr_key normt));
(VAL(0,make_constr_ref k normt t),stack)
end
and cbv_stack_term info stack env t =
cbv_stack_value info env (norm_head info env t stack)
and cbv_stack_value info env = function
| (LAM (nlams,ctxt,b,env), APP (args, stk))
when red_set info.reds fBETA ->
let nargs = Array.length args in
if nargs == nlams then
cbv_stack_term info stk (subs_consn args 0 nargs env) b
else if nlams < nargs then
let env' = subs_consn args 0 nlams env in
let eargs = Array.sub args nlams (nargs-nlams) in
cbv_stack_term info (APP(eargs,stk)) env' b
else
let ctxt' = List.skipn nargs ctxt in
LAM(nlams-nargs,ctxt', b, subs_consn args 0 nargs env)
| (FIXP(fix,env,[||]), stk)
when fixp_reducible info.reds fix stk ->
let (envf,redfix) = contract_fixp env fix in
cbv_stack_term info stk envf redfix
| (COFIXP(cofix,env,[||]), stk)
when cofixp_reducible info.reds cofix stk->
let (envf,redfix) = contract_cofixp env cofix in
cbv_stack_term info stk envf redfix
| (CONSTR(((sp,n),_),[||]), APP(args,CASE(u,pms,_p,br,iv,ci,env,stk)))
when red_set info.reds fMATCH ->
let nargs = Array.length args - ci.ci_npar in
let cargs =
Array.sub args ci.ci_npar nargs in
let env =
if (Int.equal ci.ci_cstr_ndecls.(n - 1) ci.ci_cstr_nargs.(n - 1)) then
subs_consn cargs 0 nargs env
else
let mkclos env c = cbv_stack_term info TOP env c in
let ctx = expand_branch info.env u pms (sp, n) br in
cbv_subst_of_rel_context_instance mkclos ctx cargs env
in
cbv_stack_term info stk env (snd br.(n-1))
| (CONSTR(((sp, n), _),[||]), CASE(u,pms,_,br,_,ci,env,stk))
when red_set info.reds fMATCH ->
let env =
if (Int.equal ci.ci_cstr_ndecls.(n - 1) ci.ci_cstr_nargs.(n - 1)) then
env
else
let mkclos env c = cbv_stack_term info TOP env c in
let ctx = expand_branch info.env u pms (sp, n) br in
cbv_subst_of_rel_context_instance mkclos ctx [||] env
in
cbv_stack_term info stk env (snd br.(n-1))
| (CONSTR(((sp,n),u),[||]), APP(args,PROJ(p,stk)))
when red_set info.reds fMATCH && Projection.unfolded p ->
let arg = args.(Projection.npars p + Projection.arg p) in
cbv_stack_value info env (strip_appl arg stk)
| (FIXP(fix,env,[||]), APP(appl,TOP)) -> FIXP(fix,env,appl)
| (COFIXP(cofix,env,[||]), APP(appl,TOP)) -> COFIXP(cofix,env,appl)
| (CONSTR(c,[||]), APP(appl,TOP)) -> CONSTR(c,appl)
| (PRIMITIVE(op,(_,u as c),[||]), APP(appl,stk)) ->
let nargs = CPrimitives.arity op in
let len = Array.length appl in
if nargs <= len then
let args =
if len = nargs then appl
else Array.sub appl 0 nargs in
let stk =
if nargs < len then
stack_app (Array.sub appl nargs (len - nargs)) stk
else stk in
match VredNative.red_prim info.env () op u args with
| Some (CONSTR (c, args)) ->
cbv_stack_value info env (CONSTR(c, [||]), stack_app args stk)
| Some v -> cbv_stack_value info env (v,stk)
| None -> mkSTACK(PRIMITIVE(op,c,args), stk)
else
(assert (stk = TOP);
PRIMITIVE(op,c,appl))
| (head,stk) -> mkSTACK(head, stk)
and cbv_value_cache info ref =
try KeyTable.find info.tab ref with
Not_found ->
let v =
try
let body = match ref with
| RelKey n ->
let open Context.Rel.Declaration in
begin match Environ.lookup_rel n info.env with
| LocalDef (_, c, _) -> lift n c
| LocalAssum _ -> raise Not_found
end
| VarKey id ->
let open Context.Named.Declaration in
begin match Environ.lookup_named id info.env with
| LocalDef (_, c, _) -> c
| LocalAssum _ -> raise Not_found
end
| ConstKey cst -> Environ.constant_value_in info.env cst
in
let v = cbv_stack_term info TOP (subs_id 0) body in
Declarations.Def v
with
| Environ.NotEvaluableConst (Environ.IsPrimitive (_u,op)) -> Declarations.Primitive op
| Not_found | Environ.NotEvaluableConst _ -> Declarations.Undef None
in
KeyTable.add info.tab ref v; v
let rec apply_stack info t = function
| TOP -> t
| APP (args,st) ->
apply_stack info (mkApp(t,Array.map (cbv_norm_value info) args)) st
| CASE (u,pms,ty,br,iv,ci,env,st) ->
let (_, ty, _, _, br) = Inductive.expand_case info.env (ci, u, pms, ty, iv, mkProp, br) in
let ty =
let (_, mip) = Inductive.lookup_mind_specif info.env ci.ci_ind in
Term.decompose_lam_n_decls (mip.Declarations.mind_nrealdecls + 1) ty
in
let mk_br c n = Term.decompose_lam_n_decls n c in
let br = Array.map2 mk_br br ci.ci_cstr_ndecls in
let map_ctx (nas, c) =
let open Context.Rel.Declaration in
let fold decl e = match decl with
| LocalAssum _ -> subs_lift e
| LocalDef (_, b, _) ->
let b = cbv_stack_term info TOP e b in
subs_shft (1, subs_cons b e)
in
let env = List.fold_right fold nas env in
let nas = Array.of_list (List.rev_map get_annot nas) in
(nas, cbv_norm_term info env c)
in
apply_stack info
(mkCase (ci, u, Array.map (cbv_norm_term info env) pms, map_ctx ty, iv, t,
Array.map map_ctx br))
st
| PROJ (p, st) ->
apply_stack info (mkProj (p, t)) st
and cbv_norm_term info env t =
cbv_norm_value info (cbv_stack_term info TOP env t)
and cbv_norm_value info = function
| VAL (n,t) -> lift n t
| STACK (0,v,stk) ->
apply_stack info (cbv_norm_value info v) stk
| STACK (n,v,stk) ->
lift n (apply_stack info (cbv_norm_value info v) stk)
| CBN(t,env) ->
Constr.map_with_binders subs_lift (cbv_norm_term info) env t
| LAM (n,ctxt,b,env) ->
let nctxt =
List.map_i (fun i (x,ty) ->
(x,cbv_norm_term info (subs_liftn i env) ty)) 0 ctxt in
Term.compose_lam (List.rev nctxt) (cbv_norm_term info (subs_liftn n env) b)
| FIXP ((lij,(names,lty,bds)),env,args) ->
mkApp
(mkFix (lij,
(names,
Array.map (cbv_norm_term info env) lty,
Array.map (cbv_norm_term info
(subs_liftn (Array.length lty) env)) bds)),
Array.map (cbv_norm_value info) args)
| COFIXP ((j,(names,lty,bds)),env,args) ->
mkApp
(mkCoFix (j,
(names,Array.map (cbv_norm_term info env) lty,
Array.map (cbv_norm_term info
(subs_liftn (Array.length lty) env)) bds)),
Array.map (cbv_norm_value info) args)
| CONSTR (c,args) ->
mkApp(mkConstructU c, Array.map (cbv_norm_value info) args)
| PRIMITIVE(op,c,args) ->
mkApp(mkConstU c,Array.map (cbv_norm_value info) args)
| ARRAY (u,t,ty) ->
let ty = cbv_norm_value info ty in
let t, def = Parray.to_array t in
let def = cbv_norm_value info def in
mkArray(u, Array.map (cbv_norm_value info) t, def, ty)
let cbv_norm infos constr =
let constr = EConstr.Unsafe.to_constr constr in
EConstr.of_constr (with_stats (lazy (cbv_norm_term infos (subs_id 0) constr)))
let create_cbv_infos reds env sigma =
{ tab = KeyTable.create 91; reds; env; sigma }