-
Notifications
You must be signed in to change notification settings - Fork 1
/
rules.pl
214 lines (121 loc) · 5.51 KB
/
rules.pl
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
/* this is the basic rules and the syntactic categories for the small language Mini.
The evaluation rules are described below:
bigstep_e: computes integer expressions for a given substitution and set of functions
Profile: env(PROG,S), e -> N
bigstep_i: compute an instruction
Profile: PROG, i, S -> S
bigstep_p: compute a sequence of instructions
Profile: PROG, p, S -> S
*/
/*
semantics of expressions
========================
syntax of expressions:
OPS: _ + _, _ * _, _ - _, _ / _
FCT: name( _), name( _, _)
VAR: v
exemple:
bigstep_e(ENV,E,N)
*/
bigstep_e(ENV,E + EE,NS):- bigstep_e(ENV,E,N),bigstep_e(ENV,EE,NN),NS is N + NN.
bigstep_e(ENV,E * EE,NS):- bigstep_e(ENV,E,N),bigstep_e(ENV,EE,NN),NS is N * NN.
bigstep_e(ENV,E / EE,NS):- bigstep_e(ENV,E,N),bigstep_e(ENV,EE,NN),NS is N / NN.
bigstep_e(ENV,E - EE,NS):- bigstep_e(ENV,E,N),bigstep_e(ENV,EE,NN),NS is N - NN.
bigstep_e(ENV,N,N):-integer(N).
bigstep_e(env(PROG,S),V,N):-atom(V),subsextract(S,V,N). % detect variable names as atoms
% functions without parameters are not allowed
% detect function calls as functors
bigstep_e(env(PROG,S),FCTCALL,N):-FCTCALL =..[NAME|PARAM],length(PARAM,M),M>0,
findfunction(PROG,NAME,func(NAMEPARAMFORMAL,FBODY,ERETURN)),
NAMEPARAMFORMAL =..[NAME|PARAMFORMAL],
bigstep_le(env(PROG,S),PARAM,LN),
bindparam(LN,PARAMFORMAL,SS),
bigstep_p(PROG,FBODY,SS,SSS),
bigstep_e(env(PROG,SSS),ERETURN,N).
/* evaluation of list of expressions*/
bigstep_le(env(PROG,S),[],[]).
bigstep_le(env(PROG,S),[HP|LP],[N|LN]):-bigstep_e(env(PROG,S),HP,N),bigstep_le(env(PROG,S),LP,LN).
/* operations on substitutions*/
% find the value assigned to a given variable
/* subsextract([],V,0):- fail */
subsextract([(V,N)|L],V,N).
subsextract([(V,NN)|L],VV,N):-V \== VV, subsextract(L,VV,N).
% add a value to a variable in the substitution (expected properties such as unicity and non ordering are guaranteed)
subsadd([],V,N,[(V,N)]).
subsadd([(V,NN)|L],V,N,[(V,N)|L]).
subsadd([(V,NN)|L],VV,N,[(V,NN)|LL]):-V \== VV, subsadd(L,VV,N,LL).
/* function manipulation*/
/* select a function by its unique name*/
findfunction([func(NAMEPARAMFORMAL,FBODY,ERETURN)|PROG],NAME,func(NAMEPARAMFORMAL,FBODY,ERETURN)):-NAMEPARAMFORMAL =..[NAME|PARAM].
findfunction([func(NAMEPARAMFORMAL,_,_)|PROG],NAME,FCT):- NAMEPARAMFORMAL =..[NAMED|PARAM],NAMED\==NAME,findfunction(PROG,NAME,FCT).
/* assign actual parameters to formal ones and provide a substitution*/
bindparam([],[],[]).
bindparam([HLN|LN],[HPARAM|PARAMFORMAL],SS):-bindparam(LN,PARAMFORMAL,S),subsadd(S,HPARAM,HLN,SS).
/* semantics of programs as list of instructions*/
bigstep_p(PROG,(I;P),S,SSS):-!, % cut for parsing only once single instruction
bigstep_i(PROG,I,S,SS),bigstep_p(PROG,P,SS,SSS).
bigstep_p(PROG,I,S,SS):-bigstep_i(PROG,I,S,SS).
/* semantics of the different instructions*/
bigstep_i(PROG,if_then_else(COND,P,PP),S,SS):-
bigstep_c(env(PROG,S),COND),
bigstep_p(PROG,P,S,SS).
bigstep_i(PROG,if_then_else(COND,P,PP),S,SS):-
not(bigstep_c(env(PROG,S),COND)),
bigstep_p(PROG,PP,S,SS).
bigstep_i(PROG,while(COND,P),S,SSS):-
bigstep_c(env(PROG,S),COND),
bigstep_p(PROG,P,S,SS),
bigstep_p(PROG,while(COND,P),SS,SSS).
bigstep_i(PROG,while(COND,P),S,S):-
not(bigstep_c(env(PROG,S),COND)).
bigstep_i(PROG,(V:=E),S,SS):-
bigstep_e(env(PROG,S),E,N),subsadd(S,V,N,SS).
/* evaluation of conditions */
bigstep_c(ENV,E < EE):- bigstep_e(ENV,E,N),bigstep_e(ENV,EE,NN),N < NN.
bigstep_c(ENV,E > EE):- bigstep_e(ENV,E,N),bigstep_e(ENV,EE,NN),N > NN.
bigstep_c(ENV,E = EE):- bigstep_e(ENV,E,N),bigstep_e(ENV,EE,NN),N = NN.
bigstep_c(ENV,E \== EE):- bigstep_e(ENV,E,N),bigstep_e(ENV,EE,NN),N \== NN.
/* encoded examples for simplicity*/
deffun(sqrt,func(sqrt(x),y:=1;while(sq(y)<x,y:=y+1),y)).
deffun(sq,func(sq(x),y:=1,x*x)).
loadfun([],[]).
loadfun([H|L],[HF|TL]):-deffun(H,HF),loadfun(L,TL).
/*
tests
?- bigstep_e(env([],[(x,20),(y,2)]),(1+y*3-4*7+x),N).
?- bigstep_e(env([],[(x,20)]),(1+2*3-4*7+x),N).
?- bigstep_c(env([],[(x,20),(y,2)]),(1+y*3-4*7+x<0)).
Yes
?- bigstep_c(env([],[(x,20),(y,2)]),(1+y*3-4*7+x>0)).
No
?- bigstep_c(env([],[(x,20),(y,2)]),(1+y*3-4*7+x\==0)).
Yes
?- bigstep_i([],(z:=1),[(x,20),(y,2)],S).
S = [ (x, 20), (y, 2), (z, 1)] ;
No
?- bigstep_i([],(z:=1+2*6+x),[(x,20),(y,2)],S).
S = [ (x, 20), (y, 2), (z, 33)] ;
?- bigstep_i([],(x:=1+2*6+x),[(x,20),(y,2)],S).
S = [ (x, 33), (y, 2)] ;
No
?- bigstep_p([],(x:=y+2*6+x;y:=x),[(x,20),(y,2)],S).
S = [ (x, 34), (y, 34)] ;
?- bigstep_p([],(if_then_else( x>0 ,x:=y+2*6+x;y:=x, x:=3)),[(x,20),(y,2)],S).
S = [ (x, 34), (y, 34)] ;
No
?- bigstep_p([],(if_then_else( x<0 ,x:=y+2*6+x;y:=x, x:=3)),[(x,20),(y,2)],S).
S = [ (x, 3), (y, 2)] ;
?- bigstep_p([],(x:=655360;y:=1;while( y*y<x ,y:=y+1)),[],S).
S = [ (x, 655360), (y, 810)] ;
?- bigstep_p([func(sqrt(x),y:=1;while(y*y<x,y:=y+1),y)],x:=sqrt(65536),[],S).
S = [ (x, 256)] ;
No
?- bigstep_p([func(sqrt(x),y:=1;while(y*y<x,y:=y+1),y)],x:=sqrt(36),[],S).
S = [ (x, 6)] ;
No
?- loadfun([sqrt,sq],CXT),bigstep_p(CXT,x:=sqrt(65536),[],R).
CXT = [func(sqrt(x), (y:=1;while(sq(y)<x, y:=y+1)), y), func(sq(x), y:=1, x*x)]
R = [ (x, 256)] ;
No
consult('InterpretProlog/rules.pl').
*/