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

peterv bf3 at telenet.be
Mon Jun 18 10:44:06 EDT 2007

That looks cool.

Just another wild idea which I might find useful, but is more like
refactoring, is to convert the fields of a record to get/set type-classes,
and refactor all usages of those fields.


data Person = Person { name :: String, age :: Float }

main = print $ name p ++ " is " ++ show (age p) ++ " years old"
	where p = Person { name = "Homer", age = 41 }

Would refactor into (just wild Haskell code from a newbie here)

data Person = Person String Float

class HasName a where
    nameOf :: a -> String
    withName :: a -> String -> a

class HasAge a where
    ageOf :: a -> Float
    withAge :: a -> Float -> a

instance HasName Person where
    nameOf (Person name age) = name
    withName (Person name age) newName = Person newName age

instance HasAge Person where
    ageOf (Person name age) = age
    withAge (Person name age) newAge = Person name newAge

defaultPerson = Person "" 0

main = print $ nameOf p ++ " is " ++ show(ageOf p) ++ " years old"
    where p = defaultPerson `withName` "Homer" `withAge` 41
-- or just where p = Person "Homer" 41


Visual Studio, Eclipse, IntelliJ etc already have these kind of wizards to
encapsulate fields with setters getters for C#/Java, and also introduce
boiler plate code, although less of it. However, Haskell turns each field
into a separate type class, so this is much more reusable code than their OO


-----Original Message-----
From: haskell-cafe-bounces at haskell.org
[mailto:haskell-cafe-bounces at haskell.org] On Behalf Of Claus Reinke
Sent: Monday, June 18, 2007 14:24
To: haskell-cafe at haskell.org
Subject: Re: [Haskell-cafe] Useful IDE features - "implement instance"

> Another feature which would be cool for an IDE is: "implement instance".
> 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
    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..


Haskell-Cafe mailing list
Haskell-Cafe at haskell.org

No virus found in this incoming message.
Checked by AVG Free Edition. 
Version: 7.5.472 / Virus Database: 269.9.0/852 - Release Date: 17/06/2007

No virus found in this outgoing message.
Checked by AVG Free Edition. 
Version: 7.5.472 / Virus Database: 269.9.0/852 - Release Date: 17/06/2007

More information about the Haskell-Cafe mailing list