Stepping into arbitrary monads from a REPL

GHCi always runs statements in IO. I wanted to step into StateT, ReaderT or an app monad, work there for a while, and step back out. So I made a small proof of concept.

:e runner takes any runner :: n () -> m (), infers n with a typed hole, and opens a nested REPL inside n. :q steps back out.

Code: haskell-playground/exe/playground.hs at repl · bglgwyng/haskell-playground · GitHub

Related: GHC #9966 proposes the same idea for GHCi.

17 Likes

This is very interesting, and something I’ve wanted in order to run Bluefin code in GHCi. I
guess n has to have a Monad instance, and each command in the nested block must be of type n. Is that right? I don’t see how to make that work with Bluefin, where the functions I’d be working with have types like (forall e. Throw ex e -> Eff (e :& es) r) -> Eff es r, that is, the nested block has something new in scope (the Throw ex e). Can that be accommodated in your setup? (Of course, we could use something like ReaderT, but that more or less defeats the purpose.)

I suggested something similar in the past: Haskell's missing mutable reference type - #35 by tomjaguarpaw so great to see you have a working version. I hope there’s a way of doing an equivalent that works for Bluefin. I’m curious how you dealt with the objection in Haskell's missing mutable reference type - #39 by prophet.

EDIT: Perhaps this is what’s going on:

  • User types a command of type n a
  • This wrapper adds >>= liftIO . print, turning it into n () (and requiring MonadIO n)
  • runner turns it into m () (the outer monad type), which can be run by GHCi

So the runner function works on the level of individual commands. This is a neat trick and avoids the problem with my original plan! I’ll have to see if I can get this to work with Bluefin-style handlers. Seems plausible…

1 Like

I’m not sure if I understand you correctly, but runner evaluates session itself. And it can be done recursively so that we can nests multiple effects.

I think that the prophet’s point is correct, and I didn’t realize before that we have some invalid runner definitions that prophet provides. When you prompt :e \(m :: IO ()) -> pure (), it just immediately quit the inside session, this behavior might be ok?. But when you prompt :e \(m :: IO ()) -> m >> m, you can quit twice. Maybe we can fix this by using new monad transformer definitions with linearity? Or, we can put some counter logics in session and check if it’s evaluated more than once.

I’m not very familiar with Bluefin yet, so I’m afraid I can’t give a good answer on that part. My implementation here is compatible with general monad -> monad runner. But it is required to decide the types concretely in the action type inference phase. I’m not sure if (forall e. Throw ex e -> Eff (e :& es) r) -> Eff es r has any problem with type inference.

Ah yes, I see. I misunderstood some of how your implementation works. In fact now it seems like you’ve implemented a sort of “inner GHCi”. Is that right? For example, you draw your own prompt. I think in principle it would be possible to get Bluefin handlers to work with this new kind of thing, but would probably require new syntax to bind the new variable.