# Typed Racket + Org-mode + Org tables and lists

**URL:** <https://racket.discourse.group/t/typed-racket-org-mode-org-tables-and-lists/2254>\
**Category:** Questions & Answers\
**Tags:** typed-racket\
**Created:** [August 28, 2023, 8:59am UTC](https://racket.discourse.group/t/typed-racket-org-mode-org-tables-and-lists/2254 "2023-08-28T08:59:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![scolobb](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/scolobb/32/108_2.png) [@scolobb](https://racket.discourse.group/u/scolobb)\
**Post date:** [August 28, 2023, 8:59am UTC](https://racket.discourse.group/t/typed-racket-org-mode-org-tables-and-lists/2254/1 "2023-08-28T08:59:59Z")

</div>

Hi,

I would like to use Typed Racket in Org-mode code blocks with [ob-racket-mode](https://github.com/hasu/emacs-ob-racket) and produce Org lists and tables. (Untyped) Racket code works out of the box:

```scheme
#+BEGIN_SRC racket :results table drawer
'((a 1) (b 2))
#+END_SRC

```

produces this result

```scheme
#+RESULTS:
:results:
| a | 1 |
| b | 2 |
:end:

```

Now I switch to Typed Racket in the following way:

```scheme
#+BEGIN_SRC racket :results table drawer :lang typed/racket
'((a 1) (b 2))
#+END_SRC

```

and this breaks with the following error message:

```plaintext
/tmp/babel-9vcsBj/org-babel-p3WZz0.rkt:4:0: Type Checker: missing type for identifier;
consider using `require/typed' to import it
identifier: datum->table
from module: (file /home/scolobb/.emacs.d/elisp/emacs-ob-racket/ob-racket-runtime.rkt)
in: (ob-racket-begin-print-table (quote ((a 1) (b 2))))

```

It turns out indeed that the Racket part of `ob-racket` is written in `#lang racket/base`, and then something happens which seems to try to run that code in a Typed Racket context. I am currently trying to figure out what happens exactly.

I also tried to introduce a typed submodule in the following way:

```scheme
#+BEGIN_SRC racket :results table drawer
(module a typed/racket
  '((a 1) (b 2)))
#+END_SRC

```

but this fails with the following message:

```plaintext
/tmp/babel-9vcsBj/org-babel-whnzhW.rkt:6:0: module: allowed only at the top level or in a module top-level
in: (module a typed/racket (quote ((a 1) (b 2))))
location...:
/tmp/babel-9vcsBj/org-babel-whnzhW.rkt:6:0

```

At this point, I would gladly take any pointers, hints, or ideas as to how to best proceed. I have been using Org Babel for ages, but I have never really opened the hood before now.

---

<div class="post-metadata">

**Author:** ![greghendershott](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/greghendershott/32/98_2.png) [@greghendershott](https://racket.discourse.group/u/greghendershott)\
**Post date:** [August 28, 2023, 1:37pm UTC](https://racket.discourse.group/t/typed-racket-org-mode-org-tables-and-lists/2254/2 "2023-08-28T13:37:23Z")

</div>

Have you seen [installed languages like remora/dynamic on the #+begin\_src line ? · Issue #4 · hasu/emacs-ob-racket · GitHub](https://github.com/hasu/emacs-ob-racket/issues/4) -- it seems in the same ballpark? If not, maybe you could file a fresh issue.

* * *

Caveats: I rarely use org-babel. I only have time to take a quick look through the ob-racket-mode source, which is unrelated to and doesn't use racket-mode.

Given the "template" system in the Elisp code, it's hard for me to grok what actual Racket program is being run. But I _think_ your `module` work-around (good idea!) errors because your program text (among other things from the template?) ends up as `body` in [`ob-racket-begin-print-elisp`](https://github.com/hasu/emacs-ob-racket/blob/master/ob-racket-runtime.rkt#L17-L22) or [`ob-racket-begin-print-table`](https://github.com/hasu/emacs-ob-racket/blob/master/ob-racket-runtime.rkt#L17-L22):

```scheme
(define-syntax-rule
  (ob-racket-begin-print-table body ...)
  (display
   (datum->table
    (let ()
      body ...)))))

```

Ergo the error about `module`.

* * *

To run a wider range of Racket programs, maybe all this could work differently, more akin to Scribble sandbox evaluators? But that might have its own set of disadvantages. Also org-babel might not let you share one evaluator across multiple snippets as does Scribble. idk.

---

<div class="post-metadata">

**Author:** ![scolobb](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/scolobb/32/108_2.png) [@scolobb](https://racket.discourse.group/u/scolobb)\
**Post date:** [September 7, 2023, 5:19pm UTC](https://racket.discourse.group/t/typed-racket-org-mode-org-tables-and-lists/2254/3 "2023-09-07T17:19:18Z")

</div>

Thank you very much for the pointers @greghendershott ! I actually started reading everything attentively, including the source code for `ob-racket`, which is why I still have no meaningful reaction.

Now, from reading the sources I have the impression that Tero Hasu the developer of `ob-racket` did think rather seriously about other `#lang`s, so I expect to be able to find an incantation mixing header arguments and "templates" which would allow me to achieve the desired behavior.

I will definitely report back here as soon as I have anything!

---

<div class="post-metadata">

**Author:** ![scolobb](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/scolobb/32/108_2.png) [@scolobb](https://racket.discourse.group/u/scolobb)\
**Post date:** [April 25, 2024, 9:49pm UTC](https://racket.discourse.group/t/typed-racket-org-mode-org-tables-and-lists/2254/4 "2024-04-25T21:49:26Z")

</div>

After _some_ time ( 😃 ) , I am coming back with this pull request: [[WIP] Typed racket by scolobb · Pull Request #10 · hasu/emacs-ob-racket · GitHub](https://github.com/hasu/emacs-ob-racket/pull/10) , in which I suggest adding a Typed Racket runtime for Ob-racket, and which can be used by running the following Elisp code in the Org buffer:

```scheme
(setq-local ob-racket-locate-runtime-library-function
	    (lambda ()
	      "/absolute/path/to/ob-racket-runtime-typed.rkt"))

```

As I explain in more details in the top comment on the PR, I am not entirely happy with this solution, so feel free to go over to GitHub and leave comments and suggestions, or just chime in here!

---

<div class="post-metadata">

**Author:** ![scolobb](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/scolobb/32/108_2.png) [@scolobb](https://racket.discourse.group/u/scolobb)\
**Post date:** [May 29, 2024, 10:34am UTC](https://racket.discourse.group/t/typed-racket-org-mode-org-tables-and-lists/2254/5 "2024-05-29T10:34:23Z")

</div>

Further good news for `ob-racket` and Typed Racket: it works transparently now!

Based on [[WIP] Typed racket by scolobb · Pull Request #10 · hasu/emacs-ob-racket · GitHub](https://github.com/hasu/emacs-ob-racket/pull/10) , Tero Hasu implemented a transparent way to switch Racket runtimes responsible for converting Racket data structure to Elisp/Org-mode compatible ones, and this change is now in master: [add basic support for multiple runtime libraries · hasu/emacs-ob-racket@948f05c · GitHub](https://github.com/hasu/emacs-ob-racket/commit/948f05c4f2326e6c48ffe46aa244e4a735e52619) !

More concretely:

```scheme
#+BEGIN_SRC racket :results table drawer :lang typed/racket
'((a 1) (b 2))
#+END_SRC
#+RESULTS:
:results:
| a | 1 |
| b | 2 |
:end:

```

You can also specify the `#lang` directly in the code block:

```scheme
#+BEGIN_SRC racket :results table drawer
#lang typed/racket
'((a 1) (b 2))
#+END_SRC
#+RESULTS:
:results:
| a | 1 |
| b | 2 |
:end:

```

Enjoy!
