Specializing bind for monads over special typeclasses in Haskell
By : ChooChooImATrain
Date : March 29 2020, 07:55 AM
hope this fix your issue In the second last chapter For a Few Monads More of the very nice tutorial "Learn You a Haskell for a Great Good" the author defines the following monad: , This is called a "restricted monad" and you define it like this: code :
{# LANGUAGE ConstraintKinds, TypeFamilies, KindSignatures, FlexibleContexts, UndecidableInstances #}
module Control.Restricted (RFunctor(..),
RApplicative(..),
RMonad(..),
RMonadPlus(..),) where
import Prelude hiding (Functor(..), Monad(..))
import Data.Foldable (Foldable(foldMap))
import GHC.Exts (Constraint)
class RFunctor f where
type Restriction f a :: Constraint
fmap :: (Restriction f a, Restriction f b) => (a > b) > f a > f b
class (RFunctor f) => RApplicative f where
pure :: (Restriction f a) => a > f a
(<*>) :: (Restriction f a, Restriction f b) => f (a > b) > f a > f b
class (RApplicative m) => RMonad m where
(>>=) :: (Restriction m a, Restriction m b) => m a > (a > m b) > m b
(>>) :: (Restriction m a, Restriction m b) => m a > m b > m b
a >> b = a >>= \_ > b
join :: (Restriction m a, Restriction m (m a)) => m (m a) > m a
join a = a >>= id
fail :: (Restriction m a) => String > m a
fail = error
return :: (RMonad m, Restriction m a) => a > m a
return = pure
class (RMonad m) => RMonadPlus m where
mplus :: (Restriction m a) => m a > m a > m a
mzero :: (Restriction m a) => m a
msum :: (Restriction m a, Foldable t) => t (m a) > m a
msum t = getRMonadPlusMonoid $ foldMap RMonadPlusMonoid t
data RMonadPlusMonoid m a = RMonadPlusMonoid { getRMonadPlusMonoid :: m a }
instance (RMonadPlus m, Restriction m a) => Monoid (RMonadPlusMonoid m a) where
mappend (RMonadPlusMonoid x) (RMonadPlusMonoid y) = RMonadPlusMonoid $ mplus x y
mempty = RMonadPlusMonoid mzero
mconcat t = RMonadPlusMonoid . msum $ map getRMonadPlusMonoid t
guard :: (RMonadPlus m, Restriction m a) => Bool > m ()
guard p = if p then return () else mzero
{# LANGUAGE ConstraintKinds, TypeFamilies, RebindableSyntax #}
module { module line } where
import Prelude hiding (Functor(..), Monad(..))
import Control.Restricted

What is so special about Monads?
By : Walker
Date : March 29 2020, 07:55 AM
With these it helps I suspect that the disproportionately large attention given to this one particular type class (Monad) over the many others is mainly a historical fluke. People often associate IO with Monad, although the two are independently useful ideas ( as are list reversal and bananas). Because IO is magical (having an implementation but no denotation) and Monad is often associated with IO, it's easy to fall into magical thinking about Monad. (Aside: it's questionable whether IO even is a monad. Do the monad laws hold? What do the laws even mean for IO, i.e., what does equality mean? Note the problematic association with the state monad.)

How to use Kleisli arrows with monads?
By : Kevin Blondel
Date : March 29 2020, 07:55 AM
this one helps. datedFiles can be implemented using arrows because the information flows in a "fixed pipeline", as your diagram shows. Here's a possible implementation that does not use map or zip on lists: code :
import System.Directory
import System.FilePath
import Control.Monad.List
import Control.Arrow
datedFiles :: FilePath > IO [(FilePath,FilePath)]
datedFiles = fmap runListT . runKleisli $
(Kleisli $ ListT . getDirectoryContents)
>>>
returnA &&& ((Kleisli $ liftIO . getModificationTime) >>^ show)
>>^
fst &&& (\(path,time) > replaceBaseName path $ takeBaseName path ++ time)
import Control.Lens
datedFiles :: FilePath > IO [(FilePath,FilePath)]
datedFiles = fmap runListT . runKleisli $
ListT . getDirectoryContents ^. wrapped
>>>
returnA &&& (liftIO . getModificationTime ^. wrapped >>^ show)
>>^
fst &&& (\(path,time) > replaceBaseName path $ takeBaseName path ++ time)

Cannot find Bind instances for Free Monads over Coyoneda when composing functions via Kleisli arrows in scalaz
By : jokaRichardson
Date : March 29 2020, 07:55 AM
it should still fix some issue Thank you Travis for your help. Bad type declaration was actually one of the culprits. With some help from the scalaz community via google groups and some tinkering here is the answer: code :
class BigBrother[F[_]](implicit I: Inject[Sensor,F]) extends AppCompose {
import BigBrother._
def log(log: String) = lift(Log(log))
def filter(log: String) = lift(Filter(log))
def secure(log: String) = lift(Secure(log))
def filterAndSecure(phrase: String) = for {
f < filter(phrase)
s < secure(f)
} yield s
type CoyoF[A] = Coyoneda[F, A]
type FreeCoF[A] = Free[CoyoF,A]
implicit val MonadFreeSensor = Free.freeMonad[FreeCoF]
// kleisli composition attempt  alternative to filterAndSecure
val fk = kleisli[FreeCoF, String, String](filter _)
val sk = kleisli[FreeCoF, String, String](secure _)
val fAndS = fk >=> sk
}

How to combine Kleisli[M, A, C] and Kleisli[M, B, C]
By : user5116242
Date : March 29 2020, 07:55 AM
this one helps. You'll need to come up with some way to combine the input types into a single type. One way to do this would be inheritance—you'd have a type UserRepo with ProjectRepo that is a subclass of both UserRepo and ProjectRepo. Another way would be composition, where you have a tuple (UserRepo, ProjectRepo). In both cases you'd generally use local to "expand" the input types of each arrow so that you can compose them in the forcomprehension: code :
for {
user < findByUserId.local[(UserRepo, ProjectRepo)](_._1)
project < findProjectById.local[(UserRepo, ProjectRepo)](_._2)
} yield (user, project)

