Is my Prolog Code with the following Diagram and Truth Table correct?

Viewed 65

I have the following Circuit Diagram: enter image description here

I also have the following truth table written in Prolog for the diagram that I made:

mygate(1,1,1,1,1).
mygate(1,1,1,0,1).
mygate(1,1,0,0,1).
mygate(1,1,0,1,1).
mygate(1,0,1,1,0).
mygate(1,0,1,0,0).
mygate(1,0,0,1,0).
mygate(1,0,0,0,1).
mygate(0,1,1,1,0).
mygate(0,1,1,0,0).
mygate(0,1,0,1,0).
mygate(0,1,0,0,1).
mygate(0,0,1,1,0).
mygate(0,0,1,0,0).
mygate(0,0,0,1,0).
mygate(0,0,0,0,1).

After making the truth table based on the diagram, I made my gates in prolog for the AND gates, OR gates, and NOT gates respectively:

myand(1,1,1).
myand(0,1,0).
myand(1,0,0).
myand(0,0,0).

myor(1,0,1).
myor(0,1,1).
myor(0,0,0).

mynot(0,1).
mynot(1,0).

I then have the following circuit to determine all possible answers for this truth table based on the diagram:

circuit(W,X,Y,Z,F) :-
  myand(W,X,T1),
  mynot(Y,T2),
  myor(X,T2,T3),
  myand(T1,T3,T4),
  myor(Y,Z,T5),
  mynot(T5,T6),
  myor(T4,T6,F).

I feel like I have done this right. However, I was given a sample output of what my prolog code should look like after running the circuit(W,X,Y,Z,F) rule, which is:

?- circuit(W, X, Y, Z, F).
W = X, X = Y, Y = Z, Z = 0,
F = 1 ;

W = X, X = Y, Y = F, F = 0,
Z = 1 ;

W = X, X = Z, Z = F, F = 0,
Y = 1 ;

W = X, X = F, F = 0,
Y = Z, Z = 1 ;

W = Y, Y = Z, Z = 0,
X = F, F = 1 ;

W = Y, Y = F, F = 0,
X = Z, Z = 1 ;

W = Z, Z = F, F = 0,
X = Y, Y = 1 ;

W = F, F = 0,
X = Y, Y = Z, Z = 1 ;

W = F, F = 1,
X = Y, Y = Z, Z = 0 ;

W = Z, Z = 1,
X = Y, Y = F, F = 0 ;

W = Y, Y = 1,
X = Z, Z = F, F = 0 ;

W = Y, Y = Z, Z = 1,
X = F, F = 0 ;

W = X, X = F, F = 1,
Y = Z, Z = 0 ;

W = X, X = Z, Z = F, F = 1,
Y = 0 ;

W = X, X = Y, Y = F, F = 1,
Z = 0 ;

W = X, X = Y, Y = Z, Z = F, F = 1.

However, when I run my Prolog Program, I get the following output:

?- circuit(W,X,Y,Z,F).
W = X, X = Y, Y = F, F = 1,
Z = 0 ;

W = Z, Z = F, F = 0,
X = Y, Y = 1 ;

W = X, X = Y, Y = F, F = 0,
Z = 1 ;

W = X, X = Y, Y = Z, Z = 0,
F = 1 ;

W = X, X = Z, Z = F, F = 0,
Y = 1 ;

W = Z, Z = 1,
X = Y, Y = F, F = 0 ;

W = F, F = 1,
X = Y, Y = Z, Z = 0 ;

W = Y, Y = 1,
X = Z, Z = F, F = 0.

As you can see, all of the outputs I get are correct. However, my output is missing several outputs that I should have. I have went through and checked everything. I made sure I had all the and gates listed, all of the possible solutions for my truth table, basically all the things that I could think of checking.

Is the output I get for my Prolog code correct, or am I missing something?

Any help would be appreciated. Thanks!

1 Answers

You're missing a fact:

myor(1,1,1).

For a quick test:

test :- forall(mygate(W,X,Y,Z, F0), (
  circuit(W,X,Y,Z, F1), F0=F1
  -> true
  ;  writeln(mismatch(W,X,Y,Z, F0,F1))
  )).
Related