forked from ekmett/data-lens
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This reverts commit 8fc43d0. Conflicts: data-lens.cabal
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |