X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=helm%2Fsoftware%2Fcomponents%2Fng_refiner%2FnCicUnifHint.ml;h=0f0708d638f58cef8fac17303492c6f7111a68b6;hb=186638106f23401e88e512a4a6dfd07d73d8be04;hp=01f9b594b472b973b75f0d8839da570660d3f492;hpb=e6cfe36583259c1e3f6d91e66e50a647726c1716;p=helm.git diff --git a/helm/software/components/ng_refiner/nCicUnifHint.ml b/helm/software/components/ng_refiner/nCicUnifHint.ml index 01f9b594b..0f0708d63 100644 --- a/helm/software/components/ng_refiner/nCicUnifHint.ml +++ b/helm/software/components/ng_refiner/nCicUnifHint.ml @@ -11,117 +11,397 @@ (* $Id: nCicRefiner.mli 9227 2008-11-21 16:00:06Z tassi $ *) -module COT : Set.OrderedType with type t = NCic.term = +let debug s = prerr_endline (Lazy.force s);; +let debug _ = ();; + +module HOT : Set.OrderedType +with type t = int * NCic.term * NCic.term * NCic.term = + struct + (* precedence, skel1, skel2, term *) + type t = int * NCic.term * NCic.term * NCic.term + let compare = Pervasives.compare + end + +module EOT : Set.OrderedType +with type t = int * NCic.term = struct - type t = NCic.term + type t = int * NCic.term let compare = Pervasives.compare end -module HintSet = Set.Make(COT) +module HintSet = Set.Make(HOT) +module EqSet = Set.Make(EOT) -module DB = +module HDB = Discrimination_tree.Make(NDiscriminationTree.NCicIndexable)(HintSet) -type db = DB.t +module EQDB = + Discrimination_tree.Make(NDiscriminationTree.NCicIndexable)(EqSet) + +type db = + HDB.t * (* hint database: (dummy A B)[?] |-> \forall X.(summy a b)[X] *) + EQDB.t (* eqclass DB: A[?] |-> \forall X.B[X] and viceversa *) + +exception HintNotValid + +let skel_dummy = NCic.Implicit `Type;; + +class type g_status = + object + method uhint_db: db + end + +class status = + object + val db = HDB.empty, EQDB.empty + method uhint_db = db + method set_uhint_db v = {< db = v >} + method set_unifhint_status + : 'status. #g_status as 'status -> 'self + = fun o -> {< db = o#uhint_db >} + end 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 pair' = pair t1 t2 in - let data = +let index_hint hdb context t1 t2 precedence = + assert ( + (match t1 with + | NCic.Meta _ | NCic.Appl (NCic.Meta _ :: _) -> false | _ -> true) + && + (match t2 with + | NCic.Meta _ | NCic.Appl (NCic.Meta _ :: _) -> false | _ -> true) + ); + (* here we do not use skel_dummy since it could cause an assert false in + * the subst function that lives in the kernel *) + let hole = NCic.Meta (-1,(0,NCic.Irl 0)) in + let t1_skeleton = + List.fold_left (fun t _ -> NCicSubstitution.subst hole t) t1 context + in + let t2_skeleton = + List.fold_left (fun t _ -> NCicSubstitution.subst hole t) t2 context + in + let rec cleanup_skeleton () = function + | NCic.Meta _ -> skel_dummy + | t -> NCicUtils.map (fun _ () -> ()) () cleanup_skeleton t + in + let t1_skeleton = cleanup_skeleton () t1_skeleton in + let t2_skeleton = cleanup_skeleton () t2_skeleton in + let src = pair t1_skeleton t2_skeleton in + let ctx2abstractions context t = List.fold_left (fun t (n,e) -> match e with | NCic.Decl ty -> NCic.Prod (n,ty,t) - | _ -> assert false) - pair' context in - let src = - List.fold_left - (fun t (_,e) -> - match e with - | 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 + | NCic.Def (b,ty) -> NCic.LetIn (n,ty,b,t)) + t context + 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 -(* - prerr_endline ("INDEX:" ^ - NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] src ^ " ===> " ^ - NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] tgt ^ " := " ^ - NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] c); -*) - DB.index hdb src data + debug(lazy ("INDEXING: " ^ + NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] src ^ " |==> " ^ + NCicPp.ppterm ~metasenv:[] ~subst:[] ~context:[] + (let _,x,_,_ = data_hint in x))); + + hdb#set_uhint_db ( + 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)) ;; -let empty_db = DB.empty ;; +let add_user_provided_hint db t precedence = + let c, a, b = + let rec aux ctx = function + | NCic.Appl l -> + (match List.rev l with + | b::a::_ -> + if + let ty_a = + NCicTypeChecker.typeof ~metasenv:[] ~subst:[] ctx a + in + let ty_b = + NCicTypeChecker.typeof ~metasenv:[] ~subst:[] ctx b + in + NCicReduction.are_convertible + ~metasenv:[] ~subst:[] ctx ty_a ty_b + && + NCicReduction.are_convertible + ~metasenv:[] ~subst:[] ctx a b + then ctx, a, b + else raise HintNotValid + | _ -> 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 [] t + in + index_hint db c a b precedence +;; +(* let db () = - try let _,_,_,x,_,_ = NCicEnvironment.get_checked_def - (NReference.reference_of_string "cic:/matita/tests/pullback/xxx.def(0)") - in - prerr_endline "iiiiiiiiiiiiiiiiii"; - let rec decontextualize ctx = function - | NCic.Prod (n,s,t) -> decontextualize ((n,NCic.Decl s)::ctx) t - | t -> ctx, t - 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) + 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 + 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 - match scty with - | NCic.Prod (_, src, tgt) -> - let tgt = - NCicSubstitution.subst (NCic.Meta (-1,(0,NCic.Irl 0))) tgt - in -(* - 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)); + 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 _hints = + List.fold_left + (fun acc (_,_,l) -> + acc @ + if List.length l > 1 then + combine mk_hint l + else []) + [] (CoercDb.to_list (CoercDb.dump ())) + in + prerr_endline "MISTERO"; + assert false (* ERA + List.fold_left + (fun db -> function + | (ctx,b1,b2) -> index_hint db ctx b1 b2 0) + !user_db (List.flatten hints) *) - 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 ()) - *) ;; +*) +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 ~attrs:[`Name name] metasenv context + ~with_type:s `IsTerm + 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 eq_class_of hdb t1 = + let eq_class = + if NDiscriminationTree.NCicIndexable.path_string_of t1 = + [Discrimination_tree.Variable] + then + [] (* if the trie is unable to handle the key, we skip the query since + it sould retulr the whole content of the trie *) + else + let candidates = EQDB.retrieve_unifiables (snd hdb#uhint_db) t1 in + let candidates = EqSet.elements candidates in + let candidates = List.sort (fun (x,_) (y,_) -> compare x y) candidates in + List.map snd candidates + in + debug(lazy("eq_class of: " ^ NCicPp.ppterm ~metasenv:[] ~context:[] ~subst:[] + t1 ^ " is\n" ^ String.concat "\n" + (List.map (NCicPp.ppterm ~subst:[] ~metasenv:[] ~context:[]) eq_class))); + eq_class +;; 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 - 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) + debug(lazy ("KEY1: "^NCicPp.ppterm ~metasenv ~subst ~context t1)); + debug(lazy ("KEY2: "^NCicPp.ppterm ~metasenv ~subst ~context t2)); +(* + HDB.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 = 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) -> + prec,true,saturate ~delta:max_int metasenv subst context ty 0) + (HintSet.elements candidates1) + in + let candidates2 = + 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 ~attrs:[`Name name] m context + ~with_type:ty `IsTerm + 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 + | (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) + rc in - let _ = prerr_endline ("DENTRO RICERCA: " ^ (string_of_int (List.length res))) in - res + 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 + + debug(lazy ("Hints:"^ + String.concat "\n" (List.map + (fun (metasenv, (t1, t2), premises) -> + ("\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 + 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 "@[" + F.fprintf "@[" +(* pp_ctx [] context *) + F.fprintf "@]" + F.fprintf "@;" + F.fprintf "@[" +(* 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; ;;