Skip to content

Instantly share code, notes, and snippets.

@pchiusano
Last active August 29, 2015 14:04
Show Gist options
  • Save pchiusano/bdf7549ef8eb3a32d674 to your computer and use it in GitHub Desktop.
Save pchiusano/bdf7549ef8eb3a32d674 to your computer and use it in GitHub Desktop.
Design of new basis for machines / scalaz-stream that does not require separate plan type
-- Type-aligned sequence catenable queue supporting O(1) append, snoc, uncons
-- Don't need it to be a dequeue (unsnoc not needed)
data TCQueue c a b -- c is the category, a is starting type, b is ending type
type Channel f a b = TCQueue (Transition f) a b
type Process f b = Channel f () b
data Transition f a b where
Bind :: (a -> Process f b) -> Transition f a b
OnHalt :: (Cause -> Process f b) -> Transition f a b
Emit :: b -> Transition f a b
Await :: f b -> Transition f a (Either Cause b)
Halt :: Cause -> Transition f a b
data Cause = Normal | Error String
onHalt :: Channel f a b -> (Cause -> Process f b) -> Channel f a b
onHalt h t = h |> OnHalt t
append :: Channel f a b -> Process f b -> Channel f a b
append a b = onHalt a go where
go Normal = b
go cause = singleton (Halt cause)
through :: Channel f a b -> Channel f b c -> Channel f a c
through = ><
instance Monad (Process f b) where
return a = singleton (Emit a)
(>>=) p f = p |> Bind f
step :: Cause -> a -> Channel f a b -> Process f (Either Cause (b, Channel f a b))
step = ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment