Michael Bevilacqua-Linn on 3 Feb 2011 07:32:05 -0800


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: let substitution in clojure?


Hey Dan,

It's not about the let bindings, it's about the macro.  Since one of the things
defmacro does is turn off evaluation of its arguments, the let bindings
in the code that sub generates do not have a vector where the binding forms should be
unless you pass in a literal vector. 

macroexpand-1 is your friend here:

match.core=> (macroexpand-1 '(sub (domatch '(x y z) '(1 2 3)) (+ x y z)))
(clojure.core/let (domatch (quote (x y z)) (quote (1 2 3))) (+ x y z))

So you can see that what's being passed to let in the generated code is actually
a list of generated code, not what you want at all...

One easy way to get around that is to just eval what you need evaled yourself:

(defmacro sub-2 [binds expr]
  (let [evaluated-bindings (eval binds)]
    `(let ~evaluated-bindings ~expr)))

match.core=> (macroexpand-1 '(sub-2 (domatch '(x y z) '(1 2 3)) (+ x y z)))
(clojure.core/let [x 1 y 2 z 3] (+ x y z))

match.core=> (sub-2 (domatch '(x y z) '(1 2 3)) (+ x y z))
6

I don't know if that's the right way to do it, but perhaps someone that's
more macro wizardy than I can chime in...

Also, it's idiomatic in clojure to use vectors instead of quoted lists
in most places.  Less typing I guess :-).

Thanks,
MBL

On Thu, Feb 3, 2011 at 6:02 AM, Dan Mead <d.w.mead@gmail.com> wrote:
Hi guys, I'm having trouble figuring out the proper way to do some
macro stuff for my project.

I've got a pattern matcher that works as you'd expect. I can take
patterns which are quoted s-expressions and match them to actual
values. This returns a set of bindings, which I want to use to rewrite
a corresponding _expression_.

it's working with a regular dumb function that crawls down a quoted
_expression_ and does replacement with the bindings list. This is pretty
bad as it runs in theta(n) time, and is really brittle when it comes
to scoping issues.


So, I'm trying to get a macro that will splice the bindings list as a
vector and the target _expression_ into a let statement. That way
scoping should be less of an issue and it should run in constant time
(or whatever let run in etc etc)


so, to match some pattern to a set of values we can do

user=> (match '(x y z) '(1 2 3))
((x 1) (y 2) (z 3))

If that call to the matcher is wrapped in some stuff to convert to a
vector, we can do

user=> (domatch '(x y z) '(1 2 3))
[x 1 y 2 z 3]

and clojure agrees that it's a vector

user=> (vector? (domatch '(x y z) '(1 2 3)))
true

if i have a macro like this

(defmacro sub [binds expr]
 `(let ~binds ~expr))

which works fine, if you give it a literal vector

user=> (sub [x 1 y 1] (+ x y))
2

then why doesn't it work with my call to the matcher?

user=> (sub (domatch '(x y z) '(1 2 3)) (+ x y z))
java.lang.IllegalArgumentException: let requires a vector for its
binding (NO_SOURCE_FILE:0)


I'm afraid I've lost the plot here. Any insight you guys could give
would be helpful

Here is my newbie and entirely too verbose code.

http://taz.cs.wcupa.edu/~dmead/patternmatch.clj

Dan

sorry if you get this post twice