Arc Forumnew | comments | leaders | submitlogin
1 point by fallintothis 5898 days ago | link | parent

Not sure if this is exactly like the bug noted in the top comments of arc.arc, but it seems to me that perhaps it could be boiled down to the problem of inserting literal objects into macroexpansions:

  arc> (= pusher cons)
  #<primitive:cons>
  arc> (pusher 'a 'b)
  (a . b)
  arc> (mac my-push (e ls)
         (let pusher-alias pusher
           `(,pusher-alias ,e ,ls)))
  #3(tagged mac #<procedure>)
  arc> (let pusher (fn (x ls) (+ ls (list x)))
         (prn (my-push 'a '(b c d)))
         (prn (pusher 'd '(a b c))))
  Error: "Bad object in expression #<primitive:cons>"
  arc> (macex '(my-push 'a '(b c d)))
  (#<primitive:cons> (quote a) (quote (b c d)))
  arc> (mac my-push (e ls)
         (let pusher-alias pusher
           `(list ',pusher-alias ,e ,ls)))
  *** redefining my-push
  #3(tagged mac #<procedure>)
  arc> (let pusher (fn (x ls) (+ ls (list x)))
         (prn (my-push 'a '(b c d)))
         (prn (pusher 'd '(a b c))))
  (#<primitive:cons> a (b c d))
  (a b c d)
As you note, this solution then turns into combing through and aliasing every free variable -- doing so automatically would then be the makings for some hygiene, ostensibly. This is an area of contention, as I'm sure you're well aware. It still crops up fairly frequently, generally from someone vying for such a system. But as it stands, Arc doesn't seem to be going for hygienic macros (except perhaps as a library).


1 point by xrchz 5898 days ago | link

is it possible for a macro library to provide hygienic macros? I suppose you could port the "portable syntax case" implementation from scheme to arc... but I don't know how much help, if any, you'll need from the expander

-----

1 point by rincewind 5897 days ago | link

how do you know that a variable is free at macro expansion time?

-----

1 point by stefano 5897 days ago | link

You would need help from the compiler. Basically a macro would be called with a list of bound variables as an extra argument. For a compiler, this would be quite easy. Now a macro would look like:

  (mac my-mac (non-free-var-list my-arg1 ...)
    ...)
instead of:

  (mac my-mac (my-arg1 ...)
    ...)

-----

2 points by almkglor 5897 days ago | link

Why not just mod 'mac to something like (docstring extraction and name redefinition elided):

  (mac mac (name args . body)
    `(set ,name
       (annotate 'mac
         (fn ,(cons 'expansion-context args)
           ,@body))))
Then insert stuff in Scheme-side 'ac to use (apply macro the-free-list-or-whatever (cdr ex)) instead of (apply macro (cdr ex))

That way macros that need it can get it, while macros which don't need it can ignore it.

We can potentially define 'expansion-context as a table or function, and potentially we could allow an implementation to have expansion-context!line, say provide line number etc. information so that the macro can give decent error messages. Of course if we have a dorky backend (like mzscheme) then we can just have it return expansion-context!line as nil, because it can't somehow extract line numbers, but for implementations that can, then good.

edit: Oh and ye: the 'ac compiler on scheme-side really does keep a list of bound variables ^^

-----

1 point by rincewind 5897 days ago | link

This is a solution, but I consider this cheating, because it is nearly as tedious as manually inserting symeval around global functions.

w/var:do in my mexpr.arc is an example of a macro/dsl which can not take a free-var list as 1st arg (it could be changed, but that would make it useless)

and what about list in

  (let list (list 1 2 3) list)

-----