]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/components/ng_refiner/nCicUnifHint.ml
unification hints with recursive calls do work!
[helm.git] / helm / software / components / ng_refiner / nCicUnifHint.ml
index c797b2eee6d752ca6a5e868384d0c26ba3465663..3b6d163f4bf5e94d73360ca1181996497ed1f59c 100644 (file)
@@ -11,9 +11,9 @@
 
 (* $Id: nCicRefiner.mli 9227 2008-11-21 16:00:06Z tassi $ *)
 
-module COT : Set.OrderedType with type t = NCic.term = 
+module COT : Set.OrderedType with type t = int * NCic.term = 
   struct
-        type t = NCic.term
+        type t = int * NCic.term
         let compare = Pervasives.compare
   end
 
@@ -27,7 +27,7 @@ type db = DB.t
 let dummy = NCic.Const (NReference.reference_of_string "cic:/dummy_conv.dec");;
 let pair t1 t2 = (NCic.Appl [dummy;t1;t2]) ;;
 
-let index_hint hdb context t1 t2 =
+let index_hint hdb context t1 t2 precedence =
   assert (
     (match t1 with
     | NCic.Meta _ | NCic.Appl (NCic.Meta _ :: _) -> false | _ -> true)
@@ -41,7 +41,7 @@ let index_hint hdb context t1 t2 =
      (fun t (n,e) -> 
         match e with
         | NCic.Decl ty -> NCic.Prod (n,ty,t)
-        | _ -> assert false) 
+        | NCic.Def (b,ty) -> NCic.LetIn (n,ty,b,t))
       pair' context in
   let src = 
     List.fold_left 
@@ -49,26 +49,123 @@ let index_hint hdb context t1 t2 =
         match e with
         | NCic.Decl _ -> 
             NCicSubstitution.subst (NCic.Meta (-1,(0,NCic.Irl 0))) t
-        | _ -> assert false) 
+        | NCic.Def _ -> 
+            NCicSubstitution.subst (NCic.Meta (-1,(0,NCic.Irl 0))) t)
      pair' context in
-  DB.index hdb src data
+(*
+  prerr_endline ("INDEXING: " ^ 
+    NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] src ^ " |==> " ^
+    NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] data);
+*)
+  DB.index hdb src (precedence, data)
 ;;
 
 let empty_db = DB.empty ;;
 
+let user_db = ref empty_db ;;
+
+let add_user_provided_hint t precedence =
+  let u = UriManager.uri_of_string "cic:/foo/bar.con" in
+  let c, a, b = 
+    let rec aux ctx = function
+      | NCic.Appl l ->
+          (match List.rev l with
+          | b::a::_ -> ctx, a, b
+          | _ -> assert false)
+      | NCic.Prod (n,s,t) ->
+          aux ((n, NCic.Decl s) :: ctx) t
+      | NCic.LetIn (n,ty,t,b) -> 
+          aux  ((n, NCic.Def (t,ty)) :: ctx) b
+      | _ -> assert false
+    in
+      aux [] (fst (OCic2NCic.convert_term u t))
+  in
+  user_db := index_hint !user_db c a b precedence
+;;
+
 let db () = 
-  let _,_,_,x,_,_ = NCicEnvironment.get_checked_def
-    (NReference.reference_of_string "cic:/matita/tests/pullback/xxx.def(0)")
+  let combine f l =
+   List.flatten
+     (let rec aux = function 
+      | u1 :: tl -> List.map (f u1) tl :: aux tl
+      | [] -> []
+     in aux l)
+  in
+  let mk_hint (u1,_,_) (u2,_,_) = 
+    let l = OCic2NCic.convert_obj u1 
+      (fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph u1)) in
+    let r = OCic2NCic.convert_obj u2 
+      (fst (CicEnvironment.get_obj CicUniv.oblivion_ugraph u2)) in
+    match List.hd l,List.hd r with
+    | (_,h1,_,_,NCic.Constant (_,_,Some l,_,_)), 
+      (_,h2,_,_,NCic.Constant (_,_,Some r,_,_)) ->
+        let rec aux ctx t1 t2 =
+          match t1, t2 with
+          | NCic.Lambda (n1,s1,b1), NCic.Lambda(_,s2,b2) ->
+              if NCicReduction.are_convertible ~subst:[] ~metasenv:[] ctx s1 s2
+              then aux ((n1, NCic.Decl s1) :: ctx) b1 b2
+              else []
+          | b1,b2 -> 
+              if NCicReduction.are_convertible ~subst:[] ~metasenv:[] ctx b1 b2 
+              then begin
+(*
+                prerr_endline ("hint: " ^ NCicPp.ppterm ~metasenv:[] ~subst:[]
+                  ~context:ctx b1 ^ " === " ^ NCicPp.ppterm ~metasenv:[]
+                  ~subst:[] ~context:ctx b2);
+*)
+              let rec mk_rels =  
+                 function 0 -> [] | n -> NCic.Rel n :: mk_rels (n-1) 
+              in 
+              let n1 = 
+                NCic.Appl (NCic.Const(OCic2NCic.reference_of_ouri 
+                 u1 (NReference.Def h1)) :: mk_rels (List.length ctx))
+              in
+              let n2 = 
+                NCic.Appl (NCic.Const(OCic2NCic.reference_of_ouri 
+                 u2 (NReference.Def h2)) :: mk_rels (List.length ctx))
+              in
+                [ctx,b1,b2; ctx,b1,n2; ctx,n1,b2; ctx,n1,n2]
+              end else []
+        in
+          aux [] l r
+    | _ -> []
   in
