Hi,
I'm trying to translate a small scheme library to common lisp and came
across something I'm not sure how to translate.
Given the definition,
(define (filter pred?)
.....)
the scheme library defines what looks like an alias
(define node-self filter)
I tried
(setf (symbol-function 'node-self)
(function filter))
and I think it does what I want but I get compile warnings that node-
self isn't defined. Is there another way of doing this that avoids
the warnings?
Cheers,
Andy
On Aug 3, 10:02 am, Andy Chambers <··············@googlemail.com>
wrote:
> Hi,
>
> I'm trying to translate a small scheme library to common lisp and came
> across something I'm not sure how to translate.
>
> Given the definition,
>
> (define (filter pred?)
> .....)
>
> the scheme library defines what looks like an alias
>
> (define node-self filter)
>
> I tried
>
> (setf (symbol-function 'node-self)
> (function filter))
>
> and I think it does what I want but I get compile warnings that node-
> self isn't defined. Is there another way of doing this that avoids
> the warnings?
Depends a lot on how node-self gets used in the program;
if it gets used as a constant, maybe a defun or defmacro,
but I'd lean towards:
(defparameter *node-self* #'filter)
and replace the (node-self ...) calls with:
(funcall *node-self* ...)
---
Geoff
> I tried
>
> (setf (symbol-function 'node-self)
> (function filter))
>
> and I think it does what I want but I get compile warnings that node-
> self isn't defined. Is there another way of doing this that avoids
> the warnings?
NODE-SELF is just like any other variable, and should
be defined before use. (defvar node-self) will do the
trick.
- Scott
Andy Chambers wrote:
> Hi,
>
> I'm trying to translate a small scheme library to common lisp and came
> across something I'm not sure how to translate.
>
> Given the definition,
>
> (define (filter pred?)
> .....)
>
> the scheme library defines what looks like an alias
>
> (define node-self filter)
>
>
> I tried
>
> (setf (symbol-function 'node-self)
> (function filter))
>
> and I think it does what I want but I get compile warnings that node-
> self isn't defined. Is there another way of doing this that avoids
> the warnings?
(declaim (inline node-self))
(defun node-self (pred?)
(filter pred?))
The other suggestions to use defvar or defparameter lead in the wrong
direction.
Pascal
--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
On Fri, 03 Aug 2007 18:30:05 +0200, Pascal Costanza wrote:
> Andy Chambers wrote:
>> Hi,
>>
>> I'm trying to translate a small scheme library to common lisp and came
>> across something I'm not sure how to translate.
>>
>> Given the definition,
>>
>> (define (filter pred?)
>> .....)
>>
>> the scheme library defines what looks like an alias
>>
>> (define node-self filter)
>>
>>
>> I tried
>>
>> (setf (symbol-function 'node-self)
>> (function filter))
>>
>> and I think it does what I want but I get compile warnings that node-
>> self isn't defined. Is there another way of doing this that avoids
>> the warnings?
>
> (declaim (inline node-self))
> (defun node-self (pred?)
> (filter pred?))
>
Hmm - either this was typed too fast or i miss something ... shouldn't
that be:
(declaim (inline filer))
(defun node-self (pred?)
(filter pred?))
Cheers, Ralf Mattes
> The other suggestions to use defvar or defparameter lead in the wrong
> direction.
>
>
> Pascal
>
Ralf Mattes wrote:
> On Fri, 03 Aug 2007 18:30:05 +0200, Pascal Costanza wrote:
>
>> Andy Chambers wrote:
>>> Hi,
>>>
>>> I'm trying to translate a small scheme library to common lisp and came
>>> across something I'm not sure how to translate.
>>>
>>> Given the definition,
>>>
>>> (define (filter pred?)
>>> .....)
>>>
>>> the scheme library defines what looks like an alias
>>>
>>> (define node-self filter)
>>>
>>>
>>> I tried
>>>
>>> (setf (symbol-function 'node-self)
>>> (function filter))
>>>
>>> and I think it does what I want but I get compile warnings that node-
>>> self isn't defined. Is there another way of doing this that avoids
>>> the warnings?
>> (declaim (inline node-self))
>> (defun node-self (pred?)
>> (filter pred?))
>>
> Hmm - either this was typed too fast or i miss something ... shouldn't
> that be:
>
> (declaim (inline filer))
> (defun node-self (pred?)
> (filter pred?))
The assumption in my posting was that filter is part of some third-party
library. IMHO, you shouldn't declaim things about other people's code,
because you never know what they want to do with such code internally.
If filter is your own function, then declaiming it inline is fine - but
then I don't get why you need two functions that do the same.
Anyway, maybe I am a bit too careful here, so your suggestion is
probably also fine.
Pascal
--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
On Sat, 04 Aug 2007 14:34:49 +0200, Pascal Costanza wrote:
> Ralf Mattes wrote:
>> On Fri, 03 Aug 2007 18:30:05 +0200, Pascal Costanza wrote:
>>
>>> Andy Chambers wrote:
>>>> Hi,
>>>>
>>>> I'm trying to translate a small scheme library to common lisp and came
>>>> across something I'm not sure how to translate.
>>>>
>>>> Given the definition,
>>>>
>>>> (define (filter pred?)
>>>> .....)
>>>>
>>>> the scheme library defines what looks like an alias
>>>>
>>>> (define node-self filter)
>>>>
>>>>
>>>> I tried
>>>>
>>>> (setf (symbol-function 'node-self)
>>>> (function filter))
>>>>
>>>> and I think it does what I want but I get compile warnings that node-
>>>> self isn't defined. Is there another way of doing this that avoids
>>>> the warnings?
>>> (declaim (inline node-self))
>>> (defun node-self (pred?)
>>> (filter pred?))
>>>
>> Hmm - either this was typed too fast or i miss something ... shouldn't
>> that be:
>>
>> (declaim (inline filer))
>> (defun node-self (pred?)
>> (filter pred?))
>
> The assumption in my posting was that filter is part of some third-party
> library. IMHO, you shouldn't declaim things about other people's code,
> because you never know what they want to do with such code internally.
> If filter is your own function, then declaiming it inline is fine - but
> then I don't get why you need two functions that do the same.
Ah, then I _was_ thinking in a different ditrection. I assumed that you
thought that the OP wanted to alias a given function and thta your code
introduced an extra funcall.
> Anyway, maybe I am a bit too careful here, so your suggestion is
> probably also fine.
Is there actually a standard way to restrict function inlining to a
compilation unit?
Cheers, RalfD
>
> Pascal
>
Ralf Mattes <··@mh-freiburg.de> writes:
> > Anyway, maybe I am a bit too careful here, so your suggestion is
> > probably also fine.
>
> Is there actually a standard way to restrict function inlining to a
> compilation unit?
[I didn't read upthread to see what the context was so I hope answering
this without regard to prior context works ok for you.]
I don't think there's a facility for doing that task in precisely that
way--that is, a compilation unit is not really a first-class thing that
much of anything reliably respects. It was just an arifact of some
implementations that they tried to unify the compilation space of several
files, and we tried to create some minimal accommodation to it in the
language so that we didn't have to make illegal the idea of delaying
compilation warnings across a set of files. But it's not something we
leaned heavily on requiring.
CL generally intends that modularity boundaries are gated by package,
not by compilation unit, in part because CL's facilities for
modularity and redefinition assume that you might later need to
compile additional code that is loaded into the same general semantic
space even though compiled in another compilation unit.
How about putting the function to be inlined in package used only by
that compilation unit and then not exporting the symbol, so that only
things done in that package see the symbol at all, and yet all the
things seeing that symbol are inlined...?
You could also wrap a big (locally (declare (inline ...)) ...) around
the region, I guess.
Or you could do (proclaim '(inline ...)) at the start and
(proclaim '(notinline ...)) after.
Ralf Mattes <··@mh-freiburg.de> writes:
>
> Is there actually a standard way to restrict function inlining to a
> compilation unit?
Would wrapping a
(locally (declare (inline ...))
...)
around it do what you want?
--
Thomas A. Russ, USC/Information Sciences Institute
On Fri, 03 Aug 2007 07:02:24 -0700, Andy Chambers wrote:
> Hi,
>
> I'm trying to translate a small scheme library to common lisp and came
> across something I'm not sure how to translate.
>
> Given the definition,
>
> (define (filter pred?)
> .....)
>
> the scheme library defines what looks like an alias
>
> (define node-self filter)
>
>
> I tried
>
> (setf (symbol-function 'node-self)
> (function filter))
>
> and I think it does what I want but I get compile warnings that node-
> self isn't defined. Is there another way of doing this that avoids
> the warnings?
See Pascal's post. Working on XPath?
Cheers, RalfD
> Cheers,
> Andy
On Aug 3, 4:02 pm, Andy Chambers <··············@googlemail.com>
wrote:
> Hi,
>
> I'm trying to translate a small scheme library to common lisp and came
> across something I'm not sure how to translate.
>
> Given the definition,
>
> (define (filter pred?)
> .....)
>
> the scheme library defines what looks like an alias
>
> (define node-self filter)
>
> I tried
>
> (setf (symbol-function 'node-self)
> (function filter))
>
> and I think it does what I want but I get compile warnings that node-
> self isn't defined. Is there another way of doing this that avoids
> the warnings?
Short answer: see Pascal Constanza's reply.
Longer: As for why you're getting warnings, I'm guessing you're using
the file compiler. When it compiles the file, it creates code that,
when run, will perform the setf above. As the compiler encounters
calls to node-self, it's still working in an environment where node-
self has no global function binding. Aside from being syntactically
nice and causing the function to be defined by run-time, the other job
of defun is to cause the function to be known in the compiler's
environment.
Andy Chambers <··············@googlemail.com> writes:
> Hi,
>
> I'm trying to translate a small scheme library to common lisp and came
> across something I'm not sure how to translate.
>
> Given the definition,
>
> (define (filter pred?)
> .....)
>
> the scheme library defines what looks like an alias
>
> (define node-self filter)
>
>
> I tried
>
> (setf (symbol-function 'node-self)
> (function filter))
>
> and I think it does what I want but I get compile warnings that node-
> self isn't defined. Is there another way of doing this that avoids
> the warnings?
One easy way is to not convert anything, but either:
- use pseudo-scheme (unfortunately, it's R4RS, not R5RS),
- just define a couple of CL macros to be able to run your scheme code.
For example (not tested):
(defmacro define (&whole whole spec &body body)
(cond
((consp spec)
;; (define (f arg ...) body ...)
`(define ,(first spec) (lambda ,(rest spec) ,@body)))
((cdr body)
(error "Invalid syntax for define: ~S" whole))
((and (listp (car body)) (eq 'lambda (caar body)))
;; (define f (lambda ...))
`(progn
(defparameter ,spec ,@body)
(defun ,spec ,(cadar body) ,@(cddar body))))
(t
;; (define v ...)
`(defparameter ,spec ,@body))))
You could also shadow, or rename lambda, to replace it with a macro
that would use Pascal Costanza's with-lisp-1 or something similar.
http://groups.google.com/group/comp.lang.lisp/browse_thread/thread/9e084d2612926268/30d0df9c62515a7a?lnk=st&q=&rnum=1&hl=en#30d0df9c62515a7a
You may also use defalias (or actually a modified version binding also the value slots)
http://groups.google.com/group/comp.lang.lisp/browse_thread/thread/96495af41519af69/1f7a703503c96334?lnk=st&q=&rnum=1#1f7a703503c96334
to define scheme names like:
(define-scheme-alias set! setq)
...
If your scheme program doesn't use any sophisticated scheme feature,
you can go a long way with this kind of hack.
--
__Pascal Bourguignon__ http://www.informatimago.com/
"Indentation! -- I will show you how to indent when I indent your skull!"