@@ -4,10 +4,13 @@ module Types.Substitutions (subst,
5 concretize,
5 concretize,
7− generalize) where
7+ generalize,
9import Context
11import Context
10import Control.DeepSeq (NFData (..), deepseq)
12import Control.DeepSeq (NFData (..), deepseq)
13+import Control.Arrow (second)
11import Control.Monad (liftM)
14import Control.Monad (liftM)
12import Data.List (foldl')
15import Data.List (foldl')
13import qualified Data.Set as Set
16import qualified Data.Set as Set
@@ -87,3 +90,32 @@ generalize :: [X] -> Scheme -> GuidCounter Scheme
87generalize exceptions (Forall xs cs t) = rescheme (Forall (xs ++ frees) cs t)
90generalize exceptions (Forall xs cs t) = rescheme (Forall (xs ++ frees) cs t)
88 where allFrees = Set.fromList $ freeVars t ++ concatMap freeVars cs
91 where allFrees = Set.fromList $ freeVars t ++ concatMap freeVars cs
89 frees = Set.toList $ Set.difference allFrees (Set.fromList exceptions)
92 frees = Set.toList $ Set.difference allFrees (Set.fromList exceptions)
94+dealias :: [Statement] -> [Statement]
95+dealias stmts = map dealiasS stmts
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))]
103+ dealiasT :: Type -> Type
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)
113+ dealiasS :: Statement -> Statement
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)
121+ TypeAlias _ _ _ -> s