@@ -1,5 +1,6 @@
1module AST.Type
1module AST.Type
2+ ( Type(..), AliasType(..)
2− ( Type(..), RawType, CanonicalType
3+ , RawType, CanonicalType
3 , PortType(..), PortDirection(..)
4 , PortType(..), PortDirection(..)
4 , fieldMap, recordOf, listOf, tupleOf
5 , fieldMap, recordOf, listOf, tupleOf
5 , deepDealias, dealias
6 , deepDealias, dealias
@@ -26,7 +27,13 @@ data Type var
26 | Type var
27 | Type var
27 | App (Type var) [Type var]
28 | App (Type var) [Type var]
28 | Record [(String, Type var)] (Maybe (Type var))
29 | Record [(String, Type var)] (Maybe (Type var))
29− | Aliased Var.Canonical [(String, Type var)] (Type var)
30+ | Aliased Var.Canonical [(String, Type var)] (AliasType var)
30 deriving (Eq,Show)
37 deriving (Eq,Show)
@@ -102,9 +109,14 @@ deepDealias tipe =
102 App (go f) (map go args)
109 App (go f) (map go args)
105−dealias :: [(String, Type v)] -> Type v -> Type v
112+dealias :: [(String, Type v)] -> AliasType v -> Type v
106−dealias args tipe =
113+dealias args aliasType =
107− dealiasHelp (Map.fromList args) tipe
116+ dealiasHelp (Map.fromList args) tipe
110dealiasHelp :: Map.Map String (Type var) -> Type var -> Type var
122dealiasHelp :: Map.Map String (Type var) -> Type var -> Type var
@@ -188,13 +200,6 @@ instance (Var.ToString var, Pretty var) => Pretty (Type var) where
188 prettyField (field, tipe) =
200 prettyField (field, tipe) =
189 P.text field <+> P.text ":" <+> pretty tipe
201 P.text field <+> P.text ":" <+> pretty tipe
191− Aliased name [] t ->
194− if show t' `elem` ["Int", "Float", "String", "Char", "Bool"]
198 Aliased name args _ ->
203 Aliased name args _ ->
199 P.hang (pretty name) 2 (P.sep (map (prettyParens . snd) args))
204 P.hang (pretty name) 2 (P.sep (map (prettyParens . snd) args))
@@ -246,8 +251,8 @@ flattenRecord tipe =
247 (fields' ++ fields, ext')
252 (fields' ++ fields, ext')
249− Aliased _ _ tipe' ->
254+ Aliased _ args tipe' ->
250− flattenRecord tipe'
255+ flattenRecord (dealias args tipe')
253 error "Trying to flatten ill-formed record."
258 error "Trying to flatten ill-formed record."
@@ -286,3 +291,20 @@ instance Binary var => Binary (Type var) where
286 4 -> Record <$> get <*> get
291 4 -> Record <$> get <*> get
287 5 -> Aliased <$> get <*> get <*> get
292 5 -> Aliased <$> get <*> get <*> get
288 _ -> error "Error reading a valid type from serialized string"
293 _ -> error "Error reading a valid type from serialized string"
296+instance Binary var => Binary (AliasType var) where
300+ putWord8 0 >> put tipe
303+ putWord8 1 >> put tipe
310+ _ -> error "Error reading a valid type from serialized string"