[Haskell-cafe] Referential Transparency and Monads

Brandon S. Allbery KF8NH allbery at ece.cmu.edu
Thu Apr 9 22:55:29 EDT 2009


On 2009 Apr 9, at 22:47, Brandon S. Allbery KF8NH wrote:
> > -- these implicitly are considered to return a modified RealWorld
> > readFile :: RealWorld -> (String,RealWorld)
> > writeFile :: RealWorld -> ((),RealWorld)
> > threadDelay :: RealWorld -> ((),RealWorld)
> >
> > main :: RealWorld -> ((),RealWorld)
> > main state =
> >   case readFile state "/my_file" of
> >     (s,state') ->
> >        case writeFile state' "/my_file" "Hello, world!\n" of
> >          (_,state'') ->
> >             case threadDelay state'' 10000 of
> >               (_,state'') -> writeFile "/my_file" s	

Sorry, the last line should be:

 >               (_,state''') -> writeFile state''' "/my_file" s

This code is nothing more nor less than the translation of Jonathan  
Cast's code, using a presumptive

 > type IO a = State RealWorld a

into "linear" de-monadified code.  Clearly the state is threaded into  
main, through all the calls, and the final state returned; the state  
threading is what maintains the referentially transparent model.

-- 
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery at kf8nh.com
system administrator [openafs,heimdal,too many hats] allbery at ece.cmu.edu
electrical and computer engineering, carnegie mellon university    KF8NH


-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 195 bytes
Desc: This is a digitally signed message part
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20090409/7fce2d2b/PGP.bin


More information about the Haskell-Cafe mailing list