I wish I had an Emacs's org-babel ob-purescript.

Spago? pscid?
If there was such org-babel connected to the REPL session.

Thanks :slight_smile:

2 Likes

Hello, have you found a way?

I’m trying to do something like:

#+begin_src purescript
:type 1
-- Int
:type map
-- forall (f :: Type -> Type) (a :: Type) (b :: Type). Functor f => (a -> b) -> f a -> f b
#+end_src

I found a hacky workaround:

I added this to my ~/.emacs.d/init.el (by following this guide):

(defun org-babel-execute:purescript (body params)
  "Execute a block of Purescript code with org-babel."
  (message "executing Purescript source code block")
  (org-babel-eval ". $HOME/.asdf/asdf.sh && . $HOME/.asdf/completions/asdf.bash && spago repl" body))

What complicated it for me was the fact that I use asdf to manage my nodejs versions. So I had to load those first before executing spago repl.

Then in my purescript.org file:

#+begin_src purescript
1
#+end_src

#+RESULTS:
: PSCi, version 0.14.0
: Type :? for help
: 
: import Prelude
: 
: > 1
: 
: > See ya!
: ()

I am now able to execute purescript code.

However, the evaluation takes a couple of seconds, so I tried using ob-async:

#+begin_src purescript :async
1
#+end_src

#+RESULTS:
: 81c166afbe8b3f1064df420da068f23f

But I kept getting the following error:

error in process sentinel: async-handle-result: Symbol’s function definition is void: org-babel-execute:purescript
error in process sentinel: Symbol’s function definition is void: org-babel-execute:purescript

Not really sure how to move forward from here, but at least I got it working.

EDIT:

I also noted that for multi line code to work, I needed to add :paste at the top:

#+begin_src purescript
:paste
class MyClass a where
  fun :: a -> Int

instance funString :: MyClass String where
  fun :: String -> Int
  fun s = 1

fun "asdf"
#+end_src

#+RESULTS:
: PSCi, version 0.14.0
: Type :? for help
: 
: import Prelude
: 
: > … … … … … … … … … 1
: 
: > See ya!
: ()

Otherwise, it will throw an error, because it will execute the code line by line:

#+begin_src purescript
class MyClass a where
  fun :: a -> Int

instance funString :: MyClass String where
  fun :: String -> Int
  fun s = 1

fun "asdf"
#+end_src

#+RESULTS:
#+begin_example
PSCi, version 0.14.0
Type :? for help

import Prelude

> Unexpected token 'class' at line 1, column 1
> Error found:
in module e[33m$PSCIe[0m
at <internal>:0:0 - 0:0 (line 0, column 0 - line 0, column 0)

  The type declaration for e[33mfune[0m should be followed by its definition.


See https://github.com/purescript/documentation/blob/master/errors/OrphanTypeDeclaration.md for more information,
or to contribute content related to this error.


> > Unexpected token 'instance' at line 1, column 1
> Error found:
in module e[33m$PSCIe[0m
at <internal>:0:0 - 0:0 (line 0, column 0 - line 0, column 0)

  The type declaration for e[33mfune[0m should be followed by its definition.


See https://github.com/purescript/documentation/blob/master/errors/OrphanTypeDeclaration.md for more information,
or to contribute content related to this error.


> > > 1

> See ya!
()
#+end_example
1 Like