From fc7a2876f23510ab795ab2098bbac278d29d6356 Mon Sep 17 00:00:00 2001 From: "simonpj@microsoft.com" Date: Fri, 4 Jun 2010 11:01:43 +0000 Subject: [PATCH] Don't use unnecessary parens when printing types (Fix Trac 4107) f :: Eq a => a -> a rather than f :: (Eq a) => a -> a --- compiler/hsSyn/HsTypes.lhs | 12 ++++++++++-- compiler/types/TypeRep.lhs | 16 +++++++++++++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/compiler/hsSyn/HsTypes.lhs b/compiler/hsSyn/HsTypes.lhs index cb06a7f..806faf2 100644 --- a/compiler/hsSyn/HsTypes.lhs +++ b/compiler/hsSyn/HsTypes.lhs @@ -353,8 +353,16 @@ pprHsForAll exp tvs cxt forall_part = ptext (sLit "forall") <+> interppSP tvs <> dot pprHsContext :: (OutputableBndr name) => HsContext name -> SDoc -pprHsContext [] = empty -pprHsContext cxt = ppr_hs_context cxt <+> ptext (sLit "=>") +pprHsContext [] = empty +pprHsContext [L _ pred] + | noParenHsPred pred = ppr pred <+> ptext (sLit "=>") +pprHsContext cxt = ppr_hs_context cxt <+> ptext (sLit "=>") + +noParenHsPred :: HsPred name -> Bool +-- c.f. TypeRep.noParenPred +noParenHsPred (HsClassP {}) = True +noParenHsPred (HsEqualP {}) = True +noParenHsPred (HsIParam {}) = False ppr_hs_context :: (OutputableBndr name) => HsContext name -> SDoc ppr_hs_context [] = empty diff --git a/compiler/types/TypeRep.lhs b/compiler/types/TypeRep.lhs index 819a71c..55beb28 100644 --- a/compiler/types/TypeRep.lhs +++ b/compiler/types/TypeRep.lhs @@ -452,9 +452,19 @@ pprTheta :: ThetaType -> SDoc pprTheta theta = parens (sep (punctuate comma (map pprPred theta))) pprThetaArrow :: ThetaType -> SDoc -pprThetaArrow theta - | null theta = empty - | otherwise = parens (sep (punctuate comma (map pprPred theta))) <+> ptext (sLit "=>") +pprThetaArrow [] = empty +pprThetaArrow [pred] + | noParenPred pred = pprPred pred <+> ptext (sLit "=>") +pprThetaArrow preds = parens (sep (punctuate comma (map pprPred preds))) <+> ptext (sLit "=>") + +noParenPred :: PredType -> Bool +-- A predicate that can appear without parens before a "=>" +-- C a => a -> a +-- a~b => a -> b +-- But (?x::Int) => Int -> Int +noParenPred (ClassP {}) = True +noParenPred (EqPred {}) = True +noParenPred (IParam {}) = False ------------------ instance Outputable Type where -- 1.7.10.4