Skip to content

Commit

Permalink
Revert "Remove StaredStore."
Browse files Browse the repository at this point in the history
This reverts commit 8fc43d0.

Conflicts:

	data-lens.cabal
  • Loading branch information
roconnor committed May 19, 2012
1 parent e8550f0 commit 6bd101e
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/Control/Comonad/StaredStore.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module Control.Comonad.StaredStore where

import Control.Applicative
import Control.Comonad
import Control.Comonad.Trans.Store
import Data.Functor.Coproduct
import Data.Functor.Identity

newtype StaredStore s a = StaredStore {runStaredStore :: Coproduct Identity (StoreT s (StaredStore s)) a}

instance Functor (StaredStore s) where
fmap f (StaredStore x) = StaredStore (fmap f x)

instance Extend (StaredStore s) where
duplicate (StaredStore x) = StaredStore (fmap StaredStore (duplicate x))
extend f (StaredStore x) = StaredStore (extend (f . StaredStore) x)

instance Comonad (StaredStore s) where
extract (StaredStore x) = extract x

instance Applicative (StaredStore s) where
pure = StaredStore . left . pure
f <*> (StaredStore x) = coproduct l r x
where
l (Identity y) = fmap ($ y) f
r y = StaredStore (right (StoreT ((.) <$> f <*> v) s))
where
(v, s) = runStoreT y

fromStore :: Store b a -> StaredStore b a
fromStore st = StaredStore (right (StoreT (pure g) v))
where
(g,v) = runStore st

poss :: StaredStore b a -> [b]
poss x = go x []
where
go :: StaredStore b a -> [b] -> [b]
go = coproduct (const id) h . runStaredStore
h s = go g . (v:)
where
(g, v) = runStoreT s

seekss :: (b -> b) -> StaredStore b a -> StaredStore b a
seekss f = coproduct (pure . runIdentity) h . runStaredStore
where
h s = StaredStore (right (StoreT (seekss f g) (f v)))
where
(g, v) = runStoreT s

peekss :: (b -> b) -> StaredStore b a -> a
peekss f = extract . seekss f

0 comments on commit 6bd101e

Please sign in to comment.