-
Notifications
You must be signed in to change notification settings - Fork 0
Indexed Kan Extension #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
echatav
wants to merge
19
commits into
master
Choose a base branch
from
indexed-kan
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
ca62da9
indexed right Kan extension
echatav ebf8ba3
Update Kan.hs
echatav 02c173f
Ixer
echatav 9af959f
Update Kan.hs
echatav 7879511
Codense
echatav 75a3ab2
Update State.hs
echatav 076068a
Update Codensity.hs
echatav bcb78b5
move to State
echatav 41d7c24
Update State.hs
echatav 42b1432
Update State.hs
echatav e622d85
Update State.hs
echatav b7a3e96
Update State.hs
echatav 2773e67
Update State.hs
echatav 99e3550
Update State.hs
echatav aab452e
Update Free.hs
echatav 24df74d
Update State.hs
echatav eca0297
indexed-do
echatav 784f4e2
Update State.hs
echatav 4c5d389
Update State.hs
echatav File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -8,19 +8,40 @@ The state indexed monad transformer. | |
| -} | ||
|
|
||
| module Control.Monad.Trans.Indexed.State | ||
| ( StateIx (..) | ||
| ( -- * State | ||
| IxMonadTransState (..) | ||
| , StateIx (..) | ||
| , evalStateIx | ||
| , execStateIx | ||
| , modifyIx | ||
| , putIx | ||
| , toStateT | ||
| , fromStateT | ||
| -- * Reader | ||
| , IxMonadTransReader (..) | ||
| , ReaderIx | ||
| , runReaderIx | ||
| , evalReaderIx | ||
| , execReaderIx | ||
| , toReaderT | ||
| , fromReaderT | ||
| , AskIx (..) | ||
| -- * Codensity | ||
| , CodensityIx (..) | ||
| , liftCodensityIx | ||
| , lowerCodensityIx | ||
| , fromStateIx | ||
| , toStateIx | ||
| ) where | ||
|
|
||
| import Control.Applicative | ||
| import Control.Monad | ||
| import Control.Monad.Reader | ||
| import Control.Monad.State | ||
| import Control.Monad.Trans.Indexed | ||
| import qualified Control.Monad.Trans.Indexed.Do as Ix | ||
| import Control.Monad.Trans.Indexed.Free | ||
| import Control.Monad.Trans.Indexed.Free.Wrap | ||
|
|
||
| newtype StateIx i j m x = StateIx { runStateIx :: i -> m (x, j)} | ||
| newtype StateIx i j m x = StateIx {runStateIx :: i -> m (x, j)} | ||
| deriving Functor | ||
| instance IxMonadTrans StateIx where | ||
| joinIx (StateIx f) = StateIx $ \i -> do | ||
|
|
@@ -43,14 +64,117 @@ evalStateIx m i = fst <$> runStateIx m i | |
| execStateIx :: Monad m => StateIx i j m x -> i -> m j | ||
| execStateIx m i = snd <$> runStateIx m i | ||
|
|
||
| modifyIx :: Applicative m => (i -> j) -> StateIx i j m () | ||
| modifyIx f = StateIx $ \i -> pure ((), f i) | ||
|
|
||
| putIx :: Applicative m => j -> StateIx i j m () | ||
| putIx j = modifyIx (\ _ -> j) | ||
|
|
||
| toStateT :: StateIx i i m x -> StateT i m x | ||
| toStateT (StateIx f) = StateT f | ||
|
|
||
| fromStateT :: StateT i m x -> StateIx i i m x | ||
| fromStateT (StateT f) = StateIx f | ||
|
|
||
| class | ||
| ( IxMonadTrans t | ||
| , forall m s i j. (Monad m, s ~ i, i ~ j) => MonadState s (t i j m) | ||
| ) => IxMonadTransState t where | ||
| {-# MINIMAL putIx | stateIx #-} | ||
| putIx :: Monad m => j -> t i j m () | ||
| putIx s = stateIx (\_ -> ((), s)) | ||
| modifyIx :: Monad m => (i -> j) -> t i j m () | ||
| modifyIx f = stateIx (\i -> ((), f i)) | ||
| stateIx :: Monad m => (i -> (a,j)) -> t i j m a | ||
| stateIx f = Ix.do | ||
| s <- get | ||
| let ~(a, s') = f s | ||
| putIx s' | ||
| return a | ||
| instance IxMonadTransState StateIx where | ||
| stateIx f = StateIx (return . f) | ||
|
|
||
| newtype CodensityIx t i j m a = CodensityIx | ||
| { runCodensityIx :: forall b k. (a -> t j k m b) -> t i k m b } | ||
| deriving Functor | ||
|
|
||
| lowerCodensityIx | ||
| :: (IxMonadTrans t, Monad m) | ||
| => CodensityIx t i j m a -> t i j m a | ||
| lowerCodensityIx (CodensityIx f) = f return | ||
|
|
||
| liftCodensityIx | ||
| :: (IxMonadTrans t, Monad m) | ||
| => t i j m a -> CodensityIx t i j m a | ||
| liftCodensityIx m = CodensityIx $ \h -> bindIx h m | ||
|
|
||
| fromStateIx :: Monad m => StateIx i j m x -> CodensityIx ReaderIx i j m x | ||
| fromStateIx (StateIx f) = Ix.do | ||
| i <- get | ||
| (x,j) <- lift (f i) | ||
| putIx j | ||
| return x | ||
|
|
||
| toStateIx :: Monad m => CodensityIx ReaderIx i j m x -> StateIx i j m x | ||
| toStateIx = StateIx . runReaderIx . lowerCodensityIx | ||
|
|
||
| class | ||
| ( IxMonadTrans t | ||
| , forall m r i j. (Monad m, r ~ i, i ~ j) => MonadReader r (t i j m) | ||
| ) => IxMonadTransReader t where | ||
| localIx :: Monad m => (i -> r) -> t r j m a -> t i j m a | ||
|
|
||
| type ReaderIx = FreeIx (Ixer AskIx) | ||
|
|
||
| data AskIx i j x where AskIx :: AskIx x x x | ||
|
|
||
| runReaderIx :: Monad m => ReaderIx i j m x -> i -> m (x, j) | ||
| runReaderIx (FreeIx m) i = do | ||
| wrapped <- m | ||
| case wrapped of | ||
| Unwrap x -> return (x,i) | ||
| Wrap (Ixer f AskIx) -> runReaderIx (f i) i | ||
|
|
||
| evalReaderIx :: Monad m => ReaderIx i j m x -> i -> m x | ||
| evalReaderIx m i = fst <$> runReaderIx m i | ||
|
|
||
| execReaderIx :: Monad m => ReaderIx i j m x -> i -> m j | ||
| execReaderIx m i = snd <$> runReaderIx m i | ||
|
|
||
| toReaderT :: Monad m => ReaderIx i j m x -> ReaderT i m x | ||
| toReaderT = ReaderT . evalReaderIx | ||
|
|
||
| fromReaderT :: Monad m => ReaderT i m x -> ReaderIx i i m x | ||
| fromReaderT (ReaderT f) = do | ||
| i <- ask | ||
| lift (f i) | ||
|
|
||
| instance IxMonadTrans t => IxMonadTrans (CodensityIx t) where | ||
| joinIx (CodensityIx k) = | ||
| CodensityIx $ \f -> k $ \(CodensityIx g) -> g f | ||
| instance i ~ j => Applicative (CodensityIx t i j m) where | ||
| pure x = CodensityIx $ \k -> k x | ||
| CodensityIx cf <*> CodensityIx cx = | ||
| CodensityIx $ \ k -> cf $ \ f -> cx (k . f) | ||
| instance i ~ j => Monad (CodensityIx t i j m) where | ||
| return = pure | ||
| CodensityIx cx >>= k = | ||
| CodensityIx $ \ c -> cx (\ x -> runCodensityIx (k x) c) | ||
| instance (IxMonadTrans t, i ~ j) => MonadTrans (CodensityIx t i j) where | ||
| lift m = CodensityIx (\k -> bindIx k (lift m)) | ||
| instance (i ~ j, Alternative (t i j m), IxMonadTrans t, Monad m) | ||
| => Alternative (CodensityIx t i j m) where | ||
| empty = liftCodensityIx empty | ||
| x <|> y = liftCodensityIx (lowerCodensityIx x <|> lowerCodensityIx y) | ||
| instance (i ~ j, Alternative (t i j m), IxMonadTrans t, Monad m) | ||
| => MonadPlus (CodensityIx t i j m) | ||
| instance (i ~ j, IxMonadTransReader t, Monad m) | ||
| => MonadState i (CodensityIx t i j m) where | ||
| get = liftCodensityIx ask | ||
| put = putIx | ||
| instance IxMonadTransReader t => IxMonadTransState (CodensityIx t) where | ||
| putIx s = CodensityIx (localIx (const s) . ($ ())) | ||
| instance (s ~ t, Monad m, IxMonadTransFree freeIx) | ||
| => MonadReader s (freeIx (Ixer AskIx) s t m) where | ||
| ask = liftFreerIx AskIx | ||
| local = localIx | ||
| instance IxMonadTransFree freeIx | ||
| => IxMonadTransReader (freeIx (Ixer AskIx)) where | ||
| localIx f | ||
| = lowerCodensityIx | ||
| . (\m -> Ix.do {i <- get; putIx (f i); m}) | ||
| . liftCodensityIx | ||
|
Comment on lines
+169
to
+180
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is broken. |
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The indexed codensity monad transformer type which I found in sessiontypes by @Ferdinand-vW.