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.
761
questions
-1
votes
1
answer
97
views
State Monad - HASKELL
instance Monad ST where
--return :: a -> ST a
return x = S (\s -> (x,s))
--(>>=) :: ST a -> (a -> ST b) -> ST b
st >>= f = S (\s -> let (x, s') = app st s
...
-1
votes
2
answers
79
views
Monad transformer loop failing when the short circuit happens on the first run
I have a RWST in haskell over a Maybe, which short circuits under certain conditions. I want to run this action until it returns Nothing, and get the last Just. So my situation is as follows;
type St =...
1
vote
1
answer
94
views
Understanding the RWST in Haskell
I have looked at this to try to understand how several transformer monads interact, and especially getting a better understanding of lift and stacking with monads.
For the RWST monad found here (I ...
0
votes
1
answer
74
views
How can I use LINQ query syntax with language-ext BindT monad transformer?
I want to process a set of IEnumerable<Either<Error, TA>> originalListOfEithers
such that I can keep/extract data from the type TA objects if they are not in the error (left) state but ...
3
votes
2
answers
116
views
Any difference between `Reader a (b -> c)` and `b -> Reader a c`?
(Some other mtl monads would bring up the same problem, but let's just use Reader here to exemplify)
Say I need to show something in a human-readable way, with the implementation and behaviour details ...
1
vote
1
answer
95
views
Applying changes to outer Monads in Haskell without using Transformer monads
I am trying to do something like the following where I want to return an error message in case the state of a State-Monad isn't fulfilling a certain condition, can it be done without using lift and ...
0
votes
1
answer
59
views
Transformer handling null values
user id | event | date | amount | duration(min) |
124556. login 02/23. N/A. 23.
124556. withdrawal 02/28. 500 2.
124556. login 03/02. N/A. 12.
124556. ...
2
votes
2
answers
58
views
EitherT not properly upcasting to common trait super type (Scala 3)
When chaining a for comprehension with EitherT, the compiler is having issues upcasting my errors to their common ancestor type.
I have the following type definitions in my code:
sealed trait Error
...
1
vote
2
answers
270
views
Hugging Face model deployment
My question is related to how one deploys the Hugging Face model. I recently downloaded the Falcon 7B Instruct model and ran it in my Colab. However, when I am trying to load the model and want it to ...
2
votes
1
answer
78
views
What is the pattern to combine 2 MonadState instances into the same monad?
Say that I'm implementing a TicTacToe game server. In order to better test it I define a class for the effect of getting a move from a player:
class (Monad m, MonadState TTTState m) => TTTMonad m ...
3
votes
2
answers
75
views
MonadReader without functional dependency
Is there a version of MonadReader in a library somewhere that doesn't have the functional dependency from m -> r?
If I have a function that requires reader contexts A, B and C, and I want to call a ...
0
votes
1
answer
115
views
Using monad transformers in OCaml
I'm getting myself familiar with monad transformers in OCaml using monads library.
Here is an example I'm working with:
open Base
open Stdio
open Monads.Std
module St = struct
include Monad.State....
0
votes
2
answers
169
views
Running Monad multiple times in haskell and updating state
Hi for the following code I have tried to run the RegModule monad such that one state will effect the next, as seen in the output at the end of the code (MVE) below the state of the monad doesn't seem ...
2
votes
2
answers
118
views
How to use `runAccumT`
I am trying to replace StateT with AccumT in my program but I can't figure out exactly how it works. Specifically, I don't understand why runAccumT and its derivatives seem to ignore their second ...
2
votes
2
answers
86
views
How to model a mutable key/value map in a monad transformer stack?
This is a bit of a design question.
I currently have an "App" class which is a monad transformer stack, and what I want to add to that stack is a mutable Key/Value store.
Exactly how that ...
1
vote
2
answers
111
views
Match the context of MaybeT's Monad and Applicative: I actually found a counter-example
I wonder if Applicative is derived naturally through MonadTransformer's Monad
I had a hard time finding out if MaybeT's Monad and Applicative have the same context. As you can see in the link ...
0
votes
0
answers
72
views
I wonder if Applicative is derived naturally through MonadTransformer's Monad
For the type constraint's point of view, Monad be derived from Applicative, which is the reverse of what I wrote in the title.
However, it's not a mistake.
While studying Monad, I have checked all the ...
1
vote
1
answer
51
views
How C++ lambda move capture *this in rvalue reference member function?
we can move capture variable in c++ as follow.
string s;
auto f = [s = std::move(s)](){ /* do something */ };
we can copy capture this as follow.
auto f = [*this](){ /* do something */ };
but how ...
4
votes
1
answer
172
views
Is this a generic transformer for any monad?
There have been claims that the IO monad has no monad transformer, or even cannot have a monad transformer. Why is there no IO transformer in Haskell?
First question: Then, what is this code? https://...
1
vote
2
answers
116
views
Why isn't IO an instantiation of State?
Why isn't IO an instantiation of the (strict) State monad when we have RealWorld, as provided in Control.Monad.ST? I thought RealWorld were meant to be a magical type representing the reality itself.
...
3
votes
1
answer
98
views
Composing monad transformer stacks
Suppose I have two Haskell libraries, each with one type for computations, LibA and LibB. They are both monads stacked on top of the IO monad, implemented with the ReaderT monad transformer:
import ...
1
vote
1
answer
89
views
How to refactor code handling IO (Maybe a) to use monad transformer?
I'm trying to understand monad transformers. I have a code like this (that doesn't work):
import System.IO (hFlush, stdout)
import Text.Read (readMaybe)
add1 :: Int -> IO (Maybe Int)
add1 x = ...
1
vote
2
answers
72
views
Monad Transformer missing parameter (?)
I've got this type alias:
type Board a = ReaderT String (StateT String IO) a
I know that StateT has the kind * -> (* -> *) -> * -> * so it should get three parameters. But in the above ...
3
votes
2
answers
83
views
How to test Monad instance for custom StateT?
I'm learning Monad Transformers, and one of the exercises asks to implement the Monad instance for StateT.
I want to test that my implementation admits to the Monad laws using the validity package, ...
0
votes
1
answer
119
views
Creating monad transformer for Future and List
There's an asynchronous client that returns Futures of Lists. To iterate over results I need to use nested map and flatMap. Is there a way to create a monad transformer to be able to use simpler for-...
0
votes
0
answers
89
views
What are algebraic effects, exactly? [duplicate]
There is this concept of "Algebraic Effects" sometimes coming with "handlers", sometimes not, but for the life of me I can't find a clear introduction or even a definition.
What ...
0
votes
1
answer
75
views
How do you lift a binary function to monad transformers?
I know you can lift functions to monads with liftM, but I it doesn't seem to work with binary functions.
I tried to lift (+) but it didn't work
a = return 1 :: ExceptT String Maybe Int
b = return 2 :: ...
2
votes
2
answers
102
views
Refactor impure recursion with state monad?
I've been dissecting this one-liner solution for aoc day 14 and came across an elegant impure recursive solution:
def s(x,y):
if y > h: return True
if (x, y) in m: return False
return ...
1
vote
1
answer
114
views
Implementing MonadError through ExceptT
Can one reasonably implement MonadError threw a newtype wrapping ExceptT? So far I have:
newtype BlahT e m a = BlahT (ExceptT e m a) deriving newtype (Functor, Applicative, Monad)
instance MonadError ...
1
vote
2
answers
74
views
Combining StateT and ExceptT monad tranformers
I have the following little working program:
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Monad.State.Strict
import Control.Monad.Except
...
11
votes
3
answers
2k
views
What's the difference between C++23's optional::transform and optional::and_then?
C++23 adds some "monadic-style" functionality regarding optionals, as methods of optional<T>:
optional<T>::and_then() (and ignoring qualifiers of this):
template<class F> ...
1
vote
1
answer
143
views
Timeout on StateT with IO
I have a custom type
type GI a = StateT GenState IO a
where GenState is a state I keep for Generating Random Trees of some kind.
When generating my trees, termination is not guaranteed in a ...
3
votes
1
answer
169
views
What is this concept called in Haskell?
I'm writing some relatively functional style code in C++, and am trying to avoid inventing a bad name for it. To that end I'm trying to stick to the names of isomorphic concepts in Haskell where ...
2
votes
1
answer
138
views
Monad Transformers in an Interpeter
I am encountering a problem with Monad Transformers, but I think it's helpful to include some context of how I got to the state I'm currently in, so I'll start with a rough explanation of my program:
...
6
votes
1
answer
109
views
Is there a class with the function `(m a -> n b) -> t m a -> t n b`
Here's a class, I'm calling BlahMap:
class BlahMap t where
blahMap :: (m a -> n b) -> t m a -> t n b
This is an instance of BlahMap:
instance BlahMap (ReaderT r) where
blahMap f = ...
6
votes
1
answer
322
views
How to use ST Monad with monad transformers
The haskell transformers library provides MonadIO class and liftIO to lift IO operations in a monad transformer stack. It seems to me that the same could be done for the ST monad, but I couldn't find ...
2
votes
1
answer
116
views
Getting State environment in a monadic context in an interpreter
I wanna get used to working monad transformers so I thought I'd write a simple interpreter using ExceptT and State, where my state is Map Ident Int, Ident being an alias for Strings.
My syntax ...
2
votes
2
answers
174
views
Where is `EitherT`'s Applicative instance defined?
I don't see it here by the definition of EitherT, nor here by the definition of Applicative.
Shouldn't it be defined within the type module or within the typeclass module, to avoid being orphaned? ...
0
votes
1
answer
157
views
StateT for Configuration File
I am trying to read the Configuration file sections using StateT monad. But having some trouble. Can anyone please tell me how to do this?
readSections :: StateT FilePath IO [T.Text]
readSections = ...
0
votes
1
answer
197
views
Is there ReaderT raised into a monad?
I've written the following monad transformers (which I believe are equivalent to each other), namely:
newtype MonadReaderT1 r m a = MonadReaderT (ReaderT (r m) m a)
newtype MonadReaderT2 r m a = ...
2
votes
1
answer
207
views
How to define a simple KVStore effect 'mtl-style'?
I have experimented a little with the polysemy library and enjoyed working with the KVStore k v, which is simple abstraction of a Key-Value-Store. Now I was wondering how I would define a similar ...
2
votes
0
answers
119
views
How to Group and Split Monad Transformers using Identity
I am currently learning about Monad Transformers and noticed that the MonadTrans class that the library I am using (Mageparsec) implements, does not define any way to group and split apart the ...
4
votes
1
answer
217
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
66
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
3
answers
337
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
114
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
436
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
132
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
119
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
161
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 ....