@@ -38,16 +38,23 @@ inference (Lambda x e) =
3838 Map.findWithDefault [] x a
3939 , LambdaT b t )
4040inference (Let defs e) =
41− do (a,c,t) <- inference e
42− let (xs,es) = unzip defs
43− (as,cs,ts) <- unzip3 `liftM` mapM inference es
44− let assumptions = unionsA (a:as)
45− let f x t = map (:=: t) . map VarT $ Map.findWithDefault [] x assumptions
46− let constraints = Set.fromList . concat $ zipWith f xs ts
47− return ( foldr Map.delete assumptions xs
48− , Set.unions $ c:constraints:cs
41+ do (as,cs,t) <- inference e
42+ (ass, schemes) <- liftM unzip (mapM defScheme defs)
43+ let assumptions = unionsA (as:ass)
44+ let names = map (\(Definition x _ _) -> x) defs
45+ let genCs name s = map (\x -> VarT x :<<: s) $ Map.findWithDefault [] name assumptions
46+ let cs' = Set.fromList . concat $ zipWith genCs names schemes
47+ return ( foldr Map.delete assumptions names
48+ , Set.union cs' cs
4949 , t )
5050
51+inference (Case e cases) =
52+ do (as,cs,t) <- inference e
53+ (ass,css,ts) <- liftM unzip3 $ mapM (caseInference t) cases
54+ return ( unionsA $ as:ass
55+ , Set.unions $ Set.fromList (zipWith (:=:) ts $ tail ts) : cs : css
56+ , head ts)
57+
5158inference (If e1 e2 e3) =
5259 do (a1,c1,t1) <- inference e1
5360 (a2,c2,t2) <- inference e2
@@ -72,3 +79,22 @@ inference other =
7279 _ -> beta >>= primitive
7380
7481primitive t = return (Map.empty, Set.empty, t)
82+
83+caseInference tipe (p,e) = do
84+ (as,cs,t) <- inference e
85+ return ( foldr Map.delete as (namesIn p)
86+ , cs
87+ , t)
88+
89+namesIn PAnything = []
90+namesIn (PVar v) = [v]
91+namesIn (PData name ps) = concatMap namesIn ps
92+
93+defScheme (Definition name args e) =
94+ do argDict <- mapM (\a -> liftM ((,) a) guid) args
95+ (as,cs,t) <- inference e
96+ let genCs (arg,x) = map (\y -> VarT x :=: VarT y) $ Map.findWithDefault [] arg as
97+ let cs' = concatMap genCs argDict
98+ let tipe = foldr (==>) t $ map (VarT . snd) argDict
99+ return (foldr Map.delete as args
100+ , Forall (map snd argDict) (cs' ++ Set.toList cs) tipe)