1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %% Thom Fruehwirth ECRC 1991-1993
4 %% 910528 started boolean,and,or constraints
5 %% 910904 added xor,neg constraints
6 %% 911120 added imp constraint
7 %% 931110 ported to new release
8 %% 931111 added card constraint
9 %% 961107 Christian Holzbaur, SICStus mods
11 %% ported to hProlog by Tom Schrijvers June 2003
14 :- module(bool,[main/0,main/1]).
16 :- use_module( library(chr)).
18 :- use_module(library(lists)).
20 :- constraints boolean/1, and/3, or/3, xor/3, neg/2, imp/2, labeling/0, card/4.
26 labeling, boolean(A)#Pc <=>
32 %% and/3 specification
42 and(X,Y,1) <=> X=1,Y=1.
44 %%and(X,Y,X) <=> imp(X,Y).
45 %%and(X,Y,Y) <=> imp(Y,X).
46 and(X,Y,A) \ and(X,Y,B) <=> A=B.
47 and(X,Y,A) \ and(Y,X,B) <=> A=B.
49 labeling, and(A,B,C)#Pc <=>
66 or(X,Y,0) <=> X=0,Y=0.
70 %%or(X,Y,X) <=> imp(Y,X).
71 %%or(X,Y,Y) <=> imp(X,Y).
72 or(X,Y,A) \ or(X,Y,B) <=> A=B.
73 or(X,Y,A) \ or(Y,X,B) <=> A=B.
75 labeling, or(A,B,C)#Pc <=>
84 %% xor/3 specification
93 xor(1,X,Y) <=> neg(X,Y).
94 xor(X,1,Y) <=> neg(X,Y).
95 xor(X,Y,1) <=> neg(X,Y).
99 xor(X,Y,A) \ xor(X,Y,B) <=> A=B.
100 xor(X,Y,A) \ xor(Y,X,B) <=> A=B.
102 labeling, xor(A,B,C)#Pc <=>
108 label_xor(1,X,Y):- neg(X,Y).
111 %% neg/2 specification
120 neg(X,Y) \ neg(Y,Z) <=> X=Z.
121 neg(X,Y) \ neg(Z,Y) <=> X=Z.
122 neg(Y,X) \ neg(Y,Z) <=> X=Z.
123 %% Interaction with other boolean constraints
124 neg(X,Y) \ and(X,Y,Z) <=> Z=0.
125 neg(Y,X) \ and(X,Y,Z) <=> Z=0.
126 neg(X,Z) , and(X,Y,Z) <=> X=1,Y=0,Z=0.
127 neg(Z,X) , and(X,Y,Z) <=> X=1,Y=0,Z=0.
128 neg(Y,Z) , and(X,Y,Z) <=> X=0,Y=1,Z=0.
129 neg(Z,Y) , and(X,Y,Z) <=> X=0,Y=1,Z=0.
130 neg(X,Y) \ or(X,Y,Z) <=> Z=1.
131 neg(Y,X) \ or(X,Y,Z) <=> Z=1.
132 neg(X,Z) , or(X,Y,Z) <=> X=0,Y=1,Z=1.
133 neg(Z,X) , or(X,Y,Z) <=> X=0,Y=1,Z=1.
134 neg(Y,Z) , or(X,Y,Z) <=> X=1,Y=0,Z=1.
135 neg(Z,Y) , or(X,Y,Z) <=> X=1,Y=0,Z=1.
136 neg(X,Y) \ xor(X,Y,Z) <=> Z=1.
137 neg(Y,X) \ xor(X,Y,Z) <=> Z=1.
138 neg(X,Z) \ xor(X,Y,Z) <=> Y=1.
139 neg(Z,X) \ xor(X,Y,Z) <=> Y=1.
140 neg(Y,Z) \ xor(X,Y,Z) <=> X=1.
141 neg(Z,Y) \ xor(X,Y,Z) <=> X=1.
142 neg(X,Y) , imp(X,Y) <=> X=0,Y=1.
143 neg(Y,X) , imp(X,Y) <=> X=0,Y=1.
145 labeling, neg(A,B)#Pc <=>
154 %% imp/2 specification (implication)
164 imp(X,Y),imp(Y,X) <=> X=Y.
166 labeling, imp(A,B)#Pc <=>
176 %% Boolean cardinality operator
177 %% card(A,B,L,N) constrains list L of length N to have between A and B 1s
182 A=<B,0=<B,A=<N, %0=<N
185 %% card/4 specification
186 %%card(A,B,[],0):- A=<0,0=<B.
187 %%card(A,B,[0|L],N):-
190 %%card(A,B,[1|L],N):-
191 %% A1 is A-1, B1 is B-1, N1 is N-1,
194 triv_sat @ card(A,B,L,N) <=> A=<0,N=<B | true. % trivial satisfaction
195 pos_sat @ card(N,B,L,N) <=> set_to_ones(L). % positive satisfaction
196 neg_sat @ card(A,0,L,N) <=> set_to_zeros(L). % negative satisfaction
197 pos_red @ card(A,B,L,N) <=> b_delete(X,L,L1),X==1 | % positive reduction
198 A1 is A-1, B1 is B-1, N1 is N-1,
200 neg_red @ card(A,B,L,N) <=> b_delete(X,L,L1),X==0 | % negative reduction
203 %% special cases with two variables
204 card2nand @ card(0,1,[X,Y],2) <=> and(X,Y,0).
205 card2neg @ card(1,1,[X,Y],2) <=> neg(X,Y).
206 card2or @ card(1,2,[X,Y],2) <=> or(X,Y,1).
208 b_delete( X, [X|L], L).
209 b_delete( Y, [X|Xs], [X|Xt]) :-
210 b_delete( Y, Xs, Xt).
212 labeling, card(A,B,L,N)#Pc <=>
217 label_card(A,B,[],0):- A=<0,0=<B.
218 label_card(A,B,[0|L],N):-
221 label_card(A,B,[1|L],N):-
222 A1 is A-1, B1 is B-1, %N1 is N-1,
230 set_to_zeros([0|L]):-
235 %% Auxiliary predicates
239 solve_bool(A,C) :- var(A), !, A=C.
240 solve_bool(A,C) :- atomic(A), !, A=C.
241 solve_bool(A * B, C) :- !,
245 solve_bool(A + B, C) :- !,
249 solve_bool(A # B, C) :- !,
253 solve_bool(not(A),C) :- !,
256 solve_bool((A -> B), C) :- !,
260 solve_bool(A = B, C) :- !,
271 /* % no write macros in SICStus and hProlog
273 bool_portray(and(A,B,C),Out):- !, Out = (A*B = C).
274 bool_portray(or(A,B,C),Out):- !, Out = (A+B = C).
275 bool_portray(xor(A,B,C),Out):- !, Out = (A#B = C).
276 bool_portray(neg(A,B),Out):- !, Out = (A= not(B)).
277 bool_portray(imp(A,B),Out):- !, Out = (A -> B).
278 bool_portray(card(A,B,L,N),Out):- !, Out = card(A,B,L).
280 :- define_macro(type(compound),bool_portray/2,[write]).
283 /* end of handler bool */
285 half_adder(X,Y,S,C) :-
289 full_adder(X,Y,Ci,S,Co) :-
290 half_adder(X,Y,S1,Co1),
291 half_adder(Ci,S1,S,Co2),
302 write(bench(bool, N, Time, 0, hprolog)),write('.'),nl.
314 full_adder(0,Y,C,1,NC),
319 full_adder(1,Y,C,0,NC),