@@ -15,10 +15,10 @@
15-----------------------------------------------------------------------------
15-----------------------------------------------------------------------------
17{-# LANGUAGE FlexibleContexts #-}
17{-# LANGUAGE FlexibleContexts #-}
18+{-# LANGUAGE ScopedTypeVariables #-}
18{-# LANGUAGE CPP #-}
19{-# LANGUAGE CPP #-}
20module Language.PureScript.Sugar.TypeDeclarations (
21module Language.PureScript.Sugar.TypeDeclarations (
21− desugarTypeDeclarations,
22 desugarTypeDeclarationsModule
22 desugarTypeDeclarationsModule
@@ -27,6 +27,7 @@ import Control.Applicative
28import Control.Monad (forM)
28import Control.Monad (forM)
29import Control.Monad.Error.Class (MonadError(..))
29import Control.Monad.Error.Class (MonadError(..))
30+import Control.Monad.Writer.Class (MonadWriter(tell))
31import Language.PureScript.AST
32import Language.PureScript.AST
32import Language.PureScript.Names
33import Language.PureScript.Names
@@ -37,36 +38,38 @@ import Language.PureScript.Traversals
38-- Replace all top level type declarations in a module with type annotations
39-- Replace all top level type declarations in a module with type annotations
40−desugarTypeDeclarationsModule :: (Functor m, Applicative m, MonadError MultipleErrors m) => [Module] -> m [Module]
41+desugarTypeDeclarationsModule :: forall m. (Functor m, Applicative m, MonadError MultipleErrors m, MonadWriter MultipleErrors m) => [Module] -> m [Module]
41desugarTypeDeclarationsModule ms = forM ms $ \(Module ss coms name ds exps) ->
42desugarTypeDeclarationsModule ms = forM ms $ \(Module ss coms name ds exps) ->
42 rethrow (addHint (ErrorInModule name)) $
43 rethrow (addHint (ErrorInModule name)) $
43 Module ss coms name <$> desugarTypeDeclarations ds <*> pure exps
44 Module ss coms name <$> desugarTypeDeclarations ds <*> pure exps
46−-- Replace all top level type declarations with type annotations
48−desugarTypeDeclarations :: (Functor m, Applicative m, MonadError MultipleErrors m) => [Declaration] -> m [Declaration]
49−desugarTypeDeclarations (PositionedDeclaration pos com d : ds) = do
50− (d' : ds') <- rethrowWithPosition pos $ desugarTypeDeclarations (d : ds)
51− return (PositionedDeclaration pos com d' : ds')
52−desugarTypeDeclarations (TypeDeclaration name ty : d : rest) = do
53− (_, nameKind, val) <- fromValueDeclaration d
54− desugarTypeDeclarations (ValueDeclaration name nameKind [] (Right (TypedValue True val ty)) : rest)
47+ desugarTypeDeclarations :: [Declaration] -> m [Declaration]
48+ desugarTypeDeclarations (PositionedDeclaration pos com d : ds) = do
49+ (d' : ds') <- rethrowWithPosition pos $ desugarTypeDeclarations (d : ds)
50+ return (PositionedDeclaration pos com d' : ds')
51+ desugarTypeDeclarations (TypeDeclaration name ty : d : rest) = do
52+ (_, nameKind, val) <- fromValueDeclaration d
53+ desugarTypeDeclarations (ValueDeclaration name nameKind [] (Right (TypedValue True val ty)) : rest)
56− fromValueDeclaration :: (Functor m, Applicative m, MonadError MultipleErrors m) => Declaration -> m (Ident, NameKind, Expr)
55+ fromValueDeclaration :: Declaration -> m (Ident, NameKind, Expr)
57− fromValueDeclaration (ValueDeclaration name' nameKind [] (Right val)) | name == name' = return (name', nameKind, val)
56+ fromValueDeclaration (ValueDeclaration name' nameKind [] (Right val)) | name == name' = return (name', nameKind, val)
58− fromValueDeclaration (PositionedDeclaration pos com d') = do
57+ fromValueDeclaration (PositionedDeclaration pos com d') = do
59− (ident, nameKind, val) <- rethrowWithPosition pos $ fromValueDeclaration d'
58+ (ident, nameKind, val) <- rethrowWithPosition pos $ fromValueDeclaration d'
60− return (ident, nameKind, PositionedValue pos com val)
59+ return (ident, nameKind, PositionedValue pos com val)
61− fromValueDeclaration _ = throwError . errorMessage $ OrphanTypeDeclaration name
60+ fromValueDeclaration _ = throwError . errorMessage $ OrphanTypeDeclaration name
62−desugarTypeDeclarations [TypeDeclaration name _] = throwError . errorMessage $ OrphanTypeDeclaration name
61+ desugarTypeDeclarations [TypeDeclaration name _] = throwError . errorMessage $ OrphanTypeDeclaration name
63−desugarTypeDeclarations (ValueDeclaration name nameKind bs val : rest) = do
62+ desugarTypeDeclarations (ValueDeclaration name nameKind bs val : rest) = do
64+ Right TypedValue{} -> return ()
65+ Left _ -> error "desugarTypeDeclarations: cases were not desugared"
66+ _ -> tell (addHint (ErrorInValueDeclaration name) $ errorMessage $ MissingTypeDeclaration name)
64− let (_, f, _) = everywhereOnValuesTopDownM return go return
67+ let (_, f, _) = everywhereOnValuesTopDownM return go return
65− f' (Left gs) = Left <$> mapM (pairM return f) gs
68+ f' (Left gs) = Left <$> mapM (pairM return f) gs
66− f' (Right v) = Right <$> f v
69+ f' (Right v) = Right <$> f v
67− (:) <$> (ValueDeclaration name nameKind bs <$> f' val) <*> desugarTypeDeclarations rest
70+ (:) <$> (ValueDeclaration name nameKind bs <$> f' val) <*> desugarTypeDeclarations rest
69− go (Let ds val') = Let <$> desugarTypeDeclarations ds <*> pure val'
72+ go (Let ds val') = Let <$> desugarTypeDeclarations ds <*> pure val'
70− go other = return other
73+ go other = return other
71−desugarTypeDeclarations (d:ds) = (:) d <$> desugarTypeDeclarations ds
74+ desugarTypeDeclarations (d:ds) = (:) d <$> desugarTypeDeclarations ds
72−desugarTypeDeclarations [] = return []
75+ desugarTypeDeclarations [] = return []