;;; Use of LIB FACETS for very simple database query program

:- prolog_language("pop11").

lib facets;
lib showtree;

;;; Load the component files

:- library(monarchs).               ;;; the database
:- library(dbquery).                ;;; the database query language

:- library(dbgrammar).              ;;; The grammar. This must define
                                    ;;; a procedure PARSE which, given a
                                    ;;; sentence, returns a parse tree
                                    ;;; (or []).

:- library(dbinterp).               ;;; The semantic interpretation procedures
                                    ;;; This must define a procedure INTERPRET
                                    ;;; which, given a parse tree, produces a
                                    ;;; symbolic "meaning" for the sentence

;;; Set this variable to TRUE to get extra tracing               

vars tracing;
false -> tracing;

vars query;

;;; The controlling procedure (from POP-11)

define go();
   vars parsetree, interpretation, sentence;
   [MONARCHS DATABASE HERE]=>
   nl(2);
   readline() -> sentence;
   until sentence = [bye] do
      ;;; Call grammar to parse the sentence
      parse(sentence) -> parsetree;
      if parsetree == [] then
         [i cant parse that]=>
      else
         if tracing then
             [PARSE TREE]=>
             showtree(parsetree)
         endif;
         interpret(parsetree) -> interpretation;
         if tracing then
             [QUERY]=>
             showtree(interpretation)
         endif;
         query(interpretation)
      endif;
      readline() -> sentence
   enduntil
enddefine;

"pop11" -> subsystem;           ;;; Change default subsystem to POP-11

;;; The controlling procedure (from Prolog)

:- prolog_language("prolog").

go :- prolog_eval(apply(valof(go))).

:- library(popsetq).

trace(on) :- prolog_setq(tracing,true).
trace(off) :- F is valof(false), prolog_setq(tracing,F).

:- prolog_eval(apply(valof(setpop))).       ;;; Get into POP-11
