Questions tagged [monad-transformers]
Monad transformers are an abstraction for combining monads. This allows you to compose different computational effects, building up precisely controlled computational environments.
717
questions
4
votes
1
answer
83
views
Confusing ReaderT definition
As an exercise I've been reimplementing some common monads and their corresponding transformers; here are some of the types I've defined:
newtype Writer w a = Writer { runWriter :: (w, a) }
...
0
votes
1
answer
45
views
Use explicit execution context with OptionT[Future, _]
I am trying to write a custom Akka SnapshotStore plugin.
I am at the point where I want to implement this method:
def loadAsync(persistenceId: String, criteria: SnapshotSelectionCriteria): Future[...
4
votes
2
answers
149
views
Is the monad transformer of a monad unique in Haskell?
There have been a couple of questions (e.g. this and this) asking whether every monad in Haskell (other than IO) has a corresponding monad transformer. Now I would like to ask a complementary question....
1
vote
1
answer
74
views
No instance for (MonadWriter [Log] IO) arising from a use of ‘tell’
Consider this toy exercise on Writer and WriterT: We need to filter a list of packets based on a predefined set of rules. We also need to log some packets based on another set of rules. Now consider ...
7
votes
1
answer
181
views
Problem with given instances writing MTL style code with Scala cats
I am trying to write some Scala code to have custom behaviour in an mtl style. For example, in order to expose the "write to DB" functionality abstracting over the specific effect I wrote my ...
1
vote
1
answer
67
views
Free monad transformer - how to implement bind?
I am trying to implement free monad transformer similar to the FreeT from haskell's "free" package, but I don't know how to write bind so that the termination checker doesn't complain. It ...
0
votes
1
answer
84
views
How can I manage to print from a monad transformer
I managed to get compiling code for the monad transformer MaybeT.
(I was following the haskell wikibook)
However, I dont seem to manage to make my code print anything
running askPassphrase on ghci, I ...
2
votes
2
answers
124
views
How do define a monad transformer that is the composition of two arbitrary monad transformers?
I want to write something similar to the following:
newtype FooT c d m a = FooT { unFooT :: (c (d m)) a }
instance (MonadTrans c, MonadTrans d) => MonadTrans (FooT c d) where
lift = FooT . lift ....
1
vote
0
answers
65
views
How to map and flatmap a composition of cats free monad and ZIO
guys.
I am currently trying to build a ZIO based application.
Question:
Is there any way to map and flatmap a composition of cats free monad and ZIO like the example below?
// Suppose we have a free ...
0
votes
1
answer
62
views
Breaking the loop
I have the following code sample:
data Smth = A | B
data MyError = MkMyError
data MyState = MkMyState
run :: [Smth] -> Either MyError (Maybe Integer)
run param =
evalState
( foldM
(...
5
votes
1
answer
74
views
Instance inductivity as constraint
I'm trying to express an idea that given
instance (MonadTrans t, MonadX m) => MonadX (t m)
it should follow, that any t1 (t2 ... (tn m)) is also MonadX as long as all tx have MonadTrans instance. ...
1
vote
0
answers
159
views
Binding a Maybe within another monad to avoid case stairway of doom?
I'm fetching a user from a database with a key I'm fetching from an api (uid). The uid is a Maybe ByteString, and so I can either case check whether it's a Maybe and then run the Redis monad, or ...
2
votes
1
answer
147
views
Couldn't match LoggingT with NoLoggingT
I have a following monad transformer stack including the monad-logger transformer and I want to be able to turn off logging by a parameter.
I can see, there is the NoLoggingT type but I don't know how ...
1
vote
0
answers
68
views
What is T in EitherT?
Reading about EitherT here.
Any idea what T in EitherT represents?
0
votes
0
answers
57
views
How does return work in monad-transformer Context?
found the below snippet for monad transformers from Monad Transformers
main3 :: IO ()
main3 = do
maybeCreds <- runMaybeT $ do
usr <- readUserName'
debugFunc usr
email <- ...
3
votes
1
answer
129
views
Monad transformer Inner structural order
I have been wondering why Monad Transformers has for example m(Maybe(m(Maybe a)) structural order and why not Maybe(m(Maybe( m a))). I have tried to implement the second one but I'm unable to complete ...
0
votes
2
answers
74
views
Recursive monad execution with cats
There is simple abstract look up service with possibility to retrieve value by key:
trait LookUp[F[_]] {
def read(key: String): F[Option[String]]
}
There is use case of this service, idea is to ...
0
votes
1
answer
231
views
How do you use the StateT Monad Transformers in Haskell?
Given a simple list
newtype SomeList = SomeList { items :: [SomeItem] }
and a helper function
newtype SomeListStateT a = SomeListStateT
{ runSomeListStateT :: StateT SomeList IO a
} deriving (...
1
vote
1
answer
111
views
Using monad transformers change expression result
I found a behavior of monad transformers that is not at all intuitive for me.
Taking following data as an example:
type F[X] = OptionT[Either[String, *], X]
val success: F[Int] = 1.pure[F]
val empty:...
1
vote
1
answer
114
views
Implementing a liftIO-Style lift for Monad-Transformers in Haskell
I am currently learning to use Monad-Transformers in Haskell. I have composed a simple
Monad-Transformer using StateT and ExceptT:
type DatabaseT m = StateT DatabaseState (ExceptT DatabaseError m)
...
2
votes
2
answers
345
views
How to use ExceptT to replace lots of IO (Either a b)
I have a function which connects to a database, then runs a query. Each of these steps results in IO (Either SomeErrorType SomeResultType).
One of the things I have really liked about using Either and ...
0
votes
1
answer
275
views
How to fix the MonadFail (Either String) Error using explicit pattern matching
I was trying to adapt an old Haskell project which is now not compatible with latest GHC (MonadFail is now default).
applyCosPass :: CosCtx a => (a -> QueryExpr -> Either String QueryExpr)
...
3
votes
1
answer
121
views
Difficulties understanding Monad Transformers
I was hoping to get some helpful input on understanding Monad Transformers, and in relation to that, what happens using do notation. The example I am trying to understand is the following:
data ...
1
vote
1
answer
55
views
Is it possible to pass and retrieve a state in a quickcheck generator
I would like to create a quickcheck generator to generate a data structure like a tree. Due to the specifics caracteristics of this structure I would like the value to be generated according to its ...
2
votes
1
answer
65
views
Is Yesod's Handler Monad an instance of MonadBaseControl IO?
I would like to use the withResource :: MonadBaseControl IO m => Pool a -> (a -> m b) -> m b from the Data.Pool library in my handler code. I am trying to determine if Handler is an ...
1
vote
2
answers
80
views
MonadError instance for monad-coroutine Coroutine for any suspension Functor
Is it possible to write a MonadError instance for a monad-coroutine Coroutine with an arbitrary suspension Functor.
i.e.
instance (MonadError e m, Functor s) => MonadError (Coroutine s m) where
...
0
votes
1
answer
101
views
Align types with ExceptT IO monad transformer
Trying to wrap my head around monad transformers, I could get some toy examples to work but here I'm struggling with a slightly more real-worldy use case. Building upon this previous question, with a ...
1
vote
0
answers
150
views
Haskell async: Using different types of Monads in withAsync
I'm writing a Monad transformer, and to run the transformer I need to spawn an IO thread that writes elements to a TQueue that the main thread has access to. However, none of the versions of withAsync ...
3
votes
1
answer
91
views
Defining a data type as MonadSample
I am trying to define a toy probabilistic programming language to test various inference algorithms and their effectiveness. I followed this tutorial to create a Scheme like language with a basic ...
4
votes
1
answer
170
views
Best practices with monad transformers : to hide or not to hide 'liftIO'
I will preface this by saying that I am a novice Haskell programmer (tinkered with it sporadically over the years) but I have a fair few years on the counter when it comes to OOO and imperative ...
4
votes
1
answer
108
views
Monad transformer representing an infinite chain of actions?
I attempted to implement a monad transformer representing an infinite chain of actions, like this:
import Control.Arrow
import Control.Monad
import Data.Functor.Classes
import Data.Functor.Identity
...
5
votes
1
answer
216
views
In composite StateT / Maybe monad, how to take either possibility that succeeds?
In Haskell, here's a monad that combines the State and Maybe monads:
type StatefulMaybe a = StateT Int Maybe a
This is a computation that can succeed (returning a value) or fail. If it succeeds, it ...
0
votes
0
answers
82
views
How can I make this monad stack reusable?
I'm writing an interpreter for a simple programming language. It has two stages: a parser and an evaluator. The parser reads a text file and creates a tree with the expressions, values, etc. It also ...
1
vote
0
answers
43
views
Does Range-v3 provide a monadic binding function? If not, how do I provide support for the pipe operator to a custom function?
Given this code,
#include <iostream>
#include <range/v3/view/join.hpp>
#include <range/v3/view/repeat.hpp>
#include <range/v3/view/take.hpp>
#include <range/v3/view/...
4
votes
0
answers
150
views
Multiple MonadReader constraints on a single method
It doesn't look like anything stops you from defining a function like this:
tmp :: (MonadReader Int m, MonadReader Bool m) => m Int
tmp = ifM ask ((+1) <$> ask) ((+2) <$> ask)
So would ...
1
vote
1
answer
99
views
Could not deduce MonadReader in ReaderT wrapping
Following and adapting this blog post, I've been trying to produce a solution which should allow testing of a function which reads env vars (using System.Environment.lookupEnv).
That way, I should be ...
5
votes
1
answer
133
views
Functor instance for datatype containing function
I have a datatype defined as
data Foo a = Foo a (a -> a)
The Foo data constructor has two parameter value and function. I need to write Monad and Monad transform instance for this.
I am trying to ...
14
votes
1
answer
206
views
How to get the “inflexible semantics of monad transformers” using extensible effects?
Consider the following example.
newtype TooBig = TooBig Int deriving Show
choose :: MonadPlus m => [a] -> m a
choose = msum . map return
ex1 :: (MonadPlus m, MonadError TooBig m) => m Int
...
4
votes
1
answer
102
views
Idiomatically return a Maybe at the end of a chain [duplicate]
I have the following code snippet, which kinda works:
launchTask :: (DeviceRepo m) => TaskSpec -> m (Maybe Task)
launchTask taskSpec@TaskSpec { taskSpecImage = image
...
0
votes
2
answers
176
views
How are state monads / monad transformers desugared inside do notation?
Example
sumArray :: Array Int -> State Int Unit
sumArray = traverse_ \n -> modify \sum -> sum + n
t1 :: Int
t1 = execState (do
sumArray [1, 2, 3]
sumArray [4, 5]
sumArray [6]) 0
-- ...
1
vote
1
answer
68
views
How to create a custom parser Monad for SimpleJSON in PureScript?
I have the following, which works up until the point I try to define readJSON':
newtype JSONWithErr a = JSONWithErr (Writer (Array Foreign.ForeignError) a)
derive newtype instance jsonWithErrApply :: ...
1
vote
2
answers
507
views
No instance for (Show (Eval Int)) arising from a use of ‘print’
I'm new to Haskell and tinkering with a few examples from Stephen Diehl's What I Wish I Knew When Learning Haskell.
I'm stuck on this monad transformer example : original code.
Even after simplifying ...
2
votes
1
answer
133
views
How to combine TaskT with the monad instance of Trampoline to get stackless async computations?
Trampoline is a monad and adds stack-safety to a monad transformer stack. It achieves this by relying on a special interpreter (monadRec), which is fed with the result of a monadic computation (...
1
vote
1
answer
62
views
How can I parameterize a monad by its monad transformer?
I'd like to write a type that I can parameterize by its monad transformer.
I've tried several things, and ended up with something along these lines:
newtype Foo t a = Foo { unFoo :: t (State St) a }
...
3
votes
1
answer
145
views
Permutation parsing with `ParsecT`?
I have a file parser that uses permute from parsec to combine various Parsers. It looks like permute only allows the Identity monad, which I'm sure cuts down on the algorithmic complexity.
Now I'm ...
1
vote
2
answers
102
views
How to use the ContT monad transformer?
The ContT monad transformer has the same implementation like the Cont monad, but I'm not able to apply it to all three Either cases
Right
Left from the current monadic action
Left from a previous ...
0
votes
1
answer
149
views
How to use the Trampoline type as the base monad of a transformer more efficiently?
I have an array transformer type that exhibits interleaved effect layers to ensure a lawful effect implementation. You can easily read the structure from the type's of operation const arrOfT = of =>...
5
votes
1
answer
231
views
Haskell Combining non-determinism with error handling
Suppose I'm creating a simple interpreter that can throw errors, e.g.
type Error = String
data Term = Con Int | Div Term Term
eval :: (MonadError Error m) => Term -> m Int
eval (Con a) = ...
0
votes
1
answer
140
views
Haskell. How to implement a custom monad transformer using GADT?
I'm trying to learn about monad transformers by implementing my own. The code
compiles if I use newType DummyT m x y z = DummyT { runDummyT :: m (Dummy x y z) }
but not if I use GADT. However I need ...
3
votes
1
answer
105
views
Why no MonadWriter instance for ParsecT?
I was writing some Haskell earlier today. Came up with something along the lines of
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
newtype Foo a = Foo { ParsecT String () (Writer DefinitelyAMonoid) a }
...