view td.pl @ 1:09586da5afa8 kono r1

Tokio compiler on Prolog.
author kono
date Thu, 30 Aug 2007 14:57:44 +0900
parents cfb7c6b24319
children 61743469ee56
line wrap: on
line source

/*
 Copyright (C) 1988,2005, Shinji Kono 
 Everyone is permitted to copy and distribute verbatim copies
 of this license, but changing it is not allowed.  You can also
 use this wording to make the terms for other programs.

 send your comments to kono@ie.u-ryukyu.ac.jp
*/
 
 
tokiodebug_on :- recorded(tokiodebug, on, _), !. 
tokiodebug_on :- recorda(tokiodebug, on, _),  
     write('Tokio debug mode switched on.'), nl. 
 
tokiodebug_off :- recorded(tokiodebug, _, R), !, 
    erase(R), write('Tokio debug mode  switched off.'), nl. 
tokiodebug_off. 
 
'tokiodebugon?' :-  
     recorded(tokiodebug, on, _), !, uhihi. 
 
uhihi :- 
    recorded(tokiotraceat, _, _), !, fail. 
uhihi. 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Clocked Stepping
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
r_tokioDebug(Q,Now) :- 
	r_skip(S),
	r_tokioDebug(S,Q,Now).

r_tokioDebug(S,_Q,Now) :- S > Now,!.
r_tokioDebug(S,_Q,Now) :- 0<S,S < Now,!,fail.
r_tokioDebug(_,Q,Now) :- 
	abolish(r_skip,1),
	assert(r_skip(-1)),!,
	r_select(Q,Now),!,r_skip(S),(S>Now;S= -1).

r_select(Q,Now) :- repeat,nl,write('Tokio Trace t'),write(Now),write(:),
	read(Key),r_menu(Key,Key1),
	r_tokioDebug_menu(Key1,Q,Now),!.

r_menu(n,next):-!.
r_menu(d,dump):-!.
r_menu(s(N),skip(N)):-!.
r_menu(+(N),skip(N)):-!.
r_menu(a,abort):-!.
r_menu(b,break):-!.
r_menu(v(N),value(N)):-!.
r_menu(*(N),value(N)):-!.
r_menu(q,queue):-!.
r_menu(t,trace):-!.
r_menu(N,skip(N)):-number(N),!.
r_menu(h,help):-!.
r_menu(?,help):-!.
r_menu(V,V).

r_tokioDebug_menu(help,_,_) :- 
	write('help/h/?             '),put(9),	write(' print this'),nl,
	write('next/n               '),put(9),	write(' skip to next clock'),nl,
	write('dump/d               '),put(9),	write(' dump current static value'),nl,
	write('all                  '),put(9),	write(' all history of static value'),nl,
	write('save(file)           '),put(9),	write(' save current state'),nl,
	write('skip(n)/s(n)/+-Number'),put(9),write(' goto +-Number clock'),nl,
	write('abort/a              '),put(9),	write(' abort tokio execution'),nl,
	write('break/b              '),put(9),	write(' break to prolog'),nl,
	write('value(Name)/*Name    '),put(9),write(' examine static value'),nl,
	write('queue/q              '),put(9),	write(' show current process queue'),nl,
	write('trace/t              '),put(9),	write(' enter prolog tracer'),
	nl,fail.
r_tokioDebug_menu(dump,_,_) :- 
	recorded(r_static,Name,_),record1(Name,(Name,Value,Time),_Ref),
	write(at),write(Time),write(': '),put(9),
	write(*Name),write( = ),write(Value),nl,fail.
r_tokioDebug_menu(abort,_,_Time):- !,abort.
r_tokioDebug_menu(break,_,_Time):- !,break,fail.
r_tokioDebug_menu(next,_,_Time):- !.
r_tokioDebug_menu(trace,_,_Time):- !,trace.
r_tokioDebug_menu(all,_,Time) :- % recorded(time,Now,_),!,
	r_inc_time(Now,Time), assert(r_fififi),
	recorded(r_static,Name,_),recorded(Name,(Name,Value,Now),_Ref),
	(r_fififi,write(Now),write(': '),retract(r_fififi),fail; put(9)),
	write(*),write(Name),write(' = '),write(Value),
	nl,fail.
r_tokioDebug_menu(queue,Q,_Time) :-
	numbervars(Q,0,_),r_save_queue(Q),!,fail.
r_tokioDebug_menu(value(Name),_Q,_) :-
	recorded(Name,(Name,Value,Time),_Ref),
	write(*),write(Name),write(' = '),write(Value),
	write(' at: '),write(Time),nl,fail.
