shithub: MicroHs

Download patch

ref: 11a32d05eefc1d6cabd5312af46eba9b793174fd
parent: c5476618dc0691249822b37819e7fb20d3cc0b31
author: Lennart Augustsson <lennart@augustsson.net>
date: Mon Dec 25 14:00:53 EST 2023

Update Typeable for prettier print.

--- a/lib/Data/Typeable.hs
+++ b/lib/Data/Typeable.hs
@@ -52,7 +52,14 @@
   TypeRep k1 _ _ <= TypeRep k2 _ _  =  k1 <= k2
 
 instance Show TypeRep where
-  showsPrec p (TypeRep _ c ts) = showParen (p > 11) $ showsPrec 11 c . foldr (\ t s -> showChar ' ' . showsPrec 11 t . s) id ts
+  showsPrec p (TypeRep _ c []) = showsPrec 11 c
+  showsPrec p (TypeRep _ c [a,b]) | c == funTc = showParen (p > 5) $ showsPrec 6 a . showString " -> " . showsPrec 5 b
+  showsPrec p (TypeRep _ c [a]) | tyConName c == "[]" = showString "[" . showsPrec 0 a . showString "]"
+  showsPrec p (TypeRep _ c ts) | head (tyConName c) == ',' = showParen True $ comma (map (showsPrec 0) ts)
+                               | otherwise = showParen (p > 11) $ showsPrec 11 c . foldr (\ t s -> showChar ' ' . showsPrec 12 t . s) id ts
+    where comma [] = undefined
+          comma [s] = s
+          comma (s:ss) = s . showString "," . comma ss
 
 typeRepTyCon :: TypeRep -> TyCon
 typeRepTyCon (TypeRep _ tc _) = tc
--- /dev/null
+++ b/tests/Typeable.hs
@@ -1,0 +1,21 @@
+module Typeable(main) where
+import Prelude
+import Data.Proxy
+import Data.Typeable
+
+data T = A
+  deriving Typeable
+
+data P a = B a
+  deriving (Typeable)
+
+main :: IO ()
+main = do
+  print $ typeOf True
+  print $ typeOf (return True :: IO Bool)
+  print $ typeOf ('a', False)
+  print $ typeRep (Proxy :: Proxy Monad)
+  print $ typeOf A
+  print $ typeOf (B (B A))
+  print $ typeOf [1::Int]
+  print $ typeOf ((+) :: Int -> Int -> Int)
--- /dev/null
+++ b/tests/Typeable.ref
@@ -1,0 +1,8 @@
+Char
+IO Char
+(Char,Char)
+Monad
+T
+P (P T)
+[Int]
+Int -> Int -> Int
--