diff --git a/topics/exercises/README.txt b/topics/exercises/README.txt new file mode 100644 index 00000000..2052f917 --- /dev/null +++ b/topics/exercises/README.txt @@ -0,0 +1,8 @@ +while1 - a DCG parser for a fragment of While +while2 - a DCG parser for While +while3 - big step semantics for While (+ a DCG parser) +while4 - small step semantics for While (+ a DCG parser) +xml1 - a DCG parser for XML subset (elements only) +xml2 - a DCG parser for XML subset (elements and attributes) +nb1 - abstract syntax for NB +nb1 - typed NB abstract syntax diff --git a/topics/exercises/nb1/Makefile b/topics/exercises/nb1/Makefile new file mode 100644 index 00000000..97d32b09 --- /dev/null +++ b/topics/exercises/nb1/Makefile @@ -0,0 +1,6 @@ +test: + swipl -f syntax.pro -t "term(`cat input.txt`)" + +prompt: + swipl -s syntax.pro + diff --git a/topics/exercises/nb1/input.txt b/topics/exercises/nb1/input.txt new file mode 100644 index 00000000..f477d97d --- /dev/null +++ b/topics/exercises/nb1/input.txt @@ -0,0 +1 @@ +if(iszero(zero),true,succ(zero)) diff --git a/topics/exercises/nb1/syntax.pro b/topics/exercises/nb1/syntax.pro index b6120594..49a971ea 100644 --- a/topics/exercises/nb1/syntax.pro +++ b/topics/exercises/nb1/syntax.pro @@ -1,5 +1,7 @@ +% see slides #109, #113 term(true). term(false). +term(zero). term(succ(T)) :- term(T). term(pred(T)) :- term(T). term(iszero(T)) :- term(T). diff --git a/topics/exercises/nb2/Makefile b/topics/exercises/nb2/Makefile new file mode 100644 index 00000000..179c7355 --- /dev/null +++ b/topics/exercises/nb2/Makefile @@ -0,0 +1,7 @@ +test: + swipl -f types.pro -t "type(`cat input1.txt`,T),write(T),nl" + swipl -f types.pro -t "type(`cat input2.txt`,T),write(T),nl" + +prompt: + swipl -s types.pro + diff --git a/topics/exercises/nb2/input1.txt b/topics/exercises/nb2/input1.txt new file mode 100644 index 00000000..0843cecc --- /dev/null +++ b/topics/exercises/nb2/input1.txt @@ -0,0 +1 @@ +if(iszero(zero),zero,succ(succ(zero))) diff --git a/topics/exercises/nb2/input2.txt b/topics/exercises/nb2/input2.txt new file mode 100644 index 00000000..974ed1ba --- /dev/null +++ b/topics/exercises/nb2/input2.txt @@ -0,0 +1 @@ +if(iszero(zero),iszero(succ(zero)),true) diff --git a/topics/exercises/nb2/syntax.pro b/topics/exercises/nb2/syntax.pro new file mode 100644 index 00000000..49a971ea --- /dev/null +++ b/topics/exercises/nb2/syntax.pro @@ -0,0 +1,9 @@ +% see slides #109, #113 +term(true). +term(false). +term(zero). +term(succ(T)) :- term(T). +term(pred(T)) :- term(T). +term(iszero(T)) :- term(T). +term(if(T1,T2,T3)) :- term(T1), term(T2), term(T3). + diff --git a/topics/exercises/nb2/types.pro b/topics/exercises/nb2/types.pro new file mode 100644 index 00000000..d5f79cff --- /dev/null +++ b/topics/exercises/nb2/types.pro @@ -0,0 +1,27 @@ +:- ['syntax.pro']. + +%% see slide 126 +% T-True +type(true,bool). + +% T-False +type(false,bool). + +% T-If +type(if(T1,T2,T3),T) :- + type(T1,bool), + type(T2,T), + type(T3,T). + +% T-Zero +type(zero,nat). + +% T-Succ +type(succ(T),nat) :- type(T,nat). + +% T-Pred +type(pred(T),nat) :- type(T,nat). + +% T-Iszero +type(iszero(T),bool) :- type(T,nat). + diff --git a/topics/exercises/while3/mappings.pro b/topics/exercises/while3/mappings.pro index 7928218e..ec642ea1 100644 --- a/topics/exercises/while3/mappings.pro +++ b/topics/exercises/while3/mappings.pro @@ -4,6 +4,8 @@ lookup(M,X,Y) :- append(_,[(X,Y)|_],M). % Function update in one position update([],X,Y,[(X,Y)]). update([(X,_)|M],X,Y,[(X,Y)|M]). -update([(X1,Y1)|M1],X2,Y2,[(X1,Y1)|M2]) :- \+ X1 = X2, update(M1,X2,Y2,M2). +update([(X1,Y1)|M1],X2,Y2,[(X1,Y1)|M2]) :- + \+ X1 = X2, + update(M1,X2,Y2,M2).