Questions tagged [monads]
A monad in programming is a composable computation description. Monads are an important construct in functional programming languages like Haskell.
3,492
questions
0
votes
0
answers
49
views
Monads not useful in strict languages?
So Steven F. Lott in his book "Functional Python Programming" (start of chapter 13) writes:
A monad allows us to impose an order on an expression evaluation in an otherwise lenient language....
1
vote
1
answer
48
views
How can I use `liftIO` with State to print values inside that Monad?
I am trying to print inside the State monad by using liftIO function from MonadIO class:
import Control.Monad.State
import Control.Monad.IO.Class
import Control.Applicative
facHelper :: Integer ->...
0
votes
1
answer
72
views
OCaml Addable Numbers
I'm learning OCaml, from a little bit of Haskell that I studied for my thesis, and I'm wondering if there is a similar mechanism (type classes) to expresses that two numbers are addable (like a ...
2
votes
1
answer
88
views
What does <* in Haskell readP do?
I have encountered <* in the Haskell readP library on several occassions, I am however not yet sure what it does, and couldn't find it in the library documentation. Is it the same as <++ ?
0
votes
1
answer
93
views
Haskell map on Right value of Either
I have a type Either a [b] and a function f :: (b -> c)
How can I use f on a value of type Either a [b] to get Either a [c] ?
-1
votes
1
answer
71
views
How to draw randomly-placed rectangles in Haskell's SDL2 bindings?
I'm trying to draw randomly-positioned rectangles in SDL2 using Haskell. The IO monad is really driving me insane and I can't get it to work. I have the boilerplate SDL2 code ready to go and a custom ...
0
votes
0
answers
55
views
Monadic log function writing to file utilizing 'using' keyword
There's a new context manager on the block. with died quietly, so naturally it's time to re-brand and revive :)
Setup:
Looking at the new using keyword I immediately found a scenario where it would be ...
1
vote
1
answer
85
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 ...
3
votes
2
answers
125
views
Reader Monad in Haskell. Where is the reader passed as argument?
On this monad reader example:
import Control.Monad.Reader
tom :: Reader String String
tom = do
env <- ask -- gives you the environment which in this case is a String
return (env ++ " ...
0
votes
1
answer
92
views
Haskell interpreter, dont know how to run and test it
I am working on an interpreter as a project task, in order to understand what I have to do, I decided to download the interpreter from the Internet and run it. I started the specified folder in ...
-1
votes
2
answers
38
views
Converting java.util.Function to non-Lambda expression one in Monad example taken from Wikipedia
There is a very simple Monad example, written in Java, which is taken from Wikipedia article.
import java.util.function.Function;
interface Monad<T> {
<U> Monad<U> bind(Function&...
0
votes
2
answers
36
views
What is the equivalent of mapM and mapM_ in idris2?
I know there is the mapM function that applies a monadic action to a list and returns one monadic value containing a list. (See e.g.
What is the difference between mapM_ and mapM in Haskell?)
But I ...
2
votes
1
answer
92
views
Application of Monads
The code below provides for testing the satisfiability of a 2-SAT boolean formula (a formula containing just clauses of the form X v Y). Although other algorithms exist (strongly connected components ...
1
vote
0
answers
51
views
Can nested monad be a monad? [duplicate]
I'm trying to make two nested monad to a monad, but can not find the solution.
I can make it Functor and Applicative, but can not find a way to make it a monad
newtype Man m n a = Man { runMan :: m (n ...
1
vote
1
answer
55
views
Haskell: inline auxiliary function into do block
Working through Programming in Haskell (Second Edition) by Graham Hutton, I just managed to solve exercise 10.5 (page 138). The task is to write a function adder :: IO () that reads n numbers (to be ...
4
votes
2
answers
104
views
Defining a monad in Haskell
I am currently reading Kan Extensions for Program Optimisation and in the first page of the paper the author defines given M a monad the following monad
type C a = ∀z . (a → M z ) → M z
instance Monad ...
3
votes
2
answers
76
views
Can `guard` not guards in Haskell be used to specify a particular message for `Left`
Does the guard function from RIO-Prelude offer any way to decide the content of a message if it is failing an Either-Monad? such that e.g
somefun :: Either String Int
somefun = do guard (4+2 == 8); ...
1
vote
1
answer
94
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
2
answers
97
views
Laziness of infinite lists under monadic context in Haskell
I'm currently learning haskell for the first time, and I'm having lots of trouble understanding lazy evaluation.
Main problem is that among following scenarios, some behaves lazily and some doesn't, ...
1
vote
0
answers
76
views
Free Monads in Julia Programming
I'm learning about Free Monads and I was wondering how one could implement it in the Julia programming language. I've seen the implementation in Haskell, but I'm having a hard time porting it to Julia....
0
votes
1
answer
98
views
Extracting, manipulating and accumulating values within a list of monads in Haskell
I am trying to create a function that 'extracts' and evaluate the value of a each monad in a list of monads such that a final value - accumulated or so - can be returned. The signature for such a ...
8
votes
1
answer
194
views
What's the intuition behind fixpoints of monads NOT being the direct limit?
Consider the following datatype:
data Foo = Halt | Iter Foo
This is the fixpoint of Maybe. It contains the direct limit of the sequence Void, Maybe Void, Maybe (Maybe Void) and so on, where pure is ...
1
vote
2
answers
88
views
I want to read a file, remove duplicate lines and write (unique lines) into another file
I am new to haskell and I'm trying to work out this problem. Given 2 files, an input file and output file, I want to read from the input file, remove duplicate lines then write (unique lines) into the ...
0
votes
1
answer
143
views
Haskells bind operator and the >> operator and their relation
I have recently posted a question about the >> operator, because even though I have read the LYAH walk the linee section I still have some gaps in my understanding. Below is some code/MVE that ...
1
vote
0
answers
116
views
The do-notation in Haskell, the >> and the >>= operator and their impact on the final Monad
I am trying to understand Monads and have read and played around with concept from LYAH especially the 'walk the line section': http://learnyouahaskell.com/a-fistful-of-monads#walk-the-line in order ...
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
116
views
How to convert a generic type in Haskell into a String within a Monadic context
In the MVE code below I have tried to create a function collect which is supposed to take a RegModule-monad as argument such as the scanChar and when this scanChar or other RegModule succeeds in ...
0
votes
0
answers
116
views
What values get unwrapped from a Monad with do-notation and what determines this?
I am still a bit new to Monads and have just realized that something I thought I understood can't be the case. In the MVE that follows I have a hard time understanding why next <- scanChar can ...
1
vote
1
answer
91
views
How does return work within a Monad context within Haskell
Why does the following, given that the RegModule instance defines its return as: return a = RegModule (\_s _i d -> return (a, 0, d)), not return [((c, i+1, d), Int, d)], and why does the second ...
1
vote
2
answers
165
views
Could not find implicit value for parameter F: cats.Applicative[F] with Sync and Temporal
Basically, I just want to add a sleep somewhere, but I don't understand how to provide the implicit Applicative needed.
I'm using cats effect 3.5.
It's totally new to me.
I have been using ZIO for 2 ...
0
votes
1
answer
112
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
168
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 ...
1
vote
1
answer
96
views
Reading a string from the command line in Agda 2
How do you read string input into Agda in 2.6+?
I have been fighting to just write a program that reads a line of input and immediately spits it back. There seems to always be an issue, no matter what ...
0
votes
1
answer
53
views
Cannot resolve symbol flatMap on Monad constrainted type
I've been playing with scala cats and ended up stuck with implicits and type constraints.
Assume the following definitions:
trait Client[F[_]] {
def fetchPossiblePairs(fetcher: Uri => F[List[...
2
votes
2
answers
177
views
Monad and Functor law for Monad and Functor type class in Haskell
I am a somewhat experienced functional programmer, but I have always been confused by the statement “A monad is just a monoid in the category of endofunctors” and the actual implementation of monads/...
0
votes
0
answers
31
views
TypeScript + Property 'nothing' does not exist on type '<T>(x: T)
I'm trying to hone my Typescript monad skills, so I have created a version of the Maybe monad that technically seems to be working at this point, but Typescript is complaining that Property 'nothing' ...
1
vote
1
answer
72
views
How to define bind operator using type class?
How to define bind operator using type class?
I want to define monad instance to a type.
Below is the source code I wrote.
data Set a = Eq a => Set [a]
newtype SetMonad a = SetMonad {unMonad :: ...
3
votes
0
answers
99
views
Haskell: Is it possible to write a function with signature Monad m => (a -> m a) -> m (a -> a) [duplicate]
Is it possible to write a function with signature Monad m => (a -> m a) -> m (a -> a) ?
For example I have a function
func :: Int -> IO Int
func n = pure $ n + 1
Can I transform it ...
0
votes
1
answer
119
views
fp-ts TaskEither pipeline composed with Either pipelines
I am writing a program/pipeline using fp-ts modules Either and TaskEither where the first step is to perform a TaskEither asynchronous operation that may possibly fail ("fetch document for ID&...
1
vote
1
answer
113
views
Stream monad operation bind used both as a map and filter operation?
I want to create a stream monad with the return and bind operations
I tried the following:
module type STREAM_MONAD_SIG =
sig
type 'a stream
val return : 'a -> 'a stream
val bind : '...
0
votes
0
answers
84
views
Rails Service Object with Dry-Monad yielding early
I have a couple of service objects in my rails application where I am using Dry-Monads to influence a pattern of "success" or "failure" by using their Do Notation. However, in my ...
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 ...
0
votes
1
answer
85
views
DFS Graph Traversal using Cats-Effect
I am trying to implement Graph DFS using cats-effect.
At some point in the function you have to iterate over all neighbors and call recursively call DSF on them. [neighborSet.foreach(neighbor => ......
4
votes
1
answer
137
views
Does this monadic parser cause space leaks?
I was reading the arrow tutorial.
https://en.wikibooks.org/wiki/Haskell/Understanding_arrows#Avoiding_leaks.
And I got confused reading this part
solfege :: Parser Char String
solfege = string "...
0
votes
1
answer
99
views
Should the state of the ST not change in order to be executed by the runST?
type ST s a = ST (s -> (s, a))
runST :: (forall s. ST s a) -> a
runST (ST f) = case (f realWorld) of (_, a) -> a
If you look closely, there are many errors, but the overall structure of ...
0
votes
0
answers
52
views
Does Haskell "hackage" overlaps two different type signature with same name?
While I was exploring the library, I saw the following code.
https://hackage.haskell.org/package/text-2.0.2/docs/src/Data.Text.Internal.Lazy.Fusion.html#unstreamChunks
look at unstreamChunks.
This ...
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 ...
0
votes
1
answer
72
views
How to send an Option None data to a database in Scala 2
My aim is to send data to a table in postgre database from Scala. The problem is the null cases.
I mean there are strict data types of each columns in database and I am reading data from Kafka that ...
0
votes
0
answers
42
views
Stream type with monadic effects
Is there a standard type (especially in the stdlib) for a Stream type (by which I mean a potentially infinite, lazy sequence) which allows for effects when accessing the next element? Something like ...