[Date Prev] [Date Next] [Thread Prev] [Thread Next] Date Index Thread Index Search archive:
Date:Mon May 1 18:15:48 1999 
Subject:Re: Sample Pop-11 Code? 
From:Aaron Sloman See text for reply address 
Volume-ID:990501.01 

David Whitten <whitten@netcom.com> writes:

> Date: Fri, 30 Apr 1999 19:00:15 GMT
>
> Okay, let's say I want to write some Pop-11 code to learn more about
> the langauge.  Now, as I recall, Pop-11 follows an open-stack model.

Yes, but for this purpose it may be best to ignore the open stack and
use a standard the functional style, which is one of the options
available in Pop-11. See
    ftp://ftp.cs.bham.ac.uk/pub/dist/poplog/teach/functional.style
    ftp://ftp.cs.bham.ac.uk/pub/dist/poplog/teach/recursion

Sometimes it is clearer to combine recursion with a loop: recursion
explores further options, while backtracking (fail) takes you back to a
loop which manages alternatives at the same level. However both can be
done purely with recursion (though probably with less clarity).
(Sometimes using closures is useful, for saving state.)

> So if I wanted to implement a parser for the following language:
>
> Sentence <- NounPhrase VerbPhrase Adverb
> Sentence <- NounPhrase VerbPhrase PrepostionalPhrase

The Pop-11 library has a parser (actually two versions) which can
handle recursive context free grammars expressed as lists of lists of
lists, e.g. this "toy" grammar, which is in one of the demo libraries:

    [
    ;;; alernative sentence formats
    [s
        [if s then s]
        [np vp]
        [sadv np vp]
        [s conj s]
        ]
    ;;; alnternative noun phrase formats
    [np
        [pn]
        [snp]
        [snp pp]
        [snp relp]
        [np and np]
        ]
    ;;; relative clause phrases
    [relp
        [rel vp]
        [rel np vnp]
        ]
    ;;; predicates
    [pred
        [adj]
        [a qn]
        [np]
        ]
    ;;; prepositional phrases
    [ppatnp     [at np]]
    [pptonp     [to np]]
    [ppfornp    [for np]]
    [pponnp     [on np]]
    ... etc. etc.
    [ppnpbynp   [np by np]]
    [ppnpfromnp [np from np]]
    ... etc. etc.
    [pp     [prep np]]
    ;;; verb phrases
    [vp
        [cop pred]
        [vatnp ppatnp]
        [vtonp  pptonp]
        [vfornp  ppfornp]
        ... etc. etc.
        [pv that s]
        [vnp np]
        [v]
        [vp and vp]
        [vp adv]
        [adv vp]
        ]
    ;;; simple noun phrase
    [snp
        [det qn]
        ]
    ;;; qualified noun
    [qn
        [noun]
        [adj qn]
        ]
    ]

With a toy lexicon listing terminals in the form of a list of lists.
    [ [noun ....] [adj ....] [cop ....] [vatnp ....] ]

There's a tutorial introduction this kind of grammar and a parser and
sentence generator in

    ftp://ftp.cs.bham.ac.uk/pub/dist/poplog/teach/grammar

Poplog (including Pop-11) is shortly to become available free of
charge. I don't know exactly when -- see
    ftp://ftp.cs.bham.ac.uk/pub/dist/poplog/poplog.info.html

I have therefore taken the liberty of copying two of the Poplog
Pop-11 demonstration code libraries to our ftp site:

    ftp://ftp.cs.bham.ac.uk/pub/dist/poplog/temp/lib/grammar.p
        This shows how to compile a grammar and lexicon into a
        a parser, or more precisely, a family of parsers: one for
        each non-terminal. It uses the Pop-11 built in Pop-11
        pattern matcher. (I would probably do it differently now, if
        I had time!). It also includes a random sentence genrator
        which randomly generates sentences, given a grammar and a
        lexicon. This is the library used in the teach/grammar file
        mentioned above.

    ftp://ftp.cs.bham.ac.uk/pub/dist/poplog/temp/lib/tparse.p
        The previous parser finds one parse. Often there are
        several parses of a given list of words consistent with a
        given grammar. The tparse library written by John Gibson
        is a more elegant program which returns all the parses.
        It requres a "state saving" facility which enables it to
        backtrack into a previously completed call of the parser
        to look for additional parses of previously parsed
        substructures. This uses the Pop-11 lightweight process
        mechanism, including the produre consproct_to which can
        create a new process based on part of the current calling
        stack. The process can be saved, then run later, then
        suspended, then re-entered, etc.
        (Compare Call/cc in Scheme.)

