Skip to content

Commit

Permalink
Merge pull request #90 from hvr/pr/wcompat
Browse files Browse the repository at this point in the history
Make machines forward-Wcompat clean
  • Loading branch information
glguy authored May 25, 2017
2 parents 2751702 + 00d42f8 commit 1be662b
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 7 deletions.
7 changes: 7 additions & 0 deletions machines.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ library
UndecidableInstances

ghc-options: -Wall -fwarn-tabs -O2 -fdicts-cheap -funbox-strict-fields

-- See https://ghc.haskell.org/trac/ghc/wiki/Migration/8.0#base-4.9.0.0
if impl(ghc >= 8.0)
ghc-options: -Wcompat -Wnoncanonical-monad-instances -Wnoncanonical-monadfail-instances
else
build-depends: fail == 4.9.*

hs-source-dirs: src

-- Verify the results of the examples
Expand Down
8 changes: 6 additions & 2 deletions src/Data/Machine/Is.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module Data.Machine.Is
) where

import Control.Category
import Data.Monoid
import Data.Semigroup
import Prelude

-- | Witnessed type equality
Expand All @@ -33,10 +33,14 @@ instance Ord (Is a b) where
Refl `compare` Refl = EQ
{-# INLINE compare #-}

instance (a ~ b) => Semigroup (Is a b) where
Refl <> Refl = Refl
{-# INLINE (<>) #-}

instance (a ~ b) => Monoid (Is a b) where
mempty = Refl
{-# INLINE mempty #-}
mappend Refl Refl = Refl
mappend = (<>)
{-# INLINE mappend #-}

instance (a ~ b) => Read (Is a b) where
Expand Down
4 changes: 2 additions & 2 deletions src/Data/Machine/Mealy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ unfoldMealy f = go where

-- | slow diagonalization
instance Monad (Mealy a) where
return b = r where r = Mealy (const (b, r))
return = pure
{-# INLINE return #-}
m >>= f = Mealy $ \a -> case runMealy m a of
(b, m') -> (fst (runMealy (f b) a), m' >>= f)
{-# INLINE (>>=) #-}
_ >> n = n
(>>) = (*>)
{-# INLINE (>>) #-}

instance Profunctor Mealy where
Expand Down
4 changes: 4 additions & 0 deletions src/Data/Machine/MealyT.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ instance Applicative m => Applicative (MealyT m a) where
MealyT m <*> MealyT n = MealyT $ \a -> (\(mb, mm) (nb, nm) -> (mb nb, mm <*> nm)) <$> m a <*> n a

instance Monad m => Monad (MealyT m a) where
#if !MIN_VERSION_base(4,8,0)
-- pre-AMP
{-# INLINE return #-}
return b = r where r = MealyT (const (return (b, r))) -- Stolen from Pointed
#endif

MealyT g >>= f = MealyT $ \a ->
do (b, MealyT _h) <- g a
runMealyT (f b) a
Expand Down
4 changes: 2 additions & 2 deletions src/Data/Machine/Moore.hs
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ instance Pointed (Moore a) where

-- | slow diagonalization
instance Monad (Moore a) where
return a = r where r = Moore a (const r)
return = pure
{-# INLINE return #-}
k >>= f = j (fmap f k) where
j (Moore a g) = Moore (extract a) (\x -> j $ fmap (\(Moore _ h) -> h x) (g x))
_ >> m = m
(>>) = (*>)

instance Copointed (Moore a) where
copoint (Moore b _) = b
Expand Down
6 changes: 5 additions & 1 deletion src/Data/Machine/Plan.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import Control.Monad.IO.Class
import Control.Monad.State.Class
import Control.Monad.Reader.Class
import Control.Monad.Error.Class
import qualified Control.Monad.Fail as Fail
import Control.Monad.Writer.Class
import Data.Functor.Identity
import Prelude hiding ((.),id)
Expand Down Expand Up @@ -114,10 +115,13 @@ instance Monad (PlanT k o m) where
return = pure
{-# INLINE return #-}
PlanT m >>= f = PlanT (\kp ke kr kf -> m (\a -> runPlanT (f a) kp ke kr kf) ke kr kf)
{-# INLINE (>>=) #-}
(>>) = (*>)
{-# INLINE (>>) #-}
fail = Fail.fail

instance Fail.MonadFail (PlanT k o m) where
fail _ = PlanT (\_ _ _ kf -> kf)
{-# INLINE (>>=) #-}

instance MonadPlus (PlanT k o m) where
mzero = empty
Expand Down

0 comments on commit 1be662b

Please sign in to comment.