View source with raw comments or as raw
    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
   41:- module(ordering,
   42	[
   43	    combine/3,
   44	    ordering/1,
   45	    arrangement/2
   46	]).   47:- use_module(class,
   48	[
   49	    class_get_clp/2,
   50	    class_get_prio/2,
   51	    class_put_prio/2
   52	]).   53:- use_module(itf,
   54	[
   55	    clp_type/2
   56	]).   57:- use_module(library(ugraphs),
   58	[
   59	    add_edges/3,
   60	    add_vertices/3,
   61	    top_sort/2,
   62	    ugraph_union/3
   63	]).   64:- use_module(library(lists),
   65	[
   66	    append/3
   67	]).   68
   69ordering(X) :-
   70	var(X),
   71	!,
   72	fail.
   73ordering(A>B) :-
   74	!,
   75	ordering(B<A).
   76ordering(A<B) :-
   77	join_class([A,B],Class),
   78	class_get_prio(Class,Ga),
   79	!,
   80	add_edges([],[A-B],Gb),
   81	combine(Ga,Gb,Gc),
   82	class_put_prio(Class,Gc).
   83ordering(Pb) :-
   84	Pb = [_|Xs],
   85	join_class(Pb,Class),
   86	class_get_prio(Class,Ga),
   87	!,
   88	(   Xs = [],
   89	    add_vertices([],Pb,Gb)
   90	;   Xs=[_|_],
   91	    gen_edges(Pb,Es,[]),
   92	    add_edges([],Es,Gb)
   93	),
   94	combine(Ga,Gb,Gc),
   95	class_put_prio(Class,Gc).
   96ordering(_).
   97
   98arrangement(Class,Arr) :-
   99	class_get_prio(Class,G),
  100	normalize(G,Gn),
  101	top_sort(Gn,Arr),
  102	!.
  103arrangement(_,_) :- throw(unsatisfiable_ordering).
  104
  105join_class([],_).
  106join_class([X|Xs],Class) :-
  107	(   var(X)
  108	->  clp_type(X,CLP),
  109	    (   CLP = clpr
  110	    ->  bv_r:var_intern(X,Class)
  111	    ;   bv_q:var_intern(X,Class)
  112	    )
  113	;   true
  114	),
  115	join_class(Xs,Class).
  116
  117% combine(Ga,Gb,Gc)
  118%
  119% Combines the vertices of Ga and Gb into Gc.
  120
  121combine(Ga,Gb,Gc) :-
  122	normalize(Ga,Gan),
  123	normalize(Gb,Gbn),
  124	ugraph_union(Gan,Gbn,Gc).
  125
  126%
  127% both Ga and Gb might have their internal ordering invalidated
  128% because of bindings and aliasings
  129%
  130
  131normalize([],[]) :- !.
  132normalize(G,Gsgn) :-
  133	G = [_|_],
  134	keysort(G,Gs),	% sort vertices on key
  135	group(Gs,Gsg),	% concatenate vertices with the same key
  136	normalize_vertices(Gsg,Gsgn).	% normalize
  137
  138normalize_vertices([],[]).
  139normalize_vertices([X-Xnb|Xs],Res) :-
  140	(   normalize_vertex(X,Xnb,Xnorm)
  141	->  Res = [Xnorm|Xsn],
  142	    normalize_vertices(Xs,Xsn)
  143	;   normalize_vertices(Xs,Res)
  144	).
  145
  146% normalize_vertex(X,Nbs,X-Nbss)
  147%
  148% Normalizes a vertex X-Nbs into X-Nbss by sorting Nbs, removing duplicates (also of X)
  149% and removing non-vars.
  150
  151normalize_vertex(X,Nbs,X-Nbsss) :-
  152	var(X),
  153	sort(Nbs,Nbss),
  154	strip_nonvar(Nbss,X,Nbsss).
  155
  156% strip_nonvar(Nbs,X,Res)
  157%
  158% Turns vertext X-Nbs into X-Res by removing occurrences of X from Nbs and removing
  159% non-vars. This to normalize after bindings have occurred. See also normalize_vertex/3.
  160
  161strip_nonvar([],_,[]).
  162strip_nonvar([X|Xs],Y,Res) :-
  163	(   X==Y % duplicate of Y
  164	->  strip_nonvar(Xs,Y,Res)
  165	;   var(X) % var: keep
  166	->  Res = [X|Stripped],
  167	    strip_nonvar(Xs,Y,Stripped)
  168	;   % nonvar: remove
  169	    nonvar(X),
  170	    Res = []	% because Vars<anything
  171	).
  172
  173gen_edges([]) --> [].
  174gen_edges([X|Xs]) -->
  175	gen_edges(Xs,X),
  176	gen_edges(Xs).
  177
  178gen_edges([],_) --> [].
  179gen_edges([Y|Ys],X) -->
  180	[X-Y],
  181	gen_edges(Ys,X).
  182
  183% group(Vert,Res)
  184%
  185% Concatenates vertices with the same key.
  186
  187group([],[]).
  188group([K-Kl|Ks],Res) :-
  189	group(Ks,K,Kl,Res).
  190
  191group([],K,Kl,[K-Kl]).
  192group([L-Ll|Ls],K,Kl,Res) :-
  193	(   K==L
  194	->  append(Kl,Ll,KLl),
  195	    group(Ls,K,KLl,Res)
  196	;   Res = [K-Kl|Tail],
  197	    group(Ls,L,Ll,Tail)
  198	).
  199
  200
  201		 /*******************************
  202		 *	       SANDBOX		*
  203		 *******************************/
  204:- multifile
  205	sandbox:safe_primitive/1.  206
  207sandbox:safe_primitive(ordering:ordering(_))