Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Haskell REPL within a Haskell application

Tags:

I'm trying to embed a Haskell REPL within one of my Haskell applications. The idea would be that only a subset of the Haskell libraries would be loaded by default, plus my own set of functions, and the user would use those in order to interact with the environment.

To solve this problem, I know one way would be to create a (mini-)Haskell parser + evaluator and map my mini-Haskell parser's functions to actual Haskell functions, but I'm sure there is a better way to do this.

Is there a nice and clean way to build a REPL for Haskell using Haskell?

like image 850
CharlieP Avatar asked Jul 05 '11 21:07

CharlieP


People also ask

Does Haskell have a REPL?

Haskell has a nice REPL known as GHCi, where GHC refers to the main Haskell compiler and the 'i' is for interactive.

How do I run Haskell REPL?

Repl is opened in the context of the current Atom editor, so there is no need to load the file (even though :l filename will work). Type any ghci command in the bar at the bottom of the window and press shift+enter (on Win and Linux) or cmd+enter (on macOS) to execute it.

What is REPL Haskell?

A read–evaluate–print loop (REPL) environment takes single user inputs, executes them, and returns the result to the user. GHCi is GHC's interactive environment. The stack ghci or stack repl commands, which are equivalent, allow you to load components and files of your project into GHCi.

What is the difference between GHC and GHCi?

Introduction. GHCi is GHC's interactive environment, in which Haskell expressions can be interactively evaluated and programs can be interpreted.


1 Answers

A few things that already exist:

  • GHCi, of course, both in the sense of being able to look at how it's implemented or being able to use it directly (i.e., have your REPL just talk to GHCi via stdin/stdout).
  • The full GHC API, which lets you hook into GHC and let it do all the heavy lifting for you--loading files, chasing dependencies, parsing, type checking, etc.
  • hint, which is a wrapper around a subset of the GHC API, with a focus on interactive interpretation rather than compilation--which seems to fit what you want to do.
  • mueval, an evaluator with limits on loaded modules, resource use, etc, basically a "safe" interactive mode. It's what lambdabot uses, if you've ever been in the #haskell IRC channel.

All of the above are assuming that you don't want to deal with writing a Haskell interpreter yourself, which is probably the case.

like image 165
C. A. McCann Avatar answered Oct 09 '22 10:10

C. A. McCann