[Haskell-cafe] Definition of List type?

Daniel Fischer daniel.is.fischer at web.de
Fri Jul 30 16:23:04 EDT 2010

On Friday 30 July 2010 21:54:20, michael rice wrote:
> Thanks all,
> Now that I have a (very) rudimentary understanding of Haskell, I figured
> I'd back up and have a closer (conceptual) look at type definitions to
> see what they have in common, and just happen to pick Maybe and List.
> I also noticed Maybe has a list of "Instances"
> Monad Maybe
> Functor Maybe
> Typeable1 Maybe
> MonadFix Maybe
> MonadPlus Maybe
> etc.
> while List has none, at least I don't see any in Data.List. Same reason?

Data.List provides only functions for working with lists, while Data.Maybe 
also contains the definition of the type.

Type class instances are documented
- at the type class
- at the data type

Since the list datatype isn't documented like the other types (probably 
because it's not defined in valid Haskell but built-in), the list instances 
appear only at the type classes (and at the Monad documentation, you find 
"Monad []" listed as the first instance.

> >From "Learn You A Haskell:"
> "If a type is a part of a typeclass, that means it supports and
> implements the behavior the typeclass describes."
> I'm way out on a limb here, but isn't Monad a typeclass?


> and if, as we
> say above, that Maybe is an instance of Monad, wouldn't there have to be
> instance Monad Maybe where
>  return = ...  -- return for Maybe
>  >>= = ...     -- bind for Maybe
>  etc.
> somewhere?

Yes, there is. In GHC at least, it's defined in Data.Maybe:

instance  Monad Maybe  where
    (Just x) >>= k      = k x
    Nothing  >>= _      = Nothing

    (Just _) >>  k      = k
    Nothing  >>  _      = Nothing

    return              = Just
    fail _              = Nothing

> Where? It's not in Data.Maybe. Is there some kind of scheme
> for defining this stuff, i.e., this goes here, that goes there?

Instance declarations should be in the same module where
- the class is defined, or
- the type is defined
if possible.

More information about the Haskell-Cafe mailing list