Web-based Visualisation for Definite Clause Grammars using Prolog Meta-Interpreters

System Description

Falco Nogatz, Jona Kalkus, and Dietmar Seipel, University of Würzburg

Web-based Visualisation for Definite Clause Grammars using Prolog Meta-Interpreters

System Description

Falco Nogatz, Jona Kalkus, and Dietmar Seipel, University of Würzburg

Web-based Visualisation for Definite Clause Grammars using Prolog Meta-Interpreters

System Description

Falco Nogatz, Jona Kalkus, and Dietmar Seipel, University of Würzburg

Web-based Visualisation for Definite Clause Grammars using Prolog Meta-Interpreters

System Description

Falco Nogatz, Jona Kalkus, and Dietmar Seipel, University of Würzburg

Definite Clause Grammars

Definite Clause Grammars

Basics

  • A way of expressing grammars in Prolog
  • Extension of context-free grammars:
    • Context through additional arguments and semicontext
    • Approach to return values
    • Usage of built-in Prolog predicates
  • Syntactic sugar for the description of lists

Definite Clause Grammars

Syntax

Head --> Body.
  • Head
    Non-terminal of any arity
  • Body
    Sequence of terminals, non-terminals, and Prolog predicates
  • Terminal
    Prolog list of any type, possibly strings
  • Prolog code
    Built-in or user-defined Prolog predicates, enclosed in curly braces

Definite Clause Grammars

Syntax (cont.)

  • Usage of well-known Prolog control predicates:
    ;/2 and |/2,
    ->/2,
    \+/1,
    !/1
  • Head can be any callable term:
    a,
    a(X),
    a(b,2)

Definite Clause Grammars

Example

% structure
sentence    --> noun_phrase, ws, verb_phrase.
noun_phrase --> determiner, ws, noun.
verb_phrase --> verb, ws, noun_phrase.

% vocabulary
determiner  --> "a".    % = [a]
determiner  --> "the".
noun        --> "cat".
noun        --> "mouse", { is_mouse_allowed }.
verb        --> "chases".
ws          --> " ".

% user predicate
is_mouse_allowed.

Definite Clause Grammars

Example (cont.)

% structure
sentence    --> noun_phrase, verb_phrase.
noun_phrase --> determiner, noun.
verb_phrase --> verb, noun_phrase.

% vocabulary
determiner  --> "a".    % = [a]
determiner  --> "the".
noun        --> "cat".
noun        --> "mouse", { is_mouse_allowed }.
verb        --> "chases".

% user predicate
is_mouse_allowed.

Definite Clause Grammars

Usage with SWI-Prolog

phrase/2
?- Input = "the cat chases a mouse", phrase(sentence, Input).
true.
?- Input = "the cat chases a mouse", phrase(noun_phrase, Input).
false.
phrase/3
?- Input = "the cat chases a mouse", phrase(noun_phrase, Input, Rest).
Rest = "chases a mouse".
noun_phrase/2
?- Input = "the cat chases a mouse", noun_phrase(Input, Rest).
Rest = "chases a mouse".

Applications

Definite Clause Grammars

Applications

  • Natural Language Processing
  • Formal languages, e.g., library(http/html_write)
  • Formal languages using quasi-quotations

Quasi-Quotations

Recap

Since SWI-Prolog 6.3 (Wielemaker & Hendricks, WLPE 2013)

Basic form
{| Tag || Content |}
Example
?- Person = {| type ||
   name:    String!
   age:     Integer
   books(favourite: Boolean): [Book]
   friends: [Person]
|}.

Falco Nogatz and Dietmar Seipel. 2016. Implementing GraphQL as a Query Language for Deductive Databases in SWI-Prolog Using DCGs, Quasi Quotations, and Dicts. In Proc. 30th Workshop on Logic Programming (WLP 2016).

Existing Tools

Prolog Visualizer

Description
Zhixuan Lai and Alessandro Warth. Prolog Visualizer. http://cdglabs.org/prolog/, 2015.

HDRUG

Description
Gertjan Van Noord and Gosse Bouma. Hdrug. A Flexible and Extendible Development Environment for Natural Language Processing. Computational Environments for Grammar Development and Linguistic Engineering, 1997.

Web-based Visualisation for DCGs

Goals

Focus on DCGs

Handle embedded code as preconditions (blackbox), correct line numbers

Compatibility and feature-completeness

Extend existing DCGs and Prolog systems

Reasonable performance

Reasonable overhead, pre-compilation in mind

Interactive exploration of rule-applications

