Debian Codegraph @

kernel/reduction.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 open CErrors
12 open Util
13 open Constr
14 open Vars
15 open Environ
16 open CClosure
17 open Context.Rel.Declaration
18
19 (****************************************************************************)
20 (* Reduction Functions *)
21 (****************************************************************************)
22
23 let whd_all ?evars env t =
24 match kind t with
25 | (Sort _|Meta _|Ind _|Construct _|
26 Prod _|Lambda _|Fix _|CoFix _|Int _|Float _|String _|Array _) -> t
27 | App (c, _) ->
28 begin match kind c with
29 | Ind _ | Construct _ | Meta _ | Int _ | Float _ | String _ | Array _ -> t
30 | Sort _ | Rel _ | Var _ | Evar _ | Cast _ | Prod _ | Lambda _ | LetIn _ | App _
31 | Const _ |Case _ | Fix _ | CoFix _ | Proj _ ->
32 whd_val (create_clos_infos ?evars RedFlags.all env) (create_tab ()) (inject t)
33 end
34 | Rel _ | Evar _ | Cast _ | LetIn _ | Case _ | Proj _ | Const _ | Var _ ->
35 whd_val (create_clos_infos ?evars RedFlags.all env) (create_tab ()) (inject t)
36
37 let whd_allnolet ?evars env t =
38 match kind t with
39 | (Sort _|Meta _|Ind _|Construct _|
40 Prod _|Lambda _|Fix _|CoFix _|LetIn _|Int _|Float _|String _|Array _) -> t
41 | App (c, _) ->
42 begin match kind c with
43 | Ind _ | Construct _ | Meta _ | LetIn _ | Int _ | Float _ | String _ | Array _ -> t
44 | Sort _ | Rel _ | Var _ | Evar _ | Cast _ | Prod _ | Lambda _ | App _
45 | Const _ | Case _ | Fix _ | CoFix _ | Proj _ ->
46 whd_val (create_clos_infos ?evars RedFlags.allnolet env) (create_tab ()) (inject t)
47 end
48 | Rel _ | Evar _ | Cast _ | Case _ | Proj _ | Const _ | Var _ ->
49 whd_val (create_clos_infos ?evars RedFlags.allnolet env) (create_tab ()) (inject t)
50
51 (* Application with on-the-fly reduction *)
52
53 let beta_applist c l =
54 let rec app subst c l =
55 match kind c, l with
56 | Lambda(_,_,c), arg::l -> app (arg::subst) c l
57 | _ -> Term.applist (substl subst c, l) in
58 app [] c l
59
60 let beta_appvect c v = beta_applist c (Array.to_list v)
61
62 let beta_app c a = beta_applist c [a]
63
64 (* Compatibility *)
65 let betazeta_appvect = Term.lambda_appvect_decls
66
67 (********************************************************************)
68 (* Special-Purpose Reduction *)
69 (********************************************************************)
70
71 (* pseudo-reduction rule:
72 * [hnf_prod_app env (Prod(_,B)) N --> B[N]
73 * with an HNF on the first argument to produce a product.
74 * if this does not work, then we use the string S as part of our
75 * error message. *)
76
77 let hnf_prod_app ?evars env t n =
78 match kind (whd_all ?evars env t) with
79 | Prod (_,_,b) -> subst1 n b
80 | _ -> anomaly ~label:"hnf_prod_app" (Pp.str "Need a product.")
81
82 let hnf_prod_applist ?evars env t nl =
83 List.fold_left (hnf_prod_app ?evars env) t nl
84
85 let hnf_prod_applist_decls ?evars env n c l =
86 let rec app n subst t l =
87 if Int.equal n 0 then
88 if l == [] then substl subst t
89 else anomaly (Pp.str "Too many arguments.")
90 else match kind (whd_allnolet ?evars env t), l with
91 | Prod(_,_,c), arg::l -> app (n-1) (arg::subst) c l
92 | LetIn(_,b,_,c), _ -> app (n-1) (substl subst b::subst) c l
93 | _, [] -> anomaly (Pp.str "Not enough arguments.")
94 | _ -> anomaly (Pp.str "Not enough prod/let's.") in
95 app n [] c l
96
97 (* Dealing with arities *)
98
99 let whd_decompose_prod ?evars env =
100 let rec decrec env m c =
101 let t = whd_all ?evars env c in
102 match kind t with
103 | Prod (n,a,c0) ->
104 let d = LocalAssum (n,a) in
105 decrec (push_rel d env) (Context.Rel.add d m) c0
106 | _ -> m,t
107 in
108 decrec env Context.Rel.empty
109
110 let whd_decompose_lambda ?evars env =
111 let rec decrec env m c =
112 let t = whd_all ?evars env c in
113 match kind t with
114 | Lambda (n,a,c0) ->
115 let d = LocalAssum (n,a) in
116 decrec (push_rel d env) (Context.Rel.add d m) c0
117 | _ -> m,t
118 in
119 decrec env Context.Rel.empty
120
121 (* The same but preserving lets in the context, not internal ones. *)
122 let whd_decompose_prod_decls ?evars env =
123 let rec prodec_rec env l ty =
124 let rty = whd_allnolet ?evars env ty in
125 match kind rty with
126 | Prod (x,t,c) ->
127 let d = LocalAssum (x,t) in
128 prodec_rec (push_rel d env) (Context.Rel.add d l) c
129 | LetIn (x,b,t,c) ->
130 let d = LocalDef (x,b,t) in
131 prodec_rec (push_rel d env) (Context.Rel.add d l) c
132 | _ ->
133 let rty' = whd_all ?evars env rty in
134 if Constr.equal rty' rty then l, rty
135 else prodec_rec env l rty'
136 in
137 prodec_rec env Context.Rel.empty
138
139 let whd_decompose_lambda_decls ?evars env =
140 let rec lamec_rec env l ty =
141 let rty = whd_allnolet ?evars env ty in
142 match kind rty with
143 | Lambda (x,t,c) ->
144 let d = LocalAssum (x,t) in
145 lamec_rec (push_rel d env) (Context.Rel.add d l) c
146 | LetIn (x,b,t,c) ->
147 let d = LocalDef (x,b,t) in
148 lamec_rec (push_rel d env) (Context.Rel.add d l) c
149 | _ ->
150 let rty' = whd_all ?evars env rty in
151 if Constr.equal rty' rty then l, rty
152 else lamec_rec env l rty'
153 in
154 lamec_rec env Context.Rel.empty
155
156 let whd_decompose_lambda_n_assum ?evars env n =
157 let rec lamec_rec env n l c =
158 if Int.equal n 0 then l,c
159 else
160 let rc = whd_allnolet ?evars env c in
161 match kind rc with
162 | Lambda (x,t,c) ->
163 let d = LocalAssum (x,t) in
164 lamec_rec (push_rel d env) (n-1) (Context.Rel.add d l) c
165 | LetIn (x,b,t,c) ->
166 let d = LocalDef (x,b,t) in
167 lamec_rec (push_rel d env) n (Context.Rel.add d l) c
168 | _ ->
169 let c' = whd_all ?evars env c in
170 if Constr.equal c' c then anomaly (Pp.str "whd_decompose_lambda_n_assum: not enough abstractions")
171 else lamec_rec env n l c'
172 in
173 lamec_rec env n Context.Rel.empty
174
175 exception NotArity
176
177 let dest_arity ?evars env c =
178 let l, c = whd_decompose_prod_decls ?evars env c in
179 match kind c with
180 | Sort s -> l,s
181 | _ -> raise NotArity
182
183 let is_arity ?evars env c =
184 try
185 let _ = dest_arity ?evars env c in
186 true
187 with NotArity -> false
188
189 let eta_expand ?evars env t ty =
190 let ctxt, _codom = whd_decompose_prod ?evars env ty in
191 let ctxt',t = whd_decompose_lambda ?evars env t in
192 let d = Context.Rel.nhyps ctxt - Context.Rel.nhyps ctxt' in
193 let eta_args = List.rev_map mkRel (List.interval 1 d) in
194 let t = Term.applistc (Vars.lift d t) eta_args in
195 let t = Term.it_mkLambda_or_LetIn t (List.firstn d ctxt) in
196 Term.it_mkLambda_or_LetIn t ctxt'