Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"How to apply Red to compile-time optimization of this Lisp code?"

This is a pattern of optimization in Lisp code that I want to achieve in Red:

(defmacro compute-at-compile (x)
  `(+ ,(* pi 2) ,x))

(macroexpand '(compute-at-compile 1))
; => (+ 6.283185307179586 1)

How do I express this in Red? (I realize it may not be possible in today's implementation, I'm wondering about how one would express the code at the language level to get such an optimization. Would it require special markup in the source, or would it be automatic like Lisp?)

like image 337
Jacob Good1 Avatar asked Mar 03 '14 18:03

Jacob Good1


1 Answers

Trying to extend my answer to maybe cover another idea that may help you find what you are looking for.

Red/System

From my understanding, the Red/System #define directive can help with optimization (in reducing function calls). Here is a similar example in Red/System. Within Red, it would require using within #system or #system-globaldirective.

#define COMPUTE(x) (3.13159 * 2.0 + x)

b: COMPUTE(1.0)
print b

Processing the macro should result in:

b: (3.13159 * 2.0 + 1.0)
print b

and results

7.26318

Math between types isn't defined yet, so you'll run into issues multiplying/adding float! and integer! (hence the above use of float!)

Red/Rebol

You can also take a look at compose as a higher level way to optimize your code writing. I am unsure of the effect in terms of optimizing speed. What compose does is take a block and evaluate whatever is in parenthesis and not evaluate other elements in the block.

See the Rebol2 help definition for compose

>> help compose
USAGE:
    COMPOSE value /deep /only    

DESCRIPTION:
     Evaluates a block of expressions, only evaluating parens, and returns a block.
     COMPOSE is a native value.    

ARGUMENTS:
     value -- Block to compose (Type: any)    

REFINEMENTS:
     /deep -- Compose nested blocks
     /only -- Inserts a block value as a block

This may be what you're looking for in terms of building expressions

red>> x: 1
== 1
red>> compose [3 + 2 + (x)]
== [3 + 2 + 1]

An example from the Rebol2 documentation:

>> probe compose [time: (now/time) date: (now/date)]
[time: 12:48:53 date: 5-Mar-2014]
== [time: 12:48:53 date: 5-Mar-2014]
like image 54
kealist Avatar answered Sep 28 '22 19:09

kealist