diff options
author | Timotej Lazar <timotej.lazar@fri.uni-lj.si> | 2016-05-22 17:48:30 +0200 |
---|---|---|
committer | Timotej Lazar <timotej.lazar@fri.uni-lj.si> | 2016-05-22 17:48:30 +0200 |
commit | bbcb29a202436fc0e222e187f1fadf0b1f305465 (patch) | |
tree | fed504361df37c4ce01b5b13c1082d5f3234de79 /prolog/problems/algol/algol_3 | |
parent | 4973979e40dfdc26dafe1cdeddf8e0bce859ba5d (diff) |
Prolog: update denotational_semantics problems
Diffstat (limited to 'prolog/problems/algol/algol_3')
-rw-r--r-- | prolog/problems/algol/algol_3/common.py | 78 | ||||
-rw-r--r-- | prolog/problems/algol/algol_3/en.py | 32 |
2 files changed, 110 insertions, 0 deletions
diff --git a/prolog/problems/algol/algol_3/common.py b/prolog/problems/algol/algol_3/common.py new file mode 100644 index 0000000..415e1e5 --- /dev/null +++ b/prolog/problems/algol/algol_3/common.py @@ -0,0 +1,78 @@ +id = 176 +number = 10 +visible = False +facts = None + +solution = '''\ +algol(fun(S0,S,apply176([printout=[]|S0],S,Minstructs))) --> + [begin], instructs176(Minstructs), [end]. + +instructs176(Minstr) --> instr176(Minstr). +instructs176(fun(S0,S, + (apply176(S0,S1,Minstr), + apply176(S1,S,Minstructs)))) + --> + instr176(Minstr), instructs176(Minstructs). + +instr176(Massign) --> assign176(Massign). +instr176(fun(S0,[printout = L1|S1], + (memb(X = V,S0), + del(printout = L0,S0,S1), + conc(L0,[V],L1)))) + --> + [print(X)]. +instr176(fun(S0,S, + loop176(S0,Mcond,Minstructs,S))) + --> + [while], cond176(Mcond), [do,begin], instructs176(Minstructs), [end]. + +assign176(fun(S0,[X = Value|S1], + (apply176(S0,Value,Mexpr), + del(X = _,S0,S1)))) + --> + var176(X), [:=], expr176(Mexpr). + +cond176( fun( S, TruthVal, + (apply176(S,Val1,ME1), + apply176(S,Val2,ME2), + (Val1 < Val2,!,TruthVal = true ; TruthVal = false)))) + --> + expr176(ME1), [<], expr176(ME2). + +var176(X) --> [X], {atom(X)}. + +expr176(fun(S,Value,eval(Expr,S,Value))) --> + [Expr]. + +apply176(In, Out, fun(In, Out, Goals)) :- + call(Goals). + +loop176(State0, Mcond, _, State0) :- + apply176( State0, false, Mcond), !. +loop176(S0, Mcond, MBody, S) :- + copy_term( MBody, MBodyCopy), + apply176( S0, S1, MBody), + loop176( S1, Mcond, MBodyCopy, S). + +eval176(N, _, N) :- + number176( N), !. +eval176(X, State, Val) :- % A program variable + atom(X), !, + memb(X = Val, State). +eval176(E1 + E2, State, Val) :- !, + eval176(E1, State, V1), + eval176(E2, State, V2), + Val is V1 + V2. +eval176(E1 - E2, State, Val) :- !, + eval176(E1, State, V1), + eval176(E2, State, V2), + Val is V1 - V2. +eval176(E1 * E2, State, Val) :- !, + eval176(E1, State, V1), + eval176(E2, State, V2), + Val is V1 * V2. +eval176(E1 / E2, State, Val) :- !, + eval176(E1, State, V1), + eval176(E2, State, V2), + Val is V1 / V2. +''' diff --git a/prolog/problems/algol/algol_3/en.py b/prolog/problems/algol/algol_3/en.py new file mode 100644 index 0000000..3b91a47 --- /dev/null +++ b/prolog/problems/algol/algol_3/en.py @@ -0,0 +1,32 @@ +name = 'algol/3' +slug = 'interpreter for mini-algol' + +description = '''\ +<p>A DCG for mini-algol.</p> +<pre> +% apply a function to a starting state +?- apply([a=2], Out, fun(_In, Out, eval(a+3, _In, Out))). + Out = 5. + +% a := a+b +% b := a-b +% a := a-b +?- _Program = [begin,a,:=,a+b,b,:=,a-b,a,:=,a-b,end], + algol(_F, _Program, []), + apply([a=3,b=5], Output, _F). + Output = [a=5,b=3,printout=[]]. + +% a := 0 +% while a < 10 do +% begin +% print(a) +% a := a+1 +% end +?- _Program = [begin,a,:=,0,while,a,<,10,do,begin,print(a),a,:=,a+1,end,end], + algol(_F, _Program, []), + apply([a=3], Output, _F). + Output = [a=10,printout=[0,1,2,3,4,5,6,7,8,9]]. +</pre> +''' + +hint = {} |