* http://cs.unibo.it/helm/.
*)
-module C = Cic
+module C = Cic
+module E = CicEnvironment
+module Un = CicUniv
+module TC = CicTypeChecker
+module D = Deannotate
+module UM = UriManager
+module Rd = CicReduction
-let rec need_whd i = function
- | C.ACast (_, t, _) -> need_whd i t
- | C.AProd (_, _, _, t) when i > 0 -> need_whd (pred i) t
- | _ when i > 0 -> true
- | _ -> false
+module P = ProceduralPreprocess
+module T = ProceduralTypes
+module M = ProceduralMode
+
+(* helpers ******************************************************************)
+
+let cic = D.deannotate_term
+
+let rec list_sub start length = function
+ | _ :: tl when start > 0 -> list_sub (pred start) length tl
+ | hd :: tl when length > 0 -> hd :: list_sub start (pred length) tl
+ | _ -> []
+
+(* proof construction *******************************************************)
let lift k n =
let rec lift_xns k (uri, t) = uri, lift_term k t
and lift_term k = function
| C.ASort _ as t -> t
| C.AImplicit _ as t -> t
- | C.ARel (id, rid, m, b) as t -> if m < k then t else C.ARel (id, rid, m + n, b)
+ | C.ARel (id, rid, m, b) as t ->
+ if m < k then t else
+ if m + n > 0 then C.ARel (id, rid, m + n, b) else
+ assert false
| C.AConst (id, uri, xnss) -> C.AConst (id, uri, List.map (lift_xns k) xnss)
| C.AVar (id, uri, xnss) -> C.AVar (id, uri, List.map (lift_xns k) xnss)
| C.AMutInd (id, uri, tyno, xnss) -> C.AMutInd (id, uri, tyno, List.map (lift_xns k) xnss)
| C.ACoFix (id, i, fl) -> C.ACoFix (id, i, List.map (lift_cofix (List.length fl) k) fl)
in
lift_term k
+
+let clear_absts m =
+ let rec aux k n = function
+ | C.AImplicit (_, None) as t -> t
+ | C.ALambda (id, s, v, t) when k > 0 ->
+ C.ALambda (id, s, v, aux (pred k) n t)
+ | C.ALambda (_, _, _, t) when n > 0 ->
+ aux 0 (pred n) (lift 1 (-1) t)
+ | t when n > 0 ->
+ Printf.eprintf "CLEAR: %u %s\n" n (CicPp.ppterm (cic t));
+ assert false
+ | t -> t
+ in
+ aux m
+
+let hole id = C.AImplicit (id, Some `Hole)
+
+let meta id = C.AImplicit (id, None)
+
+let anon = C.Anonymous
+
+let generalize n =
+ let is_meta =
+ let map b = function
+ | C.AImplicit (_, None) when b -> b
+ | _ -> false
+ in
+ 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 m = succ (k - n) 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, t) ->
+ let s, t = gen_term k s, gen_term (succ k) t in
+ if is_meta [s; t] then meta id else C.ALetIn (id, anon, s, 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 0
+
+let mk_pattern rpsno predicate =
+ let body = generalize rpsno predicate in
+ clear_absts 0 rpsno body