]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_interpreter/mqint.ml
Merge of the new_mathql branch with the main branch:
[helm.git] / helm / ocaml / mathql_interpreter / mqint.ml
1 (* Copyright (C) 2000, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26
27
28
29 (*
30  * implementazione del'interprete MathQL
31  *)
32
33
34
35
36 open Dbconn;;
37 open Union;;
38 open Intersect;;
39 open Meet;;
40 open Sub;;
41 open Context;;
42 open Diff;;
43 open Relation;;
44 open Func;;
45
46 let init connection_param = Dbconn.init connection_param 
47
48 let close () = Dbconn.close ()
49
50 let check () = Dbconn.pgc ()
51
52 exception BooleExpTrue
53
54 let stat = ref false
55
56 let set_stat b = stat := b
57
58 let get_stat () = ! stat
59
60 (* valuta una MathQL.set_exp e ritorna un MathQL.resource_set *)
61
62 let rec exec_set_exp c = function
63      MathQL.SVar svar -> List.assoc svar c.svars
64    | MathQL.RVar rvar -> [List.assoc rvar c.rvars]  
65    | MathQL.Ref vexp -> List.map (fun s -> (s,[])) (exec_val_exp c vexp)
66    | MathQL.Intersect (sexp1, sexp2) ->    
67         let before = Sys.time() in
68         let rs1 = exec_set_exp c sexp1 in
69         let rs2 = exec_set_exp c sexp2 in
70         let res = intersect_ex rs1 rs2 in
71         let after = Sys.time() in
72         let ll1 = string_of_int (List.length rs1) in
73         let ll2 = string_of_int (List.length rs2) in
74         let diff = string_of_float (after -. before) in
75         if !stat then
76         (print_endline("INTERSECT(" ^ ll1 ^ "," ^ ll2 ^ ") = " ^ string_of_int (List.length res) ^
77          ": " ^ diff ^ "s");
78          flush stdout);
79         res
80    | MathQL.Union (sexp1, sexp2) -> 
81         let before = Sys.time () in
82         let res = union_ex (exec_set_exp c sexp1) (exec_set_exp c sexp2) in
83         let after = Sys.time() in
84         let diff = string_of_float (after -. before) in
85         if !stat then
86         (print_endline ("UNION: " ^ diff ^ "s");
87          flush stdout);
88         res                     
89    | MathQL.LetSVar (svar, sexp1, sexp2) ->
90         let before = Sys.time() in
91         let c1 = upd_svars c ((svar, exec_set_exp c sexp1) :: c.svars) in 
92         let res = exec_set_exp c1 sexp2 in
93         if !stat then
94         (print_string ("LETIN " ^ svar ^ " = " ^ string_of_int (List.length res) ^ ": ");
95          print_endline (string_of_float (Sys.time() -. before) ^ "s");
96          flush stdout); 
97         res                     
98    | MathQL.LetVVar (vvar, vexp, sexp) ->
99         let before = Sys.time() in
100         let c1 = upd_vvars c ((vvar, exec_val_exp c vexp) :: c.vvars) in
101         let res = exec_set_exp c1 sexp in
102         if !stat then
103         (print_string ("LETIN " ^ vvar ^ " = " ^ string_of_int (List.length res) ^ ": ");
104          print_endline (string_of_float (Sys.time() -. before) ^ "s");
105          flush stdout); 
106         res
107    | MathQL.Relation (rop, path, sexp, attl) -> 
108         let before = Sys.time() in
109         let res = relation_ex rop path (exec_set_exp c sexp) attl in
110         if !stat then 
111         (print_string ("RELATION " ^ (List.hd path) ^ " = " ^ string_of_int(List.length res) ^ ": ");
112          print_endline (string_of_float (Sys.time() -. before) ^ "s");
113          flush stdout);
114         res   
115    | MathQL.Select (rvar, sexp, bexp) ->
116         let before = Sys.time() in
117         let rset = (exec_set_exp c sexp) in
118         let rec select_ex rset =
119         match rset with 
120           [] -> []
121         | r::tl -> let c1 = upd_rvars c ((rvar,r)::c.rvars) in                      
122                    if (exec_boole_exp c1 bexp) then r::(select_ex tl)
123                    else select_ex tl
124         in 
125         let res = select_ex rset in
126         if !stat then
127         (print_string ("SELECT " ^ rvar ^ " = " ^ string_of_int (List.length res) ^ ": ");
128          print_endline (string_of_float (Sys.time() -. before) ^ "s");
129          flush stdout); 
130         res
131    | MathQL.Diff (sexp1, sexp2) -> diff_ex (exec_set_exp c sexp1) (exec_set_exp c sexp2)
132    | _ -> assert false
133    
134 (* valuta una MathQL.boole_exp e ritorna un boole *)
135
136 and exec_boole_exp c = function
137      MathQL.False      -> false
138    | MathQL.True       -> true
139    | MathQL.Not x      -> not (exec_boole_exp c x)
140    | MathQL.And (x, y) -> (exec_boole_exp c x) && (exec_boole_exp c y)
141    | MathQL.Or (x, y)  -> (exec_boole_exp c x) || (exec_boole_exp c y)
142    | MathQL.Sub (vexp1, vexp2) -> sub_ex (exec_val_exp c vexp1) (exec_val_exp c vexp2)
143    | MathQL.Meet (vexp1, vexp2) -> meet_ex (exec_val_exp c vexp1) (exec_val_exp c vexp2)
144    | MathQL.Eq (vexp1, vexp2) -> (exec_val_exp c vexp1) = (exec_val_exp c vexp2)
145    | MathQL.Ex l bexp -> 
146         if l = [] then (exec_boole_exp c bexp)
147         else
148          let latt = List.map (fun uri -> 
149                                 let (r,attl) = List.assoc uri c.rvars in (uri,attl)) l (*latt = l + attributi*)
150          in
151          try
152          let rec prod c = function
153               [] -> if (exec_boole_exp c bexp) then raise BooleExpTrue 
154             | (uri,attl)::tail1 -> let rec sub_prod attl =
155                                       match attl with
156 (*per ogni el. di attl  *)              [] -> () 
157 (*devo andare in ric. su tail1*)      | att::tail2 -> let c1 = upd_groups c ((uri,att)::c.groups) in             
158                                                        prod c1 tail1; sub_prod tail2 
159                                      in       
160                                       sub_prod attl 
161          in
162          prod c latt; false
163          with BooleExpTrue -> true
164
165 (* valuta una MathQL.val_exp e ritorna un MathQL.value *)
166
167 and exec_val_exp c = function
168      MathQL.Const x -> let
169         ol = List.sort compare x in 
170                         let rec edup = function
171                         
172                            [] -> [] 
173                          | s::tl -> if tl <> [] then  
174                                                  if s = (List.hd tl) then edup tl
175                                                  else s::(edup tl)
176                                     else s::[]
177                         in
178                          edup ol
179    | MathQL.Record (rvar, vvar) -> List.assoc vvar (List.assoc rvar c.groups) 
180                                   
181    | MathQL.VVar s -> List.assoc s c.vvars                                
182    | MathQL.RefOf sexp -> List.map (fun (s,_) -> s) (exec_set_exp c sexp)
183    | MathQL.Fun (s, vexp) -> fun_ex s (exec_val_exp c vexp)
184    | _ -> assert false
185
186 (* valuta una MathQL.set_exp nel contesto vuoto e ritorna un MathQL.resource_set *)
187 and execute x =
188    exec_set_exp {svars = []; rvars = []; groups = []; vvars = []} x