[Haskell-cafe] Monad of no `return` Proposal (MRP): Moving `return` out of `Monad`

Tony Morris tonymorris at gmail.com
Tue Oct 6 01:52:22 UTC 2015

Hash: SHA256

- -- I am going to do some logging, yay!
data Logs a =
  Logs a [a]
  deriving (Eq, Show)

- -- one log message
singlelog ::
  -> Logs a
singlelog a =
  Logs a []

- -- two log messages
twologs ::
  -> a
  -> Logs a
twologs a1 a2 =
  Logs a1 [a2]

class Semigroup a where
  (<>) ::
    -> a
    -> a

- -- I can append logs
instance Semigroup (Logs a) where
  Logs h1 t1 <> Logs h2 t2 =
    Logs h1 (t1 ++ h2 : t2)

- -- I can map on Logs
instance Functor Logs where
  fmap f (Logs h t) =
    Logs (f h) (fmap f t)

- -- I will collect logs with a value
data WriteLogs l a =
  WriteLogs (Logs l) a
  deriving (Eq, Show)

- -- I can map the pair of logs and a value
instance Functor (WriteLogs l) where
  fmap f (WriteLogs l a) =
    WriteLogs l (f a)

singlewritelog ::
  -> a
  -> WriteLogs l a
singlewritelog l a =
  WriteLogs (singlelog l) a

- -- Monad without return
class Bind f where
  (-<<) ::
    (a -> f b)
    -> f a
    -> f b

- -- Can I Applicativate WriteLogs? Let's see.
instance Applicative (WriteLogs l) where
  -- Well that was easy.
  WriteLogs l1 f <*> WriteLogs l2 a =
    WriteLogs (l1 <> l2) (f a)
  pure a =
    WriteLogs (error "wait, what goes here?") a
  -- Oh I guess I cannot Applicativate WriteLogs, but I can Apply them!

- -- Well there goes that idea.
- -- instance Monad (WriteLogs l) where

- -- Wait a minute, can I bind WriteLogs?
instance Bind (WriteLogs l) where
  -- Of course I can!
  f -<< WriteLogs l1 a =
    let WriteLogs l2 b = f a
    in WriteLogs (l1 <> l2) b

- -- OK here goes ...
myprogram ::
  WriteLogs String Int
myprogram =
  -- No instance for (Monad (WriteLogs String))
  -- RAR!, why does do-notation require extraneous constraints?!
  -- Oh that's right, Haskell is broken.
  -- Oh well, I guess I need to leave Prelude turned off and rewrite
the base libraries.
  do a <- singlewritelog "message" 18
     b <- WriteLogs (twologs "hi" "bye") 73
     WriteLogs (singlelog "exit") (a * b)

- -- One day, one day soon, I can move on.

On 06/10/15 11:20, amindfv at gmail.com wrote:
> IMO, the "tech debt" you're talking about feels very small. We've
> already made the change that return = pure by default. The
> historical baggage that this proposal cleans up is just the fact
> that legacy code is able to define its own "return" without
> breaking (which must be the same as the definition of pure
> anyway). I am also moving from +0.5 to +0 on this.
> Tom
>> El 5 oct 2015, a las 18:29, Alexander Berntsen
>> <alexander at plaimi.net> escribió:
>>>> On 05/10/15 20:50, Nathan Bouscal wrote: There have been a
>>>> lot of objections based on the idea that learners will
>>>> consult books that are out of date, but the number of 
>>>> learners affected by this is dwarfed by the number of
>>>> learners who will use updated materials and be confused by
>>>> this strange historical artifact. Permanently-enshrined
>>>> historical artifacts accrete forever and cause linear
>>>> confusion, whereas outdated materials are inevitably replaced
>>>> such that the amount of confusion remains constant.
> Thank you for making this point
> I would be very saddened if the appeal to history (i.e. technical 
> debt) would halt our momentum. That's what happens to most things
> both in and out of computer science. And it's honestly depressing.
>> _______________________________________________ Haskell-Cafe
>> mailing list Haskell-Cafe at haskell.org 
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> _______________________________________________ Haskell-Cafe
> mailing list Haskell-Cafe at haskell.org 
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Version: GnuPG v2


More information about the Haskell-Cafe mailing list