Skip to content

Instantly share code, notes, and snippets.

@dydx
Created October 31, 2015 05:54
Show Gist options
  • Save dydx/8f6abc7cac888bafbdc4 to your computer and use it in GitHub Desktop.
Save dydx/8f6abc7cac888bafbdc4 to your computer and use it in GitHub Desktop.
nat(z). %% zero
nat(Z) :- %% every number after zero is a successor
successor(Y, X), nat(Y).
successor(s(A), A).
%% A + 0 = A
natadd(A, z, A).
%% s(C) = A + S(B) if C = A + B
natadd(A, s(B), s(C)) :-
natadd(A, B, C).
%% 3 + 2 = 5
natadd(s(s(s(z))), s(s(z)), S).
%% P + 2 = S
natadd(s(s(z)), P, S).
%% P = z,
%% S = s(s(z));
%% P = s(z),
%% S = s(s(s(z)));
%% P = s(s(z)),
%% S = s(s(s(s(z))));
%% IT SOLVES UNTIL YOU TELL IT TO STOP. !!!!!
%% A + B = 4
natadd(A, B, s(s(s(s(z))))).
%% A = s(s(s(s(z)))),
%% B = z;
%% A = s(s(s(z))),
%% B = s(z);
%% A = B, B = s(s(z));
%% A = s(z),
%% B = s(s(s(z)));
%% A = z,
%% B = s(s(s(s(z))));
%% This is fucking amazing
product(z, B, z).
product(s(z), B, B).
product(s(A), B, Product) :-
natadd(B, SubProduct, Product),
product(A, B, SubProduct).
%% This also just happens to be the implementation of division,
%% depending on the arguments being passed into it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment