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/.
26 (* AUTOR: Ferruccio Guidi <fguidi@cs.unibo.it>
33 (* helpers *****************************************************************)
35 let stat x = M.Fun (["stat"], [], [x])
36 let diff x y = M.Fun (["diff"], [], [x; y])
37 let union xl = M.Fun (["union"], [], xl)
38 let const s = M.Const [(s, [])]
39 let intersect xl = M.Fun (["intersect"], [], xl)
40 let lnot x = M.Fun (["not"], [], [x])
41 let lamp xl = M.Fun (["and"], [], xl)
43 (* low level functions *****************************************************)
47 M.Property true M.RefineExact ["objectName"] [] [] [] []
51 let unreferred target_pattern source_pattern =
55 M.Property false M.RefineExact [] [] [] [] []
56 true (const target_pattern)
58 M.Property false M.RefineExact ["refObj"] ["h:occurrence"] [] [] []
59 true (const source_pattern)
67 let onlyobj = ref [] in
68 let onlysort = ref [] in
69 let onlyrel = ref [] in
70 let only = ref true in
72 let set_val = function
75 let msval = union (List.map (fun s -> const s) l) in
77 let vvar = "val" ^ string_of_int (List.length ! letin) in
78 letin := (vvar, msval) :: ! letin;
82 let cons o (r, s, p, d) =
85 | l -> [(false, [p], set_val l)]
88 con "h:occurrence" r @
89 con "h:sort" (List.map U.string_of_sort s) @
90 con "h:position" (List.map U.string_of_position p) @
91 con "h:depth" (List.map U.string_of_depth d)
93 let property_must n c =
94 M.Property true M.RefineExact [n] []
95 (cons false c) [] [] false (const "")
97 let property_only n cl =
98 let cll = List.map (cons true) cl in
99 M.Property false M.RefineExact [n] []
100 ! univ cll [] false (M.AVar "obj")
102 let rec aux = function
104 | T.Universe l :: tail ->
106 let l = List.map U.string_of_position l in
107 univ := [(false, ["h:position"], set_val l)]; aux tail
108 | T.MustObj r p d :: tail ->
109 must := property_must "refObj" (r, [], p, d) :: ! must; aux tail
110 | T.MustSort s p d :: tail ->
111 must := property_must "refSort" ([], s, p, d) :: ! must; aux tail
112 | T.MustRel p d :: tail ->
113 must := property_must "refRel" ([], [], p, d) :: ! must; aux tail
114 | T.OnlyObj r p d :: tail ->
115 onlyobj := (r, [], p, d) :: ! onlyobj; aux tail
116 | T.OnlySort s p d :: tail ->
117 onlysort := ([], s, p, d) :: ! onlysort; aux tail
118 | T.OnlyRel p d :: tail ->
119 onlyrel := ([], [], p, d) :: ! onlyrel; aux tail
121 let rec iter f g = function
122 | [] -> raise (Failure "MQueryGenerator.iter")
124 | head :: tail -> let t = (iter f g tail) in g (f head) t
126 prerr_endline "(** Compose: received constraints **)";
127 U.mathql_of_specs prerr_string cl; flush stderr;
131 M.Property false M.RefineExact [] [] [] [] [] true (const ".*")
135 let onlyobj_val = lnot (property_only "refObj" ! onlyobj) in
136 let onlysort_val = lnot (property_only "refSort" ! onlysort) in
137 let onlyrel_val = lnot (property_only "refRel" ! onlyrel) in
139 match ! onlyobj, ! onlysort, ! onlyrel with
141 | _, [], [] -> M.Select ("obj", x, onlyobj_val)
142 | [], _, [] -> M.Select ("obj", x, onlysort_val)
143 | [], [], _ -> M.Select ("obj", x, onlyrel_val)
144 | _, _, [] -> M.Select ("obj", x, lamp [onlyobj_val; onlysort_val])
145 | _, [], _ -> M.Select ("obj", x, lamp [onlyobj_val; onlyrel_val])
146 | [], _, _ -> M.Select ("obj", x, lamp [onlysort_val; onlyrel_val])
147 | _, _, _ -> M.Select ("obj", x, lamp [onlyobj_val; onlysort_val; onlyrel_val])
150 if ! letin = [] then fun x -> x
152 let f (vvar, msval) x = M.Let (Some vvar, msval, x) in
153 iter f (fun x y z -> x (y z)) ! letin
155 stat (letin_query (select_query must_query))
157 (* high-level functions ****************************************************)
159 let query_of_constraints u (musts_obj, musts_rel, musts_sort)
160 (onlys_obj, onlys_rel, onlys_sort) =
162 | `MainHypothesis None -> [T.MainHypothesis], []
163 | `MainHypothesis (Some d) -> [T.MainHypothesis], [d]
164 | `MainConclusion None -> [T.MainConclusion], []
165 | `MainConclusion (Some d) -> [T.MainConclusion], [d]
166 | `InHypothesis -> [T.InHypothesis], []
167 | `InConclusion -> [T.InConclusion], []
168 | `InBody -> [T.InBody], []
170 let must_obj (p, u) = let p, d = conv p in T.MustObj ([u], p, d) in
171 let must_sort (p, s) = let p, d = conv p in T.MustSort ([s], p, d) in
172 let must_rel p = let p, d = conv p in T.MustRel (p, d) in
173 let only_obj (p, u) = let p, d = conv p in T.OnlyObj ([u], p, d) in
174 let only_sort (p, s) = let p, d = conv p in T.OnlySort ([s], p, d) in
175 let only_rel p = let p, d = conv p in T.OnlyRel (p, d) in
176 let must = List.map must_obj musts_obj @
177 List.map must_rel musts_rel @
178 List.map must_sort musts_sort
181 (match onlys_obj with
183 | Some [] -> [T.OnlyObj ([], [], [])]
184 | Some l -> List.map only_obj l
186 (match onlys_rel with
188 | Some [] -> [T.OnlyRel ([], [])]
189 | Some l -> List.map only_rel l
191 (match onlys_sort with
193 | Some [] -> [T.OnlySort ([], [], [])]
194 | Some l -> List.map only_sort l
197 let univ = match u with None -> [] | Some l -> [T.Universe l] in
198 compose (must @ only @ univ)