macro for looping over functions in a list

Discussion of Common Lisp
imba
Posts: 35
Joined: Sun Nov 21, 2010 2:13 pm

macro for looping over functions in a list

Post by imba » Wed Dec 15, 2010 12:39 pm

Given

Code: Select all

(defconstant functions
  '(1+
    1-))
,

I want to define a macro that expands into

Code: Select all

(+
(1+ 1)
(1- 1))
How do I manage this?

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: macro for looping over functions in a list

Post by ramarren » Wed Dec 15, 2010 1:15 pm

Looping over a list of functions does not require a macro. It is unclear to me what you are trying to achieve.

imba
Posts: 35
Joined: Sun Nov 21, 2010 2:13 pm

Re: macro for looping over functions in a list

Post by imba » Wed Dec 15, 2010 1:20 pm

I want the functions to be called to be inlined.

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: macro for looping over functions in a list

Post by ramarren » Wed Dec 15, 2010 1:32 pm

That looks like a probably unnecessary micro-optimization, but if you want to use a constant at compile time you have to wrap it in EVAL-WHEN to guarantee it is available at compile time, and then just use it.

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: macro for looping over functions in a list

Post by nuntius » Wed Dec 15, 2010 1:40 pm

It appears that standard functions like MAP and REDUCE may solve your problem. A good implementation (for optimization) should have a compiler macro that identifies when these functions are passed constant parameters and can then inline the function calls for you.

What do the benchmarks look like without this inlined?

gugamilare
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil
Contact:

Re: macro for looping over functions in a list

Post by gugamilare » Thu Dec 16, 2010 5:17 am

imba wrote:I want the functions to be called to be inlined.
You don't need to worry about inlining simple standard functions. The functions 1+ and 1- are already inlined in most (all?) implementations.

If you created a function and want it to be inlined, use declaim

Code: Select all

(defun my-function (...)
  ...)

(declaim (inline myfunction))

imba
Posts: 35
Joined: Sun Nov 21, 2010 2:13 pm

Re: macro for looping over functions in a list

Post by imba » Thu Dec 23, 2010 4:53 am

In the following code,

Code: Select all

(defconstant functions '(1+ 1-)) 
(defun foo ()
  (loop for fun in functions 
           summing (funcall fun 4)))
1+ and 1- aren't inlined in foo. How can I achieve this?

gugamilare
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil
Contact:

Re: macro for looping over functions in a list

Post by gugamilare » Thu Dec 23, 2010 8:58 am

Only explicit calls to a function can be inlined. If you are holding functions in a variable, how can the compiler know which function that variable will be holding so that it can inline it? Unless the compiler makes really extraordinary analysis.

You can, however, optimize your code a little bit. The functions 1+ and 1- are supposed to work on integers, floats, rationals, complexes, etc. You can create your own version:

Code: Select all

(defun fixnum-1+ (x)
  (1+ (the fixnum x)))

(defun fixnum-1- (x)
  (1- (the fixnum x)))

(defconstant +functions+ '(fixnum-1+ fixnum-1-)) 
(defun foo ()
  (loop for fun in +functions+ 
           summing (funcall fun 4)))
Note that it is convention to use '+' around the name of constants, like +this+.

imba
Posts: 35
Joined: Sun Nov 21, 2010 2:13 pm

Re: macro for looping over functions in a list

Post by imba » Thu Dec 23, 2010 10:06 am

gugamilare wrote:Only explicit calls to a function can be inlined. If you are holding functions in a variable, how can the compiler know which function that variable will be holding so that it can inline it?
Well, because +functions+ is a constant? Is there no possibility to write a macro that translates this code to

Code: Select all

(+ (funcall 1+ 4)
    (funcall 1- 4))
?

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: macro for looping over functions in a list

Post by ramarren » Thu Dec 23, 2010 11:08 am

Except the EVAL-WHEN issue I mentioned earlier this is a trivial list manipulation problem. If you don't know how to do that, you probably shouldn't be worrying about it, since, as it has been said, this is a microoptimization which is unlikely to achieve anything significant in most cases.

Post Reply