(* Copyright (C) 2000, HELM Team. * * This file is part of HELM, an Hypertextual, Electronic * Library of Mathematics, developed at the Computer Science * Department, University of Bologna, Italy. * * HELM is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * HELM is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HELM; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. * * For details, see the HELM World-Wide-Web page, * http://cs.unibo.it/helm/. *) (* * implementazione del comando INTERSECT *) (* * eccezione sollevata quando il join dei contesti * deve essere vuoto *) exception Join_must_be_empty;; (* * join fra due contesti *) let xres_join_context h1 l1 h2 l2 = match (l1, l2) with ([], _) -> l2 | (_, []) -> l1 | (_, _) -> let hh = h2 @ (List.find_all (function t -> not (List.mem t h2)) h1) and m1 = List.combine h1 l1 and m2 = List.combine h2 l2 in try (List.map (fun elem -> let value1 = try (List.assoc elem m1) with Not_found -> List.assoc elem m2 and value2 = try (List.assoc elem m2) with Not_found -> List.assoc elem m1 in if value1 = value2 then value1 else raise Join_must_be_empty ) hh ) with Join_must_be_empty -> [] ;; (* * implementazione del comando INTERSECT *) let intersect_ex alist1 alist2 = let head1 = List.hd alist1 and tail1 = List.tl alist1 and head2 = List.hd alist2 and tail2 = List.tl alist2 (* e fin qui ... *) in match (head1, head2) with ([], _) -> assert false (* gli header non devono mai essere vuoti *) | (_, []) -> assert false (* devono contenere almeno [retVal] *) | (_, _) -> (match (tail1, tail2) with ([], _) -> [["retVal"]] (* se una delle due code e' vuota... *) | (_, []) -> [["retVal"]] (* ... l'intersezione e' vuota *) | (_, _) -> [head2 @ (List.find_all (function t -> not (List.mem t head2)) head1 ) ] (* header del risultato finale *) @ List.fold_left (fun par1 elem1 -> par1 @ List.map (fun elem2 -> [(List.hd elem1)] @ (xres_join_context (List.tl head1) (List.tl elem1) (List.tl head2) (List.tl elem2)) ) (List.find_all (fun elem2 -> (* trova tutti gli elementi della lista tail2 *) ((List.hd elem1) = (List.hd elem2)) && (* che stanno in tail1 *) not ((xres_join_context (List.tl head1) (List.tl elem1) (List.tl head2) (List.tl elem2)) = []) (* e per i quali la xres_join_context non sia vuota *) ) tail2 ) ) [] tail1 (* per ogni elemento di tail1 applica la List.fold_left *) ) (* match *) ;;