Re: pilog and Definite Clause Grammar (DCG)

2011-07-20 Thread Alexander Burger
Hi Doug,

> Might be something to consider adding to pilog.l Seems to be fairly
> standard in prologs - and useful for creating parsers :-)

OK, so let's see how it works out. Should perhaps be put into the
add-ons.


To make such rule declarations easier in the future, I've added a new
'clause' function, an evaluating equivalent of 'be'. It is available in
version 3.0.7.4 (current testing release), and also in Mini and Ersatz.

With that, the definition of 'dcg' can be slightly simplified:

   (de dcg CL
  (clause
 (if (pair (cadr CL))
(list
   (car CL)
   (list (cons (caadr CL) '"@A") '"@A") )
(cons
   (car CL)
   '("@In" "@Out")
   (let Vars
  (make
 (link "@In")
 (for (I . @) (cddr CL)
(link (pack "@" I)) )
 (link "@Out") )
  (mapcar list
 (cdr CL)
 Vars
 (cdr Vars) ) ) ) ) ) )

'be' makes use of it:

   (de be CL
  (clause CL) )

Cheers,
- Alex
-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


Re: pilog and Definite Clause Grammar (DCG)

2011-07-20 Thread Doug Snead

THANK YOU! 

I haven't tested that yet but I think that is exactly what I was looking for 
there.  Might be something to consider adding to pilog.l  Seems to be fairly 
standard in prologs - and useful for creating parsers :-)

Beautiful code, thanks again so much! 

Cheers,

Doug




--- On Wed, 7/20/11, Alexander Burger  wrote:

> From: Alexander Burger 
> Subject: Re: pilog and Definite Clause Grammar (DCG)
> To: picolisp@software-lab.de
> Date: Wednesday, July 20, 2011, 2:20 AM
> Hi Doug,
> 
> > What's the best way to handle Definite Clause Grammar
> syntax in pilog?
> > ..
> > So, I'm hoping for some pilog rules or a macro maybe
> that can handle
> > the --> (or equivalent) of DCG in pilog :-)
> 
> I don't fully understand the context or purpose, but
> 
>    http://en.wikipedia.org/wiki/Definite_clause_grammar
> 
> says that "DCG notation is just syntactic sugar for normal
> definite
> clauses in Prolog".
> 
> So you can write a function 'dcg' which does the necessary
> translation.
> 
> I would simply call 'be' here, using 'macro' because 'be'
> doesn't
> evaluate its arguments (i.e. is a FEXPR):
> 
>    (de dcg CL
>       (macro
>          (be
>             ^(if (pair (cadr
> CL))
>            
>    (list (car CL) (list (cons (caadr CL) '@A)
> '@A))
>            
>    (cons
>                
>   (car CL)
>                
>   '("@In" "@Out")
>                
>   (let Vars
>                
>      (make
>                
>         (link "@In")
>                
>         (for (I . @) (cddr CL)
>                
>            (link (pack
> "@" I)) )
>                
>         (link "@Out") )
>                
>      (mapcar list
>                
>         (cdr CL)
>                
>         Vars
>                
>         (cdr Vars) ) ) ) ) ) ) )
> 
> With that, the example in Wikipedia could be written as
> 
>    (dcg sentence  nounPhrase
> verbPhrase)
>    (dcg nounPhrase  det noun)
>    (dcg verbPhrase  verb nounPhrase)
>    (dcg det [the])
>    (dcg det [a])
>    (dcg noun [cat])
>    (dcg noun [bat])
>    (dcg verb [eats])
> 
> This expands to
> 
>    : (rules 'sentence 'nounPhrase
> 'verbPhrase 'det 'noun 'verb)
>    1 (be sentence ("@In" "@Out") (nounPhrase
> "@In" "@1") (verbPhrase "@1" "@Out"))
>    1 (be nounPhrase ("@In" "@Out") (det
> "@In" "@1") (noun "@1" "@Out"))
>    1 (be verbPhrase ("@In" "@Out") (verb
> "@In" "@1") (nounPhrase "@1" "@Out"))
>    1 (be det ((the . @A) @A))
>    2 (be det ((a . @A) @A))
>    1 (be noun ((cat . @A) @A))
>    2 (be noun ((bat . @A) @A))
>    1 (be verb ((eats . @A) @A))
>    -> verb
> 
> Does this help?
> 
> Cheers,
> - Alex
> -- 
> UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
>
--
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe


Re: pilog and Definite Clause Grammar (DCG)

2011-07-20 Thread Alexander Burger
Hi Doug,

> What's the best way to handle Definite Clause Grammar syntax in pilog?
> ...
> So, I'm hoping for some pilog rules or a macro maybe that can handle
> the --> (or equivalent) of DCG in pilog :-)

I don't fully understand the context or purpose, but

   http://en.wikipedia.org/wiki/Definite_clause_grammar

says that "DCG notation is just syntactic sugar for normal definite
clauses in Prolog".

So you can write a function 'dcg' which does the necessary translation.

I would simply call 'be' here, using 'macro' because 'be' doesn't
evaluate its arguments (i.e. is a FEXPR):

   (de dcg CL
  (macro
 (be
^(if (pair (cadr CL))
   (list (car CL) (list (cons (caadr CL) '@A) '@A))
   (cons
  (car CL)
  '("@In" "@Out")
  (let Vars
 (make
(link "@In")
(for (I . @) (cddr CL)
   (link (pack "@" I)) )
(link "@Out") )
 (mapcar list
(cdr CL)
Vars
(cdr Vars) ) ) ) ) ) ) )

With that, the example in Wikipedia could be written as

   (dcg sentence  nounPhrase verbPhrase)
   (dcg nounPhrase  det noun)
   (dcg verbPhrase  verb nounPhrase)
   (dcg det [the])
   (dcg det [a])
   (dcg noun [cat])
   (dcg noun [bat])
   (dcg verb [eats])

This expands to

   : (rules 'sentence 'nounPhrase 'verbPhrase 'det 'noun 'verb)
   1 (be sentence ("@In" "@Out") (nounPhrase "@In" "@1") (verbPhrase "@1" 
"@Out"))
   1 (be nounPhrase ("@In" "@Out") (det "@In" "@1") (noun "@1" "@Out"))
   1 (be verbPhrase ("@In" "@Out") (verb "@In" "@1") (nounPhrase "@1" "@Out"))
   1 (be det ((the . @A) @A))
   2 (be det ((a . @A) @A))
   1 (be noun ((cat . @A) @A))
   2 (be noun ((bat . @A) @A))
   1 (be verb ((eats . @A) @A))
   -> verb

Does this help?

Cheers,
- Alex
-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe