state-monad

Control.ST pure type

本秂侑毒 提交于 2019-12-25 03:26:36
问题 pure : (result : ty) -> STrans m ty (out_fn result) out_fn from http://docs.idris-lang.org/en/latest/st/state.html#strans-primitive-operations I'm not sure what (out_fn result) out_fn means. Is it about constraining out_fn to be a function of result ? Does it actually say anything about the input resource list? The given explanation seems to be "...provided that the current list of resources is correct when producing that value" but I'm not sure how to interpret it. 回答1: STrans : (m : Type ->

Combine ST and List monads in Haskell

人走茶凉 提交于 2019-12-23 15:08:33
问题 Using the StateT monad transformer, I can create the type StateT s [] a , which is isomorphic to s -> [(a, s)] . Now I would prefer to use the STT monad transformer instead, as I would like to have multiple mutable variables of different types, and would like to be able to instantiate them at will, depending on the results of earlier computations. However, the linked documentation for STT mentions explicitly: This monad transformer should not be used with monads that can contain multiple

Constructing minimal Haskell example on error-handling in the State Monad

我是研究僧i 提交于 2019-12-23 13:16:14
问题 I'm twisting my brain into knots trying to understand how to combine the State monad with Maybe . Let's start with a concrete (and intentionally trivial/unnecessary) example in which we use a State monad to find the sum of a list of numbers: import Control.Monad.State list :: [Int] list = [1,4,5,6,7,0,3,2,1] adder :: Int adder = evalState addState list addState :: State [Int] Int addState = do ms <- get case ms of [] -> return 0 (x:xs) -> put xs >> fmap (+x) addState Cool. Now let's modify it

Implementation of flatMap() for State transition

青春壹個敷衍的年華 提交于 2019-12-23 12:17:06
问题 Exercise 6.8, Chiusano and Bjarnason, Functional Programming in Scala , p. 87 asks how one might implement flatMap() for the following trait: trait RNG { def nextInt: (Int, RNG) } type Rand[+A] = RNG => (A, RNG) The answer key gives the following solution: def flatMap[A,B](f: Rand[A])(g: A => Rand[B]): Rand[B] = rng => { val (a, r1) = f(rng) g(a)(r1) // We pass the new state along } Stackoverflow provides many answers to flatMap()/monad questions, but none which for me answered my questions

State and IO Monads

六眼飞鱼酱① 提交于 2019-12-23 07:47:58
问题 I've been trying to wrap my head around the concept of monads and I've been experimenting with the following example: I have an Editor data-type that represents the state of a text document and some functions that work on it. data Editor = Editor { lines :: [Line], -- editor contents are kept line by line lineCount :: Int, -- holds length lines at all times caret :: Caret -- the current caret position -- ... some more definitions } deriving (Show) -- get the line at the given position (first

Pass a lens into a funciton

≡放荡痞女 提交于 2019-12-23 00:08:21
问题 How to pass properly a lens into a function with state? Let us consider the next code: {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE FlexibleContexts #-} import Control.Lens import Control.Monad.State data Game = Game { _armies :: [Army] } deriving (Show) data Army = Army { _troops :: Int } deriving (Show) makeLenses ''Game makeLenses ''Army data BattleResult = Win | Defeat deriving (Show) offend offender defender = do Just ot <- preuse $ offender.troops Just dt <- preuse $ defender.troops

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

State monad and strategy pattern

落爺英雄遲暮 提交于 2019-12-21 11:22:14
问题 I am redesigning a library and I am not happy with the current design pattern. This question concerns the use of the strategy pattern in conjunction with a State monad I have a Filter . All it does, in its basic implementation, is to take a some datafeed of type 'd and update itself, generating a new updated copy of itself. [<AbstractClass>] type Filter<'d, 'F> (state: 'F) = member val StateVariable = state with get abstract member Update: 'd -> Filter<'d, 'F> I have then a ISignalGenerator ,

Scala and State Monad

北战南征 提交于 2019-12-21 09:23:08
问题 I have been trying to understand the State Monad. Not so much how it is used, though that is not always easy to find, either. But every discussion I find of the State Monad has basically the same information and there is always something I don't understand. Take this post, for example. In it the author has the following: case class State[S, A](run: S => (A, S)) { ... def flatMap[B](f: A => State[S, B]): State[S, B] = State(s => { val (a, t) = run(s) f(a) run t }) ... } I can see that the

Combining StateT and State monads

我只是一个虾纸丫 提交于 2019-12-21 03:26:15
问题 Lets say I have a function f :: State [Int] Int and a function: g :: StateT [Int] IO Int I want to use f in g and pass the state between them. Is there a library function for StateT (return . runState f) ? Or in general, given a monad transformer with a corresponding monad, is there a library function for it? 回答1: In even more general, what you're trying to do is apply a transformation to an inner layer of a transformer stack. For two arbitrary monads, the type signature might look something