@@ -76,7 +76,7 @@ allConstraints name val | isFunction val = do
7676 return (TypeConstraint me (TUnknown n) (ValueOrigin val): cs, n)
7777allConstraints _ val = typeConstraints M.empty val
7878
79−typeOf :: Ident -> Value -> Check PolyType
79+typeOf :: Ident -> Value -> Check Type
8080typeOf name val = do
8181 (cs, n) <- allConstraints name val
8282 desugared <- replaceAllTypeSynonyms cs
@@ -129,12 +129,12 @@ findUnknownRows = everything (++) (mkQ [] f)
129129 f (RUnknown n) = [n]
130130 f _ = []
131131
132−varIfUnknown :: Type -> PolyType
132+varIfUnknown :: Type -> Type
133133varIfUnknown ty =
134134 let
135135 (ty', m) = flip runState M.empty $ everywhereM (flip extM g $ mkM f) ty
136136 in
137− PolyType (sort $ nub $ M.elems m ++ findTypeVars ty) ty'
137+ ForAll (sort $ nub $ M.elems m ++ findTypeVars ty) ty'
138138 where
139139 f :: Type -> State (M.Map Int String) Type
140140 f (TUnknown n) = do
@@ -321,7 +321,7 @@ typeConstraints m v@(Var var@(Qualified mp name)) = do
321321 modulePath <- checkModulePath `fmap` get
322322 case M.lookup (qualify modulePath var) (names env) of
323323 Nothing -> throwError $ show var ++ " is undefined"
324− Just (PolyType idents ty, _) -> do
324+ Just (ForAll idents ty, _) -> do
325325 me <- fresh
326326 replaced <- replaceVarsWithUnknowns idents ty
327327 return ([TypeConstraint me replaced (ValueOrigin v)], me)
@@ -335,7 +335,7 @@ typeConstraints m v@(Constructor c) = do
335335 modulePath <- checkModulePath `fmap` get
336336 case M.lookup (qualify modulePath c) (dataConstructors env) of
337337 Nothing -> throwError $ "Constructor " ++ show c ++ " is undefined"
338− Just (PolyType idents ty) -> do
338+ Just (ForAll idents ty) -> do
339339 me <- fresh
340340 replaced <- replaceVarsWithUnknowns idents ty
341341 return ([TypeConstraint me replaced (ValueOrigin v)], me)
@@ -349,11 +349,15 @@ typeConstraints m v@(IfThenElse cond th el) = do
349349 (cs2, n2) <- typeConstraints m th
350350 (cs3, n3) <- typeConstraints m el
351351 return (TypeConstraint n1 Boolean (ValueOrigin cond) : TypeConstraint n2 (TUnknown n3) (ValueOrigin v) : cs1 ++ cs2 ++ cs3, n2)
352−typeConstraints m v@(TypedValue val poly@(PolyType idents ty)) = do
353− kind <- kindOf poly
352+typeConstraints m v@(TypedValue val ty) = do
353+ kind <- kindOf ty
354354 guardWith ("Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
355− (cs, n1) <- typeConstraints m val
356− return (TypeConstraint n1 ty (ValueOrigin v) : cs, n1)
355+ typeCheck m val ty
356+ me <- fresh
357+ return ([TypeConstraint me ty (ValueOrigin v)], me)
358+
359+typeCheck :: M.Map Ident Int -> Value -> Type -> Check ()
360+typeCheck _ _ _ = return ()
357361
358362unaryOperatorConstraints :: Value -> UnaryOperator -> Int -> Int -> [TypeConstraint]
359363unaryOperatorConstraints v Negate val result = [TypeConstraint val Number (ValueOrigin v), TypeConstraint result Number (ValueOrigin v)]
@@ -403,7 +407,7 @@ typeConstraintsForBinder val b@(NullaryBinder ctor) = do
403407 env <- getEnv
404408 modulePath <- checkModulePath `fmap` get
405409 case M.lookup (qualify modulePath ctor) (dataConstructors env) of
406− Just (PolyType args ret) -> do
410+ Just (ForAll args ret) -> do
407411 ret' <- replaceVarsWithUnknowns args ret
408412 return ([TypeConstraint val ret' (BinderOrigin b)], M.empty)
409413 _ -> throwError $ "Constructor " ++ show ctor ++ " is not defined"
@@ -411,7 +415,7 @@ typeConstraintsForBinder val b@(UnaryBinder ctor binder) = do
411415 env <- getEnv
412416 modulePath <- checkModulePath `fmap` get
413417 case M.lookup (qualify modulePath ctor) (dataConstructors env) of
414− Just (PolyType idents f@(Function [_] _)) -> do
418+ Just (ForAll idents f@(Function [_] _)) -> do
415419 obj <- fresh
416420 (Function [ty] ret) <- replaceVarsWithUnknowns idents f
417421 (cs, m1) <- typeConstraintsForBinder obj binder
@@ -590,6 +594,10 @@ unifyTypes o (SaturatedTypeSynonym name args) ty = do
590594 unifyTypes o replaced ty
591595 Nothing -> error "Type synonym was not defined"
592596unifyTypes o ty s@(SaturatedTypeSynonym _ _) = unifyTypes o s ty
597+unifyTypes o (ForAll idents ty1) ty2 =do
598+ sk <- skolemize idents ty1
599+ unifyTypes o sk ty2
600+unifyTypes o ty f@(ForAll _ _) = unifyTypes o f ty
593601unifyTypes _ Number Number = return []
594602unifyTypes _ String String = return []
595603unifyTypes _ Boolean Boolean = return []
@@ -609,6 +617,7 @@ unifyTypes o (TypeApp t1 t2) (TypeApp t3 t4) = do
609617 cs1 <- unifyTypes o t1 t3
610618 cs2 <- unifyTypes o t2 t4
611619 return $ cs1 ++ cs2
620+unifyTypes _ (Skolem s1) (Skolem s2) | s1 == s2 = return []
612621unifyTypes _ t1 t2 = throwError $ "Cannot unify " ++ prettyPrintType t1 ++ " with " ++ prettyPrintType t2 ++ "."
613622
614623unifyRows :: TypeConstraintOrigin -> Row -> Row -> Check [TypeConstraint]
@@ -638,3 +647,10 @@ unifyRows o r1 r2 =
638647 unifyRows' _ [] REmpty [] REmpty = return []
639648 unifyRows' _ [] (RowVar v1) [] (RowVar v2) | v1 == v2 = return []
640649 unifyRows' _ sd1 r1 sd2 r2 = throwError $ "Cannot unify " ++ prettyPrintRow (rowFromList (sd1, r1)) ++ " with " ++ prettyPrintRow (rowFromList (sd2, r2)) ++ "."
650+
651+skolemize :: [String] -> Type -> Check Type
652+skolemize idents ty = do
653+ sks <- replicateM (length idents) fresh
654+ let m = M.fromList (zip idents (map Skolem sks))
655+ return $ replaceTypeVars m ty
656+