%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                           %
%       file:           examples.pl                                         %
%       purpose:        example interpreter calls                           %
%       author:         Sebastian Varges                                    %
%       date:           Thu Feb 20 12:36:18 MET 1997                        %
%       related files:  top.pl, head.pl, memo_head.pl, chart.pl, aux.pl,    %
%                       grammar.pl, test_suite.pl, tests.pl                 % 
%                                                                           %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/*
% loading the system
| ?- ensure_loaded(top).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Generation with head driven interpreter
| ?- proc(hd,[Phon,syn:[s],sem:[pred:sleep,arg1:[cont:man,quant:sg]]]).
Phon = phon:[fi:[der,mann,schlaeft|_A],re:_A] ? ;
no

% "Parsing": correct but inefficient
proc(hd,[phon:[fi:[der,mann,sieht,petra],re:[]],syn:[s],Sem]).
Sem = sem:[pred:see,arg1:[cont:man,quant:sg],arg2:[cont:petra,quant:sg]] ? ;
no

% mixed input
| ?- proc(hd,[phon:[fi:[die,A,schlafen|B],re:C],syn:[s],sem:[D:sleep|E]]).

A = maenner,
C = B,
D = pred,
E = [arg1:[cont:man,quant:pl]] ? ;

A = frauen,
C = B,
D = pred,
E = [arg1:[cont:woman,quant:pl]] ? ;
no

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Generation with memoized head driven interpreter
| ?- proc(mhd,[Phon,syn:[s],sem:[pred:donate,arg1:[cont:woman,quant:pl],
               arg2:[cont:ball,quant:sg],arg3:[cont:peter,quant:sg]]]).
Phon = phon:[fi:[die,frauen,schenken,peter,den,ball|_A],re:_A] ? ;
no
% getting memoized results from database
| ?- memo_result(Result).
Result = [phon:[fi:[peter|_A],re:_A],syn:[np,masc,sg,dat,3],
          sem:[cont:peter,quant:sg]] ? ;
Result = [phon:[fi:[den,ball|_A],re:[ball|_A]],syn:[det,masc,sg,acc],sem:[quant:sg]] ? ;
Result = [phon:[fi:[den,ball|_A],re:_A],syn:[np,masc,sg,acc,3],
          sem:[cont:ball,quant:sg]] ? ;
Result = [phon:[fi:[die,frauen|_A],re:[frauen|_A]],syn:[det,fem,pl,nom],
          sem:[quant:pl]] ? ;
Result = [phon:[fi:[die,frauen,schenken,peter,den,ball|_A],re:[schenken,peter,den,
                    ball|_A]],syn:[np,fem,pl,nom,3],sem:[cont:woman,quant:pl]] ? ;
Result = [phon:[fi:[die,frauen,schenken,peter,den,ball|_A],re:_A],syn:[s],
          sem:[pred:donate,arg1:[cont:woman,quant:pl],arg2:[cont:ball,quant:sg],
               arg3:[cont:peter,quant:sg]]] ? ;
no

% "Parsing": stored results demonstrate inefficiancy of search
%            (but algorithm is correct)
| ?- proc(mhd,[phon:[fi:[der,mann,schlaeft],re:[]],syn:[s],Sem]).
Sem = sem:[pred:sleep,arg1:[cont:man,quant:sg]] ? ;
no
| ?- memo_result(Result).
Result = [phon:[fi:[peter,traeumt|_A],re:[traeumt|_A]],syn:[np,masc,sg,nom,3],
          sem:[cont:peter,quant:sg]] ? ;
Result = [phon:[fi:[petra,traeumt|_A],re:[traeumt|_A]],syn:[np,fem,sg,nom,3],
          sem:[cont:petra,quant:sg]] ? ;
Result = [phon:[fi:[der,mann|_A],re:[mann|_A]],syn:[det,masc,sg,nom],
          sem:[quant:sg]] ? ;
Result = [phon:[fi:[der,mann,traeumt|_A],re:[traeumt|_A]],syn:[np,masc,sg,nom,3],
          sem:[cont:man,quant:sg]] ? ;
Result = [phon:[fi:[der,ball|_A],re:[ball|_A]],syn:[det,masc,sg,nom],sem:[quant:sg]] ? ;
Result = [phon:[fi:[der,ball,traeumt|_A],re:[traeumt|_A]],syn:[np,masc,sg,nom,3],
          sem:[cont:ball,quant:sg]] ? ;
Result = [phon:[fi:[die,frau|_A],re:[frau|_A]],syn:[det,fem,sg,nom],sem:[quant:sg]] ? ;
% ....... all feature structures licensed by grammar

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% top-down Earley deduction
%  "proc(ed,FS)" corresponds to "earley_proc(FS,all,df,off,off)."
%   all: all solutions to be found
%   df:  depth-first search
%   off: trace off
%   off: no display of solutions
%%%%%%%%%%%% Parsing example:
| ?- proc(ed,[phon:[fi:[schenken,peter,den,ball],re:[]],syn:[vp|Syn],Sem]).
Sem = sem:[pred:donate,arg1:_A,arg2:[cont:ball,quant:sg],arg3:[cont:peter,quant:sg]],
Syn = [sc:[[syn:[np,_B,pl,nom,3],sem:_A]]] ? ;
no

% tables/0 shows all table items
% agenda is empty because of exhaustive search
| ?- tables.
********************  agenda items  ********************

********************  table items   ********************
%------------------------------------------------
%        Active Item:
   ans([phon:[fi:[schenken,peter,den,ball],re:[]],syn:[vp|_94],_91])   <---
   Selected:
        node([phon:[fi:[schenken,peter,den,ball],re:[]],syn:[vp|_94],_91])
   Grammar Rule Index:  0
%------------------------------------------------
%        Active Item:
   node([phon:[fi:[schenken,peter,den,ball],re:[]],syn:[vp,sc:_124],_120])   <---
   Selected:
        node([phon:[fi:[schenken,peter,den,ball],re:[]],syn:[v,sc:_124],_120])
   Grammar Rule Index:  3

.......... many more items .........

%------------------------------------------------
%         Passive Item:
ans([phon:[fi:[schenken,peter,den,ball],re:[]],syn:[vp,sc:[
     [syn:[np,_181,pl,nom,3],sem:_172]]],
      sem:[pred:donate,arg1:_172,arg2:[cont:ball,quant:sg],arg3:[cont:peter,quant:sg]]]).
   Grammar Rule Index:  [[[[4,7],17],[[[5,24],[2,[3,40]]],2]],0]

% this passive answer item is the reduced start item 0

%%%%%%%%%%%% Generation example with underspecified semantics -> two solutions
% bf: breath-first search
| ?- earley_proc([_,syn:[s],sem:[_,arg1:[cont:man,quant:sg]]],all,bf,off,on).

[phon:[fi:[der,mann,traeumt|_15913],re:_15913],syn:[s],
 sem:[pred:dream,arg1:[cont:man,quant:sg]]]
[phon:[fi:[der,mann,schlaeft|_15913],re:_15913],syn:[s],
 sem:[pred:sleep,arg1:[cont:man,quant:sg]]]
% no (more) solutions
yes

%%%%%%%%%%%% Parsing with trace
% first: stop after first solution is found
% df:    depth-first search
% on:    trace on
% on:    display of solutions
| ?- earley_proc([phon:[fi:[der,mann,schlaeft],re:[]],syn:[s],_],first,df,on,on).
***************************  new task ***************************
%        Active Item:
   ans([phon:[fi:[der,mann,schlaeft],re:[]],syn:[s],_390])   <---
   Selected:
        node([phon:[fi:[der,mann,schlaeft],re:[]],syn:[s],_390])
   Grammar Rule Index:  0

[Options: a = abort, n = nonstop,  = creep]
||  
% ------------------------------------------------------
%  inference rule:       prediction
%  reduced new item:
%        Active Item:
   node([phon:[fi:[der,mann,schlaeft],re:[]],syn:[s],sem:_643])   <---
        node([phon:[fi:_662,re:[]],syn:[vp,sc:[[syn:[np|_669],sem:_674]]],sem:_643])
   Selected:
        node([phon:[fi:[der,mann,schlaeft],re:_662],syn:[np|_669],sem:_674])
   Grammar Rule Index:  1
   selection type:  leftmost by default

[Options: a = abort, n = nonstop,  = creep]
||  
***************************  new task ***************************
%        Active Item:
   node([phon:[fi:[der,mann,schlaeft],re:[]],syn:[s],sem:_702])   <---
        node([phon:[fi:_746,re:[]],syn:[vp,sc:[[syn:[np|_738],sem:_734]]],sem:_702])
   Selected:
        node([phon:[fi:[der,mann,schlaeft],re:_746],syn:[np|_738],sem:_734])
   Grammar Rule Index:  1

[Options: a = abort, n = nonstop,  = creep]
||  
% ------------------------------------------------------
%  inference rule:       prediction
%  reduced new item:
%        Active Item:
   node([phon:[fi:[der,mann,schlaeft],re:_746],syn:[np,_1181,_1183,_1185,_1187],
         sem:[cont:_1193,quant:_1198]])   <---
        node([phon:[fi:_1217,re:_746],syn:[n,_1181,_1183,_1185,_1187],
              sem:[cont:_1193,quant:_1198]])
   Selected:
        node([phon:[fi:[der,mann,schlaeft],re:_1217],syn:[det,_1181,_1183,_1185],
              sem:[quant:_1198]])
   Grammar Rule Index:  4
   selection type:  leftmost by default

[Options: a = abort, n = nonstop,  = creep]
||  
% ------------------------------------------------------
%  inference rule:       prediction
%  reduced new item:
%        Active Item:
   node([phon:[fi:[der,mann,schlaeft],re:_746],
         syn:[np,_1181,_1183,_1185,_1187],sem:_734])   <---
   Selected:
        node([phon:[fi:[der,mann,schlaeft],re:_746],
              syn:[pn,_1181,_1183,_1185,_1187],sem:_734])
   Grammar Rule Index:  5
   selection type:  the only element

[Options: a = abort, n = nonstop,  = creep]
||  
***************************  new task ***************************
%        Active Item:
   node([phon:[fi:[der,mann,schlaeft],re:_1252],
         syn:[np,_1241,_1239,_1237,_1235],sem:_1232])   <---
   Selected:
        node([phon:[fi:[der,mann,schlaeft],re:_1252],
              syn:[pn,_1241,_1239,_1237,_1235],sem:_1232])
   Grammar Rule Index:  5
%%%%%% => no application of inference rules 
[Options: a = abort, n = nonstop,  = creep]
||  
***************************  new task ***************************
%        Active Item:
   node([phon:[fi:[der,mann,schlaeft],re:_1624],syn:[np,_1656,_1654,_1652,_1607],
         sem:[cont:_1599,quant:_1644]])   <---
        node([phon:[fi:_1667,re:_1624],syn:[n,_1656,_1654,_1652,_1607],
              sem:[cont:_1599,quant:_1644]])
   Selected:
        node([phon:[fi:[der,mann,schlaeft],re:_1667],
              syn:[det,_1656,_1654,_1652],sem:[quant:_1644]])
   Grammar Rule Index:  4

[Options: a = abort, n = nonstop,  = creep]
||  
% ------------------------------------------------------
%  inference rule:       scanning
%  reduced new item:
%        Active Item:
   node([phon:[fi:[der,mann,schlaeft],re:_1624],syn:[np,masc,sg,nom,_1607],
         sem:[cont:_1599,quant:sg]])   <---
   Selected:
        node([phon:[fi:[mann,schlaeft],re:_1624],syn:[n,masc,sg,nom,_1607],
              sem:[cont:_1599,quant:sg]])
   Grammar Rule Index:  [4,6]
   selection type:  the only element
%%%%%% => grammar rule 4 reduced with lex entry 6
[Options: a = abort, n = nonstop,  = creep]
||  
***************************  new task ***************************
%        Active Item:
   node([phon:[fi:[der,mann,schlaeft],re:_2244],syn:[np,masc,sg,nom,_2227],
         sem:[cont:_2219,quant:sg]])   <---
   Selected:
        node([phon:[fi:[mann,schlaeft],re:_2244],syn:[n,masc,sg,nom,_2227],
              sem:[cont:_2219,quant:sg]])
   Grammar Rule Index:  [4,6]

[Options: a = abort, n = nonstop,  = creep]
||  
% ------------------------------------------------------
%  inference rule:       scanning
%  reduced new item:
%         Passive Item:
node([phon:[fi:[der,mann,schlaeft],re:[schlaeft]],syn:[np,masc,sg,nom,3],
      sem:[cont:man,quant:sg]]).
   Grammar Rule Index:  [[4,6],14]
   selection type:  nothing to select
%%%%%% => scanning of "mann"; result: passive item for np
[Options: a = abort, n = nonstop,  = creep]
||  
***************************  new task ***************************
%         Passive Item:
node([phon:[fi:[der,mann,schlaeft],re:[schlaeft]],syn:[np,masc,sg,nom,3],
      sem:[cont:man,quant:sg]]).
   Grammar Rule Index:  [[4,6],14]

[Options: a = abort, n = nonstop,  = creep]
||  n   %%%%%%% => nonstop

% first solution:    [phon:[fi:[der,mann,schlaeft],re:[]],syn:[s],
                      sem:[pred:sleep,arg1:[cont:man,quant:sg]]]
yes

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%  test suite
%   testing earley deduction engine in verbose mode with mixed input
| ?- test(ed,v,mixed).
%%%  Completing consistent feature structures:
Nr. 40a:    
Input:   [phon:[fi:[petra],re:[]],syn:[pn|_253],sem:[cont:petra,quant:sg]]
Output:  [phon:[fi:[petra],re:[]],syn:[pn,fem,sg,_205,3],sem:[cont:petra,quant:sg]]
         
Nr. 41a:    
Input:   [phon:[fi:[petra|_312],re:_308],syn:_303,sem:[cont:_293,quant:sg]]
Output:  [phon:[fi:[petra|_224],re:_224],syn:[pn,fem,sg,_205,3],
          sem:[cont:petra,quant:sg]]
         [phon:[fi:[petra|_273],re:_273],syn:[np,fem,sg,_254,3],
          sem:[cont:petra,quant:sg]]     
........ etc .....

%%%  Trying to complete inconsistent feature structures:
Nr. 51a:    
Input:   [phon:[fi:[die,_227,schlaeft],re:[]],syn:[s],
          sem:[pred:_205,arg1:[cont:man,quant:_190]]]
Output:  

Nr. 51b:    
Input:   [phon:[fi:[der,traeumt|_220],_217],syn:[s],
          sem:[_203,arg1:[cont:man,quant:sg]]]
Output:  
........ etc .....
% => inconsistent input: no solution

% testing interpreters with all test sentences in silent mode
| ?- test(s).
******  Testing head-driven interpreter in silent mode  ******
%%%  Generating with consistent feature structures:
[20a]  [21a]  [21b]  [22a]  [22b]  [22c]  [22d]  [23a]  [23b]  [23c]  [23d]  ...
%%%  Generating with inconsistent feature structures:
[30a]  [30b]  [31a]  [31b]  [31c]  [32a]  [32b]  [32c]  [33a]  [33b]  [33c]  ...

******  Testing memoized head-driven interpreter in silent mode  ******
%%%  Generating with consistent feature structures:
[20a]  [21a]  [21b]  [22a]  [22b]  [22c]  [22d]  [23a]  [23b]  [23c]  [23d]  ...
%%%  Generating with inconsistent feature structures:
[30a]  [30b]  [31a]  [31b]  [31c]  [32a]  [32b]  [32c]  [33a]  [33b]  [33c]  ...

******  Testing Earley deduction interpreter in silent mode  ******
%%%  Parsing correct phrases:
[0a]  [0b]  [1a]  [1b]  [1c]  [2a]  [2b]  [3a]  [3b]  [3c]  [3d]  [4a]  [4b] ...
%%%  Parsing incorrect phrases:
[10a]  [10b]  [11a]  [12a]  [12b]  [12c]  [13a]  [13b]  [13c]  [13d]  [14a]  ...
%%%  Generating with consistent feature structures:
[20a]  [21a]  [21b]  [22a]  [22b]  [22c]  [22d]  [23a]  [23b]  [23c]  [23d]  ...
%%%  Generating with inconsistent feature structures:
[30a]  [30b]  [31a]  [31b]  [31c]  [32a]  [32b]  [32c]  [33a]  [33b]  [33c]  ...
%%%  Completing consistent feature structures:
[40a]  [41a]  [42a]  [42b]  [42c]  [42d]  [42e]  [42f]  [42g]  [42h]  [42i]  ...
%%%  Trying to complete inconsistent feature structures:
[51a]  [51b]  [52a]  [52b]  [52c]  [52d]  [53a]  [53b]  
yes
*/