How to set up org-babel for Haskell with Stack
Asked Answered
E

4

19

I'm running:

  • GNU Emacs 24.4.1
  • Stack Version 1.3.3
  • org-mode
  • haskell-mode

I've looked through:

While the gist above looks promising, I haven't found anything that looked to be an authoritative way to get org-haskell running (eg, nothing on melpa), and certainly nothing aimed specifically at whatever intricacy running a stack environment rather than using my global ghc would entail.

When I try to:

#+BEGIN_SRC haskell
let x = "test"
putStrLn x
#+END_SRC

I get

executing Haskell code-block

...which hangs forever. When I C-g, I see:

GHCi, version 7.6.3: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Some flags have not been recognized: prompt2, ghci     | 
ghci    λ> let x = "TESTING!"
putStrLn x
"org-babel-haskell-eoe"
Prelude| 
<interactive>:4:1: parse error on input `putStrLn'
ghci    λ> "org-babel-haskell-eoe"

When I tab to the haskell buffer, I see it has genuinely evaluated what I sent, it just has this org-babel-haskell-eoe error and never returns control to my org session.

Any chance this is because I have a custom prompt? Using the lambda instead of Prelude> ?

Evolution answered 7/2, 2017 at 3:49 Comment(7)
You might find this other question useful: #42081879Phenix
You need to load ob-haskell.el (or add haskell to org-babel-load-languages and restart emacs) before you can try evaluating haskell code blocks. That should get rid of the "No org-babel-execute function for haskell" message at least.Medina
@brittUWaterloo, I believe you linked to this very question of mine.Evolution
@Nick, ob-haskell.el from where?Evolution
It's part of "official" org-mode: you should be able to just say (require 'ob-haskell) and get it loaded. But I'm not sure whether it is going to help with anything other than that initial problem, which you might (or might not) have gotten past by now.Medina
Thanks, @Nick. Yep, you're right, I loaded that and same hanging message.Evolution
"org-babel-haskell-eoe" is a marker that ob-haskell uses to separate the results from the rest of the interpreter output, so that it can cut out the chaff and present the results in the org-mode buffer.Medina
M
6

This is not a complete answer: in particular, it does not even mention Stack. But I (a complete ignoramus on Haskell) wanted to find out what it would take to get the OP's test program to run in babel. Here's what I found:

  • You need a haskell interpreter ;-) I'm on Fedora 24, so I installed the ghc-compiler package and I got ghci.

  • You need haskell-mode. I installed that from MELPA, using the emacs package manager. That also installed inf-haskell.el

  • By default, inf-haskell wants to run hugs, so I customized haskell-program-name and set it to "ghci".

  • M-x load-library RET ob-haskell RET

  • C-c C-c on the code block: the first time it fails and the Messages buffer shows "Buffer haskell.org does not exist or has no process".

  • But if you do it C-c C-c on the code block again, it succeeds!

Obviously, ob-haskell.el needs some work - and that's before we even get to Stack, of which I know even less than I know of Haskell, so I'll leave that as an exercise for the interested reader :-)

EDIT: Re. version info (requested in a comment): I keep close to the bleeding edge. At this point in time (2017-05-01), I run Org mode version 9.0.5 (release_9.0.5-444-g998576 @ /home/nick/elisp/org-mode/lisp/) and GNU Emacs 26.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.20.10) of 2017-04-14

Medina answered 17/2, 2017 at 16:15 Comment(0)
C
2

It is probably because of the custom prompt: I had the same issue, and when I removed ":set +t" and ":set prompt "GHCI >" from ~/.ghci, it worked. I fiddled with it some, and it seems it will work so long as your custom prompt doesn't have any spaces in it except the end (I changes mine to "GHCI> " and it works). It seems to be that the regular expression it parses the information from assumes the prompt will have no spaces in it.

Channing answered 11/1, 2018 at 18:33 Comment(0)
E
2

In my case it was .ghci, like Testare's. As soon as I commented out :set prompt "λ ", emacs stopped freezing but gave the message ‘org-babel-script-escape’ expects a string. It was necessary to comment out :set +t for it to work.

Ellerd answered 31/5, 2021 at 3:28 Comment(2)
I'm at the stage of seeing org-bable-script escape expects a string, but it's unclear from the docs of that function why that would be occurring.Invoice
:set prompt "> " works best. The > is embedded in the regexp somewhere in ob-haskell.el.Agnesagnese
M
1

what i did after installed stack and ghci (with stack itself)), was to install intero in emacs and then add those to init.el:

 (setq haskell-process-type 'stack-ghci)
 ((org-babel-do-load-languages
     'org-babel-load-languages
        '((haskell . t)))

after that i can C-c C-c in haskell code block and i get a result under the code block.

Mckale answered 18/3, 2019 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.