]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_generator/mQueryGenerator.ml
* removed currified constructors everywhere. A bug in the ocaml compiler
[helm.git] / helm / ocaml / mathql_generator / mQueryGenerator.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 module M = MathQL
30 module T = MQGTypes
31 module U = MQGUtil
32
33 (* low level functions  *****************************************************)
34
35 let locate s =
36    let query = 
37       M.Property (true,M.RefineExact,["objectName"],[],[],[],[],false,(M.Const s) )
38    in M.StatQuery query
39
40 let unreferred target_pattern source_pattern =
41    let query = 
42       M.Bin (M.BinFDiff,
43       (
44          M.Property(false,M.RefineExact,[],[],[],[],[],true,(M.Const target_pattern))
45       ), (
46          M.Property(false,M.RefineExact,["refObj"],["h:occurrence"],[],[],[],true,(M.Const source_pattern))
47       
48       ))
49    in M.StatQuery query
50
51 let compose cl = 
52    let letin = ref [] in
53    let must = ref [] in
54    let onlyobj = ref [] in
55    let onlysort = ref [] in
56    let onlyrel = ref [] in
57    let only = ref true in
58    let univ = ref [] in
59    let set_val = function
60       | [s] -> M.Const s
61       | l   -> 
62          let msval = M.Set (List.map (fun s -> M.Const s) l) in
63          if ! only then begin
64            let vvar = "val" ^ string_of_int (List.length ! letin) in
65            letin := (vvar, msval) :: ! letin;
66            M.VVar vvar
67          end else msval
68    in
69    let cons o (r, s, p, d) =      
70       let con p = function
71          | [] -> []
72          | l  -> [(false, [p], set_val l)]
73       in
74       only := o;
75       con "h:occurrence" r @ 
76       con "h:sort" (List.map U.string_of_sort s) @ 
77       con "h:position" (List.map U.string_of_position p) @ 
78       con "h:depth" (List.map U.string_of_depth d)
79    in
80    let property_must n c =
81       M.Property(true,M.RefineExact,[n],[],(cons false c),[],[],false,(M.Const ""))
82    in   
83    let property_only n cl =
84       let cll = List.map (cons true) cl in
85       M.Property(false,M.RefineExact,[n],[],!univ,cll,[],false,(M.Proj(None,(M.AVar "obj"))))
86    in
87    let rec aux = function 
88       | [] -> ()
89       | T.Universe l :: tail    -> 
90          only := true; 
91          let l = List.map U.string_of_position l in
92          univ := [(false, ["h:position"], set_val l)]; aux tail 
93       | T.MustObj(r,p,d) :: tail ->
94          must := property_must "refObj" (r, [], p, d) :: ! must; aux tail  
95       | T.MustSort(s,p,d) :: tail ->
96          must := property_must "refSort" ([], s, p, d) :: ! must; aux tail 
97       | T.MustRel(p,d) :: tail ->
98          must := property_must "refRel" ([], [], p, d) :: ! must; aux tail
99       | T.OnlyObj(r,p,d) :: tail ->
100          onlyobj := (r, [], p, d) :: ! onlyobj; aux tail
101       | T.OnlySort(s,p,d) :: tail ->
102          onlysort := ([], s, p, d) :: ! onlysort; aux tail
103       | T.OnlyRel(p,d) :: tail ->
104          onlyrel := ([], [], p, d) :: ! onlyrel; aux tail
105    in
106    let rec iter f g = function
107       | []           -> raise (Failure "MQueryGenerator.iter")
108       | [head]       -> (f head) 
109       | head :: tail -> let t = (iter f g tail) in g (f head) t
110    in
111    prerr_endline "(** Compose: received constraints **)";
112    U.mathql_of_specs prerr_string cl; flush stderr;
113    aux cl;
114    let must_query =
115       if ! must = [] then  
116          M.Property(false,M.RefineExact,[],[],[],[],[],true,(M.Const ".*"))
117       else 
118          iter (fun x -> x) (fun x y -> M.Bin(M.BinFMeet,x,y)) ! must   
119    in 
120    let onlyobj_val = M.Not (M.Proj(None,(property_only "refObj" ! onlyobj))) in
121    let onlysort_val = M.Not (M.Proj(None,(property_only "refSort" ! onlysort))) in
122    let onlyrel_val = M.Not (M.Proj(None,(property_only "refRel" ! onlyrel))) in
123    let select_query x =
124       match ! onlyobj, ! onlysort, ! onlyrel with
125          | [], [], [] -> x
126          | _, [], []  -> M.Select("obj",x,onlyobj_val)
127          | [], _, []  -> M.Select("obj",x,onlysort_val)
128          | [], [], _  -> M.Select("obj",x,onlyrel_val)
129          | _, _, []   -> M.Select("obj",x,(M.Test(M.And,onlyobj_val,onlysort_val)))
130          | _, [], _   -> M.Select("obj",x,(M.Test(M.And,onlyobj_val,onlyrel_val)))
131          | [], _, _   -> M.Select("obj",x,(M.Test(M.And,onlysort_val,onlyrel_val)))
132          | _, _, _    -> M.Select("obj",x,(M.Test(M.And,(M.Test(M.And,onlyobj_val,onlysort_val)),onlyrel_val)))
133    in    
134    let letin_query = 
135       if ! letin = [] then fun x -> x
136       else 
137         let f (vvar, msval) x = M.LetVVar(vvar,msval,x) in 
138         iter f (fun x y z -> x (y z)) ! letin
139    in 
140    M.StatQuery (letin_query (select_query must_query))
141
142 (* high-level functions  ****************************************************)
143
144 let query_of_constraints u (musts_obj, musts_rel, musts_sort)
145                            (onlys_obj, onlys_rel, onlys_sort) =
146    let conv = function
147       | `MainHypothesis None     -> [T.MainHypothesis], []
148       | `MainHypothesis (Some d) -> [T.MainHypothesis], [d]
149       | `MainConclusion None     -> [T.MainConclusion], []
150       | `MainConclusion (Some d) -> [T.MainConclusion], [d]
151       | `InHypothesis            -> [T.InHypothesis], []
152       | `InConclusion            -> [T.InConclusion], []
153       | `InBody                  -> [T.InBody], []
154    in
155    let must_obj (p, u) = let p, d = conv p in T.MustObj ([u], p, d) in
156    let must_sort (p, s) = let p, d = conv p in T.MustSort ([s], p, d) in
157    let must_rel p = let p, d = conv p in T.MustRel (p, d) in
158    let only_obj (p, u) = let p, d = conv p in T.OnlyObj ([u], p, d) in
159    let only_sort (p, s) = let p, d = conv p in T.OnlySort ([s], p, d) in
160    let only_rel p = let p, d = conv p in T.OnlyRel (p, d) in
161    let must = List.map must_obj musts_obj @
162               List.map must_rel musts_rel @
163               List.map must_sort musts_sort
164    in
165    let only = 
166       (match onlys_obj with 
167          | None    -> []
168          | Some [] -> [T.OnlyObj ([], [], [])]
169          | Some l  -> List.map only_obj l
170       ) @
171       (match onlys_rel with 
172          | None    -> []
173          | Some [] -> [T.OnlyRel ([], [])]
174          | Some l  -> List.map only_rel l
175       ) @
176       (match onlys_sort with 
177          | None    -> []
178          | Some [] -> [T.OnlySort ([], [], [])]
179          | Some l  -> List.map only_sort l
180       )
181    in
182    let univ = match u with None -> [] | Some l -> [T.Universe l] in
183    compose (must @ only @ univ)
184