-  let rec decontextualize ctx = function
-    | NCic.Prod (n,s,t) -> decontextualize ((n,NCic.Decl s)::ctx) t
-    | t -> ctx, t
+  let hints = 
+    List.fold_left 
+      (fun acc (_,_,l) -> 
+          acc @ 
+          if List.length l > 1 then 
+           combine mk_hint l
+          else [])
+      [] (CoercDb.to_list ())
   in
-    match (decontextualize [] x) with
-    | ctx, NCic.Appl [_;_;a;b] -> index_hint empty_db ctx a b
-    | _ -> assert false
+  List.fold_left 
+    (fun db -> function 
+     | (ctx,b1,b2) -> index_hint db ctx b1 b2 0)
+    !user_db (List.flatten hints)
 ;;
 
+let saturate ?(delta=0) metasenv subst context ty goal_arity =
+ assert (goal_arity >= 0);
+  let rec aux metasenv = function
+   | NCic.Prod (name,s,t) as ty ->
+       let metasenv1, arg,_ = 
+          NCicMetaSubst.mk_meta ~name:name metasenv context (`WithType s) in
+       let t, metasenv1, args, pno = 
+           aux metasenv1 (NCicSubstitution.subst arg t) 
+       in
+       if pno + 1 = goal_arity then
+         ty, metasenv, [], goal_arity+1
+       else
+         t, metasenv1, arg::args, pno+1
+   | ty ->
+        match NCicReduction.whd ~subst context ty ~delta with
+        | NCic.Prod _ as ty -> aux metasenv ty
+        | _ -> ty, metasenv, [], 0 (* differs from the other impl in this line*)
+  in
+  let res, newmetasenv, arguments, _ = aux metasenv ty in
+  res, newmetasenv, arguments
+;;
 
 let look_for_hint hdb metasenv subst context t1 t2 =
 (*
@@ -84,19 +181,57 @@ let look_for_hint hdb metasenv subst context t1 t2 =
   let candidates1 = DB.retrieve_unifiables hdb (pair t1 t2) in
   let candidates2 = DB.retrieve_unifiables hdb (pair t2 t1) in
   let candidates1 = 
-    List.map (fun ty -> 
-       true,NCicMetaSubst.saturate ~delta:max_int metasenv subst context ty 0) 
+    List.map (fun (prec,ty) -> 
+       prec,true,saturate ~delta:max_int metasenv subst context ty 0) 
     (HintSet.elements candidates1) 
   in
   let candidates2 = 
-    List.map (fun ty -> 
-       false,NCicMetaSubst.saturate ~delta:max_int metasenv subst context ty 0) 
+    List.map (fun (prec,ty) -> 
+       prec,false,saturate ~delta:max_int metasenv subst context ty 0) 
     (HintSet.elements candidates2) 
   in
+  let rc = 
+    List.map
+      (fun (p,b,(t,m,_)) ->
+         let rec aux () (m,l as acc) = function
+           | NCic.Meta _ as t -> acc, t
+           | NCic.LetIn (name,ty,bo,t) ->
+               let m,i,_=NCicMetaSubst.mk_meta ~name m context (`WithType ty)in
+               let t = NCicSubstitution.subst i t in
+               aux () (m, (i,bo)::l) t
+           | t -> NCicUntrusted.map_term_fold_a (fun _ () -> ()) () aux acc t
+         in
+         let (m,l), t = aux () (m,[]) t in
+         p,b,(t,m,l))
+   (candidates1 @ candidates2)
+  in
+  let rc = 
   List.map
    (function 
-    | (true,(NCic.Appl [_; t1; t2],metasenv,_)) -> metasenv, t1, t2
-    | (false,(NCic.Appl [_; t1; t2],metasenv,_)) -> metasenv, t2, t1
+    | (prec,true,(NCic.Appl [_; t1; t2],metasenv,l))-> prec,metasenv,(t1,t2),l
+    | (prec,false,(NCic.Appl [_; t1; t2],metasenv,l))-> prec,metasenv,(t2,t1),l
     | _ -> assert false)
-   (candidates1 @ candidates2)
+    rc
+  in
+  let rc = 
+    List.sort (fun (x,_,_,_) (y,_,_,_) -> Pervasives.compare x y) rc
+  in 
+  let rc = List.map (fun (_,x,y,z) -> x,y,z) rc in
+(*
+    prerr_endline "Hints:";
+    List.iter 
+      (fun (metasenv, (t1, t2), premises) ->
+          prerr_endline 
+          ("\t" ^ String.concat ";  "
+                (List.map (fun (a,b) -> 
+                   NCicPp.ppterm ~margin:max_int ~metasenv ~subst ~context a ^
+                   " =?= "^
+                   NCicPp.ppterm ~margin:max_int ~metasenv ~subst ~context b)
+                premises) ^     
+              "  ==> "^
+              NCicPp.ppterm ~margin:max_int ~metasenv ~subst ~context t1 ^
+              " = "^NCicPp.ppterm ~margin:max_int ~metasenv ~subst ~context t2))
+      rc;
+*)
+  rc
 ;;