monad-stlike-io (empty) → 0.1
raw patch · 6 files changed
+196/−0 lines, 6 filesdep +basedep +bytestringdep +containerssetup-changed
Dependencies added: base, bytestring, containers, deepseq, transformers
Files
- COPYING +29/−0
- Control/Monad/STLike/IO.hs +59/−0
- Control/Monad/STLike/Internal.hs +80/−0
- Control/Monad/STLike/Unsafe.hs +8/−0
- Setup.hs +3/−0
- monad-stlike-io.cabal +17/−0
+ COPYING view
@@ -0,0 +1,29 @@+Copyright (c) 2010, Taru Karttunen+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 the Taru Karttunen; nor the names of its 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.
+ Control/Monad/STLike/IO.hs view
@@ -0,0 +1,59 @@+module Control.Monad.STLike.IO+ (-- * IOS monad+ IOS, io, runIOS+ -- * Regioned monad+ ,Regioned, runRegion, region+ -- * Utilities+ ,rbsFromPtr,rbsToBS,withRbsPtr,rbsMapLookup+ ) where++import Control.Monad.STLike.Internal+import Control.Monad.Trans+import qualified Data.ByteString as B+import qualified Data.ByteString.Unsafe as B+import qualified Data.Map as M+import Foreign+++-- | Monad for scoped IO computations+-- The underlying monad must be strict here.+type IOS s t = STLike IO s t+instance STLikeImpl IO++instance MonadIO (STLike IO s) where+ liftIO x = io x++-- | Lift IO computations into IOS. liftIO also works.+io :: IO t -> IOS s t+io x = STLike x++-- | Run an IOS computation in the IO monad.+runIOS :: (forall s. IOS s t) -> IO t+runIOS x = let STLike v = x in v+++-- | Create a ByteString representing the pointer and length. +-- No copying done, O(1).+rbsFromPtr :: Ptr a -> Int -> IOS s (Regioned s B.ByteString)+rbsFromPtr ptr len = io $ fmap R $ B.unsafePackCStringLen (castPtr ptr,len)++-- | Create a copy of a regioned ByteString as a normal ByteString. O(n).+rbsToBS :: STLikeImpl m => Regioned s B.ByteString -> STLike m s B.ByteString+rbsToBS (R b) = return $! B.copy b++-- | Use a regioned ByteString as a pointer. O(1). +-- The pointer points to the region contents,+-- so be cafeful with it.+withRbsPtr :: Regioned s B.ByteString -> (Ptr any -> Int -> IOS s t) -> IOS s t+--withRbsPtr (R b) act = io $ B.unsafeUseAsCStringLen b (\(p,l) -> unsafeSTToIO (act (castPtr p) l))+withRbsPtr (R b) act = io $ B.unsafeUseAsCStringLen b (\(p,l) -> let STLike v = (act (castPtr p) l) in v)++-- | Lookup inside a Map with a regioned ByteString.+rbsMapLookup :: (STLikeImpl m, Ord key) => Regioned s key -> M.Map key value -> STLike m s (Maybe value)+rbsMapLookup (R k) m = case M.lookup k m of+ Just x -> return (Just x)+ Nothing -> return Nothing++-- | Regions a value. Synonym for /return/.+region :: t -> Regioned s t+region = R
+ Control/Monad/STLike/Internal.hs view
@@ -0,0 +1,80 @@+module Control.Monad.STLike.Internal where++import Control.DeepSeq+import Control.Monad+import Foreign++-- | Regioned variables.+-- A regioned variable is /safe/ i.e. no references to+-- it may escape the current IOS.+newtype Regioned s t = R t++instance Monad (Regioned s) where+ return = R+ _ >> b = b+ (R v) >>= f = f v+instance Functor (Regioned s) where+ fmap f (R v) = R (f v)++-- | Run a computation on regioned data+-- and return the result in a strict fashion.+runRegion :: (NotShared ty, STLikeImpl m) => Regioned s ty -> STLike m s ty+runRegion = runRegionImpl+++class NFData ty => NotShared ty where+ runRegionImpl :: STLikeImpl m => Regioned s ty -> STLike m s ty+ runRegionImpl (R v) = v `deepseq` return v++instance NotShared Bool+instance NotShared Char+instance NotShared Double+instance NotShared Float+instance NotShared Int+instance NotShared Int8+instance NotShared Int16+instance NotShared Int32+instance NotShared Int64+instance NotShared Integer+instance NotShared Word+instance NotShared Word8+instance NotShared Word16+instance NotShared Word32+instance NotShared Word64+instance NotShared ()+--instance NotShared IntSet+instance NotShared a => NotShared ([] a)+--instance (Integral a, NotShared a) => NotShared (Ratio a)+--instance (RealFloat a, NotShared a) => NotShared (Complex a)+instance NotShared a => NotShared (Maybe a)+--NotShared a => NotShared (IntMap a)+--NotShared a => NotShared (Tree a)+--NotShared a => NotShared (Set a)+instance (NotShared a, NotShared b) => NotShared (Either a b)+instance (NotShared a, NotShared b) => NotShared ((,) a b)+--(Ix a, NotShared a, NotShared b) => NotShared (Array a b)+--(NotShared k, NotShared a) => NotShared (Map k a)+instance (NotShared a, NotShared b, NotShared c) => NotShared ((,,) a b c)+instance (NotShared a, NotShared b, NotShared c, NotShared d) => NotShared ((,,,) a b c d)+instance (NotShared a1, NotShared a2, NotShared a3, NotShared a4, NotShared a5) => NotShared ((,,,,) a1 a2 a3 a4 a5)+--instance NotShared B.ByteString where+-- runRegion (R bs) = return $! B.copy bs+++unsafeRemoveRegion :: STLikeImpl m => Regioned s r -> STLike m s r+unsafeRemoveRegion (R x) = return x++newtype STLike m s t = STLike (m t)++class Monad m => STLikeImpl m++instance STLikeImpl m => Monad (STLike m s) where+ (STLike a) >> (STLike b) = STLike (a >> b)+ (STLike a) >>= b = STLike $ do v <- a+ let STLike r = b v+ r+ return x = STLike (return x)++instance (STLikeImpl m) => Functor (STLike m s) where+ fmap f (STLike m) = STLike (liftM f m)+
+ Control/Monad/STLike/Unsafe.hs view
@@ -0,0 +1,8 @@+{-# OPTIONS_HADDOCK prune #-}+-- | Unsafe intersafe for implementators.+-- Purposefully not documented.+module Control.Monad.STLike.Unsafe+ (STLike(..), STLikeImpl, NotShared(..), unsafeRemoveRegion+ ) where++import Control.Monad.STLike.Internal
+ Setup.hs view
@@ -0,0 +1,3 @@+#!/usr/bin/env runhaskell+import Distribution.Simple+main = defaultMain
+ monad-stlike-io.cabal view
@@ -0,0 +1,17 @@+Name: monad-stlike-io+Version: 0.1+Synopsis: ST-like monad capturing variables to regions and supporting IO.+Description: ST-like monad capturing variables to regions and supporting IO.+License: BSD3+License-file: COPYING+Copyright: Taru Karttunen <taruti@taruti.net>+Author: Taru Karttunen+Category: Regions, Monads+Maintainer: taruti@taruti.net+Build-Depends: base >= 4 && < 5, bytestring, containers, deepseq, transformers+Exposed-modules: Control.Monad.STLike.IO,+ Control.Monad.STLike.Unsafe+Other-modules: Control.Monad.STLike.Internal+Extensions: RankNTypes, FlexibleInstances+GHC-Options: -Wall+Build-Type: Simple