monads

Termination-checking substitution via (monadic) join and fmap

故事扮演 提交于 2019-12-22 11:14:09
问题 I'm using sized types, and have a substitution function for typed terms which termination-checks if I give a definition directly, but not if I factor it via (monadic) join and fmap. {-# OPTIONS --sized-types #-} module Subst where open import Size To show the problem, it's enough to have unit and sums. I have data types of Trie and Term , and I use tries with a codomain of Term inside Term , as part of the elimination form for sums. data Type : Set where 𝟏 : Type _+_ : Type → Type → Type

Implementing this monad/type in Haskell?

[亡魂溺海] 提交于 2019-12-22 10:41:30
问题 I really cannot figure out the syntax necessary for this, and it probably comes from my lack of understanding of how types work. I want a type DataPoint , which stores either a tuple (x, dataval) or two fields x and dataval (where x is a Double and dataval is a Complex Double . I want a Monad instance where it goes something like: instance Monad (DataPoint x dataval) where return dataval = DataPoint 0.0 dataval DataPoint x dataval >>= f = DataPoint x (f dataval) Basically, the "value" of the

Does using a free monad in F# imply a higher startup time and limited instructions?

一曲冷凌霜 提交于 2019-12-22 08:12:47
问题 I am reading this excellent article by Mark Seemann. In it, he gives a simple demonstration of using the free monad to model interactions using pure functions. I understand it enough to be able to write such a program, and I can appreciate the virtues of such an approach. There is one bit of code though, that has me wondering about the implications. let rec bind f = function | Free instruction -> instruction |> mapI (bind f) |> Free | Pure x -> f x The function is recursive. Given that this

How to implement index-core style indexed state monad?

久未见 提交于 2019-12-22 05:03:16
问题 I'm trying to understand indexed monads in the index-core style. I have become stuck in a paradox which is that I can't understand the principles until I have constructed a few examples, and I can't construct examples until I understand the principles. I am trying to construct an indexed state monad. So far my intuition tells me it should be something like this type a :* b = forall i. (a i, b i) newtype IState f a i = IState { runIState :: f i -> (a :* f) } and that I can recover the

Can GHC derive Functor and Applicative instances for a monad transformer?

淺唱寂寞╮ 提交于 2019-12-22 04:42:31
问题 I'm trying to implement MaybeT in the spirit of the mtl library. With this non-compiling solution: {-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, UndecidableInstances #-} import Control.Monad import Control.Monad.Trans import Control.Monad.State newtype MaybeT m a = MaybeT { runMaybeT :: m (Maybe a) } instance (Monad m) => Monad (MaybeT m) where x >>= f = MaybeT $ runMaybeT x >>= maybe (return Nothing) (runMaybeT . f) return a = MaybeT $ return (Just a) fail _ = MaybeT $ return

Haskell maps returning a monad

我是研究僧i 提交于 2019-12-22 03:40:21
问题 The lookup function in Data.Map and Data.IntMap currently return values wrapped in Maybe with the type signature lookup :: Ord k => k -> Map k a -> Maybe a It used to have the more general type of lookup :: (Monad m, Ord k) => k -> Map k a -> m a I realize the former likely reduces the need of extra type specification, but the latter would make it much more general and allow lookup to be used in list comprehensions. Is there any way to mimic this behavior with the newer version, or would I

bind a monadic value (m2 a) inside some other monad m1

狂风中的少年 提交于 2019-12-21 22:18:23
问题 Working in a Coding Dojo today I tried the following example :: IO () example = do input <- getLine parsed <- parseOnly parser input ... where parseOnly :: Parser a -> Either String a (from attoparsec ) of course the compiler complained that Either .. is not IO .. essentially telling me I am mixing monads. Of course this can be solved by case parseOnly parser input of .. -> .. which is kind of unelegant, I think. Also my guess is that somebody else had this problem earlier and the solution I

Confusion over IORefs to make a counter

China☆狼群 提交于 2019-12-21 19:50:14
问题 I found some sample code, and changed it a little counter = unsafePerform $ newIORef 0 newNode _ = unsafePerformIO $ do i <- readIORef counter writeIORef counter (i+1) return i Which returns 1 then 2 then 3 then 3 etc each time it's run. But when I change it to newNode = unsafePerformIO $ do i <- readIORef counter writeIORef counter (i+1) return i then I get 0 every time I run it. Why is this happening, and what can I do to fix it? 回答1: In your second version newNode is a simple value, not a

Pure functional Random number generator - State monad

人走茶凉 提交于 2019-12-21 12:39:38
问题 The book ' Functional Programming in Scala ' demonstrates an example of pure functional random number generator as below trait RNG { def nextInt: (Int, RNG) } object RNG { def simple(seed: Long): RNG = new RNG { def nextInt = { val seed2 = (seed*0x5DEECE66DL + 0xBL) & ((1L << 48) - 1) ((seed2 >>> 16).asInstanceOf[Int], simple(seed2)) } } } The usage will look like val (randomNumber,nextState) = rng.nextInt I do get the part that it's a pure function as it returns the next state and leaves it

Haskell do clause with multiple monad types

半城伤御伤魂 提交于 2019-12-21 09:26:10
问题 I'm using a graphic library in Haskell called Threepenny-GUI. In this library the main function returns a UI monad object. This causes me much headache as when I attempt to unpack IO values into local variables I receive errors complaining of different monad types. Here's an example of my problem. This is a slightly modified version of the standard main function, as given by Threepenny-GUI's code example: main :: IO () main = startGUI defaultConfig setup setup :: Window -> UI () setup w = do