]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql/listAvs.ml
e25fc61c730975236bd11137672a97def63308d5
[helm.git] / helm / ocaml / mathql / listAvs.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://www.cs.unibo.it/helm/.
24  *)
25
26 (*  AUTOR: Ferruccio Guidi <fguidi@cs.unibo.it>
27  *)
28
29 type path            = Avs.path               (* the name of an attribute *)
30
31 type value           = Avs.value              (* the value of an attribute *)
32
33 type attribute       = path * value           (* an attribute *)
34
35 type group           = attribute list         (* a group of attributes *)
36
37 type attribute_set   = group list             (* the attributes of an URI *)
38
39 type av              = string * attribute_set (* an attributed URI *)
40
41 type avs             = av list                (* the query result *)
42
43 type peek_t = Empty
44             | Single of (string * group list)
45             | Many of (string * group list)
46
47
48 (* constructors *************************************************************)
49
50 let grp_empty = []
51
52 let grp_make p v = [(p, [v])]
53
54 let empty = grp_empty
55
56 let make s = function
57   | [] -> [(s, [])]
58   | g  -> [(s, [g])]
59
60 (* projections **************************************************************)
61
62 let subj v = List.rev (List.rev_map (fun x -> (x, [])) v)
63
64 let grp_read g p = subj (List.assoc p g)
65
66 let single = function
67    | [s, _] -> Some s
68    | _      -> None
69
70 (* iterators ****************************************************************)
71
72 let rec iter f a = function
73    | []          -> a
74    | [s, _]      -> f a s false
75    | (s, _) :: v -> iter f (f a s true) v
76
77 let rec x_iter f a = function
78    | []           -> a
79    | [s, gl]      -> f a s gl false
80    | (s, gl) :: v -> x_iter f (f a s gl true) v
81
82 let rec x_grp_iter f a g = x_iter f a g
83
84 (* tests ********************************************************************)
85
86 let rec sub v1 v2 =
87    match (v1, v2) with 
88       | [], _                                    -> true 
89       | _, []                                    -> false
90       | (h1, _) :: _, (h2, _) :: _ when h1 < h2  -> false
91       | (h1, _) :: _, (h2, _) :: t2 when h1 > h2 -> sub v1 t2
92       | _ :: t1, _ :: t2                         -> sub t1 t2
93
94 let rec meet v1 v2 =
95    match v1, v2 with
96       | [], _                                    
97       | _, []                                    -> false
98       | (h1, _) :: t1, (h2, _) :: _ when h1 < h2 -> meet t1 v2
99       | (h1, _) :: _, (h2, _) :: t2 when h1 > h2 -> meet v1 t2
100       | _, _                                     -> true
101
102 let rec eq v1 v2 =
103    match v1, v2 with
104       | [], []                                    -> true 
105       | (h1, _) :: t1, (h2, _) :: t2 when h1 = h2 -> eq t1 t2
106       | _, _                                      -> false
107
108 (* union ********************************************************************)
109
110 let rec set_union v1 v2 =
111    match v1, v2 with
112       | [], v                           -> v
113       | v, []                           -> v 
114       | h1 :: t1, h2 :: t2 when h1 < h2 -> h1 :: set_union t1 v2
115       | h1 :: t1, h2 :: t2 when h1 > h2 -> h2 :: set_union v1 t2
116       | h1 :: t1, _ :: t2               -> h1 :: set_union t1 t2
117
118 let set_iter f al = List.fold_left (fun s a -> set_union (f a) s) [] al
119
120 let grps_make l g = set_union l [g]
121
122 let rec union s1 s2 =
123    match s1, s2 with
124       | [], s                                     -> s
125       | s, []                                     -> s
126       | (r1, g1) :: t1, (r2, _) :: _ when r1 < r2 ->
127          (r1, g1) :: union t1 s2
128       | (r1, _) :: _, (r2, g2) :: t2 when r1 > r2 ->
129          (r2, g2) :: union s1 t2
130       | (r1, g1) :: t1, (_, g2) :: t2             ->
131          (r1, set_union g1 g2) :: union t1 t2
132
133 let grp_union = union
134
135 let prod g1 g2 =
136    let aux a = set_iter (fun h -> [union a h]) g2 in
137    set_iter aux g1      
138
139 let rec d_union s1 s2 =
140    match s1, s2 with
141       | [], s                                     -> s
142       | s, []                                     -> s
143       | (r1, g1) :: t1, (r2, _) :: _ when r1 < r2 ->
144          (r1, g1) :: d_union t1 s2
145       | (r1, _) :: _, (r2, g2) :: t2 when r1 > r2 ->
146          (r2, g2) :: d_union s1 t2
147       | (r1, g1) :: t1, (_, g2) :: t2             ->
148          (r1, prod g1 g2) :: d_union t1 t2
149
150 (* intersect ****************************************************************)
151
152 let rec set_intersect v1 v2 =
153    match v1, v2 with
154       | [], v                          -> []
155       | v, []                          -> []
156       | h1 :: t1, h2 :: _ when h1 < h2 -> set_intersect t1 v2
157       | h1 :: _, h2 :: t2 when h1 > h2 -> set_intersect v1 t2
158       | h1 :: t1, _ :: t2              -> h1 :: set_intersect t1 t2
159
160 let rec intersect s1 s2 =
161    match s1, s2 with
162       | [], s                                    -> []
163       | s, []                                    -> []
164       | (r1, _) :: t1, (r2, _) :: _ when r1 < r2 -> intersect t1 s2
165       | (r1, _) :: _, (r2, _) :: t2 when r1 > r2 -> intersect s1 t2
166       | (r1, g1) :: t1, (_, g2) :: t2            ->
167          (r1, set_intersect g1 g2) :: intersect t1 t2
168
169 (* diff *********************************************************************)
170
171 let rec diff s1 s2 =
172    match s1, s2 with
173       | [], _                                     -> []
174       | s, []                                     -> s
175       | (r1, g1) :: t1 , (r2, _) ::_ when r1 < r2 -> 
176          (r1, g1) :: (diff t1 s2)
177       | (r1, _) :: _, (r2, _) :: t2 when r1 > r2  -> diff s1 t2
178       | _ :: t1, _ :: t2                          -> diff t1 t2
179
180 (* concatenation ************************************************************)
181
182 let append v1 v2 = v1 @ v2
183
184 (* peeking ******************************************************************)
185
186 let peek = function
187    | []           -> Empty
188    | [s, gl]      -> Single (s, gl)
189    | (s, gl) :: _ -> Many (s, gl)