p&s 3.5, 3.7: the logic and operation of prolog revisited ...ddekok/pp/lectures/bjoern.pdf ·...

32
P&S 3.5, 3.7: The logic and operation of Prolog revisited, definite clause grammar Bj¨ornRudzewitz Logic and Operation Revisited Substitution Unification Semantics of Prolog Definite Clause Grammars Parse Trees Tree Operations in Prolog Removal of Nodes Lexicon Simplification References P&S 3.5, 3.7: The logic and operation of Prolog revisited, definite clause grammar Bj¨ orn Rudzewitz [email protected] University of T¨ ubingen June 1, 2016 1 / 29

Upload: hatram

Post on 09-Sep-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

P&S 3.5, 3.7: The logic and operation ofProlog revisited, definite clause grammar

Bjorn Rudzewitz

[email protected]

University of Tubingen

June 1, 2016

1 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

Logic and Operation RevisitedSubstitutionUnificationSemantics of Prolog

Definite Clause GrammarsParse TreesTree Operations in PrologRemoval of NodesLexicon Simplification

2 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

Introduction

I How does Prolog search/proof ?I search criteria:

1. efficient2. predictable

I search operations: substitution, unification, resolution

3 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

Substitution

I Substitute variables X by terms T with substitutionfunction σ:

σ : {x1, · · · xk} → {t1, · · · , tk},{x1 = t1, · · · , xk = tk}

I application of σ to expression e: [e]σ

I if [e1]σ = e2, thenI e1 subsumes e2

if not e2 subsumes e1, e1 is more generalI e2 is an instance of e1

4 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

Substitution

I Substitute variables X by terms T with substitutionfunction σ:

σ : {x1, · · · xk} → {t1, · · · , tk},{x1 = t1, · · · , xk = tk}

I application of σ to expression e: [e]σ

I if [e1]σ = e2, thenI e1 subsumes e2

if not e2 subsumes e1, e1 is more generalI e2 is an instance of e1

4 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

Substitution

I Substitute variables X by terms T with substitutionfunction σ:

σ : {x1, · · · xk} → {t1, · · · , tk},{x1 = t1, · · · , xk = tk}

I application of σ to expression e: [e]σ

I if [e1]σ = e2, thenI e1 subsumes e2

if not e2 subsumes e1, e1 is more generalI e2 is an instance of e1

4 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

Subsumption in Practice

Figure: Subsumption predicates in SWI-Prolog source: http:

//www.swi-prolog.org/pldoc/doc_for?object=terms%3Asubsumes_chk/2

(last accessed 31/05/16)

5 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

Subsumption in Practice

practical example !

6 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

Subsumption in Practice

Figure: Subsumption checks in SWI-Prolog

7 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

Unification

I special case of substitution: unifying substitution

[e1]σ = [e2]σ

→ result of substitutions identical

I unifier σ always ’most general’:for every other unifier σ′ of e1,2, [e1]σ subsumes [e1]σ′

I i.e. the term resulting from applying σ′ is an instance ofthe term obtained by applying σ

I application of most general unifier: unification (ofe1, e2)

I result of most general unifiers: identical expressions, orat most other variable names

8 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

UnificationUnification Algorithm

I for 2 expressions, compute most general unifier

I various implementations

I key idea: find mismatches in expressions, findsubstitutions/bindings to solve them

I occurs check: never construct a term as a substitutionof a variable such that the term contains the variableagain

9 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

Unification AlgorithmOccurs check

e1: f(X)e2: f(g(X))naive unification algorithm: substitute x by g(x)e1’: f(g(X))e2: f(g(g(X))) · · ·occurs check needed for unification !

10 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

Unification AlgorithmOccurs check

see http://www.swi-prolog.org/pldoc/man?predicate=unify_with_

occurs_check/2

11 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

Semantics of Prolog

I different perspectives on goal finding:I declarative: conjunction of program and the negation of

the goal unsatisfiable→ a logical statement doesn’t hold

I procedural: negated goal and program generate emptyclause→ the result of a resolution is empty

