[auto ekmett@gmail.com**20090504085200 Ignore-this: 216afc4d1489a179234a10aa0ef79b69 ] { addfile ./doc/html/parsimony/Text-Parsimony-Grammar-Monad.html addfile ./doc/html/parsimony/src/Text-Parsimony-Grammar-Monad.html hunk ./doc/html/parsimony/Text-Parsimony-Grammar-Monad.html 1 + + +Text.Parsimony.Grammar.Monad
 parsimony-0.0.1: Monoidal parser combinators for parallel parsing of context-free languagesSource codeContentsIndex
Text.Parsimony.Grammar.Monad
Portabilitynon-portable (MPTCs, rank-2)
Stabilityexperimental
Maintainerekmett@gmail.com
Description
Documentation
fresh :: S f IntSource
newtype S f a Source
Constructors
S
getS :: forall o. (a -> StableMap f -> Int# -> State# RealWorld -> o) -> StableMap f -> Int# -> State# RealWorld -> o
show/hide Instances
runS :: S f a -> IO (a, StableMap f, Int)Source
Produced by Haddock version 2.3.0
hunk ./doc/html/parsimony/Text-Parsimony-StableMap.html 92 +>
show/hide Instances
freshText.Parsimony.Grammar.MonadgetSText.Parsimony.Grammar.MonadrunSText.Parsimony.Grammar.MonadS1 (Type/Class)Text.Parsimony.Grammar.Monad2 (Data Constructor)Text.Parsimony.Grammar.MonadInterpreterGrammarText.Parsimony.Grammar.Monadshow/hideInterpreter
+ + + +Text/Parsimony/Grammar/Monad.hs + + + +
{-# LANGUAGE MagicHash, FlexibleContexts, FlexibleInstances, Rank2Types, MultiParamTypeClasses #-}
+{-# OPTIONS_GHC -fglasgow-exts #-}
+
+-----------------------------------------------------------------------------
+-- |
+-- Module      :  Text.Parsimony.Grammar.Monad
+-- Copyright   :  (c) Edward Kmett 2009
+-- License     :  BSD
+-- Maintainer  :  ekmett@gmail.com
+-- Stability   :  experimental
+-- Portability :  non-portable (MPTCs, rank-2)
+--
+-----------------------------------------------------------------------------
+
+module Text.Parsimony.Grammar.Monad 
+    ( fresh
+    , S(..)
+    , runS
+    ) where
+
+import Control.Applicative
+import Control.Monad
+import Control.Monad.State.Class
+import Control.Monad.Trans
+import GHC.ST (STret(..))
+import GHC.Exts
+import GHC.Prim
+import GHC.IOBase
+import Text.Parsimony.StableMap as StableMap
+
+newtype S f a = S { getS :: forall o. (a -> StableMap f -> Int# -> State# RealWorld -> o) -> StableMap f -> Int# -> State# RealWorld -> o } 
+
+instance Functor (S f) where
+    fmap f g = S (\k -> getS g (k . f))
+
+instance Applicative (S f) where
+    pure = return
+    (<*>) = ap
+
+instance Monad (S f) where
+    return a = S (\k -> k a)
+    S g >>= f = S (\k -> g (\a -> getS (f a) k))
+
+instance MonadState (StableMap f) (S f) where
+    get = S (\k s -> k s s)
+    put s = S (\k _ -> k () s)
+
+fresh :: S f Int
+fresh = S (\k s i -> k (I# i) s (i +# 1#))
+
+instance MonadIO (S f) where
+    liftIO (IO f) = S (\k s i w -> 
+        case f w of 
+            (# w', a #) -> k a s i w')
+
+runS :: S f a -> IO (a, StableMap f, Int)
+runS (S k) = IO (\w -> 
+    case k (\a s i w' -> STret w' (a, s, I# i)) StableMap.empty 1# w of 
+        STret w'' b -> (# w'', b #)) 
+
+ }