Advertisement
Guest User

Untitled

a guest
Feb 15th, 2019
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# Language
  2.  DefaultSignatures
  3. ,PatternSynonyms
  4. ,ViewPatterns
  5. ,MultiParamTypeClasses
  6. ,RankNTypes
  7. ,ScopedTypeVariables
  8. ,TypeApplications
  9. ,FunctionalDependencies
  10. #-}
  11.  
  12. module FIFO_r where
  13.  
  14. import Control.Applicative (liftA2)
  15. import Data.Maybe (fromJust)
  16. import Data.Functor.Apply (Apply(..),liftF2)
  17. import Empty
  18. import State
  19.  
  20. ----
  21. -- Stream_r
  22.  
  23. class Get_r r f | f -> r  where
  24.  get_r :: f a -> (r a,f a)
  25.  
  26. class Set_r r f | f -> r  where
  27.  set_r :: (r a,f a) -> f a
  28.  
  29. class (Get_r r f,Set_r r f) => Stream_r r f | f -> r
  30.  
  31. -- (:::) reserved for the version with r = Identity to help abstract away these Identity wrappers
  32. pattern  (::::) :: Stream_r r f => r a -> f a -> f a
  33. pattern x :::: xs <- (get_r -> (x,xs))
  34.  where  x :::: xs =   set_r    (x,xs)
  35.  
  36. stream_rDifference :: (Get_r r t, Set_r r' t', Apply f) => (r a -> f (r' b)) -> State (t a) (f ((t' b) -> t' b))
  37. stream_rDifference f xs = (\(x,xs')->(fmap (curry set_r) (f x),xs')) (get_r xs)
  38.  
  39. convertStream_r :: (Get_r r t, Set_r r' t', Apply f) => (r a -> f (r' b)) -> t a -> f (t' b)
  40. convertStream_r f = hyloState (stream_rDifference f) g
  41. where
  42.  g :: Apply f => CoState (f (t' b)) (f ((t' b) -> t' b))
  43.   g (h,xs) = liftF2 (\f x ->f (x)) h xs
  44.  
  45.  
  46. ----
  47. -- Linear_r
  48.  
  49. class (Get_r r f) => GetS_r r f | f -> r where
  50.  isLast_r :: f a -> Bool
  51.  isLast_r = null . snd . getS_r
  52.  getS_r :: f a -> (r a,Maybe (f a))
  53.  getS_r xs | isLast_r @r xs = fmap (const Nothing) $ get_r xs
  54.            | otherwise    = fmap Just            $ get_r xs
  55.  {-# MINIMAL isLast_r | getS_r #-}
  56.  
  57. get_rDefault :: GetS_r r f => f a -> (r a,f a)
  58. get_rDefault  x = (\(x',xs) -> (x',maybe (error "get_r default via getS_r given \"last\"") id xs)) (getS_r x)
  59.  
  60. class (Set_r r f) => SetS_r r f | f -> r  where
  61.  create_r :: r a -> f a
  62.  create_r r = setS_r (r,Nothing)
  63.  setS_r :: (r a,Maybe (f a)) -> f a
  64.  setS_r (r,Just xs) = set_r (r,xs)
  65.  {-# MINIMAL create_r | setS_r #-}
  66.  
  67. set_rDefault  :: SetS_r r f => (r a,f a) -> f a
  68. set_rDefault = setS_r . fmap Just
  69.  
  70. class (Stream_r r f,GetS_r r f,SetS_r r f) => Linear_r r f | f -> r
  71.  
  72. pattern Last_r :: Linear_r r f => r a -> f a
  73. pattern Last_r x   <- (getS_r -> (x, Nothing))
  74.  where  Last_r x =     setS_r    (x,Nothing)
  75.  
  76. linear_rDifference :: (GetS_r r t, SetS_r r' t', Apply f) => (r a -> f (r' b)) -> SafeState (t a) (f (Maybe (t' b) -> t' b))
  77. linear_rDifference f xs = (\(x,xs')->(fmap (curry setS_r) (f x),xs')) (getS_r xs)
  78.  
  79. convert1_r :: (GetS_r r t, SetS_r r' t', Apply f) => (r a -> f (r' b)) -> t a -> f (t' b)
  80. convert1_r f = hyloSafeState (linear_rDifference f) g
  81. where
  82.  g :: Apply f => CoSafeState (f (t' b)) (f (Maybe (t' b) -> t' b))
  83.   g (h,Nothing) = fmap (\h' -> h' Nothing) h
  84.   g (h,Just xs) = liftF2 (\f x ->f (Just x)) h xs -- is this lifting of Just slow?
  85.  
  86. ----
  87. -- Stack_r
  88.  
  89. class (GetS_r r f,Empty f) => Uncons_r r f | f -> r  where
  90.  uncons_r :: f a -> Maybe (r a,f a)
  91.  default uncons_r :: f a -> Maybe (r a,f a)
  92.  uncons_r Empty = Nothing
  93.  uncons_r xs = Just $ fmap (maybe empty id) $ getS_r xs
  94.  
  95. isLast_rDefault :: Uncons_r r f => f a -> Bool
  96. isLast_rDefault = isEmpty . snd . get_r            
  97.  
  98. getS_rDefault :: Stack_r r f => f a -> (r a,Maybe (f a))
  99. getS_rDefault Empty = error "getS_r Empty"
  100. getS_rDefault xs    = fmap Just $ fromJust $ uncons_r xs    
  101.  
  102. class (SetS_r r f,Empty f) => Cons_r r f | f -> r  where
  103.  cons_r   :: Maybe (r a,f a) -> f a
  104.  default cons_r :: Maybe (r a,f a) -> f a
  105.  cons_r Nothing = empty
  106.  cons_r (Just (x,xs)) = set_r (x,xs)
  107.  
  108. emptyDefault :: Cons_r r f => f a
  109. emptyDefault = cons_r Nothing
  110.  
  111. setS_rDefault :: Stack_r r f => (r a,Maybe (f a)) -> f a
  112. setS_rDefault = cons_r . Just . fmap (maybe empty id)
  113.  
  114. class (Empty f,Linear_r r f,Uncons_r r f,Cons_r r f) => Stack_r r f | f -> r  where
  115.  
  116. stack_rDifference :: (Uncons_r r t, Cons_r r' t', Applicative f) => (r a -> f (r' b)) -> StateSafe (t a) (f ((t' b) -> t' b))
  117. stack_rDifference f xs = fmap (\(x,xs')->(fmap (curry (cons_r .Just) ) (f x),xs')) (uncons_r xs)
  118.  
  119. convert_r :: (Uncons_r r t, Cons_r r' t', Applicative f) => (r a -> f (r' b)) -> t a -> f (t' b)
  120. convert_r f = hyloStateSafe (stack_rDifference f) g
  121. where
  122.  g :: (Empty t',Applicative f) => CoStateSafe (f (t' b)) (f ((t' b) -> t' b))
  123.  g Nothing = pure empty
  124.  g (Just (h,xs)) = liftA2 (\f x ->f (x)) h xs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement