+and try_coercions
+ ~localise ~look_for_coercion
+ metasenv subst context t orig_t infty expty perform_unification exc
+=
+ (* we try with a coercion *)
+ let rec first exc = function
+ | [] ->
+ raise (wrap_exc (lazy (localise orig_t, Printf.sprintf
+ "The term %s has type %s but is here used with type %s"
+ (NCicPp.ppterm ~metasenv ~subst ~context t)
+ (NCicPp.ppterm ~metasenv ~subst ~context infty)
+ (NCicPp.ppterm ~metasenv ~subst ~context expty))) exc)
+ | (metasenv, newterm, newtype, meta)::tl ->
+ try
+ let metasenv, subst =
+ NCicUnification.unify metasenv subst context meta t
+ in
+ let metasenv, subst =
+ if perform_unification then
+ NCicUnification.unify metasenv subst context newtype expty
+ else metasenv, subst
+ in
+ metasenv, subst, newterm, newtype
+ with
+ | NCicUnification.UnificationFailure _ -> first exc tl
+ | NCicUnification.Uncertain _ as exc -> first exc tl
+ in
+ first exc
+ (look_for_coercion metasenv subst context infty expty)
+
+and force_to_sort
+ ~look_for_coercion metasenv subst context t orig_t localise ty
+=
+ match NCicReduction.whd ~subst context ty with
+ | C.Meta (_,(0,(C.Irl 0 | C.Ctx []))) as ty ->
+ metasenv, subst, t, ty
+ | C.Meta (i,(_,(C.Irl 0 | C.Ctx []))) ->
+ metasenv, subst, t, C.Meta(i,(0,C.Irl 0))
+ | C.Meta (i,(_,lc)) ->
+ let len = match lc with C.Irl len->len | C.Ctx l->List.length l in
+ let metasenv, subst, newmeta =
+ if len > 0 then
+ NCicMetaSubst.restrict metasenv subst i (HExtlib.list_seq 1 (len+1))
+ else metasenv, subst, i
+ in
+ metasenv, subst, t, C.Meta (newmeta,(0,C.Irl 0))
+ | C.Sort _ as ty -> metasenv, subst, t, ty
+ | ty ->
+ try_coercions ~localise ~look_for_coercion metasenv subst context
+ t orig_t ty (NCic.Sort (NCic.Type NCicEnvironment.type0)) false
+ (RefineFailure (lazy (localise orig_t,
+ "The type of " ^ NCicPp.ppterm ~metasenv ~subst ~context t
+ ^ " is not a sort: " ^ NCicPp.ppterm ~metasenv ~subst ~context ty)))
+
+and sort_of_prod ~look_for_coercion
+ localise metasenv subst context orig_s orig_t (name,s) t (t1, t2)
+=
+ let metasenv, subst, s, t1 =
+ force_to_sort
+ ~look_for_coercion metasenv subst context s orig_s localise t1 in
+ let metasenv, subst, t, t2 =
+ force_to_sort ~look_for_coercion metasenv subst ((name,C.Decl s)::context)
+ t orig_t localise t2 in
+ match t1, t2 with
+ | C.Sort _, C.Sort C.Prop -> metasenv, subst, s, t, t2
+ | C.Sort (C.Type u1), C.Sort (C.Type u2) ->
+ metasenv, subst, s, t, C.Sort (C.Type (u1@u2))
+ | C.Sort C.Prop,C.Sort (C.Type _) -> metasenv, subst, s, t, t2
+ | C.Meta _, C.Sort _
+ | C.Meta _, C.Meta (_,(_,_))
+ | C.Sort _, C.Meta (_,(_,_)) -> metasenv, subst, s, t, t2
+ | x, (C.Sort _ | C.Meta _) | _, x ->
+ let y, context, orig =
+ if x == t1 then s, context, orig_s
+ else t, ((name,C.Decl s)::context), orig_t
+ in
+ raise (RefineFailure (lazy (localise orig,Printf.sprintf
+ "%s is expected to be a type, but its type is %s that is not a sort"
+ (NCicPp.ppterm ~subst ~metasenv ~context y)
+ (NCicPp.ppterm ~subst ~metasenv ~context x))))
+
+and eat_prods
+ ~localise ~look_for_coercion metasenv subst context orig_he he ty_he args
+=