comparison example @ 0:cfb7c6b24319

Initial revision
author kono
date Thu, 30 Aug 2007 14:57:44 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:cfb7c6b24319
1 %%%%%%%
2 %
3 % Temporal Logic Programming Language Tokio
4 %
5 % Fri Jun 6 1986
6 % S.Kono
7 % The Faculty of Engineering
8 % The University of Tokyo
9 % a83793@tansei.u-tokyo.csnet
10 %%%%%%%
11 % 4.1 simple examples
12
13 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
14 % No.0 always operator #
15 % length operator
16 %
17 % 1 1 1 1 1 1
18 % |---|---|---|---|---|---->
19 % t0 t1 t2 t3 t4 t5
20
21 t0 :- #write(1),length(5).
22
23
24 % No.1 chop operator
25 %
26 % 0 0 0 0
27 % 1 1 1 1 1 1
28 % |---|---|---|---|---|---|---|---|---->
29 % |--- I0 ----|------- I1 --------|
30 % |------------ I ----------------|
31
32 t1 :- #write(0),length(3) && #write(1),length(5).
33
34
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 % No.2 fin and keep
37 %
38 % 0 0 0
39 % 1
40 % 2 2 2 2 2 2
41 % 3
42 % |---|---|---|---|---|---|---|---|---->
43
44 t2 :- keep(write(0)), fin(write(1)), length(3)
45 && #write(2), fin(write(3)), length(5).
46
47
48 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49 % No.3 next operator
50 %
51 % 1 2 3 4 5
52 % |---|---|---|---|---|---->
53
54 t3 :- length(5), I = 1, counter(I), #write(I).
55
56 counter(I) :- keep( @I = I+1 ).
57
58
59 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60 % No.4 stable operator
61 %
62 % 2 2 2 2 2 2
63 % |---|---|---|---|---|---->
64
65 t4 :- length(5), I = 2, stable(I), #write(I).
66
67 % stable(I) :- keep( @I = I ). (defined internally)
68
69
70 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71 % No.5 temporal assignment
72 %
73 % A 0 1 0
74 % |---|---|---|---|---|---->
75 % B 1 0 1
76 % |---|---|---|---|---|---->
77
78 t5 :- A = 0, B = 1,
79 ( A <- B, B <- A, length(3) &&
80 A <- B, B <- A, length(2) && true ),
81 #write((A,B)).
82
83 % A <- B :- C = B, stable(C), fin( A = C ).
84 % (defined internally)
85 %
86 % B 0 ? ... ? ?
87 % |---|-- --|---|---->
88 % C 0 0 ... 0 0
89 % |---|-- --|---|---->
90 % A ? ? ... ? 0
91 % |---|-- --|---|---->
92
93
94 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
95 % No.6 interval join
96 %
97
98 t6 :- length(8),
99 N=3,(N gets N+1, halt( N=5 ) && stable(N)),
100 M=0,(M gets M+1, fin( M=6 ) && stable(M)),
101 #write((N,M)).
102
103 % A gets B :- keep(@A=B).
104
105
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107 % No.7 back track to the past
108 %
109
110 t7:- length(5),
111 fin(M=N),
112 N=3,(N gets N+1 && stable(N)),
113 M=0,(M gets M+1 && stable(M)),
114 #write((N,M)).
115
116 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117 % No.8 correct chop
118 % Not so important.... but
119 % Chop becomes slow in correct execution
120
121 a(b):-true.
122 b(c):-true.
123 c(X) :- a(X) & @ b(X).
124
125 t8 :- length(3),c(X),#write(X).
126
127 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
128 % No.9 and No. 10 projection
129 % Simple example of projection. Ben Moszkowski. Updated 19 Aug 85.
130 %
131 % changing unit of time using projection
132 % length(2) proj body(I,J)
133 %
134 % |--|--|--|--|--|--|--|--| (skip,stable(I),stable(J) & skip)
135 % I 0 1 2 3 4
136 % |-----|-----|-----|-----| body(I,J)
137
138 body(I,J) :-
139 I=0,I gets I+1,J=0,J gets J+I,halt(I=4).
140 t9 :- (skip,stable(I),stable(J) & skip) proj body(I,J),
141 #((write('I='),write(I),write(' J='),write(J))).
142
143 % |--|--|--|--|--|--|--|--|--|--| (length(I),stable(I),stable(J)& skip)
144 % I 0 1 2 3 4
145 % |--|-----|--------|-----------| body(I,J)
146
147 t10:- (length(I),stable(I),stable(J)& skip) proj body(I,J),
148 #((write('I='),write(I),write(' J='),write(J))).
149
150 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151 % No.11 prefix
152 % Terminate an interval early
153
154 t11:-A=1,prefix((A gets 2*A,length(10))),halt(A=16),#write('I='),#write(A).
155
156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157 %
158 % 4.2 two way description of Tokio
159 % 1) algorithm description using "chop operator"
160
161 magasync(A,B,Res):-
162 (if A < 0
163 then Aab <- - A
164 else Aab <- A ),
165 (if B < 0
166 then Bab <- - B
167 else Bab <- B )
168 &&
169 (if Aab > Bab
170 then G <- Aab, L <- Bab
171 else G <- Bab, L <- Aab)
172 &&
173 Sqs <- G * 7 / 8 + L / 2 , G <- G
174 &&
175 (if G > Sqs
176 then Res <- G
177 else Res <- Sqs).
178
179 % 2) "Always operator" based description
180
181 /* data flow calculation */
182
183 magasync_dataflow(A,B,Res):-
184 Aab = 0, Bab = 0, G = 0, Res = 0,
185 L = 0, Sqs = 0, G1 = 0, % initialize
186 #abs_unit(A,Aab),
187 #abs_unit(B,Bab),
188 #maxmin(Aab,Bab,G,L),
189 #calc(G,L,Sqs),
190 #delay(G,G1),
191 #result(G1,Sqs,Res).
192
193 delay(G,G1):-
194 @G1 = G.
195
196 abs_unit(I,O):-
197 if I < 0 then @O = -I else @O = I .
198
199 maxmin(I1,I2,O1,O2):-
200 if I1 > I2 then (@O1 = I1, @O2 = I2)
201 else (@O1 = I2, @O2 = I1) .
202
203 calc(I1,I2,O):-
204 @O = I1 * 7 / 8 + I2 / 2 .
205
206 result(I1,I2,O):-
207 if I1 > I2 then @O = I1 else @O = I2 .
208
209
210 mag1:- A=5,B=6,
211 magasync(A,B,Res), write_fk([A,B,Res]).
212
213 mag2:- Va = [1,2,3,4,5,6,7], Vb = [1,2,3,4,5,6,7],
214 input_data(A,Va),input_data(B,Vb),
215 magasync_dataflow(A,B,Res), write_fk([A,B,Res]).
216
217 write_fk(X) :- keep(write(X)),fin(write(X)).
218 write_fk(A) :- fin(write(A)),keep(write(A)).
219
220 input_data(0,[]) :- !,empty.
221 input_data(V,[H|T]):-
222 V = H,
223 @T = T,
224 @input_data(V,T).
225
226
227 %___________________________
228 %4.3 pipeline merge sorter
229
230
231 sorter :- Strdata = [10,20,5,100,1,6,2,3],
232 datagen(Strdata,Data),
233 pipe(Data,Out),
234 length(18),
235 #write(Out).
236
237 % Data Generator
238
239 datagen([],[]):-true.
240 datagen([H|T],Out) :-
241 Out = [H],
242 @T = T, @datagen(T,Out).
243
244 % Pipeline Merge Sorter
245
246 pipe(I0,Out) :-
247 I1 = [], I2 = [], Out = [],
248 proc_start(I0,I1, 2,1),
249 proc_start(I1,I2, 4,2),
250 proc_start(I2,Out,8,4).
251
252 % Processor Unit
253
254 proc_start(I,O,P,PP) :-
255 X = [], Y = [], Z = [], T = 1,
256 #proc(I,O,X,Y,Z,T,P,PP).
257
258 proc(I,O,X,Y,Z,T,_P,_PP) :- X=[],Y=[],I=[],!,
259 @X=X, @Y=Y, @Z=Z, @O=[], @T=1.
260 proc(I,O,X,Y,Z,T,P,PP) :-
261 load(I,O,X,Y,Yn,Z,Zn,T,P,PP),
262 merge(I,O,X,Y,Yn,Z,Zn,T,P,PP).
263
264 load(I,_O,_X,Y,Yn,Z,Zn,T,_P,PP) :- T=<PP, !,
265 append(Z,I,Zn), @Z=Zn, Yn=Y,
266 @T=T+1.
267 load(I,_O,_X,Y,Yn,Z,_Zn,T,P,_PP) :-
268 append(Y,I,Yn), @Z=[],
269 (if T<P then @T=T+1 else @T=1).
270
271 merge(_I,O,X,Y,Yn,_Z,Zn,T,_P,PP) :-X=[],Yn=[],!,
272 @O=[], @Y=Yn,
273 (if T=PP then @X=Zn else @X=X).
274 merge(_I,O,X,Y,Yn,_Z,Zn,T,_P,PP) :- X=[A|L],Yn=[],!,
275 @O=[A], @Y=Yn,
276 (if T=PP then @X=Zn else @X=L).
277 merge(_I,O,X,Y,Yn,_Z,Zn,T,_P,PP) :-X=[],Yn=[B|N],!,
278 @O=[B], @Y=N,
279 (if T=PP then @X=Zn else @X=X).
280 merge(_I,O,X,Y,Yn,_Z,_Zn,_T,_P,_PP) :-X=[A|L],Yn=[B|N],!,
281 (if A<B then
282 @O=[A], @X=L, @Y=Yn
283 else
284 @O=[B], @Y=N, @X=X).
285
286 append(Nil,L,L1) :- Nil=[],L=L1.
287 append(X,L,Y) :-[H|T]=X,[H1|M]=Y,
288 H=H1,append(T,L,M).
289
290 test :- t0 & @
291 t1 & @
292 t2 & @
293 t3 & @
294 t4 & @
295 t5 & @
296 t6 & @
297 t7 & @
298 t8 & @
299 % t9 & @
300 % t10 & @
301 % t11 & @
302 mag1 & @
303 mag2 & @
304 sorter.