public
Created

XDefaultSignatures

  • Download Gist
1.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
Prelude Control.Monad> :set -XDefaultSignatures
 
Prelude Control.Monad>
newtype Box a = Box a deriving Show
 
Prelude Control.Monad>
instance Monad Box where
return = Box
(Box x) >>= f = f x
Prelude Control.Monad> join (Box (Box 2))
Box 2
Prelude Control.Monad>
class DFunctor f where
dmap :: (a -> b) -> f a -> f b
default dmap :: Monad f => (a -> b) -> f a -> f b
dmap f = ((return . f) =<<)
Prelude Control.Monad>
instance DFunctor Box
 
Prelude Control.Monad> dmap (+1) $ Box 2
Box 3
Prelude Control.Monad>
class DApplicative f where
dpure :: a -> f a
default dpure :: Monad f => a -> f a
dpure = return
dap :: f (a -> b) -> f a -> f b
default dap :: Monad f => f (a -> b) -> f a -> f b
dap = ap
Prelude Control.Monad>
instance DApplicative Box
 
Prelude Control.Monad> dpure (+) `dap` Box 2 `dap` Box 3
Box 5
Prelude Control.Monad>
newtype Xob a = Xob a deriving Show
 
Prelude Control.Monad>
instance DFunctor Xob where
dmap f (Xob a) = Xob . f $ a
Prelude Control.Monad> dmap (+1) . Xob $ 41
Xob 42
2.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
Prelude>
class DMonad m where
djoin :: m(m a) -> m a
djoin = dbind id
dbind :: (a -> m b) -> m a -> m b
default dbind :: DFunctor m => (a -> m b) -> m a -> m b
dbind f = djoin . dmap f
dreturn :: a -> m a
default dreturn :: DApplicative m => a -> m a
dreturn = dpure
class DFunctor f where
dmap :: (a -> b) -> f a -> f b
default dmap :: DMonad f => (a -> b) -> f a -> f b
dmap f = dbind (dreturn . f)
class DApplicative f where
dpure :: a -> f a
default dpure :: DMonad f => a -> f a
dpure = dreturn
dap :: f (a -> b) -> f a -> f b
default dap :: DMonad f => f (a -> b) -> f a -> f b
dap = undefined
Prelude> instance DApplicative [] where
dpure x = [x]
dap = Control.Monad.ap
Prelude> instance DFunctor [] where
dmap = map
Prelude> instance DMonad [] where
djoin = concat
Prelude> dbind (\x -> [x,x]) [1..3]
[1,1,2,2,3,3]
Prelude> dbind dreturn [1..3]
[1,2,3]
3.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Prelude Control.Monad Data.Traversable Data.Foldable Control.Applicative>
class DTFunctor f where
tmap :: (a -> b) -> f a -> f b
default tmap :: Monad f => (a -> b) -> f a -> f b
tmap f = ((return . f) =<<)
default tmap :: Traversable f => (a -> b) -> f a -> f b
tmap = fmapDefault
 
<interactive>:102:5:
Duplicate default type signature:
<interactive>:102:5-53: default tmap ::
Monad f => (a -> b) -> f a -> f b
<interactive>:104:5-59: default tmap ::
Traversable f => (a -> b) -> f a -> f b
 
<interactive>:103:5:
Conflicting definitions for `tmap'
Bound at: <interactive>:103:5-8
<interactive>:105:5-8

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.