I difference: procedural way is not necessarilyI complete: not always a solution is found due to

depth-first searchI sound: no occurs check

12 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

Definite Clause Grammars

I extension of Context Free Grammars: nonterminal canhave arguments (and are not just atoms such as in CFG)

I string positions encoded as last two arguments

Note: DCGs exist outside Prolog

13 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

Definite Clause GrammarsReminder: Built-In Support and Notation

s --> np(Number), vp(Number).

pn(singular) --> [he].

I --> operator

I RHS comma-separated (terminals and/or nonterminals)

I terminal as list (empty string: empty list)

I implicit encoding of position arguments (P0,P1),encodes lists

14 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

Definite Clause GrammarsShorthand Notation

The following grammars are equivalent:

15 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

Definite Clause Grammars

I DCG internally transformed to arity 2 predicate withdifference lists (for partial parsing)

16 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

Parse Trees

I additional argument for NTs to indicate span / subtree

I epsilon for empty subtree

s(s(NP,VP)) --> np(NP), vp(NP).

pn(pn(he)) --> [he].

I while parsing, a tree is built up in this argument

17 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

DCGs

I internally transformed to difference lists (for partialparsing)

I for the full parse, give an empty difference list as 3rdargument

18 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

Parse Trees

I more general parse tree representation (by Daniel):I general tree t predicate for parse treeI tree daughters in list argument

I Example:s(t(s,[NP,VP])) --> np(NP), vp(VP).

I see file dcg-t.pl

19 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

Tree Operations

I ’normal’ Prolog statements can be specified in additionto/inside DCG rules

I notation in brackets {operation} to make difference togrammar clear

20 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

Node Removal: Example from Pereira andSheiber [1989]

Terry writes a program.Terry writes a program that halts.

21 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

Node Removal: Example from Pereira andSheiber [1989]

Terry writes a program.Terry writes a program that halts.

21 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

Node Removal: Example from Pereira andSheiber [1989]

22 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

Node Removal: Example from Pereira andSheiber [1989]

23 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

Node Removal: Example from Pereira andSheiber [1989]

I How to get rid of the node without terminal span ?

I Solution: add a special builder predicate

I define a predicate build np:

build np(Det, N, rel(epsilon), np(Det,N))

build np(Det, N, rel(that,VP), np(Det,N,

rel(that,VP)))

I define np rule as follows:

np(NP) --> det(Det), n(N), optrel(Rel),

{build np(Det,N, Rel, NP)}

24 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

Lexicon Simplification

I DCG lexicon so far:

n --> [house].

n --> [program].

det --> [the].

I new idea:

n --> [Word], {n(Word)}n(program).

n(house)

I instead of many rules, one rule per POS

25 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

Lexicon Simplification

I also applicable to parse tree grammar:

n(n(Word)) --> [Word], {n(Word)}.n(Number) --> [Word], {n(Word, Number)}.n(program).

n(professors, plural).

26 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

Lexicon Simplification

I even more radical approach: store singular and pluraltogether

n(program, programs).

n(professor, professors).

n(singular) --> [Word], {n(Word, )}.n(plural) --> [Word], {n( , Word)}.

27 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

Lexicon Simplification

see file dcg-t-compact.pl

28 / 29

P&S 3.5, 3.7: Thelogic and operationof Prolog revisited,

definite clausegrammar

Bjorn Rudzewitz

Logic andOperationRevisited

Substitution

Unification

Semantics of Prolog

Definite ClauseGrammars

Parse Trees

Tree Operations inProlog

Removal of Nodes

Lexicon Simplification

References

Patrick Blackburn, Johannes Bos, Kristina Striegnitz, et al.Learn prolog now!, volume 7. College PublicationsLondon, 2006.

Fernando Pereira and Stuart Sheiber. Prolog andnatural-language analysis: Fernando cn pereira and stuartm. sheiber, csli lecture notes 10 (the university of chicagopress, il, 1987); 266 pages, 13.95(paperback),28.95(cloth)., 1989.

29 / 29