%% -*- prolog -*-

%% sent(S) means S is a list representing a grammatical (english subset) sentence

%% Examples:
%%   sent([joe,saw,sally]).
%%   sent([the,apple,saw,the,pear]).

%% idea!  relations for phrases, put together using append/2.

%% Direct representation.

%% noun phrase:
noun([joe]).
noun([the,apple]).
noun([the,pear]).

%% verbs that do not take direct object
verb1([stopped]).

%% verbs that do (can) take direct object
verb2([saw]).
verb2([hit]).
verb2([ate]).

sent(S) :- append(N, V, S), noun(N), verb1(V).
sent(S) :- append(N, VP, S), append(V, DO, VP), noun(N), verb2(V), noun(DO).


%% represent list as "difference lists"

%% Definition: difference between lists is the list of elements you need to
%% chop off the beginning of the first to get the second.

%% e.g., [a,b,c,d,e,f,g,h] - [f,g,h] = [a,b,c,d,e]
diff(L1,L2,L3) :- 		% difference: L1 - L2 = L3
	append(L3,L2,L1).

%% idea: write above grammar using difference lists.
%% notation: d_ at beginning of relation to indicate diff list representation

d_noun([joe|L],L).
d_noun([the,apple|L],L).
d_noun([the,pear|L],L).

d_verb1([stopped|L],L).

d_verb2([saw|L],L).
d_verb2([hit|L],L).
d_verb2([ate|L],L).

d_sent(S,Q) :- d_noun(S,R), d_verb1(R,U), d_stop(U,Q).
d_sent(S,Q) :- d_noun(S,R), d_verb2(R,T), d_noun(T,U), d_stop(U,Q).

d_stop([.|L],L).
d_stop([?|L],L).

d_par(P,Q) :- d_sent(P,Q).
d_par(P,Q) :- d_sent(P,R), d_par(R,Q).


%% to access beginning of long list,
%% prefix(X,Y,Z) means Y is same length as X, but has same element
%% as first <however many> elements of Z
prefix([],[],_).
prefix([_|X],[E|Y],[E|Z]) :- prefix(X,Y,Z).
