/***************************************
	$@F|K\8l<-=q;2>H%W%m%0%i%`(J

	ICOT $@:4LnJ8K!MQ(J

			by TOKUNAGA, T.
			90/6/1
***************************************/

% dictionary
dictionary(Cat,Arg,T,X,Y) :-
    '**wf_dict'(_,_,X,_,_),!,
    '**wf_dict'(Cat,Arg,X,Y,T).
dictionary(_,_,_,Xx,Yx) :-
    '**index'(Xx,Xs),
    '*dict'(trie,Cat,Arg,T,Xs,Ys),
    '**index'(Yx,Ys),
    '*makeword'(T,Tree),
    assertz('**wf_dict'(Cat,Arg,Xx,Yx,[Cat|Tree])),
    fail.
dictionary(Cat,Arg,T,X,Y) :-
    '**wf_dict'(Cat,Arg,X,Y,T).

% dict
'*dict'(TRIE,Cat,Info,T0,Xs,Zs) :-
   '*fetch_dict'(TRIE,ARGs,Tries,T0,T1,Xs,Ys),
  ( '*one_of'(Tries,Trie),
    '*dict'(Trie,Cat,Info,T1,Ys,Zs)
    ;
    '*one_of'(ARGs,[Cat,Info,_]),
    Ys = Zs,
    T1 = []
  ).

% fetch_dict
'*fetch_dict'(trie,ARGs,TRIEs,[W|Ts],Ts,[W|Ws],Ws) :- !,
    trie(W,ARGs,TRIEs).
'*fetch_dict'([W,ARGs,TRIEs],ARGs,TRIEs,[W|Ts],Ts,[W|Ws],Ws) :- !.
'*fetch_dict'([[Goal|Info],ARGs,TRIEs],ARGs,TRIEs,[T|Ts],Ts,Xs,Ys) :-
    '**index'(Xx,Xs),
    goal(Goal,Info,T,Xx,Yx),
    '**index'(Yx,Ys).
'*fetch_dict'([program(PRGs),ARGs,TRIEs],ARGs,TRIEs,Ts,Ts,Ws,Ws) :- !,
    '*calls'(PRGs).

% calls
'*calls'([]) :- !.
'*calls'([X|R]) :-
    call(X),
    '*calls'(R).

% makeword
'*makeword'([],[]) :- !.
'*makeword'(Xs,[Word|Zs]) :-
    '*extract_word'(Xs,Ws,Ys),
  ( Ws == [] ->
	Xs = [Word|_]			% $@J8K!%+%F%4%j$N9=J8LZ$N>l9g(J
	;
	'*listToAtom'(Ws,Word) ),	% $@C18l$N>l9g(J
    '*makeword'(Ys,Zs).

% extract_word
'*extract_word'([],[],[]):- !.
'*extract_word'([[_|_]|Ys],[],Ys) :- !.
'*extract_word'([X|Xs],[X|Ys],Zs) :- !,
    '*extract_word'(Xs,Ys,Zs).
