]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_interpreter/mQueryInterpreter.ml
functor added
[helm.git] / helm / ocaml / mathql_interpreter / mQueryInterpreter.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 (*  AUTOR: Ferruccio Guidi <fguidi@cs.unibo.it>
27  *)
28
29 exception Found
30
31 module U = AvsUtil
32 module M = MathQL
33 module I = M.I
34 module P = MQueryUtil 
35 module C = MQIConn
36 module L = MQILib
37 module F = MQueryIO
38
39 (* contexts *****************************************************************)
40
41 type svar_context = (M.svar * M.result) list
42
43 type avar_context = (M.avar * (string * M.group list)) list
44
45 type group_context = (M.avar * M.group) list
46
47 type context = {svars: svar_context;   
48                 avars: avar_context;   
49                 groups: group_context (* auxiliary context *)  
50                }
51                
52 (* execute ******************************************************************)
53
54 let execute h x =
55    let warn q = 
56      if C.set h C.Warn then 
57      begin
58         C.log h "MQIExecute: waring: reference to undefined variables: ";
59         F.text_of_query (C.log h) "\n" q
60      end
61    in
62    let rec eval_query c = function
63       | M.Const r -> 
64          let aux2 s g = I.make s (eval_list c g) in
65          let aux (s, gl) = U.iter (aux2 s) gl in  
66          c, U.iter aux r
67       | M.Dot (i, p) -> 
68          begin
69             try c, I.grp_read (List.assoc i c.groups) p 
70             with Not_found -> warn (M.Dot (i, p)); c, I.empty 
71          end
72       | M.Ex (l, y) -> 
73          let rec ex_aux h = function
74             | []        -> 
75                let d = {c with groups = h} in
76                if snd (eval_query d y) = U.val_false then () else raise Found 
77             | i :: tail -> 
78                begin
79                   try 
80                      let (_, a) = List.assoc i c.avars in 
81                      let rec add_group = function
82                         | []     -> ()
83                         | g :: t -> ex_aux ((i, g) :: h) tail; add_group t 
84                      in
85                      add_group a
86                   with Not_found -> ()
87                end
88          in
89          begin try ex_aux [] l; c, U.val_false with Found -> c, U.val_true end
90       | M.SVar i -> 
91          begin
92             try c, List.assoc i c.svars 
93             with Not_found -> warn (M.SVar i); c, I.empty
94          end  
95       | M.AVar i -> 
96          begin
97             try 
98                let s, gl = List.assoc i c.avars in
99                c, U.make_x s gl  
100             with Not_found -> warn (M.AVar i); c, I.empty
101          end
102       | M.Let (Some i, x1, x2) ->
103          let d, r = eval_query c x1 in
104          let d = {d with svars = P.add_assoc (i, r) d.svars} in
105          eval_query d x2
106       | M.Let (None, x1, x2) ->
107          let d, r = eval_query c x1 in eval_query d x2
108       | M.For (k, i, x1, x2) ->
109          let f = match k with
110             | M.GenFJoin -> I.union
111             | M.GenFMeet -> I.intersect
112          in
113          let for_aux (d, r) s gl _ =
114             let d = {d with avars = P.add_assoc (i, (s, gl)) d.avars} in
115             let d, s = eval_query d x2 in
116             d, f r s
117          in 
118          let d, r = eval_query c x1 in
119          I.x_iter for_aux (d, I.empty) r
120       | M.While (k, x1, x2) ->
121          let f = match k with
122             | M.GenFJoin -> I.union
123             | M.GenFMeet -> I.intersect
124          in
125          let rec while_aux (d, r) = 
126             let d, b = eval_query d x1 in
127             if b = U.val_false then d, r else 
128             let d, s = eval_query d x2 in
129             while_aux (d, f r s)
130          in
131          while_aux (c, U.val_false)
132       | M.Add (b, z, x) ->
133          let f = if b then I.d_union else I.union in
134          let agl = eval_grp c z in       
135          let aux r sj gl _ = 
136             I.append (f (U.make_x sj gl) (U.make_x sj agl)) r
137          in
138          let _, r = eval_query c x in
139          c, I.x_iter aux I.empty r
140       | M.Property (q0, q1, q2, mc, ct, cfl, el, pat, y) ->
141          let _, r = eval_query c y in
142          let subj, mct = 
143             if q0 then [], (pat, q2 @ mc, r) else (q2 @ mc), (pat, [], r)  
144          in
145          let eval_cons (pat, p, y) = 
146             let _, r = eval_query c y in (pat, q2 @ p, r)
147          in
148          let cons_true = mct :: List.map eval_cons ct in
149          let cons_false = List.map (List.map eval_cons) cfl in
150          let eval_exp (p, po) = (q2 @ p, po) in
151          let exp = List.map eval_exp el in
152          let t = P.start_time () in
153          let r = MQIProperty.exec h q1 subj cons_true cons_false exp in 
154          let s = P.stop_time t in
155          if C.set h C.Stat then 
156             C.log h (Printf.sprintf "Property: %s,%i\n" s (U.count r));
157          c, r 
158       | M.Select (i, x, y) ->
159          let aux (d, r) sj gl _ =
160             let d = {d with avars = P.add_assoc (i, (sj, gl)) d.avars} in
161             let d, s = eval_query d y in
162             if s = U.val_false then d, r else d, (I.append (U.make_x sj gl) r)
163          in
164          let d, r = eval_query c x in
165          I.x_iter aux (d, I.empty) r 
166       | M.Fun (p, pl, xl) ->        
167          let e = {L.eval = (fun x -> snd (eval_query c x)); L.conn = h} in
168          c, L.fun_eval e (F.text_out_spec (C.log h) "\n") F.text_in_spec 
169             p pl xl
170       | M.Gen (p, xl) -> 
171          let e = {L.eval = (fun x -> snd (eval_query c x)); L.conn = h} in
172          eval_query c (L.gen_eval e p xl)
173    and eval_list c l =
174       let aux (p, y) = 
175          let _, r = eval_query c y in
176          U.x_grp_make_x p r
177       in
178       U.grp_iter aux l
179    and eval_grp c = function
180       | M.Attr gs ->
181          let attr_auxs s l = I.grps_make s (eval_list c l) in
182          List.fold_left attr_auxs [] gs
183       | M.From i ->
184          try snd (List.assoc i c.avars) 
185          with Not_found -> warn (M.AVar i); []
186    in
187    let c = {svars = []; avars = []; groups = []} in
188    let t = P.start_time () in
189    let _, r = eval_query c x in
190    let s = P.stop_time t in
191    if C.set h C.Stat then 
192       C.log h (Printf.sprintf "MQIExecute: %s,%s\n" s 
193          (C.string_of_flags (C.flags h)));
194    r