Debian Codegraph @

kernel/term.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 Util
12 open Pp
13 open CErrors
14 open Names
15 open Vars
16 open Constr
17 open Context
18
19 (****************************************************************************)
20 (* Functions for dealing with constr terms *)
21 (****************************************************************************)
22
23 (***************************)
24 (* Other term constructors *)
25 (***************************)
26
27 let name_annot = map_annot Name.mk_name
28
29 let mkNamedProd id typ c = mkProd (name_annot id, typ, subst_var id.binder_name c)
30 let mkNamedLambda id typ c = mkLambda (name_annot id, typ, subst_var id.binder_name c)
31 let mkNamedLetIn id c1 t c2 = mkLetIn (name_annot id, c1, t, subst_var id.binder_name c2)
32
33 (* Constructs either [(x:t)c] or [[x=b:t]c] *)
34 let mkProd_or_LetIn decl c =
35 let open Context.Rel.Declaration in
36 match decl with
37 | LocalAssum (na,t) -> mkProd (na, t, c)
38 | LocalDef (na,b,t) -> mkLetIn (na, b, t, c)
39
40 let mkNamedProd_or_LetIn decl c =
41 let open Context.Named.Declaration in
42 match decl with
43 | LocalAssum (id,t) -> mkNamedProd id t c
44 | LocalDef (id,b,t) -> mkNamedLetIn id b t c
45
46 (* Constructs either [(x:t)c] or [c] where [x] is replaced by [b] *)
47 let mkProd_wo_LetIn decl c =
48 let open Context.Rel.Declaration in
49 match decl with
50 | LocalAssum (na,t) -> mkProd (na, t, c)
51 | LocalDef (_na,b,_t) -> subst1 b c
52
53 let mkNamedProd_wo_LetIn decl c =
54 let open Context.Named.Declaration in
55 match decl with
56 | LocalAssum (id,t) -> mkNamedProd id t c
57 | LocalDef (id,b,_) -> subst1 b (subst_var id.binder_name c)
58
59 (* non-dependent product t1 -> t2 *)
60 let mkArrow t1 r t2 = mkProd (make_annot Anonymous r, t1, t2)
61 let mkArrowR t1 t2 = mkArrow t1 Sorts.Relevant t2
62
63 (* Constructs either [[x:t]c] or [[x=b:t]c] *)
64 let mkLambda_or_LetIn decl c =
65 let open Context.Rel.Declaration in
66 match decl with
67 | LocalAssum (na,t) -> mkLambda (na, t, c)
68 | LocalDef (na,b,t) -> mkLetIn (na, b, t, c)
69
70 let mkNamedLambda_or_LetIn decl c =
71 let open Context.Named.Declaration in
72 match decl with
73 | LocalAssum (id,t) -> mkNamedLambda id t c
74 | LocalDef (id,b,t) -> mkNamedLetIn id b t c
75
76 (* prodn n [xn:Tn;..;x1:T1;Gamma] b = (x1:T1)..(xn:Tn)b *)
77 let prodn n env b =
78 let rec prodrec = function
79 | (0, _env, b) -> b
80 | (n, ((v,t)::l), b) -> prodrec (n-1, l, mkProd (v,t,b))
81 | _ -> assert false
82 in
83 prodrec (n,env,b)
84
85 (* compose_prod [xn:Tn;..;x1:T1] b = (x1:T1)..(xn:Tn)b *)
86 let compose_prod l b = prodn (List.length l) l b
87
88 (* lamn n [xn:Tn;..;x1:T1;Gamma] b = [x1:T1]..[xn:Tn]b *)
89 let lamn n env b =
90 let rec lamrec = function
91 | (0, _env, b) -> b
92 | (n, ((v,t)::l), b) -> lamrec (n-1, l, mkLambda (v,t,b))
93 | _ -> assert false
94 in
95 lamrec (n,env,b)
96
97 (* compose_lam [xn:Tn;..;x1:T1] b = [x1:T1]..[xn:Tn]b *)
98 let compose_lam l b = lamn (List.length l) l b
99
100 let applist (f,l) = mkApp (f, Array.of_list l)
101
102 let applistc f l = mkApp (f, Array.of_list l)
103
104 let appvect = mkApp
105
106 let appvectc f l = mkApp (f,l)
107
108 (* to_lambda n (x1:T1)...(xn:Tn)T =
109 * [x1:T1]...[xn:Tn]T *)
110 let rec to_lambda n prod =
111 if Int.equal n 0 then
112 prod
113 else
114 match kind prod with
115 | Prod (na,ty,bd) -> mkLambda (na,ty,to_lambda (n-1) bd)
116 | Cast (c,_,_) -> to_lambda n c
117 | _ -> anomaly Pp.(str "Not enough lambda's.")
118
119 let rec to_prod n lam =
120 if Int.equal n 0 then
121 lam
122 else
123 match kind lam with
124 | Lambda (na,ty,bd) -> mkProd (na,ty,to_prod (n-1) bd)
125 | Cast (c,_,_) -> to_prod n c
126 | _ -> anomaly Pp.(str "Not enough prod's.")
127
128 let it_mkProd_or_LetIn = List.fold_left (fun c d -> mkProd_or_LetIn d c)
129 let it_mkProd_wo_LetIn = List.fold_left (fun c d -> mkProd_wo_LetIn d c)
130 let it_mkLambda_or_LetIn = List.fold_left (fun c d -> mkLambda_or_LetIn d c)
131
132 (* Application with expected on-the-fly reduction *)
133
134 let lambda_applist c l =
135 let rec app subst c l =
136 match kind c, l with
137 | Lambda(_,_,c), arg::l -> app (arg::subst) c l
138 | _, [] -> substl subst c
139 | _ -> anomaly (Pp.str "Not enough lambda's.") in
140 app [] c l
141
142 let lambda_appvect c v = lambda_applist c (Array.to_list v)
143
144 let lambda_applist_decls n c l =
145 let rec app n subst t l =
146 if Int.equal n 0 then
147 if l == [] then substl subst t
148 else anomaly (Pp.str "Too many arguments.")
149 else match kind t, l with
150 | Lambda(_,_,c), arg::l -> app (n-1) (arg::subst) c l
151 | LetIn(_,b,_,c), _ -> app (n-1) (substl subst b::subst) c l
152 | _, [] -> anomaly (Pp.str "Not enough arguments.")
153 | _ -> anomaly (Pp.str "Not enough lambda/let's.") in
154 app n [] c l
155
156 let lambda_appvect_decls n c v = lambda_applist_decls n c (Array.to_list v)
157
158 (* prod_applist T [ a1 ; ... ; an ] -> (T a1 ... an) *)
159 let prod_applist c l =
160 let rec app subst c l =
161 match kind c, l with
162 | Prod(_,_,c), arg::l -> app (arg::subst) c l
163 | _, [] -> substl subst c
164 | _ -> anomaly (Pp.str "Not enough prod's.") in
165 app [] c l
166
167 (* prod_appvect T [| a1 ; ... ; an |] -> (T a1 ... an) *)
168 let prod_appvect c v = prod_applist c (Array.to_list v)
169
170 let prod_applist_decls n c l =
171 let rec app n subst t l =
172 if Int.equal n 0 then
173 if l == [] then substl subst t
174 else anomaly (Pp.str "Too many arguments.")
175 else match kind t, l with
176 | Prod(_,_,c), arg::l -> app (n-1) (arg::subst) c l
177 | LetIn(_,b,_,c), _ -> app (n-1) (substl subst b::subst) c l
178 | _, [] -> anomaly (Pp.str "Not enough arguments.")
179 | _ -> anomaly (Pp.str "Not enough prod/let's.") in
180 app n [] c l
181
182 let prod_appvect_decls n c v = prod_applist_decls n c (Array.to_list v)
183
184 (*********************************)
185 (* Other term destructors *)
186 (*********************************)
187
188 (* Transforms a product term (x1:T1)..(xn:Tn)T into the pair
189 ([(xn,Tn);...;(x1,T1)],T), where T is not a product *)
190 let decompose_prod =
191 let rec prodec_rec l c = match kind c with
192 | Prod (x,t,c) -> prodec_rec ((x,t)::l) c
193 | Cast (c,_,_) -> prodec_rec l c
194 | _ -> l,c
195 in
196 prodec_rec []
197
198 (* Transforms a lambda term [x1:T1]..[xn:Tn]T into the pair
199 ([(xn,Tn);...;(x1,T1)],T), where T is not a lambda *)
200 let decompose_lambda =
201 let rec lamdec_rec l c = match kind c with
202 | Lambda (x,t,c) -> lamdec_rec ((x,t)::l) c
203 | Cast (c,_,_) -> lamdec_rec l c
204 | _ -> l,c
205 in
206 lamdec_rec []
207
208 (* Given a positive integer n, transforms a product term (x1:T1)..(xn:Tn)T
209 into the pair ([(xn,Tn);...;(x1,T1)],T) *)
210 let decompose_prod_n n =
211 if n < 0 then anomaly (str "decompose_prod_n: integer parameter must be positive.");
212 let rec prodec_rec l n c =
213 if Int.equal n 0 then l,c
214 else match kind c with
215 | Prod (x,t,c) -> prodec_rec ((x,t)::l) (n-1) c
216 | Cast (c,_,_) -> prodec_rec l n c
217 | _ -> anomaly (str "decompose_prod_n: not enough products.")
218 in
219 prodec_rec [] n
220
221 (* Given a positive integer n, transforms a lambda term [x1:T1]..[xn:Tn]T
222 into the pair ([(xn,Tn);...;(x1,T1)],T) *)
223 let decompose_lambda_n n =
224 if n < 0 then anomaly (str "decompose_lambda_n: integer parameter must be positive.");
225 let rec lamdec_rec l n c =
226 if Int.equal n 0 then l,c
227 else match kind c with
228 | Lambda (x,t,c) -> lamdec_rec ((x,t)::l) (n-1) c
229 | Cast (c,_,_) -> lamdec_rec l n c
230 | _ -> anomaly (str "decompose_lambda_n: not enough abstractions.")
231 in
232 lamdec_rec [] n
233
234 (* Transforms a product term (x1:T1)..(xn:Tn)T into the pair
235 ([(xn,Tn);...;(x1,T1)],T), where T is not a product *)
236 let decompose_prod_decls =
237 let open Context.Rel.Declaration in
238 let rec prodec_rec l c =
239 match kind c with
240 | Prod (x,t,c) -> prodec_rec (Context.Rel.add (LocalAssum (x,t)) l) c
241 | LetIn (x,b,t,c) -> prodec_rec (Context.Rel.add (LocalDef (x,b,t)) l) c
242 | Cast (c,_,_) -> prodec_rec l c
243 | _ -> l,c
244 in
245 prodec_rec Context.Rel.empty
246
247 (* Transforms a lambda term [x1:T1]..[xn:Tn]T into the pair
248 ([(xn,Tn);...;(x1,T1)],T), where T is not a lambda *)
249 let decompose_lambda_decls =
250 let rec lamdec_rec l c =
251 let open Context.Rel.Declaration in
252 match kind c with
253 | Lambda (x,t,c) -> lamdec_rec (Context.Rel.add (LocalAssum (x,t)) l) c
254 | LetIn (x,b,t,c) -> lamdec_rec (Context.Rel.add (LocalDef (x,b,t)) l) c
255 | Cast (c,_,_) -> lamdec_rec l c
256 | _ -> l,c
257 in
258 lamdec_rec Context.Rel.empty
259
260 (* Given a positive integer n, decompose a product or let-in term
261 of the form [forall (x1:T1)..(xi:=ci:Ti)..(xn:Tn), T] into the pair
262 of the quantifying context [(xn,None,Tn);..;(xi,Some
263 ci,Ti);..;(x1,None,T1)] and of the inner type [T]) *)
264 let decompose_prod_n_decls n =
265 if n < 0 then
266 anomaly (str "decompose_prod_n_decls: integer parameter must be positive.");
267 let rec prodec_rec l n c =
268 if Int.equal n 0 then l,c
269 else
270 let open Context.Rel.Declaration in
271 match kind c with
272 | Prod (x,t,c) -> prodec_rec (Context.Rel.add (LocalAssum (x,t)) l) (n-1) c
273 | LetIn (x,b,t,c) -> prodec_rec (Context.Rel.add (LocalDef (x,b,t)) l) (n-1) c
274 | Cast (c,_,_) -> prodec_rec l n c
275 | _ -> anomaly (str "decompose_prod_n_decls: not enough declarations.")
276 in
277 prodec_rec Context.Rel.empty n
278
279 let decompose_lambda_prod_n_decls n =
280 if n < 0 then
281 anomaly (str "decompose_lambda_prod_n_decls: integer parameter must be positive.");
282 let rec lamprodec_rec l n c t =
283 if Int.equal n 0 then (l, c, t)
284 else
285 let open Context.Rel.Declaration in
286 match kind c, kind t with
287 | Lambda (na, u, c), Prod (_, _, t) -> lamprodec_rec (LocalAssum (na, u) :: l) (n-1) c t
288 | LetIn (na, b, u, c), LetIn (_, _, _, t) -> lamprodec_rec (LocalDef (na, b, u) :: l) (n-1) c t
289 | _ -> anomaly (str "decompose_lambda_prod_n_decls: not same form.")
290 in
291 lamprodec_rec Context.Rel.empty n
292
293 (** Given a positive integer n, decompose a lambda term [fun
294 (x1:T1)..(xn:Tn) => T] (possibly with let-ins before xn) into the pair of the
295 abstracted context [(xn,None,Tn);...;(x1,None,T1)] and of the inner body [T]. *)
296 let decompose_lambda_n_assum n =
297 if n < 0 then
298 anomaly (str "decompose_lambda_n_assum: integer parameter must be positive.");
299 let rec lamdec_rec l n c =
300 if Int.equal n 0 then l,c
301 else
302 let open Context.Rel.Declaration in
303 match kind c with
304 | Lambda (x,t,c) -> lamdec_rec (Context.Rel.add (LocalAssum (x,t)) l) (n-1) c
305 | LetIn (x,b,t,c) -> lamdec_rec (Context.Rel.add (LocalDef (x,b,t)) l) n c
306 | Cast (c,_,_) -> lamdec_rec l n c
307 | _c -> anomaly (str "decompose_lambda_n_assum: not enough abstractions.")
308 in
309 lamdec_rec Context.Rel.empty n
310
311 (* Given a positive integer n, decompose a lambda or let-in term [fun
312 (x1:T1)..(xi:=ci:Ti)..(xn:Tn) => T] into the pair of the abstracted
313 context [(xn,None,Tn);...;(xi,Some ci,Ti);...;(x1,None,T1)] and of
314 the inner body [T].
315 Lets in between are not expanded but turn into local definitions,
316 and n is the number of lambdas and lets to decompose. *)
317 let decompose_lambda_n_decls n =
318 if n < 0 then
319 anomaly (str "decompose_lambda_n_decls: integer parameter must be positive.");
320 let rec lamdec_rec l n c =
321 if Int.equal n 0 then l,c
322 else
323 let open Context.Rel.Declaration in
324 match kind c with
325 | Lambda (x,t,c) -> lamdec_rec (Context.Rel.add (LocalAssum (x,t)) l) (n-1) c
326 | LetIn (x,b,t,c) -> lamdec_rec (Context.Rel.add (LocalDef (x,b,t)) l) (n-1) c
327 | Cast (c,_,_) -> lamdec_rec l n c
328 | _ -> anomaly (str "decompose_lambda_n_decls: not enough declarations.")
329 in
330 lamdec_rec Context.Rel.empty n
331
332 let prod_decls t = fst (decompose_prod_decls t)
333 let prod_n_decls n t = fst (decompose_prod_n_decls n t)
334 let strip_prod_decls t = snd (decompose_prod_decls t)
335 let strip_prod t = snd (decompose_prod t)
336 let strip_prod_n n t = snd (decompose_prod_n n t)
337 let lambda_decls t = fst (decompose_lambda_decls t)
338 let lam_n_assum n t = fst (decompose_lambda_n_assum n t)
339 let strip_lambda_decls t = snd (decompose_lambda_decls t)
340 let strip_lam t = snd (decompose_lambda t)
341 let strip_lam_n n t = snd (decompose_lambda_n n t)
342
343 (***************************)
344 (* Arities *)
345 (***************************)
346
347 (* An "arity" is a term of the form [[x1:T1]...[xn:Tn]s] with [s] a sort.
348 Such a term can canonically be seen as the pair of a context of types
349 and of a sort *)
350
351 type arity = Constr.rel_context * Sorts.t
352
353 let destArity =
354 let open Context.Rel.Declaration in
355 let rec prodec_rec l c =
356 match kind c with
357 | Prod (x,t,c) -> prodec_rec (LocalAssum (x,t) :: l) c
358 | LetIn (x,b,t,c) -> prodec_rec (LocalDef (x,b,t) :: l) c
359 | Cast (c,_,_) -> prodec_rec l c
360 | Sort s -> l,s
361 | _ -> anomaly ~label:"destArity" (Pp.str "not an arity.")
362 in
363 prodec_rec []
364
365 let mkArity (sign,s) = it_mkProd_or_LetIn (mkSort s) sign
366
367 let rec isArity c =
368 match kind c with
369 | Prod (_,_,c) -> isArity c
370 | LetIn (_,_,_,c) -> isArity c
371 | Cast (c,_,_) -> isArity c
372 | Sort _ -> true
373 | _ -> false