Why are there seemingly unnecessary if expressions shown by the Macro stepper?

36 views
Skip to first unread message

schle...@gmail.com

unread,
Mar 21, 2021, 2:28:10 PM3/21/21
to Racket Users
The [WordCount] thread caused me to play around with the solutions to see whether I could see anything else that can be tweaked, I tried a few things but they didn't improve performance, but it was fun anyway.

Then I though I could try the macro stepper and look at the expansion of [crowdsourced].
I disabled macro hiding and stepped to the end.
There I saw if statements with a condition that is `(quote #t)` with a little lock symbol before it.

I would like to understand why those terms aren't reduced further.
Are they placeholders and are replaced and resolved at a later time?

terms copied (without the lock symbol) from the macro stepper
(if (if '#t (#%app not '#f) '#f)
  (#%app for-loop (#%app unsafe-fx+ '1 pos))
  (#%app values))

From a naive standpoint I would expect it to be simplified:
(if '#t (#%app not '#f) '#f) -> (#%app not '#f)

(if (#%app not '#f)
  (#%app for-loop (#%app unsafe-fx+ '1 pos))
  (#%app values))

So expressed another way: why isn't the if with boolean literal as condition reduced to the corresponding branch? Or does this happen, just isn't shown in the macro stepper?
What am I misunderstanding here?

If there aren't simplifications like that, what is preventing them or making them not worthwhile?


Sorawee Porncharoenwase

unread,
Mar 21, 2021, 2:47:03 PM3/21/21
to schle...@gmail.com, Racket Users

That’s not the job of the macro expander. And the Racket optimizer does reduce (if '#t (#%app not '#f) '#f) — in fact to #t.

If you use Racket CS, try running:

PLT_LINKLET_SHOW_CP0=1 racket file.rkt

where file.rkt is

#lang racket
(displayln (if #t (not #f) #f))

Here’s the optimized program:

;; cp0 ---------------------
(lambda (instance-variable-reference .get-syntax-literal!1
         .set-transformer!2 displayln3 print-values4)
  (call-with-module-prompt
    (lambda ()
      (#%call-with-values
        (lambda () (displayln3 #t))
        print-values4)))
  (#2%void))

--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/fce863fe-85d7-44a6-a2ba-5778df40e6bfn%40googlegroups.com.

schle...@gmail.com

unread,
Mar 21, 2021, 3:05:05 PM3/21/21
to Racket Users
Ahh ok, thank you that makes sense.
I didn't know about PLT_LINKLET_SHOW_CP0 before that is a nice look behind the scene.
Reply all
Reply to author
Forward
0 new messages