| 1 | -- The HiPar Toolkit: state transformer routines |
| 2 | -- |
| 3 | -- Author : Manuel M. T. Chakravarty |
| 4 | -- Created: 3 March 95 |
| 5 | -- |
| 6 | -- Version $Revision: 1.1.1.1 $ from $Date: 2004/11/13 16:42:45 $ |
| 7 | -- |
| 8 | -- Copyright (C) [1995..1999] Manuel M. T. Chakravarty |
| 9 | -- |
| 10 | -- This file is free software; you can redistribute it and/or modify |
| 11 | -- it under the terms of the GNU General Public License as published by |
| 12 | -- the Free Software Foundation; either version 2 of the License, or |
| 13 | -- (at your option) any later version. |
| 14 | -- |
| 15 | -- This file is distributed in the hope that it will be useful, |
| 16 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | -- GNU General Public License for more details. |
| 19 | -- |
| 20 | --- DESCRIPTION --------------------------------------------------------------- |
| 21 | -- |
| 22 | -- This module provides basic support for the use of state transformers. |
| 23 | -- The state transformer is build around the `IO' monad to allow the |
| 24 | -- manipulation of external state. It encapsulated two separate states with |
| 25 | -- the intention to use the first one for the omnipresent compiler state |
| 26 | -- consisting of the accumulated error messages etc. and to use the second as |
| 27 | -- a generic component that can be used in different ways by the different |
| 28 | -- phases of the compiler. |
| 29 | -- |
| 30 | -- The module also supports the use of exceptions and fatal errors. |
| 31 | -- |
| 32 | --- DOCU ---------------------------------------------------------------------- |
| 33 | -- |
| 34 | -- language: Haskell 98 |
| 35 | -- |
| 36 | -- * We explicitly do not use any names for the monad types and functions |
| 37 | -- that are used by either Haskell's `IO' monad or GHC's `ST' monad. Since |
| 38 | -- Haskell 1.4, `STB' is an instance of the `Monad' constructor class. |
| 39 | -- |
| 40 | -- * To integrate the Haskell prelude `IO' monad into our `STB' monad we use |
| 41 | -- the technique from ``Composing monads'' by Mark P. Jones and Luc |
| 42 | -- Duponcheel (Report YALEU/DCS/RR-1004) from 1993, Section 8. |
| 43 | -- |
| 44 | -- * The use of GHC's inplace-update goodies within monads of kind `STB' is |
| 45 | -- possible, because `IO' is based on `ST' in the GHC. |
| 46 | -- |
| 47 | -- * In the following, we call the two kinds of state managed by the `STB' the |
| 48 | -- base state (the omnipresent state of the compiler) and generic state. |
| 49 | -- |
| 50 | -- * `STB' is a newtype, which requires careful wrapping and unwrapping of its |
| 51 | -- values in the following definitions. |
| 52 | -- |
| 53 | --- TODO ---------------------------------------------------------------------- |
| 54 | -- |
| 55 | -- * with constructor classes, the state transformer business can be made |
| 56 | -- more elegant (they weren't around when this module was initially written) |
| 57 | -- |
| 58 | -- * it would be possible to maintain the already applied changes to the base |
| 59 | -- and generic state even in the case of a fatal error, when in `listIO' |
| 60 | -- every IO operation is encapsulated into a handler that transforms IO |
| 61 | -- errors into exceptions |
| 62 | -- |
| 63 | |
| 64 | module StateTrans (-- the monad and the generic operations |
| 65 | -- |
| 66 | STB, fixSTB, |
| 67 | -- |
| 68 | -- monad specific operations |
| 69 | -- |
| 70 | readBase, writeBase, transBase, readGeneric, writeGeneric, |
| 71 | transGeneric, liftIO, runSTB, interleave, |
| 72 | -- |
| 73 | -- exception handling and fatal errors |
| 74 | -- |
| 75 | throwExc, fatal, catchExc, fatalsHandledBy, |
| 76 | -- |
| 77 | -- mutable variables and arrays |
| 78 | -- |
| 79 | MVar, newMV, readMV, assignMV) |
| 80 | where |
| 81 | |
| 82 | import Prelude hiding (catch) |
| 83 | |
| 84 | import Control.Applicative (Applicative(..)) |
| 85 | import Control.Monad (liftM, ap) |
| 86 | import Control.Exception (catch) |
| 87 | import System.IO (fixIO) |
| 88 | import Data.IORef (IORef, newIORef, readIORef, writeIORef) |
| 89 | |
| 90 | import Errors (interr) |
| 91 | |
| 92 | infixr 1 +>=, +> |
| 93 | |
| 94 | |
| 95 | -- BEWARE! You enter monad country. Read any of Wadler's or |
| 96 | -- Launchbury/Peyton-Jones' texts before entering. Otherwise, |
| 97 | -- your mental health my be in danger. You have been warned! |
| 98 | |
| 99 | |
| 100 | -- state transformer base and its monad operations |
| 101 | -- ----------------------------------------------- |
| 102 | |
| 103 | -- the generic form of a state transformer using the external state represented |
| 104 | -- by `IO'; `STB' is a abbreviation for state transformer base |
| 105 | -- |
| 106 | -- the first state component `bs' is provided for the omnipresent compiler |
| 107 | -- state and the, second, `gs' for the generic component |
| 108 | -- |
| 109 | -- the third component of the result distinguishes between erroneous and |
| 110 | -- successful computations where |
| 111 | -- |
| 112 | -- `Left (tag, msg)' -- stands for an exception identified by `tag' with |
| 113 | -- error message `msg', and |
| 114 | -- `Right a' -- is a successfully delivered result |
| 115 | -- |
| 116 | |
| 117 | newtype STB bs gs a = STB (bs -> gs -> IO (bs, gs, Either (String, String) a)) |
| 118 | |
| 119 | instance Functor (STB bs gs) where |
| 120 | fmap = liftM |
| 121 | |
| 122 | instance Applicative (STB bs gs) where |
| 123 | pure = yield |
| 124 | (<*>) = ap |
| 125 | (*>) = (+>) |
| 126 | |
| 127 | instance Monad (STB bs gs) where |
| 128 | return = pure |
| 129 | (>>=) = (+>=) |
| 130 | (>>) = (*>) |
| 131 | |
| 132 | -- the monad's unit |
| 133 | -- |
| 134 | yield :: a -> STB bs gs a |
| 135 | yield a = STB $ \bs gs -> return (bs, gs, Right a) |
| 136 | |
| 137 | -- the monad's bind |
| 138 | -- |
| 139 | -- * exceptions are propagated |
| 140 | -- |
| 141 | (+>=) :: STB bs gs a -> (a -> STB bs gs b) -> STB bs gs b |
| 142 | m +>= k = let |
| 143 | STB m' = m |
| 144 | in |
| 145 | STB $ \bs gs -> m' bs gs >>= \(bs', gs', res) -> |
| 146 | case res of |
| 147 | Left exc -> return (bs', gs', Left exc) -- prop exc |
| 148 | Right a -> let |
| 149 | STB k' = k a |
| 150 | in |
| 151 | k' bs' gs' -- cont |
| 152 | |
| 153 | -- bind dropping the result of the first state transformer |
| 154 | -- |
| 155 | (+>) :: STB bs gs a -> STB bs gs b -> STB bs gs b |
| 156 | k +> m = k +>= const m |
| 157 | |
| 158 | -- fixpoint combinator in the monad |
| 159 | -- |
| 160 | fixSTB :: (a -> STB bs gs a) -> STB bs gs a |
| 161 | -- |
| 162 | -- builds on the fixpoint combinator embedded within the IO monad; the |
| 163 | -- future overall result wrapped into a closure with the function extracting |
| 164 | -- the user-level result component is used to build the cycle |
| 165 | -- |
| 166 | fixSTB m = STB $ \bs gs |
| 167 | -> fixIO (\future -> let |
| 168 | STB m' = m (extractResult future) |
| 169 | in |
| 170 | m' bs gs) |
| 171 | where |
| 172 | extractResult (_, _, Right r) = r |
| 173 | extractResult (_, _, Left _ ) = interr "StateTrans: fixSTB: \ |
| 174 | \Tried to access result \ |
| 175 | \of unsuccessful \ |
| 176 | \recursive computation!" |
| 177 | |
| 178 | |
| 179 | -- generic state manipulation |
| 180 | -- -------------------------- |
| 181 | |
| 182 | -- base state: |
| 183 | -- |
| 184 | |
| 185 | -- given a reader function for the base state, wrap it into an STB monad |
| 186 | -- |
| 187 | readBase :: (bs -> a) -> STB bs gs a |
| 188 | readBase f = STB $ \bs gs -> return (bs, gs, Right (f bs)) |
| 189 | |
| 190 | -- given a new base state, inject it into an STB monad |
| 191 | -- |
| 192 | writeBase :: bs -> STB bs gs () |
| 193 | writeBase bs' = STB $ \_ gs -> return (bs', gs, Right ()) |
| 194 | |
| 195 | -- given a transformer function for the base state, wrap it into an STB monad |
| 196 | -- |
| 197 | transBase :: (bs -> (bs, a)) -> STB bs gs a |
| 198 | transBase f = STB $ \bs gs -> let |
| 199 | (bs', a) = f bs |
| 200 | in |
| 201 | return (bs', gs, Right a) |
| 202 | |
| 203 | -- generic state: |
| 204 | -- |
| 205 | |
| 206 | -- given a reader function for the generic state, wrap it into an STB monad |
| 207 | -- |
| 208 | readGeneric :: (gs -> a) -> STB bs gs a |
| 209 | readGeneric f = STB $ \bs gs -> return (bs, gs, Right (f gs)) |
| 210 | |
| 211 | -- given a new generic state, inject it into an STB monad |
| 212 | -- |
| 213 | writeGeneric :: gs -> STB bs gs () |
| 214 | writeGeneric gs' = STB $ \bs _ -> return (bs, gs', Right ()) |
| 215 | |
| 216 | -- given a transformer function for the generic state, wrap it into an STB |
| 217 | -- monad |
| 218 | -- |
| 219 | transGeneric :: (gs -> (gs, a)) -> STB bs gs a |
| 220 | transGeneric f = STB $ \bs gs -> let |
| 221 | (gs', a) = f gs |
| 222 | in |
| 223 | return (bs, gs', Right a) |
| 224 | |
| 225 | |
| 226 | -- interaction with the encapsulated `IO' monad |
| 227 | -- -------------------------------------------- |
| 228 | |
| 229 | -- lifts an `IO' state transformer into `STB' |
| 230 | -- |
| 231 | liftIO :: IO a -> STB bs gs a |
| 232 | liftIO m = STB $ \bs gs -> m >>= \r -> return (bs, gs, Right r) |
| 233 | |
| 234 | -- given an initial state, executes the `STB' state transformer yielding an |
| 235 | -- `IO' state transformer that must be placed into the context of the external |
| 236 | -- IO |
| 237 | -- |
| 238 | -- * uncaught exceptions become fatal errors |
| 239 | -- |
| 240 | runSTB :: STB bs gs a -> bs -> gs -> IO a |
| 241 | runSTB m bs gs = let |
| 242 | STB m' = m |
| 243 | in |
| 244 | m' bs gs >>= \(_, _, res) -> |
| 245 | case res of |
| 246 | Left (tag, msg) -> let |
| 247 | err = userError ("Exception `" |
| 248 | ++ tag ++ "': " |
| 249 | ++ msg) |
| 250 | in |
| 251 | ioError err |
| 252 | Right a -> return a |
| 253 | |
| 254 | -- interleave the (complete) execution of an `STB' with another generic state |
| 255 | -- component into an `STB' |
| 256 | -- |
| 257 | interleave :: STB bs gs' a -> gs' -> STB bs gs a |
| 258 | interleave m gs' = STB $ let |
| 259 | STB m' = m |
| 260 | in |
| 261 | \bs gs |
| 262 | -> (m' bs gs' >>= \(bs', _, a) -> return (bs', gs, a)) |
| 263 | |
| 264 | |
| 265 | -- error and exception handling |
| 266 | -- ---------------------------- |
| 267 | |
| 268 | -- * we exploit the `UserError' component of `IOError' for fatal errors |
| 269 | -- |
| 270 | -- * we distinguish exceptions and user-defined fatal errors |
| 271 | -- |
| 272 | -- - exceptions are meant to be caught in order to recover the currently |
| 273 | -- executed operation; they turn into fatal errors if they are not caught; |
| 274 | -- exceptions are tagged, which allows to deal with multiple kinds of |
| 275 | -- exceptions at the same time and to handle them differently |
| 276 | -- - user-defined fatal errors abort the currently executed operation, but |
| 277 | -- they may be caught at the top-level in order to terminate gracefully or |
| 278 | -- to invoke another operation; there is no special support for different |
| 279 | -- handling of different kinds of fatal-errors |
| 280 | -- |
| 281 | -- * the costs for fatal error handling are already incurred by the `IO' monad; |
| 282 | -- the costs for exceptions mainly is the case distinction in the definition |
| 283 | -- of `+>=' |
| 284 | -- |
| 285 | |
| 286 | -- throw an exception with the given tag and message (EXPORTED) |
| 287 | -- |
| 288 | throwExc :: String -> String -> STB bs gs a |
| 289 | throwExc tag msg = STB $ \bs gs -> return (bs, gs, Left (tag, msg)) |
| 290 | |
| 291 | -- raise a fatal user-defined error (EXPORTED) |
| 292 | -- |
| 293 | -- * such an error my be caught and handled using `fatalsHandeledBy' |
| 294 | -- |
| 295 | fatal :: String -> STB bs gs a |
| 296 | fatal s = liftIO (ioError (userError s)) |
| 297 | |
| 298 | -- the given state transformer is executed and exceptions with the given tag |
| 299 | -- are caught using the provided handler, which expects to get the exception |
| 300 | -- message (EXPORTED) |
| 301 | -- |
| 302 | -- * the base and generic state observed by the exception handler is *modified* |
| 303 | -- by the failed state transformer upto the point where the exception was |
| 304 | -- thrown (this semantics is the only reasonable when it should be possible |
| 305 | -- to use updating for maintaining the state) |
| 306 | -- |
| 307 | catchExc :: STB bs gs a |
| 308 | -> (String, String -> STB bs gs a) |
| 309 | -> STB bs gs a |
| 310 | catchExc m (tag, handler) = |
| 311 | STB $ \bs gs |
| 312 | -> let |
| 313 | STB m' = m |
| 314 | in |
| 315 | m' bs gs >>= \state@(bs', gs', res) -> |
| 316 | case res of |
| 317 | Left (tag', msg) -> if (tag == tag') -- exception with... |
| 318 | then |
| 319 | let |
| 320 | STB handler' = handler msg |
| 321 | in |
| 322 | handler' bs' gs' -- correct tag, catch |
| 323 | else |
| 324 | return state -- wrong tag, rethrow |
| 325 | Right _ -> return state -- no exception |
| 326 | |
| 327 | -- given a state transformer that may raise fatal errors and an error handler |
| 328 | -- for fatal errors, execute the state transformer and apply the error handler |
| 329 | -- when a fatal error occurs (EXPORTED) |
| 330 | -- |
| 331 | -- * fatal errors are IO monad errors and errors raised by `fatal' as well as |
| 332 | -- uncaught exceptions |
| 333 | -- |
| 334 | -- * the base and generic state observed by the error handler is *in contrast |
| 335 | -- to `catch'* the state *before* the state transformer is applied |
| 336 | -- |
| 337 | fatalsHandledBy :: STB bs gs a -> (IOError -> STB bs gs a) -> STB bs gs a |
| 338 | fatalsHandledBy m handler = |
| 339 | STB $ \bs gs |
| 340 | -> (let |
| 341 | STB m' = m |
| 342 | in |
| 343 | m' bs gs >>= \state@(gs', bs', res) -> |
| 344 | case res of |
| 345 | Left (tag, msg) -> let |
| 346 | err = userError ("Exception `" ++ tag |
| 347 | ++ "': " ++ msg) |
| 348 | in |
| 349 | ioError err |
| 350 | Right a -> return state |
| 351 | ) |
| 352 | `catch` (\err -> let |
| 353 | STB handler' = handler err |
| 354 | in |
| 355 | handler' bs gs) |
| 356 | |
| 357 | |
| 358 | -- list mutable variables and arrays stuff into `STB'; all (EXPORTED) |
| 359 | -- ------------------------------------------------------------------ |
| 360 | |
| 361 | type MVar a = IORef a |
| 362 | |
| 363 | newMV :: a -> STB bs gs (MVar a) |
| 364 | newMV x = liftIO (newIORef x) |
| 365 | |
| 366 | readMV :: MVar a -> STB bs gs a |
| 367 | readMV mv = liftIO (readIORef mv) |
| 368 | |
| 369 | assignMV :: MVar a -> a -> STB bs gs () |
| 370 | assignMV mv x = liftIO (writeIORef mv x) |