@@ -25,6 +25,7 @@ import Control.Applicative
2525import qualified Text.Parsec as P
2626import qualified Text.Parsec.Expr as P
2727import Control.Arrow (Arrow(..))
28+import Control.Monad (unless)
2829
2930parseNumber :: P.Parsec String ParseState Type
3031parseNumber = const Number <$> reserved "Number"
@@ -43,7 +44,7 @@ parseObject = braces $ Object <$> parseRow
4344
4445parseFunction :: P.Parsec String ParseState Type
4546parseFunction = do
46− args <- lexeme $ parens $ commaSep parseType
47+ args <- lexeme $ parens $ commaSep parsePolyType
4748 lexeme $ P.string "->"
4849 resultType <- parseType
4950 return $ Function args resultType
@@ -54,6 +55,10 @@ parseTypeVariable = TypeVar <$> identifier
5455parseTypeConstructor :: P.Parsec String ParseState Type
5556parseTypeConstructor = TypeConstructor <$> parseQualified properName
5657
58+parseForAll :: P.Parsec String ParseState Type
59+parseForAll = (ForAll <$> (P.try (reserved "forall") *> many (indented *> identifier) <* indented <* dot)
60+ <*> parseType)
61+
5762parseTypeAtom :: P.Parsec String ParseState Type
5863parseTypeAtom = indented *> P.choice (map P.try
5964 [ parseNumber
@@ -64,20 +69,28 @@ parseTypeAtom = indented *> P.choice (map P.try
6469 , parseFunction
6570 , parseTypeVariable
6671 , parseTypeConstructor
72+ , parseForAll
6773 , parens parseType ])
6874
69−parsePolyType :: P.Parsec String ParseState PolyType
70−parsePolyType = (ForAll <$> (P.try (reserved "forall") *> many (indented *> identifier) <* indented <* dot)
71− <*> parseType) <|>
72− (ForAll [] <$> parseType)
73−
74−parseType :: P.Parsec String ParseState Type
75−parseType = (P.buildExpressionParser operators . buildPostfixParser postfixTable $ parseTypeAtom) P.<?> "type"
75+parseAnyType :: P.Parsec String ParseState Type
76+parseAnyType = (P.buildExpressionParser operators . buildPostfixParser postfixTable $ parseTypeAtom) P.<?> "type"
7677 where
7778 postfixTable :: [P.Parsec String ParseState (Type -> Type)]
7879 postfixTable = [ flip TypeApp <$> P.try (indented *> parseTypeAtom) ]
7980 operators = [ [ P.Infix (lexeme (P.try (P.string "->")) >> return (\t1 t2 -> Function [t1] t2)) P.AssocRight ] ]
8081
82+parseType :: P.Parsec String ParseState Type
83+parseType = do
84+ ty <- parseAnyType
85+ unless (isMonoType ty) $ P.unexpected "polymorphic type"
86+ return ty
87+
88+parsePolyType :: P.Parsec String ParseState PolyType
89+parsePolyType = do
90+ ty <- parseAnyType
91+ unless (isPolyType ty) $ P.unexpected "polymorphic type"
92+ return ty
93+
8194parseNameAndType :: P.Parsec String ParseState (String, Type)
8295parseNameAndType = (,) <$> (indented *> identifier <* indented <* lexeme (P.string "::")) <*> parseType
8396