Debian Codegraph @

pretyping/heads.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 Names
13 open EConstr
14 open Vars
15 open Context.Named.Declaration
16
17 (** Characterization of the head of a term *)
18
19 (* We only compute an approximation to ensure the computation is not
20 arbitrary long (e.g. the head constant of [h] defined to be
21 [g (fun x -> phi(x))] where [g] is [fun f => g O] does not launch
22 the evaluation of [phi(0)] and the head of [h] is declared unknown). *)
23
24 type rigid_head_kind =
25 | RigidParameter of Constant.t (* a Const without body. Module substitution may instantiate it with something else. *)
26 | RigidOther (* a Var without body, inductive, product, sort, projection *)
27
28 type head_approximation =
29 | RigidHead of rigid_head_kind
30 | ConstructorHead
31 | FlexibleHead of int * int * int * bool (* [true] if a surrounding case *)
32 | NotImmediatelyComputableHead
33
34 let rec compute_head_const env sigma cst =
35 let body = Environ.constant_opt_value_in env (cst,UVars.Instance.empty) in
36 match body with
37 | None -> RigidHead (RigidParameter cst)
38 | Some c -> kind_of_head env sigma (EConstr.of_constr c)
39
40 and compute_head_var env sigma id = match lookup_named id env with
41 | LocalDef (_,c,_) -> kind_of_head env sigma c
42 | _ -> RigidHead RigidOther
43
44 and kind_of_head env sigma t =
45 let rec aux k l t b = match EConstr.kind sigma (Reductionops.clos_whd_flags RedFlags.betaiotazeta env sigma t) with
46 | Rel n when n > k -> NotImmediatelyComputableHead
47 | Rel n -> FlexibleHead (k,k+1-n,List.length l,b)
48 | Var id ->
49 (try on_subterm k l b (compute_head_var env sigma id)
50 with Not_found ->
51 (* a goal variable *)
52 match lookup_named id env with
53 | LocalDef (_,c,_) -> aux k l c b
54 | LocalAssum _ -> NotImmediatelyComputableHead)
55 | Const (cst,_) ->
56 (try on_subterm k l b (compute_head_const env sigma cst)
57 with Not_found ->
58 CErrors.anomaly
59 Pp.(str "constant not found in kind_of_head: " ++
60 Names.Constant.print cst ++
61 str "."))
62 | Construct _ | CoFix _ ->
63 if b then NotImmediatelyComputableHead else ConstructorHead
64 | Sort _ | Ind _ | Prod _ -> RigidHead RigidOther
65 | Cast (c,_,_) -> aux k l c b
66 | Lambda (_,_,c) ->
67 begin match l with
68 | [] ->
69 let () = assert (not b) in
70 aux (k + 1) [] c b
71 | h :: l -> aux k l (subst1 h c) b
72 end
73 | LetIn _ -> assert false
74 | Meta _ | Evar _ -> NotImmediatelyComputableHead
75 | App (c,al) -> aux k (Array.to_list al @ l) c b
76 | Proj (p,_,c) -> RigidHead RigidOther
77
78 | Case (_,_,_,_,_,c,_) -> aux k [] c true
79 | Int _ | Float _ | String _ | Array _ -> ConstructorHead
80 | Fix ((i,j),_) ->
81 let n = i.(j) in
82 try aux k [] (List.nth l n) true
83 with Failure _ -> FlexibleHead (k + n + 1, k + n + 1, 0, true)
84 and on_subterm k l with_case = function
85 | FlexibleHead (n,i,q,with_subcase) ->
86 let m = List.length l in
87 let k',rest,a =
88 if n > m then
89 (* eta-expansion *)
90 let a =
91 if i <= m then
92 (* we pick the head in the existing arguments *)
93 lift (n-m) (List.nth l (i-1))
94 else
95 (* we pick the head in the added arguments *)
96 mkRel (n-i+1) in
97 k+n-m,[],a
98 else
99 (* enough arguments to [cst] *)
100 k,List.skipn n l,List.nth l (i-1) in
101 let l' = List.make q (mkMeta 0) @ rest in
102 aux k' l' a (with_subcase || with_case)
103 | ConstructorHead when with_case -> NotImmediatelyComputableHead
104 | x -> x
105 in aux 0 [] t false
106
107 let is_rigid env sigma t =
108 match kind_of_head env sigma t with
109 | RigidHead _ | ConstructorHead -> true
110 | _ -> false