[Haskell-cafe] On to applicative
Vo Minh Thu
noteed at gmail.com
Tue Aug 31 14:40:22 EDT 2010
2010/8/31 michael rice <nowgate at yahoo.com>
>
> I'm not sure if my terminology is correct or even if my question makes sense, but I can create "instances" of Maybe, List, IO, and Either.
>
> Prelude Data.Either> let m = Just 7
> Prelude Data.Either> :t m
> m :: Maybe Integer
We say that m has type Maybe Integer, so :: is pronounced 'has type'.
We also say that m is a value. Just is a type constructor, Maybe Int
is a type, and Just 7, like m, is a value.
So we don't talk about instance here. Informally you could say that 7
is an instance of Int, but in Haskell we use 'instance' to mean
something (precisely) else.
This pharse is correct w.r.t to the use of 'instance' in Haskell:
Maybe is an instance of the Functor class.
> Prelude Data.Either> let l = 2:[]
> Prelude Data.Either> :t l
> l :: [Integer]
>
> Prelude Data.Either> let g = getLine
> Prelude Data.Either> :t g
> g :: IO String
>
> Prelude Data.Either> let e = Right "abc"
> Prelude Data.Either> :t e
> e :: Either a [Char]
>
> All these instances are functors, each with its own version of fmap that can be applied to it.
>
> How can I similarly create an instance of (->) so I can apply (->)'s version of fmap
>
> instance Functor ((->) r) where
> fmap f g = (\x -> f (g x))
>
> to it?
Note that for Maybe, the instance is define with
instance Functor Maybe where ...
Note how the type argument of Maybe is not given.
But above, when you create a value, it has type Maybe Int, not only Maybe.
So for the ((->) r) case, you still want to "complete" it.
E.g.
m :: Maybe Int -- not just Maybe
(+) :: (->) Int Int -- and not only (->) Int
Cheers,
Thu
> Michael
>
> --- On Tue, 8/31/10, Vo Minh Thu <noteed at gmail.com> wrote:
>
> From: Vo Minh Thu <noteed at gmail.com>
> Subject: Re: [Haskell-cafe] On to applicative
> To: "michael rice" <nowgate at yahoo.com>
> Cc: haskell-cafe at haskell.org
> Date: Tuesday, August 31, 2010, 1:50 PM
>
> 2010/8/31 michael rice <nowgate at yahoo.com>
> >
> > So it's a type constructor, not a type? Could you please provide a simple example of its usage?
>
> Sure, although I'm sure you've come by some already.
>
> -- the identity function
> id :: a -> a
> -- often, we write it like this:
> -- id x = x
> -- but here we see the relationship between the ananymous function
> syntax and the function type:
> id = \x -> x
>
> In fact, if you write in prefix form, it is quite familiar:
> f :: (->) Int Bool
> e = Either String Float
>
> Cheers,
> Thu
>
> > Michael
> >
> > --- On Tue, 8/31/10, Vo Minh Thu <noteed at gmail.com> wrote:
> >
> > From: Vo Minh Thu <noteed at gmail.com>
> > Subject: Re: [Haskell-cafe] On to applicative
> > To: "michael rice" <nowgate at yahoo.com>
> > Cc: haskell-cafe at haskell.org
> > Date: Tuesday, August 31, 2010, 1:17 PM
> >
> > 2010/8/31 michael rice <nowgate at yahoo.com>
> > >
> > > "Learn You a Haskell ..." says that (->) is a type just like Either. Where can I find its type definition?
> >
> > You can't define it *in* Haskell as user code. It is a built-in infix
> > type constructor (Either or Maybe are type constructors too, not just
> > types). In fact, if you want to implement a simple, typed functional
> > language, you'll find it is the only built-in type constructor you
> > have to implement (as the implementor of the language).
> >
> > Also,
> > Show a => a
> > is a type too, but you won't find a definition for 'a' or for '=>'.
> > All those things are defined by the language.
> >
> > Cheers,
> > Thu
> >
>
More information about the Haskell-Cafe
mailing list