[commit: ghc] wip/generalized-arrow: Fix up representation pretty-printer (a604ce9)

git at git.haskell.org git at git.haskell.org
Mon Mar 21 17:11:20 UTC 2016


Repository : ssh://git@git.haskell.org/ghc

On branch  : wip/generalized-arrow
Link       : http://ghc.haskell.org/trac/ghc/changeset/a604ce9471cec93050929fa0a34df10cb936fe29/ghc

>---------------------------------------------------------------

commit a604ce9471cec93050929fa0a34df10cb936fe29
Author: Ben Gamari <ben at smart-cactus.org>
Date:   Wed Mar 16 13:36:30 2016 +0100

    Fix up representation pretty-printer


>---------------------------------------------------------------

a604ce9471cec93050929fa0a34df10cb936fe29
 libraries/base/Data/Typeable/Internal.hs | 44 +++++++++++++++++++++++---------
 1 file changed, 32 insertions(+), 12 deletions(-)

diff --git a/libraries/base/Data/Typeable/Internal.hs b/libraries/base/Data/Typeable/Internal.hs
index 2252634..3b84aba 100644
--- a/libraries/base/Data/Typeable/Internal.hs
+++ b/libraries/base/Data/Typeable/Internal.hs
@@ -317,28 +317,48 @@ typeRepXFingerprint (TypeRepX t) = typeRepFingerprint t
 
 ----------------- Showing TypeReps --------------------
 
-instance Show (TypeRep a) where
+instance Show (TypeRep (a :: k)) where
+  showsPrec _ rep
+    | isListTyCon tc, [ty] <- tys =
+      showChar '[' . shows ty . showChar ']'
+    | isTupleTyCon tc =
+      showChar '(' . showArgs (showChar ',') tys . showChar ')'
+    where (tc, tys) = splitApps rep
   showsPrec p (TrTyCon _ tycon _) = showsPrec p tycon
-  showsPrec p (TrApp _ f x)       = showsPrec p f . showString " " . showsPrec p x
-  -- TODO: Reconsider precedence
+  showsPrec p (TrApp _ f x)
+    | Just HRefl <- f `eqTypeRep` (typeRep :: TypeRep (->)) =
+      shows x . showString " -> "
+    | otherwise =
+      showsPrec p f . space . showParen need_parens (showsPrec 10 x)
+    where
+      space = showChar ' '
+      need_parens = case x of
+                        TrApp {}   -> True
+                        TrTyCon {} -> False
 
 instance Show TypeRepX where
   showsPrec p (TypeRepX ty) = showsPrec p ty
 
--- Some (Show.TypeRepX) helpers:
-{-
--- FIXME: Handle tuples, etc.
+splitApps :: TypeRep a -> (TyCon, [TypeRepX])
+splitApps = go []
+  where
+    go :: [TypeRepX] -> TypeRep a -> (TyCon, [TypeRepX])
+    go xs (TrTyCon _ tc _) = (tc, xs)
+    go xs (TrApp _ f x)    = go (TypeRepX x : xs) f
+
+isListTyCon :: TyCon -> Bool
+isListTyCon tc = tc == typeRepTyCon (typeRep :: TypeRep [Int])
+
+isTupleTyCon :: TyCon -> Bool
+isTupleTyCon tc
+  | ('(':',':_) <- tyConName tc = True
+  | otherwise                   = False
+
 showArgs :: Show a => ShowS -> [a] -> ShowS
 showArgs _   []     = id
 showArgs _   [a]    = showsPrec 10 a
 showArgs sep (a:as) = showsPrec 10 a . sep . showArgs sep as
 
-showTuple :: [TypeRepX] -> ShowS
-showTuple args = showChar '('
-               . showArgs (showChar ',') args
-               . showChar ')'
--}
-
 -- | Helper to fully evaluate 'TypeRep' for use as @NFData(rnf)@ implementation
 --
 -- @since TODO



More information about the ghc-commits mailing list