Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there examples of functors in Haskell which fails to be monads because we cannot implement `return`?

Tags:

haskell

Are there any examples of functors in Haskell which fail to be monads because we cannot implement return?

I have seen this answer and am inspired by it.

Intuitively it seems that we can always implement return by using the type constructor. But I must be missing something.

like image 326
Eli Rose Avatar asked Oct 19 '19 20:10

Eli Rose


2 Answers

This is exactly what the Bind typeclass represents: things that have a bind operation, but not necessarily return. Here's some types that are instances of Bind, but aren't instances of Monad because they don't have return:

  • (,) w,WriterT w m, and RWST r w s m, whenever w is a Semigroup but not a Monoid
  • Map k, HashMap k, and IntMap
  • V1 (note: this is isomorphic to Daniel Wagner's Whoops and pigworker's Dead)
like image 184
Joseph Sible-Reinstate Monica Avatar answered Sep 25 '22 19:09

Joseph Sible-Reinstate Monica


I guess if there's no constructor, we can't call one:

{-# LANGUAGE EmptyCase #-}
data Whoops a
instance Functor Whoops where fmap f v = case v of

EDIT In fact, this is mentioned at the linked question: have a search for the Dead type that pigworker uses to show how something can be a functor but not applicative.

like image 38
Daniel Wagner Avatar answered Sep 26 '22 19:09

Daniel Wagner