1
0
forked from GitHub/gf-core

define return in terms of pure, >> as *>, mappend as <>

In preparation for deprecation, see https://gitlab.haskell.org/ghc/ghc/-/wikis/proposal/semigroup-monoid and https://gitlab.haskell.org/ghc/ghc/-/wikis/proposal/monad-of-no-return
This commit is contained in:
Inari Listenmaa
2024-09-09 19:43:39 +02:00
parent 1037b209ae
commit b914a25de3
12 changed files with 30 additions and 28 deletions

View File

@@ -201,11 +201,11 @@ instance Fail.MonadFail CnvMonad where
fail = bug fail = bug
instance Applicative CnvMonad where instance Applicative CnvMonad where
pure = return pure a = CM (\gr c s -> c a s)
(<*>) = ap (<*>) = ap
instance Monad CnvMonad where instance Monad CnvMonad where
return a = CM (\gr c s -> c a s) return = pure
CM m >>= k = CM (\gr c s -> m gr (\a s -> unCM (k a) gr c s) s) CM m >>= k = CM (\gr c s -> m gr (\a s -> unCM (k a) gr c s) s)
instance MonadState ([ProtoFCat],[Symbol]) CnvMonad where instance MonadState ([ProtoFCat],[Symbol]) CnvMonad where

View File

@@ -644,7 +644,7 @@ data TcResult a
newtype TcM a = TcM {unTcM :: MetaStore -> [Message] -> TcResult a} newtype TcM a = TcM {unTcM :: MetaStore -> [Message] -> TcResult a}
instance Monad TcM where instance Monad TcM where
return x = TcM (\ms msgs -> TcOk x ms msgs) return = pure
f >>= g = TcM (\ms msgs -> case unTcM f ms msgs of f >>= g = TcM (\ms msgs -> case unTcM f ms msgs of
TcOk x ms msgs -> unTcM (g x) ms msgs TcOk x ms msgs -> unTcM (g x) ms msgs
TcFail msgs -> TcFail msgs) TcFail msgs -> TcFail msgs)
@@ -659,7 +659,7 @@ instance Fail.MonadFail TcM where
instance Applicative TcM where instance Applicative TcM where
pure = return pure x = TcM (\ms msgs -> TcOk x ms msgs)
(<*>) = ap (<*>) = ap
instance Functor TcM where instance Functor TcM where

View File

@@ -239,11 +239,11 @@ instance Functor m => Functor (CollectOutput m) where
fmap f (CO m) = CO (fmap (fmap f) m) fmap f (CO m) = CO (fmap (fmap f) m)
instance (Functor m,Monad m) => Applicative (CollectOutput m) where instance (Functor m,Monad m) => Applicative (CollectOutput m) where
pure = return pure x = CO (return (return (),x))
(<*>) = ap (<*>) = ap
instance Monad m => Monad (CollectOutput m) where instance Monad m => Monad (CollectOutput m) where
return x = CO (return (return (),x)) return = pure
CO m >>= f = CO $ do (o1,x) <- m CO m >>= f = CO $ do (o1,x) <- m
let CO m2 = f x let CO m2 = f x
(o2,y) <- m2 (o2,y) <- m2

View File

@@ -64,11 +64,11 @@ finalStates :: BacktrackM s () -> s -> [s]
finalStates bm = map fst . runBM bm finalStates bm = map fst . runBM bm
instance Applicative (BacktrackM s) where instance Applicative (BacktrackM s) where
pure = return pure a = BM (\c s b -> c a s b)
(<*>) = ap (<*>) = ap
instance Monad (BacktrackM s) where instance Monad (BacktrackM s) where
return a = BM (\c s b -> c a s b) return = pure
BM m >>= k = BM (\c s b -> m (\a s b -> unBM (k a) c s b) s b) BM m >>= k = BM (\c s b -> m (\a s b -> unBM (k a) c s b) s b)
where unBM (BM m) = m where unBM (BM m) = m

View File

@@ -34,7 +34,7 @@ fromErr :: a -> Err a -> a
fromErr a = err (const a) id fromErr a = err (const a) id
instance Monad Err where instance Monad Err where
return = Ok return = pure
Ok a >>= f = f a Ok a >>= f = f a
Bad s >>= f = Bad s Bad s >>= f = Bad s
@@ -54,7 +54,7 @@ instance Functor Err where
fmap f (Bad s) = Bad s fmap f (Bad s) = Bad s
instance Applicative Err where instance Applicative Err where
pure = return pure = Ok
(<*>) = ap (<*>) = ap
-- | added by KJ -- | added by KJ

View File

@@ -283,11 +283,11 @@ instance Functor P where
fmap = liftA fmap = liftA
instance Applicative P where instance Applicative P where
pure = return pure a = a `seq` (P $ \s -> POk s a)
(<*>) = ap (<*>) = ap
instance Monad P where instance Monad P where
return a = a `seq` (P $ \s -> POk s a) return = pure
(P m) >>= k = P $ \ s -> case m s of (P m) >>= k = P $ \ s -> case m s of
POk s a -> unP (k a) s POk s a -> unP (k a) s
PFailed posn err -> PFailed posn err PFailed posn err -> PFailed posn err

View File

@@ -48,7 +48,7 @@ newtype Check a
instance Functor Check where fmap = liftM instance Functor Check where fmap = liftM
instance Monad Check where instance Monad Check where
return x = Check $ \{-ctxt-} ws -> (ws,Success x) return = pure
f >>= g = Check $ \{-ctxt-} ws -> f >>= g = Check $ \{-ctxt-} ws ->
case unCheck f {-ctxt-} ws of case unCheck f {-ctxt-} ws of
(ws,Success x) -> unCheck (g x) {-ctxt-} ws (ws,Success x) -> unCheck (g x) {-ctxt-} ws
@@ -58,7 +58,7 @@ instance Fail.MonadFail Check where
fail = raise fail = raise
instance Applicative Check where instance Applicative Check where
pure = return pure x = Check $ \{-ctxt-} ws -> (ws,Success x)
(<*>) = ap (<*>) = ap
instance ErrorMonad Check where instance ErrorMonad Check where

View File

@@ -52,11 +52,11 @@ newtype SIO a = SIO {unS::PutStr->IO a}
instance Functor SIO where fmap = liftM instance Functor SIO where fmap = liftM
instance Applicative SIO where instance Applicative SIO where
pure = return pure x = SIO (const (pure x))
(<*>) = ap (<*>) = ap
instance Monad SIO where instance Monad SIO where
return x = SIO (const (return x)) return = pure
SIO m1 >>= xm2 = SIO $ \ h -> m1 h >>= \ x -> unS (xm2 x) h SIO m1 >>= xm2 = SIO $ \ h -> m1 h >>= \ x -> unS (xm2 x) h
instance Fail.MonadFail SIO where instance Fail.MonadFail SIO where

View File

@@ -114,7 +114,7 @@ instance Semigroup Builder where
instance Monoid Builder where instance Monoid Builder where
mempty = empty mempty = empty
{-# INLINE mempty #-} {-# INLINE mempty #-}
mappend = append mappend = (<>)
{-# INLINE mappend #-} {-# INLINE mappend #-}
------------------------------------------------------------------------ ------------------------------------------------------------------------

View File

@@ -127,11 +127,11 @@ instance Functor Get where
{-# INLINE fmap #-} {-# INLINE fmap #-}
instance Applicative Get where instance Applicative Get where
pure = return pure a = Get (\s -> (a, s))
(<*>) = ap (<*>) = ap
instance Monad Get where instance Monad Get where
return a = Get (\s -> (a, s)) return = pure
{-# INLINE return #-} {-# INLINE return #-}
m >>= k = Get (\s -> case unGet m s of m >>= k = Get (\s -> case unGet m s of

View File

@@ -77,15 +77,20 @@ instance Functor PutM where
{-# INLINE fmap #-} {-# INLINE fmap #-}
instance Applicative PutM where instance Applicative PutM where
pure = return pure a = Put $ PairS a mempty
m <*> k = Put $ m <*> k = Put $
let PairS f w = unPut m let PairS f w = unPut m
PairS x w' = unPut k PairS x w' = unPut k
in PairS (f x) (w `mappend` w') in PairS (f x) (w `mappend` w')
m *> k = Put $
let PairS _ w = unPut m
PairS b w' = unPut k
in PairS b (w `mappend` w')
{-# INLINE (*>) #-}
-- Standard Writer monad, with aggressive inlining -- Standard Writer monad, with aggressive inlining
instance Monad PutM where instance Monad PutM where
return a = Put $ PairS a mempty return = pure
{-# INLINE return #-} {-# INLINE return #-}
m >>= k = Put $ m >>= k = Put $
@@ -94,10 +99,7 @@ instance Monad PutM where
in PairS b (w `mappend` w') in PairS b (w `mappend` w')
{-# INLINE (>>=) #-} {-# INLINE (>>=) #-}
m >> k = Put $ (>>) = (*>)
let PairS _ w = unPut m
PairS b w' = unPut k
in PairS b (w `mappend` w')
{-# INLINE (>>) #-} {-# INLINE (>>) #-}
tell :: Builder -> Put tell :: Builder -> Put

View File

@@ -94,11 +94,11 @@ class Selector s where
select :: CId -> Scope -> Maybe Int -> TcM s (Expr,TType) select :: CId -> Scope -> Maybe Int -> TcM s (Expr,TType)
instance Applicative (TcM s) where instance Applicative (TcM s) where
pure = return pure x = TcM (\abstr k h -> k x)
(<*>) = ap (<*>) = ap
instance Monad (TcM s) where instance Monad (TcM s) where
return x = TcM (\abstr k h -> k x) return = pure
f >>= g = TcM (\abstr k h -> unTcM f abstr (\x -> unTcM (g x) abstr k h) h) f >>= g = TcM (\abstr k h -> unTcM f abstr (\x -> unTcM (g x) abstr k h) h)
instance Selector s => Alternative (TcM s) where instance Selector s => Alternative (TcM s) where