@@ -9,6 +9,7 @@ import AST.PrettyPrint
9import qualified AST.Helpers as Help
9import qualified AST.Helpers as Help
10import Text.PrettyPrint as P
10import Text.PrettyPrint as P
12data Type var
13data Type var
13 = Lambda (Type var) (Type var)
14 = Lambda (Type var) (Type var)
14 | Var String
15 | Var String
@@ -18,47 +19,64 @@ data Type var
18 | Aliased Var.Canonical (Type var)
19 | Aliased Var.Canonical (Type var)
19 deriving (Eq,Show)
20 deriving (Eq,Show)
21−type RawType = Type Var.Raw
23+type RawType =
22−type CanonicalType = Type Var.Canonical
27+type CanonicalType =
24fieldMap :: [(String,a)] -> Map.Map String [a]
31fieldMap :: [(String,a)] -> Map.Map String [a]
25fieldMap fields =
32fieldMap fields =
26− foldl (\r (x,t) -> Map.insertWith (++) x [t] r) Map.empty fields
33+ foldl (\r (field,tipe) -> Map.insertWith (++) field [tipe] r) Map.empty fields
28recordOf :: [(String, Type var)] -> Type var
36recordOf :: [(String, Type var)] -> Type var
29−recordOf fields = Record fields Nothing
38+ Record fields Nothing
31listOf :: RawType -> RawType
41listOf :: RawType -> RawType
32−listOf t = App (Type (Var.Raw "List")) [t]
43+ App (Type (Var.Raw "List")) [tipe]
34tupleOf :: [RawType] -> RawType
46tupleOf :: [RawType] -> RawType
35−tupleOf ts = App (Type t) ts
47+tupleOf types =
37− t = Var.Raw ("_Tuple" ++ show (length ts))
48+ let name = Var.Raw ("_Tuple" ++ show (length types))
50+ App (Type name) types
39instance (Var.ToString var, Pretty var) => Pretty (Type var) where
53instance (Var.ToString var, Pretty var) => Pretty (Type var) where
40 pretty tipe =
54 pretty tipe =
41 case tipe of
55 case tipe of
42− Lambda _ _ -> P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
57+ P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
44 t:ts = map prettyLambda (collectLambdas tipe)
59 t:ts = map prettyLambda (collectLambdas tipe)
45− prettyLambda t = case t of
60+ prettyLambda t =
46− Lambda _ _ -> P.parens (pretty t)
62+ Lambda _ _ -> P.parens (pretty t)
47− _ -> pretty t
63+ _ -> pretty t
49− Var x -> P.text x
66+ P.text x
51 Type var ->
68 Type var ->
52− let v = Var.toString var in
69+ let v = Var.toString var
53− P.text (if v == "_Tuple0" then "()" else v)
71+ P.text (if v == "_Tuple0" then "()" else v)
55 App f args ->
73 App f args ->
56 case (f,args) of
74 case (f,args) of
57 (Type name, _)
75 (Type name, _)
58 | Help.isTuple (Var.toString name) ->
76 | Help.isTuple (Var.toString name) ->
59− P.parens . P.sep . P.punctuate P.comma $ map pretty args
77+ P.parens (P.sep (P.punctuate P.comma (map pretty args)))
61− _ -> P.hang (pretty f) 2 (P.sep $ map prettyParens args)
79+ _ -> P.hang (pretty f) 2 (P.sep (map prettyParens args))
63 Record _ _ ->
81 Record _ _ ->
64 case flattenRecord tipe of
82 case flattenRecord tipe of
@@ -84,20 +102,26 @@ instance (Var.ToString var, Pretty var) => Pretty (Type var) where
84 P.text field <+> P.text ":" <+> pretty tipe
102 P.text field <+> P.text ":" <+> pretty tipe
86 Aliased name t ->
104 Aliased name t ->
87− let t' = pretty t in
105+ let t' = pretty t
88− if show t' `elem` ["Int", "Float", "String", "Char", "Bool"]
107+ if show t' `elem` ["Int", "Float", "String", "Char", "Bool"]
90− else pretty name
109+ else pretty name
93collectLambdas :: Type var -> [Type var]
112collectLambdas :: Type var -> [Type var]
94collectLambdas tipe =
113collectLambdas tipe =
95 case tipe of
114 case tipe of
96− Lambda arg body -> arg : collectLambdas body
116+ arg : collectLambdas body
97− _ -> [tipe]
119+ [tipe]
99prettyParens :: (Var.ToString var, Pretty var) => Type var -> Doc
122prettyParens :: (Var.ToString var, Pretty var) => Type var -> Doc
100−prettyParens tipe = parensIf (needed tipe) (pretty tipe)
124+ parensIf (needed tipe) (pretty tipe)
102 needed t =
126 needed t =
103 case t of
127 case t of
@@ -105,26 +129,37 @@ prettyParens tipe = parensIf (needed tipe) (pretty tipe)
106 Lambda _ _ -> True
130 Lambda _ _ -> True
108− App (Type name) _ | Help.isTuple (Var.toString name) -> False
133+ | Help.isTuple (Var.toString name) ->
109 App t' [] -> needed t'
136 App t' [] -> needed t'
110 App _ _ -> True
138 App _ _ -> True
112 _ -> False
140 _ -> False
114flattenRecord :: Type var -> ( [(String, Type var)], Maybe String )
143flattenRecord :: Type var -> ( [(String, Type var)], Maybe String )
115flattenRecord tipe =
144flattenRecord tipe =
116− case tipe of
145+ case tipe of
117− Var x -> ([], Just x)
147+ ([], Just x)
149+ Record fields Nothing ->
119− Record fields Nothing -> (fields, Nothing)
152+ Record fields (Just ext) ->
153+ let (fields',ext') = flattenRecord ext
155+ (fields' ++ fields, ext')
121− Record fields (Just ext) ->
157+ Aliased _ tipe' ->
122− let (fields',ext') = flattenRecord ext
158+ flattenRecord tipe'
123− in (fields' ++ fields, ext')
125− Aliased _ tipe' -> flattenRecord tipe'
160+ _ ->
161+ error "Trying to flatten ill-formed record."
127− _ -> error "Trying to flatten ill-formed record."
129instance Binary var => Binary (Type var) where
164instance Binary var => Binary (Type var) where
130 put tipe =
165 put tipe =