Dante: Emacs mode for Interactive Haskell
Dante provides a frontend to GHCi features: type-checking, execution, completion and cross referencing. It integrates with standard Emacs tooling as much as possible.
Feature summary and cheat-sheet
Feature | Mode/Command | Keybinding |
---|---|---|
On the fly type checking | flymake-mode | |
Completion | company-mode | |
Type in echo area | eldoc-mode | |
Goto definition | xref-find-definitions | M-. |
Find uses | xref-find-references | M-? |
Remote operation | (automatic with tramp) | |
Error correction | attrap-attrap | |
Type of selection | dante-type-at | C-c . |
Info at point | dante-info | C-c , |
REPLoid | dante-eval-block | C-c ” |
Restart | dante-restart | |
Diagnosis | dante-diagnose |
REPLoid
You can evaluate code by writing it in a comment of the form
-- >>>
and run dante-eval-block
.
Example:
example :: [String]
example = ["This is an example", "of", "interactive", "evaluation"]
-- >>> intercalate " " example
In the above file, if you invoke dante-eval-block
on the line
containing “intercalate”, you’ll get:
-- >>> intercalate " " example
-- "This is an example of interactive evaluation"
--
Several commands in the same block will be executed in at once, so you can have local let statements.
-- >>> let foo = "foo"
--
-- >>> foo ++ "bar"
-- "foobar"
---
Any GHCi command can be put in such a block, but note however that:
- The GHCi state will not be maintained across several calls to
dante-eval-block
. In fact, Dante liberally executes:r
and:l
, and (re)sets various GHCi options. - It is not supported to load and/or unload modules in such blocks, or set unexpected options. This may work, or may mess with Dante internals.
So if your goal is run your webserver/database/etc. within GHCi, you should not do it using dante.
Completion
Completion works only when the current file can be loaded by GHCi (ie. is free of errors). So, this is not very useful. To mitigate the problem, Dante defers type-errors to runtime when loading.
Remote operation
When loading a remote (Tramp) path, GHCi will be run on the remote host, automatically.
However, if programs such as nix-shell
are not found on the remote host, you
may need to adjust the tramp path. For example:
(add-to-list 'tramp-remote-path 'tramp-own-remote-path)
Error correction
Error correction is implemented in the sister package attrap.
hlint
Using Dante is a GHCi interaction mode. Therefore it does not provide any support for hlint. However, you can use a third party support for hlint in addition to Dante. In fact attrap even provides support for interactive application of hints. See that package documentation for configuration.
Installation
Dante can be installed by any usual means. (Use-package is a pretty popular option)
Configuration
Eldoc
Dante has builtin Eldoc support (showing info about the symbol at point in the echo area when idle.) Unfortunately, at the time of writing (Oct 2022), the standard Haskell mode uses the old eldoc API, overriding Dante’s Eldoc support. I recommend just disabling the standard Haskell mode Eldoc support, which IMO isn’t very helpful anyway, like so:
(add-hook 'haskell-mode-hook
(defun my-fix-hs-eldoc ()
(setq eldoc-documentation-strategy #'eldoc-documentation-default)))
Configuring the GHCi loading method
Configuration can be important to make sure that GHCi is properly loaded by dante. Even though Dante will do its best to figure out the proper way to load GHCi for your project, it may still fail. You can guide Dante’s behavior by customizing variables. Note in particular that customization can be done on a per-file, per-package or per-project basis by using file- and directory-local variables (as recommended above).
In fact typical way to configure GHCi command line is to a add a
.dir-locals.el
file to your project root which sets the loading
method. The loading method is a recipe to find out the root of the
project and the command line to use to start GHCi.
((nil . ((dante-methods . (new-impure-nix)))))
Replace new-impure-nix
with the proper value, which you can figure
out by M-x describe-variable <RET> dante-methods-alist
.
Configuring the Cabal target
For a multi-target project, it can be necessary to tell dante which
target to pass to the cabal repl
or stack
command. The best method
is to create another .dir-locals.el
file in the top-level directory
of the sources of the target in question. For instance, if a sil-parser-test
executable resides in stand-in-language/test/
, you can create the
following file in that directory:
((nil . ((dante-target . "sil:sil-parser-test"))))
When using stack
and a test suite, the following configuration in
the test source directory will cause the --test
flag to be passed
when loading the files there:
((nil . ((dante-target . "--test"))))
More control over the GHCi command line
For more direct control over the command line, you can set
dante-repl-command-line
directly. If Dante additionally fails to
find the project root using any of the dante-methods
, configure
dante-project-root
explicitly. (Do it using dir-locals.el
.)
Example full configuration
(use-package dante
:ensure t ; ask use-package to install the package
:after haskell-mode
:commands 'dante-mode
:init
;; flycheck backend deprecated October 2022
;; (add-hook 'haskell-mode-hook 'flycheck-mode)
(add-hook 'haskell-mode-hook 'flymake-mode)
(remove-hook 'flymake-diagnostic-functions 'flymake-proc-legacy-flymake)
(add-hook 'haskell-mode-hook 'dante-mode)
(add-hook 'haskell-mode-hook
(defun my-fix-hs-eldoc ()
(setq eldoc-documentation-strategy #'eldoc-documentation-default)))
:config
(require 'flymake-flycheck)
(defalias 'flymake-hlint
(flymake-flycheck-diagnostic-function-for 'haskell-hlint))
(add-to-list 'flymake-diagnostic-functions 'flymake-hlint)
;; flycheck backend deprecated October 2022
;; (flycheck-add-next-checker 'haskell-dante '(info . haskell-hlint)))
Troubleshooting
If dante-type-at
gives Couldn't guess that module name. Does it
exist?
or xref-find-definitions
gives ~No definitions found for:
“/tmp/danteTqJJvj.hs” ~, you may need to add your targets to
.dir-locals.el
; see the Configuration section above.
Finally, Use M-x customize-group dante
to read the documentation for
all customizable variables.