`

haskell - few more monad - stateful computation

阅读更多

Haskell is a pure - in another word, a stateless computation, where a program makde of cuntions that can't change any global any global state or variabble, they can only do some computations and return them results. 

 

rember that we dealt with the Random numbers before. suppose that we want to generate three randome numbers to simuate three coin toss, and here is one code. 

threeCoins :: StdGen -> (Bool, Bool, Bool)  
threeCoins gen =   
    let (firstCoin, newGen) = random gen  
        (secondCoin, newGen') = random newGen  
        (thirdCoin, newGen'') = random newGen'  
    in  (firstCoin, secondCoin, thirdCoin)  

 as  you can see,  If we wanted to generate several random numbers, we always had to use the random generator that a previous function returned along with its result, so there is some requirement to preserve some state along the calls. 

 

so to help us to understand the concept, we will go ahead and give the ma type, we'll say that  a stateful computation is a funciton that takes some state and returns a value along with some new sttae, that function would have the following. 

s -> (a,s)  

 s is the type of the state and a the result of the stateful computations.

 

we will examine the stateful computation by an example of stack and Stone (stack manipulation)

 

type Stack = [Int]  
  
pop :: Stack -> (Int,Stack)  
pop (x:xs) = (x,xs)  
  
push :: Int -> Stack -> ((),Stack)  
push a xs = ((),a:xs)  

 when we pop value from a stack, the stack left is the new state, and the top value is returned as the result. and with the push operation, () is returned as the resutl. and the x:xs is the new state.

 

 

stackManip :: Stack -> (Int, Stack)  
stackManip stack = let  
    ((),newStack1) = push 3 stack  
    (a ,newStack2) = pop newStack1  
    in pop newStack2  

 

and let's do a test on the method:

 

ghci> stackManip [5,8,2,1]  
(5,[8,2,1])  

 and the code above is kind of tedious, with do Monad , and  the State monad, we might be able to write as following. 

 

stackManip = do  
    push 3  
    a <- pop  
    pop  

 

 

The state Monad 

 

Now let 's formally introduce the state monad, the State monad is defined in the Control.MOnad.State, and provde a new type that wraps stateful computations, here is the definition. 

newtype State s a = State { runState :: s -> (a,s) } 

 

and State type is part of the Monad Instance. 

instance Monad (State s) where  
    return x = State $ \s -> (x,s)  
    (State h) >>= f = State $ \s -> let (a, newState) = h s  
                                        (State g) = f a  
                                    in  g newState  

 the return method is simple as it is, it just return a State monad ( a stateful computatoin) with the value as it resutls (any state is just wrapped as the State monad's context). 

 

and what about >>=, it looks a bit more complex than the return functions, we start off with lambda, where it willl be our stateful computation (remember that hte newtype wrapper takes a lambda and returns a new State monad) . Because we are in a stateful computation, we can give the stateful computation h our current state s, which result in a pair of result and new state,  (a, newState), evry time so far when we wre implementing >>=, once we had the extracted result from the monad value, we applied the function f to it to get the new monad value, in Writer,  after doing that and getting the new monadic value, we still had to make sure that the context was taken care of by mappending the old monoid value with the new one. Here, we do f a and we get a new stateful computation g. Now that we have a new stateful computation and a new state (goes by the name of newState) we just apply that stateful computation g to the newState. The result is a tuple of final result and final state!

 

 SO, here is the revised version with Monad state computation,

 

import Control.Monad.State  
  
pop :: State Stack Int  
pop = State $ \(x:xs) -> (x,xs)  
  
push :: Int -> State Stack ()  
push a = State $ \xs -> ((),a:xs)  

 

 because of the "()" notation above, which means that the result type of the State monad is a void type. 

 

import Control.Monad.State  
  
stackManip :: State Stack Int  
stackManip = do  
    push 3  
    a <- pop  
    pop  

 

to extract the final result of the state monad, we can call the runState method to extract the value. 

ghci> runState stackManip [5,8,2,1]  
(5,[8,2,1])  

 

and you don't even need to bind to the second operation, as the 'a' returned value is not used anywhere. 

 

stackManip :: State Stack Int  
stackManip = do  
    push 3  
    pop  
    pop  

 

and with the Do monad , we can do more complex things, here is one example, where it first pop a value from the stack and check its vlaue, if it is 5, then continue to do more  ..

stackStuff :: State Stack ()  
stackStuff = do  
    a <- pop  
    if a == 5  
        then push 5  
        else do  
            push 3  
            push 8  

 

Remember, do expressions result in monadic values and with the State monad, a single do expression is also a stateful function. 

 

the Control.Monad.State module provides a type class, that's called MonadState, and it features two useful functions, namely "get" and "put", for State, the get function is implemented like this: 

 

get = State $ \s -> (s,s)  

 so it just takes the current state and presents it as the result, the put functions takes some state and make s astateful function that replace the current state with it ... (Pretty cool!!) 

 

put newState = State $ \s -> ((),newState)  

 

with this,we can do ... (stackyStack) 

stackyStack :: State Stack ()  
stackyStack = do  
    stackNow <- get  
    if stackNow == [1,2,3]  
        then put [8,3,1]  
        else put [9,2,1]  

 

It's worh examing what the type of >>= would be if it only worked for State values. (teh partial specification for State monad)

(>>=) :: State s a -> (a -> State s b) -> State s b  

 

this means we can glue together several stateful computation whose result are of different types but the types of the state has to stay the same. so what is that? given the type is Maybe, >>= may gives us : 

 

(>>=) :: Maybe a -> (a -> Maybe b) -> Maybe b  

 

Randomness and the State monad. 

 we know it is a bit awkward because every random function takes a generator and returns a random number along with a new Generator, and we have to use the new returned generator because otherwise we will get a pseudo number. 

 

the random function is like this: 

random :: (RandomGen g, Random a) => g -> (a, g)  

 

and we can tell that this is a stateful computation. we can make a function as such.

 

import System.Random  
import Control.Monad.State  
  
randomSt :: (RandomGen g, Random a) => State g a  
randomSt = State random  

 

 and with this , we can rewrite the TheeCoins function as such .. 

import System.Random  
import Control.Monad.State  
  
threeCoins :: State StdGen (Bool,Bool,Bool)  
threeCoins = do  
    a <- randomSt  
    b <- randomSt  
    c <- randomSt  
    return (a,b,c)  

 

we can check on whether it is working as we expected, by the following method cal. 

ghci> runState threeCoins (mkStdGen 33)  
((True,False,True),680029187 2103410263)  

 

分享到:
评论

相关推荐

    Atom-ide-haskell-hoogle,在光标下显示符号的滚动信息。对原子的贡献.zip

    Atom-ide-haskell-hoogle 是一个专门为 Atom 编辑器设计的插件,它整合了 Haskell 的 Hoogle 工具,以提供强大的代码提示和搜索功能。Atom 是一款由 GitHub 开发的开源文本编辑器,它利用现代 web 技术如 HTML、CSS ...

    haskell-mode emacs

    在 Emacs 中,`haskell-mode` 是一个专门为了提升 Haskell 开发体验而设计的模式。 `haskell-mode` 提供了多种增强功能,旨在帮助 Haskell 开发者更高效地编写、调试和理解代码。这个模式包括以下关键特性: 1. **...

    haskell-chart, haskell的2D 图表库.zip

    在数据可视化领域,`haskell-chart`库提供了一种高效且灵活的方式来创建2D图表,这对于数据分析、科学计算以及教学等场景非常有用。这个库是开源的,意味着任何人都可以查看其源代码,学习并贡献改进。 `haskell-...

    Atom-haskell-ghc-mod,哈斯克尔.zip

    **哈斯克尔编程语言与Atom-Haskell-GHC-Mod** 哈斯克尔(Haskell)是一种纯函数式编程语言,以其优雅的语法、强静态类型系统和编译时优化而受到程序员的喜爱。它鼓励使用不可变数据和惰性求值,这使得哈斯克尔在...

    Atom-haskell-debug,使用ghci在atom中实现图形haskell调试器.zip

    Atom-Haskell-Debug是针对Haskell开发者的一个强大工具,它允许你在流行的Atom文本编辑器中集成一个图形化的Haskell调试器。这个工具基于GHCi(Glasgow Haskell Compiler Interface),GHCi是Haskell的交互式环境,...

    Haskell-Data-Analysis-Cookbook, Haskell数据分析 cookbook的附带源代码.zip

    Haskell-Data-Analysis-Cookbook, Haskell数据分析 cookbook的附带源代码 Haskell-Data-Analysis-Cookbook这是 Haskell数据分析 cookbook的附带源代码。最新的源代码可以在GitHub上获得: ...

    haskell-ghc-mod:haskell-ghc-mod原子包

    从1.0.0开始,haskell-ghc-mod提供haskell-completion-backend服务。 注意:在1.0.0之前,提供了ide-backend服务。 它已被废弃以支持ide-haskell的UPI。 您可以在找到描述 执照 版权所有:copyright:2015 Atom-...

    haskell-ghc-mod:haskell-ghc-mod原子包

    haskell-ghc-mod原子包 该软件包主要用作后端。 Haskell ghc-mod打开通往ghc-modi的管道,并查询类型,信息并检查错误。 安装与配置 请参考官方文档站点 服务中心API 从1.0.0版本开始,haskell-ghc-mod提供...

    Get Programming with HASKELL-2018-英文版.pdf

    Get Programming with HASKELL-2018-英文版

    haskell-relational-record-driver-mysql:用于 haskell-relational-record 的 MySQL 驱动程序

    用于 haskell-relational-record 的 MySQL 驱动程序 这个项目被合并到 。 准备 $ git clone git@github.com:khibino/haskell-relational-record.git $ git clone git@github.com:bos/hdbc-mysql.git $ git clone ...

    Atom-atom-haskell-scry,扩散系数.zip

    【标题】:“Atom-atom-haskell-scry,扩散系数.zip” 涉及的主要知识点是 Atom 编辑器与 Haskell 语言的集成以及 SCRY 工具的使用。 【描述】:“Atom-atom-haskell-scry.zip”的描述指出,这个压缩包包含了一个名...

    Atom-ide-haskell-repl,原子中的ghci repl。对原子的贡献.zip

    Atom-ide-haskell-repl是针对Atom文本编辑器的一个扩展插件,专为Haskell编程语言提供集成的GHCi(Glasgow Haskell Compiler Interface)交互式环境,即REPL(Read-Eval-Print Loop)。这个插件允许开发者在Atom编辑...

    haskell-dev-tools:我用来安装升级与Haskell相关的开发工具的元项目

    在Haskell的世界里,开发环境的配置至关重要,而`haskell-dev-tools`就是一个方便的元项目,它专门设计用于简化Haskell开发工具的安装和升级过程。这个项目扮演了一个集合和自动化工具的角色,使得开发者无需手动...

    haskell-dap:Haskell DAP接口数据的实现

    Haskell-dap是一个开源项目,它实现了调试适应性协议(Debug Adapter Protocol,简称DAP)的接口,使得Haskell开发者可以充分利用这个协议进行程序调试。DAP是一个通用的、跨平台的协议,允许IDEs(集成开发环境)和...

    haskell-brainfuck:Haskel 脑残翻译

    你可以在找到 haskell-brainfuck用法图书馆 import HaskBF.Evalimport qualified Data.ByteString.Lazy as BSimport Control.Monad.Statemain = do -- The following will evaluate the file using stdin and ...

    haskell-tools:Haskell的开发人员工具

    "haskell-tools"就是这样一个项目,它专注于为Haskell开发者提供一系列实用的辅助工具,以优化他们的开发流程。 ### 1. GHC:Glasgow Haskell Compiler GHC是Haskell的主要编译器,也是haskell-tools的重要组成...

    Haskell - The Craft of Functional Programming, 2ed (Addison-Wesley, 1999) by Tantanoid

    ### Haskell - The Craft of Functional Programming #### 一、概述 《Haskell - The Craft of Functional Programming》是一本深入探讨Haskell编程语言的经典著作。本书由Tantanoid编写,并于1999年由Addison-...

    Atom-ide-haskell-cabal,IDE的Cabal后端提供商.zip

    Atom-ide-haskell-cabal.zip,Cabal backend provider for ide-haskellIDE Haskell Cabal套餐,atom是一个用web技术构建的开源文本编辑器。

    函数式编程-haskell-to-java

    ### 函数式编程:Haskell到Java的转换 #### 概述 本文旨在探讨函数式编程语言Haskell如何被编译或转换为Java语言。Haskell作为一种纯函数式编程语言,以其强大的类型系统、惰性求值机制以及高度抽象的能力在学术界...

    haskell-lsp-client:haskell-lsp的客户端库

    haskell-lsp-client 该软件包适用于希望使其文本编辑器与兼容的文本编辑器的开发人员。 我已经开发了此软件包,并计划将其集成到。 示例客户端 该存储库中包含一个示例客户端。 此示例客户端仅运行并打开在命令行...

Global site tag (gtag.js) - Google Analytics