Debian Codegraph @

tactics/eClause.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 Names
14 open Nameops
15 open Termops
16 open Constr
17 open Context
18 open Evarutil
19 open Tactypes
20
21 (** {6 Evar-based clauses} *)
22
23 type hole = {
24 hole_evar : EConstr.constr;
25 hole_type : EConstr.types;
26 hole_deps : bool;
27 hole_name : Name.t;
28 hole_evar_key : Evar.t;
29 }
30
31 type clause = {
32 cl_holes : hole list;
33 cl_concl : EConstr.types;
34 }
35
36 let qhyp_eq h1 h2 = match h1, h2 with
37 | NamedHyp n1, NamedHyp n2 -> lident_eq n1 n2
38 | AnonHyp i1, AnonHyp i2 -> Int.equal i1 i2
39 | _ -> false
40
41 let check_bindings bl =
42 match List.duplicates qhyp_eq (List.map (fun {CAst.v=x} -> fst x) bl) with
43 | NamedHyp s :: _ ->
44 user_err ?loc:s.CAst.loc
45 Pp.(str "The variable " ++ Id.print s.CAst.v ++
46 str " occurs more than once in binding list.");
47 | AnonHyp n :: _ ->
48 user_err
49 Pp.(str "The position " ++ int n ++
50 str " occurs more than once in binding list.")
51 | [] -> ()
52
53 let explain_no_such_bound_variable mvl {CAst.v=id;loc} =
54 let open Pp in
55 let expl = match mvl with
56 | [] -> str "(no bound variables at all in the expression)."
57 | [id] -> str "(possible name is: " ++ Id.print id ++ str ")."
58 | _ -> str "(possible names are: " ++ pr_enum Id.print mvl ++ str ")."
59 in
60 user_err ?loc (str "No such bound variable " ++ Id.print id ++ spc () ++ expl)
61
62 let error_not_right_number_missing_arguments n =
63 user_err
64 Pp.(strbrk "Not the right number of missing arguments (expected " ++
65 int n ++ str ").")
66
67 let make_evar_clause env sigma ?len t =
68 let open EConstr in
69 let open Vars in
70 let bound = match len with
71 | None -> -1
72 | Some n -> assert (0 <= n); n
73 in
74 let rec clrec (sigma, holes) inst n t =
75 if n = 0 then (sigma, holes, t)
76 else match EConstr.kind sigma t with
77 | Cast (t, _, _) -> clrec (sigma, holes) inst n t
78 | Prod (na, t1, t2) ->
79 (* Share the evar instances as we are living in the same context *)
80 let inst, ctx, args, subst = match inst with
81 | None ->
82 (* Dummy type *)
83 let hypnaming = VarSet.variables (Global.env ()) in
84 let ctx, _, args, subst = push_rel_context_to_named_context ~hypnaming env sigma mkProp in
85 Some (ctx, args, subst), ctx, args, subst
86 | Some (ctx, args, subst) -> inst, ctx, args, subst
87 in
88 let (sigma, evk) = new_pure_evar ~typeclass_candidate:false ctx sigma
89 ~relevance:na.binder_relevance
90 (csubst_subst sigma subst t1)
91 in
92 let ev = mkEvar (evk, args) in
93 let dep = not (noccurn sigma 1 t2) in
94 let hole = {
95 hole_evar = ev;
96 hole_type = t1;
97 hole_deps = dep;
98 (* We fix it later *)
99 hole_name = na.binder_name;
100 hole_evar_key = evk;
101 } in
102 let t2 = subst1 ev t2 in
103 clrec (sigma, hole :: holes) inst (pred n) t2
104 | LetIn (na, b, _, t) -> clrec (sigma, holes) inst n (subst1 b t)
105 | _ -> (sigma, holes, t)
106 in
107 let (sigma, holes, t) = clrec (sigma, []) None bound t in
108 let holes = List.rev holes in
109 let clause = { cl_concl = t; cl_holes = holes } in
110 (sigma, clause)
111
112 let evar_with_name holes ({CAst.v=id} as lid) =
113 let map h = match h.hole_name with
114 | Anonymous -> None
115 | Name id' -> if Id.equal id id' then Some h else None
116 in
117 let hole = List.map_filter map holes in
118 match hole with
119 | [] ->
120 let fold h accu = match h.hole_name with
121 | Anonymous -> accu
122 | Name id -> id :: accu
123 in
124 let mvl = List.fold_right fold holes [] in
125 explain_no_such_bound_variable mvl lid
126 | h::_ -> h.hole_evar
127
128 let evar_of_binder holes = function
129 | NamedHyp s -> evar_with_name holes s
130 | AnonHyp n ->
131 try
132 let nondeps = List.filter (fun hole -> not hole.hole_deps) holes in
133 let h = List.nth nondeps (pred n) in
134 h.hole_evar
135 with e when CErrors.noncritical e ->
136 user_err Pp.(str "No such binder.")
137
138 let define_with_type sigma env ev c =
139 let open EConstr in
140 let t = Retyping.get_type_of env sigma ev in
141 let ty = Retyping.get_type_of env sigma c in
142 let j = Environ.make_judge c ty in
143 let (sigma, j, _trace) = Coercion.inh_conv_coerce_to ~program_mode:false ~resolve_tc:true env sigma j t in
144 let (ev, _) = destEvar sigma ev in
145 let sigma = Evd.define ev j.Environ.uj_val sigma in
146 sigma
147
148 let solve_evar_clause env sigma hyp_only clause = function
149 | NoBindings -> sigma
150 | ImplicitBindings largs ->
151 let open EConstr in
152 let fold holes h =
153 if h.hole_deps then
154 (* Some subsequent term uses the hole *)
155 let (ev, _) = destEvar sigma h.hole_evar in
156 let is_dep hole = occur_evar sigma ev hole.hole_type in
157 let in_hyp = List.exists is_dep holes in
158 let in_ccl = occur_evar sigma ev clause.cl_concl in
159 let dep = if hyp_only then in_hyp && not in_ccl else in_hyp || in_ccl in
160 let h = { h with hole_deps = dep } in
161 h :: holes
162 else
163 (* The hole does not occur anywhere *)
164 h :: holes
165 in
166 let holes = List.fold_left fold [] (List.rev clause.cl_holes) in
167 let map h = if h.hole_deps then Some h.hole_evar else None in
168 let evs = List.map_filter map holes in
169 let len = List.length evs in
170 if Int.equal len (List.length largs) then
171 let fold sigma ev arg = define_with_type sigma env ev arg in
172 let sigma = List.fold_left2 fold sigma evs largs in
173 sigma
174 else
175 error_not_right_number_missing_arguments len
176 | ExplicitBindings lbind ->
177 let () = check_bindings lbind in
178 let fold sigma {CAst.v=(binder, c)} =
179 let ev = evar_of_binder clause.cl_holes binder in
180 define_with_type sigma env ev c
181 in
182 let sigma = List.fold_left fold sigma lbind in
183 sigma
184
185 let check_evar_clause env sigma sigma' eq_clause =
186 let f h = if h.hole_deps then Some h.hole_evar_key else None in
187 match Tacticals.check_evar_list env sigma' (Evar.Set.of_list (List.map_filter f eq_clause.cl_holes)) sigma with
188 | [] -> ()
189 | evkl ->
190 let filter h = if List.exists (Evar.equal h.hole_evar_key) evkl then Some h.hole_name else None in
191 let bindings = List.map_filter filter eq_clause.cl_holes in
192 Loc.raise (Logic.RefinerError (env, sigma, UnresolvedBindings bindings))