Debian Codegraph @

pretyping/libBinding.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 Declarations
12 open Names
13 open EConstr
14 open Environ
15 open Evd
16 open Context
17 open Util
18 open Namegen
19
20 module RelDecl = Rel.Declaration
21 open RelDecl
22
23 (* ************************************************************************** *)
24 (* State *)
25 (* ************************************************************************** *)
26
27 let _dbg = CDebug.create ~name:"API" ()
28
29 (* state *)
30 module State =
31 struct
32
33 (* Monad *)
34 type state =
35 { env : env;
36 names : Nameops.Fresh.t;
37 subst : Esubst.lift;
38 }
39
40 type 'a t = state -> evar_map -> evar_map * 'a
41
42 let return c = fun _ sigma -> (sigma, c)
43 let bind x f = fun s sigma -> let (sigma, a) = x s sigma in f a s sigma
44 let map f t = bind t (fun a -> return @@ f a)
45 let run_state s sigma t = t s sigma
46 let run env sigma t =
47 let s = {
48 env = env;
49 names = Nameops.Fresh.of_list @@ Termops.ids_of_rel_context @@ rel_context env;
50 subst = Esubst.el_id;
51 }
52 in t s sigma
53
54 (* Notation for the monad *)
55 let (let@) x f = x f
56 let (let*) x f = bind x f
57
58 (* Access the current state *)
59 let get_env = fun s -> return s.env s
60 let get_sigma = fun s sigma -> return sigma s sigma
61 let get_names = fun s -> return s.names s
62 let get_state = fun s -> return s s
63 let get_context = fun s -> return (EConstr.of_rel_context @@ Environ.rel_context s.env) s
64
65
66 (** {6 Weaken Functions From the Former Context to the New Context } *)
67
68 let weaken c = fun s -> return (Vars.exliftn s.subst c) s
69
70 let weaken_rel decl =
71 fun s sigma -> return (RelDecl.map_constr (fun t -> snd @@ weaken t s sigma) decl) s sigma
72
73 let weaken_context cxt s sigma =
74 let nb_cxt = List.length cxt in
75 let wcxt = List.mapi (fun i x ->
76 let n = nb_cxt - i -1 in
77 let weak x = Vars.exliftn (Esubst.el_liftn n s.subst) x in
78 match x with
79 | LocalAssum (na, ty) -> LocalAssum (na, weak ty)
80 | LocalDef (na, bd, ty) -> LocalDef (na, weak bd, weak ty)
81 ) cxt
82 in
83 return wcxt s sigma
84
85 (** {6 Access Key } *)
86
87 type access_key = int
88
89 let fresh_key s =
90 let (_, ctx) = get_context s Evar_empty in
91 List.length ctx
92
93 let make_key i =
94 let* ctx = get_context in
95 return @@ List.length ctx - i
96
97 (** {6 Push Functions } *)
98
99 (* Add variables to the context *)
100 let add_names names decl =
101 match get_name decl with
102 | Anonymous -> names
103 | Name id -> Nameops.Fresh.add id names
104
105 let push_old_rel decl s sigma =
106 let s' = {
107 env = EConstr.push_rel decl s.env ;
108 names = add_names s.names decl;
109 subst = Esubst.el_lift s.subst;
110 } in
111 return (s', fresh_key s) s' sigma
112
113 let push_fresh_rel decl s sigma =
114 let s' = {
115 env = EConstr.push_rel decl s.env ;
116 names = add_names s.names decl;
117 subst = Esubst.el_shft 1 (Esubst.el_lift s.subst);
118 } in
119 return (s', fresh_key s) s' sigma
120
121
122 (** {6 Access Functions } *)
123
124 let get_decl key =
125 let* ctx = get_context in
126 let n' = List.length ctx - key -1 in
127 let decl = RelDecl.map_constr (Vars.lift n') (List.nth ctx n') in
128 return decl
129
130 let getters f =
131 let get_X key =
132 let* decl = get_decl key in
133 let* cxt = get_context in
134 return (f (List.length cxt - key -1) decl) in
135
136 let geti_X keys pos_key = get_X (List.nth keys pos_key) in
137
138 let getij_X keyss pos_k1 pos_k2 = get_X (List.nth (List.nth keyss pos_k1) pos_k2) in
139
140 let get_Xs keys =
141 fun s sigma ->
142 return (Array.of_list @@ List.map (fun key -> snd @@ get_X key s sigma) keys) s sigma in
143
144 (get_X, geti_X, getij_X, get_Xs)
145
146 let get_sdecl_term =
147 fun n d ->
148 match RelDecl.get_value d with
149 | Some tm -> Vars.lift 1 tm
150 | None -> mkRel (1+n)
151
152 let get_term, geti_term, getij_term, get_terms = getters get_sdecl_term
153
154 let get_type, geti_type, getij_type, get_types = getters (fun _ d -> Vars.lift 1 (RelDecl.get_type d))
155
156 let get_aname, geti_aname, getij_aname, get_anames = getters (fun _ cdecl -> RelDecl.get_annot cdecl)
157
158 let index0_opt_i p l =
159 let rec aux i l = match l with
160 | [] -> None
161 | h::_ when p i h -> Some (i, h)
162 | _::t -> aux (1+i) t
163 in aux 0 l
164
165 let check_key_in k keys =
166 let* k = make_key k in
167 return @@ Option.map fst @@ index0_opt_i (fun _ key -> k = key) keys
168
169 (** {6 Access Functions } *)
170
171 let list_mapi f l =
172 let rec aux i l acc s sigma =
173 match l with
174 | [] -> (sigma, acc)
175 | a::l -> let (sigma, t) = (f i a) s sigma in
176 aux (i + 1) l (t::acc) s sigma
177 in
178 let* acc = aux 0 l [] in
179 return @@ List.rev acc
180
181 let list_map2i f la lb =
182 let rec aux i la lb acc s sigma =
183 match la, lb with
184 | [], [] -> (sigma, acc)
185 | a::la, b::lb -> let (sigma, t) = (f i a b) s sigma in
186 aux (i + 1) la lb (t::acc) s sigma
187 | _,_ -> assert false
188 in
189 let* acc = aux 0 la lb [] in
190 return @@ List.rev acc
191
192 let array_mapi f ar =
193 fun s sigma ->
194 let sigma_ref = ref sigma in
195 let size_ar = Array.length ar in
196 if size_ar = 0 then
197 (!sigma_ref, [||])
198 else begin
199 let update_pos i =
200 let (sigma, x) = f i ar.(i) s !sigma_ref in
201 sigma_ref := sigma;
202 x
203 in
204 let r = Array.init size_ar update_pos in
205 (!sigma_ref, r)
206 end
207
208 let array_map2i f ar1 ar2 =
209 fun s sigma ->
210 let sigma_ref = ref sigma in
211 let size_ar = Array.length ar1 in
212 if size_ar = 0
213 then (!sigma_ref, [||])
214 else begin
215 let update_pos i =
216 let (sigma, x) = f i ar1.(i) ar2.(i) s !sigma_ref in
217 sigma_ref := sigma;
218 x
219 in
220 let r = Array.init size_ar update_pos in
221 (!sigma_ref, r)
222 end
223
224 end
225
226 open State
227
228 (* ************************************************************************** *)
229 (* Naming Schemes *)
230 (* ************************************************************************** *)
231
232 let name_hd decl =
233 let* env = get_env in
234 let* sigma = get_sigma in
235 return @@ named_hd env sigma (RelDecl.get_type decl) (RelDecl.get_name decl)
236
237 type naming_scheme = rel_declaration -> rel_declaration t
238
239 (* Keep naming as is, including Anonymous *)
240 let naming_id decl = return decl
241
242 (* Chooses the next Id available from the binder's name.
243 If the binder is Anonymous, a name is generated using the head the binder's type. *)
244 let naming_hd decl =
245 let* name_or_hd = name_hd decl in
246 return @@ set_name name_or_hd decl
247
248 let naming_hd_dep dep = if dep then naming_hd else naming_id
249
250 let naming_hd_fresh decl =
251 let* env = get_env in
252 let* sigma = get_sigma in
253 let* names = get_names in
254 let id = id_of_name_using_hdchar env sigma (RelDecl.get_type decl) (RelDecl.get_name decl) in
255 let id = Namegen.mangle_id id in
256 let id, avoid = Nameops.Fresh.fresh id names in
257 return @@ set_name (Name id) decl
258
259 let naming_hd_fresh_dep dep = if dep then naming_hd_fresh else naming_id
260
261 (* ************************************************************************** *)
262 (* Fold Functions *)
263 (* ************************************************************************** *)
264
265 (* fold functions for state *)
266 let fold_right_state f l tp t =
267 let rec aux ids1 i l =
268 match l with
269 | [] -> t (List.rev ids1)
270 | a :: l -> tp i a (fun id1 -> aux (f id1 ids1) (i+1) l)
271 in
272 aux [] 0 l
273
274 let fold_left_state f l tp t = fold_right_state f (List.rev l) tp t
275
276 let fold_right_state_3 f l tp t =
277 let rec aux ids1 ids2 ids3 i l =
278 match l with
279 | [] -> t (List.rev ids1 , List.rev ids2 , List.rev ids3)
280 | a :: l -> tp i a (fun (id1 , id2 , id3) -> aux (f id1 ids1) (f id2 ids2) (f id3 ids3) (i+1) l)
281 in
282 aux [] [] [] 0 l
283
284 let fold_left_state_3 f l tp cc =
285 fold_right_state_3 f (List.rev l) tp cc
286
287
288 (* ************************************************************************** *)
289 (* Operations *)
290 (* ************************************************************************** *)
291
292 let ind_relevance ind u =
293 let* sigma = get_sigma in
294 return @@ ERelevance.make @@ Inductive.relevance_of_ind_body ind (EInstance.kind sigma u)
295
296 (* Decompose and Manipulate Terms *)
297 let whd_decompose_prod_decls t =
298 let* env = get_env in
299 let* sigma = get_sigma in
300 return @@ Reductionops.whd_decompose_prod_decls env sigma t
301
302 let decompose_lambda_decls t =
303 let* sigma = get_sigma in
304 return @@ decompose_lambda_decls sigma t
305
306 let decompose_app t =
307 let* sigma = get_sigma in
308 return @@ decompose_app sigma t
309
310 let eta_expand_instantiation inst ctxt =
311 let* env = get_env in
312 let* sigma = get_sigma in
313 return @@ Reductionops.eta_expand_instantiation env sigma inst ctxt
314
315 let fresh_global ref =
316 fun s sigma ->
317 let (sigma, t) = fresh_global s.env sigma ref in
318 return t s sigma
319
320 let fresh_inductive_instance ind =
321 fun s sigma ->
322 let sigma, ((_, u)) = Evd.fresh_inductive_instance s.env sigma ind in
323 return u s sigma
324
325 let fresh_sort_ql ?sort_rigid ?name r =
326 fun s sigma ->
327 let sigma, q, u = Evd.new_sort_info ?sort_rigid ?name r sigma in
328 return (q,u) s sigma
329
330 let new_univ_level_variable ?name r =
331 fun s sigma ->
332 let (sigma, l) = Evd.new_univ_level_variable ?name r sigma in
333 return l s sigma
334
335 (* Typing and Retyping *)
336 let typing_checked_appvect f xs s sigma =
337 let (sigma, t) = Typing.checked_appvect s.env sigma f xs in
338 return t s sigma
339
340 let typing_check_actual_type jud ty : unit t =
341 fun s sigma ->
342 let sigma = Typing.check_actual_type s.env sigma jud ty in
343 return () s sigma
344
345 let retyping_sort_of t =
346 let* env = get_env in
347 let* sigma = get_sigma in
348 return @@ Retyping.get_sort_of env sigma t
349
350 let retyping_judgment_of tm =
351 let* env = get_env in
352 let* sigma = get_sigma in
353 return @@ Retyping.get_judgment_of env sigma tm
354
355 (* debug *)
356 let print_term dbg s t =
357 let* env = get_env in
358 let* sigma = get_sigma in
359 return @@ dbg Pp.(fun () -> str s ++ Termops.Internal.print_constr_env env sigma t)
360
361 let print_current_context dbg s =
362 let* env = get_env in
363 let* sigma = get_sigma in
364 return @@ dbg Pp.(fun () -> str s ++ Termops.Internal.print_rel_context env sigma)
365
366
367 (* ************************************************************************** *)
368 (* Make Binders *)
369 (* ************************************************************************** *)
370
371 (* Notations to specify functions *)
372 type freshness = Fresh | Old
373 type binder = Lambda | Prod
374
375 let wrap_binder b =
376 match b with
377 | Lambda -> mkLambda_or_LetIn
378 | Prod -> mkProd_or_LetIn
379
380 let wrap_decl map f fresh naming_scheme decl cc s sigma =
381 match fresh with
382 | Fresh ->
383 let (sigma, decl) = naming_scheme decl s sigma in
384 let (sigma, (s', k)) = push_fresh_rel decl s sigma in
385 let (sigma, v) = cc k s' sigma in
386 return (map (f decl) v) s sigma
387 | Old ->
388 let (sigma, decl) = weaken_rel decl s sigma in
389 let (sigma, decl) = naming_scheme decl s sigma in
390 let (sigma, (s', k)) = push_old_rel decl s sigma in
391 let (sigma, v) = cc k s' sigma in
392 return (map (f decl) v) s sigma
393
394 let fid f x = f x
395 let fright f (a,b) = (a, f b)
396 let fleft f (a,b) = (f a, b)
397 let fopt = Option.map
398 let fropt = fun f -> Option.map (fun (a,b) -> (a, f b))
399
400 let add_decl a = wrap_decl fid (fun _ x -> x) a
401
402 let build_binder m binder = wrap_decl m (wrap_binder binder)
403 let make_binder m binder naming_scheme na ty = build_binder m binder Fresh naming_scheme (LocalAssum (na, ty))
404 let keep_binder m binder naming_scheme na ty = build_binder m binder Old naming_scheme (LocalAssum (na, ty))
405
406 (* 2. Iterate binders *)
407
408 (* seperate var and letin in key_vars / key_letins / key_both *)
409 let read_context_sep binder cxt =
410 fold_left_state_3 List.append cxt (fun _ decl cc ->
411 let@ key = binder decl in
412 match decl with
413 | LocalAssum _ -> cc ([key],[],[key])
414 | LocalDef _ -> cc ([],[key],[key])
415 )
416
417 let read_context_sep_forget binder cxt cc =
418 read_context_sep binder cxt (fun (x,_,_) -> cc x)
419
420 let add_context fresh naming_scheme = read_context_sep_forget (add_decl fresh naming_scheme)
421 let add_context_sep fresh naming_scheme = read_context_sep (add_decl fresh naming_scheme)
422
423 let closure_context m binder fresh naming_scheme = read_context_sep_forget (build_binder m binder fresh naming_scheme)
424 let closure_context_sep m binder fresh naming_scheme = read_context_sep (build_binder m binder fresh naming_scheme)
425
426 let rebind m binder freshness naming_scheme ty cc =
427 (* decompose type, and rebind local variable *)
428 let* (locs, hd) =
429 match binder with
430 | Prod -> whd_decompose_prod_decls ty
431 | Lambda -> decompose_lambda_decls ty
432 in
433 let@ key_locs = closure_context m binder freshness naming_scheme locs in
434 (* rebind the head *)
435 let* sort = retyping_sort_of hd in
436 let rev_hd = ESorts.relevance_of_sort sort in
437 let name_hd = make_annot Anonymous rev_hd in
438 let@ key_hd = build_binder m binder freshness naming_scheme @@ LocalAssum (name_hd, hd) in
439 (* continuation *)
440 cc (key_locs, key_hd)
441
442 (* takes a continuation after binder var and letin to add fresh binders and decide what to do with the keys *)
443 let read_by_decl cxt binder cc_letin cc_var =
444 fold_left_state List.append cxt (fun pos_decl decl cc ->
445 let@ key = binder decl in
446 match decl with
447 | LocalDef _ -> cc_letin pos_decl key cc
448 | LocalAssum _ -> cc_var pos_decl key cc
449 )
450
451 (* ************************************************************************** *)
452 (* Functions on Inductive *)
453 (* ************************************************************************** *)
454
455 let get_args mib u (cxt, ty) =
456 let nb_params_letin = List.length mib.mind_params_ctxt in
457 let (_, args) = List.chop nb_params_letin (List.rev cxt) in
458 let args = Vars.subst_instance_context u @@ EConstr.of_rel_context @@ List.rev args in
459 let* (hd, xs) = decompose_app (Vars.subst_instance_constr u @@ EConstr.of_constr ty) in
460 let indices = Array.sub xs mib.mind_nparams (Array.length xs - mib.mind_nparams) in
461 return (args, indices)
462
463 let iterate_ctors mib ind u tp cc =
464 let* ctors = array_mapi (fun _ -> get_args mib u) ind.mind_nf_lc in
465 fold_right_state (fun a l -> a :: l) (Array.to_list ctors) tp cc
466
467 let make_ind ((kn, pos_ind), u) key_uparams key_nuparams key_indices =
468 let tInd = mkIndU ((kn, pos_ind), u) in
469 let* inst_ind = get_terms (key_uparams @ key_nuparams @ key_indices) in
470 return @@ mkApp (tInd, inst_ind)
471
472 let make_cst ((kn, pos_ind), u) pos_ctor key_uparams key_nuparams args =
473 let tCst = mkConstructU (((kn, pos_ind), 1+pos_ctor), u) in
474 let* params = get_terms (key_uparams @ key_nuparams) in
475 return @@ mkApp (tCst, (Array.concat [params; args]))
476
477 (* make fix *)
478 let make_fix ind_bodies focus fix_rarg fix_name fix_type tmc =
479 (* data fix *)
480 let rargs = List.mapi fix_rarg ind_bodies in
481 let fix_names = List.mapi fix_name ind_bodies in
482 let* fix_types = list_mapi fix_type ind_bodies in
483 (* update context continuation *)
484 let fix_context = List.rev @@ List.map2_i (fun i na ty -> LocalAssum (na, Vars.lift i ty)) 0 fix_names fix_types in
485 let@ key_Fix = add_context Fresh naming_id fix_context in
486 let* fix_bodies = list_mapi (fun pos_list ind -> tmc (key_Fix, pos_list, ind)) ind_bodies in
487 (* result *)
488 return @@ EConstr.mkFix ((Array.of_list rargs, focus), (Array.of_list fix_names, Array.of_list fix_types, Array.of_list fix_bodies))
489
490 let get_indices indb u =
491 let indices, _ = List.chop indb.mind_nrealdecls indb.mind_arity_ctxt in
492 weaken_context (Vars.subst_instance_context u (EConstr.of_rel_context indices))
493
494 (* make match *)
495 let make_case_or_projections naming_vars mib ind indb u key_uparams key_nuparams params indices mk_case_pred case_relevance tm_match tc =
496 let* env = get_env in
497 let* sigma = get_sigma in
498 let case_info = Inductiveops.make_case_info env ind RegularStyle in
499
500 let case_invert =
501 if Inductiveops.Internal.should_invert_case env sigma (ERelevance.kind sigma case_relevance) case_info
502 then Constr.CaseInvert {indices=indices}
503 else Constr.NoInvert
504 in
505
506 let case_pred =
507 (* indices *)
508 let* indices = get_indices indb u in
509 let@ key_fresh_indices, _ , key_both = add_context_sep Fresh naming_vars indices in
510 (* new var *)
511 let* inst_ind = get_terms (key_uparams @ key_nuparams @ key_fresh_indices) in
512 let ty_var = mkApp (mkIndU (ind, u), inst_ind) in
513 let* env = get_env in
514 let name_var_match = make_annot Anonymous (Inductiveops.relevance_of_inductive env (ind, u)) in
515 let@ key_var_match = add_decl Fresh naming_vars (LocalAssum (name_var_match, ty_var)) in
516 (* return type *)
517 let* fresh_annot = get_anames (key_both @ [key_var_match]) in
518 let* return_type = mk_case_pred key_fresh_indices key_var_match in
519 return @@ ((fresh_annot, return_type), case_relevance)
520 in
521
522 let branch pos_ctor ctor =
523 let* args = get_args mib u ctor in
524 let args = fst args in
525 let@ key_args, key_letin, key_both = add_context_sep Old naming_vars args in
526 let* branches_body = tc (key_args, key_letin, key_both, pos_ctor) in
527 let* names_args = get_anames key_both in
528 return (names_args, branches_body)
529 in
530
531 let* case_info, pred, case_invert, c, branches =
532 let* case_pred = case_pred in
533 let* branches = array_mapi branch indb.mind_nf_lc in
534 let* env = get_env in
535 let* sigma = get_sigma in
536 return @@ EConstr.expand_case env sigma (case_info, u, params, case_pred, case_invert, tm_match, branches)
537 in
538
539 let* env = get_env in
540 let* sigma = get_sigma in
541 return @@ Inductiveops.simple_make_case_or_project env sigma case_info pred case_invert c branches