/* top.
Roller Coaster problems
Alan Bundy 7.4.81 */

/* Top Level: Question Answering */

  qa(Qual,Quan)
	:- Qual,
	   findall(X,proviso(X),Condlist),
	   trace('%t ok provided :%l',[Qual,Condlist],2),
	   setup(Condlist,Xs,Gs),
	   get_types(Xs,[],Xtypes),
	   get_types(Gs,Xtypes,Types),
	   solve(Xs,Gs,Types,Es,Xs1).
	   crunch(Es,Xs1,Ans),
	   subst(Solns,Condlist,Ncondlist),
	   trace('New conditions are :%l',[Ncondlist],2),
	   apply(Quan,[Ncondlist]).

/* Find soughts and givens */

  setup(Condlist,Xs,Gs)
	:- wordsin(Condlist,Vars),
	   givens(Gs),
	   subtract(Vars,Gs,Xs).






/*MAKING CONDITIONS*/
/*-----------------*/

/*SEE IF ITS TRUE*/
condition(L) :-
  evaluate(L,true), !,
  trace('condition: %t holds\n', [L],3).

/*SEE IF ITS FALSE*/
condition(L) :-
  evaluate(L,false), !,
  trace('condition: %t does not hold\n', [L],3),
  fail.

/*OTHERWISE NOTE IT FOR LATER*/
condition(L) :- postulate(proviso(L)),
  trace('Storing proviso : %t.\n',[L],3).

/* Retractable Assert */
postulate(Ass) :-			% assert at top first time round
	asserta(Ass).

postulate(Ass) :-			% retract and fail on backup
	retract(Ass), !, fail.


/* Evaluate condition */

evaluate(Cond,true) :- ncc Cond, !.		% condition is true

evaluate(Cond,false) :- negate(Cond,NotCond), ncc NotCond, !.

negate(positive(X), non_pos(X)).
negate(non_neg(X),negative(X)).
negate(real(X),complex(X)).


