# Visualising programs and program transformations

**URL:** <https://racket.discourse.group/t/visualising-programs-and-program-transformations/3361>\
**Category:** Questions & Answers\
**Created:** [November 27, 2024, 10:28am UTC](https://racket.discourse.group/t/visualising-programs-and-program-transformations/3361 "2024-11-27T10:28:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aanastasiou](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/aanastasiou/32/1126_2.png) [@aanastasiou](https://racket.discourse.group/u/aanastasiou)\
**Post date:** [November 27, 2024, 10:28am UTC](https://racket.discourse.group/t/visualising-programs-and-program-transformations/3361/1 "2024-11-27T10:28:34Z")

</div>

Hello

Are there any existing tools that can take racket code and visualise it as the tree it is? I would like to visualise program transformations effected by macros.

So, for example, given:

```scheme
(define a (+ 5 6))

```

The graph could have a node "a" with an incoming edge from a node "+" with two incoming edges from literals "5" and "6".

I (think I) could write something like this using the [graph module](https://docs.racket-lang.org/graph/index.html), converting a program to a list and then parsing the list recursively , but I am wondering if something like this already exists (?)

---

<div class="post-metadata">

**Author:** ![damien\_mattei](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/damien_mattei/32/2706_2.png) [@damien\_mattei](https://racket.discourse.group/u/damien_mattei)\
**Post date:** [November 27, 2024, 10:40am UTC](https://racket.discourse.group/t/visualising-programs-and-program-transformations/3361/2 "2024-11-27T10:40:24Z")

</div>

hello,

i do not know in Racket, but this make me think of a project in Kawa to display graphically Scheme code:

> **[GitHub - panicz/grasp: The GRAphical Scheme Programming Environment](https://github.com/panicz/grasp)**
>
> The GRAphical Scheme Programming Environment

after Macro expansion is very different than procedures evaluation, and i'm not sure the project above, that i have not tested, just see a video demo, is doing the same with macro too.

Indeed it is really a wonderful project,see video:

[![](https://global.discourse-cdn.com/free1/uploads/racket/original/2X/a/aa9db03a80c59f8a052c10e7a7ff4c2608f824f4.jpeg "Visual Evaluation in GRASP") ](https://www.youtube.com/watch?v=wN8Fy5xTXeQ)

---

<div class="post-metadata">

**Author:** ![aanastasiou](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/aanastasiou/32/1126_2.png) [@aanastasiou](https://racket.discourse.group/u/aanastasiou)\
**Post date:** [November 27, 2024, 11:25am UTC](https://racket.discourse.group/t/visualising-programs-and-program-transformations/3361/3 "2024-11-27T11:25:55Z")

</div>

This is not what I am after but it looks interesting overall, thank you.

---

<div class="post-metadata">

**Author:** ![benknoble](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/benknoble/32/16_2.png) [@benknoble](https://racket.discourse.group/u/benknoble)\
**Post date:** [November 27, 2024, 1:32pm UTC](https://racket.discourse.group/t/visualising-programs-and-program-transformations/3361/4 "2024-11-27T13:32:16Z")

</div>

For macros, try the macro stepper in DrRacket. There’s a text utility, too, but I haven’t looked at what it takes to wrap it in a nice interface for use outside a REPL.

For drawing trees, try [tree-layout](https://docs.racket-lang.org/pict/Tree_Layout.html) with custom nodes? I thought there was already work on this for S-expressions but I didn’t find it quickly.

---

<div class="post-metadata">

**Author:** ![gus-massa](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/gus-massa/32/507_2.png) [@gus-massa](https://racket.discourse.group/u/gus-massa)\
**Post date:** [November 27, 2024, 6:27pm UTC](https://racket.discourse.group/t/visualising-programs-and-program-transformations/3361/5 "2024-11-27T18:27:59Z")

</div>

I agree. Moreover, the [Macro Debugger: Inspecting Macro Expansion](https://docs.racket-lang.org/macro-debugger/index.html) exports a lot of the functionality. So it's possible to reuse part of it to get a more visual reprresentation (but I never tried myself). Also, the `syntax`s have location information that may be useful to display the code as similar as the original as possible, but after a few macro expansions the mix of original parts and macro introduced parts may cause a lot of problems.

---

<div class="post-metadata">

**Author:** ![aanastasiou](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/aanastasiou/32/1126_2.png) [@aanastasiou](https://racket.discourse.group/u/aanastasiou)\
**Post date:** [November 28, 2024, 10:02am UTC](https://racket.discourse.group/t/visualising-programs-and-program-transformations/3361/6 "2024-11-28T10:02:27Z")

</div>

tree-layout is fantastic, will keep it in mind for future work, thank you. At the moment, I am happy with the round-trip to graphviz 🙂

_Yep, I guess the macro-stepper is another way of doing this but I was wondering if there was something that could be done programmatically so that you can show various levels of transformations on arbitrary source code input._

The Macro Debugger: Inspecting Macro Expansion suggested by gus-masa is what I was talking about.

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [November 28, 2024, 11:15am UTC](https://racket.discourse.group/t/visualising-programs-and-program-transformations/3361/7 "2024-11-28T11:15:18Z")

</div>

@aanastasiou

Is it box-and-pointer diagrams you are interested in?

One tool:

> <https://github.com/soegaard/metapict/blob/master/metapict/box-and-pointer-diagram.rkt>

A nice one by Jack Rosenthal:  
[https://docs.racket-lang.org/sdraw/](https://docs.racket-lang.org/sdraw/)

There might be a couple more on the package server.

---

<div class="post-metadata">

**Author:** ![aanastasiou](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/aanastasiou/32/1126_2.png) [@aanastasiou](https://racket.discourse.group/u/aanastasiou)\
**Post date:** [November 29, 2024, 12:06am UTC](https://racket.discourse.group/t/visualising-programs-and-program-transformations/3361/8 "2024-11-29T00:06:17Z")

</div>

Oh this is going to come in so handy! Thank you very much 🙂
