37
38
39:- module(clpqr_ordering,
40 [ combine/3,
41 ordering/1,
42 arrangement/2
43 ]). 44:- use_module(class,
45 [
46 class_get_prio/2,
47 class_put_prio/2
48 ]). 49:- use_module(itf,
50 [
51 clp_type/2
52 ]). 53:- autoload(library(ugraphs),
54 [
55 add_edges/3,
56 add_vertices/3,
57 top_sort/2,
58 ugraph_union/3
59 ]). 60:- autoload(library(lists),
61 [
62 append/3
63 ]). 64
65ordering(X) :-
66 var(X),
67 !,
68 fail.
69ordering(A>B) :-
70 !,
71 ordering(B<A).
72ordering(A<B) :-
73 join_class([A,B],Class),
74 class_get_prio(Class,Ga),
75 !,
76 add_edges([],[A-B],Gb),
77 combine(Ga,Gb,Gc),
78 class_put_prio(Class,Gc).
79ordering(Pb) :-
80 Pb = [_|Xs],
81 join_class(Pb,Class),
82 class_get_prio(Class,Ga),
83 !,
84 ( Xs = [],
85 add_vertices([],Pb,Gb)
86 ; Xs=[_|_],
87 gen_edges(Pb,Es,[]),
88 add_edges([],Es,Gb)
89 ),
90 combine(Ga,Gb,Gc),
91 class_put_prio(Class,Gc).
92ordering(_).
93
94arrangement(Class,Arr) :-
95 class_get_prio(Class,G),
96 normalize(G,Gn),
97 top_sort(Gn,Arr),
98 !.
99arrangement(_,_) :- throw(unsatisfiable_ordering).
100
101join_class([],_).
102join_class([X|Xs],Class) :-
103 ( var(X)
104 -> clp_type(X,CLP),
105 ( CLP = clpr
106 -> bv_r:var_intern(X,Class)
107 ; bv_q:var_intern(X,Class)
108 )
109 ; true
110 ),
111 join_class(Xs,Class).
112
116
117combine(Ga,Gb,Gc) :-
118 normalize(Ga,Gan),
119 normalize(Gb,Gbn),
120 ugraph_union(Gan,Gbn,Gc).
121
126
127normalize([],[]) :- !.
128normalize(G,Gsgn) :-
129 G = [_|_],
130 keysort(G,Gs), 131 group(Gs,Gsg), 132 normalize_vertices(Gsg,Gsgn). 133
134normalize_vertices([],[]).
135normalize_vertices([X-Xnb|Xs],Res) :-
136 ( normalize_vertex(X,Xnb,Xnorm)
137 -> Res = [Xnorm|Xsn],
138 normalize_vertices(Xs,Xsn)
139 ; normalize_vertices(Xs,Res)
140 ).
141
146
147normalize_vertex(X,Nbs,X-Nbsss) :-
148 var(X),
149 sort(Nbs,Nbss),
150 strip_nonvar(Nbss,X,Nbsss).
151
156
157strip_nonvar([],_,[]).
158strip_nonvar([X|Xs],Y,Res) :-
159 ( X==Y 160 -> strip_nonvar(Xs,Y,Res)
161 ; var(X) 162 -> Res = [X|Stripped],
163 strip_nonvar(Xs,Y,Stripped)
164 ; 165 nonvar(X),
166 Res = [] 167 ).
168
169gen_edges([]) --> [].
170gen_edges([X|Xs]) -->
171 gen_edges(Xs,X),
172 gen_edges(Xs).
173
174gen_edges([],_) --> [].
175gen_edges([Y|Ys],X) -->
176 [X-Y],
177 gen_edges(Ys,X).
178
182
183group([],[]).
184group([K-Kl|Ks],Res) :-
185 group(Ks,K,Kl,Res).
186
187group([],K,Kl,[K-Kl]).
188group([L-Ll|Ls],K,Kl,Res) :-
189 ( K==L
190 -> append(Kl,Ll,KLl),
191 group(Ls,K,KLl,Res)
192 ; Res = [K-Kl|Tail],
193 group(Ls,L,Ll,Tail)
194 ).
195
196
197 200:- multifile
201 sandbox:safe_primitive/1. 202
203sandbox:safe_primitive(clpqr_ordering:ordering(_))