module HsDev.Tools.GhcMod.InferType (
untyped, inferType, inferTypes,
GhcModT,
infer
) where
import Control.Applicative
import Control.Lens (view, preview, set, _Just)
import Control.Monad.Except
import Data.Maybe (listToMaybe)
import Data.String (fromString)
import qualified Data.Text as T (unpack)
import qualified Language.Haskell.GhcMod as GhcMod
import HsDev.Cabal
import HsDev.Symbols
import HsDev.Tools.GhcMod
import HsDev.Util (withCurrentDirectory)
untyped :: DeclarationInfo -> Bool
untyped (Function Nothing _ _) = True
untyped _ = False
inferType :: [String] -> Cabal -> FilePath -> Declaration -> GhcModT IO Declaration
inferType opts cabal src decl'
| untyped (view declaration decl') = doInfer
| otherwise = return decl'
where
doInfer = do
inferred <- ((preview $ declaration . functionType . _Just) <$> byInfo) <|> (fmap fromString <$> byTypeOf)
return $ set (declaration . functionType) inferred decl'
byInfo = info opts cabal src (T.unpack $ view declarationName decl')
byTypeOf = case view declarationPosition decl' of
Nothing -> fail "No position"
Just (Position l c) -> (fmap typedType . listToMaybe) <$> typeOf opts cabal src l c
inferTypes :: [String] -> Cabal -> Module -> GhcModT IO Module
inferTypes opts cabal m = case view moduleLocation m of
FileModule src _ -> do
inferredDecls <- traverse (\d -> inferType opts cabal src d <|> return d) $
view moduleDeclarations m
return $ set moduleDeclarations inferredDecls m
_ -> fail "Type infer works only for source files"
infer :: [String] -> Cabal -> Module -> ExceptT String IO Module
infer opts cabal m = case view moduleLocation m of
FileModule src _ -> mapExceptT (withCurrentDirectory (sourceModuleRoot (view moduleName m) src)) $
runGhcMod GhcMod.defaultOptions $ inferTypes opts cabal m
_ -> throwError "Type infer works only for source files"