]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/components/ng_refiner/nCicUnifHint.ml
minor changes to make the library compile after wilmers new exists.
[helm.git] / helm / software / components / ng_refiner / nCicUnifHint.ml
index 01f9b594b472b973b75f0d8839da570660d3f492..918ada5991202d0c82cb700a6e6f9a54c143b50d 100644 (file)
@@ -28,6 +28,13 @@ 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 =
+  assert (
+    (match t1 with
+    | NCic.Meta _ | NCic.Appl (NCic.Meta _ :: _) -> false | _ -> true)
+    &&
+    (match t2 with
+    | NCic.Meta _ | NCic.Appl (NCic.Meta _ :: _) -> false | _ -> true)
+  );
   let pair' = pair t1 t2 in
   let data = 
     List.fold_left 
@@ -40,88 +47,118 @@ let index_hint hdb context t1 t2 =
     List.fold_left 
      (fun t (_,e) -> 
         match e with
-        | NCic.Decl _ -> NCicSubstitution.subst (NCic.Meta (-1,(0,NCic.Irl 0))) t
+        | NCic.Decl _ -> 
+            NCicSubstitution.subst (NCic.Meta (-1,(0,NCic.Irl 0))) t
         | _ -> assert false) 
      pair' context in
-  let _ = prerr_endline ("SONO PASSATO DI QUI") in
-
-(*
-  prerr_endline ("INDEX:" ^ 
-    NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] src ^ " ===> " ^
-    NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] tgt ^ "  :=  " ^
-    NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] c);
-*)
+  prerr_endline ("INDEXING: " ^ 
+    NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] src ^ " |==> " ^
+    NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] data);
   DB.index hdb src data
 ;;
 
 let empty_db = DB.empty ;;
 
-let db () = 
-  try let _,_,_,x,_,_ = NCicEnvironment.get_checked_def
-    (NReference.reference_of_string "cic:/matita/tests/pullback/xxx.def(0)")
+let user_db = ref empty_db ;;
+
+let add_user_provided_hint t =
+  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
+      | _ -> assert false
+    in
+      aux [] (fst (OCic2NCic.convert_term u t))
   in
-    prerr_endline "iiiiiiiiiiiiiiiiii";
-  let rec decontextualize ctx = function
-    | NCic.Prod (n,s,t) -> decontextualize ((n,NCic.Decl s)::ctx) t
-    | t -> ctx, t
+  user_db := index_hint !user_db c a b 
+;;
+
+let db () = 
+  let combine f cmp l1 l2 =
+   List.flatten
+     (List.map
+       (fun u1 -> 
+          HExtlib.filter_map 
+            (fun u2 -> if cmp u1 u2 then None else Some (f u1 u2)) l2)
+       l1)
   in
