monads

Equational reasoning with tying the knot

◇◆丶佛笑我妖孽 提交于 2019-12-08 17:28:16
问题 I'm trying to wrap my head around Cont and callCC, by reducing this function: s0 = (flip runContT) return $ do (k, n) <- callCC $ \k -> let f x = k (f, x) in return (f, 0) lift $ print n if n < 3 then k (n+1) >> return () else return () I've managed to reach this point: s21 = runContT (let f x = ContT $ \_ -> cc (f, x) in ContT ($(f,0))) cc where cc = (\(k,n) -> let iff = if n < 3 then k (n+1) else ContT ($()) in print n >> runContT iff (\_ -> return ())) And at this point i have no idea what

Execution order with (>>=) not what I expected

风格不统一 提交于 2019-12-08 16:11:57
问题 I've got a series of network requests, that each take >10 seconds. So that the user knows what's happening, I give updates: main = do putStr "Downloading the first thing... " {- Net request -} putStrLn "DONE" putStr "Downloading the second thing... " {- Net request -} putStrLn "DONE" With GHCi this works as expected, but compiled or with runghc, "Downloading" doesn't print till "DONE" does. I've rewritten it with (>>=) and (>>), but I get the same problem. What's going on? 回答1: The problem

How to keep the stacktrace when rethrowing an exception out of catch-context?

谁说胖子不能爱 提交于 2019-12-08 15:59:29
问题 TL;DR: how to raise a previously caught exception later on, while preserving the original exception's stacktrace. Since I think this is useful with the Result monad or computation expression, esp. since that pattern is often used for wrapping an exception without throwing it, here's a worked out example of that: type Result<'TResult, 'TError> = | Success of 'TResult | Fail of 'TError module Result = let bind f = function | Success v -> f v | Fail e -> Fail e let create v = Success v let

Why does “return Nothing” return Nothing?

雨燕双飞 提交于 2019-12-08 15:24:49
问题 "return a" is supposed to wrap a in the context of some Monad: *Main> :i return class Applicative m => Monad (m :: * -> *) where ... return :: a -> m a ... -- Defined in ‘GHC.Base’ If I ask GHCI what the type of "return Nothing" is, it conforms to that: *Main> :t return Nothing return Nothing :: Monad m => m (Maybe a) But if I evaluate it, I see no outer Monad, just the inner Maybe: *Main> return Nothing Nothing 回答1: When GHCi goes to print a value, it tries two different things. First, it

IEnumerable of Options to Option of IEnumerable

笑着哭i 提交于 2019-12-08 09:38:11
问题 I have a 3rd party method that get IEnumerable of T and evaluates it. I would like to introduce Option with exceptional values(either) in my LINQ evaluation statements(select, where...) for the value that needs to get fed into the method. For each T element I would like to have a method that can return the transformed element or an error (leaving it as deferred execution) and If error is returned evaluation should stop there. (technically this can be achieved with throwing an exception, but

Why is `mfix` not total in `MaybeT`

Deadly 提交于 2019-12-08 08:03:45
问题 The transformers implementation of MonadFix for MaybeT fails if the function ever evaluates to Nothing . Why is Nothing not propagating over mfix ? mfix' :: MonadFix m => (a -> MaybeT m a) -> MaybeT m a mfix' f = MaybeT $ mfix $ \case Nothing -> return Nothing Just x -> runMaybeT $ f x There must be a good reason that I do not see because ListT does not implement MonadFix at all, and Maybe implements it in the same way as above. 回答1: I think the issue is just that the error message is

Convert Railway oriented failure track to Rx friendly errors

非 Y 不嫁゛ 提交于 2019-12-08 01:52:39
问题 I'm using a library that takes the results as two-track values (success and failure).In the Observable.map function bodies I often get an observable from success track of a function, and I don't know how to handle them (at Observable.map body). In the other words I often get stuck in situations that the result looks something like the following (of course it's the simplest one): Rop.Result<IObservable<Rop.Result<IObservable,Messages>,Messages> On one hand, translating messages back to

Monadic substitution under binders

怎甘沉沦 提交于 2019-12-07 16:32:47
问题 In the following Agda code, I have a term language based on de Bruijn indices. I can define substitution over terms in the usual de Bruijn indices way, using renaming to allow the substitution to proceed under a binder. module Temp where data Type : Set where unit : Type _⇾_ : Type → Type → Type -- A context is a snoc-list of types. data Cxt : Set where ε : Cxt _∷_ : Cxt → Type → Cxt -- Context membership. data _∈_ (τ : Type) : Cxt → Set where here : ∀ {Γ} → τ ∈ Γ ∷ τ there : ∀ {Γ τ′} → τ ∈ Γ

What does Haskell call the Hom Functor/Monad?

谁都会走 提交于 2019-12-07 11:05:07
问题 I'd like to use it in my code and would rather not duplicate it, but since it involves only massively generic words like "function" or "composition" I can't find it by searching. To be completely specific, I'm looking for instance Functor (x->) where fmap f p = f . p 回答1: This is the basic reader (or environment) monad, usually referred to as ((->) e) . (This is (e ->) written as a partially applied function instead of as a section; the latter syntax is problematic to parse.) You can get it

Scotty Using MongoDB

心不动则不痛 提交于 2019-12-07 09:56:35
问题 I'm relatively new to Haskell, and this is my first time working with monad transformers. I'd really appreciate some help. runQuery :: Pipe -> Query -> ActionM (Either Failure [Document]) runQuery pipe query = access pipe master "nutrition" (find query >>= rest) main = do pipe <- runIOE $ connect $ host "127.0.0.1" scotty 3000 $ do post "/" $ do b <- body let user :: Either String User = eitherDecode b case user of Left err -> text . pack $ "Could not decode the user:" ++ err ++ ":\n" ++