r_tokioDebug_menu(save(File),Q,_Time) :-
	tell(File),
	write((:- init_static)),write('.'),nl,
	r_save_static,numbervars(Q,0,_),
	r_save_queue(Q),
	told,!,fail.
r_tokioDebug_menu(skip(N),_,Time) :- !,
	M is N+Time,abolish(r_skip,1),
	assert(r_skip(M)).
r_tokioDebug_menu(_,_,_Time) :- !,fail.

r_inc_time(X,Now) :- r_inc_time(X,Now,0).
r_inc_time(N,_Now,N).
r_inc_time(N,Now,M) :- M1 is M+1,M1 =< Now, r_inc_time(N,Now,M1).

r_save_static :-
	recorded(r_static,Name,_),recorded(Name,(Value,Time),_Ref),
	write_term((:- recordz(Name,(Value,Time),_)),[numbervars(true),quoted(true)]),
	% writeq((:- recordz(Name,(Value,Time),_))),
	write('.'),nl,fail.
r_save_static.

r_save_queue(t(Empty,X,Fin,Now,F,K,Next,Futures,True)) :-
	nl,write((:- abolish(restart,1),recorda(time,Now,_))),write('.'),
	nl,
	OO = ( restart
    :- r_solve_t(X,Fin,Now,['$t'(Next,F,K,'$'(Fin,Now,Empty))|Futures],
		    ['$t'(true,true,true,'$'(Fin,Now,Empty))|True]) ),
        % writeq(OO),
        write_term(OO,[numbervars(true),quoted(true)]),
	write('.'),nl.

restart(File) :- [-File],restart.

record1(Name,Value,Ref) :- recorded(Name,Value,Ref),!.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Clause Tracer (N.Y.I)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
tokiospied(G) :-  
    functor(G, F, A), A2 is A-2, recorded(tokiospy, F/A2, _), !. 
 
tokiofailed1(_G) :- recorded(tokiofail, _, _), !. 
tokiofailed(G) :-  
     functor(G, F, A), A2 is A-2, recorded(tokiofail, F/A2, _), !. 
 
% tokiodebug :- recorded(tokiospy, _, R), erase(R), fail. 
% tokiodebug :- recorded(tokiofail, _, R), erase(R), fail. 
tokiodebug :- tokiodebug_on, 
    recorda(tokiospy, _, _), recorda(tokiofail, _, _), 
    write('All computation will be traced.'), ttynl. 
 
% tokionodebug :- recorded(tokiospy, _, R), erase(R), fail. 
% tokionodebug :- recorded(tokiofail, _, R), erase(R), fail. 
% tokionodebug :- recorded(tokiotraceat, _, R), erase(R), fail. 
tokionodebug :- tokiodebug_off. 
 
(tokiospy) :- recorded(tokiospy, _, R), erase(R), fail. 
(tokiospy) :- recorded(tokiofail, _, R), erase(R), fail. 
(tokiospy) :- tokiodebug_on, recorda(tokiospy, _, _), 
    write('All Tokio reductions will be traced.'), ttynl. 
 
tokiospy(X) :-  
    tokiodebug_on, 
  ( X=_F/_A, !, recorda(tokiospy, X, _); recorda(tokiospy, X/_, _) ), 
    write('Tokio spy-point placed on '), write(X), put("."), nl. 
 
(tokionospy) :- recorded(tokiospy, _, R), erase(R), fail. 
(tokionospy) :- tokiodebug_off. 
 
tokionospy(F/A) :- 
    recorded(tokiospy, F/A, R), erase(R), 
    write('Tokio spy-point on '), write(F/A), write(' removed.'), nl, fail. 
tokionospy(X) :- 
    recorded(tokiospy, X/_, R), erase(R), 
    write('Tokio spy-point on '), write(X), write(' removed.'), nl, fail. 
tokionospy(_X) :- recorded(tokiospy, _, _), !. 
tokionospy(_) :- tokiodebug_off. 
 
tokiodebugging :- recorded(tokiodebug, on, _), !, 
    write('Tokio debug mode is switched on.'), nl, 
    tokiodebugging1. 
tokiodebugging :- write('Tokio debug mode is switched off.'), nl. 
 
tokiodebugging1 :- 
  ( setof(X, R^recorded(tokiospy, X, R), S), !, 
      ( S=[V|_], var(V), !, write('All Tokio reductions are traced.'); 
	write('Tokio spy-points set on: '), write(S) ), nl; 
    true ). 
 
/* end td */