Skip to content

Instantly share code, notes, and snippets.

@bmjames
Created January 3, 2011 18:36
Show Gist options
  • Save bmjames/763757 to your computer and use it in GitHub Desktop.
Save bmjames/763757 to your computer and use it in GitHub Desktop.
Prolog version of the SKI calculus evaluator
%% -*-mode: prolog-*-
%% Example of the reversal operator:
%% ?- R = [[s, [k, [s, i]]], k],
%% eval([[R, a], b], Result).
%%
%% R = [[s,[k,[s,i]]],k]
%% Result = [b,a] ?
%% eval delegates to eval1 to perform the transformations of the calculus,
%% and recurses until the term can not be transformed any further.
eval(X, Result) :-
eval1(X, Y),
\+(X == Y),
eval(Y, Result).
eval(X, X).
%% eval1 performs the actual transformations
eval1([i, X], X).
eval1([[k, X], _], X).
eval1([[[s, X], Y], Z], [[X, Z], [Y, Z]]).
eval1([X, Y], [X1, Y1]) :-
eval1(X, X1),
eval1(Y, Y1).
eval1(X, X).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment