acme-realworld (empty) → 0.1
raw patch · 4 files changed
+141/−0 lines, 4 filesdep +basesetup-changed
Dependencies added: base
Files
- Acme/RealWorld.hs +69/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- acme-realworld.cabal +40/−0
+ Acme/RealWorld.hs view
@@ -0,0 +1,69 @@+-- |+-- Module: Acme.RealWorld+-- Copyright: (c) Joseph Adams 2011+-- Maintainer: joeyadams3.14159@gmail.com+-- Portability: GHC-only+--+-- Primitives for manipulating the state of the universe.++{-# LANGUAGE MagicHash, UnboxedTuples #-}+module Acme.RealWorld (+ -- * The RealWorld type+ RealWorld,++ -- * Universe manipulation primitives+ getWorld,+ putWorld,+ execIO,++ -- * Derived combinators+ hypothetically,+) where++import Control.Exception (bracket)+import GHC.IO+import GHC.Exts++fromState :: State# RealWorld -> RealWorld+fromState = unsafeCoerce#++toState :: RealWorld -> State# RealWorld+toState = unsafeCoerce#++-- | Retrieve the current state of the universe.+getWorld :: IO RealWorld+getWorld = IO (\s -> (# s, fromState s #))++-- | Set the current state of the universe. Program values are not affected by+-- this operation, but the rest of the universe is.+--+-- 'putWorld' may not be called on the same state twice (this is enforced by+-- the runtime system). Otherwise, it would be possible to trap the universe+-- in a temporal loop:+--+-- >getWorld >>= forever . putWorld+putWorld :: RealWorld -> IO ()+putWorld s' = IO (\_ -> (# toState s', () #))++-- | Given an action, construct a function that, given a state of the universe,+-- returns the state of the universe after the action has occurred.+--+-- Example:+--+-- >main = do+-- > let f = execIO $ putStrLn "Second"+-- > g = execIO $ putStrLn "First"+-- > getWorld >>= putWorld . f . g+execIO :: IO a -> RealWorld -> RealWorld+execIO (IO k) = \w -> case k (toState w) of (# s', _ #) -> fromState s'++-- | Perform an action and return its value, but undo any side effects to the+-- universe. Thus, it appears to return instantly, regardless of how long the+-- action would take to run.+--+-- The caller must ensure that the program /would/ have enough time to perform+-- the computation. Otherwise, either an exception will be thrown, or the+-- operation will block because it never gets a chance to restore the original+-- state of the universe.+hypothetically :: IO a -> IO a+hypothetically action = bracket getWorld putWorld (\_ -> action)
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2011, Joseph Adams++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Joseph Adams nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ acme-realworld.cabal view
@@ -0,0 +1,40 @@+name: acme-realworld+version: 0.1+synopsis: Primitives for manipulating the state of the universe+description:+ GHC represents an IO action internally as a function from one state of the+ world to the next:+ .+ >State# RealWorld -> (# State# RealWorld, a #)+ .+ This module provides a very useful capability stemming directly from this+ representation: the ability to save the current state of the universe and+ restore it later. This provides a way to \"undo\" certain types of side+ effects in the IO monad:+ .+ >import Acme.Missiles+ >import Acme.RealWorld+ >+ >main :: IO ()+ >main = do+ > -- Save the current state of the universe+ > world_as_we_know_it <- getWorld+ >+ > -- Cause serious international side effects+ > launchMissiles+ >+ > -- After realizing that was a terrible, terrible mistake, restore the+ > -- pre-war state of the universe.+ > putWorld world_as_we_know_it+license: BSD3+license-file: LICENSE+author: Joey Adams+maintainer: joeyadams3.14159@gmail.com+copyright: Copyright (c) Joseph Adams 2011+category: Acme+build-type: Simple+cabal-version: >=1.8++library+ exposed-modules: Acme.RealWorld+ build-depends: base == 4.*