Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this makes samples for Asai paper to work #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Control/Monad/Indexed/Cont.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 }
Expand Down
42 changes: 42 additions & 0 deletions examples/Asai.hs
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion indexed-extras.cabal
Original file line number Diff line number Diff line change
@@ -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
Expand Down