diff --git a/Control/Monad/Indexed/Cont.hs b/Control/Monad/Indexed/Cont.hs index 3f05c41..473dee5 100644 --- a/Control/Monad/Indexed/Cont.hs +++ b/Control/Monad/Indexed/Cont.hs @@ -8,6 +8,7 @@ -- Stability : experimental -- Portability : rank-2 Types required for correctness of shift, but they can be removed ------------------------------------------------------------------------------------------- +{-# LANGUAGE RankNTypes #-} module Control.Monad.Indexed.Cont ( IxMonadCont(reset, shift) , IxContT(IxContT, runIxContT) @@ -28,7 +29,7 @@ import Control.Monad.Indexed.Trans class IxMonad m => IxMonadCont m where reset :: m a o o -> m r r a - shift :: (forall i. (a -> m i i o) -> m r j j) -> m r o a + shift :: ((forall i . a -> m i i o) -> m r j j) -> m r o a -- shift :: ((a -> m i i o) -> m r j j) -> m r o a newtype IxContT m r o a = IxContT { runIxContT :: (a -> m o) -> m r } diff --git a/examples/Asai.hs b/examples/Asai.hs new file mode 100644 index 0000000..1f5c4ab --- /dev/null +++ b/examples/Asai.hs @@ -0,0 +1,42 @@ +{- + +Samples from "Polymorphic Delimited Continuations" (Kenichi Asai and Yukiyoshi Kameyama) + +-} +{-# LANGUAGE PackageImports #-} +module Asai where + +import "indexed-extras" Control.Monad.Indexed.Cont +import "indexed" Control.Monad.Indexed +import Control.Monad.Identity + +visit :: IxMonadCont m => [a] -> m [b] b [a] +visit [] = shift (\h -> ireturn []) +visit (a : r) = shift (\k -> k [] >>>= \ph -> reset (visit r >>>= k ) + >>>= \pt -> ireturn (ph : pt) + ) >>>= ireturn . (a:) + +prefix :: IxMonadCont m => [a] -> m j j [[a]] +prefix = reset . visit + +sh :: (Show b, IxMonadCont m) => m (b -> m i i a) a String +sh = shift (\k -> ireturn (\x -> k (show x))) + +str :: IxMonadCont m => m (String -> m i i a) a String +str = shift (\k -> ireturn (\x -> k x)) + +lit :: IxMonadCont m => String -> m i i String +lit = ireturn + +printf :: IxMonadCont m => m a String String -> m i i a +printf = reset + +iapj :: IxMonad m => m i j (a -> m j k b) -> a -> m i k b +iapj l r = l >>>= \f -> f r + +(+++) :: IxMonad m => m i j [a] -> m j k [a] -> m i k [a] +l +++ r = ireturn (++) `iap` l `iap` r + +printfTest = runIdentity $ runIxContT_ $ + printf (lit "Hello, " +++ str +++ lit ", at " +++ sh) + `iapj` "World" `iapj` 10 diff --git a/indexed-extras.cabal b/indexed-extras.cabal index faed5d3..230f242 100644 --- a/indexed-extras.cabal +++ b/indexed-extras.cabal @@ -1,5 +1,5 @@ Name: indexed-extras -Version: 0.1.1 +Version: 0.1.2 Synopsis: Indexed functors, monads and comonads that require extensions to Haskell98 Description: Indexed functors, monads and comonads that require extensiosn to Haskell98 License: BSD3