1 (* Copyright (C) 2000, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
30 * implementazione del'interprete MathQL
46 let init connection_param = Dbconn.init connection_param
48 let close () = Dbconn.close ()
50 let check () = Dbconn.pgc ()
52 exception BooleExpTrue
54 (* valuta una MathQL.set_exp e ritorna un MathQL.resource_set *)
56 let rec exec_set_exp c = function
57 |MathQL.SVar svar -> List.assoc svar c.svars
58 |MathQL.RVar rvar -> [List.assoc rvar c.rvars]
59 | MathQL.Ref vexp -> List.map (fun s -> (s,[])) (exec_val_exp c vexp)
60 | MathQL.Intersect (sexp1, sexp2) -> intersect_ex (exec_set_exp c sexp1) (exec_set_exp c sexp2)
61 | MathQL.Union (sexp1, sexp2) ->
62 let before = Sys.time () in
63 let res = union_ex (exec_set_exp c sexp1) (exec_set_exp c sexp2) in
64 let after = Sys.time () in
65 let diff = string_of_float (after -. before) in
66 print_endline ("UNION: " ^ diff ^ "s") ;
69 | MathQL.LetSVar (svar, sexp1, sexp2) ->
70 let before = Sys.time () in
71 let c1 = upd_svars c ((svar, exec_set_exp c sexp1) :: c.svars) in
72 let res = exec_set_exp c1 sexp2 in
73 print_string ("LETIN " ^ svar ^ " = " ^ string_of_int (List.length res) ^ ": ") ;
74 print_endline (string_of_float (Sys.time () -. before) ^ "s") ;
76 | MathQL.LetVVar (vvar, vexp, sexp) ->
77 let before = Sys.time () in
78 let c1 = upd_vvars c ((vvar, exec_val_exp c vexp) :: c.vvars) in
79 let res = exec_set_exp c1 sexp in
80 print_string ("LETIN " ^ vvar ^ " = " ^ string_of_int (List.length res) ^ ": ") ;
81 print_endline (string_of_float (Sys.time () -. before) ^ "s") ;
83 | MathQL.Relation (rop, path, sexp, attl) -> relation_ex rop path (exec_set_exp c sexp) attl
84 | MathQL.Select (rvar, sexp, bexp) ->
85 let rset = (exec_set_exp c sexp) in
86 let rec select_ex rset =
89 | r::tl -> let c1 = upd_rvars c ((rvar,r)::c.rvars) in
90 if (exec_boole_exp c1 bexp) then r::(select_ex tl)
93 | MathQL.Diff (sexp1, sexp2) -> diff_ex (exec_set_exp c sexp1) (exec_set_exp c sexp2)
96 (* valuta una MathQL.boole_exp e ritorna un boole *)
98 and exec_boole_exp c = function
99 | MathQL.False -> false
100 | MathQL.True -> true
101 | MathQL.Not x -> not (exec_boole_exp c x)
102 | MathQL.And (x, y) -> (exec_boole_exp c x) && (exec_boole_exp c y)
103 | MathQL.Or (x, y) -> (exec_boole_exp c x) || (exec_boole_exp c y)
104 | MathQL.Sub (vexp1, vexp2) -> sub_ex (exec_val_exp c vexp1) (exec_val_exp c vexp2)
105 | MathQL.Meet (vexp1, vexp2) -> meet_ex (exec_val_exp c vexp1) (exec_val_exp c vexp2)
106 | MathQL.Eq (vexp1, vexp2) -> (exec_val_exp c vexp1) = (exec_val_exp c vexp2)
107 | MathQL.Ex l bexp ->
108 if l = [] then (exec_boole_exp c bexp)
110 let latt = List.map (fun uri ->
111 let (r,attl) = List.assoc uri c.rvars in (uri,attl)) l (*latt = l + attributi*)
114 let rec prod c = function
115 [] -> if (exec_boole_exp c bexp) then raise BooleExpTrue
116 | (uri,attl)::tail1 -> let rec sub_prod attl =
118 (*per ogni el. di attl *) [] -> ()
119 (*devo andare in ric. su tail1*) | att::tail2 -> let c1 = upd_groups c ((uri,att)::c.groups) in
120 prod c1 tail1; sub_prod tail2
125 with BooleExpTrue -> true
128 (* valuta una MathQL.val_exp e ritorna un MathQL.value *)
130 and exec_val_exp c = function
131 | MathQL.Const x -> let
132 ol = List.sort compare x in
133 let rec edup = function
136 | s::tl -> if tl <> [] then
137 if s = (List.hd tl) then edup tl
142 | MathQL.Record (rvar, vvar) -> List.assoc vvar (List.assoc rvar c.groups)
144 | MathQL.VVar s -> List.assoc s c.vvars
145 | MathQL.RefOf sexp -> List.map (fun (s,_) -> s) (exec_set_exp c sexp)
150 (* valuta una MathQL.set_exp nel contesto vuoto e ritorna un MathQL.resource_set *)
153 exec_set_exp {svars = []; rvars = []; groups = []; vvars = []} x