Advertisement
elvecent

continuations Kleisli & monad example

Nov 18th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. main = print . evalCont $ (lazySum <== lazySq <== liftCont product) [[1,2,3],[4,5]]
  2.  
  3. data Cont r a = Cont ((a -> r) -> r)
  4.  
  5. runCont :: Cont r a -> (a -> r) -> r
  6. runCont (Cont f) = f
  7.  
  8. lazySum :: [[Int]] -> Cont Int [Int]
  9. lazySum xs = Cont $ \f -> sum $ map f xs
  10.  
  11. square x = x * x
  12.  
  13. lazySq :: [Int] -> Cont Int [Int]
  14. lazySq xs = Cont $ \f -> square $ f xs
  15.  
  16. contpose :: (a -> Cont r b)
  17.          -> (b -> Cont r c)
  18.          -> (a -> Cont r c)
  19. contpose c1 c2 ys = Cont $ \f -> (runCont $ c1 ys) (\xs -> runCont (c2 xs) f)
  20.  
  21. contFish :: Cont r a -> (a -> Cont r b) -> Cont r b
  22. contFish c f = (contpose (\_ -> c) f) ()
  23.  
  24. contRet :: a -> Cont r a
  25. contRet x = Cont $ \f -> f x
  26.  
  27. liftCont :: (a -> b) -> a -> Cont r b
  28. liftCont f = \x -> contRet $ f x
  29.  
  30. contposeAll (c:[]) = c
  31. contposeAll (c1:cs) = contpose c1 (contposeAll cs)
  32.  
  33. evalCont :: Cont a a -> a
  34. evalCont c = runCont c id
  35.  
  36. (<==) = contpose
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement