Questions tagged [monads]
A monad in programming is a composable computation description. Monads are an important construct in functional programming languages like Haskell.
monads
3,515
questions
15
votes
7
answers
1k
views
How can I write a std::apply on a std::expected?
In C++23, given:
expected<A, string> getA(const X& x);
expected<B, string> getB(const Y& y);
C compute_all(const A& a, const B& b);
Is there a way to avoid a classic ...
1
vote
2
answers
85
views
(seemingly) equivalent transformations of my Haskell code not working
I try to learn Haskell and struggle with converting do-syntax into monads.
So, I boiled it down to a tiny example.
I got this:
work :: String
work = do
let arr = ["Get", "this",...
1
vote
2
answers
76
views
How to Write a Monad in Haskell to Track and Prefetch i18n Keys Efficiently?
I'm trying to write a Monad that tracks which i18n keys are being used by a piece of code. The idea is to pre-fetch values of all those keys in an efficient manner beforehand, rather than end-up with ...
1
vote
0
answers
42
views
Why LiftingAccum for MonadAccum but no auto lifting?
In the design of Control.Monad.Accum, I noticed that for a new Monad transformer t, one typically has to define the instance via LiftingAccum. However, I believe it would be more straightforward to ...
0
votes
2
answers
77
views
Confused output of `traceM` in recursive call on function monad
I am new to Haskell and learning how to use haskell writing a compiler. I rewrote those code in "Monadic Parser Combinators" using a monad syntax like below. Everything seemed working well ...
6
votes
4
answers
127
views
Trouble understanding Haskell function
I'm having trouble understanding why the following code correctly compiles:
f :: a -> Maybe a
f = return Just 3
return Just has a type of Monad m => m (a -> Maybe a) so I'm not sure why ...
4
votes
2
answers
88
views
Can I avoid the Monad constraint in this Alternative based function?
I've written the following function (using the these library):
import Control.Applicative (Alternative ((<|>)), optional)
import Data.These (These(..))
asumThese :: (Alternative f, Monad f) =&...
1
vote
1
answer
97
views
How to deal with Monad, Functor and Applicative in order to write stateful code
I am attempting to write some stateful code in Haskell. To this end, I follow this material. At this point, I got my way to monads and functors, and, roughly put, I am confused and can't make progress ...
-1
votes
1
answer
75
views
Haskell HashTable from ST monad
I am trying to work with Haskell St monad, but, unfortunatly, do not understand, how to combine pure functions and this monad. Here are example of program that need to count individuals words in text, ...
3
votes
1
answer
163
views
How to define free monads and cofree comonads in Lean4?
in Haskell we can define both of these in this way:
data Free (f :: Type -> Type) (a :: Type) = Pure a | Free (f (Free f a))
data Cofree (f :: Type -> Type) (a :: Type) = Cofree a (f (Cofree f a)...
3
votes
1
answer
94
views
Why does `let fmap f = id >=> (Ok << f)` work?
Asked How to implement map using the fish (>=>, Kleisli composition) operator in F#? a couple of hours ago, and 'kaefer's answer blew my mind:
let fmap f = id >=> (Ok << f)
It is ...
1
vote
2
answers
109
views
How to implement `map` using the fish (>=>, Kleisli composition) operator in F#?
I'm learning monadic composition through Scott Wlaschin's Railway-oriented Programming post. Oncebind, switch, and >=> functions are defined, he introduces map to show how to "turn a one-...
2
votes
1
answer
99
views
Is `Pair` a valid instance of `MonadRec`?
In the paper Stack Safety for Free, Phil Freeman defines the MonadRec type class as follows.
class (Monad m) <= MonadRec m where
tailRecM :: forall a b. (a -> m (Either a b)) -> a -> m b
...
5
votes
2
answers
166
views
Collecting errors (instead of short-circuiting) until value is actually being used
What's the simplest yet most elegant way to NOT short-circuit and instead collect errors until their values are used?
What's so hard in accumulating errors? Short circuit only if a function call ...
2
votes
2
answers
82
views
Monad Map with two values in Java Functional Programming
Forgive me if this is a basic question in FP. Let's consider Optional monad. I know I can use map to transform an Optional to another Optional based on a function (which will be called if optional has ...