Skip to content

Emacs haskell-mode: how to send PART of file to repl? #1002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
chauinau opened this issue Nov 25, 2015 · 6 comments
Closed

Emacs haskell-mode: how to send PART of file to repl? #1002

chauinau opened this issue Nov 25, 2015 · 6 comments

Comments

@chauinau
Copy link

In many emacs language modes, there are commands for interacting with a repl/interpreter for that language. Often there are commands for sending just specific parts of the file (like single expressions or subexpressions or regions) to the repl.

For example (among, no doubt, numerous others):

  • In ocaml, with tuareg C-c C-e (tuareg-eval-phrase)
  • In clojure, with cider (M-x cider-jack-in) C-c C-e (cider-eval-last-sexp)
  • In ruby, with ruby-mode C-x C-e (ruby-send-last-sexp)

I'm trying to do this in haskell-mode. I can type stuff in the repl within emacs and it seems to work. And while editing a buffer containing a .hs file, I can load the WHOLE file into the repl with:
C-c C-l (haskell-process-load-file).
BUT I'm not sure how to send to the repl:

  1. only a portion of the file
  2. "naked expressions"

I tried a file with just a single expression and it gave an error. This is no big deal if you have to evaluate the whole file anyway. But if a "portion" were possible (point 1) then being able to send an expression or subexpression to the repl is sometimes useful. I'm just having a little look at Haskell, and am just trying to get a nice interactive setup. Any suggestions on how to send portions of a Haskell file to the repl would be much appreciated. I'm hoping there's just some configuration technique that I'm not aware of. I guess it might be possible to write an emacs mode or modify haskell-mode to do this. But as just a basic emacs user, that's probably not a good option. And also, if it is not possible in the existing haskell-mode (written by someone who knows a lot more Haskell and emacs than I do) then maybe there's some technical difficulty with doing it. Considering it's a fairly obvious thing to do and is done for many other languages.

@gracjan gracjan changed the title Emacs haskell-mode : how to send PART of file to repl ? Emacs haskell-mode: how to send PART of file to repl? Nov 26, 2015
@cocreature
Copy link
Contributor

I outlined some of the issues with doing this in haskell in #893, but let me summarize it:

The main problem is that the syntax for executing code in the repl is different than the one used by ghc. In particular you can’t just take a function definition from a file and paste it in the repl, but you need to use let and similar stuff.

Now you can solve that problem by writing whatever you’ve selected and write that to a temp file which we then load by the repl (if you load a file in ghci, it behaves the same as ghc). The problem is that loading a file throws away about everything that you’ve defined before, so you can’t really incrementally load code that way. The reason for that is that keeping some part while loading new stuff gets significantly harder if the types need to match.

@chauinau
Copy link
Author

Oh, ok...

@PierreR
Copy link
Contributor

PierreR commented Nov 27, 2015

@chauinau the situation might improve from ghc-8. If I recall correctly you won't need to include function in let expression anymore. But I guess it might take while before seeing this kind of feature integrated.
Personally I don't mind too much; reloading the whole buffer is not so bad.

@ryukinix
Copy link

After ghc-8, we have some improvement on this?

@ivan-m
Copy link
Contributor

ivan-m commented Jul 13, 2017

I don't recall coming across anything in GHC 8 that means it can load only part of a file (but we can define data structures; not sure about classes and instances).

It might be possible to do some kind of "send region to ghci and evaluate" (wrapping it in :{ ... :}?) but that's a different story.

@anka-213
Copy link

anka-213 commented Oct 27, 2018

@ivan-m The new feature in GHC 8 is that you don't need to write let before declarations in ghci anymore. This makes the difference between top level declarations and ghci code a lot smaller.

Yes, I think "send region to ghci and evaluate" would be a very useful feature. I found this bug report when trying to do exactly that.


My current use case is that I have a module with a commented out sequence of binding IO actions that I want run in ghci after reloading the file. Something like this:

> p <- startSomeProcess
> thing <- generateOtherThing
> displayThing thing
...

After that I'd experiment with the values I got from these actions in ghci. useThingOnProcess p thing 6.

A similar use case would be if these actions where in actual do notation code in a function in the module.


Another possible use case would be to run the declarations in a where-binding in ghci (initial indentation is ignored). In this case you would first declare values for any used function arguments in ghci, then select the relevant parts of the where clause and press "send region to ghci and evaluate". For example:

functionDefinedInMultipleSteps arg = step3
  where
     step1 = f arg
     step2 = g arg step1
     step3 = h step1 step2

In this example we would want to inspect what values step1, step2 and step3 gets for a specific arg.


The second use case (where-bindings) would work best with wrapping in :{ ... :}. The first use case (do-notation) would require you to send the commands one line at a time to ghci without wrapping them in :{ ... :}. Binding works even with wrapping as long as only one line is contained in each :{ ... :}, so doing it one line at a time manually could be a workaround for my use case.

One option would be to have a smart system that checks if the region is top level/inside where binding or if it is inside do-notation and automatically choose method for sending to ghci.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants