@@ -39,21 +39,21 @@ import Language.PureScript.TypeClassDictionaries
3939import Language.PureScript.Environment
4040import Language.PureScript.Errors
4141
42−addDataType :: ModuleName -> ProperName -> [String] -> [(ProperName, [Type])] -> Kind -> Check ()
43−addDataType moduleName name args dctors ctorKind = do
42+addDataType :: ModuleName -> DataDeclType -> ProperName -> [String] -> [(ProperName, [Type])] -> Kind -> Check ()
43+addDataType moduleName dtype name args dctors ctorKind = do
4444 env <- getEnv
4545 putEnv $ env { types = M.insert (Qualified (Just moduleName) name) (ctorKind, DataType args dctors) (types env) }
4646 forM_ dctors $ \(dctor, tys) ->
4747 rethrow (strMsg ("Error in data constructor " ++ show dctor) <>) $
48− addDataConstructor moduleName name args dctor tys
48+ addDataConstructor moduleName dtype name args dctor tys
4949
50−addDataConstructor :: ModuleName -> ProperName -> [String] -> ProperName -> [Type] -> Check ()
51−addDataConstructor moduleName name args dctor tys = do
50+addDataConstructor :: ModuleName -> DataDeclType -> ProperName -> [String] -> ProperName -> [Type] -> Check ()
51+addDataConstructor moduleName dtype name args dctor tys = do
5252 env <- getEnv
5353 let retTy = foldl TypeApp (TypeConstructor (Qualified (Just moduleName) name)) (map TypeVar args)
5454 let dctorTy = foldr function retTy tys
5555 let polyType = mkForAll args dctorTy
56− putEnv $ env { dataConstructors = M.insert (Qualified (Just moduleName) dctor) (name, polyType) (dataConstructors env) }
56+ putEnv $ env { dataConstructors = M.insert (Qualified (Just moduleName) dctor) (dtype, name, polyType) (dataConstructors env) }
5757
5858addTypeSynonym :: ModuleName -> ProperName -> [String] -> Type -> Kind -> Check ()
5959addTypeSynonym moduleName name args ty kind = do
@@ -111,19 +111,25 @@ checkTypeClassInstance _ ty = throwError $ mkErrorStack "Type class instance hea
111111--
112112typeCheckAll :: Maybe ModuleName -> ModuleName -> [Declaration] -> Check [Declaration]
113113typeCheckAll _ _ [] = return []
114−typeCheckAll mainModuleName moduleName (d@(DataDeclaration name args dctors) : rest) = do
114+typeCheckAll mainModuleName moduleName (d@(DataDeclaration dtype name args dctors) : rest) = do
115115 rethrow (strMsg ("Error in type constructor " ++ show name) <>) $ do
116+ when (dtype == Newtype) $ checkNewtype dctors
116117 ctorKind <- kindsOf True moduleName name args (concatMap snd dctors)
117− addDataType moduleName name args dctors ctorKind
118+ addDataType moduleName dtype name args dctors ctorKind
118119 ds <- typeCheckAll mainModuleName moduleName rest
119120 return $ d : ds
121+ where
122+ checkNewtype :: [(ProperName, [Type])] -> Check ()
123+ checkNewtype [(_, [ty])] = return ()
124+ checkNewtype [(_, _)] = throwError . strMsg $ "newtypes constructors must have a single argument"
125+ checkNewtype _ = throwError . strMsg $ "newtypes must have a single constructor"
120126typeCheckAll mainModuleName moduleName (d@(DataBindingGroupDeclaration tys) : rest) = do
121127 rethrow (strMsg "Error in data binding group" <>) $ do
122128 let syns = mapMaybe toTypeSynonym tys
123129 let dataDecls = mapMaybe toDataDecl tys
124− (syn_ks, data_ks) <- kindsOfAll moduleName syns (map (\(name, args, dctors) -> (name, args, concatMap snd dctors)) dataDecls)
125− forM_ (zip dataDecls data_ks) $ \((name, args, dctors), ctorKind) ->
126− addDataType moduleName name args dctors ctorKind
130+ (syn_ks, data_ks) <- kindsOfAll moduleName syns (map (\(_, name, args, dctors) -> (name, args, concatMap snd dctors)) dataDecls)
131+ forM_ (zip dataDecls data_ks) $ \((dtype, name, args, dctors), ctorKind) ->
132+ addDataType moduleName dtype name args dctors ctorKind
127133 forM_ (zip syns syn_ks) $ \((name, args, ty), kind) ->
128134 addTypeSynonym moduleName name args ty kind
129135 ds <- typeCheckAll mainModuleName moduleName rest
@@ -132,7 +138,7 @@ typeCheckAll mainModuleName moduleName (d@(DataBindingGroupDeclaration tys) : re
132138 toTypeSynonym (TypeSynonymDeclaration nm args ty) = Just (nm, args, ty)
133139 toTypeSynonym (PositionedDeclaration _ d') = toTypeSynonym d'
134140 toTypeSynonym _ = Nothing
135− toDataDecl (DataDeclaration nm args dctors) = Just (nm, args, dctors)
141+ toDataDecl (DataDeclaration dtype nm args dctors) = Just (dtype, nm, args, dctors)
136142 toDataDecl (PositionedDeclaration _ d') = toDataDecl d'
137143 toDataDecl _ = Nothing
138144typeCheckAll mainModuleName moduleName (d@(TypeSynonymDeclaration name args ty) : rest) = do