MyNixOS website logo
Description

A tool to generate missing import statements for Haskell modules.

This tool uses the Haskell Suite [0] to determine the unbound variables and types in your source code, and generate import statements for them. If there are multiple choices, it provides a simple interactive menu for you to choose from. See the home page for more details. [0] https://github.com/haskell-suite

Halberd: generating imports

Halberd is a tool to help you add missing imports to your Haskell source files. With it, you can write your source without imports, call Halberd, and just paste in the import lines.

Currently, it tries to automatically choose an import if there is a single sensible option. If it can't, it will prompt you with a simple menu. After running, it prints the imports, which you need to copy manually. Editor integration is planned.

The imports generated are either qualified, if the unbound function looks like M.lookup, or explicit, if it looks like void.

Example

As an example, say you have written the following (nonsensical and type incorrect) Haskell file called test.hs:

main :: IO Int8
main = do
    forM_ [1,2] $ \x -> print $ M.lookup x table
    liftM id $ return $ headNote "Impossible" [10]

table :: M.Map String Int8
table = M.fromList [("Odeca", 1), ("Hackathon",2)]

You can run Halberd on this by running halberd test.hs. It will prompt you with three questions:

forM_:
1) Control.Monad
2) Data.Foldable

M.lookup:
1) Data.List
2) GHC.List
3) Prelude
4) Data.IntMap
5) Data.IntMap.Lazy
6) Data.IntMap.Strict
7) Data.Map
8) Data.Map.Lazy
9) Data.Map.Strict

Int8:
1) Data.Int
2) Foreign
3) Foreign.Safe
4) GHC.Int

After making the choices by typing 1, 7 and 1, it generated the folling output:

------------- Could not find import for -------------
 - headNote

-------- Insert these imports into your file --------

import qualified Data.Map as M
import Control.Monad ( forM_, liftM )
import Data.Int ( Int8 )

As you can see, it didn't ask questions about M.Map, M.fromList, liftM and the second usage of Int8. It figured these out either because of previous choices, or because there was only a single option.

Installation

Halberd uses the Haskell Suite packages (haskell-src-exts, haskell-packages and haskell-names) for parsing, name resolution and finding exposed identifiers of packages. While these can be installed from hackage, generating databases of names for new packages currently needs a custom version of the cabal install executable. See the documentation for haskell-names for more details.

To install Halberd, just do

cabal install halberd

This will give you a halberd executable that takes a single argument, the file to generate imports for. By default, it only draws names from base. If you want to add more names, use hs-gen-iface from the haskell-names package.

Improvements

Halberd is still in an unfinished state. Some planned improvements are:

  • Smarter automatic choices.
  • Editor integration.
  • Easier installation of name databases (haskell-names).
  • Integration with existing imports.
  • Better choice UI.
  • Choices based on type information (haskell-type-exts).

Contributions are welcome!

Metadata

Version

0.1.2.9

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows