Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-# Language RankNTypes , DefaultSignatures #-}
- class Unfoldable f => Set f where
- set :: (Maybe (a,f a) -> f a)
- class Foldable' f => Get f where
- get :: (f a -> Maybe (a,f a))
- class (Get f,Set f) => Stream f
- --
- class Foldable' f where
- foldr' :: (Maybe (a,b) -> b) -> f a -> b
- default foldr' :: Get f => (Maybe (a,b) -> b) -> f a -> b
- foldr' f xs = case get xs of
- Nothing -> f Nothing
- (Just (x,xs')) -> f (Just (x,(foldr' f xs')))
- class Unfoldable f where
- unfoldr :: s -> (s->Maybe (a,s)) -> f a
- default unfoldr :: Set f => s -> (s->Maybe (a,s)) -> f a
- unfoldr s fs = case fs s of
- Nothing -> set Nothing
- (Just (a,s')) -> set (Just (a,(unfoldr s' fs)))
- --
- instance Get [] where
- get [] = Nothing
- get (x : xs) = Just (x,xs)
- instance Set [] where
- set Nothing = []
- set (Just (x,xs)) = x : xs
- instance Stream []
- instance Foldable' []
- instance Unfoldable []
- --
- data Church a = Church {runChurch :: forall b. ((Maybe (a,b) -> b) -> b)}
- instance Set Church where
- set Nothing = Church (\f -> f Nothing)
- set (Just (x,(Church xs))) = Church (\set' -> set' (Just (x,(xs set'))))
- instance Unfoldable Church
- data CoChurch' f a = CoChurch' {runCoChurch' :: Set f => (CoChurch' f a->Maybe (a,CoChurch' f a)) -> f a}
- instance Set f => Set (CoChurch' f) where
- set Nothing = CoChurch' (\get' -> set Nothing)
- set (Just (x,(CoChurch' xs))) = CoChurch' (\get' -> set (Just (x,xs get')))
- where
- get'' get' x c = Just (x,c)
- instance Set f => Unfoldable (CoChurch' f)
- --
- build :: Set f => Church a -> f a
- build (Church f) = f set
- cobuild :: Set f => CoChurch' f a -> f a
- cobuild (CoChurch' f) = f undefined
- --
- loop :: Set f => s -> (s -> Maybe (a,s)) -> f a
- loop s = build . unfoldr s
- testLoop :: [ Char ]
- testLoop = loop eg (\y -> case y of [] -> Nothing; (x:xs) -> Just (x,xs))
- coLoop :: (Get f,Set f') => f a -> (forall s. s -> Maybe (a,s)) -> f' a
- coLoop xs f = cobuild $ unfoldr xs get
- testCoLoop :: [ Char ]
- testCoLoop = coLoop eg undefined
- eg = "hello world"
- {-
- *Main> testLoop
- "hello world"
- *Main> testCoLoop
- "hello world"
- -}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement