Skip to content

Commit b018774

Browse files
committed
Update SystemF syntax
Add a universal type
1 parent 3a94b6c commit b018774

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/Language/SystemF/Expression.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ data SystemFExpr name ty
1818
data 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
9495
pprTy pdoc space (TyVar n) = prettyPrint n `add` pdoc
9596
pprTy pdoc space (TyArrow a b) = pprTyArrow pdoc space a b
97+
pprTy pdoc _ (TyForAll n t) = pprTyForAll pdoc n t
9698

9799
pprTyArrow :: 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
117127
pprTAbs :: (PrettyPrint n, PrettyPrint t)
118128
=> PDoc String

test/Language/SystemF/ExpressionSpec.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff 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+

0 commit comments

Comments
 (0)