fork download
  1. import Prelude hiding (const, id, (.))
  2. import Control.Category
  3.  
  4. data St a b = St (a -> (b, St a b))
  5.  
  6. ap :: St a b -> [a] -> [b]
  7. ap _ [] = []
  8. ap (St f) (a:as) = let (b, nSt) = f a in b : ap nSt as
  9.  
  10. const :: a -> St b a
  11. const x = St $ \_ -> (x, const x)
  12.  
  13. integral :: Num a => St a a
  14. integral = from 0 where
  15. from n = St $ \x -> st x where
  16. st x = let s = x + n in (s, from s)
  17.  
  18. instance Category St where
  19. -- id :: St a a
  20. id = St $ (\x -> (x, id))
  21. -- (.) :: St b c -> St a b -> St a c
  22. (.) (St f) (St g) = St $ \x -> compose x where
  23. compose x = (x2, f1 . g1) where
  24. (x1, g1) = g x
  25. (x2, f1) = f x1
  26.  
  27. main = do
  28. print $ ap (integral . id) [0,1,2] == ap (id . integral) [0,1,2]
  29. print $ ap (integral . (const 42 . integral)) [0,1,2] == ap ((integral . const 42) . integral) [0,1,2]
  30. print $ ap (const 42) [0,1,2] == [42,42,42]
  31. print $ ap integral [0,1,2,3] == [0,1,3,6]
  32.  
Success #stdin #stdout 0s 8388607KB
stdin
Standard input is empty
stdout
True
True
True
True