In what sense is the IO Monad pure?

后端 未结 9 1654
暗喜
暗喜 2020-12-04 08:41

I\'ve had the IO monad described to me as a State monad where the state is \"the real world\". The proponents of this approach to IO argue that this makes IO operations pure

9条回答
  •  不思量自难忘°
    2020-12-04 09:08

    Suppose we have something like:

    animatePowBoomWhenHearNoiseInMicrophone :: TimeDiff -> Sample -> IO ()
    animatePowBoomWhenHearNoiseInMicrophone
        levelWeightedAverageHalfLife levelThreshord = ...
    
    programA :: IO ()
    programA = animatePowBoomWhenHearNoiseInMicrophone 3 10000
    
    programB :: IO ()
    programB = animatePowBoomWhenHearNoiseInMicrophone 3 10000
    

    Here's a point of view:

    animatePowBoomWhenHearNoiseInMicrophone is a pure function in the sense that its results for same input, programA and programB, are exactly the same. You can do main = programA or main = programB and it would be exactly the same.

    animatePowBoomWhenHearNoiseInMicrophone is a function receiving two arguments and resulting in a description of a program. The Haskell runtime can execute this description if you set main to it or otherwise include it in main via binding.

    What is IO? IO is a DSL for describing imperative programs, encoded in "pure-haskell" data structures and functions.

    "complete-haskell" aka GHC is an implementation of both "pure-haskell", and an imperative implementation of an IO decoder/executer.

提交回复
热议问题