/*************************************************************************

         name: clls.pl
      version: Mar 2002
  description: clls underspecified semantic construction for FOL
       author: Aljoscha Burchardt
 
*************************************************************************/

:- module(clls,[clls/0,clls/2]).

:- use_module(readLine,[readLine/1]).
:- use_module(comsemLib,[printRepresentations/1]).
:- use_module(signature,[resetVars/0,vars2atoms/1]).
:- use_module(betaConversion,[betaConvertList/2]).
:- ensure_loaded(comsemOperators).
:- [user:comsemOperators].
:- [englishGrammar].

:- use_module(cllsLib,[mergeUSR/2,usr2LambdaList/2]).
:- use_module(solveConstraint,[solve/2]).


/*========================================================================
   Driver Predicates
========================================================================*/

clls :-
	readLine(Sentence),
	parse(Sentence,UsSem),
	resetVars,vars2atoms(UsSem),
%	printRepresentations([UsSem]),
	solve(UsSem,Sems),
%	printRepresentations(Sems),
	usr2LambdaList(Sems,LambdaTerms),
%	printRepresentations(LambdaTerms),
	betaConvertList(LambdaTerms,Converted),
	printRepresentations(Converted).

%%%%% The second driver makes much output if the first argument is *not* "silent".
%%%%% It can be used to feed input sentences directly:
%%%%% clls(silent,[every,owner,of,a,siamese,cat,loves,a,therapist]).
%%%%% clls(verbose,[every,owner,of,a,siamese,cat,loves,a,therapist]).
clls(Verbose,Sentence) :-
	parse(Sentence,UsSem),
	resetVars,vars2atoms(UsSem),
	(Verbose==silent;printRepresentations([UsSem])),
	solve(UsSem,Sems),
	(Verbose==silent;printRepresentations(Sems)),
	usr2LambdaList(Sems,LambdaTerms),
	(Verbose==silent;printRepresentations(LambdaTerms)),
	betaConvertList(LambdaTerms,Converted),
	printRepresentations(Converted).

/*========================================================================
   Parsing 
========================================================================*/

parse(Sentence,Formula):-
	s2(Formula,Sentence,[]).
	

/*========================================================================
   Semantic Rules
========================================================================*/

combine(s2:X,[s1:X]).

combine(s1:S,[np2:NP,vp2:VP]):-
	NP = usr([NPRoot|_],_,_,_),
        VP = usr([VPRoot|_],_,_,_),
	NewUsr = usr([Root],[Root:(VPRoot@NPRoot)],[],[]),
	mergeUSR(merge(NewUsr,merge(NP,VP)),S).

combine(np2:X,[np1:X]).

combine(np1:X,[pn:X]).
combine(np1:NP,[det:DET,n2:N]) :-
	DET = usr([DETRoot|_],_,_,_),
        N = usr([NRoot|_],_,_,_),
	NewUsr = usr(
	[Root,N1,N2,N3,N4],
	[N1:(N2@N3),N2:(DETRoot@NRoot),N3:lambda(N4),Root:var],
	[dom(N4,Root)],
	[bind(Root,N3)]),
	mergeUSR(merge(NewUsr,merge(DET,N)),NP).
	
combine(n2:X,[n1:X]).

combine(n1:X,[noun:X]).
combine(n1:N,[noun:Noun,pp:PP]) :-
	Noun = usr([NounRoot|_],_,_,_),
        PP = usr([PPRoot|_],_,_,_),
	NewUsr = usr(
	[Root,N1,N2,N3,N4,N5,N6],
	[Root:lambda(N1),N1:(N2 & N3),N2:(NounRoot@N4),N4:var,N5:(PPRoot@N6),N6:var],
	[dom(N3,N5)],
	[bind(N4,Root),bind(N6,Root)]),
	mergeUSR(merge(NewUsr,merge(Noun,PP)),N).

combine(vp2:X,[vp1:X]).

combine(vp1:X,[v2:X]).

combine(v2:X,[v1:X]).

combine(v1:X,[iv:X]).
combine(v1:V,[tv:TV,np2:NP]) :-
	TV = usr([TVRoot|_],_,_,_),
        NP = usr([NPRoot|_],_,_,_),
	NewUsr = usr([Root],[Root:(TVRoot@NPRoot)],[],[]),
	mergeUSR(merge(NewUsr,merge(TV,NP)),V).

combine(pp:PP,[prep:Prep,np2:NP]) :-
	Prep = usr([PrepRoot|_],_,_,_),
        NP = usr([NPRoot|_],_,_,_),
	NewUsr = usr([Root],[Root:(PrepRoot@NPRoot)],[],[]),
	mergeUSR(merge(NewUsr,merge(Prep,NP)),PP).


/*========================================================================
   Semantic Macros
========================================================================*/

detSem(uni,usr([Root,N1,N2,N3,N4,N5,N6,N7,N8,N9],
	[Root:lambda(N1),N1:lambda(N2),N2:forall(N3),N3:(N4 > N5),
	 N4:(N6@N7),N5:(N8@N9),N6:var,N7:var,N8:var,N9:var],
	[],
	[bind(N6,Root),bind(N7,N2),bind(N8,N1),bind(N9,N2)])).

detSem(indef,usr([Root,N1,N2,N3,N4,N5,N6,N7,N8,N9],
	[Root:lambda(N1),N1:lambda(N2),N2:exists(N3),N3:(N4 & N5),
	 N4:(N6@N7),N5:(N8@N9),N6:var,N7:var,N8:var,N9:var],
	[],
	[bind(N6,Root),bind(N7,N2),bind(N8,N1),bind(N9,N2)])).

nounSem(Symbol,usr([Root],[Root:Symbol],[],[])).

pnSem(Symbol,usr([Root],[Root:Symbol],[],[])).

ivSem(Symbol,usr([Root],[Root:Symbol],[],[])).

tvSem(Symbol,usr([Root],[Root:Symbol],[],[])).

prepSem(Symbol,usr([Root],[Root:Symbol],[],[])).


/*========================================================================
   Unused Macros
========================================================================*/
detSem(wh,_) :- fail.
adjSem(_,_) :- fail.
coordSem(_,_) :- fail.
modSem(_,_) :- fail.
npSem(_,_,_) :- fail.
proSem(_,_) :- fail.
relproSem(_) :- fail.
