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(ugraphs)).   47
   48% combine(Ga,Gb,Gc)
   49%
   50% Combines the vertices of Ga and Gb into Gc.
   51
   52combine(Ga,Gb,Gc) :-
   53	normalize(Ga,Gan),
   54	normalize(Gb,Gbn),
   55	ugraph_union(Gan,Gbn,Gc).
   56
   57%
   58% both Ga and Gb might have their internal ordering invalidated
   59% because of bindings and aliasings
   60%
   61
   62normalize([],[]) :- !.
   63normalize(G,Gsgn) :-
   64	G = [_|_],
   65	keysort(G,Gs),	% sort vertices on key
   66	group(Gs,Gsg),	% concatenate vertices with the same key
   67	normalize_vertices(Gsg,Gsgn).	% normalize
   68
   69normalize_vertices([],[]).
   70normalize_vertices([X-Xnb|Xs],Res) :-
   71	(   normalize_vertex(X,Xnb,Xnorm)
   72	->  Res = [Xnorm|Xsn],
   73	    normalize_vertices(Xs,Xsn)
   74	;   normalize_vertices(Xs,Res)
   75	).
   76
   77% normalize_vertex(X,Nbs,X-Nbss)
   78%
   79% Normalizes a vertex X-Nbs into X-Nbss by sorting Nbs, removing duplicates (also of X)
   80% and removing non-vars.
   81
   82normalize_vertex(X,Nbs,X-Nbsss) :-
   83	var(X),
   84	sort(Nbs,Nbss),
   85	strip_nonvar(Nbss,X,Nbsss).
   86
   87% strip_nonvar(Nbs,X,Res)
   88%
   89% Turns vertext X-Nbs into X-Res by removing occurrences of X from Nbs and removing
   90% non-vars. This to normalize after bindings have occurred. See also normalize_vertex/3.
   91
   92strip_nonvar([],_,[]).
   93strip_nonvar([X|Xs],Y,Res) :-
   94	(   X==Y % duplicate of Y
   95	->  strip_nonvar(Xs,Y,Res)
   96	;   var(X) % var: keep
   97	->  Res = [X|Stripped],
   98	    strip_nonvar(Xs,Y,Stripped)
   99	;   % nonvar: remove
  100	    nonvar(X),
  101	    Res = []	% because Vars<anything
  102	).
  103
  104% group(Vert,Res)
  105%
  106% Concatenates vertices with the same key.
  107
  108group([],[]).
  109group([K-Kl|Ks],Res) :-
  110	group(Ks,K,Kl,Res).
  111
  112group([],K,Kl,[K-Kl]).
  113group([L-Ll|Ls],K,Kl,Res) :-
  114	(   K==L
  115	->  append(Kl,Ll,KLl),
  116	    group(Ls,K,KLl,Res)
  117	;   Res = [K-Kl|Tail],
  118	    group(Ls,L,Ll,Tail)
  119	)