
From: Data.Maybe Description The Maybe type, and associated operations. From: Data.List Description Operations on lists. One description has the type and associated operations, the other only has the operations. Where can I find the type definition for List, and why isn't it in Data.List? Michael

Excerpts from michael rice's message of Fri Jul 30 14:41:44 -0400 2010:
One description has the type and associated operations, the other only has the operations.
Where can I find the type definition for List, and why isn't it in Data.List?
Hello Michael, This is because the List datatype is built into Haskell. A close approximation to it would be: data List a = Nil | Cons a (List a) where List a is [a] (type) Nil is [] (constructor) Const x xs is x:xs (constructor) Cheers, Edward

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?
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? 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?
Michael
--- On Fri, 7/30/10, Edward Z. Yang
Const x xs is x:xs (constructor)
That should be a Cons, not Const. :o) Cheers, Edward

On Fri, Jul 30, 2010 at 8:54 PM, michael rice
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?
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? 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?
It *is* in Data.Maybe: http://hackage.haskell.org/packages/archive/base/4.2.0.1/doc/html/src/Data-M... Generally speaking a type class instance should go either where the type is defined, or where the class is defined, although there are exceptions. In any case, if you can get the instance loaded in ghci, you can find out where its defined: ghci> :i Maybe data Maybe a = Nothing | Just a -- Defined in Data.Maybe instance (Eq a) => Eq (Maybe a) -- Defined in Data.Maybe instance Monad Maybe -- Defined in Data.Maybe instance Functor Maybe -- Defined in Data.Maybe instance (Ord a) => Ord (Maybe a) -- Defined in Data.Maybe instance (Read a) => Read (Maybe a) -- Defined in GHC.Read instance (Show a) => Show (Maybe a) -- Defined in GHC.Show

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?
Yes.
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.

Hi Daniel,
OK, now I'm getting somewhere. I was looking here:
http://haskell.org/ghc/docs/6.12.1/html/libraries/base-4.2.0.0/Data-Maybe.ht...
instead of in the source.
Seem to be on the right track. I'm going to do some more poking around.
Thanks a lot, guys.
Michael
--- On Fri, 7/30/10, Daniel Fischer
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?
Yes.
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.

List are actually built in to the language, but they are roughly equivalent to the following definition: data List a = [] | a:List a The reason why this definition never actually appears is because it defines the constructors using operators rather than names, which is not allowed in vanilla Haskell. (There is an extension, TypeOperators, however, that does allow this.) Cheers, Greg On 07/30/10 11:41, michael rice wrote:
From: Data.Maybe
Description The Maybe type, and associated operations.
From: Data.List
Description Operations on lists.
One description has the type and associated operations, the other only has the operations.
Where can I find the type definition for List, and why isn't it in Data.List?
Michael
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Fri, Jul 30, 2010 at 7:50 PM, Gregory Crosswhite
The reason why this definition never actually appears is because it defines the constructors using operators rather than names, which is not allowed in vanilla Haskell. (There is an extension, TypeOperators, however, that does allow this.)
Nope: see Data.Complex.Complex; only infix *type* constructors are nonstandard. The thing about lists that makes them impossible to define in normal Haskell is the [a] syntax, which is some kind of "outfix" type constructor, which no amount of currently available extensions will allow. In addition, the constructor [] for the empty list isn't a normal constructor, syntactically, because it doesn't start with an uppercase character or a colon. Basically, lists are so ubiquitous in Haskell that they have their own special syntax, which cannot be defined like any other data type. It is simple, as others have said, to define a new data type that works identically to lists.

Ben Millwood wrote:
On Fri, Jul 30, 2010 at 7:50 PM, Gregory Crosswhite
wrote: The reason why this definition never actually appears is because it defines the constructors using operators rather than names, which is not allowed in vanilla Haskell. (There is an extension, TypeOperators, however, that does allow this.)
Nope: see Data.Complex.Complex; only infix *type* constructors are nonstandard. The thing about lists that makes them impossible to define in normal Haskell is the [a] syntax, which is some kind of "outfix" type constructor, which no amount of currently available extensions will allow.
It's normally called "confix" or "circumfix". Similarly, the tuple syntaxes can't be defined in Haskell because they use "mixfix" notation (which apparently some folks call "distfix"[1]). Also, lists support a mixfix sugar where [a,b,...,c] = a:b:...:c:[] Agda does support full mixfix notation. While it's nice in theory, when combined with the verbosity of dependently typed languages it gets illegible pretty quickly for the uninitiated. [1] http://github.com/noteed/syntactical -- Live well, ~wren
participants (6)
-
Ben Millwood
-
Daniel Fischer
-
Edward Z. Yang
-
Gregory Crosswhite
-
michael rice
-
wren ng thornton