From: Chris Wright
Subject: compile-predicate in PAIP
Date: 
Message-ID: <d2dc3928.0402232116.5a34b073@posting.google.com>
Norvig has the following in PAIP

(defun compile-predicate (symbol arity clauses)
  "Compile all the clauses for a given symbol/arity into a single LISP
function."
  (let ((predicate (make-predicate symbol arity))
        (parameters (make-paramenters arity)))
    (compile
     (eval
      `(defun ,predicate (,@parameters cont)
         .,(mapcar #'(lambda (clause)
                       (compile-clause parameters clause 'cont))
                   clauses))))))


I have two questions:

Why is eval used here? Is it to get "macro-substitution" within
`(defun ,predicate ....)?

What is the ".," syntax? Is this a dotted list "." followed by comma,
or is ".," a form with meaning?


Thanks for the help

chris wright

From: Rahul Jain
Subject: Re: compile-predicate in PAIP
Date: 
Message-ID: <87n079c9bp.fsf@nyct.net>
···@cs.mu.oz.au (Chris Wright) writes:

> Norvig has the following in PAIP
>
> (defun compile-predicate (symbol arity clauses)
    ...)

> I have two questions:
>
> Why is eval used here? Is it to get "macro-substitution" within
> `(defun ,predicate ....)?

You'll notice that the result of the EVAL is COMPILEd. The defun is
EVALed so that the function gets defined in the usual way and then the
compiler optimizes it in the usual way.

You could instead do (compile predicate `(lambda (,@parameters cont)
...)) but that would not allow any implementation-specific optimization
of self-recursion to take place, nor would it allow any
implementation-specific "tagging" of function names with information
about the function, for example. There's no guarantee that COMPILE will
be implemented in such a way that the compiler will know that the form
being compiled will be given the name in the variable PREDICATE.

 [Aside:] I'd prefer if the arity of the relation were not even encoded
 in the predicate name. I would rather that it be one of the matching
 criteria used for the clause being defined. This way dangerous (as
 though there is non-dangerous) symbol munging would be unnecessary, and
 &rest-like matching could be implemented. (Something like &optional
 might make sense in this context, but &key probably wouldn't.)

> What is the ".," syntax? Is this a dotted list "." followed by comma,
> or is ".," a form with meaning?

The former. It can be replaced with ",@" with no change in effect.

-- 
Rahul Jain
·····@nyct.net
Professional Software Developer, Amateur Quantum Mechanicist
From: Henrik Motakef
Subject: Re: compile-predicate in PAIP
Date: 
Message-ID: <87u11h3tsj.fsf@aenaeas.internal.henrik-motakef.de>
···@cs.mu.oz.au (Chris Wright) writes:

> (defun compile-predicate (symbol arity clauses)
>   "Compile all the clauses for a given symbol/arity into a single LISP
> function."
>   (let ((predicate (make-predicate symbol arity))
>         (parameters (make-paramenters arity)))
>     (compile
>      (eval
>       `(defun ,predicate (,@parameters cont)
>          .,(mapcar #'(lambda (clause)
>                        (compile-clause parameters clause 'cont))
>                    clauses))))))

> Why is eval used here? Is it to get "macro-substitution" within
> `(defun ,predicate ....)?

`(defun ,predicate ...) builds a list, not a function, it is
equivalent to (list 'defun predicate ...). Eval treats this list as
code, resulting in the function to be defined. Since defun returns the
function name, this is then passed to compile, which compiles an
existing function. Another possibility would have been using the
two-argument form of compile, as in (compile 'plus '(lambda (x y) (+ x y))).

The substitution is done by backquote, not eval.

> What is the ".," syntax? Is this a dotted list "." followed by comma,
> or is ".," a form with meaning?

I suppose it is ",.", not ".,"? If so:

| Anywhere ``,@'' may be used, the syntax ``,.'' may be used instead to
| indicate that it is permissible to operate destructively on the list
| structure produced by the form following the ``,.'' (in effect, to
| use nconc instead of append).
2.4.6 Backquote, <http://www.lispworks.com/reference/HyperSpec/Body/02_df.htm>
From: [Invalid-From-Line]
Subject: Re: compile-predicate in PAIP
Date: 
Message-ID: <NaF_b.152$ej2.6896@nnrp1.ozemail.com.au>
[helpful explanation of eval ]

> > What is the ".," syntax? Is this a dotted list "." followed by comma,
> > or is ".," a form with meaning?
>
> I suppose it is ",.", not ".,"? If so:
 um, nope, its ".," The file is at http://www.norvig.com/paip/README.html
and is called  prologc1.lisp and there is no mention in the errata of
changes to this code.

He also has some other examples in PAIP which use the ., form

Thanks for the help

>
> | Anywhere ``,@'' may be used, the syntax ``,.'' may be used instead to
> | indicate that it is permissible to operate destructively on the list
> | structure produced by the form following the ``,.'' (in effect, to
> | use nconc instead of append).
> 2.4.6 Backquote,
<http://www.lispworks.com/reference/HyperSpec/Body/02_df.htm>
From: Matthew Danish
Subject: Re: compile-predicate in PAIP
Date: 
Message-ID: <20040224101455.GB31147@mapcar.org>
On Tue, Feb 24, 2004 at 08:51:55PM +1100, ··········@ozemail.com.au wrote:
> > > What is the ".," syntax? Is this a dotted list "." followed by comma,
> > > or is ".," a form with meaning?
> >
> > I suppose it is ",.", not ".,"? If so:
>  um, nope, its ".," The file is at http://www.norvig.com/paip/README.html
> and is called  prologc1.lisp and there is no mention in the errata of
> changes to this code.

Yes, it is dotted list followed by unquote.  In (x ., y) or (x . ,y) it
is structurally equivalent to (list* 'x y) or (cons 'x y) in this case.
The splicing operator ,@ may be used instead, in addition it may be used
to splice a list in at any point.  `(,@list-1 ,@list-2) for example.

-- 
; Matthew Danish <·······@andrew.cmu.edu>
; OpenPGP public key: C24B6010 on keyring.debian.org
; Signed or encrypted mail welcome.
; "There is no dark side of the moon really; matter of fact, it's all dark."