-    match (decontextualize [] x) with
-    | ctx, NCic.Appl [_;_;a;b] -> index_hint empty_db ctx a b
-    | _ -> assert false
-  with exn -> prerr_endline ("PIPPO" ^ Printexc.to_string exn); empty_db
-  
-(*  List.fold_left 
-    (fun db (_,tgt,clist) -> 
-       List.fold_left 
-         (fun db (uri,_,arg) ->
-            let c=fst (OCic2NCic.convert_term uri (CicUtil.term_of_uri uri)) in
-            let arity = match tgt with | CoercDb.Fun i -> i | _ -> 0 in
-            let src, tgt = 
-              let cty = NCicTypeChecker.typeof ~subst:[] ~metasenv:[] [] c in
-              let scty, metasenv,_ = 
-                NCicMetaSubst.saturate ~delta:max_int [] [] [] cty (arity+1) 
-              in
-              match scty with
-              | NCic.Prod (_, src, tgt) -> 
-                 let tgt =
-                   NCicSubstitution.subst (NCic.Meta (-1,(0,NCic.Irl 0))) tgt
-                 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
+    | (_,_,_,_,NCic.Constant (_,_,Some l,_,_)), 
+      (_,_,_,_,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 None
+          | b1,b2 -> 
+              if NCicReduction.are_convertible ~subst:[] ~metasenv:[] ctx b1 b2 
+              then begin
 (*
-            prerr_endline (Printf.sprintf "indicizzo %s (%d) : %s ===> %s" 
-              (NCicPp.ppterm ~metasenv ~subst:[] ~context:[] scty) (arity+1)
-              (NCicPp.ppterm ~metasenv ~subst:[] ~context:[] src)
-              (NCicPp.ppterm ~metasenv ~subst:[] ~context:[] tgt));
+                prerr_endline ("hint: " ^ NCicPp.ppterm ~metasenv:[] ~subst:[]
+                  ~context:ctx b1 ^ " === " ^ NCicPp.ppterm ~metasenv:[]
+                  ~subst:[] ~context:ctx b2);
 *)
-                src, tgt
-              | t -> 
-                  prerr_endline (
-                    NCicPp.ppterm ~metasenv ~subst:[] ~context:[] t);
-                  assert false
-            in
-            index_coercion db c src tgt arity arg)
-         db clist)
-    (DB.empty,DB.empty) (CoercDb.to_list ())
- *)
+                Some (ctx,b1,b2)
+              end else None
+        in
+          aux [] l r
+    | _ -> None
+  in
+  let hints = 
+    List.fold_left 
+      (fun acc (_,_,l) -> 
+          acc @ 
+          if List.length l > 1 then 
+           combine mk_hint (fun (u1,_,_) (u2,_,_) -> UriManager.eq u1 u2) l l
+          else [])
+      [] (CoercDb.to_list ())
+  in
+  List.fold_left 
+    (fun db -> function 
+     | None -> db 
+     | Some (ctx,b1,b2) -> index_hint db ctx b1 b2)
+    !user_db hints
 ;;
 
 
 let look_for_hint hdb metasenv subst context t1 t2 =
-
-  let candidates = DB.retrieve_unifiables hdb (pair t1 t2) in
-  let res = List.map
-      (fun ty ->
-          let ty, metasenv, _ = 
-           NCicMetaSubst.saturate ~delta:max_int metasenv subst context ty 0 
-          in
-          match ty with
-          | NCic.Appl [_; t1; t2] -> 
-          prerr_endline ("candidate1: " ^ NCicPp.ppterm ~metasenv ~subst ~context t1);
-          prerr_endline ("candidate2: " ^ NCicPp.ppterm ~metasenv ~subst ~context t2);
-              
-              metasenv, t1, t2
-          | _ -> assert false)
-      (HintSet.elements candidates)
+(*
+  prerr_endline ("KEY1: "^NCicPp.ppterm ~metasenv ~subst ~context (pair t1 t2));
+  prerr_endline ("KEY2: "^NCicPp.ppterm ~metasenv ~subst ~context (pair t2 t1));
+  DB.iter hdb
+   (fun p ds ->
+      prerr_endline ("ENTRY: " ^
+      NDiscriminationTree.NCicIndexable.string_of_path p ^ " |--> " ^
+      String.concat "|" (List.map (NCicPp.ppterm ~metasenv:[] ~subst:[]
+      ~context:[]) (HintSet.elements ds))));
+*)
+  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) 
+    (HintSet.elements candidates1) 
+  in
+  let candidates2 = 
+    List.map (fun ty -> 
+       false,NCicMetaSubst.saturate ~delta:max_int metasenv subst context ty 0) 
+    (HintSet.elements candidates2) 
   in
-  let _ = prerr_endline ("DENTRO RICERCA: " ^ (string_of_int (List.length res))) in
-  res
+  List.map
+   (function 
+    | (true,(NCic.Appl [_; t1; t2],metasenv,_)) -> metasenv, t1, t2
+    | (false,(NCic.Appl [_; t1; t2],metasenv,_)) -> metasenv, t2, t1
+    | _ -> assert false)
+   (candidates1 @ candidates2)
 ;;