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>
29 (* boolean constants *******************************************************)
35 (* set theoretic operations *************************************************)
37 let rec set_sub v1 v2 =
41 | h1 :: _, h2 :: _ when h1 < h2 -> mql_false
42 | h1 :: _, h2 :: t2 when h1 > h2 -> set_sub v1 t2
43 | _ :: t1, _ :: t2 -> set_sub t1 t2
45 let rec set_meet v1 v2 =
49 | h1 :: t1, h2 :: _ when h1 < h2 -> set_meet t1 v2
50 | h1 :: _, h2 :: t2 when h1 > h2 -> set_meet v1 t2
54 if v1 = v2 then mql_true else mql_false
56 let rec set_union v1 v2 =
59 | v, [] -> v (* (zz h1 ">" h2); *)
60 | h1 :: t1, h2 :: t2 when h1 < h2 -> h1 :: set_union t1 v2
61 | h1 :: t1, h2 :: t2 when h1 > h2 -> h2 :: set_union v1 t2
62 | h1 :: t1, _ :: t2 -> h1 :: set_union t1 t2
64 let rec iter f = function
66 | head :: tail -> set_union (f head) (iter f tail)
68 (* MathQL specific set operations ******************************************)
70 let rec mql_union s1 s2 =
74 | (r1, g1) :: t1, (r2, _) :: _ when r1 < r2 ->
75 (r1, g1) :: mql_union t1 s2
76 | (r1, _) :: _, (r2, g2) :: t2 when r1 > r2 ->
77 (r2, g2) :: mql_union s1 t2
78 | (r1, g1) :: t1, (_, g2) :: t2 ->
79 (r1, set_union g1 g2) :: mql_union t1 t2
81 let rec mql_iter f = function
83 | head :: tail -> mql_union (f head) (mql_iter f tail)
85 let rec mql_iter2 f l1 l2 = match l1, l2 with
87 | h1 :: t1, h2 :: t2 -> mql_union (f h1 h2) (mql_iter2 f t1 t2)
88 | _ -> raise (Invalid_argument "mql_itwr2")
90 let rec mql_prod g1 g2 =
91 let mql_prod_aux a = iter (fun h -> [mql_union a h]) g2 in
94 let rec mql_intersect s1 s2 =
98 | (r1, _) :: t1, (r2, _) :: _ when r1 < r2 -> mql_intersect t1 s2
99 | (r1, _) :: _, (r2, _) :: t2 when r1 > r2 -> mql_intersect s1 t2
100 | (r1, g1) :: t1, (_, g2) :: t2 ->
101 (r1, mql_prod g1 g2) :: mql_intersect t1 t2
103 let rec mql_diff s1 s2 =
107 | (r1, g1) :: t1 , (r2, _) ::_ when r1 < r2 ->
108 (r1, g1) :: (mql_diff t1 s2)
109 | (r1, _) :: _, (r2, _) :: t2 when r1 > r2 -> mql_diff s1 t2
110 | _ :: t1, _ :: t2 -> mql_diff t1 t2
112 (* logic operations ********************************************************)
115 let b = v1 <> mql_false in
116 if b && v2 <> mql_false then mql_false else
119 (* numeric operations ******************************************************)
121 let int_of_list = function
122 | [s] -> int_of_string s
123 | _ -> raise (Failure "int_of_list")
126 try if int_of_list v1 <= int_of_list v2 then mql_true else mql_false
130 try if int_of_list v1 < int_of_list v2 then mql_true else mql_false
134 let c = String.length v in
136 let l = int_of_list [n] in
137 if c < l then [(String.make (l - c) ' ') ^ v] else [v]
140 (* context handling ********************************************************)
142 let rec set ap = function
144 | head :: tail when fst head = fst ap -> ap :: tail
145 | head :: tail -> head :: set ap tail