| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Hipattern |
| 12 |
open Names |
| 13 |
open Constr |
| 14 |
open EConstr |
| 15 |
open Vars |
| 16 |
open Util |
| 17 |
open Declarations |
| 18 |
|
| 19 |
module RelDecl = Context.Rel.Declaration |
| 20 |
|
| 21 |
type flags = { |
| 22 |
reds : RedFlags.reds; |
| 23 |
} |
| 24 |
|
| 25 |
let (=?) f g i1 i2 j1 j2= |
| 26 |
let c=f i1 i2 in |
| 27 |
if Int.equal c 0 then g j1 j2 else c |
| 28 |
|
| 29 |
let (==?) fg h i1 i2 j1 j2 k1 k2= |
| 30 |
let c=fg i1 i2 j1 j2 in |
| 31 |
if Int.equal c 0 then h k1 k2 else c |
| 32 |
|
| 33 |
type ('a,'b) sum = Left of 'a | Right of 'b |
| 34 |
|
| 35 |
type counter = bool -> metavariable |
| 36 |
|
| 37 |
module Env : |
| 38 |
sig |
| 39 |
type t |
| 40 |
type uid |
| 41 |
val empty : t |
| 42 |
val add : flags -> Environ.env -> Evd.evar_map -> EConstr.t -> t -> t * uid |
| 43 |
val find : uid -> t -> EConstr.t |
| 44 |
val repr : uid -> int |
| 45 |
end = |
| 46 |
struct |
| 47 |
|
| 48 |
module CM = Map.Make(Constr) |
| 49 |
|
| 50 |
type t = { |
| 51 |
max_uid : int; |
| 52 |
repr : int CM.t; |
| 53 |
data : Constr.t Int.Map.t; |
| 54 |
} |
| 55 |
|
| 56 |
type uid = int |
| 57 |
|
| 58 |
let empty = { |
| 59 |
max_uid = 0; |
| 60 |
repr = CM.empty; |
| 61 |
data = Int.Map.empty; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
let add flags env sigma c e = |
| 66 |
let c = Reductionops.clos_norm_flags flags.reds env sigma c in |
| 67 |
let c = EConstr.to_constr ~abort_on_undefined_evars:false sigma c in |
| 68 |
match CM.find_opt c e.repr with |
| 69 |
| Some i -> e, i |
| 70 |
| None -> |
| 71 |
let i = e.max_uid in |
| 72 |
let e = { |
| 73 |
max_uid = i + 1; |
| 74 |
repr = CM.add c i e.repr; |
| 75 |
data = Int.Map.add i c e.data; |
| 76 |
} in |
| 77 |
e, i |
| 78 |
|
| 79 |
let find id e = EConstr.of_constr (Int.Map.find id e.data) |
| 80 |
|
| 81 |
let repr id = id |
| 82 |
|
| 83 |
end |
| 84 |
|
| 85 |
type atom = { |
| 86 |
atom : Env.uid; |
| 87 |
vars : Int.Set.t; |
| 88 |
} |
| 89 |
|
| 90 |
let repr_atom state a = Env.find a.atom state |
| 91 |
let compare_atom a1 a2 = |
| 92 |
Int.compare (Env.repr a1.atom) (Env.repr a2.atom) |
| 93 |
|
| 94 |
let meta_in_atom mv a = Int.Set.mem mv a.vars |
| 95 |
|
| 96 |
let meta_succ m = m+1 |
| 97 |
|
| 98 |
let rec nb_prod_after n c= |
| 99 |
match Constr.kind c with |
| 100 |
| Prod (_,_,b) ->if n>0 then nb_prod_after (n-1) b else |
| 101 |
1+(nb_prod_after 0 b) |
| 102 |
| _ -> 0 |
| 103 |
|
| 104 |
let construct_nhyps env ind = |
| 105 |
let ind = on_snd EInstance.make ind in |
| 106 |
let nparams = (fst (Global.lookup_inductive (fst ind))).mind_nparams in |
| 107 |
let constr_types = Inductiveops.arities_of_constructors env ind in |
| 108 |
let hyp c = nb_prod_after nparams (EConstr.Unsafe.to_constr c) in |
| 109 |
Array.map hyp constr_types |
| 110 |
|
| 111 |
|
| 112 |
let ind_hyps env sigma nevar ind largs = |
| 113 |
let ind = on_snd EInstance.make ind in |
| 114 |
let types= Inductiveops.arities_of_constructors env ind in |
| 115 |
let myhyps t = |
| 116 |
let nparam_decls = Context.Rel.length (fst (Global.lookup_inductive (fst ind))).mind_params_ctxt in |
| 117 |
let t1=Termops.prod_applist_decls sigma nparam_decls t largs in |
| 118 |
let t2=snd (decompose_prod_n_decls sigma nevar t1) in |
| 119 |
fst (decompose_prod_decls sigma t2) in |
| 120 |
Array.map myhyps types |
| 121 |
|
| 122 |
let special_whd ~flags env sigma t = |
| 123 |
Reductionops.clos_whd_flags flags.reds env sigma t |
| 124 |
|
| 125 |
type kind_of_formula= |
| 126 |
Arrow of constr*constr |
| 127 |
| False of pinductive*constr list |
| 128 |
| And of pinductive*constr list*bool |
| 129 |
| Or of pinductive*constr list*bool |
| 130 |
| Exists of pinductive*constr list |
| 131 |
| Forall of constr*constr |
| 132 |
| Atom of EConstr.t |
| 133 |
|
| 134 |
let pop t = Vars.lift (-1) t |
| 135 |
|
| 136 |
let fresh_atom ~flags state env sigma metas atm = |
| 137 |
let vars, atm = |
| 138 |
if List.is_empty metas then Int.Set.empty, atm |
| 139 |
else |
| 140 |
let metas = Array.of_list metas in |
| 141 |
let nmetas = Array.length metas in |
| 142 |
let seen = ref Int.Set.empty in |
| 143 |
let rec substrec depth c = match Constr.kind c with |
| 144 |
| Constr.Rel k -> |
| 145 |
if k <= depth then c |
| 146 |
else if k - depth <= nmetas then |
| 147 |
let mv = metas.(k - depth - 1) in |
| 148 |
let () = seen := Int.Set.add mv !seen in |
| 149 |
Constr.mkMeta mv |
| 150 |
else Constr.mkRel (k - nmetas) |
| 151 |
| _ -> Constr.map_with_binders succ substrec depth c |
| 152 |
in |
| 153 |
let ans = EConstr.of_constr @@ substrec 0 (EConstr.Unsafe.to_constr atm) in |
| 154 |
!seen, ans |
| 155 |
in |
| 156 |
let st, uid = Env.add flags env sigma atm !state in |
| 157 |
let () = state := st in |
| 158 |
{ atom = uid; vars } |
| 159 |
|
| 160 |
let kind_of_formula ~flags env sigma term = |
| 161 |
let cciterm = special_whd ~flags env sigma term in |
| 162 |
match match_with_imp_term env sigma cciterm with |
| 163 |
Some (a,b)-> Arrow (a, pop b) |
| 164 |
|_-> |
| 165 |
match match_with_forall_term env sigma cciterm with |
| 166 |
Some (_,a,b)-> Forall (a, b) |
| 167 |
|_-> |
| 168 |
match match_with_nodep_ind env sigma cciterm with |
| 169 |
Some (i,l,n)-> |
| 170 |
let ind,u=EConstr.destInd sigma i in |
| 171 |
let u = EConstr.EInstance.kind sigma u in |
| 172 |
let (mib,mip) = Global.lookup_inductive ind in |
| 173 |
let nconstr=Array.length mip.mind_consnames in |
| 174 |
if Int.equal nconstr 0 then |
| 175 |
False((ind,u),l) |
| 176 |
else |
| 177 |
let has_realargs=(n>0) in |
| 178 |
let is_trivial= |
| 179 |
let is_constant n = Int.equal n 0 in |
| 180 |
Array.exists is_constant mip.mind_consnrealargs in |
| 181 |
if Inductiveops.mis_is_recursive mip || (has_realargs && not is_trivial) |
| 182 |
then |
| 183 |
Atom cciterm |
| 184 |
else |
| 185 |
if Int.equal nconstr 1 then |
| 186 |
And((ind,u),l,is_trivial) |
| 187 |
else |
| 188 |
Or((ind,u),l,is_trivial) |
| 189 |
| _ -> |
| 190 |
match match_with_sigma_type env sigma cciterm with |
| 191 |
Some (i,l)-> |
| 192 |
let (ind, u) = EConstr.destInd sigma i in |
| 193 |
let u = EConstr.EInstance.kind sigma u in |
| 194 |
Exists((ind, u), l) |
| 195 |
|_-> |
| 196 |
Atom cciterm |
| 197 |
|
| 198 |
type atoms = {positive:atom list;negative:atom list} |
| 199 |
|
| 200 |
type _ side = |
| 201 |
| Hyp : bool -> [ `Hyp ] side |
| 202 |
| Concl : [ `Goal ] side |
| 203 |
|
| 204 |
let build_atoms (type a) ~flags state env sigma metagen (side : a side) cciterm = |
| 205 |
let trivial =ref false |
| 206 |
and positive=ref [] |
| 207 |
and negative=ref [] in |
| 208 |
let rec build_rec subst polarity cciterm= |
| 209 |
match kind_of_formula ~flags env sigma cciterm with |
| 210 |
False(_,_)->if not polarity then trivial:=true |
| 211 |
| Arrow (a,b)-> |
| 212 |
build_rec subst (not polarity) a; |
| 213 |
build_rec subst polarity b |
| 214 |
| And(i,l,b) | Or(i,l,b)-> |
| 215 |
if b then |
| 216 |
begin |
| 217 |
let unsigned= fresh_atom ~flags state env sigma subst cciterm in |
| 218 |
if polarity then |
| 219 |
positive:= unsigned :: !positive |
| 220 |
else |
| 221 |
negative:= unsigned :: !negative |
| 222 |
end; |
| 223 |
let v = ind_hyps env sigma 0 i l in |
| 224 |
let g i _ decl = |
| 225 |
build_rec subst polarity (lift i (RelDecl.get_type decl)) in |
| 226 |
let f l = |
| 227 |
List.fold_left_i g (1-(List.length l)) () l in |
| 228 |
if polarity && |
| 229 |
Array.exists (function []->true|_->false) v |
| 230 |
then trivial:=true; |
| 231 |
Array.iter f v |
| 232 |
| Exists(i,l)-> |
| 233 |
let var = metagen true in |
| 234 |
let v =(ind_hyps env sigma 1 i l).(0) in |
| 235 |
let g i _ decl = |
| 236 |
build_rec (var::subst) polarity (lift i (RelDecl.get_type decl)) in |
| 237 |
List.fold_left_i g (2-(List.length l)) () v |
| 238 |
| Forall(_,b)-> |
| 239 |
let var = metagen true in |
| 240 |
build_rec (var::subst) polarity b |
| 241 |
| Atom t-> |
| 242 |
let unsigned = fresh_atom ~flags state env sigma subst t in |
| 243 |
if not (isMeta sigma (repr_atom !state unsigned)) then |
| 244 |
if polarity then |
| 245 |
positive:= unsigned :: !positive |
| 246 |
else |
| 247 |
negative:= unsigned :: !negative in |
| 248 |
begin |
| 249 |
match side with |
| 250 |
Concl -> build_rec [] true cciterm |
| 251 |
| Hyp false -> build_rec [] false cciterm |
| 252 |
| Hyp true -> |
| 253 |
let rels,head=decompose_prod sigma cciterm in |
| 254 |
let subst=List.rev_map (fun _ -> metagen true) rels in |
| 255 |
build_rec subst false head;trivial:=false |
| 256 |
end; |
| 257 |
(!trivial, |
| 258 |
{positive= !positive; |
| 259 |
negative= !negative}) |
| 260 |
|
| 261 |
type right_pattern = |
| 262 |
Rarrow |
| 263 |
| Rand |
| 264 |
| Ror |
| 265 |
| Rfalse |
| 266 |
| Rforall |
| 267 |
| Rexists of metavariable*constr*bool |
| 268 |
|
| 269 |
type left_arrow_pattern= |
| 270 |
LLatom |
| 271 |
| LLfalse of pinductive*constr list |
| 272 |
| LLand of pinductive*constr list |
| 273 |
| LLor of pinductive*constr list |
| 274 |
| LLforall of constr |
| 275 |
| LLexists of pinductive*constr list |
| 276 |
| LLarrow of constr*constr*constr |
| 277 |
|
| 278 |
type left_pattern= |
| 279 |
Lfalse |
| 280 |
| Land of pinductive |
| 281 |
| Lor of pinductive |
| 282 |
| Lforall of metavariable*constr*bool |
| 283 |
| Lexists of pinductive |
| 284 |
| LA of left_arrow_pattern |
| 285 |
|
| 286 |
type _ identifier = |
| 287 |
| GoalId : [ `Goal ] identifier |
| 288 |
| FormulaId : GlobRef.t -> [ `Hyp ] identifier |
| 289 |
|
| 290 |
let goal_id = GoalId |
| 291 |
let formula_id env gr = FormulaId (Environ.QGlobRef.canonize env gr) |
| 292 |
|
| 293 |
type _ pattern = |
| 294 |
| LeftPattern : left_pattern -> [ `Hyp ] pattern |
| 295 |
| RightPattern : right_pattern -> [ `Goal ] pattern |
| 296 |
|
| 297 |
type uid = unit ref |
| 298 |
let eq_uid (r1 : uid) r2 = r1 == r2 |
| 299 |
|
| 300 |
type 'a t = { |
| 301 |
id : 'a identifier; |
| 302 |
uid : uid; |
| 303 |
pat : 'a pattern; |
| 304 |
atoms : atoms; |
| 305 |
} |
| 306 |
|
| 307 |
type any_formula = AnyFormula : 'a t -> any_formula |
| 308 |
|
| 309 |
exception Is_atom of EConstr.t |
| 310 |
|
| 311 |
let build_formula (type a) ~flags state env sigma (side : a side) (nam : a identifier) typ metagen : Env.t * (a t, atom) sum = |
| 312 |
try |
| 313 |
let state = ref state in |
| 314 |
let m=meta_succ(metagen false) in |
| 315 |
let trivial, atoms = build_atoms ~flags state env sigma metagen side typ in |
| 316 |
let pattern : a pattern = |
| 317 |
match side with |
| 318 |
Concl -> |
| 319 |
let pat= |
| 320 |
match kind_of_formula ~flags env sigma typ with |
| 321 |
False(_,_) -> Rfalse |
| 322 |
| Atom a -> raise (Is_atom a) |
| 323 |
| And(_,_,_) -> Rand |
| 324 |
| Or(_,_,_) -> Ror |
| 325 |
| Exists (i,l) -> |
| 326 |
let d = RelDecl.get_type (List.last (ind_hyps env sigma 0 i l).(0)) in |
| 327 |
Rexists(m,d,trivial) |
| 328 |
| Forall (_,a) -> Rforall |
| 329 |
| Arrow (a,b) -> Rarrow in |
| 330 |
RightPattern pat |
| 331 |
| Hyp _ -> |
| 332 |
let pat= |
| 333 |
match kind_of_formula ~flags env sigma typ with |
| 334 |
False(i,_) -> Lfalse |
| 335 |
| Atom a -> raise (Is_atom a) |
| 336 |
| And(i,_,b) -> |
| 337 |
if b then raise (Is_atom typ) |
| 338 |
else Land i |
| 339 |
| Or(i,_,b) -> |
| 340 |
if b then raise (Is_atom typ) |
| 341 |
else Lor i |
| 342 |
| Exists (ind,_) -> Lexists ind |
| 343 |
| Forall (d,_) -> |
| 344 |
Lforall(m,d,trivial) |
| 345 |
| Arrow (a,b) -> |
| 346 |
LA (match kind_of_formula ~flags env sigma a with |
| 347 |
False(i,l)-> LLfalse(i,l) |
| 348 |
| Atom t-> LLatom |
| 349 |
| And(i,l,_)-> LLand(i,l) |
| 350 |
| Or(i,l,_)-> LLor(i,l) |
| 351 |
| Arrow(a,c)-> LLarrow(a,c,b) |
| 352 |
| Exists(i,l)->LLexists(i,l) |
| 353 |
| Forall(_,_)->LLforall a) in |
| 354 |
LeftPattern pat |
| 355 |
in |
| 356 |
let uid = ref () in |
| 357 |
!state, Left { id = nam; uid; pat = pattern; atoms = atoms} |
| 358 |
with Is_atom a -> |
| 359 |
let state = ref state in |
| 360 |
let a = fresh_atom ~flags state env sigma [] a in |
| 361 |
!state, Right a |
| 362 |
|