git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge branch '618'

Conflicts: src/Language/PureScript/Types.hs

commit40f03bf93338d0bba9cb0119d0ce45840b5cbf31 authorPhil Freeman <paf31@cantab.net> dateWed 29 Oct 2014, 15:33 (12 years ago) parentee67aece703a92
13 files changed+166−45
docgen/Main.hs+11−3 examples/passing/KindedType.purs+31 src/Language/PureScript/CodeGen/Externs.hs+16−4 src/Language/PureScript/Declarations.hs+3−3 src/Language/PureScript/Environment.hs+3−3 src/Language/PureScript/Parser/Declarations.hs+10−3 src/Language/PureScript/Parser/Types.hs+4−1 src/Language/PureScript/Pretty/Types.hs+9 src/Language/PureScript/Sugar/TypeClasses.hs+6−5 src/Language/PureScript/TypeChecker.hs+30−16 src/Language/PureScript/TypeChecker/Kinds.hs+16−5 src/Language/PureScript/TypeChecker/Types.hs+14−2 src/Language/PureScript/Types.hs+13
docgen/Main.hs+11−3
@@ -141,7 +141,7 @@ renderDeclaration n _ (P.ExternDeclaration _ ident _ ty) =
141141 atIndent n $ show ident ++ " :: " ++ prettyPrintType' ty
142142renderDeclaration n exps (P.DataDeclaration dtype name args ctors) = do
143143 let
144− typeApp = foldl P.TypeApp (P.TypeConstructor (P.Qualified Nothing name)) (map P.TypeVar args)
144+ typeApp = foldl P.TypeApp (P.TypeConstructor (P.Qualified Nothing name)) (map toTypeVar args)
145145 typeName = prettyPrintType' typeApp
146146 exported = filter (isDctorExported name exps . fst) ctors
147147 atIndent n $ show dtype ++ " " ++ typeName ++ (if null exported then "" else " where")
@@ -151,13 +151,17 @@ renderDeclaration n exps (P.DataDeclaration dtype name args ctors) = do
151151renderDeclaration n _ (P.ExternDataDeclaration name kind) =
152152 atIndent n $ "data " ++ P.runProperName name ++ " :: " ++ P.prettyPrintKind kind
153153renderDeclaration n _ (P.TypeSynonymDeclaration name args ty) = do
154− let typeName = P.runProperName name ++ " " ++ unwords args
154+ let
155+ typeApp = foldl P.TypeApp (P.TypeConstructor (P.Qualified Nothing name)) (map toTypeVar args)
156+ typeName = prettyPrintType' typeApp
155157 atIndent n $ "type " ++ typeName ++ " = " ++ prettyPrintType' ty
156158renderDeclaration n exps (P.TypeClassDeclaration name args implies ds) = do
157159 let impliesText = case implies of
158160 [] -> ""
159161 is -> "(" ++ intercalate ", " (map (\(pn, tys') -> show pn ++ " " ++ unwords (map P.prettyPrintTypeAtom tys')) is) ++ ") <= "
160− atIndent n $ "class " ++ impliesText ++ P.runProperName name ++ " " ++ unwords args ++ " where"
162+ classApp = foldl P.TypeApp (P.TypeConstructor (P.Qualified Nothing name)) (map toTypeVar args)
163+ className = prettyPrintType' classApp
164+ atIndent n $ "class " ++ impliesText ++ className ++ " where"
161165 mapM_ (renderDeclaration (n + 2) exps) ds
162166renderDeclaration n _ (P.TypeInstanceDeclaration name constraints className tys _) = do
163167 let constraintsText = case constraints of
@@ -168,6 +172,10 @@ renderDeclaration n exps (P.PositionedDeclaration _ d) =
168172 renderDeclaration n exps d
169173renderDeclaration _ _ _ = return ()
170174
175+toTypeVar :: (String, Maybe P.Kind) -> P.Type
176+toTypeVar (s, Nothing) = P.TypeVar s
177+toTypeVar (s, Just k) = P.KindedType (P.TypeVar s) k
178+
171179prettyPrintType' :: P.Type -> String
172180prettyPrintType' = P.prettyPrintType . P.everywhereOnTypes dePrim
173181 where
examples/passing/KindedType.pursnew file+31
@@ -0,0 +1,31 @@
1+module Main where
2+
3+type Star2Star f = f :: * -> *
4+
5+type Star t = t :: *
6+
7+test1 :: Star2Star [] String
8+test1 = ["test"]
9+
10+f :: Star (String -> String)
11+f s = s
12+
13+test2 = f "test"
14+
15+data Proxy (f :: * -> *) = Proxy
16+
17+test3 :: Proxy []
18+test3 = Proxy
19+
20+type Test (f :: * -> *) = f String
21+
22+test4 :: Test []
23+test4 = ["test"]
24+
25+class Clazz (a :: *) where
26+ def :: a
27+
28+instance clazzString :: Clazz String where
29+ def = "test"
30+
31+main = Debug.Trace.trace "Done"
src/Language/PureScript/CodeGen/Externs.hs+16−4
@@ -29,6 +29,8 @@ import Language.PureScript.TypeClassDictionaries
2929import Language.PureScript.Declarations
3030import Language.PureScript.Pretty
3131import Language.PureScript.Names
32+import Language.PureScript.Kinds
33+import Language.PureScript.Types
3234import Language.PureScript.Environment
3335
3436-- |
@@ -64,12 +66,15 @@ moduleToPs (Module moduleName ds (Just exts)) env = intercalate "\n" . execWrite
6466 let dtype = if length dctors' == 1 && isNewtypeConstructor env (Qualified (Just moduleName) $ head dctors')
6567 then "newtype"
6668 else "data"
67− tell [dtype ++ " " ++ show pn ++ " " ++ unwords args ++ (if null dctors' then "" else " = " ++ intercalate " | " (mapMaybe printDctor dctors'))]
69+ typeName = prettyPrintType $ foldl TypeApp (TypeConstructor (Qualified Nothing pn)) (map toTypeVar args)
70+ tell [dtype ++ " " ++ typeName ++ (if null dctors' then "" else " = " ++ intercalate " | " (mapMaybe printDctor dctors'))]
6871 Just (_, TypeSynonym) ->
6972 case Qualified (Just moduleName) pn `M.lookup` typeSynonyms env of
7073 Nothing -> error $ show pn ++ " has no type synonym info in exportToPs"
7174 Just (args, synTy) ->
72− tell ["type " ++ show pn ++ " " ++ unwords args ++ " = " ++ prettyPrintType synTy]
75+ let
76+ typeName = prettyPrintType $ foldl TypeApp (TypeConstructor (Qualified Nothing pn)) (map toTypeVar args)
77+ in tell ["type " ++ typeName ++ " = " ++ prettyPrintType synTy]
7378 _ -> error "Invalid input in exportToPs"
7479
7580 exportToPs (ValueRef ident) =
@@ -82,8 +87,11 @@ moduleToPs (Module moduleName ds (Just exts)) env = intercalate "\n" . execWrite
8287 case Qualified (Just moduleName) className `M.lookup` typeClasses env of
8388 Nothing -> error $ show className ++ " has no type class definition in exportToPs"
8489 Just (args, members, implies) -> do
85− let impliesString = if null implies then "" else "(" ++ intercalate ", " (map (\(pn, tys') -> show pn ++ " " ++ unwords (map prettyPrintTypeAtom tys')) implies) ++ ") <= "
86− tell ["class " ++ impliesString ++ show className ++ " " ++ unwords args ++ " where"]
90+ let impliesString = if null implies
91+ then ""
92+ else "(" ++ intercalate ", " (map (\(pn, tys') -> show pn ++ " " ++ unwords (map prettyPrintTypeAtom tys')) implies) ++ ") <= "
93+ typeName = prettyPrintType $ foldl TypeApp (TypeConstructor (Qualified Nothing className)) (map toTypeVar args)
94+ tell ["class " ++ impliesString ++ typeName ++ " where"]
8795 forM_ (filter (isValueExported . fst) members) $ \(member ,ty) ->
8896 tell [ " " ++ show member ++ " :: " ++ prettyPrintType ty ]
8997
@@ -95,6 +103,10 @@ moduleToPs (Module moduleName ds (Just exts)) env = intercalate "\n" . execWrite
95103 cs -> "(" ++ intercalate ", " (map (\(pn, tys') -> show pn ++ " " ++ unwords (map prettyPrintTypeAtom tys')) cs) ++ ") => "
96104 tell ["foreign import instance " ++ show ident ++ " :: " ++ constraintsText ++ show className ++ " " ++ unwords (map prettyPrintTypeAtom tys)]
97105
106+ toTypeVar :: (String, Maybe Kind) -> Type
107+ toTypeVar (s, Nothing) = TypeVar s
108+ toTypeVar (s, Just k) = KindedType (TypeVar s) k
109+
98110 isValueExported :: Ident -> Bool
99111 isValueExported ident = ValueRef ident `elem` exts
100112
src/Language/PureScript/Declarations.hs+3−3
@@ -138,7 +138,7 @@ data Declaration
138138 -- |
139139 -- A data type declaration (data or newtype, name, arguments, data constructors)
140140 --
141− = DataDeclaration DataDeclType ProperName [String] [(ProperName, [Type])]
141+ = DataDeclaration DataDeclType ProperName [(String, Maybe Kind)] [(ProperName, [Type])]
142142 -- |
143143 -- A minimal mutually recursive set of data type declarations
144144 --
@@ -146,7 +146,7 @@ data Declaration
146146 -- |
147147 -- A type synonym declaration (name, arguments, type)
148148 --
149− | TypeSynonymDeclaration ProperName [String] Type
149+ | TypeSynonymDeclaration ProperName [(String, Maybe Kind)] Type
150150 -- |
151151 -- A type declaration for a value (name, ty)
152152 --
@@ -183,7 +183,7 @@ data Declaration
183183 -- |
184184 -- A type class declaration (name, argument, implies, member declarations)
185185 --
186− | TypeClassDeclaration ProperName [String] [(Qualified ProperName, [Type])] [Declaration]
186+ | TypeClassDeclaration ProperName [(String, Maybe Kind)] [(Qualified ProperName, [Type])] [Declaration]
187187 -- |
188188 -- A type instance declaration (name, dependencies, class name, instance types, member
189189 -- declarations)
src/Language/PureScript/Environment.hs+3−3
@@ -45,7 +45,7 @@ data Environment = Environment {
4545 -- |
4646 -- Type synonyms currently in scope
4747 --
48− , typeSynonyms :: M.Map (Qualified ProperName) ([String], Type)
48+ , typeSynonyms :: M.Map (Qualified ProperName) ([(String, Maybe Kind)], Type)
4949 -- |
5050 -- Available type class dictionaries
5151 --
@@ -53,7 +53,7 @@ data Environment = Environment {
5353 -- |
5454 -- Type classes
5555 --
56− , typeClasses :: M.Map (Qualified ProperName) ([String], [(Ident, Type)], [(Qualified ProperName, [Type])])
56+ , typeClasses :: M.Map (Qualified ProperName) ([(String, Maybe Kind)], [(Ident, Type)], [(Qualified ProperName, [Type])])
5757 } deriving (Show)
5858
5959-- |
@@ -124,7 +124,7 @@ data TypeKind
124124 -- |
125125 -- Data type
126126 --
127− = DataType [String] [(ProperName, [Type])]
127+ = DataType [(String, Maybe Kind)] [(ProperName, [Type])]
128128 -- |
129129 -- Type synonym
130130 --
src/Language/PureScript/Parser/Declarations.hs+10−3
@@ -13,6 +13,8 @@
1313--
1414-----------------------------------------------------------------------------
1515
16+{-# LANGUAGE TupleSections #-}
17+
1618module Language.PureScript.Parser.Declarations (
1719 parseDeclaration,
1820 parseModule,
@@ -28,6 +30,7 @@ import Data.Maybe (isJust, fromMaybe)
2830import Control.Applicative
2931import Control.Arrow ((+++))
3032
33+import Language.PureScript.Kinds
3134import Language.PureScript.Parser.State
3235import Language.PureScript.Parser.Common
3336import Language.PureScript.Declarations
@@ -48,11 +51,15 @@ sourcePos = toSourcePos <$> P.getPosition
4851 where
4952 toSourcePos p = SourcePos (P.sourceName p) (P.sourceLine p) (P.sourceColumn p)
5053
54+kindedIdent :: P.Parsec String ParseState (String, Maybe Kind)
55+kindedIdent = (, Nothing) <$> identifier
56+ <|> parens ((,) <$> identifier <*> (Just <$> (indented *> lexeme (P.string "::") *> indented *> parseKind)))
57+
5158parseDataDeclaration :: P.Parsec String ParseState Declaration
5259parseDataDeclaration = do
5360 dtype <- (reserved "data" *> return Data) <|> (reserved "newtype" *> return Newtype)
5461 name <- indented *> properName
55− tyArgs <- many (indented *> identifier)
62+ tyArgs <- many (indented *> kindedIdent)
5663 ctors <- P.option [] $ do
5764 _ <- lexeme $ indented *> P.char '='
5865 sepBy1 ((,) <$> properName <*> P.many (indented *> parseTypeAtom)) pipe
@@ -66,7 +73,7 @@ parseTypeDeclaration =
6673parseTypeSynonymDeclaration :: P.Parsec String ParseState Declaration
6774parseTypeSynonymDeclaration =
6875 TypeSynonymDeclaration <$> (P.try (reserved "type") *> indented *> properName)
69− <*> many (indented *> identifier)
76+ <*> many (indented *> kindedIdent)
7077 <*> (lexeme (indented *> P.char '=') *> parsePolyType)
7178
7279parseValueDeclaration :: P.Parsec String ParseState Declaration
@@ -163,7 +170,7 @@ parseTypeClassDeclaration = do
163170 reservedOp "<="
164171 return implies
165172 className <- indented *> properName
166− idents <- P.many (indented *> identifier)
173+ idents <- P.many (indented *> kindedIdent)
167174 members <- P.option [] . P.try $ do
168175 indented *> reserved "where"
169176 mark (P.many (same *> positioned parseTypeDeclaration))
src/Language/PureScript/Parser/Types.hs+4−1
@@ -25,6 +25,7 @@ import Control.Monad (when, unless)
2525import Language.PureScript.Types
2626import Language.PureScript.Parser.State
2727import Language.PureScript.Parser.Common
28+import Language.PureScript.Parser.Kinds
2829import Language.PureScript.Environment
2930
3031import qualified Text.Parsec as P
@@ -85,10 +86,12 @@ parseConstrainedType = do
8586 return $ maybe ty (flip ConstrainedType ty) constraints
8687
8788parseAnyType :: P.Parsec String ParseState Type
88−parseAnyType = P.buildExpressionParser operators parseTypeAtom P.<?> "type"
89+parseAnyType = P.buildExpressionParser operators (buildPostfixParser postfixTable parseTypeAtom) P.<?> "type"
8990 where
9091 operators = [ [ P.Infix (return TypeApp) P.AssocLeft ]
9192 , [ P.Infix (P.try (lexeme (P.string "->")) >> return function) P.AssocRight ] ]
93+ postfixTable = [ \t -> KindedType t <$> (P.try (lexeme (indented *> P.string "::")) *> parseKind)
94+ ]
9295
9396-- |
9497-- Parse a monotype
src/Language/PureScript/Pretty/Types.hs+9
@@ -26,7 +26,9 @@ import Control.Arrow ((<+>))
2626import Control.PatternArrows
2727
2828import Language.PureScript.Types
29+import Language.PureScript.Kinds
2930import Language.PureScript.Pretty.Common
31+import Language.PureScript.Pretty.Kinds
3032import Language.PureScript.Environment
3133
3234typeLiterals :: Pattern () Type String
@@ -71,6 +73,12 @@ appliedFunction = mkPattern match
7173 match (PrettyPrintFunction arg ret) = Just (arg, ret)
7274 match _ = Nothing
7375
76+kinded :: Pattern () Type (Kind, Type)
77+kinded = mkPattern match
78+ where
79+ match (KindedType t k) = Just (k, t)
80+ match _ = Nothing
81+
7482insertPlaceholders :: Type -> Type
7583insertPlaceholders = everywhereOnTypesTopDown convertForAlls . everywhereOnTypes convert
7684 where
@@ -96,6 +104,7 @@ matchType = buildPrettyPrinter operators matchTypeAtom
96104 , [ AssocR appliedFunction $ \arg ret -> arg ++ " -> " ++ ret
97105 ]
98106 , [ Wrap forall_ $ \idents ty -> "forall " ++ unwords idents ++ ". " ++ ty ]
107+ , [ Wrap kinded $ \k ty -> ty ++ " :: " ++ prettyPrintKind k ]
99108 ]
100109
101110forall_ :: Pattern () Type ([String], Type)
src/Language/PureScript/Sugar/TypeClasses.hs+6−5
@@ -21,6 +21,7 @@ module Language.PureScript.Sugar.TypeClasses (
2121import Language.PureScript.Declarations
2222import Language.PureScript.Names
2323import Language.PureScript.Types
24+import Language.PureScript.Kinds
2425import Language.PureScript.Sugar.CaseDeclarations
2526import Language.PureScript.Environment
2627import Language.PureScript.Errors
@@ -197,7 +198,7 @@ memberToNameAndType (TypeDeclaration ident ty) = (ident, ty)
197198memberToNameAndType (PositionedDeclaration _ d) = memberToNameAndType d
198199memberToNameAndType _ = error "Invalid declaration in type class definition"
199200
200−typeClassDictionaryDeclaration :: ProperName -> [String] -> [(Qualified ProperName, [Type])] -> [Declaration] -> Declaration
201+typeClassDictionaryDeclaration :: ProperName -> [(String, Maybe Kind)] -> [(Qualified ProperName, [Type])] -> [Declaration] -> Declaration
201202typeClassDictionaryDeclaration name args implies members =
202203 let superclassTypes = [ (fieldName, function unit tySynApp)
203204 | (index, (superclass, tyArgs)) <- zip [0..] implies
@@ -208,11 +209,11 @@ typeClassDictionaryDeclaration name args implies members =
208209 mtys = members' ++ superclassTypes
209210 in TypeSynonymDeclaration name args (TypeApp tyObject $ rowFromList (mtys, REmpty))
210211
211−typeClassMemberToDictionaryAccessor :: ModuleName -> ProperName -> [String] -> Declaration -> Declaration
212+typeClassMemberToDictionaryAccessor :: ModuleName -> ProperName -> [(String, Maybe Kind)] -> Declaration -> Declaration
212213typeClassMemberToDictionaryAccessor mn name args (TypeDeclaration ident ty) =
213214 ValueDeclaration ident TypeClassAccessorImport [] Nothing $
214215 TypedValue False (Abs (Left $ Ident "dict") (Accessor (runIdent ident) (Var $ Qualified Nothing (Ident "dict")))) $
215− moveQuantifiersToFront (quantify (ConstrainedType [(Qualified (Just mn) name, map TypeVar args)] ty))
216+ moveQuantifiersToFront (quantify (ConstrainedType [(Qualified (Just mn) name, map (TypeVar . fst) args)] ty))
216217typeClassMemberToDictionaryAccessor mn name args (PositionedDeclaration pos d) =
217218 PositionedDeclaration pos $ typeClassMemberToDictionaryAccessor mn name args d
218219typeClassMemberToDictionaryAccessor _ _ _ _ = error "Invalid declaration in type class definition"
@@ -240,7 +241,7 @@ typeInstanceDictionaryDeclaration name mn deps className tys decls =
240241 let instanceTys = map memberToNameAndType tyDecls
241242
242243 -- Replace the type arguments with the appropriate types in the member types
243− let memberTypes = map (second (replaceAllTypeVars (zip args tys))) instanceTys
244+ let memberTypes = map (second (replaceAllTypeVars (zip (map fst args) tys))) instanceTys
244245 -- Create values for the type instance members
245246 memberNames <- map (first runIdent) <$> mapM (memberToNameAndValue memberTypes) decls
246247 -- Create the type of the dictionary
@@ -249,7 +250,7 @@ typeInstanceDictionaryDeclaration name mn deps className tys decls =
249250 let superclasses =
250251 [ (fieldName, Abs (Left (Ident C.__unused)) (SuperClassDictionary superclass tyArgs))
251252 | (index, (superclass, suTyArgs)) <- zip [0..] implies
252− , let tyArgs = map (replaceAllTypeVars (zip args tys)) suTyArgs
253+ , let tyArgs = map (replaceAllTypeVars (zip (map fst args) tys)) suTyArgs
253254 , let fieldName = mkSuperclassDictionaryName superclass index
254255 ]
255256
src/Language/PureScript/TypeChecker.hs+30−16
@@ -42,13 +42,13 @@ import Language.PureScript.TypeClassDictionaries
4242import Language.PureScript.Environment
4343import Language.PureScript.Errors
4444
45−addDataType :: ModuleName -> DataDeclType -> ProperName -> [String] -> [(ProperName, [Type])] -> Kind -> Check ()
45+addDataType :: ModuleName -> DataDeclType -> ProperName -> [(String, Maybe Kind)] -> [(ProperName, [Type])] -> Kind -> Check ()
4646addDataType moduleName dtype name args dctors ctorKind = do
4747 env <- getEnv
4848 putEnv $ env { types = M.insert (Qualified (Just moduleName) name) (ctorKind, DataType args dctors) (types env) }
4949 forM_ dctors $ \(dctor, tys) ->
5050 rethrow (strMsg ("Error in data constructor " ++ show dctor) <>) $
51− addDataConstructor moduleName dtype name args dctor tys
51+ addDataConstructor moduleName dtype name (map fst args) dctor tys
5252
5353addDataConstructor :: ModuleName -> DataDeclType -> ProperName -> [String] -> ProperName -> [Type] -> Check ()
5454addDataConstructor moduleName dtype name args dctor tys = do
@@ -58,7 +58,7 @@ addDataConstructor moduleName dtype name args dctor tys = do
5858 let polyType = mkForAll args dctorTy
5959 putEnv $ env { dataConstructors = M.insert (Qualified (Just moduleName) dctor) (dtype, name, polyType) (dataConstructors env) }
6060
61−addTypeSynonym :: ModuleName -> ProperName -> [String] -> Type -> Kind -> Check ()
61+addTypeSynonym :: ModuleName -> ProperName -> [(String, Maybe Kind)] -> Type -> Kind -> Check ()
6262addTypeSynonym moduleName name args ty kind = do
6363 env <- getEnv
6464 putEnv $ env { types = M.insert (Qualified (Just moduleName) name) (kind, TypeSynonym) (types env)
@@ -76,7 +76,7 @@ addValue moduleName name ty nameKind = do
7676 env <- getEnv
7777 putEnv (env { names = M.insert (moduleName, name) (ty, nameKind, Defined) (names env) })
7878
79−addTypeClass :: ModuleName -> ProperName -> [String] -> [(Qualified ProperName, [Type])] -> [Declaration] -> Check ()
79+addTypeClass :: ModuleName -> ProperName -> [(String, Maybe Kind)] -> [(Qualified ProperName, [Type])] -> [Declaration] -> Check ()
8080addTypeClass moduleName pn args implies ds =
8181 let members = map toPair ds in
8282 modify $ \st -> st { checkEnv = (checkEnv st) { typeClasses = M.insert (Qualified (Just moduleName) pn) (args, members, implies) (typeClasses . checkEnv $ st) } }
@@ -124,14 +124,15 @@ typeCheckAll mainModuleName moduleName exps = go
124124 where
125125 go :: [Declaration] -> Check [Declaration]
126126 go [] = return []
127− go (d@(DataDeclaration dtype name args dctors) : rest) = do
127+ go (DataDeclaration dtype name args dctors : rest) = do
128128 rethrow (strMsg ("Error in type constructor " ++ show name) <>) $ do
129129 when (dtype == Newtype) $ checkNewtype dctors
130− checkDuplicateTypeArguments args
130+ checkDuplicateTypeArguments $ map fst args
131131 ctorKind <- kindsOf True moduleName name args (concatMap snd dctors)
132− addDataType moduleName dtype name args dctors ctorKind
132+ let args' = args `withKinds` ctorKind
133+ addDataType moduleName dtype name args' dctors ctorKind
133134 ds <- go rest
134− return $ d : ds
135+ return $ DataDeclaration dtype name args dctors : ds
135136 where
136137 checkNewtype :: [(ProperName, [Type])] -> Check ()
137138 checkNewtype [(_, [_])] = return ()
@@ -143,11 +144,13 @@ typeCheckAll mainModuleName moduleName exps = go
143144 let dataDecls = mapMaybe toDataDecl tys
144145 (syn_ks, data_ks) <- kindsOfAll moduleName syns (map (\(_, name, args, dctors) -> (name, args, concatMap snd dctors)) dataDecls)
145146 forM_ (zip dataDecls data_ks) $ \((dtype, name, args, dctors), ctorKind) -> do
146− checkDuplicateTypeArguments args
147− addDataType moduleName dtype name args dctors ctorKind
147+ checkDuplicateTypeArguments $ map fst args
148+ let args' = args `withKinds` ctorKind
149+ addDataType moduleName dtype name args' dctors ctorKind
148150 forM_ (zip syns syn_ks) $ \((name, args, ty), kind) -> do
149− checkDuplicateTypeArguments args
150− addTypeSynonym moduleName name args ty kind
151+ checkDuplicateTypeArguments $ map fst args
152+ let args' = args `withKinds` kind
153+ addTypeSynonym moduleName name args' ty kind
151154 ds <- go rest
152155 return $ d : ds
153156 where
@@ -157,13 +160,14 @@ typeCheckAll mainModuleName moduleName exps = go
157160 toDataDecl (DataDeclaration dtype nm args dctors) = Just (dtype, nm, args, dctors)
158161 toDataDecl (PositionedDeclaration _ d') = toDataDecl d'
159162 toDataDecl _ = Nothing
160− go (d@(TypeSynonymDeclaration name args ty) : rest) = do
163+ go (TypeSynonymDeclaration name args ty : rest) = do
161164 rethrow (strMsg ("Error in type synonym " ++ show name) <>) $ do
162− checkDuplicateTypeArguments args
165+ checkDuplicateTypeArguments $ map fst args
163166 kind <- kindsOf False moduleName name args [ty]
164− addTypeSynonym moduleName name args ty kind
167+ let args' = args `withKinds` kind
168+ addTypeSynonym moduleName name args' ty kind
165169 ds <- go rest
166− return $ d : ds
170+ return $ TypeSynonymDeclaration name args ty : ds
167171 go (TypeDeclaration _ _ : _) = error "Type declarations should have been removed"
168172 go (ValueDeclaration name nameKind [] Nothing val : rest) = do
169173 d <- rethrow (strMsg ("Error in declaration " ++ show name) <>) $ do
@@ -240,6 +244,16 @@ typeCheckAll mainModuleName moduleName exps = go
240244 rethrowWithPosition pos $ do
241245 (d' : rest') <- go (d : rest)
242246 return (PositionedDeclaration pos d' : rest')
247+
248+ -- |
249+ -- This function adds the argument kinds for a type constructor so that they may appear in the externs file,
250+ -- extracted from the kind of the type constructor itself.
251+ --
252+ withKinds :: [(String, Maybe Kind)] -> Kind -> [(String, Maybe Kind)]
253+ withKinds [] _ = []
254+ withKinds (s@(_, Just _ ):ss) (FunKind _ k) = s : withKinds ss k
255+ withKinds ( (s, Nothing):ss) (FunKind k1 k2) = (s, Just k1) : withKinds ss k2
256+ withKinds _ _ = error "Invalid arguments to peelKinds"
243257
244258-- |
245259-- Type check an entire module and ensure all types and classes defined within the module that are
src/Language/PureScript/TypeChecker/Kinds.hs+16−5
@@ -80,20 +80,27 @@ kindOf _ ty =
8080-- |
8181-- Infer the kind of a type constructor with a collection of arguments and a collection of associated data constructors
8282--
83−kindsOf :: Bool -> ModuleName -> ProperName -> [String] -> [Type] -> Check Kind
83+kindsOf :: Bool -> ModuleName -> ProperName -> [(String, Maybe Kind)] -> [Type] -> Check Kind
8484kindsOf isData moduleName name args ts = fmap tidyUp . liftUnify $ do
8585 tyCon <- fresh
8686 kargs <- replicateM (length args) fresh
87− let dict = (name, tyCon) : zipWith (\arg kind -> (arg, kind)) (map ProperName args) kargs
87+ rest <- zipWithM freshKindVar args kargs
88+ let dict = (name, tyCon) : rest
8889 bindLocalTypeVariables moduleName dict $
8990 solveTypes isData ts kargs tyCon
9091 where
9192 tidyUp (k, sub) = starIfUnknown $ sub $? k
93+
94+freshKindVar :: (String, Maybe Kind) -> Kind -> UnifyT Kind Check (ProperName, Kind)
95+freshKindVar (arg, Nothing) kind = return (ProperName arg, kind)
96+freshKindVar (arg, Just kind') kind = do
97+ kind =?= kind'
98+ return (ProperName arg, kind')
9299
93100-- |
94101-- Simultaneously infer the kinds of several mutually recursive type constructors
95102--
96−kindsOfAll :: ModuleName -> [(ProperName, [String], Type)] -> [(ProperName, [String], [Type])] -> Check ([Kind], [Kind])
103+kindsOfAll :: ModuleName -> [(ProperName, [(String, Maybe Kind)], Type)] -> [(ProperName, [(String, Maybe Kind)], [Type])] -> Check ([Kind], [Kind])
97104kindsOfAll moduleName syns tys = fmap tidyUp . liftUnify $ do
98105 synVars <- replicateM (length syns) fresh
99106 let dict = zipWith (\(name, _, _) var -> (name, var)) syns synVars
@@ -103,12 +110,12 @@ kindsOfAll moduleName syns tys = fmap tidyUp . liftUnify $ do
103110 bindLocalTypeVariables moduleName dict' $ do
104111 data_ks <- zipWithM (\tyCon (_, args, ts) -> do
105112 kargs <- replicateM (length args) fresh
106− let argDict = zip (map ProperName args) kargs
113+ argDict <- zipWithM freshKindVar args kargs
107114 bindLocalTypeVariables moduleName argDict $
108115 solveTypes True ts kargs tyCon) tyCons tys
109116 syn_ks <- zipWithM (\synVar (_, args, ty) -> do
110117 kargs <- replicateM (length args) fresh
111− let argDict = zip (map ProperName args) kargs
118+ argDict <- zipWithM freshKindVar args kargs
112119 bindLocalTypeVariables moduleName argDict $
113120 solveTypes False [ty] kargs synVar) synVars syns
114121 return (syn_ks, data_ks)
@@ -179,6 +186,10 @@ infer' (ConstrainedType deps ty) = do
179186 k <- infer ty
180187 k =?= Star
181188 return Star
189+infer' (KindedType ty k) = do
190+ k' <- infer ty
191+ k =?= k'
192+ return k'
182193infer' _ = error "Invalid argument to infer"
183194
184195
src/Language/PureScript/TypeChecker/Types.hs+14−2
@@ -124,6 +124,8 @@ unifyTypes t1 t2 = rethrow (mkErrorStack ("Error unifying type " ++ prettyPrintT
124124 t3 `unifyTypes` t5
125125 t4 `unifyTypes` t6
126126 unifyTypes' (Skolem _ s1 _) (Skolem _ s2 _) | s1 == s2 = return ()
127+ unifyTypes' (KindedType ty1 _) ty2 = ty1 `unifyTypes` ty2
128+ unifyTypes' ty1 (KindedType ty2 _) = ty1 `unifyTypes` ty2
127129 unifyTypes' r1@RCons{} r2 = unifyRows r1 r2
128130 unifyTypes' r1 r2@RCons{} = unifyRows r1 r2
129131 unifyTypes' r1@REmpty r2 = unifyRows r1 r2
@@ -342,7 +344,7 @@ entails env moduleName context = solve (sortedNubBy canonicalizeDictionary (filt
342344 -- Make sure the types unify with the types in the superclass implication
343345 , subst <- maybeToList . (>>= verifySubstitution) . fmap concat $ zipWithM (typeHeadsAreEqual moduleName env) tys' suTyArgs
344346 -- Finally, satisfy the subclass constraint
345− , args' <- maybeToList $ mapM (flip lookup subst) args
347+ , args' <- maybeToList $ mapM (flip lookup subst) (map fst args)
346348 , suDict <- go True subclassName args' ]
347349
348350 -- Create dictionaries for subgoals which still need to be solved by calling go recursively
@@ -630,7 +632,7 @@ expandTypeSynonym' :: Environment -> Qualified ProperName -> [Type] -> Either St
630632expandTypeSynonym' env name args =
631633 case M.lookup name (typeSynonyms env) of
632634 Just (synArgs, body) -> do
633− let repl = replaceAllTypeVars (zip synArgs args) body
635+ let repl = replaceAllTypeVars (zip (map fst synArgs) args) body
634636 replaceAllTypeSynonyms' env repl
635637 Nothing -> error "Type synonym was not defined"
636638
@@ -1041,6 +1043,10 @@ check' (Let ds val) ty = do
10411043check' val ty | containsTypeSynonyms ty = do
10421044 ty' <- introduceSkolemScope <=< expandAllTypeSynonyms $ ty
10431045 check val ty'
1046+check' val kt@(KindedType ty kind) = do
1047+ guardWith (strMsg $ "Expected type of kind *, was " ++ prettyPrintKind kind) $ kind == Star
1048+ val' <- check' val ty
1049+ return $ TypedValue True val' kt
10441050check' (PositionedValue pos val) ty =
10451051 rethrowWithPosition pos $ check val ty
10461052check' val ty = throwError $ mkErrorStack ("Expr does not have type " ++ prettyPrintType ty) (Just (ExprError val))
@@ -1123,6 +1129,8 @@ checkFunctionApplication' fn u@(TUnknown _) arg ret = do
11231129checkFunctionApplication' fn (SaturatedTypeSynonym name tyArgs) arg ret = do
11241130 ty <- introduceSkolemScope <=< expandTypeSynonym name $ tyArgs
11251131 checkFunctionApplication fn ty arg ret
1132+checkFunctionApplication' fn (KindedType ty _) arg ret = do
1133+ checkFunctionApplication fn ty arg ret
11261134checkFunctionApplication' fn (ConstrainedType constraints fnTy) arg ret = do
11271135 dicts <- getTypeClassDictionaries
11281136 checkFunctionApplication' (foldl App fn (map (flip (TypeClassDictionary True) dicts) constraints)) fnTy arg ret
@@ -1167,6 +1175,10 @@ subsumes' val (SaturatedTypeSynonym name tyArgs) ty2 = do
11671175subsumes' val ty1 (SaturatedTypeSynonym name tyArgs) = do
11681176 ty2 <- introduceSkolemScope <=< expandTypeSynonym name $ tyArgs
11691177 subsumes val ty1 ty2
1178+subsumes' val (KindedType ty1 _) ty2 = do
1179+ subsumes val ty1 ty2
1180+subsumes' val ty1 (KindedType ty2 _) = do
1181+ subsumes val ty1 ty2
11701182subsumes' (Just val) (ConstrainedType constraints ty1) ty2 = do
11711183 dicts <- getTypeClassDictionaries
11721184 subsumes' (Just $ foldl App val (map (flip (TypeClassDictionary True) dicts) constraints)) ty1 ty2
src/Language/PureScript/Types.hs+13
@@ -26,6 +26,7 @@ import Control.Applicative
2626import Control.Monad ((<=<))
2727
2828import Language.PureScript.Names
29+import Language.PureScript.Kinds
2930import Language.PureScript.Traversals
3031
3132-- |
@@ -78,6 +79,11 @@ data Type
7879 --
7980 | RCons String Type Type
8081 -- |
82+ -- A type with a kind annotation
83+ --
84+ | KindedType Type Kind
85+ --
86+ -- |
8187 -- A placeholder used in pretty printing
8288 --
8389 | PrettyPrintFunction Type Type
@@ -153,6 +159,7 @@ replaceAllTypeVars = go []
153159 usedVars = concatMap (usedTypeVariables . snd) m
154160 go bs m (ConstrainedType cs t) = ConstrainedType (map (second $ map (go bs m)) cs) (go bs m t)
155161 go bs m (RCons name' t r) = RCons name' (go bs m t) (go bs m r)
162+ go bs m (KindedType t k) = KindedType (go bs m t) k
156163 go _ _ ty = ty
157164
158165 genName orig inUse = try 0
@@ -183,6 +190,7 @@ freeTypeVariables = nub . go []
183190 go bound (ForAll v t _) = go (v : bound) t
184191 go bound (ConstrainedType cs t) = concatMap (concatMap (go bound) . snd) cs ++ go bound t
185192 go bound (RCons _ t r) = go bound t ++ go bound r
193+ go bound (KindedType t _) = go bound t
186194 go _ _ = []
187195
188196-- |
@@ -219,6 +227,7 @@ everywhereOnTypes f = go
219227 go (ForAll arg ty sco) = f (ForAll arg (go ty) sco)
220228 go (ConstrainedType cs ty) = f (ConstrainedType (map (fmap (map go)) cs) (go ty))
221229 go (RCons name ty rest) = f (RCons name (go ty) (go rest))
230+ go (KindedType ty k) = f (KindedType (go ty) k)
222231 go (PrettyPrintFunction t1 t2) = f (PrettyPrintFunction (go t1) (go t2))
223232 go (PrettyPrintArray t) = f (PrettyPrintArray (go t))
224233 go (PrettyPrintObject t) = f (PrettyPrintObject (go t))
@@ -233,6 +242,7 @@ everywhereOnTypesTopDown f = go . f
233242 go (ForAll arg ty sco) = ForAll arg (go (f ty)) sco
234243 go (ConstrainedType cs ty) = ConstrainedType (map (fmap (map (go . f))) cs) (go (f ty))
235244 go (RCons name ty rest) = RCons name (go (f ty)) (go (f rest))
245+ go (KindedType ty k) = KindedType (go (f ty)) k
236246 go (PrettyPrintFunction t1 t2) = PrettyPrintFunction (go (f t1)) (go (f t2))
237247 go (PrettyPrintArray t) = PrettyPrintArray (go (f t))
238248 go (PrettyPrintObject t) = PrettyPrintObject (go (f t))
@@ -247,6 +257,7 @@ everywhereOnTypesM f = go
247257 go (ForAll arg ty sco) = (ForAll arg <$> go ty <*> pure sco) >>= f
248258 go (ConstrainedType cs ty) = (ConstrainedType <$> mapM (sndM (mapM go)) cs <*> go ty) >>= f
249259 go (RCons name ty rest) = (RCons name <$> go ty <*> go rest) >>= f
260+ go (KindedType ty k) = (KindedType <$> go ty <*> pure k) >>= f
250261 go (PrettyPrintFunction t1 t2) = (PrettyPrintFunction <$> go t1 <*> go t2) >>= f
251262 go (PrettyPrintArray t) = (PrettyPrintArray <$> go t) >>= f
252263 go (PrettyPrintObject t) = (PrettyPrintObject <$> go t) >>= f
@@ -261,6 +272,7 @@ everywhereOnTypesTopDownM f = go <=< f
261272 go (ForAll arg ty sco) = ForAll arg <$> (f ty >>= go) <*> pure sco
262273 go (ConstrainedType cs ty) = ConstrainedType <$> mapM (sndM (mapM (go <=< f))) cs <*> (f ty >>= go)
263274 go (RCons name ty rest) = RCons name <$> (f ty >>= go) <*> (f rest >>= go)
275+ go (KindedType ty k) = KindedType <$> (f ty >>= go) <*> pure k
264276 go (PrettyPrintFunction t1 t2) = PrettyPrintFunction <$> (f t1 >>= go) <*> (f t2 >>= go)
265277 go (PrettyPrintArray t) = PrettyPrintArray <$> (f t >>= go)
266278 go (PrettyPrintObject t) = PrettyPrintObject <$> (f t >>= go)
@@ -275,6 +287,7 @@ everythingOnTypes (<>) f = go
275287 go t@(ForAll _ ty _) = f t <> go ty
276288 go t@(ConstrainedType cs ty) = foldl (<>) (f t) (map go $ concatMap snd cs) <> go ty
277289 go t@(RCons _ ty rest) = f t <> go ty <> go rest
290+ go t@(KindedType ty _) = f t <> go ty
278291 go t@(PrettyPrintFunction t1 t2) = f t <> go t1 <> go t2
279292 go t@(PrettyPrintArray t1) = f t <> go t1
280293 go t@(PrettyPrintObject t1) = f t <> go t1