Skip to content

Commit

Permalink
Added typeChecksWithDetails, exported onCompilationError
Browse files Browse the repository at this point in the history
Exporting the latter to make it more possible for others to catch errors too
Also added a relevant note on `-fdefer-type-errors`
  • Loading branch information
Wizek committed Jan 16, 2018
1 parent f10e357 commit 8e8ab5a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Hint/Typecheck.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Hint.Typecheck (
typeOf, typeChecks, kindOf, normalizeType
typeOf, typeChecks, kindOf, normalizeType, onCompilationError, typeChecksWithDetails
) where

import Control.Monad.Catch
Expand All @@ -23,11 +23,21 @@ typeOf expr =
typeToString ty

-- | Tests if the expression type checks.
--
-- NB. Be careful if there is `-fdefer-type-errors` involved.
-- Perhaps unsurprisingly, that can falsely make @typeChecks@ and @getType@
-- return @True@ and @Right _@ respectively.
typeChecks :: MonadInterpreter m => String -> m Bool
typeChecks expr = (typeOf expr >> return True)
`catchIE`
onCompilationError (\_ -> return False)

-- | Similar to @typeChecks@, but gives more information, e.g. the type errors.
typeChecksWithDetails :: MonadInterpreter m => String -> m (Either [GhcError] String)
typeChecksWithDetails expr = (typeOf expr >>= return . Right)
`catchIE`
onCompilationError (\a -> return (Left a))

-- | Returns a string representation of the kind of the type expression.
kindOf :: MonadInterpreter m => String -> m String
kindOf type_expr =
Expand Down
1 change: 1 addition & 0 deletions src/Language/Haskell/Interpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module Language.Haskell.Interpreter(
-- pragmas inline in the code since GHC scarfs them up.
getModuleAnnotations, getValAnnotations,
-- ** Type inference
typeChecksWithDetails, onCompilationError,
typeOf, typeChecks, kindOf, normalizeType,
-- ** Evaluation
interpret, as, infer, eval, runStmt,
Expand Down

0 comments on commit 8e8ab5a

Please sign in to comment.