Skip to content

Commit

Permalink
some examples of shift0/reset0 that change the return type
Browse files Browse the repository at this point in the history
  • Loading branch information
slindley committed Aug 17, 2015
1 parent 14deaf9 commit a89715e
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion pa/PaHandlers.hs
Expand Up @@ -78,13 +78,62 @@ data Shift0 (a :: *) (i :: *) (o :: *) where
Shift0 :: ((a -> o) -> i) -> Shift0 a i o
type instance Return (Shift0 a i o) = a

shift0 :: Handles h (Shift0 a) => ((a -> j) -> i) -> Comp h i j a
shift0 c = doOp (Shift0 c)

data Reset0 (a :: *) where
Reset0 :: Reset0 a

type instance Result (Reset0 a) = a
instance (Reset0 `Handles` Shift0 a) where
clause (Shift0 p) k Reset0 = p (\x -> k x Reset0)

reset0 :: Comp Reset0 i j j -> i
reset0 m = handle m (\x _ -> x) Reset0

comp2 :: Comp Reset0 Char Bool Bool
comp2 = shift0 (\k -> if k 42 then 'a' else 'b') =>= (\z ->
paReturn $ z > 0)
test2 = reset0 comp2


comp3 :: (Int -> Bool) -> Comp Reset0 String Char Char
comp3 k = shift0 (\l -> (l $ if k 42 then "abc" else "xyz") : "def") =>= (\s ->
paReturn $ head s)
comp3' :: Comp Reset0 (Comp Reset0 String Char Char) Bool Bool
comp3' = shift0 (\k -> comp3 k) =>= (\x ->
paReturn $ x > 0)
comp3'' :: Comp Reset0 String Char Char
comp3'' = reset0 comp3'
test3' :: String
test3' = reset0 comp3''
test3 = reset0
(reset0
(shift0 (\k ->
shift0 (\l -> (l $ if k 32 then "abc" else "xyz") : "def") =>= \s ->
paReturn $ head s) =>= \x ->
paReturn $ x > 0))


comp4 :: (Int -> Bool) -> Comp Reset0 Int String String
comp4 k = shift0 (\l -> length (l $ if k 42 then 'a' else 'x')) =>= \c ->
paReturn $ [c, c]
comp4' :: Comp Reset0 (Comp Reset0 Int String String) Bool Bool
comp4' = shift0 (\k -> comp4 k) =>= \x ->
paReturn $ x > 0
comp4'' :: Comp Reset0 Int String String
comp4'' = reset0 comp4' =>= \y -> paReturn $ y ++ y
test4' :: Int
test4' = reset0 comp4''
test4 = reset0
(reset0
(shift0 (\k ->
shift0 (\l -> length (l $ if k 42 then 'a' else 'x')) =>= \s ->
paReturn [s,s]) =>= \x ->
paReturn $ x > 0) =>= \y ->
paReturn $ y ++ y)


-- Forwarding is where things start to get interesting. Here's a
-- useless example of forwarding. This approach isn't going to scale
-- because it doesn't allow states to be transmitted across the
Expand Down

0 comments on commit a89715e

Please sign in to comment.