Reducing Complex DCGs Prolog

180 views Asked by At

How do I reduce a DCG rule like this dtv(P1^P2^P3^Q1) using apply(X^P,X,P)? I'm trying to describe the semantics of different grammatical components and I'm using lambda calculus. This is what I have for ditransitive verbs, however, the second apply function doesn't work and I wonder what I'm doing wrong.

dtv(P1^P2^P3^Q1)-->[gave],
   {apply(P1,X^Q3,Q1)},
   {apply(P,Y^Q3, P2)},
   {apply(P3,Z^gave(X,Y,Z),Q3)}.

This is the full code below:

    % Apply function takes a lambda expression in the form X^P. It unifies X
    % in the first argument with the second argument to get a result P
    apply(X^P, X,P).
    %%%%%%%%
    %SYNTAX
    %%%%%%%%


    % I converted the lambda symbol to ^ in order to make it consistent with
    % the prolog convention and also the library module on lambda.

    s(S) --> np(NP), vp(VP),
    {apply(VP, NP, S)}.%This applies NP and VP to get S

    adjp(Adjp)-->adj(Adj),noun(X^P),
        {apply(Adj,X^P,Adjp)}.%This applies Adjective to noun to get an
    %   adjective Phrase

    np(NP) --> det(Det), noun(X^P), {apply(Det, X^P, NP)}.%Applies Determiner and Noun to get NounPhrase
    np(NP) --> pn(NP).

    np(NP)-->det(Det), adjp(Adjp),
        {apply(Det,Adjp,NP)}.%This applies determiner to adjective phrase to get a Noun Phrase

    pp(PP)-->prep(Prep),np(NP),%prepositional phrase
        {apply(Prep,NP,PP)}.                                                                  
    mp(MP)--> dtv(DTV),np(NP),
        {apply(DTV,NP,MP)}.%This applies ditransitive verb
    %noun phrase to get a modal phrase.

    vp(VP)--> tv(TV), np(NP),
        {apply(TV, NP, VP)}.%This applies transitive verb, noun phrase
    %   to get a verb phrase.


    vp(VP)--> modal(M),np(NP),
        {apply(M, NP, VP)}.%This applies modal (to be) to an adjective to get a verb phrase.

    vp(VP)--> mp(MP),pp(PP),
        {apply(MP,PP,VP)}.%This applies ditransitive verb,
    %noun phrase to get a verb phrase.

    vp(VP) --> iv(VP).

    %%%%%%%%
    % LEXICON
    %%%%%%%%

    prep(X^to(X))-->[to].
    modal(R^S^M)-->[is],
        {apply(R,X,M)},
        {apply(S,X,M)}.
    noun(X^food(X))-->[food].
    noun(X^girl(X))-->[girl].

    adj((X^P)^(Y^and(P,beautiful(Y))))-->[beautiful].

    pn(P^Q)-->[jane],
        {apply(P,jane,Q)}.
    iv(P^Q)-->[runs],
        {apply(P,X^runs(X),Q)}.
    iv(P^Q)-->[sings],
        {apply(P,X^sings(X),Q)}.

    tv(P1^P2^Q2)-->[loves],
        {apply(P2,X^Q1,Q2)},
        {apply(P1,Y^loves(X,Y),Q1)}.

    %Problem spot

dtv(P1^P2^P3^Q1)-->[gave],
    {apply(P1,X^Q3,P2)},
    {apply(P2^P3,Y^Q3, P)},
    {apply(Q1,Z^gave(X,Y,Z),Q3)}.

det(P1^P2^the(X,Q1,Q2))-->[the],
    {apply(P1,X,Q1)},
    {apply(P2,X,Q2)}.
det(P1^P2^a(X,Q1,Q2))-->[a],
    {apply(P1,X,Q1)},
    {apply(P2,X,Q2)}.
1

There are 1 answers

0
false On

So, let me start with a recommendation: Read at least Chapter 4 of Prolog and Natural-Language Analysis, pdf. It covers exactly what you look at — with the exception that apply/3 is called reduce/3. This apply/3 is a bit of a formalistic way to explain things, that is parameter passing. It seems that, for many examples you mixed it up a bit.

As a first step consider this part of your program:

apply(X^P,X,P).

s(S) --> np(NP), vp(VP), {apply(VP, NP, S)}.

This corresponds1 to:

s(S) --> np(NP), vp(NP^S).

So effectively, there is no need to use apply/3 - it's a nice device, but finally, above notation is even more compact, as it uses less variables.

I recommend you clean up your code, I found it very hard to read.


Footnotes you can safely ignore
1 Actually, the correspondence is a declarative only, the transformation might improve (= change) termination of vp//1.