Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between calling function and macro inside macro?

Tags:

macros

clojure

My puzzle is the following example:

(defmacro macro1 [x]
  (println x))

(defn func1 [x]
  (println x))

(defmacro macro2 [x]
  `(macro1 ~x)
  (func1 x))

(defmacro macro3 [x]
  (func1 x)
  `(macro1 ~x))

(println "macro2")
(macro2 hello)

(println "macro3")
(macro3 hello)

Surprisingly, the output is:

macro2
hello
macro3
hello
hello

Why the output of macro2 and macro3 are different? In my understanding, all the calling of macro inside macro could be substituted with function (except for the reason of reuse). Anything wrong in my understanding?


Thanks Michael for clarifying. My general question is how to choose between using function or macro inside macro for the purpose of manipulating the s-expression. I wonder whether they can be used exchangeably except that they're evaled at different phases. Another example:

(defn manipulate-func [x]
  (list + x 1))

(defmacro manipulate-macro [x]
  (list + x 1))

(defmacro macro1 [x y]
  [(manipulate-func x) `(manipulate-macro ~y)])

(println (clojure.walk/macroexpand-all '(macro1 (+ 1 2) (+ 3 4))))
;; [(#<core$_PLUS_ clojure.core$_PLUS_@332b9f79> (+ 1 2) 1) (#<core$_PLUS_ clojure.core$_PLUS_@332b9f79> (+ 3 4) 1)]
like image 745
M. Tong Avatar asked Jan 01 '14 03:01

M. Tong


3 Answers

macro2 doesn't call macro1. Look at its body:

`(macro1 ~x)
(func1 x)

The first line is syntax-quoted; its value is list structure of the form (user/macro1 x-value) (assuming macro1 is defined in the user namespace; x-value here is the literal argument provided to macro2) and it has no side effects. Because there are no side effects and the value is discarded, this line has no effect.


Responding to the edit:

Firstly, it is important to distinguish calling another macro inside a macros body from emitting a call to another macro:

(defmacro some-macro []
  ...)

;; calls some-macro:
(defmacro example-1 []
  (some-macro))

;; emits a call to some-macro:
(defmacro example-2 []
  `(some-macro))

Secondly, in the case of calling functions and macros inside a macro's body, one must keep in mind what the relevant notions of runtime and compile time are:

  • functions called by a macro will be called at the macro expander's runtime, which is compile time from the point of view of user code;

  • macros called by a macro will be expanded when the macro body is compiled.

If a macro emits a call to another macro, the notions of runtime and compile time relevant to the emitted macro call will be the same as those relevant to the original macro call. If a macro calls another macro, they are shifted one step back, as it were.

To illustrate, let's consider a macro that delegates all its work to a helper function:

(defn emit-abc [abc-name [a b c]]
  `(def ~abc-name {:a ~a :b ~b :c ~c}))

(defmacro defabc [abc-name abc-vals]
  (emit-abc abc-name abc-vals))

From the REPL:

user> (defabc foo [1 2 3])
#'user/foo
user> foo
{:a 1, :c 3, :b 2}

If emit-abc were itself a macro, the above definition of defabc wouldn't even compile, because emit-abc would attempt to destructure the literal symbol abc-vals, throwing an UnsupportedOperationException.

Here's another example that makes it easier to explain what's happening:

(let [[a b c] [1 2 3]]
  (defabc foo [a b c]))

defabc receives the vector of the three literal symbols a, b and c as the second argument; it has no access to the runtime values 1, 2 and 3. It passes this exact vector of symbols to the function emit-abc, which is then able to reach into this vector and extract the symbols to produce the map {:a a :b b :c c}. This map becomes the expansion of the defabc call. At runtime a, b and c turn out to be bound to the values 1, 2 and three, and so the map {:a 1 :b 2 :c 3} is produced.

Suppose we tried to write emit-abc as a macro with the same body (just changing defn to defmacro in its definition). Then we couldn't usefully call it from defabc, because we wouldn't have any way of conveying to it the actual values of the arguments to defabc. We could write

(emit-abc abc-name [(abc-vals 0) (abc-vals 1) (abc-vals 2)])

to make defabc compile, but this would end up emitting abc-name as the name of the Var being defined and include code for the vector literal [a b c] three times in the generated code. We could however emit a call to it:

`(emit-abc ~abc-name ~abc-vals)

This works as expected.

like image 67
Michał Marczyk Avatar answered Oct 30 '22 11:10

Michał Marczyk


I think you're confused about the difference between macros and functions.

  • Macros are evaluated at compile time, and they take code as input and give code as output.
  • Functions evaluate their results at run time, taking run-time values as input and returning run-time values as output.

The result of a macro should pretty much always be an s-expression representing the source code resulting form applying the macro. This is why macros usually use the syntax quote functionality, since it makes it easy to generate source code with inserted parameterized values via the ~ and ~@ escapes.

Defining a couple of functions might help you see how this works. Let's run the following code:

(defn testing-macro-2 [my-arg]
  (macro2 my-arg))

(testing-macro-2 "macro 2 test")

(defn testing-macro-3 [my-arg]
  (macro3 my-arg))

(testing-macro-3 "macro 3 test")

Here's what I get in my REPL:

user=>
(defn testing-macro-2 [my-arg]
  (macro2 my-arg))
my-arg
#'user/testing-macro-2
user=> (testing-macro-2 "macro 2 test")
nil
user=>
(defn testing-macro-3 [my-arg]
  (macro3 my-arg))
my-arg
my-arg
#'user/testing-macro-3
user=> (testing-macro-3 "macro 3 test")
nil

As you can see, my-arg is printed when defining the functions where the macros are invoked, not when I call the functions. This is because the macros are evaluated when the Clojure compiler is generating code for the function, so that's when the call to println happens.

However, if you use the syntax-quote in macro1 to make it return code instead of calling println, which returns nil, then it all changes:

user=>
    (defmacro macro1 [x]
      `(println ~x))
#'user/macro1
user=>
    (defn func1 [x]
      (println x))
#'user/func1
user=>
    (defmacro macro2 [x]
      `(macro1 ~x)
      (func1 x))
#'user/macro2
user=>
    (defmacro macro3 [x]
      (func1 x)
      `(macro1 ~x))
#'user/macro3
user=>
    (defn testing-macro-2 [my-arg]
      (macro2 my-arg))
my-arg
#'user/testing-macro-2
user=> (testing-macro-2 "macro 2 test")
nil
    (defn testing-macro-3 [my-arg]
      (macro3 my-arg))
my-arg
#'user/testing-macro-3
user=> (testing-macro-3 "macro 3 test")
macro 3 test
nil
user=> (macro2 hello)
hello
nil
user=> (macro3 hello)
hello
CompilerException java.lang.RuntimeException: Unable to resolve symbol: hello in this context, compiling:(NO_SOURCE_PATH:107)

Each of the macros still prints the argument due to a println being called when the macro is evaluated, but since macro3 now actually returns source code it actually works like println.

Note that testing-macro-2 prints nothing because macro2 throws away the result of the intermediate calculation `(macro1 ~x) and simply returns nil (the result of println). In other words, using (macro2 foo) is the same as just putting a nil literal in your code, except that the compiler will print foo as a side-effect when it evaluates the macro.

Invoking (macro3 hello) results in a CompilerException because the macro substitution results in the code (println hello), but hello is not defined. If you do something like (def hello "Hello there!") then you won't get an error since it will find a binding for hello.

like image 38
DaoWen Avatar answered Oct 30 '22 13:10

DaoWen


I'm not satisfied with the answers so far, so let me take a stab...

The issue is that defmacro returns data that is then used as code. Only the last expression in defmacro is returned and, in your example, is then evaluated as code.

So... in your call to macro2 the following steps occur

  1. `(macro1 ~x) is syntax quoted so it evaluates to (macro1 hello) and is not evaluated further because of the syntax quote. This line effectively does nothing as a result.
  2. (func1 x) executes inside the macro, prints the string, and the result nil is returned.
  3. The result of calling (macro2 hello) is nil so nothing further occurs.

Later, you call macro3 and the following steps occur

  1. (func1 x) executes, prints hello, returns nil. But since the macro is not finished this nil does nothing and the next expression is evaluated.
  2. `(macro1 ~x) evaluates (just as before) to (macro1 hello) and is returned as code (it is not evaluated further, yet, because it is syntax quoted). This is the return value of calling macro3 since it is the last expression in the implicit do of defmacro.
  3. The return value of calling macro3 from the previous step is now evaluated. It evaluates to (println hello), but this is not evaluated yet.
  4. Finally the previous steps result is evaluated and a second hello is printed.

I believe the point to understand is that a macro returns code to be executed and only the last expression is returned. Perhaps also, remember that the syntax quote ` prevents the expression following it from being evaluated and instead creates a list in place that is not evaluated further (unless some other action is taken to evaluate the expression).

Yes there is a runtime/compile time difference between when code is evaluated, but for this question I don't think that is the key thing to notice. The key thing to notice is that with macro2 the result of calling macro1 is not returned, but in macro3 it is returned and is therefore evaluated further.

like image 3
Jason Avatar answered Oct 30 '22 13:10

Jason