Skip to content

Commit

Permalink
naming
Browse files Browse the repository at this point in the history
  • Loading branch information
echatav committed Jan 15, 2024
1 parent 122bd75 commit 0512e54
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 66 deletions.
10 changes: 5 additions & 5 deletions src/Control/Monad/Trans/Indexed.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Control.Monad.Trans.Indexed
( IndexedMonadTrans (..)
( IxMonadTrans (..)
, Indexed (..)
, (&)
) where
Expand All @@ -24,14 +24,14 @@ even if the source and target index are different.
>>> :set -XQualifiedDo
>>> import qualified Control.Monad.Trans.Indexed.Do as Indexed
-}
type IndexedMonadTrans
type IxMonadTrans
:: (k -> k -> (Type -> Type) -> Type -> Type)
-> Constraint
class
( forall i j m. Monad m => Functor (t i j m)
, forall i j m. (i ~ j, Monad m) => Monad (t i j m)
, forall i j. i ~ j => MonadTrans (t i j)
) => IndexedMonadTrans t where
) => IxMonadTrans t where

{-# MINIMAL joinIx | bindIx #-}

Expand Down Expand Up @@ -90,11 +90,11 @@ class
-> x -> t i k m z
andThenIx g f x = bindIx g (f x)

{- | `Indexed` reshuffles the type parameters of an `IndexedMonadTrans`,
{- | `Indexed` reshuffles the type parameters of an `IxMonadTrans`,
exposing its `Category` instance.-}
newtype Indexed t m r i j = Indexed {runIndexed :: t i j m r}
instance
( IndexedMonadTrans t
( IxMonadTrans t
, Monad m
, Monoid r
) => Category (Indexed t m r) where
Expand Down
2 changes: 1 addition & 1 deletion src/Control/Monad/Trans/Indexed/Cont.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Control.Monad.Trans.Indexed

newtype ContIx i j m x = ContIx {runContIx :: (x -> m j) -> m i}
deriving Functor
instance IndexedMonadTrans ContIx where
instance IxMonadTrans ContIx where
joinIx (ContIx k) = ContIx $ \f -> k $ \(ContIx g) -> g f
instance i ~ j => Applicative (ContIx i j m) where
pure x = ContIx $ \k -> k x
Expand Down
6 changes: 3 additions & 3 deletions src/Control/Monad/Trans/Indexed/Do.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ import qualified Control.Monad.Trans.Indexed as Ix
import Prelude hiding ((>>=), (>>), fail)

(>>=)
:: (Ix.IndexedMonadTrans t, M.Monad m)
:: (Ix.IxMonadTrans t, M.Monad m)
=> t i j m x
-> (x -> t j k m y)
-> t i k m y
(>>=) = flip Ix.bindIx

(>>)
:: (Ix.IndexedMonadTrans t, M.Monad m)
:: (Ix.IxMonadTrans t, M.Monad m)
=> t i j m x
-> t j k m y
-> t i k m y
(>>) = flip Ix.thenIx

fail
:: (Ix.IndexedMonadTrans t, M.MonadFail m, i ~ j)
:: (Ix.IxMonadTrans t, M.MonadFail m, i ~ j)
=> String
-> t i j m x
fail = T.lift . M.fail
82 changes: 41 additions & 41 deletions src/Control/Monad/Trans/Indexed/Free.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}

module Control.Monad.Trans.Indexed.Free
( IxFree (liftIxFree, hoistIxFree, runIxFree), coerceIxFree
, IxFunctor, IxMap (IxMap), liftIxFreer, hoistIxFreer
( IxMonadTransFree (liftFreeIx, hoistFreeIx, foldFreeIx), coerceIxMonadTransFree
, IxFunctor, IxMap (IxMap), liftFreeIxr, hoistFreeIxr
) where

import Control.Monad.Free
import Control.Monad.Trans.Indexed
import Data.Kind

{- |
The free `IndexedMonadTrans` generated by an `IxFunctor`
is characterized by the `IxFree` class
up to the isomorphism `coerceIxFree`.
The free `IxMonadTrans` generated by an `IxFunctor`
is characterized by the `IxMonadTransFree` class
up to the isomorphism `coerceIxMonadTransFree`.
`IxFree` and `IxMap`, the free `IndexedMonadTrans` and
the free `IxFunctor`, can be combined as a "freer" `IndexedMonadTrans`
`IxMonadTransFree` and `IxMap`, the free `IxMonadTrans` and
the free `IxFunctor`, can be combined as a "freer" `IxMonadTrans`
and used as a DSL generated by primitive commands like this
[Conor McBride example]
(https://stackoverflow.com/questions/28690448/what-is-indexed-monad).
Expand All @@ -44,24 +44,24 @@ data DVDCommand
>>> :{
insert
:: (IxFree free, Monad m)
=> DVD -> free (IxMap DVDCommand) 'False 'True m ()
insert dvd = liftIxFreer (Insert dvd)
:: (IxMonadTransFree freeIx, Monad m)
=> DVD -> freeIx (IxMap DVDCommand) 'False 'True m ()
insert dvd = liftFreeIxr (Insert dvd)
:}
>>> :{
eject
:: (IxFree free, Monad m)
=> free (IxMap DVDCommand) 'True 'False m DVD
eject = liftIxFreer Eject
:: (IxMonadTransFree freeIx, Monad m)
=> freeIx (IxMap DVDCommand) 'True 'False m DVD
eject = liftFreeIxr Eject
:}
>>> :set -XQualifiedDo
>>> import qualified Control.Monad.Trans.Indexed.Do as Indexed
>>> :{
swap
:: (IxFree free, Monad m)
=> DVD -> free (IxMap DVDCommand) 'True 'True m DVD
:: (IxMonadTransFree freeIx, Monad m)
=> DVD -> freeIx (IxMap DVDCommand) 'True 'True m DVD
swap dvd = Indexed.do
dvd' <- eject
insert dvd
Expand All @@ -70,7 +70,7 @@ swap dvd = Indexed.do
>>> import Control.Monad.Trans
>>> :{
printDVD :: IxFree free => free (IxMap DVDCommand) 'True 'True IO ()
printDVD :: IxMonadTransFree freeIx => freeIx (IxMap DVDCommand) 'True 'True IO ()
printDVD = Indexed.do
dvd <- eject
insert dvd
Expand All @@ -79,30 +79,30 @@ printDVD = Indexed.do
-}
class
( forall f. IxFunctor f => IndexedMonadTrans (free f)
( forall f. IxFunctor f => IxMonadTrans (freeIx f)
, forall f m i j. (IxFunctor f, Monad m, i ~ j)
=> MonadFree (f i j) (free f i j m)
) => IxFree free where
liftIxFree
=> MonadFree (f i j) (freeIx f i j m)
) => IxMonadTransFree freeIx where
liftFreeIx
:: (IxFunctor f, Monad m)
=> f i j x
-> free f i j m x
hoistIxFree
-> freeIx f i j m x
hoistFreeIx
:: (IxFunctor f, IxFunctor g, Monad m)
=> (forall i j x. f i j x -> g i j x)
-> free f i j m x -> free g i j m x
runIxFree
:: (IxFunctor f, IndexedMonadTrans t, Monad m)
-> freeIx f i j m x -> freeIx g i j m x
foldFreeIx
:: (IxFunctor f, IxMonadTrans t, Monad m)
=> (forall i j x. f i j x -> t i j m x)
-> free f i j m x -> t i j m x
-> freeIx f i j m x -> t i j m x

{- |
prop> coerceIxFree = runIxFree liftIxFree
prop> coerceIxMonadTransFree = foldFreeIx liftFreeIx
-}
coerceIxFree
:: (IxFree free0, IxFree free1, IxFunctor f, Monad m)
=> free0 f i j m x -> free1 f i j m x
coerceIxFree = runIxFree liftIxFree
coerceIxMonadTransFree
:: (IxMonadTransFree freeIx0, IxMonadTransFree freeIx1, IxFunctor f, Monad m)
=> freeIx0 f i j m x -> freeIx1 f i j m x
coerceIxMonadTransFree = foldFreeIx liftFreeIx

type IxFunctor
:: (k -> k -> Type -> Type)
Expand All @@ -111,8 +111,8 @@ type IxFunctor f = forall i j. Functor (f i j)

{- |
`IxMap` is the free `IxFunctor`. It's a left Kan extension.
Combining `IxFree` with `IxMap` as demonstrated in the above example,
gives the "freer" `IndexedMonadTrans`, modeled on this
Combining `IxMonadTransFree` with `IxMap` as demonstrated in the above example,
gives the "freer" `IxMonadTrans`, modeled on this
[Oleg Kiselyov explanation]
(https://okmij.org/ftp/Computation/free-monad.html#freer).
-}
Expand All @@ -121,13 +121,13 @@ data IxMap f i j x where
instance Functor (IxMap f i j) where
fmap g (IxMap f x) = IxMap (g . f) x

liftIxFreer
:: (IxFree free, Monad m)
=> f i j x -> free (IxMap f) i j m x
liftIxFreer x = liftIxFree (IxMap id x)
liftFreeIxr
:: (IxMonadTransFree freeIx, Monad m)
=> f i j x -> freeIx (IxMap f) i j m x
liftFreeIxr x = liftFreeIx (IxMap id x)

hoistIxFreer
:: (IxFree free, Monad m)
hoistFreeIxr
:: (IxMonadTransFree freeIx, Monad m)
=> (forall i j x. f i j x -> g i j x)
-> free (IxMap f) i j m x -> free (IxMap g) i j m x
hoistIxFreer f = hoistIxFree (\(IxMap g x) -> IxMap g (f x))
-> freeIx (IxMap f) i j m x -> freeIx (IxMap g) i j m x
hoistFreeIxr f = hoistFreeIx (\(IxMap g x) -> IxMap g (f x))
14 changes: 7 additions & 7 deletions src/Control/Monad/Trans/Indexed/Free/Fold.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import Control.Monad.Trans.Indexed
import Control.Monad.Trans.Indexed.Free

newtype FreeIx g i j m x = FreeIx
{runFreeIx :: forall t. (IndexedMonadTrans t, Monad m)
{runFreeIx :: forall t. (IxMonadTrans t, Monad m)
=> (forall i j x. g i j x -> t i j m x) -> t i j m x}
instance (IxFunctor f, Monad m) => Functor (FreeIx f i j m) where
fmap f (FreeIx k) = FreeIx $ \step -> fmap f (k step)
Expand All @@ -36,15 +36,15 @@ instance (IxFunctor f, i ~ j)
=> MonadTrans (FreeIx f i j) where
lift m = FreeIx $ const $ lift m
instance IxFunctor f
=> IndexedMonadTrans (FreeIx f) where
=> IxMonadTrans (FreeIx f) where
joinIx (FreeIx g) = FreeIx $ \k -> bindIx (\(FreeIx f) -> f k) (g k)
instance
( IxFunctor f
, Monad m
, i ~ j
) => MonadFree (f i j) (FreeIx f i j m) where
wrap = join . liftIxFree
instance IxFree FreeIx where
liftIxFree m = FreeIx $ \k -> k m
hoistIxFree f (FreeIx k) = FreeIx $ \g -> k (g . f)
runIxFree f (FreeIx k) = k f
wrap = join . liftFreeIx
instance IxMonadTransFree FreeIx where
liftFreeIx m = FreeIx $ \k -> k m
hoistFreeIx f (FreeIx k) = FreeIx $ \g -> k (g . f)
foldFreeIx f (FreeIx k) = k f
14 changes: 7 additions & 7 deletions src/Control/Monad/Trans/Indexed/Free/Wrap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ instance (IxFunctor f, i ~ j)
=> MonadTrans (FreeIx f i j) where
lift = FreeIx . fmap Unwrap
instance IxFunctor f
=> IndexedMonadTrans (FreeIx f) where
=> IxMonadTrans (FreeIx f) where
joinIx (FreeIx mm) = FreeIx $ mm >>= \case
Unwrap (FreeIx m) -> m
Wrap fm -> return $ Wrap $ fmap joinIx fm
Expand All @@ -53,15 +53,15 @@ instance
, i ~ j
) => MonadFree (f i j) (FreeIx f i j m) where
wrap = FreeIx . return . Wrap
instance IxFree FreeIx where
liftIxFree = FreeIx . return . Wrap . fmap return
hoistIxFree f (FreeIx m) = FreeIx (fmap hoist_f m)
instance IxMonadTransFree FreeIx where
liftFreeIx = FreeIx . return . Wrap . fmap return
hoistFreeIx f (FreeIx m) = FreeIx (fmap hoist_f m)
where
hoist_f = \case
Unwrap x -> Unwrap x
Wrap y -> Wrap (f (fmap (hoistIxFree f) y))
runIxFree f (FreeIx m) = bindIx foldMap_f (lift m)
Wrap y -> Wrap (f (fmap (hoistFreeIx f) y))
foldFreeIx f (FreeIx m) = bindIx foldMap_f (lift m)
where
foldMap_f = \case
Unwrap x -> return x
Wrap y -> bindIx (runIxFree f) (f y)
Wrap y -> bindIx (foldFreeIx f) (f y)
2 changes: 1 addition & 1 deletion src/Control/Monad/Trans/Indexed/State.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import Control.Monad.Trans.Indexed

newtype StateIx i j m x = StateIx { runStateIx :: i -> m (x, j)}
deriving Functor
instance IndexedMonadTrans StateIx where
instance IxMonadTrans StateIx where
joinIx (StateIx f) = StateIx $ \i -> do
(StateIx g, j) <- f i
g j
Expand Down
2 changes: 1 addition & 1 deletion src/Control/Monad/Trans/Indexed/Writer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import Control.Monad.Trans.Indexed
newtype WriterIx w i j m x = WriterIx {runWriterIx :: m (x, w i j)}
deriving Functor

instance Category w => IndexedMonadTrans (WriterIx w) where
instance Category w => IxMonadTrans (WriterIx w) where
joinIx (WriterIx mm) = WriterIx $ do
(WriterIx m, ij) <- mm
(x, jk) <- m
Expand Down

0 comments on commit 0512e54

Please sign in to comment.