X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=helm%2Focaml%2Fmathql%2FmQueryUtil.ml;h=22d1f91e5d27bf53d0920840063e62f2408f745e;hb=c172220b965a4d0e95004ae42911a886faac878c;hp=ba2382ef99be82526e2b36dd592bf1bae24735cf;hpb=3d70d837a733b4daed65d7c568f4be08f3c37b54;p=helm.git diff --git a/helm/ocaml/mathql/mQueryUtil.ml b/helm/ocaml/mathql/mQueryUtil.ml index ba2382ef9..22d1f91e5 100644 --- a/helm/ocaml/mathql/mQueryUtil.ml +++ b/helm/ocaml/mathql/mQueryUtil.ml @@ -23,109 +23,57 @@ * http://cs.unibo.it/helm/. *) -(******************************************************************************) -(* *) -(* PROJECT HELM *) -(* *) -(* Ferruccio Guidi *) -(* 30/04/2002 *) -(* *) -(* *) -(******************************************************************************) +(* AUTOR: Ferruccio Guidi + *) +(* time handling ***********************************************************) -(* text linearization and parsing *******************************************) +type time = float * float -let rec txt_list out f s = function - | [] -> () - | [a] -> f a - | a :: tail -> f a; out s; txt_list out f s tail +let start_time () = + (Sys.time (), Unix.time ()) -let txt_str out s = out ("\"" ^ s ^ "\"") - -let txt_path out (p0, p1) = - txt_str out p0; if p1 <> [] then out "/"; txt_list out (txt_str out) "/" p1 - -let txt_svar out sv = out ("%" ^ sv) +let stop_time (s0, u0) = + let s1 = Sys.time () in + let u1 = Unix.time () in + Printf.sprintf "%.2fs,%.2fs" (s1 -. s0) (u1 -. u0) -let txt_rvar out rv = out ("@" ^ rv) +(* operations on lists *****************************************************) -let txt_vvar out vv = out ("$" ^ vv) +type 'a comparison = Lt + | Gt + | Eq of 'a -let text_of_query out x sep = - let module M = MathQL in - let txt_inv i = if i then out "inverse " in - let txt_ref = function - | M.RefineExact -> () - | M.RefineSub -> out "sub " - | M.RefineSuper -> out "super " - in - let txt_refpath i r p = txt_inv i; txt_ref r; txt_path out p; out " " in - let txt_assign (pl, pr) = txt_vvar out (fst pl); out " <- "; txt_path out pr in - let rec txt_val = function - | M.Const [s] -> txt_str out s - | M.Const l -> out "{"; txt_list out (txt_str out) ", " l; out "}" - | M.VVar vv -> txt_vvar out vv - | M.Record (rv, p) -> txt_rvar out rv; out "."; txt_vvar out (fst p) - | M.Fun (s, x) -> out "fun "; txt_str out s; out " "; txt_val x - | M.Property (i, r, p, x) -> out "property "; txt_refpath i r p; txt_val x - | M.RefOf x -> out "refof "; txt_set x - and txt_boole = function - | M.False -> out "false" - | M.True -> out "true" - | M.Ex b x -> out "ex "; txt_boole x -(* | M.Ex b x -> "ex [" ^ txt_list txt_rvar "," b ^ "] " ^ txt_boole x -*) | M.Not x -> out "not "; txt_boole x - | M.And (x, y) -> out "("; txt_boole x; out " and "; txt_boole y; out ")" - | M.Or (x, y) -> out "("; txt_boole x; out " or "; txt_boole y; out ")" - | M.Sub (x, y) -> out "("; txt_val x; out " sub "; txt_val y; out ")" - | M.Meet (x, y) -> out "("; txt_val x; out " meet "; txt_val y; out ")" - | M.Eq (x, y) -> out "("; txt_val x; out " eq "; txt_val y; out ")" - and txt_set = function - | M.SVar sv -> txt_svar out sv - | M.RVar rv -> txt_rvar out rv - | M.Relation (i, r, p, M.Ref x, []) -> out "relation "; txt_refpath i r p; txt_val x - | M.Relation (i, r, p, M.Ref x, l) -> out "relation "; txt_refpath i r p; txt_val x; out " attr "; txt_list out txt_assign ", " l - | M.Union (x, y) -> out "("; txt_set x; out " union "; txt_set y; out ")" - | M.Intersect (x, y) -> out "("; txt_set x; out " intersect "; txt_set y; out ")" - | M.Diff (x, y) -> out "("; txt_set x; out " diff "; txt_set y; out ")" - | M.LetSVar (sv, x, y) -> out "let "; txt_svar out sv; out " be "; txt_set x; out " in "; txt_set y - | M.LetVVar (vv, x, y) -> out "let "; txt_vvar out vv; out " be "; txt_val x; out " in "; txt_set y - | M.Select (rv, x, y) -> out "select "; txt_rvar out rv; out " in "; txt_set x; out " where "; txt_boole y - | M.Pattern x -> out "pattern "; txt_val x - | M.Ref x -> out "ref "; txt_val x - | _ -> assert false - in - txt_set x; out sep +let list_join f l1 l2 = + let rec aux = function + | [], v + | v, [] -> v + | ((h1 :: t1) as v1), ((h2 :: t2) as v2) -> begin + match f h1 h2 with + | Lt -> h1 :: aux (t1, v2) + | Gt -> h2 :: aux (v1, t2) + | Eq h -> h :: aux (t1, t2) + end + in aux (l1, l2) -let text_of_result out x sep = - let txt_attr = function - | (p, []) -> txt_vvar out (fst p) - | (p, l) -> txt_vvar out (fst p); out " = "; txt_list out (txt_str out) ", " l - in - let txt_group l = out "{"; txt_list out txt_attr "; " l; out "}" in - let txt_res = function - | (s, []) -> txt_str out s - | (s, l) -> txt_str out s; out " attr "; txt_list out txt_group ", " l - in - let txt_set l = txt_list out txt_res ("; " ^ sep) l in - txt_set x; out sep +let list_meet f l1 l2 = + let rec aux = function + | [], v + | v, [] -> [] + | ((h1 :: t1) as v1), ((h2 :: t2) as v2) -> begin + match f h1 h2 with + | Lt -> aux (t1, v2) + | Gt -> aux (v1, t2) + | Eq h -> h :: aux (t1, t2) + end + in aux (l1, l2) -let query_of_text lexbuf = - MQueryTParser.query MQueryTLexer.query_token lexbuf - -let result_of_text lexbuf = - MQueryTParser.result MQueryTLexer.result_token lexbuf - -(* conversion functions *****************************************************) - -type uriref = UriManager.uri * (int list) +let rec flat_list out f s = function + | [] -> () + | [a] -> f a + | a :: tail -> f a; out s; flat_list out f s tail -let string_of_uriref (uri, fi) = - let module UM = UriManager in - let str = UM.string_of_uri uri in - let xp t = "#xpointer(1/" ^ string_of_int (t + 1) in - match fi with - | [] -> str - | [t] -> str ^ xp t ^ ")" - | t :: c :: _ -> str ^ xp t ^ "/" ^ string_of_int c ^ ")" +let rec add_assoc ap = function + | [] -> [ap] + | head :: tail when fst head = fst ap -> ap :: tail + | head :: tail -> head :: add_assoc ap tail