Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  basic Functor, Applicative and Monad instances (Imants Cekusins)
   2. Re:  basic Functor,       Applicative and Monad instances
      (Rein Henrichs)
   3. Re:  basic Functor,       Applicative and Monad instances
      (Imants Cekusins)
   4. Re:  basic Functor,       Applicative and Monad instances
      (Imants Cekusins)
   5. Re:  basic Functor,       Applicative and Monad instances
      (Imants Cekusins)


----------------------------------------------------------------------

Message: 1
Date: Thu, 16 Jul 2015 21:52:36 +0200
From: Imants Cekusins <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] basic Functor, Applicative and Monad
        instances
Message-ID:
        <cap1qinzb47k2qolkcuco-5la6u7qfdmp-vkmhv+k4ur+96g...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

Here are complete working snippets with Functor, Applicative and Monad
instances. This is my first attempt to write own instances rather than
use ready ones.

Here we go:



module Part.Monad where

import Control.Applicative
{-# ANN module ("HLint: ignore Use let"::String) #-}

 {-
   Val' { val = b1 } can also be written as
   Val' b1
 -}


data Val a = Val' {
    val::a
}  deriving Show


data Weather = Cold | Warm deriving Show


instance Functor Val where
-- (a -> b) -> f a -> f b
   fmap ab fa = let  a1 = val fa
                     b1 = ab a1
                in Val' { val = b1 }


instance Applicative Val where
-- a -> f a
   pure a =  Val' { val = a }

-- f (a -> b) -> f a -> f b
   (<*>) fab fa =  let  ab1 = val fab
                        a1 = val fa
                        b1 = ab1 a1
                   in Val' { val = b1 }


instance Monad Val where
-- a -> m a
   return a = Val' { val = a }

--  m a -> (a -> m b) -> m b
   (>>=) ma amb = let a1 = val ma
                 in amb a1


-- pure and return in this example are interchangeable
main::Int -> IO()
main i = do                         -- do:   Val as monad
   v1 <- pure Val' { val = i }      -- pure: applicative

   v2 <- return $ over20 <$> v1     -- <$> : functor
   print v2

   v3 <- return $ Val' weather <*> v2  -- <*> : applicative
   print v3


over20::Int-> Bool
over20 i
   | i > 20 = True
   | otherwise = False


weather::Bool-> Weather
weather False = Cold
weather True = Warm


------------------------------

Message: 2
Date: Thu, 16 Jul 2015 20:26:19 +0000
From: Rein Henrichs <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] basic Functor, Applicative and Monad
        instances
Message-ID:
        <CAJp6G8xhAVK-=agd_+rninketkg5tjmyql2x-qsnnoi3_x5...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

This would all be much easier with pattern matching. For example:

instance Functor Val where
  fmap f (Val x) = Val (f x)

instance Applicative Val where
  pure = Val
  Val f <*> Val x = Val (f x)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150716/a4723d69/attachment-0001.html>

------------------------------

Message: 3
Date: Thu, 16 Jul 2015 22:45:53 +0200
From: Imants Cekusins <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] basic Functor, Applicative and Monad
        instances
Message-ID:
        <cap1qinbhds4mn4w0zb-kzpuco6caz412gquk-meksidxja-...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

Cheers, Rein. This is new for me. I tried to make it work.

BTW the monad instance is not used. Commenting it out has no effect on
running main.

How can I apply Val (not IO) Monad instance in this example? Could you
suggest a simple change?


------------------------------

Message: 4
Date: Thu, 16 Jul 2015 22:54:36 +0200
From: Imants Cekusins <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] basic Functor, Applicative and Monad
        instances
Message-ID:
        <CAP1qinakgQRGuZe=jau0fpqptj7hfvp39uusc7mevedaexu...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

v2: Val monad is now necessary:

main::Int -> Val Weather
main i = do                         -- do:   Val as monad
   v1 <- return $ Val' i       -- pure: applicative
   v2 <- return $ over20 <$> v1     -- <$> : functor
   v3 <- Val' weather <*> v2  -- <*> : applicative
   return v3


------------------------------

Message: 5
Date: Fri, 17 Jul 2015 09:53:01 +0200
From: Imants Cekusins <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] basic Functor, Applicative and Monad
        instances
Message-ID:
        <CAP1qinZRq=ysfn7qa+xnp+s5pkndtlegt32xfjraid-hlko...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

based on this snippet and Rein's comment, here is monad file template
for intellij Idea to make new monads a quick exercise:

module ${PACKAGE_NAME}.${NAME} where


data ${Type} a = ${ctor} {
    ${prop}::a
}


instance Functor ${Type} where
-- (a -> b) -> f a -> f b
--   fmap f (${ctor} x) = ${ctor} (f x)
   fmap ab fa = let  a1 = ${prop} fa
                     b1 = ab a1
                in fa { ${prop} = b1 }


instance Applicative ${Type} where
-- a -> f a
--  pure = ${ctor}
   pure a =  ${ctor} { ${prop} = a }

-- f (a -> b) -> f a -> f b
--   ${ctor} f <*> ${ctor} x = ${ctor} (f x)
   (<*>) fab fa =  let  ab1 = ${prop} fab
                        a1 = ${prop} fa
                        b1 = ab1 a1
                   in fa { ${prop} = b1 }


instance Monad ${Type} where
-- a -> m a
   return a = ${ctor} { ${prop} = a }

--  m a -> (a -> m b) -> m b
   (>>=) ma amb = let a1 = ${prop} ma
                 in amb a1


------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 85, Issue 10
*****************************************

Reply via email to