All these are very old Pop-11 programs, originally written in the
1970s or 1980s using early versions of Pop-11. Probably a different
style would be used nowadays.

I have just remembered that there is a comprehensive introduction to
Natural Language Processing in Pop-11, by Gerald Gazdar and Chris
Mellish:
%A Gazdar, G.
%A Mellish, C.
%T Natural Language Processing in POP-11,
%I Addison Wesley
%D 1989.
%O Versions for Prolog and Common Lisp also available.

All(??) the program code from that book (including Pop-11, Prolog and
Lisp versions) is available in
    ftp://ftp.cs.bham.ac.uk/pub/dist/poplog/contrib/nlp_book

also included in the poplog contrib tar file
    ftp://ftp.cs.bham.ac.uk/pub/dist/poplog/contribtar.gz

It includes code for several kinds of parsers, including chart parsers.
There's a simple top down parser using the Pop-11 pattern matcher in

    ftp://ftp.cs.bham.ac.uk/pub/dist/poplog/contrib/nlp_book/pop11/tdparse.p

Without the pattern matcher it would be slightly more complex. E.g.

            if rule matches [^goal ??rhs] then
                <code using rhs>

if "goal" is an atom, not a pattern, would translate into raw Pop-11 as

            if hd(rule) = goal then
                tl(rule) -> rhs;
                <code using rhs>

The Pop-11 matcher is summarised in chapter 7 of the primer
    ftp://ftp.cs.bham.ac.uk/pub/dist/poplog/primer/START.html

The file describing the contents of the Gazdar&Mellish
pop11 files is

     ftp://ftp.cs.bham.ac.uk/pub/dist/poplog/contrib/nlp_book/pop11/files.doc

> And I wanted to do this as a recursive descent parser that backtracks,
> how would I code it?

Pop-11 like most AI languages allows you umpteen different ways of
doings things, whose relative merits may depend on the context.

> Note: I have three goals with this question:
>
> 1) I want to see some sample Pop-11 code

There is lots in the Primer, referenced above. Even more in the other
online stuff in the Birmingham poplog ftp directory, as described in the
README file:
    ftp://ftp.cs.bham.ac.uk/pub/dist/poplog

There are also examples (including parsing) in both Pop-11 and Prolog in

%A Chris Thornton
%A Benedict du Boulay
%T Artificial Intelligence Through Search
%I Kluwer Academic (Paperback version Intellect Books)
%C Dordrecht Netherlands & Norwell, MA USA (Intellect at Oxford)
%D 1992

> 2) I want to see a code fragment that implements a recursive descent parser
> 3) I want to see how to implement backtracking (a la Prolog) in a system
> that doesn't provide it 'under the covers'
(Combine loops and recursion, with closures, or processes if necessary.)

When you get Poplog you also get libraries showing how to implement
incremental compilers for

    Prolog (using the standard "Edinburgh" syntax)
    Common Lisp (only compiled, not interpreted).
    Standard ML

If anyone is interested I could put those libraries online in our ftp
directory, since they will all become freely available in the near
future anyway.

If you want to see some non-trivial Pop-11 implementing a rather
flexible forward chaining production system interpreter described in

    ftp://ftp.cs.bham.ac.uk/pub/dist/poplog/prb/help/poprulebase

see the main code file in
    ftp://ftp.cs.bham.ac.uk/pub/dist/poplog/prb/lib/poprulebase.p

Pop-11 is largely similar in power to Common Lisp, but each has some
features the other lacks, and many people love the syntax of Lisp,
whereas many others hate it and find Pop-11's syntax (for equivalent
code) more readable. I personally think it's a far better teaching
language for beginners learning AI/Cognitive Science.

Aaron
===
-- 
Aaron Sloman, ( http://www.cs.bham.ac.uk/~axs/ )
School of Computer Science, The University of Birmingham, B15 2TT, UK
EMAIL A.Sloman AT cs.bham.ac.uk   (NB: Anti Spam address)
PAPERS: ftp://ftp.cs.bham.ac.uk/pub/groups/cog_affect/0-INDEX.html