]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/components/ng_refiner/nCicUnifHint.ml
the trie indexes terms up to 10 nested applications and skips applications with more...
[helm.git] / helm / software / components / ng_refiner / nCicUnifHint.ml
index ce88085521c2ddabda8d96069f9fba79bb90a876..313fbbd0d13470354a71d607468df38114fd001a 100644 (file)
 let debug s = prerr_endline (Lazy.force s);;
 let debug _ = ();;
 
-module COT : Set.OrderedType with type t = int * NCic.term = 
+module HOT : Set.OrderedType 
+with type t = int * NCic.term  * NCic.term * NCic.term = 
   struct
-        type t = int * NCic.term
+        (* precedence, skel1, skel2, term *)
+        type t = int * NCic.term * NCic.term * NCic.term
         let compare = Pervasives.compare
   end
 
-module HintSet = Set.Make(COT)
+module EOT : Set.OrderedType 
+with type t = int * NCic.term =
+  struct
+        type t = int * NCic.term 
+        let compare = Pervasives.compare
+  end
+
+module HintSet = Set.Make(HOT)
+module EqSet = Set.Make(EOT)
 
 module HDB = 
   Discrimination_tree.Make(NDiscriminationTree.NCicIndexable)(HintSet)
 
 module EQDB = 
-  Discrimination_tree.Make(NDiscriminationTree.NCicIndexable)(HintSet)
+  Discrimination_tree.Make(NDiscriminationTree.NCicIndexable)(EqSet)
 
 type db =
   HDB.t * (* hint database: (dummy A B)[?] |-> \forall X.(summy a b)[X] *)
@@ -86,16 +96,19 @@ let index_hint hdb context t1 t2 precedence =
         | NCic.Def (b,ty) -> NCic.LetIn (n,ty,b,t))
       t context 
   in
-  let data_hint = ctx2abstractions context (pair t1 t2) in
+  let data_hint = 
+    precedence, t1_skeleton, t2_skeleton, ctx2abstractions context (pair t1 t2)
+  in
   let data_t1 = t2_skeleton in
   let data_t2 = t1_skeleton in
 
   debug(lazy ("INDEXING: " ^ 
     NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] src ^ " |==> " ^
-    NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] data_hint));
+    NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] 
+      (let _,x,_,_ = data_hint in x)));
 
   hdb#set_uhint_db (
