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