summaryrefslogtreecommitdiff
path: root/prolog/problems/denotational_semantics/algol_if_3
diff options
context:
space:
mode:
Diffstat (limited to 'prolog/problems/denotational_semantics/algol_if_3')
-rw-r--r--prolog/problems/denotational_semantics/algol_if_3/common.py86
-rw-r--r--prolog/problems/denotational_semantics/algol_if_3/en.py20
2 files changed, 106 insertions, 0 deletions
diff --git a/prolog/problems/denotational_semantics/algol_if_3/common.py b/prolog/problems/denotational_semantics/algol_if_3/common.py
new file mode 100644
index 0000000..8ca92bc
--- /dev/null
+++ b/prolog/problems/denotational_semantics/algol_if_3/common.py
@@ -0,0 +1,86 @@
+id = 177
+group = 'denotational_semantics'
+number = 84
+visible = True
+facts = None
+
+solution = '''\
+algol_if(fun(S0,S,apply177([printout=[]|S0],S,Minstructs))) -->
+ [begin], instructs177(Minstructs), [end].
+
+instructs177(Minstr) --> instr177(Minstr).
+instructs177(fun(S0,S,
+ (apply177(S0,S1,Minstr),
+ apply177(S1,S,Minstructs))))
+ -->
+ instr177(Minstr), instructs177(Minstructs).
+
+instr177(Massign) --> assign177(Massign).
+instr177(fun(S0,[printout = L1|S1],
+ (memb(X = V,S0),
+ del(printout = L0,S0,S1),
+ conc(L0,[V],L1))))
+ -->
+ [print(X)].
+instr177(fun(S0,S,
+ loop177(S0,Mcond,Minstructs,S)))
+ -->
+ [while], cond177(Mcond), [do,begin], instructs177(Minstructs), [end].
+instr177(fun(S0, S,
+ (apply177(S0, true, Mcond), !,
+ apply177(S0, S, MinstructsA)
+ ;
+ apply177(S0, S, MinstructsB))))
+ -->
+ [if], cond177(Mcond), [then], instructs177(MinstructsA), [else], instructs177(MinstructsB), [end].
+
+assign177(fun(S0,[X = Value|S1],
+ (apply177(S0,Value,Mexpr),
+ del(X = _,S0,S1))))
+ -->
+ var177(X), [:=], expr177(Mexpr).
+
+cond177( fun( S, TruthVal,
+ (apply177(S,Val1,ME1),
+ apply177(S,Val2,ME2),
+ (Val1 < Val2,!,TruthVal = true ; TruthVal = false))))
+ -->
+ expr177(ME1), [<], expr177(ME2).
+
+var177(X) --> [X], {atom(X)}.
+
+expr177(fun(S,Value,eval(Expr,S,Value))) -->
+ [Expr].
+
+apply177(In, Out, fun(In, Out, Goals)) :-
+ call(Goals).
+
+loop177( State0, Mcond, _, State0) :-
+ apply177( State0, false, Mcond), !.
+loop177( S0, Mcond, MBody, S) :-
+ copy_term( MBody, MBodyCopy),
+ apply177( S0, S1, MBody),
+ loop177( S1, Mcond, MBodyCopy, S).
+
+eval177( N, _, N) :-
+ number177( N), !.
+eval177( X, State, Val) :- % A program variable
+ atom( X), !,
+ memb( X = Val, State).
+eval177( E1 + E2, State, Val) :- !,
+ eval177( E1, State, V1),
+ eval177( E2, State, V2),
+ Val is V1 + V2.
+eval177( E1 - E2, State, Val) :- !,
+ eval177( E1, State, V1),
+ eval177( E2, State, V2),
+ Val is V1 - V2.
+eval177( E1 * E2, State, Val) :- !,
+ eval177( E1, State, V1),
+ eval177( E2, State, V2),
+ Val is V1 * V2.
+eval177( E1 / E2, State, Val) :- !,
+ eval177( E1, State, V1),
+ eval177( E2, State, V2),
+ Val is V1 / V2.
+'''
diff --git a/prolog/problems/denotational_semantics/algol_if_3/en.py b/prolog/problems/denotational_semantics/algol_if_3/en.py
new file mode 100644
index 0000000..6bc0cb8
--- /dev/null
+++ b/prolog/problems/denotational_semantics/algol_if_3/en.py
@@ -0,0 +1,20 @@
+id = 177
+name = 'algol_if/3'
+slug = 'interpreter for mini-algol with if-statement'
+
+description = '''\
+<p>Extend the given DCG for mini-algol to support the if-statement. You can assume that both branches are present in every if-statement. Example:</p>
+<pre>
+ % if a < b then
+ % print(a)
+ % else
+ % print(b)
+ % end
+ ?- _Program = [begin,if,a,<,b,then,print(a),else,print(b),end,end],
+ algol_if(_F, _Program, []),
+ apply([a=3,b=5], Output, _F).
+ Output = [a=3,b=5,printout=[3]].
+</pre>
+'''
+
+hint = {}