-      HDB.index (fst (hdb#uhint_db)) src (precedence, data_hint),
+      HDB.index (fst (hdb#uhint_db)) src data_hint,
       EQDB.index  
         (EQDB.index (snd (hdb#uhint_db)) t2_skeleton (precedence, data_t2))
         t1_skeleton (precedence, data_t1))
@@ -131,6 +144,7 @@ let add_user_provided_hint db t precedence =
    index_hint db c a b precedence
 ;;
 
+(*
 let db () = 
   let combine f l =
    List.flatten
@@ -190,13 +204,15 @@ let db () =
     !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
+          NCicMetaSubst.mk_meta ~attrs:[`Name name] metasenv context
+           (`WithType s) in
        let t, metasenv1, args, pno = 
            aux metasenv1 (NCicSubstitution.subst arg t) 
        in
@@ -222,7 +238,7 @@ let eq_class_of hdb t1 =
           it sould retulr the whole content of the trie *)
   else
     let candidates = EQDB.retrieve_unifiables (snd hdb#uhint_db) t1 in
-    let candidates = HintSet.elements candidates in
+    let candidates = EqSet.elements candidates in
     let candidates = List.sort (fun (x,_) (y,_) -> compare x y) candidates in
     List.map snd candidates
  in
@@ -233,6 +249,11 @@ let eq_class_of hdb t1 =
 ;;
 
 let look_for_hint hdb metasenv subst context t1 t2 =
+  if NDiscriminationTree.NCicIndexable.path_string_of t1 =
+          [Discrimination_tree.Variable] || 
+     NDiscriminationTree.NCicIndexable.path_string_of t2 =
+             [Discrimination_tree.Variable] then [] else begin
+
   debug(lazy ("KEY1: "^NCicPp.ppterm ~metasenv ~subst ~context t1));
   debug(lazy ("KEY2: "^NCicPp.ppterm ~metasenv ~subst ~context t2));
 (*
@@ -246,12 +267,12 @@ let look_for_hint hdb metasenv subst context t1 t2 =
   let candidates1 = HDB.retrieve_unifiables (fst hdb#uhint_db) (pair t1 t2) in
   let candidates2 = HDB.retrieve_unifiables (fst hdb#uhint_db) (pair t2 t1) in
   let candidates1 = 
-    List.map (fun (prec,ty) -> 
+    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 (prec,ty) -> 
+    List.map (fun (prec,_,_,ty) -> 
        prec,false,saturate ~delta:max_int metasenv subst context ty 0) 
     (HintSet.elements candidates2) 
   in
@@ -262,7 +283,8 @@ let look_for_hint hdb metasenv subst context t1 t2 =
            | NCic.Meta _ as t -> acc, t
            | NCic.LetIn (name,ty,bo,t) ->
                let m,_,i,_=
-                 NCicMetaSubst.mk_meta ~name m context (`WithType ty)in
+                 NCicMetaSubst.mk_meta ~attrs:[`Name 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
@@ -299,4 +321,85 @@ let look_for_hint hdb metasenv subst context t1 t2 =
     rc)));
 
   rc
+             end
+;;
+
+let pp_hint t p =
+  let context, t = 
+     let rec aux ctx = function
+       | NCic.Prod (name, ty, rest) -> aux ((name, NCic.Decl ty) :: ctx) rest
+       | t -> ctx, t
+     in
+      aux [] t
+  in
+  let recproblems, concl = 
+    let rec aux ctx = function
+      | NCic.LetIn (name,ty,bo,rest) -> aux ((name, NCic.Def(bo,ty))::ctx) rest
+      | t -> ctx, t
+    in
+      aux [] t
+  in
+  let buff = Buffer.create 100 in
+  let fmt = Format.formatter_of_buffer buff in
+(*
+  F.fprintf "@[<hov>"
+   F.fprintf "@[<hov>"
+(*    pp_ctx [] context *)
+   F.fprintf "@]"
+  F.fprintf "@;"
+   F.fprintf "@[<hov>"
+(*    pp_ctx context recproblems *)
+   F.fprintf "@]"
+  F.fprintf "\vdash@;";
+    NCicPp.ppterm ~fmt ~context:(recproblems@context) ~subst:[] ~metasenv:[];
+  F.fprintf "@]"
+  F.fprintf formatter "@?";
+  prerr_endline (Buffer.contents buff);
+*)
+()
+;;
+
+let generate_dot_file status fmt =
+  let module Pp = GraphvizPp.Dot in
+  let h_db, _ = status#uhint_db in
+  let names = ref [] in
+  let id = ref 0 in
+  let mangle l =
+    try List.assoc l !names
+    with Not_found ->
+      incr id;
+      names := (l,"node"^string_of_int!id) :: !names;
+      List.assoc l !names
+  in
+  let nodes = ref [] in
+  let edges = ref [] in
+  HDB.iter h_db (fun _key dataset -> 
+      List.iter
+        (fun (precedence, l,r, hint) ->
+            let l = 
+              Str.global_substitute (Str.regexp "\n") (fun _ -> "")
+               (NCicPp.ppterm 
+                ~margin:max_int ~metasenv:[] ~context:[] ~subst:[] l) 
+            in
+            let r = 
+              Str.global_substitute (Str.regexp "\n") (fun _ -> "")
+               (NCicPp.ppterm 
+                ~margin:max_int ~metasenv:[] ~context:[] ~subst:[] r)
+            in
+            let shint =  "???" (*
+              string_of_int precedence ^ "..." ^
+              Str.global_substitute (Str.regexp "\n") (fun _ -> "")
+               (NCicPp.ppterm 
+                ~margin:max_int ~metasenv:[] ~context:[] ~subst:[] hint)*)
+            in
+            nodes := (mangle l,l) :: (mangle r,r) :: !nodes;
+            edges := (mangle l, mangle r, shint, precedence, hint) :: !edges)
+        (HintSet.elements dataset);
+    );
+  List.iter (fun x, l -> Pp.node x ~attrs:["label",l] fmt) !nodes;
+  List.iter (fun x, y, l, _, _ -> 
+      Pp.raw (Printf.sprintf "%s -- %s [ label=\"%s\" ];\n" x y "?") fmt)
+  !edges;
+  edges := List.sort (fun (_,_,_,p1,_) (_,_,_,p2,_) -> p1 - p2) !edges;
+  List.iter (fun x, y, _, p, l -> pp_hint l p) !edges;
 ;;