Skip to content

Instantly share code, notes, and snippets.

@wouterj
Created July 28, 2014 08:57
Show Gist options
  • Select an option

  • Save wouterj/7169bb2e4adc1def907b to your computer and use it in GitHub Desktop.

Select an option

Save wouterj/7169bb2e4adc1def907b to your computer and use it in GitHub Desktop.
and(A, A, A).
and(A, B, 0) :- A \= B.
or(1, 0, 1).
or(0, 1, 1).
or(1, 1, 1).
or(0, 0, 0).
xor(1, 0, 1).
xor(0, 1, 1).
xor(0, 0, 0).
xor(1, 1, 0).
not(1, 0).
not(0, 1).
half-adder(A, B, [S | Cout]) :- xor(A, B, S), and(A, B, Cout).
full-adder(A, B, Cin, [S | Cout]) :-
half-adder(A, B, [Xs | Xc]),
half-adder(Xs, Cin, [S | Yc]),
xor(Xc, Yc, Cout).
add([X8, X4, X2, X1], [Y8, Y4, Y2, Y1], [S16, S8, S4, S2, S1]) :-
full-adder(X1, Y1, 0, [S1 | C1]),
full-adder(X2, Y2, C1, [S2 | C2]),
full-adder(X4, Y4, C2, [S4 | C3]),
full-adder(X8, Y8, C3, [S8 | S16]).
half-substractor(A, B, [D | Bout]) :- xor(A, B, D), not(A, NotA), and(NotA, B, Bout).
full-substractor(A, B, Bin, [D | Bout]) :-
half-substractor(A, B, [Xd | Xb]),
half-substractor(Xd, Bin, [D | Yb]),
or(Xb, Yb, Bout).
substract([X8, X4, X2, X1], [Y8, Y4, Y2, Y1], [S8, S4, S2, S1]) :-
full-substractor(X1, Y1, 0, [S1 | B1]),
full-substractor(X2, Y2, B1, [S2 | B2]),
full-substractor(X4, Y4, B2, [S4 | B3]),
full-substractor(X8, Y8, B3, [S8 | _]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment