]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/components/ng_refiner/nCicUnification.ml
Beta-expansion was avoided as soon as one argument was flexible.
[helm.git] / helm / software / components / ng_refiner / nCicUnification.ml
index 9ee056aed4f6a6911ea11c658bafe4873c019494..795b8504296249d2e624d8005a776c2cd2eaa4cb 100644 (file)
@@ -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,11 +85,22 @@ 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)
-;;  
+;;
+
+let ppcontext = NCicPp.ppcontext;;
+let ppmetasenv = NCicPp.ppmetasenv;;
+
+let ppcontext ~metasenv:_metasenv ~subst:_subst _context = "...";;
+let ppmetasenv ~subst:_subst _metasenv = "...";;
 
 let pp _ = ();;
 
@@ -115,24 +131,46 @@ 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 mk_appl = function [] -> assert false | [t] -> t | l -> NCic.Appl l 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 = 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 =
+        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 =  
@@ -151,8 +189,8 @@ and instantiate rdb test_eq_only metasenv subst context n lc t swap =
     | _ ->
        pp (lazy (
          "typeof: " ^ NCicPp.ppterm ~metasenv ~subst ~context t ^ "\nctx:\n"^
-          NCicPp.ppcontext ~metasenv ~subst context ^ "\nmenv:\n"^
-          NCicPp.ppmetasenv ~subst metasenv));
+          ppcontext ~metasenv ~subst context ^ "\nmenv:\n"^
+          ppmetasenv ~subst metasenv));
        let t, ty_t = 
          try t, NCicTypeChecker.typeof ~subst ~metasenv context t 
          with 
@@ -163,8 +201,8 @@ and instantiate rdb test_eq_only metasenv subst context n lc t swap =
              (prerr_endline ( ("ILLTYPED: " ^ 
                 NCicPp.ppterm ~metasenv ~subst ~context t
             ^ "\nBECAUSE:" ^ Lazy.force msg ^ "\nCONTEXT:\n" ^
-            NCicPp.ppcontext ~metasenv ~subst context ^ "\nMENV:\n" ^
-            NCicPp.ppmetasenv ~subst metasenv
+            ppcontext ~metasenv ~subst context ^ "\nMENV:\n" ^
+            ppmetasenv ~subst metasenv
             ));
                      assert false)
            else
@@ -214,7 +252,7 @@ and instantiate rdb test_eq_only metasenv subst context n lc t swap =
   in
          pp (lazy(string_of_int n ^ " := 222 = "^
            NCicPp.ppterm ~metasenv ~subst ~context:ctx t
-         ^ "\n" ^ NCicPp.ppmetasenv ~subst metasenv));
+         ^ "\n" ^ ppmetasenv ~subst metasenv));
   (* Unifying the types may have already instantiated n. *)
   try
     let _, _,oldt,_ = NCicUtils.lookup_subst n subst in
@@ -239,7 +277,7 @@ and unify rdb test_eq_only metasenv subst context t1 t2 =
    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 ^ "\n" ^ NCicPp.ppmetasenv
+         NCicPp.ppterm ~metasenv ~subst ~context t2 ^ "\n" ^ ppmetasenv
          ~subst metasenv));
      if t1 === t2 then
        metasenv, subst
@@ -305,11 +343,9 @@ 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,
@@ -324,6 +360,20 @@ and unify rdb test_eq_only metasenv subst context t1 t2 =
               let metasenv, subst = 
                 instantiate rdb test_eq_only metasenv subst context j lj t2 true
               in
+              (* We need to remove the out_scope_tags to avoid propagation of
+                 them that triggers again the ad-hoc case *)
+              let subst =
+               List.map (fun (i,(tag,ctx,bo,ty)) ->
+                let tag =
+                 match tag with
+                    Some tag when
+                        tag = NCicMetaSubst.in_scope_tag
+                     || NCicMetaSubst.is_out_scope_tag tag -> None
+                  | _ -> tag
+                in
+                  i,(tag,ctx,bo,ty)
+                ) subst
+              in
               (try
                 let name, ctx, term, ty = NCicUtils.lookup_subst i subst in
                 let term = eta_reduce subst term in
@@ -371,45 +421,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), _ ->
+          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) 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 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)) 
@@ -490,8 +536,7 @@ and unify rdb test_eq_only metasenv subst context t1 t2 =
         NCicPp.ppterm ~metasenv ~subst ~context t2);
 *)
       let candidates = 
-        NCicUnifHint.look_for_hint 
-          rdb.NRstatus.uhint_db metasenv subst context t1 t2
+        NCicUnifHint.look_for_hint rdb metasenv subst context t1 t2
       in
       let rec cand_iter = function
         | [] -> None (* raise exc *)