Wolfgang Jeltsch pushed to branch wip/jeltsch/text-read-uncovering at Glasgow Haskell Compiler / GHC Commits: 40d2e59f by Wolfgang Jeltsch at 2026-03-18T23:41:54+02:00 Move the implementation of version parsing into `base` - - - - - b2122fdb by Wolfgang Jeltsch at 2026-03-18T23:42:09+02:00 Move the implementation of `readConstr` into `base` - - - - - 9 changed files: - libraries/base/src/Data/Data.hs - libraries/base/src/Data/Version.hs - libraries/ghc-internal/src/GHC/Internal/Data/Data.hs - libraries/ghc-internal/src/GHC/Internal/Data/Version.hs - testsuite/tests/interface-stability/base-exports.stdout - testsuite/tests/interface-stability/base-exports.stdout-javascript-unknown-ghcjs - testsuite/tests/interface-stability/base-exports.stdout-mingw32 - testsuite/tests/interface-stability/base-exports.stdout-ws-32 - testsuite/tests/plugins/plugins10.stdout Changes: ===================================== libraries/base/src/Data/Data.hs ===================================== @@ -99,3 +99,38 @@ module Data.Data ( import GHC.Internal.Data.Data import Data.Typeable + +import GHC.Real (toRational) +import GHC.Float (Double) +import Data.Eq ((==)) +import Data.Function ((.)) +import Data.Maybe (Maybe (Nothing, Just)) +import Data.List (filter) +import Data.String (String) +import Text.Read (Read, reads) + +-- | Lookup a constructor via a string +readConstr :: DataType -> String -> Maybe Constr +readConstr dt str = + case dataTypeRep dt of + AlgRep cons -> idx cons + IntRep -> mkReadCon (\i -> (mkPrimCon dt str (IntConstr i))) + FloatRep -> mkReadCon ffloat + CharRep -> mkReadCon (\c -> (mkPrimCon dt str (CharConstr c))) + NoRep -> Nothing + where + + -- Read a value and build a constructor + mkReadCon :: Read t => (t -> Constr) -> Maybe Constr + mkReadCon f = case (reads str) of + [(t,"")] -> Just (f t) + _ -> Nothing + + -- Traverse list of algebraic datatype constructors + idx :: [Constr] -> Maybe Constr + idx cons = case filter ((==) str . showConstr) cons of + [] -> Nothing + hd : _ -> Just hd + + ffloat :: Double -> Constr + ffloat = mkPrimCon dt str . FloatConstr . toRational ===================================== libraries/base/src/Data/Version.hs ===================================== @@ -1,5 +1,7 @@ {-# LANGUAGE Safe #-} +{-# LANGUAGE StandaloneDeriving #-} + -- | -- Module : Data.Version -- Copyright : (c) The University of Glasgow 2004 @@ -33,3 +35,25 @@ module Data.Version ( ) where import GHC.Internal.Data.Version + +import Control.Applicative (pure, (*>)) +import Data.Functor (fmap) +import Data.Char (isDigit, isAlphaNum) +import Text.ParserCombinators.ReadP (ReadP, char, munch1, sepBy1, many) +import Text.Read (Read, read) + +{-NOTE: + The following instance is technically an orphan, but practically it is not, + since ordinary users should not use @ghc-internal@ directly and thus get + 'Version' only through this module. +-} + +-- | @since base-2.01 +deriving instance Read Version + +-- | A parser for versions in the format produced by 'showVersion'. +-- +parseVersion :: ReadP Version +parseVersion = do branch <- sepBy1 (fmap read (munch1 isDigit)) (char '.') + tags <- many (char '-' *> munch1 isAlphaNum) + pure Version{versionBranch=branch, versionTags=tags} ===================================== libraries/ghc-internal/src/GHC/Internal/Data/Data.hs ===================================== @@ -61,6 +61,7 @@ module GHC.Internal.Data.Data ( mkIntType, mkFloatType, mkCharType, + mkPrimCon, mkNoRepType, -- ** Observers dataTypeName, @@ -94,7 +95,6 @@ module GHC.Internal.Data.Data ( constrIndex, -- ** From strings to constructors and vice versa: all data types showConstr, - readConstr, -- * Convenience functions: take type constructors apart tyconUQname, @@ -126,10 +126,8 @@ import GHC.Internal.Base ( import GHC.Internal.Err (errorWithoutStackTrace) import GHC.Internal.List import GHC.Internal.Num -import GHC.Internal.Read import GHC.Internal.Show import GHC.Internal.Tuple (Solo (..)) -import GHC.Internal.Text.Read( reads ) import GHC.Internal.Types ( Bool(..), Char, Coercible, Float, Double, Type, type (~), type (~~), ) @@ -688,32 +686,6 @@ showConstr :: Constr -> String showConstr = constring --- | Lookup a constructor via a string -readConstr :: DataType -> String -> Maybe Constr -readConstr dt str = - case dataTypeRep dt of - AlgRep cons -> idx cons - IntRep -> mkReadCon (\i -> (mkPrimCon dt str (IntConstr i))) - FloatRep -> mkReadCon ffloat - CharRep -> mkReadCon (\c -> (mkPrimCon dt str (CharConstr c))) - NoRep -> Nothing - where - - -- Read a value and build a constructor - mkReadCon :: Read t => (t -> Constr) -> Maybe Constr - mkReadCon f = case (reads str) of - [(t,"")] -> Just (f t) - _ -> Nothing - - -- Traverse list of algebraic datatype constructors - idx :: [Constr] -> Maybe Constr - idx cons = case filter ((==) str . showConstr) cons of - [] -> Nothing - hd : _ -> Just hd - - ffloat :: Double -> Constr - ffloat = mkPrimCon dt str . FloatConstr . toRational - ------------------------------------------------------------------------------ -- -- Convenience functions: algebraic data types ===================================== libraries/ghc-internal/src/GHC/Internal/Data/Version.hs ===================================== @@ -10,7 +10,7 @@ -- -- Maintainer : libraries@haskell.org -- Stability : stable --- Portability : non-portable (local universal quantification in ReadP) +-- Portability : non-portable -- -- A general library for representation and manipulation of versions. -- @@ -31,23 +31,18 @@ module GHC.Internal.Data.Version ( -- * The @Version@ type Version(..), -- * A concrete representation of @Version@ - showVersion, parseVersion, + showVersion, -- * Constructor function makeVersion ) where -import GHC.Internal.Classes ( Eq(..), (&&) ) -import GHC.Internal.Data.Functor ( Functor(..) ) +import GHC.Internal.Classes ( Eq ((==)), (&&) ) import GHC.Internal.Int ( Int ) import GHC.Internal.Data.List ( map, sort, concat, concatMap, intersperse, (++) ) import GHC.Internal.Data.Ord -import GHC.Internal.Base ( Applicative(..), String ) +import GHC.Internal.Base ( String ) import GHC.Internal.Generics -import GHC.Internal.Unicode ( isDigit, isAlphaNum ) -import GHC.Internal.Read import GHC.Internal.Show -import GHC.Internal.Text.ParserCombinators.ReadP -import GHC.Internal.Text.Read ( read ) {- | A 'Version' represents the version of a software entity. @@ -69,8 +64,8 @@ operations are the right thing for every 'Version'. Similarly, concrete representations of versions may differ. One possible concrete representation is provided (see 'showVersion' and -'parseVersion'), but depending on the application a different concrete -representation may be more appropriate. +'Data.Version.parseVersion'), but depending on the application a +different concrete representation may be more appropriate. -} data Version = Version { versionBranch :: [Int], @@ -92,8 +87,7 @@ data Version = -- The interpretation of the list of tags is entirely dependent -- on the entity that this version applies to. } - deriving ( Read -- ^ @since base-2.01 - , Show -- ^ @since base-2.01 + deriving ( Show -- ^ @since base-2.01 , Generic -- ^ @since base-4.9.0.0 ) {-# DEPRECATED versionTags "See GHC ticket #2496" #-} @@ -121,13 +115,6 @@ showVersion (Version branch tags) = concat (intersperse "." (map show branch)) ++ concatMap ('-':) tags --- | A parser for versions in the format produced by 'showVersion'. --- -parseVersion :: ReadP Version -parseVersion = do branch <- sepBy1 (fmap read (munch1 isDigit)) (char '.') - tags <- many (char '-' *> munch1 isAlphaNum) - pure Version{versionBranch=branch, versionTags=tags} - -- | Construct tag-less 'Version' -- -- @since base-4.8.0.0 ===================================== testsuite/tests/interface-stability/base-exports.stdout ===================================== @@ -12496,7 +12496,7 @@ instance forall a. GHC.Internal.Read.Read a => GHC.Internal.Read.Read (Data.Semi instance forall a. GHC.Internal.Read.Read a => GHC.Internal.Read.Read (Data.Semigroup.Min a) -- Defined in ‘Data.Semigroup’ instance forall m. GHC.Internal.Read.Read m => GHC.Internal.Read.Read (Data.Semigroup.WrappedMonoid m) -- Defined in ‘Data.Semigroup’ instance forall k (a :: k) (b :: k). Coercible a b => GHC.Internal.Read.Read (GHC.Internal.Data.Type.Coercion.Coercion a b) -- Defined in ‘GHC.Internal.Data.Type.Coercion’ -instance GHC.Internal.Read.Read GHC.Internal.Data.Version.Version -- Defined in ‘GHC.Internal.Data.Version’ +instance [safe] GHC.Internal.Read.Read GHC.Internal.Data.Version.Version -- Defined in ‘Data.Version’ instance GHC.Internal.Read.Read GHC.Internal.Foreign.Ptr.IntPtr -- Defined in ‘GHC.Internal.Foreign.Ptr’ instance GHC.Internal.Read.Read GHC.Internal.Foreign.Ptr.WordPtr -- Defined in ‘GHC.Internal.Foreign.Ptr’ instance GHC.Internal.Read.Read GHC.Internal.Foreign.C.Types.CBool -- Defined in ‘GHC.Internal.Foreign.C.Types’ ===================================== testsuite/tests/interface-stability/base-exports.stdout-javascript-unknown-ghcjs ===================================== @@ -12525,7 +12525,7 @@ instance forall a. GHC.Internal.Read.Read a => GHC.Internal.Read.Read (Data.Semi instance forall a. GHC.Internal.Read.Read a => GHC.Internal.Read.Read (Data.Semigroup.Min a) -- Defined in ‘Data.Semigroup’ instance forall m. GHC.Internal.Read.Read m => GHC.Internal.Read.Read (Data.Semigroup.WrappedMonoid m) -- Defined in ‘Data.Semigroup’ instance forall k (a :: k) (b :: k). Coercible a b => GHC.Internal.Read.Read (GHC.Internal.Data.Type.Coercion.Coercion a b) -- Defined in ‘GHC.Internal.Data.Type.Coercion’ -instance GHC.Internal.Read.Read GHC.Internal.Data.Version.Version -- Defined in ‘GHC.Internal.Data.Version’ +instance [safe] GHC.Internal.Read.Read GHC.Internal.Data.Version.Version -- Defined in ‘Data.Version’ instance GHC.Internal.Read.Read GHC.Internal.Foreign.Ptr.IntPtr -- Defined in ‘GHC.Internal.Foreign.Ptr’ instance GHC.Internal.Read.Read GHC.Internal.Foreign.Ptr.WordPtr -- Defined in ‘GHC.Internal.Foreign.Ptr’ instance GHC.Internal.Read.Read GHC.Internal.Foreign.C.Types.CBool -- Defined in ‘GHC.Internal.Foreign.C.Types’ ===================================== testsuite/tests/interface-stability/base-exports.stdout-mingw32 ===================================== @@ -12767,7 +12767,7 @@ instance forall a. GHC.Internal.Read.Read a => GHC.Internal.Read.Read (Data.Semi instance forall a. GHC.Internal.Read.Read a => GHC.Internal.Read.Read (Data.Semigroup.Min a) -- Defined in ‘Data.Semigroup’ instance forall m. GHC.Internal.Read.Read m => GHC.Internal.Read.Read (Data.Semigroup.WrappedMonoid m) -- Defined in ‘Data.Semigroup’ instance forall k (a :: k) (b :: k). Coercible a b => GHC.Internal.Read.Read (GHC.Internal.Data.Type.Coercion.Coercion a b) -- Defined in ‘GHC.Internal.Data.Type.Coercion’ -instance GHC.Internal.Read.Read GHC.Internal.Data.Version.Version -- Defined in ‘GHC.Internal.Data.Version’ +instance [safe] GHC.Internal.Read.Read GHC.Internal.Data.Version.Version -- Defined in ‘Data.Version’ instance GHC.Internal.Read.Read GHC.Internal.Foreign.Ptr.IntPtr -- Defined in ‘GHC.Internal.Foreign.Ptr’ instance GHC.Internal.Read.Read GHC.Internal.Foreign.Ptr.WordPtr -- Defined in ‘GHC.Internal.Foreign.Ptr’ instance GHC.Internal.Read.Read GHC.Internal.Foreign.C.Types.CBool -- Defined in ‘GHC.Internal.Foreign.C.Types’ ===================================== testsuite/tests/interface-stability/base-exports.stdout-ws-32 ===================================== @@ -12496,7 +12496,7 @@ instance forall a. GHC.Internal.Read.Read a => GHC.Internal.Read.Read (Data.Semi instance forall a. GHC.Internal.Read.Read a => GHC.Internal.Read.Read (Data.Semigroup.Min a) -- Defined in ‘Data.Semigroup’ instance forall m. GHC.Internal.Read.Read m => GHC.Internal.Read.Read (Data.Semigroup.WrappedMonoid m) -- Defined in ‘Data.Semigroup’ instance forall k (a :: k) (b :: k). Coercible a b => GHC.Internal.Read.Read (GHC.Internal.Data.Type.Coercion.Coercion a b) -- Defined in ‘GHC.Internal.Data.Type.Coercion’ -instance GHC.Internal.Read.Read GHC.Internal.Data.Version.Version -- Defined in ‘GHC.Internal.Data.Version’ +instance [safe] GHC.Internal.Read.Read GHC.Internal.Data.Version.Version -- Defined in ‘Data.Version’ instance GHC.Internal.Read.Read GHC.Internal.Foreign.Ptr.IntPtr -- Defined in ‘GHC.Internal.Foreign.Ptr’ instance GHC.Internal.Read.Read GHC.Internal.Foreign.Ptr.WordPtr -- Defined in ‘GHC.Internal.Foreign.Ptr’ instance GHC.Internal.Read.Read GHC.Internal.Foreign.C.Types.CBool -- Defined in ‘GHC.Internal.Foreign.C.Types’ ===================================== testsuite/tests/plugins/plugins10.stdout ===================================== @@ -2,6 +2,7 @@ parsePlugin() interfacePlugin: Prelude interfacePlugin: Language.Haskell.TH interfacePlugin: Language.Haskell.TH.Quote +interfacePlugin: Data.Version interfacePlugin: System.IO interfacePlugin: GHC.Internal.Base interfacePlugin: GHC.Internal.Data.NonEmpty View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/a78ea8163e36526842596c5b764a2f0... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/a78ea8163e36526842596c5b764a2f0... You're receiving this email because of your account on gitlab.haskell.org.
participants (1)
-
Wolfgang Jeltsch (@jeltsch)