@@ -25,6 +25,7 @@ import Control.Applicative
25import qualified Text.Parsec as P
25import qualified Text.Parsec as P
26import qualified Text.Parsec.Expr as P
26import qualified Text.Parsec.Expr as P
27import Control.Arrow (Arrow(..))
27import Control.Arrow (Arrow(..))
28+import Control.Monad (unless)
29parseNumber :: P.Parsec String ParseState Type
30parseNumber :: P.Parsec String ParseState Type
30parseNumber = const Number <$> reserved "Number"
31parseNumber = const Number <$> reserved "Number"
@@ -43,7 +44,7 @@ parseObject = braces $ Object <$> parseRow
44parseFunction :: P.Parsec String ParseState Type
45parseFunction :: P.Parsec String ParseState Type
45parseFunction = do
46parseFunction = do
46− args <- lexeme $ parens $ commaSep parseType
47+ args <- lexeme $ parens $ commaSep parsePolyType
47 lexeme $ P.string "->"
48 lexeme $ P.string "->"
48 resultType <- parseType
49 resultType <- parseType
49 return $ Function args resultType
50 return $ Function args resultType
@@ -54,6 +55,10 @@ parseTypeVariable = TypeVar <$> identifier
54parseTypeConstructor :: P.Parsec String ParseState Type
55parseTypeConstructor :: P.Parsec String ParseState Type
55parseTypeConstructor = TypeConstructor <$> parseQualified properName
56parseTypeConstructor = TypeConstructor <$> parseQualified properName
58+parseForAll :: P.Parsec String ParseState Type
59+parseForAll = (ForAll <$> (P.try (reserved "forall") *> many (indented *> identifier) <* indented <* dot)
57parseTypeAtom :: P.Parsec String ParseState Type
62parseTypeAtom :: P.Parsec String ParseState Type
58parseTypeAtom = indented *> P.choice (map P.try
63parseTypeAtom = indented *> P.choice (map P.try
59 [ parseNumber
64 [ parseNumber
@@ -64,20 +69,28 @@ parseTypeAtom = indented *> P.choice (map P.try
64 , parseFunction
69 , parseFunction
65 , parseTypeVariable
70 , parseTypeVariable
66 , parseTypeConstructor
71 , parseTypeConstructor
67 , parens parseType ])
73 , parens parseType ])
69−parsePolyType :: P.Parsec String ParseState PolyType
70−parsePolyType = (ForAll <$> (P.try (reserved "forall") *> many (indented *> identifier) <* indented <* dot)
72− (ForAll [] <$> parseType)
74−parseType :: P.Parsec String ParseState Type
75+parseAnyType :: P.Parsec String ParseState Type
75−parseType = (P.buildExpressionParser operators . buildPostfixParser postfixTable $ parseTypeAtom) P.<?> "type"
76+parseAnyType = (P.buildExpressionParser operators . buildPostfixParser postfixTable $ parseTypeAtom) P.<?> "type"
77 postfixTable :: [P.Parsec String ParseState (Type -> Type)]
78 postfixTable :: [P.Parsec String ParseState (Type -> Type)]
78 postfixTable = [ flip TypeApp <$> P.try (indented *> parseTypeAtom) ]
79 postfixTable = [ flip TypeApp <$> P.try (indented *> parseTypeAtom) ]
79 operators = [ [ P.Infix (lexeme (P.try (P.string "->")) >> return (\t1 t2 -> Function [t1] t2)) P.AssocRight ] ]
80 operators = [ [ P.Infix (lexeme (P.try (P.string "->")) >> return (\t1 t2 -> Function [t1] t2)) P.AssocRight ] ]
82+parseType :: P.Parsec String ParseState Type
85+ unless (isMonoType ty) $ P.unexpected "polymorphic type"
88+parsePolyType :: P.Parsec String ParseState PolyType
91+ unless (isPolyType ty) $ P.unexpected "polymorphic type"
81parseNameAndType :: P.Parsec String ParseState (String, Type)
94parseNameAndType :: P.Parsec String ParseState (String, Type)
82parseNameAndType = (,) <$> (indented *> identifier <* indented <* lexeme (P.string "::")) <*> parseType
95parseNameAndType = (,) <$> (indented *> identifier <* indented <* lexeme (P.string "::")) <*> parseType