X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=helm%2Fsoftware%2Fcomponents%2Fng_refiner%2FnCicUnification.ml;h=07eba73b564385b6742ada4848bcb16be7f255fd;hb=aff007d80f87128884c96a664bc88aec69107d85;hp=13ddac96d9be7e7995ae4ddc1b1aa105e6d701c6;hpb=00d66d5ac17cc72d525d8b2cd089a963e1fef3bf;p=helm.git diff --git a/helm/software/components/ng_refiner/nCicUnification.ml b/helm/software/components/ng_refiner/nCicUnification.ml index 13ddac96d..07eba73b5 100644 --- a/helm/software/components/ng_refiner/nCicUnification.ml +++ b/helm/software/components/ng_refiner/nCicUnification.ml @@ -15,6 +15,11 @@ exception UnificationFailure of string Lazy.t;; exception Uncertain of string Lazy.t;; exception AssertFailure of string Lazy.t;; +let refiner_typeof = + ref (fun _ ?localise _ _ _ _ _ -> ignore localise; assert false);; +let set_refiner_typeof f = refiner_typeof := f +;; + let (===) x y = Pervasives.compare x y = 0 ;; let uncert_exc metasenv subst context t1 t2 = @@ -80,7 +85,12 @@ 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 outside () = + try + indent := String.sub !indent 0 (String.length !indent -1) + with + Invalid_argument _ -> indent := "??"; () +;; let pp s = prerr_endline (Printf.sprintf "%-20s" !indent ^ " " ^ Lazy.force s) @@ -89,18 +99,19 @@ let pp s = let ppcontext = NCicPp.ppcontext;; let ppmetasenv = NCicPp.ppmetasenv;; -(*let ppcontext ~metasenv ~subst context = "...";; -let ppmetasenv ~subst metasenv = "...";;*) +let ppcontext ~metasenv:_metasenv ~subst:_subst _context = "...";; +let ppmetasenv ~subst:_subst _metasenv = "...";; let pp _ = ();; -let fix_sorts swap metasenv subst context meta t = +let fix_sorts swap exc t = let rec aux () = function | NCic.Sort (NCic.Type u) as orig -> if swap then match NCicEnvironment.sup u with - | None -> prerr_endline "no sup for" ; - raise (fail_exc metasenv subst context meta t) + | None -> + prerr_endline ("no sup for " ^ NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] orig) ; + raise exc | Some u1 -> if u = u1 then orig else NCic.Sort (NCic.Type u1) else NCic.Sort (NCic.Type ( @@ -121,24 +132,47 @@ let is_locked n subst = with NCicUtils.Subst_not_found _ -> false ;; +let rec mk_irl = + function + 0 -> [] + | n -> NCic.Rel n :: mk_irl (n-1) +;; -let rec lambda_intros metasenv subst context t args = +let rec lambda_intros rdb metasenv subst context t args = let tty = NCicTypeChecker.typeof ~metasenv ~subst context t in - let argsty = List.map (NCicTypeChecker.typeof ~metasenv ~subst context) args in - let rec mk_lambda context n = function - | [] -> + let argsty = + List.map + (fun arg -> arg, NCicTypeChecker.typeof ~metasenv ~subst context arg) args in + let context_of_args = context in + let rec mk_lambda metasenv subst context n processed_args = function + | [] -> let metasenv, _, bo, _ = NCicMetaSubst.mk_meta metasenv context (`WithType (NCicSubstitution.lift n tty)) in - metasenv, bo - | ty::tail -> + metasenv, subst, bo + | (arg,ty)::tail -> let name = "HBeta"^string_of_int n in - let ty = NCicSubstitution.lift n ty in - let metasenv, bo = mk_lambda ((name,NCic.Decl ty)::context) (n+1) tail in - metasenv, NCic.Lambda (name, ty, bo) + let metasenv,_,instance,_ = + NCicMetaSubst.mk_meta metasenv context_of_args `Term in + let meta_applied = + NCicUntrusted.mk_appl instance (List.rev processed_args) in +let metasenv,subst,_,_ = + !refiner_typeof ((rdb :> NRstatus.status)#set_coerc_db NCicCoercion.empty_db) metasenv subst context_of_args meta_applied None +in + let metasenv,subst = + unify rdb true metasenv subst context_of_args meta_applied ty in + let telescopic_ty = NCicSubstitution.lift n instance in + let telescopic_ty = + NCicUntrusted.mk_appl + telescopic_ty (mk_irl (List.length processed_args)) in + let metasenv, subst, bo = + mk_lambda metasenv subst ((name,NCic.Decl telescopic_ty)::context) (n+1) + (arg::processed_args) tail + in + metasenv, subst, NCic.Lambda (name, telescopic_ty, bo) in - mk_lambda context 0 argsty + mk_lambda metasenv subst context 0 [] argsty and instantiate rdb test_eq_only metasenv subst context n lc t swap = (*D*) inside 'I'; try let rc = @@ -159,12 +193,13 @@ and instantiate rdb test_eq_only metasenv subst context n lc t swap = "typeof: " ^ NCicPp.ppterm ~metasenv ~subst ~context t ^ "\nctx:\n"^ ppcontext ~metasenv ~subst context ^ "\nmenv:\n"^ ppmetasenv ~subst metasenv)); + let exc_to_be = fail_exc metasenv subst context (NCic.Meta (n,lc)) t in let t, ty_t = try t, NCicTypeChecker.typeof ~subst ~metasenv context t with | NCicTypeChecker.AssertFailure msg -> (pp (lazy "fine typeof (fallimento)"); - let ft=fix_sorts swap metasenv subst context (NCic.Meta (n,lc)) t in + let ft = fix_sorts swap exc_to_be t in if ft == t then (prerr_endline ( ("ILLTYPED: " ^ NCicPp.ppterm ~metasenv ~subst ~context t @@ -195,7 +230,12 @@ and instantiate rdb test_eq_only metasenv subst context n lc t swap = NCicPp.ppterm ~metasenv ~subst ~context lty ^ " === " ^ NCicPp.ppterm ~metasenv ~subst ~context ty_t)); let metasenv,subst = - unify test_eq_only metasenv subst context lty ty_t in + try + unify test_eq_only metasenv subst context lty ty_t + with NCicEnvironment.BadConstraint _ as exc -> + let ty_t = fix_sorts swap exc_to_be ty_t in + try unify test_eq_only metasenv subst context lty ty_t + with _ -> raise exc in metasenv, subst, t in pp (lazy(string_of_int n ^ " := 111 = "^ @@ -311,17 +351,17 @@ and unify rdb test_eq_only metasenv subst context t1 t2 = | _, NCic.Meta (n, _) when is_locked n subst -> (let (metasenv, subst), i = match NCicReduction.whd ~subst context t1 with - | NCic.Appl (NCic.Meta (i,l)::args) when - not (NCicMetaSubst.flexible subst args) - -> - let metasenv, lambda_Mj = - lambda_intros metasenv subst context t1 args + | NCic.Appl (NCic.Meta (i,l)::args) -> + let metasenv, subst, lambda_Mj = + lambda_intros rdb metasenv subst context t1 args in unify rdb test_eq_only metasenv subst context (C.Meta (i,l)) lambda_Mj, i | NCic.Meta (i,_) -> (metasenv, subst), i - | _ -> assert false + | _ -> + raise (UnificationFailure (lazy "Locked term vs non + flexible term; probably not saturated enough yet!")) in let t1 = NCicReduction.whd ~subst context t1 in let j, lj = @@ -391,45 +431,41 @@ and unify rdb test_eq_only metasenv subst context t1 t2 = with Invalid_argument _ -> raise (fail_exc metasenv subst context t1 t2)) - | NCic.Appl (NCic.Meta (i,l)::args), _ when - not (NCicMetaSubst.flexible subst args) -> - (* we verify that none of the args is a Meta, - since beta expanding w.r.t a metavariable makes no sense *) - let metasenv, lambda_Mj = - lambda_intros metasenv subst context t1 args - in - let metasenv, subst = - unify rdb test_eq_only metasenv subst context - (C.Meta (i,l)) lambda_Mj - in - let metasenv, subst = - unify rdb test_eq_only metasenv subst context t1 t2 - in - (try - let name, ctx, term, ty = NCicUtils.lookup_subst i subst in - let term = eta_reduce subst term in - let subst = List.filter (fun (j,_) -> j <> i) subst in - metasenv, ((i, (name, ctx, term, ty)) :: subst) - with Not_found -> assert false) - - | _, NCic.Appl (NCic.Meta (i,l)::args) when - not(NCicMetaSubst.flexible subst args) -> - let metasenv, lambda_Mj = - lambda_intros metasenv subst context t2 args - in - let metasenv, subst = - unify rdb test_eq_only metasenv subst context - lambda_Mj (C.Meta (i,l)) - in - let metasenv, subst = - unify rdb test_eq_only metasenv subst context t1 t2 - in - (try - let name, ctx, term, ty = NCicUtils.lookup_subst i subst in - let term = eta_reduce subst term in - let subst = List.filter (fun (j,_) -> j <> i) subst in - metasenv, ((i, (name, ctx, term, ty)) :: subst) - with Not_found -> assert false) + | NCic.Appl (NCic.Meta (i,l)::args), _ -> + let metasenv, subst, lambda_Mj = + lambda_intros rdb metasenv subst context t1 args + in + let metasenv, subst = + unify rdb test_eq_only metasenv subst context + (C.Meta (i,l)) lambda_Mj + in + let metasenv, subst = + unify rdb test_eq_only metasenv subst context t1 t2 + in + (try + let name, ctx, term, ty = NCicUtils.lookup_subst i subst in + let term = eta_reduce subst term in + let subst = List.filter (fun (j,_) -> j <> i) subst in + metasenv, ((i, (name, ctx, term, ty)) :: subst) + with Not_found -> assert false) + + | _, NCic.Appl (NCic.Meta (i,l)::args) -> + let metasenv, subst, lambda_Mj = + lambda_intros rdb metasenv subst context t2 args + in + let metasenv, subst = + unify rdb test_eq_only metasenv subst context + lambda_Mj (C.Meta (i,l)) + in + let metasenv, subst = + unify rdb test_eq_only metasenv subst context t1 t2 + in + (try + let name, ctx, term, ty = NCicUtils.lookup_subst i subst in + let term = eta_reduce subst term in + let subst = List.filter (fun (j,_) -> j <> i) subst in + metasenv, ((i, (name, ctx, term, ty)) :: subst) + with Not_found -> assert false) (* processing this case here we avoid a useless small delta step *) | (C.Appl ((C.Const r1) as _hd1::tl1), C.Appl (C.Const r2::tl2)) @@ -587,8 +623,8 @@ pp (lazy (string_of_bool norm1 ^ " ?? " ^ string_of_bool norm2)); | _ -> [] *) in let unif_from_stack t1 t2 b metasenv subst = try - let t1 = NCicReduction.from_stack t1 in - let t2 = NCicReduction.from_stack t2 in + let t1 = NCicReduction.from_stack ~delta:max_int t1 in + let t2 = NCicReduction.from_stack ~delta:max_int t2 in unif_machines metasenv subst (put_in_whd t1 t2) with UnificationFailure _ | Uncertain _ when not b -> metasenv, subst @@ -638,3 +674,5 @@ pp (lazy (string_of_bool norm1 ^ " ?? " ^ string_of_bool norm2)); let unify rdb ?(test_eq_only=false) = indent := ""; unify rdb test_eq_only;; + +let fix_sorts = fix_sorts false (UnificationFailure (lazy "no sup"));;