1/* $Id$ 2 3 Part of CLP(Q) (Constraint Logic Programming over Rationals) 4 5 Author: Leslie De Koninck 6 E-mail: Leslie.DeKoninck@cs.kuleuven.be 7 WWW: http://www.swi-prolog.org 8 http://www.ai.univie.ac.at/cgi-bin/tr-online?number+95-09 9 Copyright (C): 2006, K.U. Leuven and 10 1992-1995, Austrian Research Institute for 11 Artificial Intelligence (OFAI), 12 Vienna, Austria 13 14 This software is based on CLP(Q,R) by Christian Holzbaur for SICStus 15 Prolog and distributed under the license details below with permission from 16 all mentioned authors. 17 18 This program is free software; you can redistribute it and/or 19 modify it under the terms of the GNU General Public License 20 as published by the Free Software Foundation; either version 2 21 of the License, or (at your option) any later version. 22 23 This program is distributed in the hope that it will be useful, 24 but WITHOUT ANY WARRANTY; without even the implied warranty of 25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 26 GNU General Public License for more details. 27 28 You should have received a copy of the GNU Lesser General Public 29 License along with this library; if not, write to the Free Software 30 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 31 32 As a special exception, if you link this library with other files, 33 compiled with a Free Software compiler, to produce an executable, this 34 library does not by itself cause the resulting executable to be covered 35 by the GNU General Public License. This exception does not however 36 invalidate any other reasons why the executable file might be covered by 37 the GNU General Public License. 38*/ 39 40:- module(clpcd_combine, 41 [ 42 combine/3, 43 normalize/2 44 ]). 45 46:- use_module(library(lists)). 47:- use_module(library(ugraphs)). 48 49% combine(Ga,Gb,Gc) 50% 51% Combines the vertices of Ga and Gb into Gc. 52 53combine(Ga,Gb,Gc) :- 54 normalize(Ga,Gan), 55 normalize(Gb,Gbn), 56 ugraph_union(Gan,Gbn,Gc). 57 58% 59% both Ga and Gb might have their internal ordering invalidated 60% because of bindings and aliasings 61% 62 63normalize([],[]) :- !. 64normalize(G,Gsgn) :- 65 G = [_|_], 66 keysort(G,Gs), % sort vertices on key 67 group(Gs,Gsg), % concatenate vertices with the same key 68 normalize_vertices(Gsg,Gsgn). % normalize 69 70normalize_vertices([],[]). 71normalize_vertices([X-Xnb|Xs],Res) :- 72 ( normalize_vertex(X,Xnb,Xnorm) 73 -> Res = [Xnorm|Xsn], 74 normalize_vertices(Xs,Xsn) 75 ; normalize_vertices(Xs,Res) 76 ). 77 78% normalize_vertex(X,Nbs,X-Nbss) 79% 80% Normalizes a vertex X-Nbs into X-Nbss by sorting Nbs, removing duplicates (also of X) 81% and removing non-vars. 82 83normalize_vertex(X,Nbs,X-Nbsss) :- 84 var(X), 85 sort(Nbs,Nbss), 86 strip_nonvar(Nbss,X,Nbsss). 87 88% strip_nonvar(Nbs,X,Res) 89% 90% Turns vertext X-Nbs into X-Res by removing occurrences of X from Nbs and removing 91% non-vars. This to normalize after bindings have occurred. See also normalize_vertex/3. 92 93strip_nonvar([],_,[]). 94strip_nonvar([X|Xs],Y,Res) :- 95 ( X==Y % duplicate of Y 96 -> strip_nonvar(Xs,Y,Res) 97 ; var(X) % var: keep 98 -> Res = [X|Stripped], 99 strip_nonvar(Xs,Y,Stripped) 100 ; % nonvar: remove 101 nonvar(X), 102 Res = [] % because Vars<anything 103 ). 104 105% group(Vert,Res) 106% 107% Concatenates vertices with the same key. 108 109group([],[]). 110group([K-Kl|Ks],Res) :- 111 group(Ks,K,Kl,Res). 112 113group([],K,Kl,[K-Kl]). 114group([L-Ll|Ls],K,Kl,Res) :- 115 ( K==L 116 -> append(Kl,Ll,KLl), 117 group(Ls,K,KLl,Res) 118 ; Res = [K-Kl|Tail], 119 group(Ls,L,Ll,Tail) 120 )