:- module(_,_).
% :- use_package(sr/bfall).

%% * Parsing: using difference lists
%    -------------------------------

% Reminder - difference lists:
% ?- L = [t,h,e|T], T = [' ',p,l,a,n,e|T2].
% We can add at the end by instantiating T, then T2, etc..

myphrase(P,C) :-
    % Article is beginning of phrase, rest is beginning of spaces
    article(P,CA), spaces(CA,CS1),
    noun(CS1,CN),  spaces(CN,CS2),
    verb(CS2,C).

article([a|T],T).
article([t,h,e|T],T).

spaces([' ' | T],T).
spaces([' ' | Y],T) :- 
    spaces(Y,T).

noun([c,a,r | T],T).
noun([p,l,a,n,e | T],T).

verb([f,l,i,e,s | T],T).
verb([d,r,i,v,e,s | T],T).

% Try:

% ?- myphrase([t,h,e,' ',p,l,a,n,e,' ',f,l,i,e,s|T], T).
% ?- myphrase([t,h,e,' ',p,l,a,n,e|T], T).

% We can also call with a closed list: 
% (Better behavior in case of failure)
% ?- myphrase([t,h,e,' ',p,l,a,n,e,' ',f,l,i,e,s], []).
% ?- myphrase([t,h,e,' ',p,l,a,n,e], []).

% And we can generate both:
% ?- myphrase(X, T).
% ?- myphrase(X, []).


% Try also uncommenting :- use_pakage(sr/bfall).
% above and, again:
% ?- myphrase(X, []).
