From bbcb29a202436fc0e222e187f1fadf0b1f305465 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Sun, 22 May 2016 17:48:30 +0200 Subject: Prolog: update denotational_semantics problems --- prolog/problems/algol/algol_for_3/common.py | 92 +++++++++++++++++++++++++++++ prolog/problems/algol/algol_for_3/en.py | 18 ++++++ 2 files changed, 110 insertions(+) create mode 100644 prolog/problems/algol/algol_for_3/common.py create mode 100644 prolog/problems/algol/algol_for_3/en.py (limited to 'prolog/problems/algol/algol_for_3') diff --git a/prolog/problems/algol/algol_for_3/common.py b/prolog/problems/algol/algol_for_3/common.py new file mode 100644 index 0000000..654d478 --- /dev/null +++ b/prolog/problems/algol/algol_for_3/common.py @@ -0,0 +1,92 @@ +id = 178 +number = 30 +visible = False +facts = None + +solution = '''\ +algol_for(fun(S0,S,apply178([printout=[]|S0],S,Minstructs))) --> + [begin], instructs178(Minstructs), [end]. + +instructs178(Minstr) --> instr178(Minstr). +instructs178(fun(S0,S, + (apply178(S0,S1,Minstr), + apply178(S1,S,Minstructs)))) + --> + instr178(Minstr), instructs178(Minstructs). + +instr178(Massign) --> assign178(Massign). +instr178(fun(S0,[printout = L1|S1], + (memb(X = V,S0), + del(printout = L0,S0,S1), + conc(L0,[V],L1)))) + --> + [print(X)]. +instr178(fun(S0,S, + loop178(S0,Mcond,Minstructs,S))) + --> + [while], cond178(Mcond), [do,begin], instructs178(Minstructs), [end]. +instr178(fun(S0, S, + (apply178(S0, S1, Minit), + for178(S1, Mcond, Mstep, Mbody, S)))) + --> + [for], assign178(Minit), [&], cond178(Mcond), [&], assign178(Mstep), [do, begin], instructs178(Mbody), [end]. + +assign178(fun(S0,[X = Value|S1], + (apply178(S0,Value,Mexpr), + del(X = _,S0,S1)))) + --> + var178(X), [:=], expr178(Mexpr). + +cond178( fun( S, TruthVal, + (apply178(S,Val1,ME1), + apply178(S,Val2,ME2), + (Val1 < Val2,!,TruthVal = true ; TruthVal = false)))) + --> + expr178(ME1), [<], expr178(ME2). + +var178(X) --> [X], {atom(X)}. + +expr178(fun(S,Value,eval(Expr,S,Value))) --> + [Expr]. + +apply178(In, Out, fun(In, Out, Goals)) :- + call(Goals). + +loop178( State0, Mcond, _, State0) :- + apply178( State0, false, Mcond), !. +loop178( S0, Mcond, MBody, S) :- + copy_term( MBody, MBodyCopy), + apply178( S0, S1, MBody), + loop178( S1, Mcond, MBodyCopy, S). + +for178(S0, Mcond, _, _, S0) :- + apply178(S0, false, Mcond), !. +for178(S0, Mcond, Mstep, Mbody, S) :- + copy_term(Mbody, Mbody2), + copy_term(Mstep, Mstep2), + apply178(S0, S1, Mbody), + apply178(S1, S2, Mstep), + for178(S2, Mcond, Mstep2, Mbody2, S). + +eval178( N, _, N) :- + number178( N), !. +eval178( X, State, Val) :- % A program variable + atom( X), !, + memb( X = Val, State). +eval178( E1 + E2, State, Val) :- !, + eval178( E1, State, V1), + eval178( E2, State, V2), + Val is V1 + V2. +eval178( E1 - E2, State, Val) :- !, + eval178( E1, State, V1), + eval178( E2, State, V2), + Val is V1 - V2. +eval178( E1 * E2, State, Val) :- !, + eval178( E1, State, V1), + eval178( E2, State, V2), + Val is V1 * V2. +eval178( E1 / E2, State, Val) :- !, + eval178( E1, State, V1), + eval178( E2, State, V2), + Val is V1 / V2. +''' diff --git a/prolog/problems/algol/algol_for_3/en.py b/prolog/problems/algol/algol_for_3/en.py new file mode 100644 index 0000000..2f46d36 --- /dev/null +++ b/prolog/problems/algol/algol_for_3/en.py @@ -0,0 +1,18 @@ +name = 'algol_for/3' +slug = 'interpreter for mini-algol with for-statement' + +description = '''\ +

Extend the given DCG for mini-algol to support the for-statement. Example:

+
+% for a := 0 & a < 5 & a := a + 1 do
+% begin
+%   print(a)
+% end
+?- _Program = [begin,for,a,:=,0,&,a,<,5,&,a,:=,a+1,do,begin,print(a),end,end],
+    algol_for(_F, _Program, []),
+    apply([a=2], Output, _F).
+  Output = [a=5,printout=[0,1,2,3,4]].
+
+''' + +hint = {} -- cgit v1.2.1