@@ -4,10 +4,13 @@ module Types.Substitutions (subst,
44 freeVars,
55 concretize,
66 rescheme,
7− generalize) where
7+ generalize,
8+ dealias) where
89
10+import Ast
911import Context
1012import Control.DeepSeq (NFData (..), deepseq)
13+import Control.Arrow (second)
1114import Control.Monad (liftM)
1215import Data.List (foldl')
1316import qualified Data.Set as Set
@@ -87,3 +90,32 @@ generalize :: [X] -> Scheme -> GuidCounter Scheme
8790generalize exceptions (Forall xs cs t) = rescheme (Forall (xs ++ frees) cs t)
8891 where allFrees = Set.fromList $ freeVars t ++ concatMap freeVars cs
8992 frees = Set.toList $ Set.difference allFrees (Set.fromList exceptions)
93+
94+dealias :: [Statement] -> [Statement]
95+dealias stmts = map dealiasS stmts
96+ where
97+ getAliases :: [Statement] -> Map.Map String ([X],Type)
98+ getAliases stmts = Map.fromList (concatMap getAlias stmts)
99+ where getAlias stmt = case stmt of
100+ TypeAlias alias xs t -> [(alias, (xs,t))]
101+ _ -> []
102+
103+ dealiasT :: Type -> Type
104+ dealiasT t =
105+ case t of
106+ ADT name ts -> case Map.lookup name (getAliases stmts) of
107+ Just (xs,t) -> dealiasT (subst (zip xs ts) t)
108+ Nothing -> ADT name (map dealiasT ts)
109+ LambdaT t u -> LambdaT (dealiasT t) (dealiasT u)
110+ RecordT r t -> RecordT (Map.map (map dealiasT) r) (dealiasT t)
111+ _ -> t
112+
113+ dealiasS :: Statement -> Statement
114+ dealiasS s =
115+ case s of
116+ Datatype n xs tcs -> Datatype n xs (map (second (map dealiasT)) tcs)
117+ ExportEvent js elm tipe -> ExportEvent js elm (dealiasT tipe)
118+ ImportEvent js e elm tipe -> ImportEvent js e elm (dealiasT tipe)
119+ TypeAnnotation name tipe -> TypeAnnotation name (dealiasT tipe)
120+ Definition _ -> s
121+ TypeAlias _ _ _ -> s