[Haskell-cafe] Useful IDE features - "implement instance"

Claus Reinke claus.reinke at talk21.com
Mon Jun 18 08:24:02 EDT 2007


> Another feature which would be cool for an IDE is: "implement instance". So
> you automatically get to see all the functions of a type class you need to
> implement. Using C#/Java, this is used all over the place.

sounds potentially useful, but perhaps not quite as useful as one might
expect: if you only want to see all the class methods, hugs/ghci provide 
the ':info' command (and haskell modes tend to provide access to that). 

    $ ghc -e ':i Monad'
    class Monad m where
      (>>=) :: m a -> (a -> m b) -> m b
      (>>) :: m a -> m b -> m b
      return :: a -> m a
      fail :: String -> m a
            -- Defined in GHC.Base
    instance Monad Maybe -- Defined in Data.Maybe
    instance Monad IO -- Defined in GHC.IOBase
    instance Monad [] -- Defined in GHC.Base

with a little bit of filtering and replacing, we get

    $ ghc -e ':i Monad' | sed -n '/^class/,/-- Defined in/{s/class/instance/;p}'
    instance Monad m where
      (>>=) :: m a -> (a -> m b) -> m b
      (>>) :: m a -> m b -> m b
      return :: a -> m a
      fail :: String -> m a
            -- Defined in GHC.Base

i've used sed here, to keep it editor-independent, one can do the
equivalent within emacs/vim, without sed. now, if one wanted to
save typing, one might want to translate the type declarations into
definition templates, but the type has more information than such
template, and there are many forms of definition that fit a type, so
having to replace the type declarations with definitions is perhaps 
as good as it gets?

a similarly useful code template generation transformation would 
be to introduce complete case distinctions over sum types, so that

    f x = undefined

would, if we knew (x::Maybe a), become

    f (Just a) = undefined
    f Nothing = undefined

or 'doSomething >>= \(x::Either l r)->body' would become

    doSomething >>= \x->case x of {Left l->body; Right r->body}

which, of course, should rather be

    doSomething >>= either (\l->body) (\r->body)

yes, there are many opportunities for making haskell editing
easier, and not all of them require detailed editor hacking or
haskell analysis and transformation skills (though some do).


keep the suggestions coming. perhaps summarize them on a
haskell.org wiki page, though, so they don't get lost. someone
might get round to implementing them, some of them might
already be available!-)

if someone were to put up a simple table/list of desired ide
features (with brief descriptions) on the wiki, everyone could
add links to each feature showing how their favourite ide 
handles said feature. 

then new users could go through that list and choose to learn
one of those ides that provides most of the features they need.
and fans of a particular ide could use the list to pick any 
missing feature that they feel able to implement..

claus



More information about the Haskell-Cafe mailing list