Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Control.Applicative
  2. import Data.Monoid
  3. import Data.Maybe
  4.  
  5. -- Task 1
  6. data Tree a = Nil | Branch (Tree a) a (Tree a) deriving Show
  7.  
  8. instance Functor Tree where
  9.     fmap f Nil = Nil
  10.     fmap f (Branch (left) root (right)) = Branch (fmap f left) (f root) (fmap f right)
  11.    
  12. instance Applicative Tree where
  13.     pure x = Branch (pure x) x (pure x)
  14.     Nil <*> _ = Nil
  15.     _ <*> Nil = Nil
  16.     (Branch (left_f) root_f (right_f)) <*> (Branch (left) root (right)) =
  17.         Branch (left_f <*> left) (root_f root) (right_f <*> right)
  18.        
  19. check1 = (+) <$> (Branch (Branch Nil 1 Nil) 2 Nil) <*> (Branch (Branch Nil 3 Nil) 4 (Branch Nil 5 Nil))
  20.  
  21. -- Task 2
  22. x1 >$< x2 = getZipList(x1 <$> ZipList x2)
  23. x1 >*< x2 = getZipList(ZipList x1 <*> ZipList x2)
  24.  
  25. -- Task 3
  26. newtype Compose f g x = Compose {getCompose :: f (g x)} deriving Show
  27.  
  28. example :: Compose Tree [] Integer
  29. example = Compose (Branch (Branch (Nil) [1] (Nil)) [2] (Branch (Nil) [3] (Nil)))
  30.  
  31. ffmap h = getCompose . fmap h . Compose
  32. -- ffmap :: Functor (Compose f g) => (a -> x) -> f (g a) -> f (g x)
  33. {--
  34.     Получили почти fmap, только на этот раз у нас имеется однопараметрический контейнер,
  35.     составленный из композиции f и g.
  36. --}
  37.  
  38. -- ffmap (+42) $ Just [1,2,3]
  39. -- Нужно объявить Functor для Compose f g
  40. instance (Functor f, Functor g) => Functor (Compose f g) where
  41.     fmap f = Compose . fmap (fmap f) . getCompose
  42.  
  43. check2 = ffmap (+42) $ Just [1,2,3]
  44. check3 = ffmap (+42) $ [Just 1,Just 2,Nothing]
  45.  
  46. instance (Applicative f, Applicative g) => Applicative (Compose f g) where
  47.     pure x = (Compose . pure . pure) x
  48.     xs <*> ys = Compose ((<*>) <$> (getCompose xs) <*> (getCompose ys))
  49.    
  50. check4 =  getCompose $ (+) <$> Compose [Just 1,Just 2] <*> Compose [Nothing,Just 40]
  51. check5 = getCompose $ (+) <$> Compose [Just 1,Just 2] <*> Compose [Just 30,Just 40]
  52. check6 = getCompose $ Compose [[(+1)],[(+2),(+3)]] <*> Compose [[10,20],[]]
  53.  
  54. -- Task 4
  55. {--
  56.     Maybe:
  57.    
  58.     instance Applicative Maybe where
  59.         pure = Just
  60.         Nothing <*> _ = Nothing
  61.         (Just g) <*> x = fmap g x
  62.        
  63.     instance Functor Maybe where
  64.         fmap _ Nothing = Nothing
  65.         fmap g (Just a) = Just (g a)
  66.        
  67.     1) pure id <*> v = v
  68.         pure id <*> v = Just id <*> v = fmap id v
  69.         1.1) v == Nothing => fmap id Nothing = Nothing
  70.         1.2) v == Just a => fmap id (Just a) = Just (id a) = Just a = v
  71.        
  72.     2) pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
  73.         2.1) u == Nothing || v == Nothing || w == Nothing => Nothing == Nothing
  74.         2.2) u = Just a, v = Just b, w = Just c
  75.             левая часть:
  76.             pure (.) <*> (Just a) <*> (Just b) <*> (Just c) =
  77.             Just (.) <*> (Just a) <*> (Just b) <*> (Just c) =
  78.             fmap (.) (Just a) <*> (Just b) <*> (Just c) =
  79.             (Just ((.) a)) <*> (Just b) <*> (Just c) =
  80.             (fmap ((. a)) (Just b))) <*> (Just c) =
  81.             (Just ((. a) (Just b))) <*> (Just c) =
  82.             (Just (a . b)) <*> (Just c) =
  83.             fmap (a . b) (Just c) =
  84.             Just ((a . b) c) =
  85.             Just (a (b c))
  86.            
  87.             правая часть:
  88.             (Just a) <*> ((Just b) <*> (Just c)) =
  89.             fmap a ((Just b) <*> (Just c)) =
  90.             fmap a (fmap b (Just c)) =
  91.             fmap a (Just (b c)) =
  92.             Just (a (b c))
  93.            
  94.     3) pure g <*> pure x = pure (g x)
  95.         левая часть:
  96.         pure g <*> pure x = Just g <*> Just x = fmap g (Just x) = Just (g x) = pure (Just x)
  97.        
  98.     4) g <*> pure x = pure ($ x) <*> g
  99.         4.1) g == Nothing => с обоих сторон Nothing
  100.         4.2) g == Just a
  101.         левая часть:
  102.         g <*> pure x = g <*> Just x = Just a <*> Just x = fmap a (Just x) = Just (a x)
  103.        
  104.         правая часть:
  105.         pure ($ x) <*> g = Just ($ x) <*> g = fmap ($ x) g =
  106.         fmap ($ x) (Just a) = Just (($ x) a) = Just (a x)
  107.        
  108.        
  109.     List
  110.    
  111.     instance Functor [] where
  112.         fmap _ [] = []
  113.         fmap g (x:xs) = g x : fmap g xs
  114.  
  115.     instance Applicative [] where
  116.         pure x = [x]
  117.         gs <*> xs = [ g x | g <- gs, x <- xs ]
  118.  
  119.     1) pure id <*> v = v
  120.         pure id <*> v = [id] <*> v = [id x | x <- v] = [x | x <- v] = v
  121.        
  122.     2) pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
  123.         левая часть:
  124.         pure (.) <*> u <*> v <*> w =
  125.         [(.)] <*> u <*> v <*> w =
  126.         [(. x) | x <- u] <*> v <*> w =
  127.         [(x . y) | x <- u, y <- v] <*> w =
  128.         [(x . y) z | x <- u, y <- v, z <- w] =
  129.         [x (y z) | x <- u, y <- v, z <- w]
  130.        
  131.         правая часть:
  132.         u <*> (v <*> w) =
  133.         u <*> [y z | y <- v, z <- w] =
  134.         [x (y z) | x <- u, y <- v, z <- w]
  135.        
  136.     3) pure g <*> pure x = pure (g x)
  137.         левая часть:
  138.         pure g <*> pure x = [g] <*> [x] = [g x]
  139.        
  140.         правая часть:
  141.         pure (g x) = [g x]
  142.        
  143.     4) g <*> pure x = pure ($ x) <*> g
  144.         левая часть:
  145.         g <*> pure x = g <*> [x] =
  146.         [G x | G <- g]
  147.        
  148.         правая часть:
  149.         pure ($ x) <*> g =
  150.         [$ x] <*> g =
  151.         [($ x) G | G <- g] =
  152.         [G x | G <- g]
  153. --}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement