@@ -32,6 +32,7 @@ import Language.PureScript.Pretty
3232import Language.PureScript.Types
3333import Language.PureScript.Names
3434import Language.PureScript.Kinds
35+import Language.PureScript.TypeClassDictionaries
3536
3637-- |
3738-- A type of error messages
@@ -49,6 +50,7 @@ data ErrorMessage
4950 | UnknownTypeClass (Qualified ProperName)
5051 | UnknownValue (Qualified Ident)
5152 | UnknownDataConstructor (Qualified ProperName) (Maybe (Qualified ProperName))
53+ | UnknownTypeConstructor (Qualified ProperName)
5254 | ConflictingImport String ModuleName
5355 | ConflictingImports String ModuleName ModuleName
5456 | ConflictingTypeDecls ProperName
@@ -69,8 +71,23 @@ data ErrorMessage
6971 | PartiallyAppliedSynonym (Qualified ProperName)
7072 | NotYetDefined [Ident] ErrorMessage
7173 | EscapedSkolem (Maybe Expr)
74+ | UnspecifiedSkolemScope
75+ | TypesDoNotUnify Type Type
76+ | KindsDoNotUnify Kind Kind
77+ | ConstrainedTypeUnified Type Type
78+ | OverlappingInstances (Qualified ProperName) [Type] [DictionaryValue]
79+ | NoInstanceFound (Qualified ProperName) [Type]
80+ | DuplicateLabel String Expr
81+ | DuplicateValueDeclaration Ident
82+ | ArgListLengthsDiffer Ident
83+ | OverlappingArgNames Ident
84+ | MissingClassMember Ident
85+ | ErrorUnifyingTypes Type Type ErrorMessage
7286 | ErrorInExpression Expr ErrorMessage
7387 | ErrorInModule ModuleName ErrorMessage
88+ | ErrorInInstance (Qualified ProperName) [Type] ErrorMessage
89+ | ErrorInSubsumption Type Type ErrorMessage
90+ | ErrorCheckingKind Type ErrorMessage
7491 | PositionedError SourceSpan ErrorMessage
7592 deriving (Show)
7693
@@ -87,16 +104,18 @@ prettyPrintErrorMessage :: ErrorMessage -> String
87104prettyPrintErrorMessage InvalidDoBind = "Bind statement cannot be the last statement in a do block"
88105prettyPrintErrorMessage InvalidDoLet = "Let statement cannot be the last statement in a do block"
89106prettyPrintErrorMessage CannotReorderOperators = "Unable to reorder operators"
107+prettyPrintErrorMessage UnspecifiedSkolemScope = "Skolem variable scope is unspecified"
90108prettyPrintErrorMessage OverlappingNamesInLet = "Overlapping names in let binding."
91109prettyPrintErrorMessage (InfiniteType ty) = "Infinite type detected: " ++ prettyPrintType ty
92110prettyPrintErrorMessage (InfiniteKind ki) = "Infinite kind detected: " ++ prettyPrintKind ki
93111prettyPrintErrorMessage (MultipleFixities name) = "Multiple fixity declarations for " ++ show name
94112prettyPrintErrorMessage (OrphanTypeDeclaration pn) = "Orphan type declaration for: " ++ show pn
95113prettyPrintErrorMessage (RedefinedModule name) = "Module " ++ show name ++ " has been defined multiple times"
96−prettyPrintErrorMessage (UnknownModule mn) = "Unknown module: " ++ show mn
97−prettyPrintErrorMessage (UnknownType name) = "Unknown type: " ++ show name
98−prettyPrintErrorMessage (UnknownTypeClass name) = "Unknown type class: " ++ show name
99−prettyPrintErrorMessage (UnknownValue name) = "Unknown value: " ++ show name
114+prettyPrintErrorMessage (UnknownModule mn) = "Unknown module " ++ show mn
115+prettyPrintErrorMessage (UnknownType name) = "Unknown type " ++ show name
116+prettyPrintErrorMessage (UnknownTypeClass name) = "Unknown type class " ++ show name
117+prettyPrintErrorMessage (UnknownValue name) = "Unknown value " ++ show name
118+prettyPrintErrorMessage (UnknownTypeConstructor name) = "Unknown type constructor " ++ show name
100119prettyPrintErrorMessage (UnknownDataConstructor dc tc) = "Unknown data constructor " ++ show dc ++ foldMap ((" for type constructor " ++) . show) tc
101120prettyPrintErrorMessage (ConflictingImport nm mn) = "Declaration " ++ nm ++ " conflicts with import " ++ show mn
102121prettyPrintErrorMessage (ConflictingImports nm m1 m2) = "Conflicting imports for " ++ nm ++ " from modules " ++ show m1 ++ " and " ++ show m2
@@ -116,15 +135,49 @@ prettyPrintErrorMessage (NameNotInScope ident) = show ident ++ " may no
116135prettyPrintErrorMessage (UndefinedTypeVariable name) = "Type variable " ++ show name ++ " is undefined"
117136prettyPrintErrorMessage (PartiallyAppliedSynonym name) = "Partially applied type synonym " ++ show name
118137prettyPrintErrorMessage (EscapedSkolem binding) = "Rigid/skolem type variable " ++ foldMap (("bound by " ++) . prettyPrintValue) binding ++ " has escaped."
138+prettyPrintErrorMessage (TypesDoNotUnify t1 t2) = "Cannot unify " ++ prettyPrintType t1 ++ " with " ++ prettyPrintType t2
139+prettyPrintErrorMessage (KindsDoNotUnify k1 k2) = "Cannot unify " ++ prettyPrintKind k1 ++ " with " ++ prettyPrintKind k2
140+prettyPrintErrorMessage (ConstrainedTypeUnified t1 t2) = "Cannot unify constrained type " ++ prettyPrintType t1 ++ " with " ++ prettyPrintType t2
141+prettyPrintErrorMessage (OverlappingInstances nm ts ds) = unlines (("Overlapping instances found for " ++ show nm ++ " " ++ unwords (map prettyPrintType ts) ++ ":")
142+ : map prettyPrintDictionaryValue ds)
143+prettyPrintErrorMessage (NoInstanceFound nm ts) = "No instance found for " ++ show nm ++ " " ++ unwords (map prettyPrintTypeAtom ts)
144+prettyPrintErrorMessage (DuplicateLabel l expr) = "Duplicate label " ++ show l ++ " in row. Relevant expression: " ++ prettyPrintValue expr
145+prettyPrintErrorMessage (DuplicateValueDeclaration nm) = "Duplicate value declaration for " ++ show nm
146+prettyPrintErrorMessage (ArgListLengthsDiffer ident) = "Argument list lengths differ in declaration " ++ show ident
147+prettyPrintErrorMessage (OverlappingArgNames ident) = "Overlapping function argument names in declaration" ++ show ident
148+prettyPrintErrorMessage (MissingClassMember ident) = "Member " ++ show ident ++ " has not been implemented"
149+prettyPrintErrorMessage (ErrorUnifyingTypes t1 t2 err) = "Error unifying type " ++ prettyPrintType t1 ++ " with type " ++ prettyPrintType t2 ++ ":\n" ++ prettyPrintErrorMessage err
119150prettyPrintErrorMessage (ErrorInExpression expr err) = "Error in expression " ++ prettyPrintValue expr ++ ":\n" ++ prettyPrintErrorMessage err
120151prettyPrintErrorMessage (ErrorInModule mn err) = "Error in module " ++ show mn ++ ":\n" ++ prettyPrintErrorMessage err
152+prettyPrintErrorMessage (ErrorInSubsumption t1 t2 err) = "Error checking that type " ++ prettyPrintType t1 ++ " subsumes type " ++ prettyPrintType t2 ++ ":\n" ++ prettyPrintErrorMessage err
153+prettyPrintErrorMessage (ErrorInInstance name ts err) = "Error in type class instance " ++ show name ++ " " ++ unwords (map prettyPrintTypeAtom ts) ++ ":\n" ++ prettyPrintErrorMessage err
154+prettyPrintErrorMessage (ErrorCheckingKind ty err) = "Error checking kind of type " ++ prettyPrintType ty ++ ":\n" ++ prettyPrintErrorMessage err
121155prettyPrintErrorMessage (PositionedError pos err) = "Error at " ++ show pos ++ ":\n" ++ prettyPrintErrorMessage err
122156
157+-- |
158+-- Render a DictionaryValue fit for human consumption in error messages
159+--
160+prettyPrintDictionaryValue :: DictionaryValue -> String
161+prettyPrintDictionaryValue = unlines . indented 0
162+ where
163+ indented n (LocalDictionaryValue _) = [spaces n ++ "Dictionary in scope"]
164+ indented n (GlobalDictionaryValue nm) = [spaces n ++ show nm]
165+ indented n (DependentDictionaryValue nm args) = (spaces n ++ show nm ++ " via") : concatMap (indented (n + 2)) args
166+ indented n (SubclassDictionaryValue sup nm _) = (spaces n ++ show nm ++ " via superclass") : indented (n + 2) sup
167+
168+ spaces n = replicate n ' ' ++ "- "
169+
123170-- |
124171-- A stack trace for an error
125172--
126173newtype MultipleErrors = MultipleErrors
127174 { runMultipleErrors :: [ErrorMessage] } deriving (Show, Monoid)
175+
176+instance UnificationError Type MultipleErrors where
177+ occursCheckFailed = errorMessage . occursCheckFailed
178+
179+instance UnificationError Kind MultipleErrors where
180+ occursCheckFailed = errorMessage . occursCheckFailed
128181
129182-- |
130183-- Simplify an error message
@@ -134,6 +187,10 @@ simplifyErrorMessage = unwrap Nothing
134187 where
135188 unwrap :: Maybe SourceSpan -> ErrorMessage -> ErrorMessage
136189 unwrap pos (ErrorInExpression _ err) = unwrap pos err
190+ unwrap pos (ErrorInInstance name ts err) = ErrorInInstance name ts (unwrap pos err)
191+ unwrap pos (ErrorInSubsumption t1 t2 err) = ErrorInSubsumption t1 t2 (unwrap pos err)
192+ unwrap pos (ErrorUnifyingTypes _ _ err) = unwrap pos err
193+ unwrap pos (ErrorCheckingKind ty err) = ErrorCheckingKind ty (unwrap pos err)
137194 unwrap pos (ErrorInModule mn err) = ErrorInModule mn (unwrap pos err)
138195 unwrap pos (NotYetDefined ns err) = NotYetDefined ns (unwrap pos err)
139196 unwrap _ (PositionedError pos err) = unwrap (Just pos) err