]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/components/ng_refiner/nCicUnification.ml
librarian.ml: now the read_only .moo's are managed "correctly" (i.e. better than...
[helm.git] / helm / software / components / ng_refiner / nCicUnification.ml
index 6a47817946bb92e0b51eeb213576f0c991fc2eee..147bb555fe349a063c9f6fc77cb52e7993d6bd56 100644 (file)
@@ -23,7 +23,124 @@ let fail_exc metasenv subst context t1 t2 =
   " with " ^ NCicPp.ppterm ~metasenv ~subst ~context t2))
 ;;
 
-let unify metasenv subst context t1 t2 =
+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(name, src, NCic.Appl [hd; NCic.Rel 1]) as orig ->
+      delift_if_not_occur hd orig
+  | NCic.Lambda(name, src, 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
+;;
+  
+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 =
+     unify test_eq_only metasenv subst context
+      (NCicSubstitution.lift n arg) t'
+    in
+     (metasenv, subst), C.Rel (1 + n)
+   with Uncertain _ | UnificationFailure _ ->
+       match t' with
+       | NCic.Rel m 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 (metasenv,subst), tgt1 = aux k (metasenv, subst) tgt in 
+           if src == src1 && tgt == tgt1 then orig else
+           NCic.Prod (name, src1, tgt1)
+       | t -> 
+           NCicUntrusted.map_term_fold_a 
+             (fun e (n,ctx) -> n+1,e::ctx) 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 (C.Lambda (fresh_name,argty,t1)) in
+  try 
+    ignore(NCicTypeChecker.typeof ~metasenv ~subst context t2);
+    metasenv, subst, t2
+  with NCicTypeChecker.TypeCheckerFailure _ -> 
+    NCic.Lambda ("_", argty, NCicSubstitution.lift 1 arg)
+
+and beta_expand_many test_equality_only metasenv subst context t args ugraph =
+  let _, subst, metasenv, hd =
+    List.fold_right
+      (fun arg (num,subst,metasenv,t) ->
+         let subst, metasenv, t =
+           beta_expand num test_equality_only metasenv subst context t arg
+         in
+           num+1,subst,metasenv,t)
+      args (1,subst,metasenv,t) 
+  in
+    metasenv, subst, hd
+
+and instantiate test_eq_only metasenv subst context n lc t swap =
+  let unif m s c t1 t2 =
+    if swap then unify m s c t2 t1 else unify m s c t1 t2
+  in
+  let ty_t = 
+    try NCicTypeChecker.typeof ~subst ~metasenv context t 
+    with NCicTypeChecker.TypeCheckerFailure _ -> assert false
+  in
+  let name, ctx, ty = NCicUtils.lookup_meta n metasenv in
+  let ty = NCicSubstitution.subst_meta lc ty in
+  let metasenv, subst = unify metasenv susbt context ty ty_t in
+  let (metasenv, subst), t = 
+    NCicMetaSubst.delift metasenv subst context n lc t
+  in
+  (* Unifying the types may have already instantiated n. *)
+  try
+    let _, _,oldt,_ = CicUtil.lookup_subst n subst in
+    let oldt = NCicSubstitution.subst_meta lc oldt in
+    (* conjecture: always fail --> occur check *)
+    unify test_eq_only metasenv subst context oldt t
+  with CicUtil.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
+    let metasenv = 
+      List.filter (fun (m,_) -> not (n = m)) metasenv 
+    in
+    subst, metasenv
+
+and unify metasenv subst context t1 t2 =
  let rec aux test_eq_only metasenv subst context t1 t2 =
    let fo_unif test_eq_only metasenv subst t1 t2 =
      if t1 === t2 then
@@ -79,25 +196,61 @@ let unify metasenv subst context t1 t2 =
                   aux test_eq_only metasenv subst context term1 term2
               with NCicUtils.Subst_not_found _-> raise (UnificationFailure msg))
 
-       | C.Meta (n1,l1), _
-       | _, C.Meta (n2,l2) ->
+       | C.Meta (n,lc), t -> 
+           try 
+             let _,_,term,_ = NCicUtils.lookup_subst n subst in
+             let term = NCicSubstitution.subst_meta lc term in
+               aux test_eq_only metasenv subst context term t
+           with NCicUtils.Subst_not_found _-> 
+             instantiate test_eq_only metasenv subst context n lc t false
 
+       | t, C.Meta (n,lc) -> 
+           try 
+             let _,_,term,_ = NCicUtils.lookup_subst n subst in
+             let term = NCicSubstitution.subst_meta lc term in
+               aux test_eq_only metasenv subst context t term
+           with NCicUtils.Subst_not_found _-> 
+             instantiate test_eq_only metasenv subst context n lc t true
 
+       | NCic.Appl (NCic.Meta (i,l)::args), _ when List.mem_assoc i subst ->
+            let _,_,term,_ = NCicUtils.lookup_subst i subst in
+            let term = NCicSubstitution.subst_meta l term in
+              aux test_eq_only metasenv subst context (mk_appl term args) t2
 
+       | _, NCic.Appl (NCic.Meta (i,l)::args) when List.mem_assoc i subst ->
+            let _,_,term,_ = NCicUtils.lookup_subst i subst in
+            let term = NCicSubstitution.subst_meta l term in
+              aux test_eq_only metasenv subst context t1 (mk_appl term args)
 
-          (try 
-             let _,_,term,_ = NCicUtils.lookup_subst n1 subst in
-             let term = NCicSubstitution.subst_meta l1 term in
-              aux test_eq_only metasenv subst context term t2
-           with NCicUtils.Subst_not_found _ -> 
-             
-       )
-          (try 
-             let _,_,term,_ = NCicUtils.lookup_subst n2 subst in
-             let term = NCicSubstitution.subst_meta l2 term in
-              aux test_eq_only metasenv subst context t1 term
-           with NCicUtils.Subst_not_found _ -> false)
-       
+       |  NCic.Appl (NCic.Meta (i,_)::_ as l1),
+          NCic.Appl (NCic.Meta (j,_)::_ as l2) when i=j ->
+            try
+              List.fold_left2 
+                (fun (metasenv, subst) t1 t2 ->
+                  aux test_eq_only metasenv subst context t1 t2)
+                (metasenv,subst) l1 l2
+            with Invalid_argument _ -> 
+              raise (fail_exc metasenv subst context t1 t2)
+
+       | NCic.Appl (NCic.Meta (i,l)::args), _ when not (flexible args) ->
+           (* we verify that none of the args is a Meta, 
+              since beta expanding w.r.t a metavariable makes no sense  *)
+              let subst, metasenv, beta_expanded =
+                beta_expand_many (* passare swap *)
+                  test_equality_only metasenv subst context t2 args ugraph 
+              in
+                aux test_eq_only metasenv subst context 
+                  (C.Meta (i,l)) beta_expanded 
+       | _, NCic.Appl (NCic.Meta (i,l)::args) when not(flexible args) ->
+                 let subst,metasenv,beta_expanded =
+                   beta_expand_many 
+                     test_equality_only 
+                     metasenv subst context t1 args ugraph 
+                 in
+                   fo_unif_subst test_equality_only subst context metasenv 
+                     (C.Meta (i,l)) beta_expanded ugraph1
+          | _,_ ->
+.......       
        | (C.Appl ((C.Const r1) as _hd1::tl1), C.Appl (C.Const r2::tl2)) 
            when (Ref.eq r1 r2 && 
              List.length (E.get_relevance r1) >= List.length tl1) ->