diff --git a/hint.cabal b/hint.cabal index b7365c8..eb5dcd1 100644 --- a/hint.cabal +++ b/hint.cabal @@ -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 diff --git a/src/Hint/Internal.hs b/src/Hint/Internal.hs new file mode 100644 index 0000000..2accb81 --- /dev/null +++ b/src/Hint/Internal.hs @@ -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) diff --git a/src/Hint/Typecheck.hs b/src/Hint/Typecheck.hs index 1b6988f..c4dc053 100644 --- a/src/Hint/Typecheck.hs +++ b/src/Hint/Typecheck.hs @@ -1,5 +1,5 @@ module Hint.Typecheck ( - typeOf, typeChecks, kindOf, normalizeType + typeOf, typeChecks, kindOf, normalizeType, onCompilationError, typeChecksWithDetails ) where import Control.Monad.Catch @@ -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 = diff --git a/src/Language/Haskell/Interpreter.hs b/src/Language/Haskell/Interpreter.hs index dc252d6..286f65a 100644 --- a/src/Language/Haskell/Interpreter.hs +++ b/src/Language/Haskell/Interpreter.hs @@ -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,