Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Logger where
  2.  
  3. import           Control.Arrow
  4. import           Data.Function
  5.  
  6. {-
  7.  
  8. f n = (n * f (n - 1), "recursion")
  9. f 0 = (1, "start")
  10.  
  11. type of f is not correct!
  12. f :: Int -> (Int, String)
  13. f :: Int -> Int
  14.  
  15. \n ->
  16. f n = (m, s)
  17. g m = (k, s2)
  18. return (k, s ++ s2)
  19.  
  20.  
  21. -- f x y = 3 * (x + y)
  22. -- f = (.(* 3)) . (+)
  23.  
  24. -- f1 x y z = z + 3 * (x + y)
  25. -- f1 = (+) . (.(* 3)) . (+)
  26.  
  27. -- let s4= (((+).).) . ((+).) . (+)
  28.  
  29. -}
  30. newtype Cont r a = Cont
  31.     { unC :: (a -> r) -> r
  32.     }
  33.  
  34. bindC :: ((a -> r) -> r) -> (a -> (b -> r) -> r) -> (b -> r) -> r
  35. bindC a f = \x -> a $ (($x) . f)
  36.  
  37. applyC :: (((a -> b) -> r) -> r) -> ((a -> r) -> r) -> (b -> r) -> r
  38. applyC f c = f . (.) c . (.) -- (\beta -> c (rho . beta))
  39.  
  40. instance Functor (Cont r)
  41.     --fmap :: (a -> b) -> Cont r a -> Cont r b
  42.                                                where
  43.     fmap f (Cont c) = Cont (\b_r -> c $ b_r . f) -- Cont $ c.(.f)
  44.  
  45. instance Applicative (Cont r) where
  46.     pure = Cont . (flip ($))
  47.     (Cont f) <*> (Cont c) = Cont $ \bToR -> f (\aToB -> c (bToR . aToB))
  48.  
  49. instance Monad (Cont r) where
  50.     return a = Cont $ \f -> f a
  51.     (Cont c) >>= f = Cont $ \g -> c (\a -> unC (f a) g)
  52.  
  53. newtype St a val = St
  54.     { unSt :: a -> (val, a)
  55.     }
  56.  
  57. type State = St String
  58.  
  59. bind2 :: St x a -> (a -> St x b) -> St x b
  60. f `bind2` g =
  61.     St $ \a ->
  62.         case unSt f a of
  63.             (a', str) ->
  64.                case unSt (g a') str of
  65.                     (a'', str') -> (a'', str')
  66.  
  67. ret :: v -> St a v
  68. ret v = St (\a -> (v, a))
  69.  
  70. set :: s -> St s s
  71. set s = St (\_ -> (s, s))
  72.  
  73. get :: St s s
  74. get = St (\s -> (s, s))
  75.  
  76. instance Functor (St a) where
  77.     fmap f (St s) =
  78.         St
  79.             (\a ->
  80.                  case s a of
  81.                      (v, a') -> (f v, a'))
  82.  
  83. instance Applicative (St a) where
  84.     (St f) <*> (St s) =
  85.         St
  86.             (\a ->
  87.                  case f a of
  88.                      (fun, _) ->
  89.                          case s a of
  90.                              (v, a'') -> (fun v, a''))
  91.     pure v = St (\a -> (v, a))
  92.  
  93. instance Monad (St a) where
  94.     (>>=) = bind2
  95.     return = ret
  96.  
  97. newtype RSt s a = RSt
  98.     { unRSt :: s -> (s, a)
  99.     }
  100.  
  101. modifyR :: (s -> s) -> RSt s ()
  102. modifyR f = RSt (\s -> (f s, ()))
  103.  
  104. setR :: s -> RSt s ()
  105. setR s = RSt (\_ -> (s, ()))
  106.  
  107. getR :: RSt s s
  108. getR = RSt (\s -> (s, s))
  109.  
  110. instance Functor (RSt s) where
  111.     fmap f (RSt x) =
  112.         RSt $ \s ->
  113.             case x s of
  114.                 (s', a) -> (s', f a)
  115.  
  116. instance Applicative (RSt s) where
  117.     pure a = RSt $ \s -> (s, a)
  118.     (RSt f) <*> (RSt s) =
  119.         RSt
  120.             (\a ->
  121.                  case f a of
  122.                      (_, fun) ->
  123.                          case s a of
  124.                              (a'', v) -> (a'', fun v))
  125.  
  126. instance Monad (RSt s) where
  127.     (RSt x) >>= f =
  128.         RSt $ \s ->
  129.             let (state2, res1) = x state1
  130.                 (state1, res2) = unRSt (f res1) s
  131.              in (state2, res2)
  132.  
  133. {-
  134.     return x >>= f = f x
  135.     a >>= return = a
  136.     (a >>= f) >>= g = a >>= \ x -> f x >>= g
  137. return :: a -> State a
  138. return x = St $ \a -> (x, a)
  139. -}
  140. newtype R e a = R
  141.     { unR :: e -> a
  142.     }
  143.  
  144. instance Functor (R e) where
  145.     fmap f (R a) = R $ f . a
  146.  
  147. instance Applicative (R e) where
  148.     pure x = R $ const x
  149.     (R f) <*> (R x) = R $ \e -> f e (x e)
  150.  
  151. instance Monad (R e) where
  152.     return x = R $ const x
  153.     -- bind :: (e -> a) -> (a -> e -> b) -> e -> b
  154.     (R x) >>= f = R (\e -> unR (f (x e)) e)
  155.  
  156. newtype A a v = A
  157.     { unA :: (a, v)
  158.     }
  159.  
  160. instance Functor (A a) where
  161.     fmap f (A (a, v)) = A (a, f v)
  162.  
  163. instance Monoid a => Applicative (A a) where
  164.     pure v = A (mempty, v)
  165.     (A (a, f)) <*> (A (a', v')) = A (a <> a', f v')
  166.  
  167. instance Monoid a => Monad (A a) where
  168.     A (u, v) >>= k =
  169.         case k v of
  170.             A (v1, b) -> A (u <> v1, b)
  171.  
  172. -- fix f = let x = f x in x
  173. f n k =
  174.     if n == 0
  175.         then 1
  176.         else n * f (n - 1) (k - 1)
  177.  
  178. fix' (f) =
  179.    let x = f x
  180.     in f (f x)
  181. {-
  182. f 3 == \k -> 3 * f 2 (k - 1)
  183.  
  184.  
  185.  
  186.  
  187. * @'return' a '>>=' k  =  k a@
  188. * @m '>>=' 'return'  =  m@
  189. * @m '>>=' (\\x -> k x '>>=' h)  =  (m '>>=' k) '>>=' h@
  190. -}
  191. -- [/identity/]
  192. --
  193. --      @'pure' 'id' '<*>' v = v@
  194. --
  195. -- [/composition/]
  196. --
  197. --      @'pure' (.) '<*>' u '<*>' v '<*>' w = u '<*>' (v '<*>' w)@
  198. --
  199. -- [/homomorphism/]
  200. --
  201. --      @'pure' f '<*>' 'pure' x = 'pure' (f x)@
  202. --
  203. -- [/interchange/]
  204. --
  205. --      @u '<*>' 'pure' y = 'pure' ('$' y) '<*>' u@
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement