Understanding pure functions in Haskell with IO

Given the meaning of Halkell (edit per Rein Heinrich comment ) f:

f :: IO Int
f = ... -- ignoring its implementation

Quote: “Type-driven development with Idris,”

The key property of a pure function is that the same inputs always give the same result. This property is called referential transparency.

Is f, and, namely, all functions IO ...in Haskell pure? It seems to me that they have not since, lookInDatabase :: IO DBThingwill not always return the same value, since:

  • at t = 0, the database may not be available
  • at t = 1, the database can be up and return MyDbThingwill lead to

In short, is the function f(and IO ...generally) pure? If so, please correct my misunderstanding, given my attempt to refute functional purity fwith my examples t=....

+4
source share
4 answers

IO is a truly separate language, conceptually. This is the Haskell RTS (Runtime System) language. It is implemented in Haskell as a (relatively simple) embedded DSL whose scripts are of type IO a.

, Haskell, IO a, , - , , IO a. , , .

, IO - , , , . , , IO, , , .


FRP - FRP , IO , .

" ", .. Conal Elliott google, IO.


P.S. , , IO , , , - DSL ( IO - ), , IO, IO - , .

+6

, , , - . . , , ?

, , ( " a" ) IO Something , IO Something . IO "" , . IO String, / String/[Char], , String - . , IO , , -.

, IO - main main. - "" IO. ... ( : )

main = do
    input <- getLine
    putStrLn input

...

main =
    getLine >>= (\input -> putStrLn input)

, main - - , , . ? IO - , , , , , , Haskell .

. (: IO monad) (: Something IO Something), , , ( ). (: Haskell, main) (: / ), , , " (: )... , (: System.IO.Error), , ( ).

, IO . ( GHC), .

, . !

, !

+2

: , f .

, , .
, .

+2

. IO - , , .

"IO is pure" , IO DBThing, DBThing. - , Stuff -> IO DBThing , , Stuff DBThing; , , ! , , Stuff IO DBThing.

DBThing IO DBThing , Haskell , ( ) . , IO DBThing, - , DBThing - IO thing; IO thing.

Haskell , Haskell , . , , , . , IO , Haskell IO, , .

, "" . , IO - , , , - ; = ( Stuff -> IO DBThing , , a DBThing , DBThing Stuff). , IO DBThing ; Haskell , IO, , IO, Haskell () .

. , IO , -, , .

, . Haskell IO ; Monad - , , , , - ( , - IO). Monad , , IO , , , IO.

, -? , . ", IO - " . - , , ( ), ; , - ( , ). IO pure , .

? , , , IO , . IO, IO . . ; Haskell -IO-, , , ( , ).

, IO , ( ) IO-. , , -, -. . , - ( , ), , ; - " " " ", IO.

+1
source

All Articles