Jana Chadt pushed to branch wip/VeryMilkyJoe/mv-const-base at Glasgow Haskell Compiler / GHC
Commits:
-
f2044a46
by Jana Chadt at 2026-02-25T12:39:35+01:00
9 changed files:
- libraries/base/src/Data/Functor/Const.hs
- libraries/base/src/Data/Functor/Identity.hs
- libraries/base/src/Data/Traversable.hs
- libraries/ghc-internal/ghc-internal.cabal.in
- libraries/ghc-internal/src/GHC/Internal/Control/Monad/Zip.hs
- − libraries/ghc-internal/src/GHC/Internal/Data/Functor/Const.hs
- − libraries/ghc-internal/src/GHC/Internal/Data/Functor/Identity.hs
- libraries/ghc-internal/src/GHC/Internal/Data/String.hs
- libraries/ghc-internal/src/GHC/Internal/Data/Traversable.hs
Changes:
| 1 | +{-# LANGUAGE DeriveGeneric #-}
|
|
| 2 | +{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
|
| 3 | +{-# LANGUAGE NoImplicitPrelude #-}
|
|
| 4 | +{-# LANGUAGE PolyKinds #-}
|
|
| 1 | 5 | {-# LANGUAGE Safe #-}
|
| 6 | +{-# LANGUAGE ScopedTypeVariables #-}
|
|
| 7 | +{-# LANGUAGE Trustworthy #-}
|
|
| 2 | 8 | |
| 9 | +-----------------------------------------------------------------------------
|
|
| 3 | 10 | -- |
|
| 4 | 11 | --
|
| 5 | 12 | -- Module : Data.Functor.Const
|
| ... | ... | @@ -9,9 +16,105 @@ |
| 9 | 16 | -- Maintainer : libraries@haskell.org
|
| 10 | 17 | -- Stability : stable
|
| 11 | 18 | -- Portability : portable
|
| 19 | +-----------------------------------------------------------------------------
|
|
| 20 | + |
|
| 21 | +-- The 'Const' functor.
|
|
| 22 | +--
|
|
| 23 | +-- @since base-4.9.0.0
|
|
| 12 | 24 | |
| 13 | 25 | module Data.Functor.Const
|
| 14 | 26 | (Const(..)
|
| 15 | 27 | ) where
|
| 16 | 28 | |
| 17 | -import GHC.Internal.Data.Functor.Const |
|
| \ No newline at end of file | ||
| 29 | +import GHC.Internal.Data.Bits (Bits, FiniteBits)
|
|
| 30 | +import GHC.Internal.Data.Foldable (Foldable(foldMap))
|
|
| 31 | +import GHC.Internal.Data.String (IsString)
|
|
| 32 | +import GHC.Internal.Foreign.Storable (Storable)
|
|
| 33 | + |
|
| 34 | +import GHC.Internal.Ix (Ix)
|
|
| 35 | +import GHC.Internal.Base
|
|
| 36 | +import GHC.Internal.Enum (Bounded, Enum)
|
|
| 37 | +import GHC.Internal.Float (Floating, RealFloat)
|
|
| 38 | +import GHC.Internal.Generics (Generic, Generic1)
|
|
| 39 | +import GHC.Internal.Num (Num)
|
|
| 40 | +import GHC.Internal.Real (Fractional, Integral, Real, RealFrac)
|
|
| 41 | +import GHC.Internal.Read (Read(readsPrec), readParen, lex)
|
|
| 42 | +import GHC.Internal.Show (Show(showsPrec), showParen, showString)
|
|
| 43 | + |
|
| 44 | +-- | The 'Const' functor.
|
|
| 45 | +--
|
|
| 46 | +-- ==== __Examples__
|
|
| 47 | +--
|
|
| 48 | +-- >>> fmap (++ "World") (Const "Hello")
|
|
| 49 | +-- Const "Hello"
|
|
| 50 | +--
|
|
| 51 | +-- Because we ignore the second type parameter to 'Const',
|
|
| 52 | +-- the Applicative instance, which has
|
|
| 53 | +-- @'(<*>)' :: Monoid m => Const m (a -> b) -> Const m a -> Const m b@
|
|
| 54 | +-- essentially turns into @Monoid m => m -> m -> m@, which is '(<>)'
|
|
| 55 | +--
|
|
| 56 | +-- >>> Const [1, 2, 3] <*> Const [4, 5, 6]
|
|
| 57 | +-- Const [1,2,3,4,5,6]
|
|
| 58 | +newtype Const a b = Const { getConst :: a }
|
|
| 59 | + deriving ( Bits -- ^ @since base-4.9.0.0
|
|
| 60 | + , Bounded -- ^ @since base-4.9.0.0
|
|
| 61 | + , Enum -- ^ @since base-4.9.0.0
|
|
| 62 | + , Eq -- ^ @since base-4.9.0.0
|
|
| 63 | + , FiniteBits -- ^ @since base-4.9.0.0
|
|
| 64 | + , Floating -- ^ @since base-4.9.0.0
|
|
| 65 | + , Fractional -- ^ @since base-4.9.0.0
|
|
| 66 | + , Generic -- ^ @since base-4.9.0.0
|
|
| 67 | + , Generic1 -- ^ @since base-4.9.0.0
|
|
| 68 | + , Integral -- ^ @since base-4.9.0.0
|
|
| 69 | + , Ix -- ^ @since base-4.9.0.0
|
|
| 70 | + , Semigroup -- ^ @since base-4.9.0.0
|
|
| 71 | + , Monoid -- ^ @since base-4.9.0.0
|
|
| 72 | + , Num -- ^ @since base-4.9.0.0
|
|
| 73 | + , Ord -- ^ @since base-4.9.0.0
|
|
| 74 | + , Real -- ^ @since base-4.9.0.0
|
|
| 75 | + , RealFrac -- ^ @since base-4.9.0.0
|
|
| 76 | + , RealFloat -- ^ @since base-4.9.0.0
|
|
| 77 | + , Storable -- ^ @since base-4.9.0.0
|
|
| 78 | + )
|
|
| 79 | + |
|
| 80 | +-- | This instance would be equivalent to the derived instances of the
|
|
| 81 | +-- 'Const' newtype if the 'getConst' field were removed
|
|
| 82 | +--
|
|
| 83 | +-- @since base-4.8.0.0
|
|
| 84 | +instance Read a => Read (Const a b) where
|
|
| 85 | + readsPrec d = readParen (d > 10)
|
|
| 86 | + $ \r -> [(Const x,t) | ("Const", s) <- lex r, (x, t) <- readsPrec 11 s]
|
|
| 87 | + |
|
| 88 | +-- | This instance would be equivalent to the derived instances of the
|
|
| 89 | +-- 'Const' newtype if the 'getConst' field were removed
|
|
| 90 | +--
|
|
| 91 | +-- @since base-4.8.0.0
|
|
| 92 | +instance Show a => Show (Const a b) where
|
|
| 93 | + showsPrec d (Const x) = showParen (d > 10) $
|
|
| 94 | + showString "Const " . showsPrec 11 x
|
|
| 95 | + |
|
| 96 | +-- | @since base-4.7.0.0
|
|
| 97 | +instance Foldable (Const m) where
|
|
| 98 | + foldMap _ _ = mempty
|
|
| 99 | + |
|
| 100 | +-- | @since base-2.01
|
|
| 101 | +instance Functor (Const m) where
|
|
| 102 | + fmap _ (Const v) = Const v
|
|
| 103 | + |
|
| 104 | +-- | @since base-2.0.1
|
|
| 105 | +instance Monoid m => Applicative (Const m) where
|
|
| 106 | + pure _ = Const mempty
|
|
| 107 | + liftA2 _ (Const x) (Const y) = Const (x `mappend` y)
|
|
| 108 | + (<*>) = coerce (mappend :: m -> m -> m)
|
|
| 109 | +-- This is pretty much the same as
|
|
| 110 | +-- Const f <*> Const v = Const (f `mappend` v)
|
|
| 111 | +-- but guarantees that mappend for Const a b will have the same arity
|
|
| 112 | +-- as the one for a; it won't create a closure to raise the arity
|
|
| 113 | +-- to 2.
|
|
| 114 | + |
|
| 115 | +-- | @since base-4.7.0.0
|
|
| 116 | +instance Traversable (Const m) where
|
|
| 117 | + traverse _ (Const m) = pure $ Const m
|
|
| 118 | + |
|
| 119 | +-- | @since base-4.9.0.0
|
|
| 120 | +deriving instance IsString a => IsString (Const a (b :: k)) |
| ... | ... | @@ -29,4 +29,130 @@ module Data.Functor.Identity |
| 29 | 29 | (Identity(..)
|
| 30 | 30 | ) where
|
| 31 | 31 | |
| 32 | -import GHC.Internal.Data.Functor.Identity |
|
| \ No newline at end of file | ||
| 32 | +import GHC.Internal.Control.Monad.Fix
|
|
| 33 | +import GHC.Internal.Data.Bits (Bits, FiniteBits)
|
|
| 34 | +import GHC.Internal.Data.Coerce
|
|
| 35 | +import GHC.Internal.Data.Foldable
|
|
| 36 | +import GHC.Internal.Data.Functor.Utils ((#.))
|
|
| 37 | +import GHC.Internal.Data.String (IsString)
|
|
| 38 | +import GHC.Internal.Foreign.Storable (Storable)
|
|
| 39 | +import GHC.Internal.Ix (Ix)
|
|
| 40 | +import GHC.Internal.Base ( Applicative(..), Eq(..), Functor(..), Monad(..)
|
|
| 41 | + , Semigroup, Monoid, Ord(..), ($), (.) )
|
|
| 42 | +import GHC.Internal.Enum (Bounded, Enum)
|
|
| 43 | +import GHC.Internal.Float (Floating, RealFloat)
|
|
| 44 | +import GHC.Internal.Generics (Generic, Generic1)
|
|
| 45 | +import GHC.Internal.Num (Num)
|
|
| 46 | +import GHC.Internal.Read (Read(..), lex, readParen)
|
|
| 47 | +import GHC.Internal.Real (Fractional, Integral, Real, RealFrac)
|
|
| 48 | +import GHC.Internal.Show (Show(..), showParen, showString)
|
|
| 49 | +import GHC.Internal.Types (Bool(..))
|
|
| 50 | +import GHC.Internal.Control.Monad.Zip (MonadZip(..))
|
|
| 51 | + |
|
| 52 | +-- | Identity functor and monad. (a non-strict monad)
|
|
| 53 | +--
|
|
| 54 | +-- ==== __Examples__
|
|
| 55 | +--
|
|
| 56 | +-- >>> fmap (+1) (Identity 0)
|
|
| 57 | +-- Identity 1
|
|
| 58 | +--
|
|
| 59 | +-- >>> Identity [1, 2, 3] <> Identity [4, 5, 6]
|
|
| 60 | +-- Identity [1,2,3,4,5,6]
|
|
| 61 | +--
|
|
| 62 | +-- @
|
|
| 63 | +-- >>> do
|
|
| 64 | +-- x <- Identity 10
|
|
| 65 | +-- y <- Identity (x + 5)
|
|
| 66 | +-- pure (x + y)
|
|
| 67 | +-- Identity 25
|
|
| 68 | +-- @
|
|
| 69 | +--
|
|
| 70 | +-- @since base-4.8.0.0
|
|
| 71 | +newtype Identity a = Identity { runIdentity :: a }
|
|
| 72 | + deriving ( Bits -- ^ @since base-4.9.0.0
|
|
| 73 | + , Bounded -- ^ @since base-4.9.0.0
|
|
| 74 | + , Enum -- ^ @since base-4.9.0.0
|
|
| 75 | + , Eq -- ^ @since base-4.8.0.0
|
|
| 76 | + , FiniteBits -- ^ @since base-4.9.0.0
|
|
| 77 | + , Floating -- ^ @since base-4.9.0.0
|
|
| 78 | + , Fractional -- ^ @since base-4.9.0.0
|
|
| 79 | + , Generic -- ^ @since base-4.8.0.0
|
|
| 80 | + , Generic1 -- ^ @since base-4.8.0.0
|
|
| 81 | + , Integral -- ^ @since base-4.9.0.0
|
|
| 82 | + , Ix -- ^ @since base-4.9.0.0
|
|
| 83 | + , Semigroup -- ^ @since base-4.9.0.0
|
|
| 84 | + , Monoid -- ^ @since base-4.9.0.0
|
|
| 85 | + , Num -- ^ @since base-4.9.0.0
|
|
| 86 | + , Ord -- ^ @since base-4.8.0.0
|
|
| 87 | + , Real -- ^ @since base-4.9.0.0
|
|
| 88 | + , RealFrac -- ^ @since base-4.9.0.0
|
|
| 89 | + , RealFloat -- ^ @since base-4.9.0.0
|
|
| 90 | + , Storable -- ^ @since base-4.9.0.0
|
|
| 91 | + )
|
|
| 92 | + |
|
| 93 | +-- | This instance would be equivalent to the derived instances of the
|
|
| 94 | +-- 'Identity' newtype if the 'runIdentity' field were removed
|
|
| 95 | +--
|
|
| 96 | +-- @since base-4.8.0.0
|
|
| 97 | +instance (Read a) => Read (Identity a) where
|
|
| 98 | + readsPrec d = readParen (d > 10) $ \ r ->
|
|
| 99 | + [(Identity x,t) | ("Identity",s) <- lex r, (x,t) <- readsPrec 11 s]
|
|
| 100 | + |
|
| 101 | +-- | This instance would be equivalent to the derived instances of the
|
|
| 102 | +-- 'Identity' newtype if the 'runIdentity' field were removed
|
|
| 103 | +--
|
|
| 104 | +-- @since base-4.8.0.0
|
|
| 105 | +instance (Show a) => Show (Identity a) where
|
|
| 106 | + showsPrec d (Identity x) = showParen (d > 10) $
|
|
| 107 | + showString "Identity " . showsPrec 11 x
|
|
| 108 | + |
|
| 109 | +-- ---------------------------------------------------------------------------
|
|
| 110 | +-- Identity instances for Functor and Monad
|
|
| 111 | + |
|
| 112 | +-- | @since base-4.8.0.0
|
|
| 113 | +instance Foldable Identity where
|
|
| 114 | + foldMap = coerce
|
|
| 115 | + |
|
| 116 | + elem = (. runIdentity) #. (==)
|
|
| 117 | + foldl = coerce
|
|
| 118 | + foldl' = coerce
|
|
| 119 | + foldl1 _ = runIdentity
|
|
| 120 | + foldr f z (Identity x) = f x z
|
|
| 121 | + foldr' = foldr
|
|
| 122 | + foldr1 _ = runIdentity
|
|
| 123 | + length _ = 1
|
|
| 124 | + maximum = runIdentity
|
|
| 125 | + minimum = runIdentity
|
|
| 126 | + null _ = False
|
|
| 127 | + product = runIdentity
|
|
| 128 | + sum = runIdentity
|
|
| 129 | + toList (Identity x) = [x]
|
|
| 130 | + |
|
| 131 | +-- | @since base-4.8.0.0
|
|
| 132 | +instance Functor Identity where
|
|
| 133 | + fmap = coerce
|
|
| 134 | + |
|
| 135 | +-- | @since base-4.8.0.0
|
|
| 136 | +instance Applicative Identity where
|
|
| 137 | + pure = Identity
|
|
| 138 | + (<*>) = coerce
|
|
| 139 | + liftA2 = coerce
|
|
| 140 | + |
|
| 141 | +-- | @since base-4.8.0.0
|
|
| 142 | +instance Monad Identity where
|
|
| 143 | + m >>= k = k (runIdentity m)
|
|
| 144 | + |
|
| 145 | +-- | @since base-4.8.0.0
|
|
| 146 | +instance MonadFix Identity where
|
|
| 147 | + mfix f = Identity (fix (runIdentity . f))
|
|
| 148 | + |
|
| 149 | +-- | @since 4.8.0.0
|
|
| 150 | +instance MonadZip Identity where
|
|
| 151 | + mzipWith = liftM2
|
|
| 152 | + munzip (Identity (a, b)) = (Identity a, Identity b)
|
|
| 153 | + |
|
| 154 | +-- | @since base-4.9.0.0
|
|
| 155 | +deriving instance Traversable Identity
|
|
| 156 | + |
|
| 157 | +-- | @since base-4.9.0.0
|
|
| 158 | +deriving instance IsString a => IsString (Identity a) |
| ... | ... | @@ -86,6 +86,32 @@ module Data.Traversable ( |
| 86 | 86 | |
| 87 | 87 | import GHC.Internal.Data.Traversable
|
| 88 | 88 | |
| 89 | +-- | This function may be used as a value for `fmap` in a `Functor`
|
|
| 90 | +-- instance, provided that 'traverse' is defined. (Using
|
|
| 91 | +-- `fmapDefault` with a `Traversable` instance defined only by
|
|
| 92 | +-- 'sequenceA' will result in infinite recursion.)
|
|
| 93 | +--
|
|
| 94 | +-- @
|
|
| 95 | +-- 'fmapDefault' f ≡ 'runIdentity' . 'traverse' ('Identity' . f)
|
|
| 96 | +-- @
|
|
| 97 | +fmapDefault :: forall t a b . Traversable t
|
|
| 98 | + => (a -> b) -> t a -> t b
|
|
| 99 | +{-# INLINE fmapDefault #-}
|
|
| 100 | +-- See Note [Function coercion] in Data.Functor.Utils.
|
|
| 101 | +fmapDefault = coerce (traverse @t @Identity @a @b)
|
|
| 102 | + |
|
| 103 | +-- | This function may be used as a value for `Data.Foldable.foldMap`
|
|
| 104 | +-- in a `Foldable` instance.
|
|
| 105 | +--
|
|
| 106 | +-- @
|
|
| 107 | +-- 'foldMapDefault' f ≡ 'getConst' . 'traverse' ('Const' . f)
|
|
| 108 | +-- @
|
|
| 109 | +foldMapDefault :: forall t m a . (Traversable t, Monoid m)
|
|
| 110 | + => (a -> m) -> t a -> m
|
|
| 111 | +{-# INLINE foldMapDefault #-}
|
|
| 112 | +-- See Note [Function coercion] in Data.Functor.Utils.
|
|
| 113 | +foldMapDefault = coerce (traverse @t @(Const m) @a @())
|
|
| 114 | + |
|
| 89 | 115 | -- $setup
|
| 90 | 116 | -- >>> import Prelude
|
| 91 | 117 | -- >>> import Data.Maybe
|
| ... | ... | @@ -148,8 +148,6 @@ Library |
| 148 | 148 | GHC.Internal.Data.Foldable
|
| 149 | 149 | GHC.Internal.Data.Function
|
| 150 | 150 | GHC.Internal.Data.Functor
|
| 151 | - GHC.Internal.Data.Functor.Const
|
|
| 152 | - GHC.Internal.Data.Functor.Identity
|
|
| 153 | 151 | GHC.Internal.Data.Functor.Utils
|
| 154 | 152 | GHC.Internal.Data.IORef
|
| 155 | 153 | GHC.Internal.Data.List
|
| ... | ... | @@ -19,7 +19,6 @@ |
| 19 | 19 | module GHC.Internal.Control.Monad.Zip ( MonadZip(..) ) where
|
| 20 | 20 | |
| 21 | 21 | import GHC.Internal.Control.Monad (liftM, liftM2, Monad(..))
|
| 22 | -import GHC.Internal.Data.Functor.Identity
|
|
| 23 | 22 | import qualified GHC.Internal.Data.Functor
|
| 24 | 23 | import GHC.Internal.Data.Monoid
|
| 25 | 24 | import GHC.Internal.Data.NonEmpty ( NonEmpty(..) )
|
| ... | ... | @@ -73,11 +72,6 @@ instance MonadZip NonEmpty where |
| 73 | 72 | mzipWith = NE.zipWith
|
| 74 | 73 | munzip = GHC.Internal.Data.Functor.unzip
|
| 75 | 74 | |
| 76 | --- | @since 4.8.0.0
|
|
| 77 | -instance MonadZip Identity where
|
|
| 78 | - mzipWith = liftM2
|
|
| 79 | - munzip (Identity (a, b)) = (Identity a, Identity b)
|
|
| 80 | - |
|
| 81 | 75 | -- | @since 4.15.0.0
|
| 82 | 76 | instance MonadZip Solo where
|
| 83 | 77 | mzipWith = liftM2
|
| 1 | -{-# LANGUAGE DeriveGeneric #-}
|
|
| 2 | -{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
|
| 3 | -{-# LANGUAGE NoImplicitPrelude #-}
|
|
| 4 | -{-# LANGUAGE PolyKinds #-}
|
|
| 5 | -{-# LANGUAGE ScopedTypeVariables #-}
|
|
| 6 | -{-# LANGUAGE Trustworthy #-}
|
|
| 7 | - |
|
| 8 | ------------------------------------------------------------------------------
|
|
| 9 | --- |
|
|
| 10 | --- Module : GHC.Internal.Data.Functor.Const
|
|
| 11 | --- Copyright : Conor McBride and Ross Paterson 2005
|
|
| 12 | --- License : BSD-style (see the LICENSE file in the distribution)
|
|
| 13 | ---
|
|
| 14 | --- Maintainer : libraries@haskell.org
|
|
| 15 | --- Stability : stable
|
|
| 16 | --- Portability : portable
|
|
| 17 | - |
|
| 18 | --- The 'Const' functor.
|
|
| 19 | ---
|
|
| 20 | --- @since base-4.9.0.0
|
|
| 21 | - |
|
| 22 | -module GHC.Internal.Data.Functor.Const (Const(..)) where
|
|
| 23 | - |
|
| 24 | -import GHC.Internal.Data.Bits (Bits, FiniteBits)
|
|
| 25 | -import GHC.Internal.Data.Foldable (Foldable(foldMap))
|
|
| 26 | -import GHC.Internal.Foreign.Storable (Storable)
|
|
| 27 | - |
|
| 28 | -import GHC.Internal.Ix (Ix)
|
|
| 29 | -import GHC.Internal.Base
|
|
| 30 | -import GHC.Internal.Enum (Bounded, Enum)
|
|
| 31 | -import GHC.Internal.Float (Floating, RealFloat)
|
|
| 32 | -import GHC.Internal.Generics (Generic, Generic1)
|
|
| 33 | -import GHC.Internal.Num (Num)
|
|
| 34 | -import GHC.Internal.Real (Fractional, Integral, Real, RealFrac)
|
|
| 35 | -import GHC.Internal.Read (Read(readsPrec), readParen, lex)
|
|
| 36 | -import GHC.Internal.Show (Show(showsPrec), showParen, showString)
|
|
| 37 | - |
|
| 38 | --- | The 'Const' functor.
|
|
| 39 | ---
|
|
| 40 | --- ==== __Examples__
|
|
| 41 | ---
|
|
| 42 | --- >>> fmap (++ "World") (Const "Hello")
|
|
| 43 | --- Const "Hello"
|
|
| 44 | ---
|
|
| 45 | --- Because we ignore the second type parameter to 'Const',
|
|
| 46 | --- the Applicative instance, which has
|
|
| 47 | --- @'(<*>)' :: Monoid m => Const m (a -> b) -> Const m a -> Const m b@
|
|
| 48 | --- essentially turns into @Monoid m => m -> m -> m@, which is '(<>)'
|
|
| 49 | ---
|
|
| 50 | --- >>> Const [1, 2, 3] <*> Const [4, 5, 6]
|
|
| 51 | --- Const [1,2,3,4,5,6]
|
|
| 52 | -newtype Const a b = Const { getConst :: a }
|
|
| 53 | - deriving ( Bits -- ^ @since base-4.9.0.0
|
|
| 54 | - , Bounded -- ^ @since base-4.9.0.0
|
|
| 55 | - , Enum -- ^ @since base-4.9.0.0
|
|
| 56 | - , Eq -- ^ @since base-4.9.0.0
|
|
| 57 | - , FiniteBits -- ^ @since base-4.9.0.0
|
|
| 58 | - , Floating -- ^ @since base-4.9.0.0
|
|
| 59 | - , Fractional -- ^ @since base-4.9.0.0
|
|
| 60 | - , Generic -- ^ @since base-4.9.0.0
|
|
| 61 | - , Generic1 -- ^ @since base-4.9.0.0
|
|
| 62 | - , Integral -- ^ @since base-4.9.0.0
|
|
| 63 | - , Ix -- ^ @since base-4.9.0.0
|
|
| 64 | - , Semigroup -- ^ @since base-4.9.0.0
|
|
| 65 | - , Monoid -- ^ @since base-4.9.0.0
|
|
| 66 | - , Num -- ^ @since base-4.9.0.0
|
|
| 67 | - , Ord -- ^ @since base-4.9.0.0
|
|
| 68 | - , Real -- ^ @since base-4.9.0.0
|
|
| 69 | - , RealFrac -- ^ @since base-4.9.0.0
|
|
| 70 | - , RealFloat -- ^ @since base-4.9.0.0
|
|
| 71 | - , Storable -- ^ @since base-4.9.0.0
|
|
| 72 | - )
|
|
| 73 | - |
|
| 74 | --- | This instance would be equivalent to the derived instances of the
|
|
| 75 | --- 'Const' newtype if the 'getConst' field were removed
|
|
| 76 | ---
|
|
| 77 | --- @since base-4.8.0.0
|
|
| 78 | -instance Read a => Read (Const a b) where
|
|
| 79 | - readsPrec d = readParen (d > 10)
|
|
| 80 | - $ \r -> [(Const x,t) | ("Const", s) <- lex r, (x, t) <- readsPrec 11 s]
|
|
| 81 | - |
|
| 82 | --- | This instance would be equivalent to the derived instances of the
|
|
| 83 | --- 'Const' newtype if the 'getConst' field were removed
|
|
| 84 | ---
|
|
| 85 | --- @since base-4.8.0.0
|
|
| 86 | -instance Show a => Show (Const a b) where
|
|
| 87 | - showsPrec d (Const x) = showParen (d > 10) $
|
|
| 88 | - showString "Const " . showsPrec 11 x
|
|
| 89 | - |
|
| 90 | --- | @since base-4.7.0.0
|
|
| 91 | -instance Foldable (Const m) where
|
|
| 92 | - foldMap _ _ = mempty
|
|
| 93 | - |
|
| 94 | --- | @since base-2.01
|
|
| 95 | -instance Functor (Const m) where
|
|
| 96 | - fmap _ (Const v) = Const v
|
|
| 97 | - |
|
| 98 | --- | @since base-2.0.1
|
|
| 99 | -instance Monoid m => Applicative (Const m) where
|
|
| 100 | - pure _ = Const mempty
|
|
| 101 | - liftA2 _ (Const x) (Const y) = Const (x `mappend` y)
|
|
| 102 | - (<*>) = coerce (mappend :: m -> m -> m)
|
|
| 103 | --- This is pretty much the same as
|
|
| 104 | --- Const f <*> Const v = Const (f `mappend` v)
|
|
| 105 | --- but guarantees that mappend for Const a b will have the same arity
|
|
| 106 | --- as the one for a; it won't create a closure to raise the arity
|
|
| 107 | --- to 2. |
| 1 | -{-# LANGUAGE DeriveGeneric #-}
|
|
| 2 | -{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
|
| 3 | -{-# LANGUAGE NoImplicitPrelude #-}
|
|
| 4 | -{-# LANGUAGE Trustworthy #-}
|
|
| 5 | - |
|
| 6 | ------------------------------------------------------------------------------
|
|
| 7 | --- |
|
|
| 8 | --- Module : GHC.Internal.Data.Functor.Identity
|
|
| 9 | --- Copyright : (c) Andy Gill 2001,
|
|
| 10 | --- (c) Oregon Graduate Institute of Science and Technology 2001
|
|
| 11 | --- License : BSD-style (see the file LICENSE)
|
|
| 12 | ---
|
|
| 13 | --- Maintainer : ross@soi.city.ac.uk
|
|
| 14 | --- Stability : stable
|
|
| 15 | --- Portability : portable
|
|
| 16 | ---
|
|
| 17 | --- The identity functor and monad.
|
|
| 18 | ---
|
|
| 19 | --- This trivial type constructor serves two purposes:
|
|
| 20 | ---
|
|
| 21 | --- * It can be used with functions parameterized by functor or monad classes.
|
|
| 22 | ---
|
|
| 23 | --- * It can be used as a base monad to which a series of monad
|
|
| 24 | --- transformers may be applied to construct a composite monad.
|
|
| 25 | --- Most monad transformer modules include the special case of
|
|
| 26 | --- applying the transformer to 'Identity'. For example, @State s@
|
|
| 27 | --- is an abbreviation for @StateT s 'Identity'@.
|
|
| 28 | ---
|
|
| 29 | --- @since base-4.8.0.0
|
|
| 30 | ------------------------------------------------------------------------------
|
|
| 31 | - |
|
| 32 | -module GHC.Internal.Data.Functor.Identity (
|
|
| 33 | - Identity(..),
|
|
| 34 | - ) where
|
|
| 35 | - |
|
| 36 | -import GHC.Internal.Control.Monad.Fix
|
|
| 37 | -import GHC.Internal.Data.Bits (Bits, FiniteBits)
|
|
| 38 | -import GHC.Internal.Data.Coerce
|
|
| 39 | -import GHC.Internal.Data.Foldable
|
|
| 40 | -import GHC.Internal.Data.Functor.Utils ((#.))
|
|
| 41 | -import GHC.Internal.Foreign.Storable (Storable)
|
|
| 42 | -import GHC.Internal.Ix (Ix)
|
|
| 43 | -import GHC.Internal.Base ( Applicative(..), Eq(..), Functor(..), Monad(..)
|
|
| 44 | - , Semigroup, Monoid, Ord(..), ($), (.) )
|
|
| 45 | -import GHC.Internal.Enum (Bounded, Enum)
|
|
| 46 | -import GHC.Internal.Float (Floating, RealFloat)
|
|
| 47 | -import GHC.Internal.Generics (Generic, Generic1)
|
|
| 48 | -import GHC.Internal.Num (Num)
|
|
| 49 | -import GHC.Internal.Read (Read(..), lex, readParen)
|
|
| 50 | -import GHC.Internal.Real (Fractional, Integral, Real, RealFrac)
|
|
| 51 | -import GHC.Internal.Show (Show(..), showParen, showString)
|
|
| 52 | -import GHC.Internal.Types (Bool(..))
|
|
| 53 | - |
|
| 54 | --- | Identity functor and monad. (a non-strict monad)
|
|
| 55 | ---
|
|
| 56 | --- ==== __Examples__
|
|
| 57 | ---
|
|
| 58 | --- >>> fmap (+1) (Identity 0)
|
|
| 59 | --- Identity 1
|
|
| 60 | ---
|
|
| 61 | --- >>> Identity [1, 2, 3] <> Identity [4, 5, 6]
|
|
| 62 | --- Identity [1,2,3,4,5,6]
|
|
| 63 | ---
|
|
| 64 | --- @
|
|
| 65 | --- >>> do
|
|
| 66 | --- x <- Identity 10
|
|
| 67 | --- y <- Identity (x + 5)
|
|
| 68 | --- pure (x + y)
|
|
| 69 | --- Identity 25
|
|
| 70 | --- @
|
|
| 71 | ---
|
|
| 72 | --- @since base-4.8.0.0
|
|
| 73 | -newtype Identity a = Identity { runIdentity :: a }
|
|
| 74 | - deriving ( Bits -- ^ @since base-4.9.0.0
|
|
| 75 | - , Bounded -- ^ @since base-4.9.0.0
|
|
| 76 | - , Enum -- ^ @since base-4.9.0.0
|
|
| 77 | - , Eq -- ^ @since base-4.8.0.0
|
|
| 78 | - , FiniteBits -- ^ @since base-4.9.0.0
|
|
| 79 | - , Floating -- ^ @since base-4.9.0.0
|
|
| 80 | - , Fractional -- ^ @since base-4.9.0.0
|
|
| 81 | - , Generic -- ^ @since base-4.8.0.0
|
|
| 82 | - , Generic1 -- ^ @since base-4.8.0.0
|
|
| 83 | - , Integral -- ^ @since base-4.9.0.0
|
|
| 84 | - , Ix -- ^ @since base-4.9.0.0
|
|
| 85 | - , Semigroup -- ^ @since base-4.9.0.0
|
|
| 86 | - , Monoid -- ^ @since base-4.9.0.0
|
|
| 87 | - , Num -- ^ @since base-4.9.0.0
|
|
| 88 | - , Ord -- ^ @since base-4.8.0.0
|
|
| 89 | - , Real -- ^ @since base-4.9.0.0
|
|
| 90 | - , RealFrac -- ^ @since base-4.9.0.0
|
|
| 91 | - , RealFloat -- ^ @since base-4.9.0.0
|
|
| 92 | - , Storable -- ^ @since base-4.9.0.0
|
|
| 93 | - )
|
|
| 94 | - |
|
| 95 | --- | This instance would be equivalent to the derived instances of the
|
|
| 96 | --- 'Identity' newtype if the 'runIdentity' field were removed
|
|
| 97 | ---
|
|
| 98 | --- @since base-4.8.0.0
|
|
| 99 | -instance (Read a) => Read (Identity a) where
|
|
| 100 | - readsPrec d = readParen (d > 10) $ \ r ->
|
|
| 101 | - [(Identity x,t) | ("Identity",s) <- lex r, (x,t) <- readsPrec 11 s]
|
|
| 102 | - |
|
| 103 | --- | This instance would be equivalent to the derived instances of the
|
|
| 104 | --- 'Identity' newtype if the 'runIdentity' field were removed
|
|
| 105 | ---
|
|
| 106 | --- @since base-4.8.0.0
|
|
| 107 | -instance (Show a) => Show (Identity a) where
|
|
| 108 | - showsPrec d (Identity x) = showParen (d > 10) $
|
|
| 109 | - showString "Identity " . showsPrec 11 x
|
|
| 110 | - |
|
| 111 | --- ---------------------------------------------------------------------------
|
|
| 112 | --- Identity instances for Functor and Monad
|
|
| 113 | - |
|
| 114 | --- | @since base-4.8.0.0
|
|
| 115 | -instance Foldable Identity where
|
|
| 116 | - foldMap = coerce
|
|
| 117 | - |
|
| 118 | - elem = (. runIdentity) #. (==)
|
|
| 119 | - foldl = coerce
|
|
| 120 | - foldl' = coerce
|
|
| 121 | - foldl1 _ = runIdentity
|
|
| 122 | - foldr f z (Identity x) = f x z
|
|
| 123 | - foldr' = foldr
|
|
| 124 | - foldr1 _ = runIdentity
|
|
| 125 | - length _ = 1
|
|
| 126 | - maximum = runIdentity
|
|
| 127 | - minimum = runIdentity
|
|
| 128 | - null _ = False
|
|
| 129 | - product = runIdentity
|
|
| 130 | - sum = runIdentity
|
|
| 131 | - toList (Identity x) = [x]
|
|
| 132 | - |
|
| 133 | --- | @since base-4.8.0.0
|
|
| 134 | -instance Functor Identity where
|
|
| 135 | - fmap = coerce
|
|
| 136 | - |
|
| 137 | --- | @since base-4.8.0.0
|
|
| 138 | -instance Applicative Identity where
|
|
| 139 | - pure = Identity
|
|
| 140 | - (<*>) = coerce
|
|
| 141 | - liftA2 = coerce
|
|
| 142 | - |
|
| 143 | --- | @since base-4.8.0.0
|
|
| 144 | -instance Monad Identity where
|
|
| 145 | - m >>= k = k (runIdentity m)
|
|
| 146 | - |
|
| 147 | --- | @since base-4.8.0.0
|
|
| 148 | -instance MonadFix Identity where
|
|
| 149 | - mfix f = Identity (fix (runIdentity . f)) |
| ... | ... | @@ -33,8 +33,6 @@ module GHC.Internal.Data.String ( |
| 33 | 33 | ) where
|
| 34 | 34 | |
| 35 | 35 | import GHC.Internal.Base
|
| 36 | -import GHC.Internal.Data.Functor.Const (Const (Const))
|
|
| 37 | -import GHC.Internal.Data.Functor.Identity (Identity (Identity))
|
|
| 38 | 36 | import GHC.Internal.Data.List (lines, words, unlines, unwords)
|
| 39 | 37 | |
| 40 | 38 | -- | `IsString` is used in combination with the @-XOverloadedStrings@
|
| ... | ... | @@ -105,9 +103,3 @@ ensure the good behavior of the above example remains in the future. |
| 105 | 103 | instance (a ~ Char) => IsString [a] where
|
| 106 | 104 | -- See Note [IsString String]
|
| 107 | 105 | fromString xs = xs |
| 108 | - |
|
| 109 | --- | @since base-4.9.0.0
|
|
| 110 | -deriving instance IsString a => IsString (Const a (b :: k))
|
|
| 111 | - |
|
| 112 | --- | @since base-4.9.0.0
|
|
| 113 | -deriving instance IsString a => IsString (Identity a) |
| ... | ... | @@ -32,17 +32,12 @@ module GHC.Internal.Data.Traversable ( |
| 32 | 32 | mapAccumL,
|
| 33 | 33 | mapAccumR,
|
| 34 | 34 | mapAccumM,
|
| 35 | - -- * General definitions for superclass methods
|
|
| 36 | - fmapDefault,
|
|
| 37 | - foldMapDefault,
|
|
| 38 | 35 | ) where
|
| 39 | 36 | |
| 40 | 37 | import GHC.Internal.Data.Coerce
|
| 41 | 38 | import GHC.Internal.Data.Either ( Either(..) )
|
| 42 | 39 | import GHC.Internal.Data.Foldable
|
| 43 | 40 | import GHC.Internal.Data.Functor
|
| 44 | -import GHC.Internal.Data.Functor.Const ( Const(..) )
|
|
| 45 | -import GHC.Internal.Data.Functor.Identity ( Identity(..) )
|
|
| 46 | 41 | import GHC.Internal.Data.Functor.Utils ( StateL(..), StateR(..), StateT(..), (#.) )
|
| 47 | 42 | import GHC.Internal.Data.Monoid ( Dual(..), Sum(..), Product(..),
|
| 48 | 43 | First(..), Last(..), Alt(..), Ap(..) )
|
| ... | ... | @@ -274,10 +269,6 @@ instance Traversable Proxy where |
| 274 | 269 | sequence _ = pure Proxy
|
| 275 | 270 | {-# INLINE sequence #-}
|
| 276 | 271 | |
| 277 | --- | @since base-4.7.0.0
|
|
| 278 | -instance Traversable (Const m) where
|
|
| 279 | - traverse _ (Const m) = pure $ Const m
|
|
| 280 | - |
|
| 281 | 272 | -- | @since base-4.8.0.0
|
| 282 | 273 | instance Traversable Dual where
|
| 283 | 274 | traverse f (Dual x) = Dual <$> f x
|
| ... | ... | @@ -306,10 +297,6 @@ instance (Traversable f) => Traversable (Alt f) where |
| 306 | 297 | instance (Traversable f) => Traversable (Ap f) where
|
| 307 | 298 | traverse f (Ap x) = Ap <$> traverse f x
|
| 308 | 299 | |
| 309 | --- | @since base-4.9.0.0
|
|
| 310 | -deriving instance Traversable Identity
|
|
| 311 | - |
|
| 312 | - |
|
| 313 | 300 | -- Instances for GHC.Generics
|
| 314 | 301 | -- | @since base-4.9.0.0
|
| 315 | 302 | instance Traversable U1 where
|
| ... | ... | @@ -460,29 +447,3 @@ forAccumM |
| 460 | 447 | => s -> t a -> (s -> a -> m (s, b)) -> m (s, t b)
|
| 461 | 448 | {-# INLINE forAccumM #-}
|
| 462 | 449 | forAccumM s t f = mapAccumM f s t |
| 463 | - |
|
| 464 | --- | This function may be used as a value for `fmap` in a `Functor`
|
|
| 465 | --- instance, provided that 'traverse' is defined. (Using
|
|
| 466 | --- `fmapDefault` with a `Traversable` instance defined only by
|
|
| 467 | --- 'sequenceA' will result in infinite recursion.)
|
|
| 468 | ---
|
|
| 469 | --- @
|
|
| 470 | --- 'fmapDefault' f ≡ 'runIdentity' . 'traverse' ('Identity' . f)
|
|
| 471 | --- @
|
|
| 472 | -fmapDefault :: forall t a b . Traversable t
|
|
| 473 | - => (a -> b) -> t a -> t b
|
|
| 474 | -{-# INLINE fmapDefault #-}
|
|
| 475 | --- See Note [Function coercion] in Data.Functor.Utils.
|
|
| 476 | -fmapDefault = coerce (traverse @t @Identity @a @b)
|
|
| 477 | - |
|
| 478 | --- | This function may be used as a value for `Data.Foldable.foldMap`
|
|
| 479 | --- in a `Foldable` instance.
|
|
| 480 | ---
|
|
| 481 | --- @
|
|
| 482 | --- 'foldMapDefault' f ≡ 'getConst' . 'traverse' ('Const' . f)
|
|
| 483 | --- @
|
|
| 484 | -foldMapDefault :: forall t m a . (Traversable t, Monoid m)
|
|
| 485 | - => (a -> m) -> t a -> m
|
|
| 486 | -{-# INLINE foldMapDefault #-}
|
|
| 487 | --- See Note [Function coercion] in Data.Functor.Utils.
|
|
| 488 | -foldMapDefault = coerce (traverse @t @(Const m) @a @()) |