@@ -76,7 +76,7 @@ allConstraints name val | isFunction val = do
76 return (TypeConstraint me (TUnknown n) (ValueOrigin val): cs, n)
76 return (TypeConstraint me (TUnknown n) (ValueOrigin val): cs, n)
77allConstraints _ val = typeConstraints M.empty val
77allConstraints _ val = typeConstraints M.empty val
79−typeOf :: Ident -> Value -> Check PolyType
79+typeOf :: Ident -> Value -> Check Type
80typeOf name val = do
80typeOf name val = do
81 (cs, n) <- allConstraints name val
81 (cs, n) <- allConstraints name val
82 desugared <- replaceAllTypeSynonyms cs
82 desugared <- replaceAllTypeSynonyms cs
@@ -129,12 +129,12 @@ findUnknownRows = everything (++) (mkQ [] f)
129 f (RUnknown n) = [n]
129 f (RUnknown n) = [n]
132−varIfUnknown :: Type -> PolyType
132+varIfUnknown :: Type -> Type
133varIfUnknown ty =
133varIfUnknown ty =
135 (ty', m) = flip runState M.empty $ everywhereM (flip extM g $ mkM f) ty
135 (ty', m) = flip runState M.empty $ everywhereM (flip extM g $ mkM f) ty
137− PolyType (sort $ nub $ M.elems m ++ findTypeVars ty) ty'
137+ ForAll (sort $ nub $ M.elems m ++ findTypeVars ty) ty'
139 f :: Type -> State (M.Map Int String) Type
139 f :: Type -> State (M.Map Int String) Type
140 f (TUnknown n) = do
140 f (TUnknown n) = do
@@ -321,7 +321,7 @@ typeConstraints m v@(Var var@(Qualified mp name)) = do
321 modulePath <- checkModulePath `fmap` get
321 modulePath <- checkModulePath `fmap` get
322 case M.lookup (qualify modulePath var) (names env) of
322 case M.lookup (qualify modulePath var) (names env) of
323 Nothing -> throwError $ show var ++ " is undefined"
323 Nothing -> throwError $ show var ++ " is undefined"
324− Just (PolyType idents ty, _) -> do
324+ Just (ForAll idents ty, _) -> do
325 me <- fresh
325 me <- fresh
326 replaced <- replaceVarsWithUnknowns idents ty
326 replaced <- replaceVarsWithUnknowns idents ty
327 return ([TypeConstraint me replaced (ValueOrigin v)], me)
327 return ([TypeConstraint me replaced (ValueOrigin v)], me)
@@ -335,7 +335,7 @@ typeConstraints m v@(Constructor c) = do
335 modulePath <- checkModulePath `fmap` get
335 modulePath <- checkModulePath `fmap` get
336 case M.lookup (qualify modulePath c) (dataConstructors env) of
336 case M.lookup (qualify modulePath c) (dataConstructors env) of
337 Nothing -> throwError $ "Constructor " ++ show c ++ " is undefined"
337 Nothing -> throwError $ "Constructor " ++ show c ++ " is undefined"
338− Just (PolyType idents ty) -> do
338+ Just (ForAll idents ty) -> do
339 me <- fresh
339 me <- fresh
340 replaced <- replaceVarsWithUnknowns idents ty
340 replaced <- replaceVarsWithUnknowns idents ty
341 return ([TypeConstraint me replaced (ValueOrigin v)], me)
341 return ([TypeConstraint me replaced (ValueOrigin v)], me)
@@ -349,11 +349,15 @@ typeConstraints m v@(IfThenElse cond th el) = do
349 (cs2, n2) <- typeConstraints m th
349 (cs2, n2) <- typeConstraints m th
350 (cs3, n3) <- typeConstraints m el
350 (cs3, n3) <- typeConstraints m el
351 return (TypeConstraint n1 Boolean (ValueOrigin cond) : TypeConstraint n2 (TUnknown n3) (ValueOrigin v) : cs1 ++ cs2 ++ cs3, n2)
351 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
352+typeConstraints m v@(TypedValue val ty) = do
353− kind <- kindOf poly
353+ kind <- kindOf ty
354 guardWith ("Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
354 guardWith ("Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
355− (cs, n1) <- typeConstraints m val
355+ typeCheck m val ty
356− return (TypeConstraint n1 ty (ValueOrigin v) : cs, n1)
357+ return ([TypeConstraint me ty (ValueOrigin v)], me)
359+typeCheck :: M.Map Ident Int -> Value -> Type -> Check ()
360+typeCheck _ _ _ = return ()
358unaryOperatorConstraints :: Value -> UnaryOperator -> Int -> Int -> [TypeConstraint]
362unaryOperatorConstraints :: Value -> UnaryOperator -> Int -> Int -> [TypeConstraint]
359unaryOperatorConstraints v Negate val result = [TypeConstraint val Number (ValueOrigin v), TypeConstraint result Number (ValueOrigin v)]
363unaryOperatorConstraints v Negate val result = [TypeConstraint val Number (ValueOrigin v), TypeConstraint result Number (ValueOrigin v)]
@@ -403,7 +407,7 @@ typeConstraintsForBinder val b@(NullaryBinder ctor) = do
403 env <- getEnv
407 env <- getEnv
404 modulePath <- checkModulePath `fmap` get
408 modulePath <- checkModulePath `fmap` get
405 case M.lookup (qualify modulePath ctor) (dataConstructors env) of
409 case M.lookup (qualify modulePath ctor) (dataConstructors env) of
406− Just (PolyType args ret) -> do
410+ Just (ForAll args ret) -> do
407 ret' <- replaceVarsWithUnknowns args ret
411 ret' <- replaceVarsWithUnknowns args ret
408 return ([TypeConstraint val ret' (BinderOrigin b)], M.empty)
412 return ([TypeConstraint val ret' (BinderOrigin b)], M.empty)
409 _ -> throwError $ "Constructor " ++ show ctor ++ " is not defined"
413 _ -> throwError $ "Constructor " ++ show ctor ++ " is not defined"
@@ -411,7 +415,7 @@ typeConstraintsForBinder val b@(UnaryBinder ctor binder) = do
411 env <- getEnv
415 env <- getEnv
412 modulePath <- checkModulePath `fmap` get
416 modulePath <- checkModulePath `fmap` get
413 case M.lookup (qualify modulePath ctor) (dataConstructors env) of
417 case M.lookup (qualify modulePath ctor) (dataConstructors env) of
414− Just (PolyType idents f@(Function [_] _)) -> do
418+ Just (ForAll idents f@(Function [_] _)) -> do
415 obj <- fresh
419 obj <- fresh
416 (Function [ty] ret) <- replaceVarsWithUnknowns idents f
420 (Function [ty] ret) <- replaceVarsWithUnknowns idents f
417 (cs, m1) <- typeConstraintsForBinder obj binder
421 (cs, m1) <- typeConstraintsForBinder obj binder
@@ -590,6 +594,10 @@ unifyTypes o (SaturatedTypeSynonym name args) ty = do
590 unifyTypes o replaced ty
594 unifyTypes o replaced ty
591 Nothing -> error "Type synonym was not defined"
595 Nothing -> error "Type synonym was not defined"
592unifyTypes o ty s@(SaturatedTypeSynonym _ _) = unifyTypes o s ty
596unifyTypes o ty s@(SaturatedTypeSynonym _ _) = unifyTypes o s ty
597+unifyTypes o (ForAll idents ty1) ty2 =do
598+ sk <- skolemize idents ty1
600+unifyTypes o ty f@(ForAll _ _) = unifyTypes o f ty
593unifyTypes _ Number Number = return []
601unifyTypes _ Number Number = return []
594unifyTypes _ String String = return []
602unifyTypes _ String String = return []
595unifyTypes _ Boolean Boolean = return []
603unifyTypes _ Boolean Boolean = return []
@@ -609,6 +617,7 @@ unifyTypes o (TypeApp t1 t2) (TypeApp t3 t4) = do
609 cs1 <- unifyTypes o t1 t3
617 cs1 <- unifyTypes o t1 t3
610 cs2 <- unifyTypes o t2 t4
618 cs2 <- unifyTypes o t2 t4
611 return $ cs1 ++ cs2
619 return $ cs1 ++ cs2
620+unifyTypes _ (Skolem s1) (Skolem s2) | s1 == s2 = return []
612unifyTypes _ t1 t2 = throwError $ "Cannot unify " ++ prettyPrintType t1 ++ " with " ++ prettyPrintType t2 ++ "."
621unifyTypes _ t1 t2 = throwError $ "Cannot unify " ++ prettyPrintType t1 ++ " with " ++ prettyPrintType t2 ++ "."
614unifyRows :: TypeConstraintOrigin -> Row -> Row -> Check [TypeConstraint]
623unifyRows :: TypeConstraintOrigin -> Row -> Row -> Check [TypeConstraint]
@@ -638,3 +647,10 @@ unifyRows o r1 r2 =
638 unifyRows' _ [] REmpty [] REmpty = return []
647 unifyRows' _ [] REmpty [] REmpty = return []
639 unifyRows' _ [] (RowVar v1) [] (RowVar v2) | v1 == v2 = return []
648 unifyRows' _ [] (RowVar v1) [] (RowVar v2) | v1 == v2 = return []
640 unifyRows' _ sd1 r1 sd2 r2 = throwError $ "Cannot unify " ++ prettyPrintRow (rowFromList (sd1, r1)) ++ " with " ++ prettyPrintRow (rowFromList (sd2, r2)) ++ "."
649 unifyRows' _ sd1 r1 sd2 r2 = throwError $ "Cannot unify " ++ prettyPrintRow (rowFromList (sd1, r1)) ++ " with " ++ prettyPrintRow (rowFromList (sd2, r2)) ++ "."
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