Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is join standalone, instead of part of the minimal implementation of the Monad typeclass?

Tags:

haskell

I would prefer defining an instance of Monad via the join function instead of >>= ... Starting from that equivalence:

x >>= f = join (fmap f x)

Could you define a Monad instance only with join?

I would have expected join in Monad with: {-# MINIMAL (>>=)| join #-}

Why is join at the top level and not even in the Monad typeclass?

like image 475
Nicolas Henin Avatar asked Jul 30 '18 15:07

Nicolas Henin


2 Answers

Sadly, join is not a part of the Monad typeclass in GHC’s standard library because of technical restrictions related to generalized newtype deriving and the roles system. Long story short, given some newtype newtype T m a = MkT (m a), GHC is not smart enough to figure out how to prove representational equality between m (m a) and m (T m a), which is necessary for proving representational equality for the first argument of join (which has type m (m a) -> m a).

Fortunately, a recent extension to GHC Haskell, QuantifiedConstraints, might make it possible to make the roles system smart enough to support this. For a more detailed treatment of both the problem and its potential solution, see Ryan Scott’s blog post, How QuantifiedConstraints can let us put join back in Monad.

like image 163
Alexis King Avatar answered Nov 13 '22 03:11

Alexis King


join is not in Monad because it would break GeneralizedNewtypeDeriving in a subtle way for newtype-wrapped monad transformers, a very common use case. See the GHC wiki for details.

like image 31
Daniel Wagner Avatar answered Nov 13 '22 03:11

Daniel Wagner