{-# LANGUAGE FlexibleContexts #-}

-- |
-- Module      : Jikka.Common.Error
-- Description : provides a data type which represents various errors. / 種々のエラーを表現するデータ型を提供します。
-- Copyright   : (c) Kimiyuki Onaka, 2020
-- License     : Apache License 2.0
-- Maintainer  : kimiyuki95@gmail.com
-- Stability   : experimental
-- Portability : portable
module Jikka.Common.Error
  ( module Control.Monad.Except,

    -- * error data types
    Responsibility (..),
    ErrorGroup (..),
    Error (..),

    -- * general utilities for `Control.Monad.Except`
    wrapError,
    wrapError',
    wrapAt,
    wrapAt',
    maybeToError,
    eitherToError,

    -- * utilities to report multiple errors
    catchError',
    reportErrors,
    reportErrors2,
    reportErrors3,
    reportErrors4,
    reportErrors5,

    -- * function to construct errors
    lexicalError,
    lexicalErrorAt,
    syntaxError,
    syntaxErrorAt,
    symbolError,
    symbolErrorAt,
    typeError,
    semanticError,
    evaluationError,
    runtimeError,
    assertionError,
    commandLineError,
    wrongInputError,
    internalError,

    -- * actions to throw errors
    throwLexicalError,
    throwLexicalErrorAt,
    throwSyntaxError,
    throwSyntaxErrorAt,
    throwSyntaxErrorAt',
    throwSymbolError,
    throwSymbolErrorAt,
    throwSymbolErrorAt',
    throwTypeError,
    throwTypeErrorAt,
    throwTypeErrorAt',
    throwSemanticError,
    throwSemanticErrorAt,
    throwSemanticErrorAt',
    throwEvaluationError,
    throwRuntimeError,
    throwRuntimeErrorAt,
    throwRuntimeErrorAt',
    throwAssertionError,
    throwCommandLineError,
    throwWrongInputError,
    throwInternalError,
    throwInternalErrorAt,
    throwInternalErrorAt',

    -- * utilities for other types of errors
    bug,
    todo,
  )
where

import Control.Monad.Except
import Data.Either (isRight, lefts, rights)
import Jikka.Common.Location

data Responsibility
  = UserMistake
  | ImplementationBug
  deriving (Responsibility -> Responsibility -> Bool
(Responsibility -> Responsibility -> Bool)
-> (Responsibility -> Responsibility -> Bool) -> Eq Responsibility
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Responsibility -> Responsibility -> Bool
$c/= :: Responsibility -> Responsibility -> Bool
== :: Responsibility -> Responsibility -> Bool
$c== :: Responsibility -> Responsibility -> Bool
Eq, Eq Responsibility
Eq Responsibility
-> (Responsibility -> Responsibility -> Ordering)
-> (Responsibility -> Responsibility -> Bool)
-> (Responsibility -> Responsibility -> Bool)
-> (Responsibility -> Responsibility -> Bool)
-> (Responsibility -> Responsibility -> Bool)
-> (Responsibility -> Responsibility -> Responsibility)
-> (Responsibility -> Responsibility -> Responsibility)
-> Ord Responsibility
Responsibility -> Responsibility -> Bool
Responsibility -> Responsibility -> Ordering
Responsibility -> Responsibility -> Responsibility
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Responsibility -> Responsibility -> Responsibility
$cmin :: Responsibility -> Responsibility -> Responsibility
max :: Responsibility -> Responsibility -> Responsibility
$cmax :: Responsibility -> Responsibility -> Responsibility
>= :: Responsibility -> Responsibility -> Bool
$c>= :: Responsibility -> Responsibility -> Bool
> :: Responsibility -> Responsibility -> Bool
$c> :: Responsibility -> Responsibility -> Bool
<= :: Responsibility -> Responsibility -> Bool
$c<= :: Responsibility -> Responsibility -> Bool
< :: Responsibility -> Responsibility -> Bool
$c< :: Responsibility -> Responsibility -> Bool
compare :: Responsibility -> Responsibility -> Ordering
$ccompare :: Responsibility -> Responsibility -> Ordering
$cp1Ord :: Eq Responsibility
Ord, Int -> Responsibility -> ShowS
[Responsibility] -> ShowS
Responsibility -> String
(Int -> Responsibility -> ShowS)
-> (Responsibility -> String)
-> ([Responsibility] -> ShowS)
-> Show Responsibility
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Responsibility] -> ShowS
$cshowList :: [Responsibility] -> ShowS
show :: Responsibility -> String
$cshow :: Responsibility -> String
showsPrec :: Int -> Responsibility -> ShowS
$cshowsPrec :: Int -> Responsibility -> ShowS
Show, ReadPrec [Responsibility]
ReadPrec Responsibility
Int -> ReadS Responsibility
ReadS [Responsibility]
(Int -> ReadS Responsibility)
-> ReadS [Responsibility]
-> ReadPrec Responsibility
-> ReadPrec [Responsibility]
-> Read Responsibility
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Responsibility]
$creadListPrec :: ReadPrec [Responsibility]
readPrec :: ReadPrec Responsibility
$creadPrec :: ReadPrec Responsibility
readList :: ReadS [Responsibility]
$creadList :: ReadS [Responsibility]
readsPrec :: Int -> ReadS Responsibility
$creadsPrec :: Int -> ReadS Responsibility
Read)

data ErrorGroup
  = -- | It's impossible to split the given source text into tokens.
    LexicalError
  | -- | It's impossible to construct AST from tokens.
    SyntaxError
  | -- | There are undefined variables or functions in AST.
    SymbolError
  | -- | It's impossible reconstruct types for AST.
    TypeError
  | -- | other semantic erros
    SemanticError
  | -- | User's program are not ready to evaluate.
    EvaluationError
  | -- | User's program failed while running.
    RuntimeError
  | -- | User's program violates its assertion.
    AssertionError
  | -- | The given command line arguments are not acceptable.
    CommandLineError
  | -- | User's program was correctly running but wrong input text is given.
    WrongInputError
  | -- | It's an bug of implementation.
    InternalError
  deriving (ErrorGroup -> ErrorGroup -> Bool
(ErrorGroup -> ErrorGroup -> Bool)
-> (ErrorGroup -> ErrorGroup -> Bool) -> Eq ErrorGroup
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ErrorGroup -> ErrorGroup -> Bool
$c/= :: ErrorGroup -> ErrorGroup -> Bool
== :: ErrorGroup -> ErrorGroup -> Bool
$c== :: ErrorGroup -> ErrorGroup -> Bool
Eq, Eq ErrorGroup
Eq ErrorGroup
-> (ErrorGroup -> ErrorGroup -> Ordering)
-> (ErrorGroup -> ErrorGroup -> Bool)
-> (ErrorGroup -> ErrorGroup -> Bool)
-> (ErrorGroup -> ErrorGroup -> Bool)
-> (ErrorGroup -> ErrorGroup -> Bool)
-> (ErrorGroup -> ErrorGroup -> ErrorGroup)
-> (ErrorGroup -> ErrorGroup -> ErrorGroup)
-> Ord ErrorGroup
ErrorGroup -> ErrorGroup -> Bool
ErrorGroup -> ErrorGroup -> Ordering
ErrorGroup -> ErrorGroup -> ErrorGroup
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ErrorGroup -> ErrorGroup -> ErrorGroup
$cmin :: ErrorGroup -> ErrorGroup -> ErrorGroup
max :: ErrorGroup -> ErrorGroup -> ErrorGroup
$cmax :: ErrorGroup -> ErrorGroup -> ErrorGroup
>= :: ErrorGroup -> ErrorGroup -> Bool
$c>= :: ErrorGroup -> ErrorGroup -> Bool
> :: ErrorGroup -> ErrorGroup -> Bool
$c> :: ErrorGroup -> ErrorGroup -> Bool
<= :: ErrorGroup -> ErrorGroup -> Bool
$c<= :: ErrorGroup -> ErrorGroup -> Bool
< :: ErrorGroup -> ErrorGroup -> Bool
$c< :: ErrorGroup -> ErrorGroup -> Bool
compare :: ErrorGroup -> ErrorGroup -> Ordering
$ccompare :: ErrorGroup -> ErrorGroup -> Ordering
$cp1Ord :: Eq ErrorGroup
Ord, Int -> ErrorGroup -> ShowS
[ErrorGroup] -> ShowS
ErrorGroup -> String
(Int -> ErrorGroup -> ShowS)
-> (ErrorGroup -> String)
-> ([ErrorGroup] -> ShowS)
-> Show ErrorGroup
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ErrorGroup] -> ShowS
$cshowList :: [ErrorGroup] -> ShowS
show :: ErrorGroup -> String
$cshow :: ErrorGroup -> String
showsPrec :: Int -> ErrorGroup -> ShowS
$cshowsPrec :: Int -> ErrorGroup -> ShowS
Show, ReadPrec [ErrorGroup]
ReadPrec ErrorGroup
Int -> ReadS ErrorGroup
ReadS [ErrorGroup]
(Int -> ReadS ErrorGroup)
-> ReadS [ErrorGroup]
-> ReadPrec ErrorGroup
-> ReadPrec [ErrorGroup]
-> Read ErrorGroup
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [ErrorGroup]
$creadListPrec :: ReadPrec [ErrorGroup]
readPrec :: ReadPrec ErrorGroup
$creadPrec :: ReadPrec ErrorGroup
readList :: ReadS [ErrorGroup]
$creadList :: ReadS [ErrorGroup]
readsPrec :: Int -> ReadS ErrorGroup
$creadsPrec :: Int -> ReadS ErrorGroup
Read)

data Error
  = Error String
  | ErrorAppend Error Error
  | WithGroup ErrorGroup Error
  | WithWrapped String Error
  | WithLocation Loc Error
  | WithResponsibility Responsibility Error
  deriving (Error -> Error -> Bool
(Error -> Error -> Bool) -> (Error -> Error -> Bool) -> Eq Error
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Error -> Error -> Bool
$c/= :: Error -> Error -> Bool
== :: Error -> Error -> Bool
$c== :: Error -> Error -> Bool
Eq, Eq Error
Eq Error
-> (Error -> Error -> Ordering)
-> (Error -> Error -> Bool)
-> (Error -> Error -> Bool)
-> (Error -> Error -> Bool)
-> (Error -> Error -> Bool)
-> (Error -> Error -> Error)
-> (Error -> Error -> Error)
-> Ord Error
Error -> Error -> Bool
Error -> Error -> Ordering
Error -> Error -> Error
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Error -> Error -> Error
$cmin :: Error -> Error -> Error
max :: Error -> Error -> Error
$cmax :: Error -> Error -> Error
>= :: Error -> Error -> Bool
$c>= :: Error -> Error -> Bool
> :: Error -> Error -> Bool
$c> :: Error -> Error -> Bool
<= :: Error -> Error -> Bool
$c<= :: Error -> Error -> Bool
< :: Error -> Error -> Bool
$c< :: Error -> Error -> Bool
compare :: Error -> Error -> Ordering
$ccompare :: Error -> Error -> Ordering
$cp1Ord :: Eq Error
Ord, Int -> Error -> ShowS
[Error] -> ShowS
Error -> String
(Int -> Error -> ShowS)
-> (Error -> String) -> ([Error] -> ShowS) -> Show Error
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Error] -> ShowS
$cshowList :: [Error] -> ShowS
show :: Error -> String
$cshow :: Error -> String
showsPrec :: Int -> Error -> ShowS
$cshowsPrec :: Int -> Error -> ShowS
Show, ReadPrec [Error]
ReadPrec Error
Int -> ReadS Error
ReadS [Error]
(Int -> ReadS Error)
-> ReadS [Error]
-> ReadPrec Error
-> ReadPrec [Error]
-> Read Error
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Error]
$creadListPrec :: ReadPrec [Error]
readPrec :: ReadPrec Error
$creadPrec :: ReadPrec Error
readList :: ReadS [Error]
$creadList :: ReadS [Error]
readsPrec :: Int -> ReadS Error
$creadsPrec :: Int -> ReadS Error
Read)

instance Semigroup Error where
  <> :: Error -> Error -> Error
(<>) = Error -> Error -> Error
ErrorAppend

-- | The list must be non-empty.
errorList :: [Error] -> Error
errorList :: [Error] -> Error
errorList [] = String -> Error
forall a. String -> a
bug String
"The list must be non-empty."
errorList (Error
err : [Error]
errs) = (Error -> Error -> Error) -> Error -> [Error] -> Error
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl Error -> Error -> Error
ErrorAppend Error
err [Error]
errs

wrapError :: MonadError e m => (e -> e) -> m a -> m a
wrapError :: (e -> e) -> m a -> m a
wrapError e -> e
wrap m a
f = m a
f m a -> (e -> m a) -> m a
forall e (m :: * -> *) a.
MonadError e m =>
m a -> (e -> m a) -> m a
`catchError` (\e
err -> e -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (e -> e
wrap e
err))

wrapError' :: MonadError Error m => String -> m a -> m a
wrapError' :: String -> m a -> m a
wrapError' String
message m a
f = (Error -> Error) -> m a -> m a
forall e (m :: * -> *) a. MonadError e m => (e -> e) -> m a -> m a
wrapError (String -> Error -> Error
WithWrapped String
message) m a
f

wrapAt :: MonadError Error m => Loc -> m a -> m a
wrapAt :: Loc -> m a -> m a
wrapAt Loc
loc = (Error -> Error) -> m a -> m a
forall e (m :: * -> *) a. MonadError e m => (e -> e) -> m a -> m a
wrapError (Loc -> Error -> Error
WithLocation Loc
loc)

wrapAt' :: MonadError Error m => Maybe Loc -> m a -> m a
wrapAt' :: Maybe Loc -> m a -> m a
wrapAt' Maybe Loc
loc = (m a -> m a) -> (Loc -> m a -> m a) -> Maybe Loc -> m a -> m a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe m a -> m a
forall a. a -> a
id Loc -> m a -> m a
forall (m :: * -> *) a. MonadError Error m => Loc -> m a -> m a
wrapAt Maybe Loc
loc

maybeToError :: MonadError a m => a -> Maybe b -> m b
maybeToError :: a -> Maybe b -> m b
maybeToError a
a Maybe b
Nothing = a -> m b
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError a
a
maybeToError a
_ (Just b
b) = b -> m b
forall (m :: * -> *) a. Monad m => a -> m a
return b
b

eitherToError :: MonadError a m => Either a b -> m b
eitherToError :: Either a b -> m b
eitherToError = Either a b -> m b
forall e (m :: * -> *) a. MonadError e m => Either e a -> m a
liftEither

-- | `catchError'` is the inverse of `liftError`.
catchError' :: MonadError e m => m a -> m (Either e a)
catchError' :: m a -> m (Either e a)
catchError' m a
f = (a -> Either e a
forall a b. b -> Either a b
Right (a -> Either e a) -> m a -> m (Either e a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m a
f) m (Either e a) -> (e -> m (Either e a)) -> m (Either e a)
forall e (m :: * -> *) a.
MonadError e m =>
m a -> (e -> m a) -> m a
`catchError` (\e
err -> Either e a -> m (Either e a)
forall (m :: * -> *) a. Monad m => a -> m a
return (e -> Either e a
forall a b. a -> Either a b
Left e
err))

reportErrors :: MonadError Error m => [Either Error a] -> m [a]
reportErrors :: [Either Error a] -> m [a]
reportErrors [Either Error a]
xs
  | (Either Error a -> Bool) -> [Either Error a] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all Either Error a -> Bool
forall a b. Either a b -> Bool
isRight [Either Error a]
xs = [a] -> m [a]
forall (m :: * -> *) a. Monad m => a -> m a
return ([a] -> m [a]) -> [a] -> m [a]
forall a b. (a -> b) -> a -> b
$ [Either Error a] -> [a]
forall a b. [Either a b] -> [b]
rights [Either Error a]
xs
  | Bool
otherwise = Error -> m [a]
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m [a]) -> Error -> m [a]
forall a b. (a -> b) -> a -> b
$ [Error] -> Error
errorList ([Either Error a] -> [Error]
forall a b. [Either a b] -> [a]
lefts [Either Error a]
xs)

reportErrors2 :: MonadError Error m => Either Error a -> Either Error b -> m (a, b)
reportErrors2 :: Either Error a -> Either Error b -> m (a, b)
reportErrors2 (Right a
a) (Right b
b) = (a, b) -> m (a, b)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, b
b)
reportErrors2 Either Error a
a Either Error b
b = Error -> m (a, b)
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m (a, b)) -> Error -> m (a, b)
forall a b. (a -> b) -> a -> b
$ [Error] -> Error
errorList ([Either Error ()] -> [Error]
forall a b. [Either a b] -> [a]
lefts [() () -> Either Error a -> Either Error ()
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Either Error a
a, () () -> Either Error b -> Either Error ()
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Either Error b
b])

reportErrors3 :: MonadError Error m => Either Error a -> Either Error b -> Either Error c -> m (a, b, c)
reportErrors3 :: Either Error a -> Either Error b -> Either Error c -> m (a, b, c)
reportErrors3 (Right a
a) (Right b
b) (Right c
c) = (a, b, c) -> m (a, b, c)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, b
b, c
c)
reportErrors3 Either Error a
a Either Error b
b Either Error c
c = Error -> m (a, b, c)
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m (a, b, c)) -> Error -> m (a, b, c)
forall a b. (a -> b) -> a -> b
$ [Error] -> Error
errorList ([Either Error ()] -> [Error]
forall a b. [Either a b] -> [a]
lefts [() () -> Either Error a -> Either Error ()
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Either Error a
a, () () -> Either Error b -> Either Error ()
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Either Error b
b, () () -> Either Error c -> Either Error ()
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Either Error c
c])

reportErrors4 :: MonadError Error m => Either Error a -> Either Error b -> Either Error c -> Either Error d -> m (a, b, c, d)
reportErrors4 :: Either Error a
-> Either Error b
-> Either Error c
-> Either Error d
-> m (a, b, c, d)
reportErrors4 (Right a
a) (Right b
b) (Right c
c) (Right d
d) = (a, b, c, d) -> m (a, b, c, d)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, b
b, c
c, d
d)
reportErrors4 Either Error a
a Either Error b
b Either Error c
c Either Error d
d = Error -> m (a, b, c, d)
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m (a, b, c, d)) -> Error -> m (a, b, c, d)
forall a b. (a -> b) -> a -> b
$ [Error] -> Error
errorList ([Either Error ()] -> [Error]
forall a b. [Either a b] -> [a]
lefts [() () -> Either Error a -> Either Error ()
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Either Error a
a, () () -> Either Error b -> Either Error ()
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Either Error b
b, () () -> Either Error c -> Either Error ()
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Either Error c
c, () () -> Either Error d -> Either Error ()
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Either Error d
d])

reportErrors5 :: MonadError Error m => Either Error a -> Either Error b -> Either Error c -> Either Error d -> Either Error e -> m (a, b, c, d, e)
reportErrors5 :: Either Error a
-> Either Error b
-> Either Error c
-> Either Error d
-> Either Error e
-> m (a, b, c, d, e)
reportErrors5 (Right a
a) (Right b
b) (Right c
c) (Right d
d) (Right e
e) = (a, b, c, d, e) -> m (a, b, c, d, e)
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, b
b, c
c, d
d, e
e)
reportErrors5 Either Error a
a Either Error b
b Either Error c
c Either Error d
d Either Error e
e = Error -> m (a, b, c, d, e)
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m (a, b, c, d, e)) -> Error -> m (a, b, c, d, e)
forall a b. (a -> b) -> a -> b
$ [Error] -> Error
errorList ([Either Error ()] -> [Error]
forall a b. [Either a b] -> [a]
lefts [() () -> Either Error a -> Either Error ()
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Either Error a
a, () () -> Either Error b -> Either Error ()
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Either Error b
b, () () -> Either Error c -> Either Error ()
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Either Error c
c, () () -> Either Error d -> Either Error ()
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Either Error d
d, () () -> Either Error e -> Either Error ()
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Either Error e
e])

lexicalError :: String -> Error
lexicalError :: String -> Error
lexicalError = ErrorGroup -> Error -> Error
WithGroup ErrorGroup
LexicalError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

lexicalErrorAt :: Loc -> String -> Error
lexicalErrorAt :: Loc -> String -> Error
lexicalErrorAt Loc
loc = Loc -> Error -> Error
WithLocation Loc
loc (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorGroup -> Error -> Error
WithGroup ErrorGroup
LexicalError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

syntaxError :: String -> Error
syntaxError :: String -> Error
syntaxError = ErrorGroup -> Error -> Error
WithGroup ErrorGroup
SyntaxError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

syntaxErrorAt :: Loc -> String -> Error
syntaxErrorAt :: Loc -> String -> Error
syntaxErrorAt Loc
loc = Loc -> Error -> Error
WithLocation Loc
loc (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorGroup -> Error -> Error
WithGroup ErrorGroup
SyntaxError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

symbolError :: String -> Error
symbolError :: String -> Error
symbolError = ErrorGroup -> Error -> Error
WithGroup ErrorGroup
SymbolError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

symbolErrorAt :: Loc -> String -> Error
symbolErrorAt :: Loc -> String -> Error
symbolErrorAt Loc
loc = Loc -> Error -> Error
WithLocation Loc
loc (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorGroup -> Error -> Error
WithGroup ErrorGroup
SymbolError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

typeError :: String -> Error
typeError :: String -> Error
typeError = ErrorGroup -> Error -> Error
WithGroup ErrorGroup
TypeError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

semanticError :: String -> Error
semanticError :: String -> Error
semanticError = ErrorGroup -> Error -> Error
WithGroup ErrorGroup
SemanticError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

evaluationError :: String -> Error
evaluationError :: String -> Error
evaluationError = ErrorGroup -> Error -> Error
WithGroup ErrorGroup
EvaluationError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

runtimeError :: String -> Error
runtimeError :: String -> Error
runtimeError = ErrorGroup -> Error -> Error
WithGroup ErrorGroup
RuntimeError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

assertionError :: String -> Error
assertionError :: String -> Error
assertionError = ErrorGroup -> Error -> Error
WithGroup ErrorGroup
AssertionError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

commandLineError :: String -> Error
commandLineError :: String -> Error
commandLineError = ErrorGroup -> Error -> Error
WithGroup ErrorGroup
CommandLineError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

wrongInputError :: String -> Error
wrongInputError :: String -> Error
wrongInputError = ErrorGroup -> Error -> Error
WithGroup ErrorGroup
WrongInputError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

internalError :: String -> Error
internalError :: String -> Error
internalError = ErrorGroup -> Error -> Error
WithGroup ErrorGroup
InternalError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

throwLexicalError :: MonadError Error m => String -> m a
throwLexicalError :: String -> m a
throwLexicalError = Error -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m a) -> (String -> Error) -> String -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorGroup -> Error -> Error
WithGroup ErrorGroup
LexicalError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

throwLexicalErrorAt :: MonadError Error m => Loc -> String -> m a
throwLexicalErrorAt :: Loc -> String -> m a
throwLexicalErrorAt Loc
loc = Error -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m a) -> (String -> Error) -> String -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Loc -> Error -> Error
WithLocation Loc
loc (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorGroup -> Error -> Error
WithGroup ErrorGroup
LexicalError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

throwSyntaxError :: MonadError Error m => String -> m a
throwSyntaxError :: String -> m a
throwSyntaxError = Error -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m a) -> (String -> Error) -> String -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorGroup -> Error -> Error
WithGroup ErrorGroup
SyntaxError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

throwSyntaxErrorAt :: MonadError Error m => Loc -> String -> m a
throwSyntaxErrorAt :: Loc -> String -> m a
throwSyntaxErrorAt Loc
loc = Error -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m a) -> (String -> Error) -> String -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Loc -> Error -> Error
WithLocation Loc
loc (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorGroup -> Error -> Error
WithGroup ErrorGroup
SyntaxError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

throwSyntaxErrorAt' :: MonadError Error m => Maybe Loc -> String -> m a
throwSyntaxErrorAt' :: Maybe Loc -> String -> m a
throwSyntaxErrorAt' Maybe Loc
loc = Error -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m a) -> (String -> Error) -> String -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Error -> Error)
-> (Loc -> Error -> Error) -> Maybe Loc -> Error -> Error
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Error -> Error
forall a. a -> a
id Loc -> Error -> Error
WithLocation Maybe Loc
loc (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorGroup -> Error -> Error
WithGroup ErrorGroup
SyntaxError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

throwSymbolError :: MonadError Error m => String -> m a
throwSymbolError :: String -> m a
throwSymbolError = Error -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m a) -> (String -> Error) -> String -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorGroup -> Error -> Error
WithGroup ErrorGroup
SymbolError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

throwSymbolErrorAt :: MonadError Error m => Loc -> String -> m a
throwSymbolErrorAt :: Loc -> String -> m a
throwSymbolErrorAt Loc
loc = Error -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m a) -> (String -> Error) -> String -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Loc -> Error -> Error
WithLocation Loc
loc (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorGroup -> Error -> Error
WithGroup ErrorGroup
SymbolError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

throwSymbolErrorAt' :: MonadError Error m => Maybe Loc -> String -> m a
throwSymbolErrorAt' :: Maybe Loc -> String -> m a
throwSymbolErrorAt' Maybe Loc
loc = Error -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m a) -> (String -> Error) -> String -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Error -> Error)
-> (Loc -> Error -> Error) -> Maybe Loc -> Error -> Error
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Error -> Error
forall a. a -> a
id Loc -> Error -> Error
WithLocation Maybe Loc
loc (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorGroup -> Error -> Error
WithGroup ErrorGroup
SymbolError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

throwTypeError :: MonadError Error m => String -> m a
throwTypeError :: String -> m a
throwTypeError = Error -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m a) -> (String -> Error) -> String -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorGroup -> Error -> Error
WithGroup ErrorGroup
TypeError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

throwTypeErrorAt :: MonadError Error m => Loc -> String -> m a
throwTypeErrorAt :: Loc -> String -> m a
throwTypeErrorAt Loc
loc = Error -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m a) -> (String -> Error) -> String -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Loc -> Error -> Error
WithLocation Loc
loc (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorGroup -> Error -> Error
WithGroup ErrorGroup
TypeError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

throwTypeErrorAt' :: MonadError Error m => Maybe Loc -> String -> m a
throwTypeErrorAt' :: Maybe Loc -> String -> m a
throwTypeErrorAt' Maybe Loc
loc = Error -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m a) -> (String -> Error) -> String -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Error -> Error)
-> (Loc -> Error -> Error) -> Maybe Loc -> Error -> Error
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Error -> Error
forall a. a -> a
id Loc -> Error -> Error
WithLocation Maybe Loc
loc (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorGroup -> Error -> Error
WithGroup ErrorGroup
TypeError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

throwSemanticError :: MonadError Error m => String -> m a
throwSemanticError :: String -> m a
throwSemanticError = Error -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m a) -> (String -> Error) -> String -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorGroup -> Error -> Error
WithGroup ErrorGroup
SemanticError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

throwSemanticErrorAt :: MonadError Error m => Loc -> String -> m a
throwSemanticErrorAt :: Loc -> String -> m a
throwSemanticErrorAt Loc
loc = Error -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m a) -> (String -> Error) -> String -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Loc -> Error -> Error
WithLocation Loc
loc (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorGroup -> Error -> Error
WithGroup ErrorGroup
SemanticError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

throwSemanticErrorAt' :: MonadError Error m => Maybe Loc -> String -> m a
throwSemanticErrorAt' :: Maybe Loc -> String -> m a
throwSemanticErrorAt' Maybe Loc
loc = Error -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m a) -> (String -> Error) -> String -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Error -> Error)
-> (Loc -> Error -> Error) -> Maybe Loc -> Error -> Error
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Error -> Error
forall a. a -> a
id Loc -> Error -> Error
WithLocation Maybe Loc
loc (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorGroup -> Error -> Error
WithGroup ErrorGroup
SemanticError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

throwEvaluationError :: MonadError Error m => String -> m a
throwEvaluationError :: String -> m a
throwEvaluationError = Error -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m a) -> (String -> Error) -> String -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorGroup -> Error -> Error
WithGroup ErrorGroup
EvaluationError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

throwRuntimeError :: MonadError Error m => String -> m a
throwRuntimeError :: String -> m a
throwRuntimeError = Error -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m a) -> (String -> Error) -> String -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorGroup -> Error -> Error
WithGroup ErrorGroup
RuntimeError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

throwRuntimeErrorAt :: MonadError Error m => Loc -> String -> m a
throwRuntimeErrorAt :: Loc -> String -> m a
throwRuntimeErrorAt Loc
loc = Error -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m a) -> (String -> Error) -> String -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Loc -> Error -> Error
WithLocation Loc
loc (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorGroup -> Error -> Error
WithGroup ErrorGroup
RuntimeError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

throwRuntimeErrorAt' :: MonadError Error m => Maybe Loc -> String -> m a
throwRuntimeErrorAt' :: Maybe Loc -> String -> m a
throwRuntimeErrorAt' Maybe Loc
loc = Error -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m a) -> (String -> Error) -> String -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Error -> Error)
-> (Loc -> Error -> Error) -> Maybe Loc -> Error -> Error
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Error -> Error
forall a. a -> a
id Loc -> Error -> Error
WithLocation Maybe Loc
loc (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorGroup -> Error -> Error
WithGroup ErrorGroup
RuntimeError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

throwAssertionError :: MonadError Error m => String -> m a
throwAssertionError :: String -> m a
throwAssertionError = Error -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m a) -> (String -> Error) -> String -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorGroup -> Error -> Error
WithGroup ErrorGroup
AssertionError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

throwCommandLineError :: MonadError Error m => String -> m a
throwCommandLineError :: String -> m a
throwCommandLineError = Error -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m a) -> (String -> Error) -> String -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorGroup -> Error -> Error
WithGroup ErrorGroup
CommandLineError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

throwWrongInputError :: MonadError Error m => String -> m a
throwWrongInputError :: String -> m a
throwWrongInputError = Error -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m a) -> (String -> Error) -> String -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorGroup -> Error -> Error
WithGroup ErrorGroup
WrongInputError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

throwInternalError :: MonadError Error m => String -> m a
throwInternalError :: String -> m a
throwInternalError = Error -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m a) -> (String -> Error) -> String -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorGroup -> Error -> Error
WithGroup ErrorGroup
InternalError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

throwInternalErrorAt :: MonadError Error m => Loc -> String -> m a
throwInternalErrorAt :: Loc -> String -> m a
throwInternalErrorAt Loc
loc = Error -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m a) -> (String -> Error) -> String -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Loc -> Error -> Error
WithLocation Loc
loc (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorGroup -> Error -> Error
WithGroup ErrorGroup
InternalError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

throwInternalErrorAt' :: MonadError Error m => Maybe Loc -> String -> m a
throwInternalErrorAt' :: Maybe Loc -> String -> m a
throwInternalErrorAt' Maybe Loc
loc = Error -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Error -> m a) -> (String -> Error) -> String -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Error -> Error)
-> (Loc -> Error -> Error) -> Maybe Loc -> Error -> Error
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Error -> Error
forall a. a -> a
id Loc -> Error -> Error
WithLocation Maybe Loc
loc (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorGroup -> Error -> Error
WithGroup ErrorGroup
InternalError (Error -> Error) -> (String -> Error) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Error
Error

bug :: String -> a
bug :: String -> a
bug String
msg = String -> a
forall a. HasCallStack => String -> a
error (String -> a) -> String -> a
forall a b. (a -> b) -> a -> b
$ String
"Fatal Error (implementation's bug): " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
msg

todo :: String -> a
todo :: String -> a
todo String
msg = String -> a
forall a. HasCallStack => String -> a
error (String -> a) -> String -> a
forall a b. (a -> b) -> a -> b
$ String
"TODO Error (the feature is not implemented yet): " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
msg