Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow getting type errors without having to kill the interpreter #48

Merged
merged 3 commits into from
Jan 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions hint.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ library
exposed-modules: Language.Haskell.Interpreter
Language.Haskell.Interpreter.Extension
Language.Haskell.Interpreter.Unsafe
Hint.Internal
other-modules: Hint.GHC
Hint.Base
Hint.InterpreterT
Expand Down
33 changes: 33 additions & 0 deletions src/Hint/Internal.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
-- | In this module we intend to export some internal functions.
--
-- __Important note__: the authors of this library imply no assurance whatsoever
-- of the stability or functionality of the API exposed here, and compatibility
-- may break even by minor version changes. Rely on these at your
-- own risk.
--
-- The reason for showing them here is to aid discoverability
-- of already written code and prevent having to reinvent the wheel from
-- scratch if said wheel is already invented.
--
-- In case you find something here especially useful, please submit
-- an issue or a pull request at https://github.com/mvdan/hint so
-- we can discuss making it part of the official public API.
--
-- Some further context can be found here:
-- https://github.com/mvdan/hint/pull/48#issuecomment-358722638



module Hint.Internal (
onCompilationError
) where

import Hint.Typecheck (onCompilationError)



-- todo: Consider refactoring like the following when
-- https://github.com/haskell/haddock/issues/563 is fixed
--
-- module Hint.Internal (module ReExport) where
-- import Hint.Typecheck as ReExport (onCompilationError)
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 @@ -35,6 +35,7 @@ module Language.Haskell.Interpreter(
-- pragmas inline in the code since GHC scarfs them up.
getModuleAnnotations, getValAnnotations,
-- ** Type inference
typeChecksWithDetails,
typeOf, typeChecks, kindOf, normalizeType,
-- ** Evaluation
interpret, as, infer, eval, runStmt,
Expand Down