| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Pp |
| 12 |
open CErrors |
| 13 |
open Util |
| 14 |
open Names |
| 15 |
open Nameops |
| 16 |
open Termops |
| 17 |
open Environ |
| 18 |
open Reductionops |
| 19 |
|
| 20 |
module NamedDecl = Context.Named.Declaration |
| 21 |
|
| 22 |
type refiner_error = |
| 23 |
|
| 24 |
|
| 25 |
| UnresolvedBindings of Name.t list |
| 26 |
| CannotApply of EConstr.t * EConstr.t |
| 27 |
| NonLinearProof of EConstr.t |
| 28 |
|
| 29 |
|
| 30 |
| IntroNeedsProduct |
| 31 |
| NoSuchHyp of Id.t |
| 32 |
|
| 33 |
exception RefinerError of Environ.env * Evd.evar_map * refiner_error |
| 34 |
|
| 35 |
|
| 36 |
let error_no_such_hypothesis env sigma id = raise (RefinerError (env, sigma, NoSuchHyp id)) |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
let mt_q = (Id.Map.empty,[]) |
| 53 |
let push_val y = function |
| 54 |
(_,[] as q) -> q |
| 55 |
| (m, (x,l)::q) -> (m, (x,Id.Set.add y l)::q) |
| 56 |
let push_item x v (m,l) = |
| 57 |
(Id.Map.add x v m, (x,Id.Set.empty)::l) |
| 58 |
let mem_q x (m,_) = Id.Map.mem x m |
| 59 |
let find_q x (m,q) = |
| 60 |
let v = Id.Map.find x m in |
| 61 |
let m' = Id.Map.remove x m in |
| 62 |
let rec find accs acc = function |
| 63 |
[] -> raise Not_found |
| 64 |
| [(x',l)] -> |
| 65 |
if Id.equal x x' then ((v,Id.Set.union accs l),(m',List.rev acc)) |
| 66 |
else raise Not_found |
| 67 |
| (x',l as i)::((x'',l'')::q as itl) -> |
| 68 |
if Id.equal x x' then |
| 69 |
((v,Id.Set.union accs l), |
| 70 |
(m',List.rev acc@(x'',Id.Set.add x (Id.Set.union l l''))::q)) |
| 71 |
else find (Id.Set.union l accs) (i::acc) itl in |
| 72 |
find Id.Set.empty [] q |
| 73 |
|
| 74 |
let occur_vars_in_decl env sigma hyps d = |
| 75 |
if Id.Set.is_empty hyps then false else |
| 76 |
let ohyps = global_vars_set_of_decl env sigma d in |
| 77 |
Id.Set.exists (fun h -> Id.Set.mem h ohyps) hyps |
| 78 |
|
| 79 |
let reorder_context env sigma sign ord = |
| 80 |
let ords = List.fold_right Id.Set.add ord Id.Set.empty in |
| 81 |
if not (Int.equal (List.length ord) (Id.Set.cardinal ords)) then |
| 82 |
user_err Pp.(str "Order list has duplicates"); |
| 83 |
let rec step ord expected ctxt_head moved_hyps ctxt_tail = |
| 84 |
match ord with |
| 85 |
| [] -> List.rev ctxt_tail @ ctxt_head |
| 86 |
| top::ord' when mem_q top moved_hyps -> |
| 87 |
let ((d,h),mh) = find_q top moved_hyps in |
| 88 |
if occur_vars_in_decl env sigma h d then |
| 89 |
user_err |
| 90 |
(str "Cannot move declaration " ++ Id.print top ++ spc() ++ |
| 91 |
str "before " ++ |
| 92 |
pr_sequence Id.print |
| 93 |
(Id.Set.elements (Id.Set.inter h |
| 94 |
(global_vars_set_of_decl env sigma d))) ++ str "."); |
| 95 |
step ord' expected ctxt_head mh (d::ctxt_tail) |
| 96 |
| _ -> |
| 97 |
(match ctxt_head with |
| 98 |
| [] -> error_no_such_hypothesis env sigma (List.hd ord) |
| 99 |
| d :: ctxt -> |
| 100 |
let x = NamedDecl.get_id d in |
| 101 |
if Id.Set.mem x expected then |
| 102 |
step ord (Id.Set.remove x expected) |
| 103 |
ctxt (push_item x d moved_hyps) ctxt_tail |
| 104 |
else |
| 105 |
step ord expected |
| 106 |
ctxt (push_val x moved_hyps) (d::ctxt_tail)) in |
| 107 |
step ord ords sign mt_q [] |
| 108 |
|
| 109 |
let reorder_val_context env sigma sign ord = |
| 110 |
match ord with |
| 111 |
| [] | [_] -> |
| 112 |
|
| 113 |
sign |
| 114 |
| _ :: _ :: _ -> |
| 115 |
let open EConstr in |
| 116 |
val_of_named_context (reorder_context env sigma (named_context_of_val sign) ord) |
| 117 |
|
| 118 |
let check_decl_position env sigma sign d = |
| 119 |
let open EConstr in |
| 120 |
let x = NamedDecl.get_id d in |
| 121 |
let needed = global_vars_set_of_decl env sigma d in |
| 122 |
let deps = dependency_closure env sigma (named_context_of_val sign) needed in |
| 123 |
if Id.List.mem x deps then |
| 124 |
user_err |
| 125 |
(str "Cannot create self-referring hypothesis " ++ Id.print x ++ str "."); |
| 126 |
x::deps |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
type 'id move_location = |
| 136 |
| MoveAfter of 'id |
| 137 |
| MoveBefore of 'id |
| 138 |
| MoveFirst |
| 139 |
| MoveLast |
| 140 |
|
| 141 |
let pr_move_location pr_id = function |
| 142 |
| MoveAfter id -> brk(1,1) ++ str "after " ++ pr_id id |
| 143 |
| MoveBefore id -> brk(1,1) ++ str "before " ++ pr_id id |
| 144 |
| MoveFirst -> str " at top" |
| 145 |
| MoveLast -> str " at bottom" |
| 146 |
|
| 147 |
let move_location_eq m1 m2 = match m1, m2 with |
| 148 |
| MoveAfter id1, MoveAfter id2 -> Id.equal id1 id2 |
| 149 |
| MoveBefore id1, MoveBefore id2 -> Id.equal id1 id2 |
| 150 |
| MoveLast, MoveLast -> true |
| 151 |
| MoveFirst, MoveFirst -> true |
| 152 |
| _ -> false |
| 153 |
|
| 154 |
let mem_id_context id ctx = Id.Map.mem id ctx.Environ.env_named_map |
| 155 |
|
| 156 |
let split_sign env sigma hfrom l = |
| 157 |
let () = if not (mem_id_context hfrom l) then error_no_such_hypothesis env sigma hfrom in |
| 158 |
let rec splitrec left sign = match EConstr.match_named_context_val sign with |
| 159 |
| None -> assert false |
| 160 |
| Some (d, right) -> |
| 161 |
let hyp = NamedDecl.get_id d in |
| 162 |
if Id.equal hyp hfrom then (left, right, d) |
| 163 |
else splitrec (d :: left) right |
| 164 |
in |
| 165 |
splitrec [] l |
| 166 |
|
| 167 |
|
| 168 |
type cannot_move_hyp = { from : Id.t; hto : Id.t move_location; hyp : Id.t } |
| 169 |
exception CannotMoveHyp of cannot_move_hyp |
| 170 |
|
| 171 |
let () = CErrors.register_handler (function |
| 172 |
| CannotMoveHyp { from; hto; hyp } -> |
| 173 |
Some Pp.(str "Cannot move " ++ Id.print from ++ |
| 174 |
pr_move_location Id.print hto ++ |
| 175 |
str ": it occurs in the declaration of " ++ |
| 176 |
Id.print hyp ++ str ".") |
| 177 |
| _ -> None) |
| 178 |
|
| 179 |
let move_hyp env sigma toleft (left,declfrom,right) hto = |
| 180 |
let open EConstr in |
| 181 |
let push prefix sign = List.fold_right push_named_context_val prefix sign in |
| 182 |
let push_rev prefix sign = List.fold_left (fun accu d -> push_named_context_val d accu) sign prefix in |
| 183 |
let rec moverec_toleft ans first middle midvars = function |
| 184 |
| [] -> push middle @@ push first ans |
| 185 |
| d :: _ as right when move_location_eq hto (MoveBefore (NamedDecl.get_id d)) -> |
| 186 |
push_rev right @@ push middle @@ push first ans |
| 187 |
| d :: right -> |
| 188 |
let hyp = NamedDecl.get_id d in |
| 189 |
let (first', middle', midvars') = |
| 190 |
if occur_vars_in_decl env sigma midvars d then |
| 191 |
if not (move_location_eq hto (MoveAfter hyp)) then |
| 192 |
(first, d :: middle, Id.Set.add hyp midvars) |
| 193 |
else raise (CannotMoveHyp {from = NamedDecl.get_id declfrom; hto; hyp}) |
| 194 |
else |
| 195 |
(d::first, middle, midvars) |
| 196 |
in |
| 197 |
if move_location_eq hto (MoveAfter hyp) then |
| 198 |
push_rev right @@ push middle' @@ push first' ans |
| 199 |
else |
| 200 |
moverec_toleft ans first' middle' midvars' right |
| 201 |
in |
| 202 |
let rec moverec_toright first middle depvars right = match EConstr.match_named_context_val right with |
| 203 |
| None -> push_rev first @@ push_rev middle right |
| 204 |
| Some (d, _) when move_location_eq hto (MoveBefore (NamedDecl.get_id d)) -> |
| 205 |
push_rev first @@ push_rev middle @@ right |
| 206 |
| Some (d, right) -> |
| 207 |
let hyp = NamedDecl.get_id d in |
| 208 |
let (first', middle', depvars') = |
| 209 |
if Id.Set.mem hyp depvars then |
| 210 |
if not (move_location_eq hto (MoveAfter hyp)) then |
| 211 |
let vars = global_vars_set_of_decl env sigma d in |
| 212 |
let depvars = Id.Set.union vars depvars in |
| 213 |
(first, d::middle, depvars) |
| 214 |
else raise (CannotMoveHyp {from = NamedDecl.get_id declfrom; hto; hyp}) |
| 215 |
else |
| 216 |
(d::first, middle, depvars) |
| 217 |
in |
| 218 |
if move_location_eq hto (MoveAfter hyp) then |
| 219 |
push_rev first' @@ push_rev middle' @@ right |
| 220 |
else |
| 221 |
moverec_toright first' middle' depvars' right |
| 222 |
in |
| 223 |
if toleft then |
| 224 |
let id = NamedDecl.get_id declfrom in |
| 225 |
moverec_toleft right [] [declfrom] (Id.Set.singleton id) left |
| 226 |
else |
| 227 |
let depvars = global_vars_set_of_decl env sigma declfrom in |
| 228 |
let right = moverec_toright [] [declfrom] depvars right in |
| 229 |
push_rev left @@ right |
| 230 |
|
| 231 |
let move_hyp_in_named_context env sigma hfrom hto sign = |
| 232 |
let (left, right, declfrom) = split_sign env sigma hfrom sign in |
| 233 |
let toleft = match hto with |
| 234 |
| MoveLast -> true |
| 235 |
| MoveAfter id | MoveBefore id -> |
| 236 |
if mem_id_context id right then false |
| 237 |
else if mem_id_context id sign then true |
| 238 |
else error_no_such_hypothesis env sigma id |
| 239 |
| MoveFirst -> false |
| 240 |
in |
| 241 |
move_hyp env sigma toleft (left,declfrom,right) hto |
| 242 |
|
| 243 |
let insert_decl_in_named_context env sigma decl hto sign = |
| 244 |
move_hyp env sigma false ([],decl,sign) hto |
| 245 |
|
| 246 |
let convert_hyp ~check ~reorder env sigma d = |
| 247 |
let id = NamedDecl.get_id d in |
| 248 |
let b = NamedDecl.get_value d in |
| 249 |
let sign = Environ.named_context_val env in |
| 250 |
match lookup_named_ctxt id sign with |
| 251 |
| exception Not_found -> |
| 252 |
if check then error_no_such_hypothesis env sigma id |
| 253 |
else sign |
| 254 |
| d' -> |
| 255 |
let c = Option.map EConstr.of_constr (NamedDecl.get_value d') in |
| 256 |
if check && not (is_conv env sigma (NamedDecl.get_type d) (EConstr.of_constr (NamedDecl.get_type d'))) then |
| 257 |
user_err |
| 258 |
(str "Incorrect change of the type of " ++ Id.print id ++ str "."); |
| 259 |
if check && not (Option.equal (is_conv env sigma) b c) then |
| 260 |
user_err |
| 261 |
(str "Incorrect change of the body of "++ Id.print id ++ str "."); |
| 262 |
let sign' = apply_to_hyp sign id (fun _ _ _ -> EConstr.Unsafe.to_named_decl d) in |
| 263 |
if reorder then reorder_val_context env sigma sign' (check_decl_position env sigma sign d) |
| 264 |
else sign' |