tomasfdel

ALP Práctica 6

Nov 24th, 2018
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haskell 16.25 KB | None | 0 0
  1. -- EJERCICIO 1
  2. newtype Id a = Id a
  3.  
  4. instance Monad Id where
  5.     return x = Id x
  6.     (Id x) >>= f = f x
  7.  
  8. {-
  9. 1- return x >>= f = Id x >>= f = f x
  10. 2- (Id x) >>= return = return x = Id x
  11. 3- ((Id x) >>= f) >>= g = f x >>= g = (\x -> f x >>= g) x = (Id x) >>= (\x -> f x >>= g)
  12. -}
  13.  
  14. newtype Maybe a = Nothing | Just a
  15.  
  16. instance Monad Maybe where
  17.     return = Just
  18.     Nothing >>= f = Nothing
  19.     (Just x) >>= f = f x
  20.  
  21. {-
  22. 1- return x >>= f = Just x >>= f = f x
  23. 2- Nothing >>= return = Nothing
  24.    (Just x) >>= return = return x = Just x
  25. 3- (Nothing >>= f) >>= g = Nothing >>= g = Nothing = Nothing >>= (\x -> f x >>= g)
  26.    ((Just x) >>= f) >>= g = (f x) >>= g = (\x -> f x >>= g) x = (Just x) >>= (\x -> f x >>= g)
  27. -}
  28.  
  29.  
  30. -- EJERCICIO 2
  31. instance Monad [] where
  32.     return x = [x]
  33.     [] >>= f = []
  34.     (x:xs) >>= f = (f x) ++ (xs >>= f)
  35.  
  36. {-
  37. LEMA: (xs ++ ys) >>= f = xs >>= f ++ ys >>= g
  38. Por inducción en xs:
  39.    ([] ++ ys) >>= f =
  40.    ys >>= f =
  41.    [] ++ ys >>= f =
  42.    [] >>= f ++ ys >>= f
  43.    ((x:xs) ++ ys) >>= f =
  44.    (x : (xs ++ ys)) >>= f =
  45.    (f x) ++ (xs ++ ys) >>= f =
  46.    (f x) ++ xs >>= f ++ ys >>= f =
  47.    ((f x) ++ xs >>= f) ++ ys >>= f =
  48.    (x:xs) >>= f ++ ys >>= f
  49. DEMOSTRACION:
  50. 1- return x >>= f = [x] >>= f = (x:[]) >>= f = (f x) ++ ([] >>= f) = (f x) ++ [] = f x
  51. 2- [] >>= return = []
  52.    (x:xs) >>= return = (return x) ++ (xs >>= return) = [x] ++ (xs >>= return) = [x] ++ xs = x:xs
  53. 3- ([] >>= f) >>= g = [] >>= g = [] = [] >>= (\x -> f x >>= g)
  54.    ((x:xs) >>= f) >>= g = ((f x) ++ (xs >>= f)) >>= g =
  55.    (f x) >>= g ++ (xs >>= f) >>= g =
  56.    (\x f x >>= g) x ++ (xs >>= (\x -> f x >>= g)) =
  57.    (x:xs) >>= (\x f x >>= g)
  58. -}
  59.  
  60.  
  61. -- EJERCICIO 3
  62. -- (La demostración está copiada del TP 4 y no tengo ganas de cambiarla para que corresponda al ejercicio)
  63. newtype State a = State {runState :: Env -> (a, Env)}
  64.  
  65. instance Monad State where
  66.     return x = State (\s -> (x, s))
  67.     m >>= f = State (\s -> let (v, s') = runState m s
  68.                           in runState (f v) s')
  69.  
  70. -- Monad 1
  71. return y >>= f =
  72. -- {Definicion de return}
  73. State (\s -> (y, s)) >>= f =
  74. -- {Definicion de >>=}
  75. State (\s -> let (v, s') = runState (State (\s -> (y, s))) s
  76.              in runState (f v) s') =                          
  77. -- {Definicion de runState}
  78. State (\s -> let (v, s') = (\s -> (y, s)) s
  79.              in runState (f v) s') =                          
  80. -- {Aplicacion de funcion anonima}
  81. State (\s -> let (v, s') = (y, s)
  82.              in runState (f v) s') =                          
  83. -- {Sustitucion de let}
  84. State (\s -> runState (f y) s) =                              
  85. -- {Eta-reduccion}
  86. State (runState (f y)) =                                      
  87. -- {Sea x = State z. Luego, State (runState x) = State z = x}
  88. -- {Entonces, la composicion de State y runState es la identidad}
  89. f y
  90.  
  91. -- Monad 2
  92. m >>= return =                                                
  93. -- {Definicion de >>=}
  94. State (\s -> let (v, s') = runState m s
  95.              in runstate (return v) s') =                    
  96. -- {Definicion de return}
  97. State (\s -> let (v, s') = runState m s
  98.              in runstate (State (\s -> (v, s))) s') =        
  99. -- {Definicion de runState}
  100. State (\s -> let (v, s') = runState m s
  101.              in (\s -> (v, s)) s') =                          
  102. -- {Aplicacion de funcion anonima}
  103. State (\s -> let (v, s') = runState m s
  104.              in (v, s')) =                                    
  105. -- {Sustitucion de let}
  106. State (\s -> runState m s) =                                  
  107. --{Eta-reduccion}
  108. State (runState m) =                                          
  109. -- {La composicion de State y runState es la identidad}
  110. m
  111.  
  112. -- Monad 3
  113. (m >>= f) >>= g =                                              
  114. -- {Definicion de >>=}
  115. (State (\s -> let (v, s') = runState m s
  116.               in runState (f v) s')) >>= g =                  
  117. -- {Definicion de >>=}
  118. State (\s -> let (v, s') = runState (State (\s -> let (v, s') = runState m s
  119.                                                    in runState (f v) s')) s
  120.              in runState (g v) s') =                          
  121. -- {Definicion de runState}
  122. State (\s -> let (v, s') = (\s -> let (v, s') = runState m s
  123.                                    in runState (f v) s')) s
  124.              in runState (g v) s') =                          
  125. -- {Aplicacion de funcion anonima}
  126. State (\s -> let (v, s') = (let (v, s') = runState m s
  127.                              in runState (f v) s')
  128.              in runState (g v) s') =                          
  129. -- {Se reescribe el let de la siguiente forma:  
  130. -- let a = (let b = c in f(b)) in g(a)
  131. -- let b = c
  132. --     a = f(b)
  133. --  in g(a)}
  134. State (\s -> let (v, s')  = runState m s
  135.                 (w, s'') = runState (f v) s'
  136.              in runState (g w) s'')
  137.  
  138.  
  139. m >>= (\x -> f x >>= g) =                                      
  140. -- {Definicion de >>=}
  141. State (\s -> let (v, s') = runState m s
  142.             in runState ((\x -> f x >>= g) v) s') =          
  143. -- {Aplicacion de funcion anonima}
  144. State (\s -> let (v, s') = runState m s
  145.             in runState (f v >>= g) s') =                    
  146. -- {Definicion de >>=}
  147. State (\s -> let (v, s') = runState m s
  148.              in runState (State (\s -> let (v, s') = runState (f v) s
  149.                                          in runState (g v) s')) s') =  
  150. -- {Definicion de runState}
  151. State (\s -> let (v, s') = runState m s
  152.              in (\s -> let (v, s') = runState (f v) s
  153.                          in runState (g v) s') s') =          
  154. -- {Aplicacion de funcion anonima}
  155. State (\s -> let (v, s') = runState m s
  156.              in (let (v, s') = runState (f v) s'
  157.                   in runState (g v) s')) =                    
  158. -- {Se reescribe el let de la siguiente forma:  
  159. -- let a = b in (let c = f(a) in g(c))
  160. -- let a = b
  161. --     c = f(a)
  162. --  in g(c)}
  163. State (\s -> let (v, s')  = runState m s
  164.                 (w, s'') = runState (f v) s'
  165.              in runState (g w) s'')
  166.              
  167. -- {Ambos terminos son iguales a una misma expresion}
  168. -- {Por transitividad, (m >>= f) >>= g = m >>= (\x -> f x >>= g)}
  169.  
  170. set :: s -> State s ()
  171. set x = St (\s -> ((), x))
  172.  
  173. get :: State s s
  174. get = St (\s -> (s, s))
  175.  
  176.  
  177. -- EJERCICIO 4
  178. data Tree a = Leaf a | Branch (Tree a) (Tree a) deriving Show
  179.  
  180. instance Functor Tree where
  181.     fmap f (Leaf a) = Leaf (f a)
  182.     fmap f (Branch l r) = Branch (fmap f l) (fmap f r)
  183.  
  184. numTree :: Tree a -> Tree Int
  185. numTree t = fst (mapTreeNro update t 0)
  186.             where update a n = (n, n + 1)
  187.  
  188. mapTreeNro :: (a -> Int -> (b, Int)) -> Tree a -> Int -> (Tree b, Int)
  189. mapTreeNro update (Leaf x) n = let (actual, next) = update x n
  190.                                 in (Leaf actual, next)
  191. mapTreeNro update (Branch l r) n = let (ml, next)  = mapTreeNro update l n
  192.                                        (mr, next2) = mapTreeNro update r next
  193.                                     in (Branch ml mr, next2)
  194.  
  195. mapTreeSt :: (a -> s -> (b, s)) -> Tree a -> s -> (Tree b, s)
  196. mapTreeSt update (Leaf x) s = let (actual, next) = update x s
  197.                                in (Leaf actual, next)
  198. mapTreeSt update (Branch l r) s = let (ml, next)  = mapTreeSt update l s
  199.                                       (mr, next2) = mapTreeSt update r next
  200.                                    in (Branch ml mr, next2)
  201.  
  202. newtype State s a = St {runState :: s -> (a, s)}
  203.  
  204. instance Monad (State s) where
  205.     return x = St (\s -> (x, s))
  206.     (St h) >>= f = St (\s -> let (x, s') = h s
  207.                              in runState (f x) s')
  208.  
  209. mapTreeM :: (a -> State s b) -> Tree a -> State s (Tree b)
  210. mapTreeM update (Leaf x) = do y <- update x
  211.                               return (Leaf y)
  212. mapTreeM update (Branch l r) = do ml <- mapTreeM update l
  213.                                   mr <- mapTreeM update r
  214.                                   return (Branch ml mr)
  215.  
  216.  
  217. -- EJERCICIO 5
  218. data MyString = Empty | Cons Char MyString
  219.  
  220. instance Monoid MyString where
  221.     mempty = Empty
  222.     mappend Empty ys = ys
  223.     mappend (Cons c xs) ys = Cons c (mappend xs ys)
  224.  
  225. {-
  226. 1- mappend mempty xs = mappend Empty xs = xs
  227. 2- mappend mempty mempty = mempty
  228.    mappend (Cons c xs) mempty = Cons c (mappend xs mempty) = Cons c xs
  229. 3- mappend (mappend xs ys) zs
  230.    mappend xs (mappend ys zs)
  231. -}
  232.  
  233. newtype Output w a = Out (a, w)
  234.  
  235.  
  236. instance Monad (Output w) where
  237.     return x = Out (x, mempty)
  238.     Out (a, mempty) >>= f = f a
  239.     Out (a, w) = let Out (b, w2) = f a
  240.                   in Out (b, w)
  241.  
  242. {-
  243. 1- return x >>= f = Out (x, mempty) >>= f = f x
  244. 2- Out (a, mempty) >>= return = return a = Out (a, mempty)
  245.    Out (a, w) >>= return = let Out (b, w2) = return a in Out (b, w) =
  246.  = let Out (b, w2) = Out (a, mempty) in Out (b, w) = Out (a, w)
  247. 3- (Out (a, mempty) >>= f) >>= g =
  248.  = (f a) >>= g = (\x -> f x >>= g) a = Out (a, mempty) >>= (\x -> f x >>= g)
  249.  
  250.    (Out (a, w) >>= f) >>= g =
  251.  = (let Out (b, w2) = f a in Out (b, w)) >>= g = **Sea Out (b, w2) = f a**
  252.  = Out (b, w) >>= g =
  253.  = let Out (b, w2) = g b in Out (b, w) =
  254.  = let Out (b, w2) = f a
  255.        Out (c, w3) = g b
  256.     in Out (c, w)
  257.  
  258.    Out (a, w) >>= (\x -> f x >>= g) =
  259.  = let Out (b, w2) = (\x -> fx >>= g) a in Out (b, w) =
  260.  = let Out (b, w2) = (f a >>= g) in Out (b, w) = **Sea Out (b, w2) = f a**
  261.  = let Out (b, w2) = (let Out (c, w2) = g b in Out (c, w)) in Out (b, w) =
  262.  = let Out (b, w2) = f a
  263.        Out (c, w3) = g b
  264.     in Out (c, w)
  265. -}
  266.  
  267.  
  268. instance Monad (Output w) where
  269.     return x = Out (x, mempty)
  270.     Out (a, w) = let Out (b, w2) = f a
  271.                   in Out (b, mappend w2 w)
  272.  
  273. {-
  274. 1- return x >>= f =
  275.  = Out (x, mempty) >>= f =
  276.  = let Out (b, w2) = f a
  277.     in Out (b, mappend w2 mempty) =
  278.  = let Out (b, w2) = f a
  279.     in Out (b, w2) =
  280.  = f a
  281. 2- Out (a, w) >>= return =
  282.  = let Out (b, w2) = return a
  283.     in Out (b, mappend w2 w) =
  284.  = let Out (b, w2) = Out (a, mempty)
  285.     in Out (b, mappend w2 w) =
  286.  = let Out (b, w2) = Out (a, mempty)
  287.     in Out (a, mappend mempty w) =
  288.  = Out (a, w)
  289. 3- (Out (a, w) >>= f) >>= g =
  290.  = (let Out (b, w2) = f a in Out (b, mappend w2 w)) >>= g =
  291.  = (let Out (b, w2) = f a in Out (b, mappend w2 w) >>= g) =
  292.  = (let Out (b, w2) = f a in (let Out (c, w3) = g b
  293.                                in Out (c, mappend w3 (mappend w2 w))
  294.  
  295.    Out (a, w) >>= (\x -> f x >>= g) =
  296.  = let Out (b, w2) = (\x -> f x >>= g) a in Out (b, mappend w2 w) =
  297.  = let Out (b, w2) = (f a >>= g) in Out (b, mappend w2 w) =
  298.  = let Out (b, w2) = (let Out (b, w2) = f a in Out (b, w2) >>= g) in Out (b, mappend w2 w) =
  299.  = let Out (b, w2) = (let Out (b, w2) = f a in (let Out (c, w3) = g b
  300.                                                  in Out (c, mappend w3 w2))) in Out (b, mappend w2 w) =
  301.  = let Out (b, w2) = f a
  302.        Out (c, w3) = g b
  303.     in Out (c, mappend (mappend w3 w2) w) =
  304.  = let Out (b, w2) = f a
  305.        Out (c, w3) = g b
  306.     in Out (c, mappend w3 (mappend w2 w))
  307. -}
  308.  
  309.  
  310.  
  311. -- EJERCICIO 6
  312. (>>)  :: M a -> M b -> M b
  313. (>>) = \a b -> (>>=) a (\a -> b)
  314.  
  315. -- (>>=) no puede definirse en términos de (>>) pues cuando se hace un paso de secuenciación, se pierde el resultado de la primera computación para poder usarlo en la segunda.
  316.  
  317.  
  318. -- EJERCICIO 7
  319. sequence1 :: Monad m => [m a] -> m [a]
  320. sequence1 [] = return []
  321. sequence1 (mx:mxs) = (sequence1 mxs) >>= \xs -> mx >>= \x -> return (x : xs)
  322.  
  323. liftM :: Monad m => (a -> b) -> m a -> m b
  324. liftM f ma = ma >>= \a -> return (f a)
  325.  
  326. liftM2 :: Monad m => (a -> b -> c) -> m a -> m b -> m c
  327. liftM2 f ma mb = ma >>= \a -> mb >>= \b -> return (f a b)
  328.  
  329. sequence2 :: Monad m => [m a] -> m [a]
  330. sequence2 mxs = foldr (liftM2 (:)) (return []) mxs
  331.  
  332.  
  333. -- EJERCICIO 8
  334. data Error er a = Raise er | Return a deriving Show
  335.  
  336. instance Monad (Error er) where
  337.     return a = Return a
  338.     Raise er >>= f = Raise er
  339.     Return a >>= f = f a
  340.  
  341. {-
  342. 1- return x >>= f = Return x >>= f = f x
  343. 2- Raise er >>= return = Raise er
  344.    Return x >>= return = return x = Return x
  345. 3- (Raise er >>= f) >>= g = Raise er >>= g = Raise er = Raise er >>= (\x -> f x >>= g)
  346.    ((Return a) >>= f) >>= g = f a >>= g = (\x -> f x >>= g) a = Return a >>= (\x -> f x >>= g)
  347. -}
  348.  
  349. headEr :: [a] -> Error String a
  350. headEr [] = Raise "No existe head de []"
  351. headEr (x:xs) = Return x
  352.  
  353. tailEr :: [a] -> Error String [a]
  354. tailEr [] = Raise "No existe tail de []"
  355. tailEr (x:xs) = Return xs
  356.  
  357. pushEr :: a -> [a] -> Error String [a]
  358. pushEr a xs = Return (a:xs)
  359.  
  360. popEr :: [a] -> Error String [a]
  361. popEr = tailEr
  362.  
  363.  
  364. -- EJERCICIO 9
  365. import Control.Applicative (Applicative(..))
  366. import Control.Monad       (liftM, ap)  
  367.  
  368. data Error er a = Raise er | Return a deriving Show
  369.  
  370. data T = Con Int | Div T T
  371.  
  372. newtype M s e a = M {runM :: s -> Error e (a, s)}
  373.  
  374. -- Para calmar al GHC
  375. instance Functor (Error er) where
  376.     fmap = liftM
  377.  
  378. instance Applicative (Error er) where
  379.     pure   = return
  380.     (<*>)  = ap      
  381. --
  382.  
  383. instance Monad (Error er) where
  384.     return a = Return a
  385.     Raise er >>= f = Raise er
  386.     Return a >>= f = f a
  387.  
  388.  
  389. -- Para calmar al GHC
  390. instance Functor (M s e) where
  391.     fmap = liftM
  392.  
  393. instance Applicative (M s e) where
  394.     pure   = return
  395.     (<*>)  = ap      
  396. --
  397.  
  398. instance Monad (M s e) where
  399.     return x = M (\s -> Return (x, s))
  400.     (M h) >>= f = M (\s -> case h s of
  401.                                 Raise er -> Raise er
  402.                                 Return (a, s2) -> runM (f a) s2)
  403.  
  404. raise :: String -> M Int String Int
  405. raise xs = M (\s -> Raise xs)
  406.  
  407. modify :: (Int -> Int) -> M Int String ()
  408. modify f = M (\s -> Return ((), f s))
  409.  
  410.  
  411. eval :: T -> M Int String Int
  412. eval (Con n) = return n
  413. eval (Div t1 t2) = do v1 <- eval t1
  414.                       v2 <- eval t2
  415.                       if v2 == 0 then raise "Error: Division por cero"
  416.                                  else do modify (+1)
  417.                                          return (div v1 v2)
  418.  
  419. doEval :: T -> Error String (Int, Int)
  420. doEval t = runM (eval t) 0
  421.  
  422.  
  423. eval2 :: T -> Error String (Int, Int)
  424. eval2 (Con n) = Return (n, 0)
  425. eval2 (Div t1 t2) = case eval2 t1 of
  426.                          Raise er -> Raise er
  427.                          Return (v1, s1) -> case eval2 t2 of
  428.                                                  Raise er -> Raise er
  429.                                                  Return (v2, s2) -> if v2 == 0 then Raise "Error: Division por cero"
  430.                                                                                else Return ((div v1 v2), s1 + s2 + 1)
  431.  
  432.  
  433. -- EJERCICIO 10
  434. data Cont r a = Cont ((a -> r) -> r)
  435.  
  436. instance Monad (Cont r) where
  437.     return x = Cont (\f -> f x)
  438.     (Cont f) >>= g = Cont (\h -> f (\a -> let (Cont j) = g a
  439.                                            in j h))
  440.  
  441. {-
  442. 1- return x >>= f
  443.  = Cont (\f -> f x) >>= f =
  444.  = Cont (\h -> (\f -> f x) (\a -> let (Cont j) = f a
  445.                                    in j h)) =
  446.  = Cont (\h -> (\a -> let (Cont j) = f a
  447.                        in j h) x) =
  448.  = Cont (\h -> let (Cont j) = f x
  449.                 in j h) =
  450.  = Cont (let (Cont j) = f x in j) =
  451.  = let (Cont j) = f x in Cont j =
  452.  = f x
  453.  
  454. 2- (Cont z) >>= return =
  455.  = Cont (\h -> z (\a -> let (Cont j) = return a
  456.                          in j h)) =
  457.  = Cont (\h -> z (\a -> let (Cont j) = Cont (\f -> f a)
  458.                          in j h)) =
  459.  = Cont (\h -> z (\a -> (\f -> f a) h)) =
  460.  = Cont (\h -> z (\a -> h a)) =
  461.  = Cont (\h -> z h) =
  462.  = Cont z
  463.  
  464. 3- ((Cont z) >>= f) >>= g =
  465.  = Cont (\h -> z (\a -> let (Cont j) = f a
  466.                          in j h)) >>= g =
  467.  = Cont (\h -> (\h -> z (\a -> let (Cont j) = f a
  468.                                 in j h)) (\a -> let (Cont j) = g a
  469.                                                  in j h)) =
  470.  = Cont (\h -> z (\a -> let (Cont j) = f a
  471.                          in j (\a -> let (Cont j) = g a
  472.                                       in j h))) =
  473. ...
  474.                                      
  475.    (Cont z) >>= (\x -> f x >>= g) =
  476.  = Cont (\h -> z (\a -> let (Cont j) = (\x -> f x >>= g) a
  477.                          in j h)) =
  478.  = Cont (\h -> z (\a -> let (Cont j) = f a >>= g
  479.                          in j h)) =
  480.  = Cont (\h -> z (\a -> let (Cont j) = (let (Cont y) = f a in (Cont y) >>= g)
  481.                          in j h)) =
  482.  = Cont (\h -> z (\a -> let (Cont j) = (let (Cont y) = f a in Cont (\h -> y (\a -> let (Cont j) = g a
  483.                                                                                     in j h))
  484.                          in j h)) =
  485. ...
  486.  
  487. -}
Add Comment
Please, Sign In to add comment