summaryrefslogtreecommitdiff
path: root/prolog/problems/algol/algol_for_3/common.py
blob: 38d5d45f63fa691146e46bb3ae873851993bc72e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
from operator import itemgetter
import socket

import prolog.engine
import prolog.util
from server.hints import Hint, HintPopup

id = 178
number = 30
visible = True
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.
'''

initial = '''\
% program
algol_for(fun(S0, S, apply([printout=[]|S0], S, Minstructs))) -->
    [begin], instructs(Minstructs), [end].

% sequence of instructions
instructs(Minstr) --> instr(Minstr).
instructs(fun(S0,S,
    (apply(S0,S1,Minstr),
     apply(S1,S,Minstructs))))
    -->
    instr(Minstr), instructs(Minstructs).

% statement (instruction)
instr(Massign) --> assign(Massign).

% put the current value of a variable into the output list
instr(fun(S0, [printout = L1|S1],
    (memb(X = V,S0),
     del(printout = L0, S0, S1),
     conc(L0, [V], L1))))
    -->
    [print(X)].

% while-loop
instr(fun(S0, S,
    loop(S0, Mcond, Minstructs, S)))
    -->
    [while], cond(Mcond), [do, begin], instructs(Minstructs), [end].

% assignment statement
assign(fun(S0, [X = Value|S1],
    (apply(S0, Value, Mexpr),
     del(X = _, S0, S1))))
    -->
    var(X), [':='], expr(Mexpr).

% conditional expression
cond(fun(S, TruthVal,
    (apply(S, Val1, ME1),
     apply(S, Val2, ME2),
     (Val1 < Val2, !, TruthVal = true ; TruthVal = false))))
    -->
    expr(ME1), ['<'], expr(ME2).

% variable
var(X) --> [X], { atom(X) }.

% expression
expr(fun(S, Value, eval(Expr, S, Value))) -->
    [Expr].   % Expr is a Prolog arithmetic expression, e.g. x + 2*y + 3

% helper predicates
% function call
apply(In, Out, fun(In, Out, Goals)):-
    call(Goals).

% loop
loop(State0, Mcond, _, State0) :-
    apply(State0, false, Mcond), !. % condition is false
loop(S0, Mcond, MBody, S) :-
    copy_term(MBody, MBodyCopy),    % copy goals
    apply(S0, S1, MBody),           % run loop body
    loop(S1, Mcond, MBodyCopy, S).  % next iteration

% expression evaluation
eval(N, _, N) :-
    number(N), !.
eval(X, State, Val) :-  % a program variable
    atom(X), !,
    memb(X = Val, State).
eval(E1 + E2, State, Val) :- !,
    eval(E1, State, V1),
    eval(E2, State, V2),
    Val is V1 + V2.
eval(E1 - E2, State, Val) :- !,
    eval(E1, State, V1),
    eval(E2, State, V2),
    Val is V1 - V2.
eval(E1 * E2, State, Val) :- !,
    eval(E1, State, V1),
    eval(E2, State, V2),
    Val is V1 * V2.
eval(E1 / E2, State, Val) :- !,
    eval(E1, State, V1),
    eval(E2, State, V2),
    Val is V1 / V2.
'''

test_cases = [
    ('findall(W, (W = [_], algol_for(_, W, [])), Words), Words == []',
        [{}]),
    ('algol_for(F, [begin,for,a,:=,0,&,a,<,9,&,a,:=,a+1,do,begin,print(a),a,:=,a+1,end,end], []), \
                   apply([a=0], Out, F), msort(Out, OutSorted), \
		           OutSorted == [a=10,printout=[0,2,4,6,8]]',
        [{}]),
    ('algol_for(F, [begin,for,a,:=,1,&,a,<,32,&,a,:=,a*2,do,begin,print(a),end,end], []), \
                   apply([a=0], Out, F), msort(Out, OutSorted), \
		           OutSorted == [a=32,printout=[1,2,4,8,16]]',
        [{}]),
]

def test(code, aux_code):
    n_correct = 0
    engine_id = None
    try:
        engine_id, output = prolog.engine.create(code=code+aux_code, timeout=1.0)
        if engine_id is not None and 'error' not in map(itemgetter(0), output):
            # Engine successfully created, and no syntax error in program.
            for query, answers in test_cases:
                if prolog.engine.check_answers(engine_id, query=query, answers=answers, timeout=1.0):
                    n_correct += 1
    except socket.timeout:
        pass
    finally:
        if engine_id:
            prolog.engine.destroy(engine_id)

    hints = [{'id': 'test_results', 'args': {'passed': n_correct, 'total': len(test_cases)}}]
    return n_correct, len(test_cases), hints