on master help sign in
cosmetic: generalize `collectLambdas` to make a list of types, move prettifying code to the Pretty instance
6c1d2d7Evan Czaplicki
compiler/SourceSyntax/Type.hs+10−6
@@ -1,4 +1,4 @@
1−{-# OPTIONS_GHC -Wall #-}
1+{-# OPTIONS_GHC -W #-}
2module SourceSyntax.Type where
2module SourceSyntax.Type where
3
3
4import Data.Binary
4import Data.Binary
@@ -32,7 +32,12 @@ instance Pretty Type where
32 pretty tipe =
32 pretty tipe =
33 case tipe of
33 case tipe of
34 Lambda _ _ -> P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
34 Lambda _ _ -> P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
35+ where
35− where t:ts = collectLambdas tipe
36+ t:ts = map prettyLambda (collectLambdas tipe)
37+ prettyLambda t = case t of
38+ Lambda _ _ -> P.parens (pretty t)
39+ _ -> pretty t
40+
36 Var x -> P.text x
41 Var x -> P.text x
37 Data "_List" [t] -> P.brackets (pretty t)
42 Data "_List" [t] -> P.brackets (pretty t)
38 Data name tipes
43 Data name tipes
@@ -46,12 +51,11 @@ instance Pretty Type where
46 prettyField (f,t) = P.text f <+> P.text ":" <+> pretty t
51 prettyField (f,t) = P.text f <+> P.text ":" <+> pretty t
47 prettyFields = commaSep . map prettyField $ fields
52 prettyFields = commaSep . map prettyField $ fields
48
53
49−collectLambdas :: Type -> [Doc]
54+collectLambdas :: Type -> [Type]
50collectLambdas tipe =
55collectLambdas tipe =
51 case tipe of
56 case tipe of
52− Lambda arg@(Lambda _ _) body -> P.parens (pretty arg) : collectLambdas body
53− Lambda arg body -> pretty arg : collectLambdas body
57+ Lambda arg body -> arg : collectLambdas body
54− _ -> [pretty tipe]
58+ _ -> [tipe]
55
59
56prettyParens :: Type -> Doc
60prettyParens :: Type -> Doc
57prettyParens tipe = parensIf needed (pretty tipe)
61prettyParens tipe = parensIf needed (pretty tipe)