Skip to content

Commit

Permalink
reference solutions for Assignment 11
Browse files Browse the repository at this point in the history
git-svn-id: https://slps.svn.sourceforge.net/svnroot/slps@768 ab42f6e0-554d-0410-b580-99e487e6eeb2
  • Loading branch information
grammarware committed Feb 8, 2010
1 parent 140347b commit 713a491
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 0 deletions.
2 changes: 2 additions & 0 deletions topics/exercises/README.txt
Expand Up @@ -32,3 +32,5 @@ lambda5 - typed lambda calculus with alpha conversion in Prolog
lambda6 - typed lambda calculus with alpha conversion & fixed point operator

shapes - the Shapes Problem (object encoding) in Prolog
ccs - the Calculus of Communicating Systems in Prolog (JobShop)
clp - Constraint Logic Programming in Prolog (palindrom dates)
59 changes: 59 additions & 0 deletions topics/exercises/ccs/JobShop.pro
@@ -0,0 +1,59 @@
%% Transition relation

% Prefix
t(X:E,E,X,_).

% Basic combinator
t(E1+_,E2,X,S) :- t(E1,E2,X,S).
t(_+E1,E2,X,S) :- t(E1,E2,X,S).

% Composition
t(E1|E2,E3|E2,X,S) :- t(E1,E3,X,S).
t(E1|E2,E1|E3,X,S) :- t(E2,E3,X,S).

t(E1|E2,E3|E4,tau,S)
:-
t(E1,E3,X1,S),
t(E2,E4,X2,S),
samename(X1,X2).

% References
t(ref(A),E2,X,S)
:-
member((A,E1),S),
t(E1,E2,X,S).

% Restriction
t(restrict(E1,L),restrict(E2,L),X,S)
:-
t(E1,E2,X,S),
oklabel(X,L).

% Relate corresponding names
samename(name(N),coname(N)).
samename(coname(N),name(N)).


% Check label to be not restricted
oklabel(tau,_).
oklabel(X,L) :- basename(X,N), \+ member(N,L).


% Retrieve basename of name and co-name
basename(name(N),N).
basename(coname(N),N).


% Reflexive, transitive closure
tclosure(E,E,[],_).
tclosure(E1,E3,[A|As],S) :- t(E1,E2,A,S), tclosure(E2,E3,As,S).


system(As)
:-
S = [
(jobshop, restrict((ref(hammer)|ref(jobber)),[get,put]))
, (hammer, name(get):name(put):ref(hammer))
, (jobber, name(job):coname(get):coname(put):coname(done):ref(jobber))
],
tclosure(ref(jobshop),_,As,S).
6 changes: 6 additions & 0 deletions topics/exercises/ccs/Makefile
@@ -0,0 +1,6 @@
test:
echo "README: type system(As) and see what happens."
swipl -s JobShop.pro

clean:
rm -rf *~
6 changes: 6 additions & 0 deletions topics/exercises/clp/Makefile
@@ -0,0 +1,6 @@
test:
echo "README: type pdate(X) and see what happens."
swipl -s pdate.pro

clean:
rm -rf *~
38 changes: 38 additions & 0 deletions topics/exercises/clp/pdate.pro
@@ -0,0 +1,38 @@
:- use_module(library(clpfd)).

pdate(Vars)
:-
Vars = [M1,M2,D1,D2,Y1,Y2,Y3,Y4],
reverse(Vars,Vars),

% Overall range
Year in 1380..2010,
Month in 1..12,
Day in 1..31,

% Determine digits by modulo arithmetic
Y1 #= Year / 1000,
Y2 #= (Year - 1000 * Y1) / 100,
Y3 #= (Year - 1000 * Y1 - 100 * Y2) / 10,
Y4 #= Year - 1000 * Y1 - 100 * Y2 - Y3 * 10,
M1 #= Month / 10,
M2 #= Month - M1 * 10,
D1 #= Day / 10,
D2 #= Day - D1 * 10,

% Check validity
valid(Month,Day).

valid(N,M) :- M #=< 31, member(N,[1,3,5,7,8,10,12]).
valid(N,M) :- M #=< 30, member(N,[4,6,9,11]).
valid(2,M) :- M #=< 29. % Actually, Feb 29 maps to year 9220...

/*
?- pdate(X).
X = [0, 1, 0, 2, 2, 0, 1, 0] ;
X = [0, 8, 3, 1, 1, 3, 8, 0] ;
X = [1, 0, 0, 2, 2, 0, 0, 1] ;
false.
*/

0 comments on commit 713a491

Please sign in to comment.