Skip to content

Instantly share code, notes, and snippets.

@fumieval
Last active December 6, 2015 07:08
Show Gist options
  • Save fumieval/b7c35ae3129c5a1b69e4 to your computer and use it in GitHub Desktop.
Save fumieval/b7c35ae3129c5a1b69e4 to your computer and use it in GitHub Desktop.
newtype Zombie t a = Zombie { unZombie :: [Spine t (Zombie t) a] }
instance Functor (Zombie t) where
fmap = liftM
instance Applicative (Zombie t) where
pure = return
(<*>) = ap
instance Alternative (Zombie t) where
empty = Zombie []
Zombie xs <|> Zombie ys = Zombie (xs ++ ys)
instance Monad (Zombie t) where
return a = Zombie [Spine (Return a) id]
Zombie xs >>= k = Zombie $ map (\(Spine t c) -> Spine t (c |> Kleisli k)) xs
deflesh :: Zombie t a -> Maybe (MonadView t (Zombie t) a)
deflesh (Zombie (Spine (Return a) s : ss)) = viewL s (Just $ Return a) $ \(Kleisli k) c -> case k a of
Zombie ss' -> deflesh $ Zombie $ map (\(Spine h t) -> Spine h (c . t)) ss' ++ ss
deflesh (Zombie (Spine (t :>>= k) s : ss)) = Just $ t :>>= \a -> case k a of
Zombie ss' -> Zombie $ map (\(Spine h c) -> Spine h (s . c)) ss' ++ ss
deflesh _ = Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment