[Haskell-cafe] autocomplete using hoogle

Yuras Shumovich shumovichy at gmail.com
Sun Sep 5 06:57:31 EDT 2010


Just want to share some results of my weekend hacking.

It is clear that haskell type checker can help to build a list of
suggestions for autocomplete (very old idea). I tried to create a very basic
prototype to play with the idea.

The approach I used:
The task can be divided into the next two parts:
  - find the most general type of the word we are trying to complete
    that will satisfy the type checker
  - find all the symbols that "match" the found type
The first task can be solved using ghc. Just replace the word with "()" and
ghc will tell you something like
  Couldn't match expected type `m String' against inferred type `()'
The second task can be solved using hoogle. Just ask it to find everything
that matches "base :: type", where base -- already known part of the word;
type -- the type that ghc expects.

Source code is attached (linux only at a moment)
haskell.vim -- very basic ftplugin for vim
Place it into your ~/.vim/ftplugin directory (don't forget to backup
an existent file if any)
complete.hs -- simple script that does actual work.

How to use it.
cd to the cabal package you are working on at a moment
mkdir dist/hscomplete
copy complete.hs file to the dist/hscomplete
edit complete.hs (at least change the package name, it is hard coded)
create hoogle database for your package:
  cabal haddock --hoogle
  hoogle --convert=dist/doc/html/<packageName>/<packageName>.hoo +base
+directory +... (all packages you depend on)
start vim (you should be in the directory where <package>.cabal file is placed!)
Use C-X C-O to auto complete

cabalized package "tmp" contains two modules Data.Tmp and Data.Tmp1
Data.Tmp1 imports Data.Tmp
Data/Tmp.hs contains
  veryLongLongName1 :: Int
  veryLongLongName1 = 1

  veryLongLongName2 :: Char
  veryLongLongName2 = 'c'

  veryLongLongName3 :: String
  veryLongLongName3 = "Hello"

vim src/Data/Tmp1.hs

  import Data.Tmp

  tmp1 :: Monad m => [a] -> m Int
  tmp1 a = very<C-x C-O> suggests veryLongLongName1

  tmp2 :: Monad m => [a] -> m Char
  tmp2 a = very<C-x C-O> suggests veryLongLongName2 and veryLongLongName3

  tmp3 :: Monad m => [a] -> m String
  tmp3 a = very<C-x C-O> suggests veryLongLongName3

Warning: not ready for real use (no error handling, a lot of hard
codes, slow, etc). Just for playing

-------------- next part --------------
A non-text attachment was scrubbed...
Name: haskell.vim
Type: application/octet-stream
Size: 1043 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20100905/8d7275fa/haskell.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: complete.hs
Type: text/x-haskell
Size: 2169 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20100905/8d7275fa/complete.bin

More information about the Haskell-Cafe mailing list