Advertisement
Guest User

Untitled

a guest
Jun 14th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Main where
  2.  
  3. import Data.Array hiding (take)
  4. import Prelude
  5.  
  6. import Control.Alternative (class Alternative, empty, (<|>))
  7. import Control.Apply (applySecond)
  8. import Control.Monad.List.Trans (ListT(..), cons, lift, nil, runListT, runListTRec, take)
  9. import Data.Foldable (class Foldable, foldr, oneOf, foldlDefault, foldrDefault)
  10. import Data.Generic.Rep (class Generic)
  11. import Data.Generic.Rep.Eq (genericEq)
  12. import Data.Generic.Rep.Show (genericShow)
  13. import Data.Lazy (defer)
  14. import Data.List.Lazy as LL
  15. import Effect (Effect)
  16. import Effect.Class (class MonadEffect, liftEffect)
  17. import Effect.Class.Console (log, logShow)
  18. import Data.Traversable
  19. import Data.Maybe
  20.  
  21. data RoseTree a = RoseNode a (Array (RoseTree a))
  22.  
  23. data Tree a = Empty | Node (Tree a) a (Tree a)
  24.  
  25. derive instance genericTree :: Generic (Tree a) _
  26. derive instance functorTree :: Functor Tree
  27.  
  28. instance showTree :: Show a => Show (Tree a) where
  29.   show t = genericShow t
  30.  
  31. instance applyTree :: Apply Tree where
  32.   apply Empty _ = Empty
  33.   apply _ Empty = Empty
  34.   apply (Node lf f rf) (Node lv v rv) = Node (apply lf lv) (f v) (apply rf rv)
  35.  
  36. foldMonoidTree :: forall a m. Monoid m => (a -> m) -> Tree a -> m
  37. foldMonoidTree _ Empty = mempty
  38. foldMonoidTree f (Node l v r) = foldMonoidTree f l <> f v <> foldMonoidTree f r
  39.  
  40. -- Show (generic or manual)
  41. -- Functor
  42. -- Apply ?
  43. -- Applicative ?
  44. -- foldTree ?
  45. -- foldMonoidTree
  46.  
  47. -- (a -> b) -> a -> b
  48. -- Functor f => (a -> b) -> f a -> f b
  49. -- Applicative f => f (a -> b) -> f a -> f b
  50. -- Monad f => (a -> f b) -> f a -> f b
  51. -- Traversable f => (a -> t b) -> m a -> t (m b)
  52.  
  53. data List a = Nil | Cons a (List a)
  54.  
  55. instance semigroupList :: Semigroup (List a) where
  56.   append x y = foldr Cons y x
  57.  
  58. instance functorList :: Functor List where
  59.   map = go
  60.     where
  61.       go :: forall a b. (a -> b) -> List a -> List b
  62.       go f Nil = Nil
  63.       go f (Cons x xs) = Cons (f x) (go f xs)
  64.  
  65. instance applyList :: Apply List where
  66.   apply = go
  67.     where
  68.       go :: forall a b. List (a -> b) -> List a -> List b
  69.       go Nil _ = Nil
  70.       go _ Nil = Nil
  71.       go (Cons f fs) xs = (f <$> xs) <> (go fs xs)
  72.  
  73. instance applicativeList :: Applicative List where
  74.   pure x = Cons x Nil
  75.  
  76. instance foldableList :: Foldable List where
  77.   foldl f = foldlDefault f
  78.   foldr f = foldrDefault f
  79.   foldMap = go
  80.     where
  81.       go :: forall a m. Monoid m => (a -> m) -> List a -> m
  82.       go f Nil = mempty
  83.       go f (Cons x xs) = f x <> go f xs
  84.  
  85. instance bindList :: Bind List where
  86.   bind l f = case l of
  87.     Nil -> Nil
  88.     Cons x xs -> f x <> bind xs f
  89.  
  90. instance monadList :: Monad List
  91.  
  92. instance traversableList :: Traversable List where
  93.   sequence = sequenceDefault
  94.   traverse f = foldr (\x xs -> Cons <$> (f x) <*> xs) (pure Nil)
  95.  
  96. derive instance genericList :: Generic (List a) _
  97.  
  98. instance showList :: Show a => Show (List a) where
  99.   show l = genericShow l
  100.  
  101. instance eqList :: Eq a => Eq (List a) where
  102.   eq l = genericEq l
  103.  
  104. list :: List Int
  105. list = Cons 10 (Cons 20 (Cons 30 Nil))
  106.  
  107. list2 :: List (Int -> Int)
  108. list2 = Cons (_*10) (Cons (_*20) (Cons (_+30) Nil))
  109.  
  110. main :: Effect Unit
  111. main = do
  112.   for_ list $ \x -> do
  113.     log $ show x <> "!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement