Skip to content
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

Improve pre-empty-case situation #78

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions generic-deriving.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ library
Generics.Deriving.Monoid.Internal
Generics.Deriving.Semigroup.Internal
Generics.Deriving.TH.Internal
Generics.Deriving.Absurd
Paths_generic_deriving
if flag(base-4-9)
build-depends: base >= 4.9 && < 5
Expand Down
24 changes: 24 additions & 0 deletions src/Generics/Deriving/Absurd.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{-# language CPP #-}
#if __GLASGOW_HASKELL__ >= 708
{-# language EmptyCase #-}
#endif
module Generics.Deriving.Absurd where
import GHC.Generics
#if __GLASGOW_HASKELL__ < 708
import GHC.Conc (pseq)
#endif

absurd1 :: V1 a -> b
absurd1 x =
#if __GLASGOW_HASKELL__ >= 708
case x of {}
#else
-- Using pseq instead of seq or a bang pattern guarantees
-- that the impossible error will never be reached.
pseq x impossible
#endif
{-# INLINE absurd1 #-}

impossible :: a
impossible = error "Utterly impossible"
{-# NOINLINE impossible #-}
14 changes: 3 additions & 11 deletions src/Generics/Deriving/Functor.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
Expand All @@ -13,10 +12,6 @@
{-# LANGUAGE PolyKinds #-}
#endif

#if __GLASGOW_HASKELL__ >= 708
{-# LANGUAGE EmptyCase #-}
#endif

#if __GLASGOW_HASKELL__ >= 710
{-# LANGUAGE Safe #-}
#elif __GLASGOW_HASKELL__ >= 701
Expand Down Expand Up @@ -69,6 +64,8 @@ import qualified Data.Semigroup as Semigroup (First, Last)
import Data.Semigroup (Arg, Max, Min, WrappedMonoid)
#endif

import Generics.Deriving.Absurd (absurd1)

--------------------------------------------------------------------------------
-- Generic fmap
--------------------------------------------------------------------------------
Expand All @@ -77,12 +74,7 @@ class GFunctor' f where
gmap' :: (a -> b) -> f a -> f b

instance GFunctor' V1 where
gmap' _ x = case x of
#if __GLASGOW_HASKELL__ >= 708
{}
#else
!_ -> error "Void gmap"
#endif
gmap' _ x = absurd1 x

instance GFunctor' U1 where
gmap' _ U1 = U1
Expand Down
13 changes: 2 additions & 11 deletions src/Generics/Deriving/Show.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
Expand All @@ -12,10 +11,6 @@
{-# LANGUAGE Trustworthy #-}
#endif

#if __GLASGOW_HASKELL__ >= 708
{-# LANGUAGE EmptyCase #-}
#endif

module Generics.Deriving.Show (
-- * Generic show class
GShow(..)
Expand All @@ -42,6 +37,7 @@ import Foreign.ForeignPtr (ForeignPtr)
import Foreign.Ptr

import Generics.Deriving.Base
import Generics.Deriving.Absurd (absurd1)

import GHC.Exts hiding (Any)

Expand Down Expand Up @@ -91,12 +87,7 @@ class GShow' f where
isNullary = error "generic show (isNullary): unnecessary case"

instance GShow' V1 where
gshowsPrec' _ _ x = case x of
#if __GLASGOW_HASKELL__ >= 708
{}
#else
!_ -> error "Void gshowsPrec"
#endif
gshowsPrec' _ _ x = absurd1 x

instance GShow' U1 where
gshowsPrec' _ _ U1 = id
Expand Down
13 changes: 2 additions & 11 deletions src/Generics/Deriving/Traversable.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
Expand All @@ -13,10 +12,6 @@
{-# LANGUAGE PolyKinds #-}
#endif

#if __GLASGOW_HASKELL__ >= 708
{-# LANGUAGE EmptyCase #-}
#endif

#if __GLASGOW_HASKELL__ >= 710
{-# LANGUAGE Safe #-}
#elif __GLASGOW_HASKELL__ >= 701
Expand Down Expand Up @@ -46,6 +41,7 @@ import Data.Monoid (Dual)
import Generics.Deriving.Base
import Generics.Deriving.Foldable
import Generics.Deriving.Functor
import Generics.Deriving.Absurd

#if MIN_VERSION_base(4,4,0)
import Data.Complex (Complex)
Expand Down Expand Up @@ -81,12 +77,7 @@ class GTraversable' t where
gtraverse' :: Applicative f => (a -> f b) -> t a -> f (t b)

instance GTraversable' V1 where
gtraverse' _ x = pure $ case x of
#if __GLASGOW_HASKELL__ >= 708
{}
#else
!_ -> error "Void gtraverse"
#endif
gtraverse' _ x = absurd1 x

instance GTraversable' U1 where
gtraverse' _ U1 = pure U1
Expand Down