Skip to content

Instantly share code, notes, and snippets.

@co-dan
Created April 7, 2014 06:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save co-dan/10015772 to your computer and use it in GitHub Desktop.
Save co-dan/10015772 to your computer and use it in GitHub Desktop.
Sequence-algebras
{-# LANGUAGE TypeFamilies, FlexibleInstances #-}
module SeqFoldable where
import Prelude hiding (drop, take)
import Data.Functor.Foldable
import Data.Sequence (Seq, ViewL (..), (<|))
import qualified Data.Sequence as S
data instance Prim (Seq a) b = EmptyP | ViewP a b
instance Functor (Prim (Seq a)) where
fmap _ EmptyP = EmptyP
fmap f (ViewP a b) = ViewP a (f b)
type instance Base (Seq a) = Prim (Seq a)
viewlToPrim :: ViewL a -> Prim (Seq a) (Seq a)
viewlToPrim EmptyL = EmptyP
viewlToPrim (a :< as) = ViewP a as
instance Foldable (Seq a) where
project = viewlToPrim . S.viewl
instance Unfoldable (Seq a) where
embed EmptyP = S.empty
embed (ViewP a b) = a <| b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment