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 (* Query issuing functions **************************************************)
29 type position = string
30 type depth = int option
33 type r_obj = (uri * position * depth)
34 type r_rel = (position* depth)
35 type r_sort = (position* depth * sort)
37 type must_restrictions = (r_obj list * r_rel list * r_sort list)
38 type only_restrictions =
39 (r_obj list option * r_rel list option * r_sort list option)
40 type universe = position list option
45 | "MH" -> ns ^ "MainHypothesis"
46 | "IH" -> ns ^ "InHypothesis"
47 | "MC" -> ns ^ "MainConclusion"
48 | "IC" -> ns ^ "InConclusion"
49 | "IB" -> ns ^ "InBody"
51 | "PROP" -> ns ^ "Prop"
52 | "TYPE" -> ns ^ "Type"
53 | _ -> raise (Failure "MQueryGenerator.builtin")
55 (* Query building functions ************************************************)
60 M.Ref (M.Property true M.RefineExact ("objectName", []) (M.Const [s]))
62 let query_of_constraints univ must_use can_use =
63 (* FG : The (univ : universe) is not used in this implementation *)
64 let in_path s = (s, []) in
65 let assign v p = (in_path v, in_path p) in
67 (* can restrictions *)
69 let (cr_o,cr_r,cr_s) = can_use in
71 let uri_of_entry (r, p, d) = r in
76 | Some cr_o -> List.map uri_of_entry cr_o
79 let tfst (a,b,c) = a in
80 let tsnd (a,b,c) = b in
81 let trd (a,b,c) = c in
92 | Some cr_o -> (List.map tsnd cr_o) in
97 | Some cr_o -> List.map trd cr_o
98 (* let odep_option_list = List.map trd cr_o in
99 let lo_dep_int = List.fold_left to_int_list [] odep_option_list in
100 List.map string_of_int lo_dep_int*)
102 print_string "#### LUNGHEZZA ODEP: "; print_int (List.length odep); flush stdout;
107 | Some cr_r -> (List.map fst cr_r) in
112 | Some cr_r -> List.map snd cr_r
113 (* let rdep_option_list = List.map snd cr_r in
114 let lr_dep_int = List.fold_left to_int_list [] rdep_option_list in
115 List.map string_of_int lr_dep_int *)
122 | Some cr_s -> (List.map tfst cr_s) in
128 | Some cr_s -> List.map tsnd cr_s
129 (* let sdep_option_list = List.map tsnd cr_s in
130 let ls_dep_int = List.fold_left to_int_list [] sdep_option_list in
131 List.map string_of_int ls_dep_int*)
138 | Some cr_s -> List.map trd cr_s in
140 (* let q_where_obj = function
147 M.Relation (false, M.RefineExact, in_path "refObj", M.Ref (M.RefOf (M.RVar "uri0")), [assign "pos" "position"]),
149 (M.Meet (M.VVar "obj_positions", M.Record ("uri", in_path "pos"))))),
157 (false, M.RefineExact, in_path "refObj",
158 M.Ref (M.RefOf (M.RVar "uri0")),
159 [assign "p" "position"; assign "d" "depth"]
163 ((M.Meet(M.VVar "obj_positions",M.Record("uri",in_path "p"))),
164 (M.Meet(M.VVar "obj_depths", M.Record("uri",in_path "d")))))
175 let q_where_obj n = function
177 let rec q_ex n = function
180 M.Meet (M.VVar ("obj_position" ^ string_of_int n), M.Record ("uri", in_path "p"))
183 print_string "@@@@@ IN-WHERE-OBJ"; flush stdout;
186 (M.Meet(M.VVar ("obj_position" ^ string_of_int n),M.Record("uri",in_path "p")),
187 M.Meet(M.VVar ("obj_depth" ^ string_of_int n), M.Record("uri",in_path "d")))
190 (M.Meet (M.VVar ("obj_position" ^ string_of_int n), M.Record ("uri", in_path "p")),
193 print_string "@@@@@ IN-WHERE-OBJ"; flush stdout;
197 ((M.Meet(M.VVar ("obj_position" ^ string_of_int n),M.Record("uri",in_path "p"))),
198 (M.Meet(M.VVar ("obj_depth" ^ string_of_int n), M.Record("uri",in_path "d"))))),
206 (false, M.RefineExact, in_path "refObj",
207 M.Ref (M.RefOf (M.RVar "uri0")),
208 [assign "p" "position"; assign "d" "depth"]
219 let rec q_where_rel n cr_r= (*function*)
224 (false, M.RefineExact, ("refRel", ["position"]),
225 M.RefOf(M.RVar "uri0")),
226 M.VVar ("rel_position" ^ string_of_int n))
230 | Some [(p,None)] -> q0
236 (false, M.RefineExact, ("refRel", ["depth"]),
237 M.RefOf(M.RVar "uri0")),
238 M.VVar ("rel_depth" ^ string_of_int n)))
239 | Some ((p,None)::tl) ->
242 q_where_rel (n+1) (Some tl))
243 | Some ((p,d)::tl) ->
249 (false, M.RefineExact, ("refRel", ["depth"]),
250 M.RefOf(M.RVar "uri0")),
251 M.VVar ("rel_depth" ^ string_of_int n))),
252 q_where_rel (n+1) (Some tl))
256 let rec q_where_sort n cr_s = (*function *)
262 (false, M.RefineExact, ("refSort", ["position"]),
263 M.RefOf(M.RVar "uri0")
265 M.VVar ("sort_position" ^ string_of_int n)),
268 (false, M.RefineExact, ("refSort", ["sort"]),
269 M.RefOf(M.RVar "uri0")),
270 M.VVar ("sort" ^ string_of_int n)))
274 | Some [(p,None,s)] -> q0
281 (false, M.RefineExact, ("refSort", ["depth"]),
282 M.RefOf(M.RVar "uri0")),
283 M.VVar ("sort_depth" ^ string_of_int n)))
285 | Some ((p,None,s)::tl) ->
288 q_where_sort (n+1) (Some tl))
290 | Some((p,d,s)::tl) ->
296 (false, M.RefineExact, ("refSort", ["depth"]),
297 M.RefOf(M.RVar "uri0")),
298 M.VVar ("sort_depth" ^ string_of_int n))),
299 q_where_sort (n+1) (Some tl))
307 let (cr_o,cr_r,cr_s) = cr in
308 M.And(M.And(q_where_obj 1 cr_o, (q_where_rel 1 cr_r)), (q_where_sort 1 cr_s))
312 (* must restrictions *)
314 let build_select_obj (r, pos, dep) =
318 M.Relation (false, M.RefineExact, ("backPointer", []),
319 M.Ref (M.Const [r]), [assign "p" "position"]),
321 ((M.Sub (M.Const [pos], M.Record ("uri", in_path "p")))))
322 | Some dep -> let string_dep = string_of_int dep in
325 M.Relation (false, M.RefineExact, ("backPointer", []),
326 M.Ref (M.Const [r]), [assign "p" "position";assign "d" "depth"]),
329 ((M.Sub (M.Const [pos], M.Record ("uri", in_path "p"))),
330 (M.Sub (M.Const [string_dep], M.Record ("uri", in_path "d"))))))
333 let build_select_rel (pos, dep) =
337 M.Relation (true, M.RefineExact, ("refRel", []), M.Ref (M.Const [""]), [assign "p" "position";assign "d" "depth"]),
339 (M.Sub (M.Const [pos], M.Record ("uri", in_path "p"))))
340 | Some dep -> let string_dep = string_of_int dep in
343 M.Relation (true, M.RefineExact, ("refRel", []), M.Ref (M.Const [""]), [assign "p" "position";assign "d" "depth"]),
346 ((M.Sub (M.Const [pos], M.Record ("uri", in_path "p"))),
347 (M.Sub (M.Const [string_dep], M.Record ("uri", in_path "d"))))))
350 let build_select_sort (pos, dep, sor) =
354 M.Relation (true, M.RefineExact, ("refSort", []), M.Ref (M.Const [""]), [assign "p" "position";assign "d" "depth";assign "s" "sort"]),
357 ((M.Sub (M.Const [pos], M.Record ("uri", in_path "p"))),
358 (M.Sub (M.Const [sor], M.Record ("uri", in_path "s"))))))
360 | Some dep -> let string_dep = string_of_int dep in
363 M.Relation (true, M.RefineExact, ("refSort", []), M.Ref (M.Const [""]), [assign "p" "position";assign "d" "depth";assign "s" "sort"]),
367 ((M.Sub (M.Const [pos], M.Record ("uri", in_path "p"))),
368 (M.Sub (M.Const [string_dep], M.Record ("uri", in_path "d"))))),
369 (M.Sub (M.Const [sor], M.Record ("uri", in_path "s"))))))
372 let rec build_intersect_obj = function
373 [] -> M.Pattern (M.Const ["[.]*"])
374 | [hd] -> build_select_obj hd
375 | hd :: tl -> M.Intersect (build_select_obj hd, build_intersect_obj tl)
378 let rec build_intersect_rel = function
379 [] -> M.Ref(M.Const [])
380 | [hd] -> build_select_rel hd
381 | hd :: tl -> M.Intersect (build_select_rel hd, build_intersect_rel tl)
384 let rec build_intersect_sort = function
385 [] -> M.Ref(M.Const [])
386 | [hd] -> build_select_sort hd
387 | hd :: tl -> M.Intersect (build_select_sort hd, build_intersect_sort tl)
390 let build_intersect = function
391 (* let tostring_sort (a,b,c) =
392 let b1 = string_of_int b in
395 let tostring_rel (a,b) =
396 let b1 = string_of_int b in
400 (* let (l1,l2,l3) = must in
401 match (l1,l2,l3) with *)
402 l1,[],[] -> build_intersect_obj l1
403 | [],l2,[] -> (*let lrel = List.map tostring_rel l2 in*)
404 build_intersect_rel l2
405 | [],[],l3 ->(* let lsort = List.map tostring_sort l3 in*)
406 build_intersect_sort l3
407 | l1,l2,[] -> (*let lrel = List.map tostring_rel l2 in*)
408 M.Intersect (build_intersect_obj l1, build_intersect_rel l2)
409 | l1,[],l3 ->(* let lsort = List.map tostring_sort l3 in *)
410 M.Intersect (build_intersect_obj l1, build_intersect_sort l3)
411 | [],l2,l3 ->(* let lrel = List.map tostring_rel l2 in
412 let lsort = List.map tostring_sort l3 in*)
413 M.Intersect (build_intersect_rel l2, build_intersect_sort l3)
414 | l1,l2,l3 ->(* let lrel = List.map tostring_rel l2 in
415 let lsort = List.map tostring_sort l3 in *)
416 M.Intersect (M.Intersect (build_intersect_obj l1, build_intersect_rel l2), build_intersect_sort l3)
419 let q_in = build_intersect must_use in
420 let q_select = M.Select ("uri0", q_in, q_where can_use) in
422 (* variables for can restrictions *)
424 let q_let_u = M.LetVVar ("universe", M.Const universe, q_select) in
426 let rec q_let_s sor n =
429 | [s] -> M.LetVVar ("sort" ^ (string_of_int n), M.Const [s], q_let_u)
430 | s::tl -> M.LetVVar ("sort" ^ (string_of_int n), M.Const [s], q_let_s tl (n+1))
433 (* let q_let_s = M.LetVVar ("sorts", M.Const sor, q_let_u) in *)
435 let rec q_let_ds sdep n =
438 | [None] -> q_let_s sor 1
439 | (None)::tl -> q_let_ds tl (n+1)
440 | [Some d] -> M.LetVVar ("sort_depth" ^ (string_of_int n), M.Const [(string_of_int d)], q_let_s sor 1)
441 | (Some d)::tl -> M.LetVVar ("sort_depth" ^ (string_of_int n), M.Const [(string_of_int d)], q_let_ds tl (n+1))
444 (* let q_let_ds = M.LetVVar ("sort_depths", M.Const sdep, q_let_s) in *)
446 let rec q_let_dr rdep n =
449 | [None] -> q_let_ds sdep 1
450 | (None)::tl -> q_let_dr tl (n+1)
451 | [Some d] -> M.LetVVar ("rel_depth" ^ (string_of_int n), M.Const [(string_of_int d)], q_let_ds sdep 1)
452 | (Some d)::tl -> M.LetVVar ("rel_depth" ^ (string_of_int n), M.Const [(string_of_int d)], q_let_dr tl (n+1))
456 (*let q_let_dr = M.LetVVar ("rel_depths", M.Const rdep, q_let_ds) in*)
458 let rec q_let_do odep n =
461 | [None] -> q_let_dr rdep 1
462 | (None)::tl -> q_let_do tl (n+1)
463 | [Some d] -> M.LetVVar ("obj_depth" ^ (string_of_int n), M.Const [(string_of_int d)], q_let_dr rdep 1)
464 | (Some d)::tl -> M.LetVVar ("obj_depth" ^ (string_of_int n), M.Const [(string_of_int d)], q_let_do tl (n+1))
468 (* let q_let_do = M.LetVVar ("obj_depths", M.Const odep, q_let_dr) in *)
471 let rec q_let_ps spos n =
473 [] -> q_let_do odep 1
474 | [p] -> M.LetVVar ("sort_position" ^ (string_of_int n), M.Const [p], q_let_do odep 1)
475 | p::tl -> M.LetVVar ("sort_position" ^ (string_of_int n), M.Const [p], q_let_ps tl (n+1))
479 (* let q_let_ps = M.LetVVar ("sort_positions", M.Const spos, q_let_do) in *)
482 let rec q_let_pr rpos n =
484 [] -> q_let_ps spos 1
485 | [p] -> M.LetVVar ("rel_position" ^ (string_of_int n), M.Const [p], q_let_ps spos 1)
486 | p::tl -> M.LetVVar ("rel_position" ^ (string_of_int n), M.Const [p], q_let_pr tl (n+1))
491 (* let q_let_pr = M.LetVVar ("rel_positions", M.Const rpos, q_let_ps) in *)
493 let rec q_let_po opos n =
495 [] -> q_let_pr rpos 1
496 | [p] -> M.LetVVar ("obj_position" ^ (string_of_int n), M.Const [p], q_let_pr rpos 1)
497 | p::tl -> M.LetVVar ("obj_position" ^ (string_of_int n), M.Const [p], q_let_po tl (n+1))
500 (*let q_let_po = M.LetVVar ("obj_positions", M.Const opos, q_let_pr) in*)
502 let query = (M.Ref (M.RefOf (q_let_po opos 1))) in
504 print_endline "### "; MQueryUtil.text_of_query print_string query "\n"; flush stdout;