]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/mathql_interpreter/intersect.ml
sortedby implemented and new uri result format
[helm.git] / helm / ocaml / mathql_interpreter / intersect.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 (*
27  * implementazione del comando INTERSECT
28  *)
29
30 (*
31  * eccezione sollevata quando il join dei contesti
32  * deve essere vuoto
33  *)
34 exception Join_must_be_empty;;
35
36 (*
37  * join fra due contesti
38  *)
39 let xres_join_context h1 l1 h2 l2 =
40  match (l1, l2) with
41     ([], _) -> l2
42  |  (_, []) -> l1
43  |  (_,  _) ->
44      let hh = h2 @ (List.find_all (function t -> not (List.mem t h2)) h1)
45      and m1 = List.combine h1 l1
46      and m2 = List.combine h2 l2
47      in
48       try 
49        (List.map
50         (fun elem ->
51          let value1 = try (List.assoc elem m1) with Not_found -> List.assoc elem m2
52          and value2 = try (List.assoc elem m2) with Not_found -> List.assoc elem m1
53          in
54           if value1 = value2 then value1 else raise Join_must_be_empty
55         )
56         hh
57        ) with
58         Join_must_be_empty -> []
59 ;;
60
61 (*
62  * implementazione del comando INTERSECT
63  *)
64 let intersect_ex alist1 alist2 =
65  let head1 = List.hd alist1
66  and tail1 = List.tl alist1
67  and head2 = List.hd alist2
68  and tail2 = List.tl alist2 (* e fin qui ... *)
69  in
70   match (head1, head2) with
71      ([], _) -> assert false (* gli header non devono mai essere vuoti *)
72   |  (_, []) -> assert false (* devono contenere almeno [retVal] *)
73   |  (_,  _) ->
74       (match (tail1, tail2) with
75           ([], _) -> [["retVal"]] (* se una delle due code e' vuota... *)
76        |  (_, []) -> [["retVal"]] (* ... l'intersezione e' vuota *)
77        |  (_,  _) ->
78            [head2 @
79             (List.find_all
80              (function t -> not (List.mem t head2))
81              head1
82             )
83            ] (* header del risultato finale *)
84            @
85            List.fold_left
86             (fun par1 elem1 -> par1 @
87              List.map
88               (fun elem2 ->
89                [(List.hd elem1)] @
90                (xres_join_context (List.tl head1) (List.tl elem1)
91                                   (List.tl head2) (List.tl elem2))
92               )
93               (List.find_all
94                (fun elem2 -> (* trova tutti gli elementi della lista tail2 *)
95                 ((List.hd elem1) = (List.hd elem2)) && (* che stanno in tail1 *)
96                 not ((xres_join_context (List.tl head1) (List.tl elem1)
97                                         (List.tl head2) (List.tl elem2)) = [])
98                 (* e per i quali la xres_join_context non sia vuota *)
99                )
100                tail2
101               )
102              )
103            []
104            tail1 (* per ogni elemento di tail1 applica la List.fold_left *)
105       ) (* match *)
106 ;;
107