{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
#if __GLASGOW_HASKELL__ >= 704
{-# LANGUAGE DefaultSignatures #-}
#endif
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
module Control.Newtype
( Newtype(..)
, op
, ala
, ala'
, under
, over
, underF
, overF
) where
import Control.Applicative
import Control.Arrow
import Data.Fixed
import Data.Functor.Compose
import Data.Functor.Identity
import Data.Monoid
import Data.Ord
#if !MIN_VERSION_base(4,7,0)
import Unsafe.Coerce (unsafeCoerce)
#else
import Data.Coerce
#endif
class Newtype n o | n -> o where
pack :: o -> n
unpack :: n -> o
#if __GLASGOW_HASKELL__ >= 704
default pack :: Coercible o n => o -> n
pack = o -> n
coerce
default unpack :: Coercible n o => n -> o
unpack = n -> o
coerce
#endif
#if __GLASGOW_HASKELL__ >= 704 && !MIN_VERSION_base(4,7,0)
class Coercible o n
coerce :: a -> b
coerce = undefined
#endif
op :: Newtype n o => (o -> n) -> n -> o
op :: (o -> n) -> n -> o
op o -> n
_ = n -> o
forall n o. Newtype n o => n -> o
unpack
ala :: (Newtype n o, Newtype n' o') => (o -> n) -> ((o -> n) -> b -> n') -> (b -> o')
ala :: (o -> n) -> ((o -> n) -> b -> n') -> b -> o'
ala o -> n
pa (o -> n) -> b -> n'
hof = (o -> n) -> ((o -> n) -> b -> n') -> (o -> o) -> b -> o'
forall n o n' o' a b.
(Newtype n o, Newtype n' o') =>
(o -> n) -> ((a -> n) -> b -> n') -> (a -> o) -> b -> o'
ala' o -> n
pa (o -> n) -> b -> n'
hof o -> o
forall a. a -> a
id
ala' :: (Newtype n o, Newtype n' o') => (o -> n) -> ((a -> n) -> b -> n') -> (a -> o) -> (b -> o')
ala' :: (o -> n) -> ((a -> n) -> b -> n') -> (a -> o) -> b -> o'
ala' o -> n
_ (a -> n) -> b -> n'
hof a -> o
f = n' -> o'
forall n o. Newtype n o => n -> o
unpack (n' -> o') -> (b -> n') -> b -> o'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> n) -> b -> n'
hof (o -> n
forall n o. Newtype n o => o -> n
pack (o -> n) -> (a -> o) -> a -> n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> o
f)
under :: (Newtype n o, Newtype n' o') => (o -> n) -> (n -> n') -> (o -> o')
under :: (o -> n) -> (n -> n') -> o -> o'
under o -> n
_ n -> n'
f = n' -> o'
forall n o. Newtype n o => n -> o
unpack (n' -> o') -> (o -> n') -> o -> o'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. n -> n'
f (n -> n') -> (o -> n) -> o -> n'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. o -> n
forall n o. Newtype n o => o -> n
pack
over :: (Newtype n o, Newtype n' o') => (o -> n) -> (o -> o') -> (n -> n')
over :: (o -> n) -> (o -> o') -> n -> n'
over o -> n
_ o -> o'
f = o' -> n'
forall n o. Newtype n o => o -> n
pack (o' -> n') -> (n -> o') -> n -> n'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. o -> o'
f (o -> o') -> (n -> o) -> n -> o'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. n -> o
forall n o. Newtype n o => n -> o
unpack
underF :: (Newtype n o, Newtype n' o', Functor f) => (o -> n) -> (f n -> f n') -> (f o -> f o')
underF :: (o -> n) -> (f n -> f n') -> f o -> f o'
underF o -> n
_ f n -> f n'
f = (n' -> o') -> f n' -> f o'
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap n' -> o'
forall n o. Newtype n o => n -> o
unpack (f n' -> f o') -> (f o -> f n') -> f o -> f o'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f n -> f n'
f (f n -> f n') -> (f o -> f n) -> f o -> f n'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (o -> n) -> f o -> f n
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap o -> n
forall n o. Newtype n o => o -> n
pack
overF :: (Newtype n o, Newtype n' o', Functor f) => (o -> n) -> (f o -> f o') -> (f n -> f n')
overF :: (o -> n) -> (f o -> f o') -> f n -> f n'
overF o -> n
_ f o -> f o'
f = (o' -> n') -> f o' -> f n'
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap o' -> n'
forall n o. Newtype n o => o -> n
pack (f o' -> f n') -> (f n -> f o') -> f n -> f n'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f o -> f o'
f (f o -> f o') -> (f n -> f o) -> f n -> f o'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (n -> o) -> f n -> f o
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap n -> o
forall n o. Newtype n o => n -> o
unpack
instance Newtype All Bool where
pack :: Bool -> All
pack = Bool -> All
All
unpack :: All -> Bool
unpack (All Bool
a) = Bool
a
instance Newtype Any Bool where
pack :: Bool -> Any
pack = Bool -> Any
Any
unpack :: Any -> Bool
unpack (Any Bool
a) = Bool
a
instance Newtype (Sum a) a where
pack :: a -> Sum a
pack = a -> Sum a
forall a. a -> Sum a
Sum
unpack :: Sum a -> a
unpack (Sum a
a) = a
a
instance Newtype (Product a) a where
pack :: a -> Product a
pack = a -> Product a
forall a. a -> Product a
Product
unpack :: Product a -> a
unpack (Product a
a) = a
a
instance Newtype (Kleisli m a b) (a -> m b) where
pack :: (a -> m b) -> Kleisli m a b
pack = (a -> m b) -> Kleisli m a b
forall (m :: * -> *) a b. (a -> m b) -> Kleisli m a b
Kleisli
unpack :: Kleisli m a b -> a -> m b
unpack (Kleisli a -> m b
a) = a -> m b
a
instance Newtype (WrappedMonad m a) (m a) where
pack :: m a -> WrappedMonad m a
pack = m a -> WrappedMonad m a
forall (m :: * -> *) a. m a -> WrappedMonad m a
WrapMonad
unpack :: WrappedMonad m a -> m a
unpack (WrapMonad m a
a) = m a
a
instance Newtype (WrappedArrow a b c) (a b c) where
pack :: a b c -> WrappedArrow a b c
pack = a b c -> WrappedArrow a b c
forall (a :: * -> * -> *) b c. a b c -> WrappedArrow a b c
WrapArrow
unpack :: WrappedArrow a b c -> a b c
unpack (WrapArrow a b c
a) = a b c
a
instance Newtype (ZipList a) [a] where
pack :: [a] -> ZipList a
pack = [a] -> ZipList a
forall a. [a] -> ZipList a
ZipList
unpack :: ZipList a -> [a]
unpack (ZipList [a]
a) = [a]
a
instance Newtype (Const a x) a where
pack :: a -> Const a x
pack = a -> Const a x
forall k a (b :: k). a -> Const a b
Const
unpack :: Const a x -> a
unpack (Const a
a) = a
a
instance Newtype (Endo a) (a -> a) where
pack :: (a -> a) -> Endo a
pack = (a -> a) -> Endo a
forall a. (a -> a) -> Endo a
Endo
unpack :: Endo a -> a -> a
unpack (Endo a -> a
a) = a -> a
a
instance Newtype (First a) (Maybe a) where
pack :: Maybe a -> First a
pack = Maybe a -> First a
forall a. Maybe a -> First a
First
unpack :: First a -> Maybe a
unpack (First Maybe a
a) = Maybe a
a
instance Newtype (Last a) (Maybe a) where
pack :: Maybe a -> Last a
pack = Maybe a -> Last a
forall a. Maybe a -> Last a
Last
unpack :: Last a -> Maybe a
unpack (Last Maybe a
a) = Maybe a
a
instance ArrowApply a => Newtype (ArrowMonad a b) (a () b) where
pack :: a () b -> ArrowMonad a b
pack = a () b -> ArrowMonad a b
forall (a :: * -> * -> *) b. a () b -> ArrowMonad a b
ArrowMonad
unpack :: ArrowMonad a b -> a () b
unpack (ArrowMonad a () b
a) = a () b
a
instance Newtype (Fixed a) Integer where
#if MIN_VERSION_base(4,7,0)
pack :: Integer -> Fixed a
pack = Integer -> Fixed a
forall k (a :: k). Integer -> Fixed a
MkFixed
unpack :: Fixed a -> Integer
unpack (MkFixed Integer
x) = Integer
x
#else
pack = unsafeCoerce
unpack = unsafeCoerce
#endif
instance Newtype (Dual a) a where
pack :: a -> Dual a
pack = a -> Dual a
forall a. a -> Dual a
Dual
unpack :: Dual a -> a
unpack (Dual a
a) = a
a
#if MIN_VERSION_base(4,8,0)
instance Newtype (Alt f a) (f a) where
pack :: f a -> Alt f a
pack = f a -> Alt f a
forall k (f :: k -> *) (a :: k). f a -> Alt f a
Alt
unpack :: Alt f a -> f a
unpack (Alt f a
x) = f a
x
#endif
#if MIN_VERSION_base(4,6,0)
instance Newtype (Down a) a where
pack :: a -> Down a
pack = a -> Down a
forall a. a -> Down a
Down
unpack :: Down a -> a
unpack (Down a
a) = a
a
#endif
instance Newtype (Identity a) a where
pack :: a -> Identity a
pack = a -> Identity a
forall a. a -> Identity a
Identity
unpack :: Identity a -> a
unpack (Identity a
a) = a
a
instance Newtype (Compose f g a) (f (g a)) where
pack :: f (g a) -> Compose f g a
pack = f (g a) -> Compose f g a
forall k k1 (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose
unpack :: Compose f g a -> f (g a)
unpack (Compose f (g a)
x) = f (g a)
x
#if MIN_VERSION_base(4,12,0)
instance Newtype (Ap f a) (f a) where
pack :: f a -> Ap f a
pack = f a -> Ap f a
forall k (f :: k -> *) (a :: k). f a -> Ap f a
Ap
unpack :: Ap f a -> f a
unpack (Ap f a
x) = f a
x
#endif