Web-based Visualisation for Definite Clause Grammars using Prolog Meta-Interpreters

System Description

Falco Nogatz, Jona Kalkus, and Dietmar Seipel, University of Würzburg

Web-based Visualisation for Definite Clause Grammars using Prolog Meta-Interpreters

System Description

Falco Nogatz, Jona Kalkus, and Dietmar Seipel, University of Würzburg

Definite Clause Grammars

Example (recap)

% structure
sentence    --> noun_phrase, verb_phrase.
noun_phrase --> determiner, noun.
verb_phrase --> verb, noun_phrase.

% vocabulary
determiner  --> "a".    % = [a]
determiner  --> "the".
noun        --> "cat".
noun        --> "mouse", { is_mouse_allowed }.
verb        --> "chases".

% user predicate
is_mouse_allowed.

Definite Clause Grammars

Term Expansion in SWI-Prolog

sentence(A, C) :-
  noun_phrase(A, B),
  verb_phrase(B, C).
...
determiner([a|A], A).
determiner([t,h,e|A], A).
noun([c,a,t|A], A).
noun([m,o,u,s,e|A], A) :- is_mouse_allowed.
...

Naïve Approaches

Tracing — Idea I

Parse Tree as Additional Argument

sentence(sentence(NP,VP)) --> noun_phrase(NP), verb_phrase(VP).
noun_phrase(noun_phrase(D,N)) --> determiner(D), noun(N).
verb_phrase(verb_phrase(V,NP)) --> verb(V), noun_phrase(NP).
determiner(determiner("a")) --> "a".
determiner(determiner("the")) --> "the".
noun(noun("cat")) --> "cat".
noun(noun("mouse")) --> "mouse", { is_mouse_allowed }.
verb(verb("chases")) --> "chases".

Tracing — Idea I

Parse Tree as Additional Argument (cont.)

?- Input = "the cat chases a mouse",
   phrase(noun_phrase(NP), Input, Rest).
Rest = "chases a mouse",
NP = noun_phrase(determiner("the"), noun("cat")).

Tracing — Idea I

Parse Tree as Additional Argument (cont.)

Description

Tracing — Idea I

Parse Tree as Additional Argument (cont.)

Pro

Simple implementation using term expansion

Con

Name clash
Can not use existing programs
Only successful applications

Tracing — Idea II

Intercept Built-in Tracer

prolog_trace_interception(Port, Frame, Choice, Action) :-
  % retrieve unique program counter
  get_counter(N),
  % get information about current event
  prolog_frame_attribute(Frame,goal,Goal),
  prolog_frame_attribute(Frame,parent,Parent),
  prolog_choice_attribute(Choice,frame,ChoiceFrame),
  % ...
  % assert information as predicate `step/N`
  assert(step(N,Port,Frame,Goal,Parent,Choice,ChoiceFrame, ...)),
  % continue with the execution
  Action = continue.

Tracing — Idea II

Intercept Built-in Tracer (cont.)

?- Input = "the cat chases a mouse",
   trace, phrase(noun_phrase, Input, Rest), notrace.
Rest = "chases a mouse".
?- listing(step).
% ...
step(3,unify,235,noun_phrase(A,[]),
   $dcg:call_dcg(noun_phrase,A,[]), ...).
step(4,call,255,determiner(A,B),
   noun_phrase(A,[]), ...).
% ...

Tracing — Idea II

Intercept Built-in Tracer (cont.)

Pro

Native support for backtracking
Information about current port, stack frame, choice points

Con

No unique identifiers (in deterministic predicates)
No easy distinction between non-terminals and Prolog predicates
No way to re-use existing tracer

Tracing — Idea II

Intercept Built-in Tracer (cont./backup)

prolog_trace_interception(Port, Frame, Choice, Action) :-
  % retrieve unique program counter
  get_counter(N),
  % get information about current event
  prolog_frame_attribute(Frame,goal,Goal),
  prolog_frame_attribute(Frame,parent,Parent),
  prolog_choice_attribute(Choice,frame,ChoiceFrame),
  % ...
  % assert information as predicate `step/N`
  assert(step(N,Port,Frame,Goal,Parent,Choice,ChoiceFrame, ...)),
  % continue with the execution
  Action = continue.

Prolog Meta-Interpreters

Meta-Interpreter I

Vanilla Meta-Interpreter

Meta-Interpreter I
vanilla(true).
vanilla((A , B)) :- vanilla(A), vanilla(B).
vanilla((A = B)) :- A = B.
vanilla(A) :- 
   A \= (_ , _), A \= (_ = _), A \= true,
   clause(A, B),
   vanilla(B).
