Questions tagged [monads]

A monad in programming is a composable computation description. Monads are an important construct in functional programming languages like Haskell.

Filter by
Sorted by
Tagged with
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....
Amaterasu's user avatar
  • 307
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 ->...
F. Zer's user avatar
  • 1,113
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 ...
Rhohen's user avatar
  • 3
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 <++ ?
Piskator's user avatar
  • 649
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] ?
confusedandsad's user avatar
-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 ...
TheSinisterStone's user avatar
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 ...
liminor's user avatar
  • 171
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 ...
Piskator's user avatar
  • 649
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 ++ " ...
Mafaldo Reliedo's user avatar
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 ...
Davus's user avatar
  • 3
-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&...
toowren's user avatar
  • 83
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 ...
thor's user avatar
  • 21.7k
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 ...
scobiem's user avatar
  • 21
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 ...
stone_guru's user avatar
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 ...
Patrick Bucher's user avatar
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 ...
Julia's user avatar
  • 175
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); ...
Piskator's user avatar
  • 649
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 ...
Piskator's user avatar
  • 649
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, ...
jimnwq's user avatar
  • 60
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....
Davi Barreira's user avatar
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 ...
Piskator's user avatar
  • 649
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 ...
Dannyu NDos's user avatar
  • 2,478
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 ...
Mon's user avatar
  • 73
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 ...
Piskator's user avatar
  • 649
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 ...
Piskator's user avatar
  • 649
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 ...
Clinton's user avatar
  • 22.5k
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 ...
Piskator's user avatar
  • 649
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 ...
Piskator's user avatar
  • 649
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 ...
Piskator's user avatar
  • 649
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 ...
Terry BRUNIER's user avatar
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....
Jay Lee's user avatar
  • 1,724
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 ...
Piskator's user avatar
  • 649
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 ...
mattdf's user avatar
  • 11
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[...
shybovycha's user avatar
  • 11.7k
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/...
SmoothTraderKen's user avatar
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' ...
C5m7b4's user avatar
  • 108
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 :: ...
Yugumo's user avatar
  • 13
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 ...
Николай Николай's user avatar
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&...
qqq's user avatar
  • 1,390
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 : '...
v_head's user avatar
  • 739
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 ...
dennismonsewicz's user avatar
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 ...
kwonryul's user avatar
  • 503
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 ...
kwonryul's user avatar
  • 503
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 => ......
Lukas Tycho's user avatar
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 "...
12412316's user avatar
  • 747
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 ...
kwonryul's user avatar
  • 503
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 ...
kwonryul's user avatar
  • 503
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 ...
nullptr's user avatar
  • 321
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 ...
bbgghh's user avatar
  • 99
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 ...
joel's user avatar
  • 6,738

1
2 3 4 5
70