diff example @ 0:cfb7c6b24319

Initial revision
author kono
date Thu, 30 Aug 2007 14:57:44 +0900
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example	Thu Aug 30 14:57:44 2007 +0900
@@ -0,0 +1,304 @@
+%%%%%%%
+%
+%    Temporal Logic Programming Language Tokio		
+%
+%				Fri Jun  6 1986
+%				S.Kono
+%				The Faculty of Engineering
+%				The University of Tokyo
+%				a83793@tansei.u-tokyo.csnet
+%%%%%%%
+%	4.1  simple examples
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%   No.0	always operator   #
+%		length operator
+%
+%   1   1   1   1   1   1
+%   |---|---|---|---|---|---->
+%  t0  t1  t2  t3  t4  t5
+
+t0 :- #write(1),length(5).
+
+
+%   No.1	chop operator
+%		
+%   0   0   0   0
+%               1   1   1   1   1   1
+%   |---|---|---|---|---|---|---|---|---->
+%   |--- I0 ----|------- I1 --------|
+%   |------------ I ----------------|
+
+t1 :- #write(0),length(3) && #write(1),length(5).
+
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%   No.2	fin and keep
+%		
+%   0   0   0
+%               1
+%               2   2   2   2   2   2
+%                                   3
+%   |---|---|---|---|---|---|---|---|---->
+
+t2 :- keep(write(0)), fin(write(1)), length(3)
+	&& #write(2), fin(write(3)), length(5).
+
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%   No.3	next operator
+%		
+%   1   2   3   4   5
+%   |---|---|---|---|---|---->
+
+t3 :- length(5), I = 1, counter(I), #write(I).
+
+    counter(I) :- keep( @I = I+1 ).
+
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%   No.4	stable operator
+%		
+%   2   2   2   2   2   2
+%   |---|---|---|---|---|---->
+
+t4 :- length(5), I = 2, stable(I), #write(I).
+
+% stable(I) :- keep( @I = I ).    (defined internally)
+
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%   No.5	temporal assignment
+%		
+% A 0           1       0
+%   |---|---|---|---|---|---->
+% B 1           0       1
+%   |---|---|---|---|---|---->
+
+t5 :- A = 0, B = 1, 
+	( A <- B, B <- A, length(3) &&
+	  A <- B, B <- A, length(2) && true ),
+     #write((A,B)).
+
+%  A <- B :- C = B, stable(C), fin( A = C ).
+%     (defined internally)
+%
+% B 0   ?  ...  ?   ? 
+%   |---|--   --|---|---->
+% C 0   0  ...  0   0
+%   |---|--   --|---|---->
+% A ?   ?  ...  ?   0
+%   |---|--   --|---|---->
+
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%   No.6	interval join 
+%		
+
+t6 :-	length(8),
+	N=3,(N gets N+1, halt( N=5 ) && stable(N)),
+	M=0,(M gets M+1, fin( M=6 )  && stable(M)),
+	#write((N,M)).
+
+% A gets B :- keep(@A=B).
+
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%   No.7	back track to the past
+%		
+ 
+t7:-	length(5),
+	fin(M=N),
+	N=3,(N gets N+1 && stable(N)),
+	M=0,(M gets M+1 && stable(M)),
+	#write((N,M)).
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%   No.8	correct chop
+%	        Not so important.... but	
+%               Chop becomes slow in correct execution
+ 
+a(b):-true.
+b(c):-true.
+c(X) :- a(X) & @ b(X).
+
+t8 :- length(3),c(X),#write(X).
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%  No.9 and No. 10 projection
+%  Simple example of projection.   Ben Moszkowski.   Updated 19 Aug 85.
+%
+%  changing unit of time using projection
+%    length(2) proj body(I,J)
+% 
+%   |--|--|--|--|--|--|--|--| (skip,stable(I),stable(J) & skip)
+% I 0     1     2     3     4 
+%   |-----|-----|-----|-----| body(I,J)
+
+body(I,J) :- 
+    I=0,I gets I+1,J=0,J gets J+I,halt(I=4).
+t9 :-   (skip,stable(I),stable(J) & skip) proj body(I,J),
+	#((write('I='),write(I),write(' J='),write(J))).
+
+%   |--|--|--|--|--|--|--|--|--|--|  (length(I),stable(I),stable(J)& skip)
+% I 0  1     2        3           4
+%   |--|-----|--------|-----------|  body(I,J)
+
+t10:- (length(I),stable(I),stable(J)& skip) proj body(I,J),
+	#((write('I='),write(I),write(' J='),write(J))).
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%  No.11 prefix
+%  Terminate an interval early  
+
+t11:-A=1,prefix((A gets 2*A,length(10))),halt(A=16),#write('I='),#write(A).
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% 
+% 4.2  two way description of Tokio
+% 1) algorithm description using "chop operator"
+
+magasync(A,B,Res):-
+	(if A < 0 
+	   then Aab <- - A
+	   else Aab <- A ),
+	(if B < 0 
+	   then Bab <- - B
+	   else Bab <- B )
+    &&
+	(if Aab > Bab 
+	   then G <- Aab, L <- Bab
+	   else G <- Bab, L <- Aab)
+    &&
+     	Sqs <- G * 7 / 8 + L / 2 , G <- G
+    &&
+	(if G > Sqs 
+	   then Res <- G
+	   else Res <- Sqs).
+
+% 2) "Always operator" based description
+
+/* data flow calculation */
+
+magasync_dataflow(A,B,Res):-
+	Aab = 0, Bab = 0, G = 0, Res = 0,
+	L = 0, Sqs = 0, G1 = 0,  % initialize 
+	#abs_unit(A,Aab),
+	#abs_unit(B,Bab),
+	#maxmin(Aab,Bab,G,L),
+	#calc(G,L,Sqs),
+	#delay(G,G1),
+	#result(G1,Sqs,Res).
+
+delay(G,G1):-
+	@G1 = G.
+
+abs_unit(I,O):-
+	if I < 0 then @O = -I else @O = I .
+
+maxmin(I1,I2,O1,O2):-
+	if I1 > I2 then (@O1 = I1, @O2 = I2)
+        	   else (@O1 = I2, @O2 = I1) .
+
+calc(I1,I2,O):- 
+	@O = I1 * 7 / 8 + I2 / 2 .
+	
+result(I1,I2,O):-
+	if I1 > I2 then @O = I1 else @O = I2 .
+
+
+mag1:- A=5,B=6,
+	magasync(A,B,Res), write_fk([A,B,Res]).
+
+mag2:- Va = [1,2,3,4,5,6,7], Vb = [1,2,3,4,5,6,7],
+	input_data(A,Va),input_data(B,Vb),
+	magasync_dataflow(A,B,Res), write_fk([A,B,Res]).
+
+write_fk(X) :- keep(write(X)),fin(write(X)).
+write_fk(A) :- fin(write(A)),keep(write(A)).
+
+input_data(0,[]) :- !,empty.
+input_data(V,[H|T]):-
+	V = H,
+	@T = T,
+	@input_data(V,T).
+
+
+%___________________________
+%4.3  pipeline merge sorter
+
+
+sorter :- Strdata = [10,20,5,100,1,6,2,3],
+    datagen(Strdata,Data),
+    pipe(Data,Out),
+    length(18),
+    #write(Out).
+
+        % Data Generator
+
+datagen([],[]):-true.
+datagen([H|T],Out) :-
+    Out = [H],
+    @T = T, @datagen(T,Out).
+
+        % Pipeline Merge Sorter
+
+pipe(I0,Out) :-
+    I1 = [], I2 = [], Out = [],
+    proc_start(I0,I1, 2,1),
+    proc_start(I1,I2, 4,2),
+    proc_start(I2,Out,8,4).
+
+    % Processor Unit
+
+proc_start(I,O,P,PP) :-
+    X = [], Y = [], Z = [], T = 1,
+    #proc(I,O,X,Y,Z,T,P,PP).
+
+proc(I,O,X,Y,Z,T,_P,_PP) :- X=[],Y=[],I=[],!,
+    @X=X, @Y=Y, @Z=Z, @O=[], @T=1.
+proc(I,O,X,Y,Z,T,P,PP) :-
+     load(I,O,X,Y,Yn,Z,Zn,T,P,PP),
+    merge(I,O,X,Y,Yn,Z,Zn,T,P,PP).
+
+load(I,_O,_X,Y,Yn,Z,Zn,T,_P,PP) :- T=<PP, !,
+    append(Z,I,Zn), @Z=Zn, Yn=Y,
+    @T=T+1.
+load(I,_O,_X,Y,Yn,Z,_Zn,T,P,_PP) :- 
+    append(Y,I,Yn), @Z=[],
+    (if T<P then @T=T+1 else @T=1).
+
+merge(_I,O,X,Y,Yn,_Z,Zn,T,_P,PP) :-X=[],Yn=[],!,
+    @O=[], @Y=Yn,
+    (if T=PP then @X=Zn else @X=X).
+merge(_I,O,X,Y,Yn,_Z,Zn,T,_P,PP) :- X=[A|L],Yn=[],!,
+    @O=[A], @Y=Yn,
+    (if T=PP then @X=Zn else @X=L).
+merge(_I,O,X,Y,Yn,_Z,Zn,T,_P,PP) :-X=[],Yn=[B|N],!,
+    @O=[B], @Y=N,
+    (if T=PP then @X=Zn else @X=X).
+merge(_I,O,X,Y,Yn,_Z,_Zn,_T,_P,_PP) :-X=[A|L],Yn=[B|N],!,
+    (if A<B then
+	@O=[A], @X=L, @Y=Yn
+    else
+	@O=[B], @Y=N, @X=X).
+
+append(Nil,L,L1) :- Nil=[],L=L1.
+append(X,L,Y) :-[H|T]=X,[H1|M]=Y,
+     H=H1,append(T,L,M).
+
+test :- t0 & @
+	t1 & @
+	t2 & @
+	t3 & @
+	t4 & @
+	t5 & @
+	t6 & @
+	t7 & @
+	t8 & @
+%	t9 & @
+%	t10 & @
+%	t11 & @
+	mag1 & @
+	mag2 & @
+	sorter.