Announcing Imp, a GHC plugin for automatically importing modules

by Taylor Fausak on

Typically in Haskell you have to import things before you can use them. This is widely considered to be a good idea. However sometimes it’s convenient to use something without importing it, as long as that thing is unambiguous. That’s what GHCi’s -fimplicit-import-qualified flag, which is enabled by default, does for you. For example the following works in GHCi even if you haven’t imported Data.Tuple:

ghci> Data.Tuple.swap ("world", "hello")
("hello","world")

I’m happy to announce Imp, which is a GHC plugin that brings this functionality to regular Haskell modules. So a module like the following will compile and behave the same as the GHCi example above:

{-# OPTIONS_GHC -fplugin=Imp #-}

main :: IO ()
main =
  print $ Data.Tuple.swap ("world", "hello")

You can also set up aliases if you want to refer to modules by shorter names. For example maybe you want Tuple to mean Data.Tuple. You can do that with the --alias=SOURCE:TARGET option:

{-# OPTIONS_GHC
  -fplugin=Imp
  -fplugin-opt=Imp:--alias=Data.Tuple:Tuple #-}

main :: IO ()
main =
  print $ Tuple.swap ("world", "hello")

That’s about it! Please check out the Imp plugin if this sounds interesting to you.