-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
git-svn-id: https://slps.svn.sourceforge.net/svnroot/slps@710 ab42f6e0-554d-0410-b580-99e487e6eeb2
- Loading branch information
1 parent
4f979ef
commit f708834
Showing
14 changed files
with
118 additions
and
61 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
test: | ||
swipl -f syntax.pro -t "write(term(`cat input.txt`)),nl" | ||
|
||
prompt: | ||
swipl -s syntax.pro | ||
swipl -f run.pro -t "main('input.txt')" | ||
|
||
clean: | ||
rm -rf *~ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
lambda(f,lambda(g,lambda(x,apply(f,apply(g,x))))) | ||
lambda f . lambda g . lambda x . app f : app g : x |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
% Program is a list of statements | ||
expression(app(M,N)) --> keyword("app"), expression(M), keyword(":"), expression(N). | ||
expression(lam(X,N)) --> keyword("lambda"), name(X), keyword("."), expression(N). | ||
expression(var(N)) --> name(N). | ||
|
||
% Dealing with spaces | ||
spaces --> [0' ], spaces. %' | ||
spaces --> []. | ||
|
||
% Keywords are space-consuming strings | ||
keyword(X) --> | ||
spaces, | ||
string(X). | ||
|
||
% Bare strings | ||
string([],X,X). | ||
string([H|T1],[H|T2],X) :- string(T1,T2,X). | ||
|
||
% Identifiers are space-consuming letters | ||
name(V) --> | ||
spaces, | ||
letter(H), | ||
{ atom_chars(V,[H]) }. | ||
|
||
% Bare letters | ||
letters([H|T]) --> letter(H), letters(T). | ||
letters([]) --> []. | ||
letter(H,[H|T],T) :- H >= 0'a, H =< 0'z. | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
:- ['parser.pro','syntax.pro']. | ||
|
||
main(File) :- | ||
parseFile(File,expression,E), | ||
term(E), | ||
write(E),nl. | ||
|
||
parseFile(File,P,R) :- | ||
open(File,read,Stream,[]), | ||
read_stream_to_codes(Stream, Contents), | ||
close(Stream), | ||
apply(P,[R,Contents,Rest]), | ||
eof(Rest,_). | ||
|
||
eof([],[]). | ||
eof([0' |T],R) :- eof(T,R). %' | ||
eof([10|T],R) :- eof(T,R). |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,21 @@ | ||
% see slides #147 | ||
% See slide 174 | ||
|
||
term(x). | ||
term(y). | ||
term(z). | ||
term(f). | ||
term(g). | ||
term(h). | ||
term(var(X)) :- variable(X). | ||
term(app(T1,T2)) :- term(T1), term(T2). | ||
term(lam(X,T)) :- variable(X), term(T). | ||
|
||
term(apply(M,N)) :- term(M), term(N). | ||
term(lambda(X,M)) :- term(X), term(M). | ||
value(lam(X,T)) :- variable(X), term(T). | ||
value(var(X)) :- variable(X). % pragmatic extension to deal with open terms | ||
|
||
variable(a). | ||
variable(f). | ||
variable(g). | ||
variable(l). | ||
variable(m). | ||
variable(n). | ||
variable(t). | ||
variable(v). | ||
variable(w). | ||
variable(x). | ||
variable(y). | ||
variable(z). |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
if(iszero(zero),zero,succ(succ(zero))) | ||
if(iszero(pred(succ(zero))),zero,succ(succ(zero))) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
:- ['syntax.pro']. | ||
|
||
%% Small-step transition relation: slide 120 | ||
eval(E,V,T) :- | ||
evalstep(E,V,T), | ||
isfinal(V). | ||
|
||
eval(E1,V,T) :- | ||
evalstep(E1,E2,T), | ||
eval(E2,V,T). | ||
|
||
evalstep(true,tt,bool). | ||
evalstep(false,ff,bool). | ||
evalstep(zero,0,nat). | ||
% E-Succ | ||
evalstep(succ(E1),succ(E2),nat) :- evalstep(E1,E2,nat). | ||
% E-PredZero | ||
evalstep(pred(zero),zero,nat). | ||
% E-PredSucc | ||
evalstep(pred(succ(N)),N,nat). | ||
% E-Pred | ||
evalstep(pred(E1),pred(E2),nat) :- evalstep(E1,E2,nat). | ||
% E-Iszero-Zero | ||
evalstep(iszero(zero),tt,bool). | ||
% E-Iszero | ||
evalstep(iszero(T1),iszero(T2),bool) :- evalstep(T1,T2,nat). | ||
% E-IszeroSucc | ||
evalstep(iszero(succ(_)),ff,bool). | ||
% E-IfTrue | ||
evalstep(if(tt,T2,_),T2,_). | ||
% E-IfFalse | ||
evalstep(if(ff,_,T3),T3,_). | ||
% E-If | ||
evalstep(if(T1,T2,T3),if(T4,T2,T3),_) :- evalstep(T1,T4,bool). | ||
|
||
isfinal(tt). | ||
isfinal(ff). | ||
isfinal(0). | ||
isfinal(succ(T)) :- isfinal(T). |