summaryrefslogtreecommitdiff
path: root/prolog/problems/denotational_semantics/algol_3/common.py
blob: d7607c4720ddb7be33ddbc42b43357d33be99094 (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
# coding=utf-8

id = 176
number = 83
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.
'''