Macro expansion error (help!) - migrating calendrical calculations

29 views
Skip to first unread message

Kip Cole

unread,
Apr 30, 2016, 6:10:12 PM4/30/16
to Lisp Flavoured Erlang
I'm working on migrating the calendrical calculations CL code to LFE (my first lfe project - and I haven't really worked in lisp since it ran on a Multics machine and the Maclisp book was version 1 ....).  Its useful for an elixir project I'm working on - and migrating the CL code seems a better idea than reimplementing in erlang/elixir.  All suggestions welcome.

Given the code below, the macro expansion of sigma is failing with:

       exception error: #(expand_macro
                          (sigma
                           ((x coefficients) (y addends) (z multipliers))
                           (* x (sin-degrees (+ ...))))
                          #(case_clause car))
         in lfe_eval:eval_apply_expr/3 (src/lfe_eval.erl, line 505)
         in lfe_eval:eval_expr/2 (src/lfe_eval.erl, line 113) 

 The code:

(defmodule cal
  (export all))

 ;; shim to aid migration - calendrical calculations is 6k lines of CL in all
(defun mapcar (fun list)  
 (cond
   ((== list ()) ())
   ('true (cons (funcall fun (car list)) (mapcar f (cdr list))))
 ))
  
;; Macro as defined in calendrical calculations 
(defmacro sigma (list body)
  ;; TYPE (list-of-pairs (list-of-reals->real))
  ;; TYPE -> real
  ;; $list$ is of the form ((i1 l1)..(in ln)).
  ;; Sum of $body$ for indices i1..in
  ;; running simultaneously thru lists l1..ln.
  `(apply '+ (mapcar (function (lambda
                                 ,(mapcar 'car list)
                                 ,body))
                     ,@(mapcar 'cadr list))))
  
;; Fails on macro expansion 
(sigma ((x coefficients)
        (y addends)
        (z multipliers))
       (* x (sin-degrees (+ y (* z c)))))

Robert Virding

unread,
May 1, 2016, 7:19:06 PM5/1/16
to Lisp Flavoured Erlang
I definitely see one thing I need to do and that is how errors in macro expansion are displayed.

Some comments which I think will explain what has gone wrong.

One important thing to realise is that CL packages and LFE/Erlang modules are very different things. For instance:

- Modules aren't namespaces, there is only one namespace which contains all names.
- LFE modules are the unit of code handling, they are compiled as one unit and no function is accessible until the module has been compiled and loaded into the system. Functions can't be added or removed from a module without recompiling it all.
- This means the separation between compile-time and run-time is very strict. So when macros are expanded they can't call functions in the module itself. They don't exist yet. This may be relaxed in the future but then the functions will be interpreted.
- Macros are specially treated and are interpreted at compile-time.
- You can functions with the same name but different number of arguments, arity, and they are different functions. Functions cannot have a variable number of arguments. So when you specify a function you must give it module, name, and arity, for example with #'foo:bar/3 .
- Symbols don't really have a function value in the sense that you can apply a symbol.
- No truthy values, LFE uses true/false as booleans.

These are all properties of the underlying Erlang system which we can't do anything about. It means that while LFE looks like CL in many ways it is actually a different lisp.

So in your code you can't call the local mapcar from your macro and trying to use '+ as a function just means you are using the symbol. There is a cl module with some of the standard CL library functions, for example mapcar.

I hope this helps you get going, but remember it is a different lisp. Try going to http://lfe.io/ for more help

Robert


Robert Virding

unread,
May 1, 2016, 7:34:47 PM5/1/16
to Lisp Flavoured Erlang
I hate referencing myself but at this years LambdaDays conference I gave a talk about LFE:

http://www.lambdadays.org/lambdadays2016/robert-virding

Eric Bailey

unread,
May 1, 2016, 8:18:54 PM5/1/16
to Lisp Flavoured Erlang
I'd be interested in taking a look. For some reason, I quite like porting CL, Clojure et al. to LFE. Is the code online somewhere, Kip?

Eric




On Sun, May 1, 2016 at 4:34 PM -0700, "Robert Virding" <rvir...@gmail.com> wrote:

I hate referencing myself but at this years LambdaDays conference I gave a talk about LFE:

http://www.lambdadays.org/lambdadays2016/robert-virding

--
You received this message because you are subscribed to the Google Groups "Lisp Flavoured Erlang" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lisp-flavoured-e...@googlegroups.com.
To post to this group, send email to lisp-flavo...@googlegroups.com.
Visit this group at https://groups.google.com/group/lisp-flavoured-erlang.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages