File tree Expand file tree Collapse file tree 2 files changed +18
-0
lines changed
Expand file tree Collapse file tree 2 files changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ data SystemFExpr name ty
1818data Ty name
1919 = TyVar name -- Type variable (T)
2020 | TyArrow (Ty name ) (Ty name ) -- Type arrow (T -> U)
21+ | TyForAll name (Ty name ) -- Universal type (forall T. X)
2122 deriving (Eq , Show )
2223
2324-- Pretty printing
@@ -93,6 +94,7 @@ pprTy :: PrettyPrint n
9394 -> PDoc String
9495pprTy pdoc space (TyVar n) = prettyPrint n `add` pdoc
9596pprTy pdoc space (TyArrow a b) = pprTyArrow pdoc space a b
97+ pprTy pdoc _ (TyForAll n t) = pprTyForAll pdoc n t
9698
9799pprTyArrow :: PrettyPrint n
98100 => PDoc String
@@ -113,6 +115,14 @@ pprTyArrow' space a b = a <> arrow <> b
113115 where arrow | space = " -> " `add` empty
114116 | otherwise = " ->" `add` empty
115117
118+ pprTyForAll :: PrettyPrint n
119+ => PDoc String
120+ -> n
121+ -> Ty n
122+ -> PDoc String
123+ pprTyForAll pdoc n t = prefix <> prettyPrint t `add` pdoc
124+ where prefix = between (prettyPrint n `add` empty) " forall " " . " empty
125+
116126-- Pretty print a type abstraction
117127pprTAbs :: (PrettyPrint n , PrettyPrint t )
118128 => PDoc String
Original file line number Diff line number Diff line change @@ -58,10 +58,18 @@ spec = describe "prettyPrint" $ do
5858 it " print simple arrow types" $
5959 prettyPrint (TyArrow (TyVar " A" ) (TyVar " B" )) `shouldBe` " A -> B"
6060
61+ it " prints simple forall types" $
62+ prettyPrint (TyForAll " X" (TyVar " X" )) `shouldBe` " forall X. X"
63+
6164 it " prints chained arrow types" $
6265 prettyPrint (TyArrow (TyVar " X" ) (TyArrow (TyVar " Y" ) (TyVar " Z" )))
6366 `shouldBe` " X -> Y -> Z"
6467
6568 it " prints nested arrow types" $
6669 prettyPrint (TyArrow (TyArrow (TyVar " T" ) (TyVar " U" )) (TyVar " V" ))
6770 `shouldBe` " (T -> U) -> V"
71+
72+ it " prints complex forall types" $
73+ prettyPrint (TyForAll " A" (TyArrow (TyVar " A" ) (TyVar " A" )))
74+ `shouldBe` " forall A. A -> A"
75+
You can’t perform that action at this time.
0 commit comments