Usage
?- Input = "the cat chases a mouse", 
   vanilla(noun_phrase(Input, Rest)).
Rest = "chases a mouse".

Leon Sterling and Ehud Y Shapiro. 1994. The art of Prolog: advanced programming techniques. MIT press.

Meta-Interpreter II

Extension to Store Parse Trees

Meta-Interpreter II
mi_tree(true, true).
mi_tree((A, B) , (TA, TB)) :- mi_tree(A, TA), mi_tree(B, TB).
mi_tree((A = B), (A = B)) :- A = B.
mi_tree(A,(A:T)) :-
   A \= (_ , _), A \= (_ = _), A \= true,
   clause(A, B), 
   mi_tree(B, T).
Usage
?- ..., mi_tree(noun_phrase(Input, Rest), T).
Rest = "chases a mouse",
T = noun_phrase("the cat chases a mouse", "chases a mouse"):(
      determiner("the cat", "cat"):true,
      noun("cat", ""):true ).

Meta-Interpreter III

With Backtracking

Meta-Interpreter III
mi_print(true, _Level).
mi_print((A , B), L) :-
   mi_print(A, L), mi_print(B, L).
mi_print((A = B), L) :-
   ( A = B ~> print_indented((A = B):exit, L)
   ; print_indented((A = B):fail, L), fail ).
mi_print(A, L) :- L1 is L+1,
   A \= (_ , _), A \= (_ = _), A \= true,
   print_indented(A:call, L),
   nth_clause(A, Index, Sign),
   ( clause(A, B, Sign), mi_print(B, L1) ~>
     print_indented(A:Index:exit, L)
   ; print_indented(A:Index:fail, L), fail ).
print_indented(A, L) :-
   Indent is 2*L, tab(Indent), writeln(A).

Meta-Interpreter III

With Backtracking (cont.)

Usage
?- mi_print(verb_phrase("chases a dog", ""), 0).
verb_phrase("chases a dog",""):call
   verb("chases a dog",_6966):call
   verb("chases a dog","a dog"):1:exit
   noun_phrase("a dog",""):call
      determiner("a dog",_7198):call
      determiner("a dog",_7198):1:fail
      determiner("a dog","dog"):2:exit
      noun("dog",""):call
      noun("dog",""):1:fail
      noun("dog",""):2:fail
      determiner("a dog",_7198):2:fail
   noun_phrase("a dog",""):1:fail
   verb("chases a dog",_6966):1:fail
verb_phrase("chases a dog",""):1:fail
false.

Meta-Interpreter IV

Modified Term Expansion

mouse_phrase --> determiner, "mouse", { is_mouse_allowed }.
Normal Term Expansion
mouse_phrase(A, C) :-
   determiner(A, B),
   B = [m,o,u,s,e|C],
   is_mouse_allowed.
Wrap non-terminals and Prolog predicates
mouse_phrase(A, C) :-
   'DCGBody'(determiner, A, B),
   'T'("mouse", B, C),
   'P'(is_mouse_allowed).

Meta-Interpreter IV (cont.)

Storing Information

Dicts in SWI-Prolog 7
Tag{
   key1: Value1,
   key2: Value2, ... }
Asserted facts
event{
   step: <N>,
   type: <unify | exit | fail | depth>,
   redo: <Redo>,
   goal: <Goal with current variable bindings>,
   line: <Line number of associated clause
            | 'T' | 'P' | 'DCGBody'>
}

Web-based Visualisation

Overall Architecture

Using Tracing Meta-Interpreter and Pengines

Description

Web-Based Visualisation

Technology

Server
  • Dicts
  • Pengines

Web-Based Visualisation (cont.)

Technology: Pengines

Description Description

Web-Based Visualisation

Technology (cont.)

Server
  • Dicts
  • Pengines
Client
  • HTML5
  • JavaScript
  • CSS3
  • MUI, jQuery, CodeMirror

Demo

Conclusion

Contributions
  • Discussion about Tracing with Meta-Interpreters
  • DCG Tracer: Tracing Meta-Interpreter
  • DCG Visualiser: Pengine- and web-based Visualisation
Future Work
  • Support for full DCG spec
  • EDCG

Open Source @ GitHub




https://github.com/jonakalkus/dcg-visualiser
      

Falco Nogatz

University of Würzburg
Chair for Computer Science I

falco.nogatz@uni-wuerzburg.de
http://uni-wuerzburg.de/?nogatz