Typeclass(Interface) is Kernel Concept of Haskell Functor, Monad These Weird
chenge

chenge @chenge

About: Ruby, and learn Rust, Go, Elixir, Erlang...

Location:
China
Joined:
Sep 10, 2017

Typeclass(Interface) is Kernel Concept of Haskell Functor, Monad These Weird

Publish Date: Jan 26 '19
8 1

In short, typeclass is just like interface, so it's not so hard to understand.

Let's see some code about Maybe functor, functor has a special map function:

class Functor Maybe where
    fmap :: (a -> b) -> Maybe a -> Maybe b

instance Functor Maybe where
    fmap func (Just x) = Just (func x)
    fmap func Nothing  = Nothing

Enter fullscreen mode Exit fullscreen mode

Functor is a typeclass.
Applicative Functor extends Functor.
Monad extends Applicative.

class Applicative m => Monad m where
    return :: a -> m a
    (>>=) :: m a -> (a -> m b) -> m b
Enter fullscreen mode Exit fullscreen mode

And another one Monoid is a math concept. It's a typeclass too.

They are all typeclasses.

ref


Functor、Applicative 和 Monad(Chinese)

Learn You a Haskell for Great Good!

Comments 1 total

  • Jan van Brügge
    Jan van BrüggeJan 28, 2019

    Just a small side note, in the class declaration you don't specify a concrete type:

    class Functor f where
        fmap :: (a -> b) -> f a -> f b
    
Add comment