50 likes | 487 Views
Pattern Matching in Prolog. f rom simple to complicated … let’s review how it works Database : suppose we have p(1). p(a(1)). p(a(b(1,2),3)). pattern matching (PM) in queries …. functors can’t be variables in Prolog. Pattern Matching in Prolog. Pattern matching (PM) in programs
E N D
Pattern Matching in Prolog from simple to complicated … let’s review how it works • Database: suppose we have • p(1). • p(a(1)). • p(a(b(1,2),3)). • pattern matching (PM) in queries … functors can’t be variables in Prolog
Pattern Matching in Prolog Pattern matching (PM) in programs • Suppose we have defined • q(X,Y) :- p(a(X,Y)). • already have: • p(1). • p(a(1)). • p(a(b(1,2),3)). PM q(A,B) q(X,Y) A=X, B=Y PM p(a(X,Y)) p(1) PM a(X,Y) 1fails PM p(a(X,Y)) p(a(1)) PM a(X,Y) a(1) fails PM p(a(X,Y)) p(a(b(1,2),3)) PM a(X,Y) a(b(1,2),3) PM X b(1,2) X=b(1,2) PM Y 3 Y=3
From lecture 9 • Example: [S [NP [Q every][N man]][VP [V likes][NP John]]] • WordExpression • every λP1.[λP2.[∀X (P1(X) -> P2(X))]] • man man • likes λY.[λX.[ X likes Y]] • John John • Logic steps: • [Q every][N man]] λP1.[λP2.[∀X (P1(X) -> P2(X))]](man) • [Q every][N man]] λP2.[∀X (man(X) -> P2(X))] • [VP [V likes][NP John]] λY.[λX.[ X likes Y]](John) • [VP [V likes][NP John]] λX.[ X likes John] • [S [NP [Q every][N man]][VP [V likes][NP John]]] • λP2.[∀X (man(X) -> P2(X))](λX.[ X likes John]) • ∀X (man(X) -> λX.[ X likes John](X)) • ∀X (man(X) -> [ X likes John]) Let’s take a look at how to compute this …
Pattern Matching in Prolog • Given: • likes λY.[λX.[ X likes Y]] • Prolog representation is: lambda(Y,lambda(X,likes(X,Y))) • pattern matching (PM) this in a program … • Given grammar rule fragment: vp(LE2) --> v(LE1), np(X). v(lambda(Y,lambda(X,likes(X,Y))) --> [likes]. np(john) --> [john]. • How do we define LE1 and LE2 (lambda expression)? so the VP rule returns: lambda(X,likes(X,john) Answer: using PM substitute lambda(X,LE2) forLE1in VP rule
Pattern Matching in Prolog • WordSemanticExpression • every λP1.[λP2.[∀X (P1(X) -> P2(X))]] • man man • Logic steps: • [Q every][N man] λP1.[λP2.[∀X (P1(X) -> P2(X))]](man) • [Q every][N man] λP2.[∀X (man(X) -> P2(X))] • λP1.[λP2.[∀X (P1(X) -> P2(X))]] Illegal: lambda(P1,lambda(P2,(\+ (P1(X), \+ P2(X))))) Alternate: lambda(P1,lambda(P2,(\+ (call(P1,X),\+ call(P2,X))))) Let’s write the grammar rule fragment for [NP[Q every][N man]]