monads

Implementing Haskell's Maybe Monad in c++11

三世轮回 提交于 2019-12-02 17:11:16
I am trying to implement the Maybe monad from Haskell using the lambda functions in C++11 and templates. Here's what I have so far #include<functional> #include<iostream> using namespace std; template<typename T1> struct Maybe { T1 data; bool valid; }; template<typename T1, typename T2> Maybe<T2> operator>>=(Maybe<T1> t, std::function < Maybe<T2> (T1)> &f) { Maybe<T2> return_value; if(t.valid == false) { return_value.valid = false; return return_value; } else { return f(t.data); } } int main() { Maybe<int> x = {5, true}; Maybe<int> y = {29, false}; auto z = [](int a) -> Maybe<int> { Maybe<int>

What is so special about Monads?

一曲冷凌霜 提交于 2019-12-02 17:08:19
A monad is a mathematical structure which is heavily used in (pure) functional programming, basically Haskell. However, there are many other mathematical structures available, like for example applicative functors, strong monads, or monoids. Some have more specific, some are more generic . Yet, monads are much more popular. Why is that? One explanation I came up with, is that they are a sweet spot between genericity and specificity. This means monads capture enough assumptions about the data to apply the algorithms we typically use and the data we usually have fulfills the monadic laws.

Where can I learn advanced Haskell? [closed]

岁酱吖の 提交于 2019-12-02 16:47:45
In a comment to one of my answers , SO user sdcwc essentially pointed out that the following code: comb 0 = [[]] comb n = let rest = comb (n-1) in map ('0':) rest ++ map ('1':) rest could be replaced by: comb n = replicateM n "01" which had me completely stunned. Now I am looking for a tutorial, book or PDF that teaches these advanced concepts. I am not looking for a "what's a monad" tutorial aimed at beginners or online references explaining the type of replicateM . I want to learn how to think in monads and use them effectively, monadic "patterns" if you will. From my perspective (which

What is a monad in FP, in categorical terms?

帅比萌擦擦* 提交于 2019-12-02 16:42:43
Every time someone promises to "explain monads", my interest is piqued, only to be replaced by frustration when the alleged "explanation" is a long list of examples terminated by some off-hand remark that the "mathematical theory" behind the "esoteric ideas" is "too complicated to explain at this point". Now I'm asking for the opposite. I have a solid grasp on category theory and I'm not afraid of diagram chasing, Yoneda's lemma or derived functors (and indeed on monads and adjunctions in the categorical sense). Could someone give me a clear and concise definition of what a monad is in

Using Reader Monad for Dependency Injection

自古美人都是妖i 提交于 2019-12-02 16:42:24
I recently saw the talks Dead-Simple Dependency Injection and Dependency Injection Without the Gymnastics about DI with Monads and was impressed. I tried to apply it on a simple problem, but failed as soon as it got non-trivial. I really would like to see a running version of dependency injection where a class that depends on more than one value that has to be injected a class that depends on a class that depends on something to be injected as in the following example trait FlyBehaviour { def fly() } trait QuackBehaviour { def quack() } trait Animal { def makeSound() } // needs two behaviours

Combine state with IO actions

断了今生、忘了曾经 提交于 2019-12-02 16:17:19
Suppose I have a state monad such as: data Registers = Reg {...} data ST = ST {registers :: Registers, memory :: Array Int Int} newtype Op a = Op {runOp :: ST -> (ST, a)} instance Monad Op where return a = Op $ \st -> (st, a) (>>=) stf f = Op $ \st -> let (st1, a1) = runOp stf st (st2, a2) = runOp (f a1) st1 in (st2, a2) with functions like getState :: (ST -> a) -> Op a getState g = Op (\st -> (st, g st) updState :: (ST -> ST) -> Op () updState g = Op (\st -> (g st, ())) and so forth. I want to combine various operations in this monad with IO actions. So I could either write an evaluation loop

How do I do logging in Haskell?

≡放荡痞女 提交于 2019-12-02 15:45:45
I'm attempting to use HSlogger to get some information about my program. So I add the following line to my function import Data.Word import qualified Data.ByteString as B import qualified Data.ByteString.Lazy as L import Data.Bits import Data.Int import Data.ByteString.Parser import System.Log.Logger import System.Log.Handler.Syslog importFile :: FilePath -> IO (Either String (PESFile )) importFile n = do warningM "MyApp.Component2" "Something Bad is about to happen." ... And that works fine, because the function is inside IO. However when I add a similar line to the following function: ...

F#: Is there a way to extend the monad keyword list?

三世轮回 提交于 2019-12-02 15:35:30
Inside an F# monad, if you say let! , the compiler translates that to a Bind member that you've defined on the monad builder. Now I see there are Query monads, as shown here on MSDN , where you can say: query { for student in db.Student do select student count } and the select and count , for example, will be translated to the QueryBuilder members Linq.QueryBuilder.Select and Linq.QueryBuilder.Count . My question is, is this mapping of keywords to members hardwired into the F# compiler, or is it extensible? For example, can I say something like: FooMonadBuilder() { bar } and somehow tell the F

Has anyone ever encountered a Monad Transformer in the wild?

女生的网名这么多〃 提交于 2019-12-02 15:23:36
In my area of business - back office IT for a financial institution - it is very common for a software component to carry a global configuration around, to log its progress, to have some kind of error handling / computation short circuit... Things that can be modelled nicely by Reader-, Writer-, Maybe-monads and the like in Haskell and composed together with monad transformers. But there seem to some drawbacks: The concept behind monad transformers is quite tricky and hard to understand, monad transformers lead to very complex type signatures, and they inflict some performance penalty. So I'm

Is operational really isomorphic to a free monad?

无人久伴 提交于 2019-12-02 14:55:24
Proofs In this blog post, Tekmo makes the point that we can prove that ExitSuccess exits because (I presume) it's like the Const functor for that constructor (it doesn't carry the x so fmap behaves like const ). With the operational package, Tekmo's TeletypeF might be translated something like this: data TeletypeI a where PutStrLn :: String -> TeletypeI () GetLine :: TeletypeI String ExitSuccess :: TeletypeI () I've read that operational is isomorphic to a free monad, but can we prove here that ExitSuccess exits? It seems to me that it suffers from exactly the same problem as exitSuccess :: IO