+ List.fold_left map true
+ in
+ let rec gen_fix len k (id, name, i, ty, bo) =
+ id, name, i, gen_term k ty, gen_term (k + len) bo
+ and gen_cofix len k (id, name, ty, bo) =
+ id, name, gen_term k ty, gen_term (k + len) bo
+ and gen_term k = function
+ | C.ASort (id, _)
+ | C.AImplicit (id, _)
+ | C.AConst (id, _, _)
+ | C.AVar (id, _, _)
+ | C.AMutInd (id, _, _, _)
+ | C.AMutConstruct (id, _, _, _, _)
+ | C.AMeta (id, _, _) -> meta id
+ | C.ARel (id, _, m, _) ->
+ if succ (k - n) <= m && m <= k then hole id else meta id
+ | C.AAppl (id, ts) ->
+ let ts = List.map (gen_term k) ts in
+ if is_meta ts then meta id else C.AAppl (id, ts)
+ | C.ACast (id, te, ty) ->
+ let te, ty = gen_term k te, gen_term k ty in
+ if is_meta [te; ty] then meta id else C.ACast (id, te, ty)
+ | C.AMutCase (id, sp, i, outty, t, pl) ->
+ let outty, t, pl = gen_term k outty, gen_term k t, List.map (gen_term k) pl in
+ if is_meta (outty :: t :: pl) then meta id else hole id (* C.AMutCase (id, sp, i, outty, t, pl) *)
+ | C.AProd (id, _, s, t) ->
+ let s, t = gen_term k s, gen_term (succ k) t in
+ if is_meta [s; t] then meta id else C.AProd (id, anon, s, t)
+ | C.ALambda (id, _, s, t) ->
+ let s, t = gen_term k s, gen_term (succ k) t in
+ if is_meta [s; t] then meta id else C.ALambda (id, anon, s, t)
+ | C.ALetIn (id, _, s, ty, t) ->
+ let s, ty, t = gen_term k s, gen_term k ty, gen_term (succ k) t in
+ if is_meta [s; t] then meta id else C.ALetIn (id, anon, s, ty, t)
+ | C.AFix (id, i, fl) -> C.AFix (id, i, List.map (gen_fix (List.length fl) k) fl)
+ | C.ACoFix (id, i, fl) -> C.ACoFix (id, i, List.map (gen_cofix (List.length fl) k) fl)
+ in
+ gen_term
+
+let convert g ity k predicate =
+ let rec aux = function
+ | C.ALambda (_, _, b, ity), C.ALambda (id, n, u, pred) ->
+ C.ALambda (id, n, aux (b, u), aux (ity, pred))
+ | C.AProd (_, _, b, ity), C.AProd (id, n, u, pred) ->
+ C.AProd (id, n, aux (b, u), aux (ity, pred))
+ | C.ALetIn (_, _, a, b, ity), C.ALetIn (id, n, v, u, pred) ->
+ C.ALetIn (id, n, aux (a, v), aux (b, u), aux (ity, pred))
+ | C.AAppl (_, bs), C.AAppl (id, us) when List.length bs = List.length us ->
+ let map b u = aux (b,u) in
+ C.AAppl (id, List.map2 map bs us)
+ | C.ACast (_, ity, b), C.ACast (id, pred, u) ->
+ C.ACast (id, aux (ity, pred), aux (b, u))
+ | ity, C.AAppl (_, C.ALambda (_, _, _, pred) :: v :: []) ->
+ aux (ity, subst 1 v pred)
+ | ity, C.AAppl (id, C.ALambda (_, _, _, pred) :: v :: vs) ->
+ aux (ity, C.AAppl (id, subst 1 v pred :: vs))
+ | _, pred -> pred
+ in
+ g k (aux (ity, predicate))
+
+let mk_pattern psno ity predicate =
+ clear_absts (convert (generalize psno) ity) psno 0 predicate
+
+let beta v = function
+ | C.ALambda (_, _, _, t) -> subst 1 v t
+ | _ -> assert false
+
+let get_clears c p xtypes =
+ let meta = C.Implicit None in
+ let rec aux c names p it et = function
+ | [] ->
+ List.rev c, List.rev names
+ | Some (C.Name name as n, C.Decl v) as hd :: tl ->
+ let hd, names, v =
+ if DTI.does_not_occur 1 p && DTI.does_not_occur 1 it && DTI.does_not_occur 1 et then
+ Some (C.Anonymous, C.Decl v), name :: names, meta
+ else
+ hd, names, v
+ in
+ let p = C.Lambda (n, v, p) in
+ let it = C.Prod (n, v, it) in
+ let et = C.Prod (n, v, et) in
+ aux (hd :: c) names p it et tl
+ | Some (C.Name name as n, C.Def (v, x)) as hd :: tl ->
+ let hd, names, v =
+ if DTI.does_not_occur 1 p && DTI.does_not_occur 1 it && DTI.does_not_occur 1 et then
+ Some (C.Anonymous, C.Def (v, x)), name :: names, meta
+ else
+ hd, names, v
+ in
+ let p = C.LetIn (n, v, x, p) in
+ let it = C.LetIn (n, v, x, it) in
+ let et = C.LetIn (n, v, x, et) in
+ aux (hd :: c) names p it et tl
+ | Some (C.Anonymous as n, C.Decl v) as hd :: tl ->
+ let p = C.Lambda (n, meta, p) in
+ let it = C.Lambda (n, meta, it) in
+ let et = C.Lambda (n, meta, et) in
+ aux (hd :: c) names p it et tl
+ | Some (C.Anonymous as n, C.Def (v, _)) as hd :: tl ->
+ let p = C.LetIn (n, meta, meta, p) in
+ let it = C.LetIn (n, meta, meta, it) in
+ let et = C.LetIn (n, meta, meta, et) in
+ aux (hd :: c) names p it et tl
+ | None :: tl -> assert false
+ in
+ match xtypes with
+ | Some (it, et) -> aux [] [] p it et c
+ | None -> c, []
+
+let clear c hyp =
+ let rec aux c = function
+ | [] -> List.rev c
+ | Some (C.Name name, entry) :: tail when name = hyp ->
+ aux (Some (C.Anonymous, entry) :: c) tail
+ | entry :: tail -> aux (entry :: c) tail
+ in
+ aux [] c
+(*
+let elim_inferred_type context goal arg using cpattern =
+ let metasenv, ugraph = [], Un.default_ugraph in
+ let ety = H.get_type "elim_inferred_type" context using in
+ let _splits, args_no = PEH.split_with_whd (context, ety) in
+ let _metasenv, _subst, predicate, _arg, actual_args =
+ PT.mk_predicate_for_elim
+ ~context ~metasenv ~subst:[] ~ugraph ~goal ~arg ~using ~cpattern ~args_no