Commit 56e0ac98 authored by Herbert Valerio Riedel's avatar Herbert Valerio Riedel 🕺 Committed by Austin Seipp
Browse files

Add public rnf/hash operations to TypeRep/TyCon

Summary:
`TyCon` and `TypeRep` are supposed to be abstract, by providing these
additional few public operations the need to import
`Data.Typeable.Internal` is reduced, and future changes to the internal
structure of `TypeRep`/`TyCon` shouldn't require changes in packages such as
`deepseq` or `hashable` anymore (hopefully).

Reviewers: ekmett, simonpj, tibbe, austin

Reviewed By: ekmett, simonpj, austin

Subscribers: thomie

Differential Revision: https://phabricator.haskell.org/D699
parent 8a5d3205
...@@ -61,13 +61,17 @@ module Data.Typeable ...@@ -61,13 +61,17 @@ module Data.Typeable
-- * Type representations -- * Type representations
TypeRep, -- abstract, instance of: Eq, Show, Typeable TypeRep, -- abstract, instance of: Eq, Show, Typeable
typeRepHash,
rnfTypeRep,
showsTypeRep, showsTypeRep,
TyCon, -- abstract, instance of: Eq, Show, Typeable TyCon, -- abstract, instance of: Eq, Show, Typeable
tyConHash,
tyConString, tyConString,
tyConPackage, tyConPackage,
tyConModule, tyConModule,
tyConName, tyConName,
rnfTyCon,
-- * Construction of type representations -- * Construction of type representations
-- mkTyCon, -- :: String -> TyCon -- mkTyCon, -- :: String -> TyCon
......
...@@ -42,8 +42,11 @@ module Data.Typeable.Internal ( ...@@ -42,8 +42,11 @@ module Data.Typeable.Internal (
splitTyConApp, splitTyConApp,
funResultTy, funResultTy,
typeRepArgs, typeRepArgs,
typeRepHash,
rnfTypeRep,
showsTypeRep, showsTypeRep,
tyConString, tyConString,
rnfTyCon,
listTc, funTc listTc, funTc
) where ) where
...@@ -93,7 +96,7 @@ instance Ord TypeRep where ...@@ -93,7 +96,7 @@ instance Ord TypeRep where
-- | An abstract representation of a type constructor. 'TyCon' objects can -- | An abstract representation of a type constructor. 'TyCon' objects can
-- be built using 'mkTyCon'. -- be built using 'mkTyCon'.
data TyCon = TyCon { data TyCon = TyCon {
tyConHash :: {-# UNPACK #-} !Fingerprint, tyConHash :: {-# UNPACK #-} !Fingerprint, -- ^ @since 4.8.0.0
tyConPackage :: String, -- ^ @since 4.5.0.0 tyConPackage :: String, -- ^ @since 4.5.0.0
tyConModule :: String, -- ^ @since 4.5.0.0 tyConModule :: String, -- ^ @since 4.5.0.0
tyConName :: String -- ^ @since 4.5.0.0 tyConName :: String -- ^ @since 4.5.0.0
...@@ -191,6 +194,12 @@ typeRepArgs (TypeRep _ _ args) = args ...@@ -191,6 +194,12 @@ typeRepArgs (TypeRep _ _ args) = args
tyConString :: TyCon -> String tyConString :: TyCon -> String
tyConString = tyConName tyConString = tyConName
-- | Observe the 'Fingerprint' of a type representation
--
-- @since 4.8.0.0
typeRepHash :: TypeRep -> Fingerprint
typeRepHash (TypeRep fpr _ _) = fpr
------------------------------------------------------------- -------------------------------------------------------------
-- --
-- The Typeable class and friends -- The Typeable class and friends
...@@ -301,6 +310,24 @@ isTupleTyCon :: TyCon -> Bool ...@@ -301,6 +310,24 @@ isTupleTyCon :: TyCon -> Bool
isTupleTyCon (TyCon _ _ _ ('(':',':_)) = True isTupleTyCon (TyCon _ _ _ ('(':',':_)) = True
isTupleTyCon _ = False isTupleTyCon _ = False
-- | Helper to fully evaluate 'TypeRep' for use as @NFData(rnf)@ implementation
--
-- @since 4.8.0.0
rnfTypeRep :: TypeRep -> ()
rnfTypeRep (TypeRep _ tyc tyrs) = rnfTyCon tyc `seq` go tyrs
where
go [] = ()
go (x:xs) = rnfTypeRep x `seq` go xs
-- | Helper to fully evaluate 'TyCon' for use as @NFData(rnf)@ implementation
--
-- @since 4.8.0.0
rnfTyCon :: TyCon -> ()
rnfTyCon (TyCon _ tcp tcm tcn) = go tcp `seq` go tcm `seq` go tcn
where
go [] = ()
go (x:xs) = x `seq` go xs
-- Some (Show.TypeRep) helpers: -- Some (Show.TypeRep) helpers:
showArgs :: Show a => ShowS -> [a] -> ShowS showArgs :: Show a => ShowS -> [a] -> ShowS
......
...@@ -138,6 +138,9 @@ ...@@ -138,6 +138,9 @@
* Restore invariant in `Data (Ratio a)` instance (#10011) * Restore invariant in `Data (Ratio a)` instance (#10011)
* Add/expose `rnfTypeRep`, `rnfTyCon`, `TypeRepHash`, and
`TyConHash` helpers to `Data.Typeable`.
## 4.7.0.2 *Dec 2014* ## 4.7.0.2 *Dec 2014*
* Bundled with GHC 7.8.4 * Bundled with GHC 7.8.4
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment