]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_interpreter/mQIUtil.ml
8d5782deaf4c66160f627c938c32f574328f334f
[helm.git] / helm / ocaml / mathql_interpreter / mQIUtil.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 (* boolean constants  *******************************************************)
30
31 let mql_false = []
32
33 let mql_true = ["", []]
34
35 (* set theoretic operations *************************************************)
36
37 let rec set_sub v1 v2 =
38    match (v1, v2) with 
39       | [], _                                    -> mql_true 
40       | _, []                                    -> mql_false
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
44
45 let rec set_meet v1 v2 =
46    match v1, v2 with
47       | [], _                                    
48       | _, []                                    -> mql_false
49       | (h1, _) :: t1, (h2, _) :: _ when h1 < h2 -> set_meet t1 v2
50       | (h1, _) :: _, (h2, _) :: t2 when h1 > h2 -> set_meet v1 t2
51       | _, _                                     -> mql_true
52
53 let rec set_eq v1 v2 =
54    match v1, v2 with
55       | [], []                                    -> mql_true 
56       | (h1, _) :: t1, (h2, _) :: t2 when h1 = h2 -> set_eq t1 t2
57       | _, _                                      -> mql_false
58
59 let rec set_union v1 v2 =
60    match v1, v2 with
61       | [], v                           -> v
62       | v, []                           -> v 
63       | h1 :: t1, h2 :: t2 when h1 < h2 -> h1 :: set_union t1 v2
64       | h1 :: t1, h2 :: t2 when h1 > h2 -> h2 :: set_union v1 t2
65       | h1 :: t1, _ :: t2               -> h1 :: set_union t1 t2
66
67 let rec set_intersect v1 v2 =
68    match v1, v2 with
69       | [], v                          -> []
70       | v, []                          -> []
71       | h1 :: t1, h2 :: _ when h1 < h2 -> set_intersect t1 v2
72       | h1 :: _, h2 :: t2 when h1 > h2 -> set_intersect v1 t2
73       | h1 :: t1, _ :: t2              -> h1 :: set_intersect t1 t2
74
75 let rec iter f = function
76    | []           -> []
77    | head :: tail -> set_union (f head) (iter f tail)  
78
79 (* MathQL specific set operations  ******************************************)
80
81 let mql_subj v = List.map (fun s -> (s, [])) v
82
83 let rec mql_union s1 s2 =
84    match s1, s2 with
85       | [], s                                     -> s
86       | s, []                                     -> s
87       | (r1, g1) :: t1, (r2, _) :: _ when r1 < r2 ->
88          (r1, g1) :: mql_union t1 s2
89       | (r1, _) :: _, (r2, g2) :: t2 when r1 > r2 ->
90          (r2, g2) :: mql_union s1 t2
91       | (r1, g1) :: t1, (_, g2) :: t2             ->
92          (r1, set_union g1 g2) :: mql_union t1 t2
93
94 let rec mql_iter f = function
95    | []           -> []
96    | head :: tail -> mql_union (f head) (mql_iter f tail)  
97
98 let rec mql_iter2 f l1 l2 = match l1, l2 with
99    | [], []             -> []
100    | h1 :: t1, h2 :: t2 -> mql_union (f h1 h2) (mql_iter2 f t1 t2)
101    | _                  -> raise (Invalid_argument "mql_iter2")
102
103 let rec mql_prod g1 g2 =
104    let mql_prod_aux a = iter (fun h -> [mql_union a h]) g2 in
105    iter mql_prod_aux g1      
106
107 let rec mql_intersect s1 s2 =
108    match s1, s2 with
109       | [], s                                    -> []
110       | s, []                                    -> []
111       | (r1, _) :: t1, (r2, _) :: _ when r1 < r2 -> mql_intersect t1 s2
112       | (r1, _) :: _, (r2, _) :: t2 when r1 > r2 -> mql_intersect s1 t2
113       | (r1, g1) :: t1, (_, g2) :: t2            ->
114          (r1, set_intersect g1 g2) :: mql_intersect t1 t2
115
116 let rec mql_diff s1 s2 =
117    match s1, s2 with
118       | [], _                                     -> []
119       | s, []                                     -> s
120       | (r1, g1) :: t1 , (r2, _) ::_ when r1 < r2 -> 
121          (r1, g1) :: (mql_diff t1 s2)
122       | (r1, _) :: _, (r2, _) :: t2 when r1 > r2  -> mql_diff s1 t2
123       | _ :: t1, _ :: t2                          -> mql_diff t1 t2
124
125 (* logic operations  ********************************************************)
126
127 let xor v1 v2 =
128    let b = v1 <> mql_false in 
129    if b && v2 <> mql_false then mql_false else
130    if b then v1 else v2
131
132 (* numeric operations *******************************************************)
133
134 exception NumberError of MathQL.result
135
136 let int_of_set r =
137    try match r with 
138       | [s, _] -> MQueryUtil.int_of_string s
139       | _      -> raise (Failure "int_of_string")
140    with Failure "int_of_string" -> raise (NumberError r)