On Mon, Sep 17, 2012 at 3:00 AM, Mark Green <mark(a)antelope.nildram.co.uk> wrote:
Hi,
Thanks for your reply. I was interested in an example of how the hook predicates work. For example, if I wished to have a pattern as follows:
The PROPERTY of SUBJECT is OBJECT.
Where PROPERTY, SUBJECT, and OBJECT are atoms and "the", "of", and "is" are keywords, to parse as PRED(SUBJECT,OBJECT); how would that be done?
Using hook predicates for language extensions (term_expansion, etc.) allow some pre-processing at the abstract-syntax tree level (i.e., terms). Currently, it is not possible to redefine the parser itself with hooks.
So one cheap option here is to define the keywords as prefix, infix, or postfix operators with the right priorities. That is, read:
the PROPERTY of SUBJECT is OBJECT.
as:
(the (PROPERTY of SUBJECT)) is OBJECT.
For example:
?- op(650, fx, the). ?- op(640, xfx, of). ?- X = (the brother of john is paul). X = (the brother of john is paul) ?
which is equivalent to "X = is(the(of(brother, john)), paul)", which resembles your PRED(SUBJECT, OBJECT) term. This has the great advantage that you can merge it with Prolog syntax, but it also has some limitations (mostly, it is useful only for toy examples).
For more complex stuff you probably need your own reader and parser predicates (e.g., get codes from the stream and parse them with a DCG).