:- use_module(input).
:- use_module(lub_operations, [set_union/1]).
:- use_module(facts,[fact/1]).
:- use_module(utilities, [showfacts/0]).


:- use_module(library(lists),[append/3]).

%% interface
  go(File,Goal) :-              % Goal is atomic
      retractall(fact(_)),      
      load_file(File),
      new(call(Goal)),
      fail.
  go(_,_)   :- showfacts.

%% logic

  new(Fact) :- set_union(Fact), eager(Fact).

  eager(ans(Atom)) :-                       
          my_clause(Head,Body),
          fact(call(Head)),
          append(Left,[Atom|Right],Body),
          prove(Left),
          prove_right(Right),
          new(ans(Head)).

  eager(call(Atom)) :-                       
          my_clause(Atom,Body),
          prove_right(Body),
          new(ans(Atom)).



  prove([B|Bs]) :- fact(ans(B)), prove(Bs).  
  prove([]).

  prove_right([B|_]) :-
      new(call(B)).
  prove_right([B|Bs]) :- 
      fact(ans(B)), prove_right(Bs).  
  prove_right([]).




