+let mk_appl hd tl =
+ match hd with
+ | NCic.Appl l -> NCic.Appl (l@tl)
+ | _ -> NCic.Appl (hd :: tl)
+;;
+
+let flexible l =
+ List.exists
+ (function
+ | NCic.Meta _
+ | NCic.Appl (NCic.Meta _::_) -> true
+ | _ -> false) l
+;;
+
+exception WrongShape;;
+
+let eta_reduce =
+ let delift_if_not_occur body orig =
+ try
+ NCicSubstitution.psubst ~avoid_beta_redexes:true
+ (fun () -> raise WrongShape) [()] body
+ with WrongShape -> orig
+ in
+ function
+ | NCic.Lambda(_, _, NCic.Appl [hd; NCic.Rel 1]) as orig ->
+ delift_if_not_occur hd orig
+ | NCic.Lambda(_, _, NCic.Appl (hd :: l)) as orig
+ when HExtlib.list_last l = NCic.Rel 1 ->
+ let body =
+ let args, _ = HExtlib.split_nth (List.length l - 1) l in
+ NCic.Appl (hd::args)
+ in
+ delift_if_not_occur body orig
+ | t -> t
+;;
+
+module C = NCic;;
+module Ref = NReference;;
+
+let indent = ref "";;
+let inside c = indent := !indent ^ String.make 1 c;;
+let outside () = indent := String.sub !indent 0 (String.length !indent -1);;
+
+(*
+let pp s =
+ prerr_endline (Printf.sprintf "%-20s" !indent ^ " " ^ Lazy.force s)
+;;
+*)
+
+let pp _ = ();;
+
+let rec beta_expand num test_eq_only swap metasenv subst context t arg =
+ let rec aux (n,context,test_eq_only as k) (metasenv, subst as acc) t' =
+ try
+ let metasenv, subst =
+ if swap then
+ unify test_eq_only metasenv subst context t' (NCicSubstitution.lift n arg)
+ else
+ unify test_eq_only metasenv subst context (NCicSubstitution.lift n arg) t'
+ in
+ (metasenv, subst), NCic.Rel (1 + n)
+ with Uncertain _ | UnificationFailure _ ->
+ match t' with
+ | NCic.Rel m as orig ->
+ (metasenv, subst), if m <= n then orig else NCic.Rel (m+1)
+ (* andrea: in general, beta_expand can create badly typed
+ terms. This happens quite seldom in practice, UNLESS we
+ iterate on the local context. For this reason, we renounce
+ to iterate and just lift *)
+ | NCic.Meta (i,(shift,lc)) ->
+ (metasenv,subst), NCic.Meta (i,(shift+1,lc))
+ | NCic.Prod (name, src, tgt) as orig ->
+ let (metasenv, subst), src1 = aux (n,context,true) acc src in
+ let k = n+1, (name, NCic.Decl src) :: context, test_eq_only in
+ let ms, tgt1 = aux k (metasenv, subst) tgt in
+ if src == src1 && tgt == tgt1 then ms, orig else
+ ms, NCic.Prod (name, src1, tgt1)
+ | t ->
+ NCicUntrusted.map_term_fold_a
+ (fun e (n,ctx,teq) -> n+1,e::ctx,teq) k aux acc t
+
+ in
+ let argty = NCicTypeChecker.typeof ~metasenv ~subst context arg in
+ let fresh_name = "Hbeta" ^ string_of_int num in
+ let (metasenv,subst), t1 =
+ aux (0, context, test_eq_only) (metasenv, subst) t in
+ let t2 = eta_reduce (NCic.Lambda (fresh_name,argty,t1)) in
+ try
+ ignore(NCicTypeChecker.typeof ~metasenv ~subst context t2);
+ metasenv, subst, t2
+ with NCicTypeChecker.TypeCheckerFailure _ ->
+ metasenv, subst, NCic.Lambda ("_", argty, NCicSubstitution.lift 1 arg)
+
+and beta_expand_many test_equality_only swap metasenv subst context t args =
+(* (*D*) inside 'B'; try let rc = *)
+ pp (lazy (String.concat ", "
+ (List.map (NCicPp.ppterm ~metasenv ~subst ~context)
+ args) ^ " ∈ " ^ NCicPp.ppterm ~metasenv ~subst ~context t));
+ let _, subst, metasenv, hd =
+ List.fold_right
+ (fun arg (num,subst,metasenv,t) ->
+ let metasenv, subst, t =
+ beta_expand num test_equality_only swap metasenv subst context t arg
+ in
+ num+1,subst,metasenv,t)
+ args (1,subst,metasenv,t)
+ in
+ pp (lazy ("Head syntesized by b-exp: " ^
+ NCicPp.ppterm ~metasenv ~subst ~context hd));
+ metasenv, subst, hd
+(* (*D*) in outside (); rc with exn -> outside (); raise exn *)
+
+and instantiate test_eq_only metasenv subst context n lc t swap =
+(* (*D*) inside 'I'; try let rc = *)
+ let unify test_eq_only m s c t1 t2 =
+ if swap then unify test_eq_only m s c t2 t1
+ else unify test_eq_only m s c t1 t2
+ in
+ let name, ctx, ty = NCicUtils.lookup_meta n metasenv in
+ let metasenv, subst =
+ match ty with
+ | NCic.Implicit (`Typeof _) -> metasenv, subst
+ | _ ->
+ let lty = NCicSubstitution.subst_meta lc ty in
+ let ty_t =
+ try NCicTypeChecker.typeof ~subst ~metasenv context t
+ with NCicTypeChecker.TypeCheckerFailure msg ->
+ prerr_endline (NCicPp.ppterm ~metasenv ~subst ~context t);
+ prerr_endline (Lazy.force msg);
+ assert false
+ in
+ pp (lazy("On the types: " ^
+ NCicPp.ppterm ~metasenv ~subst ~context lty ^ " === "
+ ^ NCicPp.ppterm ~metasenv ~subst ~context ty_t));
+ unify test_eq_only metasenv subst context lty ty_t
+ in
+ let (metasenv, subst), t =
+ try NCicMetaSubst.delift metasenv subst context n lc t
+ with NCicMetaSubst.Uncertain msg -> raise (Uncertain msg)
+ | NCicMetaSubst.MetaSubstFailure msg -> raise (UnificationFailure msg)
+ in
+ (* Unifying the types may have already instantiated n. *)
+ try
+ let _, _,oldt,_ = NCicUtils.lookup_subst n subst in
+ let oldt = NCicSubstitution.subst_meta lc oldt in
+ let t = NCicSubstitution.subst_meta lc t in
+ (* conjecture: always fail --> occur check *)
+ unify test_eq_only metasenv subst context oldt t
+ with NCicUtils.Subst_not_found _ ->
+ (* by cumulativity when unify(?,Type_i)
+ * we could ? := Type_j with j <= i... *)
+ let subst = (n, (name, ctx, t, ty)) :: subst in
+ pp (lazy ("?"^string_of_int n^" := "^NCicPp.ppterm
+ ~metasenv ~subst ~context (NCicSubstitution.subst_meta lc t)));
+ let metasenv =
+ List.filter (fun (m,_) -> not (n = m)) metasenv
+ in
+ metasenv, subst
+(* (*D*) in outside(); rc with exn -> outside (); raise exn *)
+
+and unify test_eq_only metasenv subst context t1 t2 =
+(* (*D*) inside 'U'; try let rc = *)
+ let fo_unif test_eq_only metasenv subst t1 t2 =
+(* (*D*) inside 'F'; try let rc = *)
+(*
+ pp (lazy(" " ^ NCicPp.ppterm ~metasenv ~subst ~context t1 ^ " === " ^
+ NCicPp.ppterm ~metasenv ~subst ~context t2));
+*)