Created
February 16, 2023 00:50
-
-
Save hsk/ea5a37f28608dc51621c8519b1197526 to your computer and use it in GitHub Desktop.
cek machine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% https://github.com/pycket/pycket/blob/master/papers/icfp15-paper.pdf | |
% e ::= x | λ(x,e) | e $ e | i | e + e | add(x,x) | |
% k ::= [] | [arg(e,p)|k] | [fun(v,p)|k] | |
:- op(1100,yfx,$). | |
v(I):- integer(I). | |
v(λ(_X,_E)). | |
cek(X/P/K , V/P/K):- atom(X),member(X->V,P). | |
cek((E1$E2)/P/K , E1/P/[arg(E2,P)|K]). | |
cek(V/P/[arg(E,P_)|K] , E/P_/[fun(V,P)|K]):- v(V). | |
cek(V/_/[fun(λ(X,E),P_)|K], E/[X->V|P_]/K):- v(V). | |
cek((E1+E2)/P/K , λ(x,λ(y,add(x,y)))/P/[arg(E1,P),arg(E2,P)|K]). | |
cek(add(X,Y)/P/K , I/P/K):- member(X->I1,P),member(Y->I2,P),I is I1+I2. | |
eval(CEK,CEK2):- cek(CEK,CEK1),!,eval(CEK1,CEK2). | |
eval(CEK,CEK). | |
run(E,R):- eval(E/[]/[],R/_/_). | |
:- run(1+2,R),writeln(R),R=3. | |
:- run(1+2+3,R),writeln(R),R=6. | |
:- run(λ(x,λ(y,x+y))$10$2,R),writeln(R),R=12. | |
:- halt. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment