idea: tool to suggest adding imports

I have an idea for a tool I'd like to implement, and I'm looking for advice on the best way to do it. Ideally, I want to write an Emacs extension where, if I'm editing Haskell code and I try to use a symbol that's not defined or imported, it will try to automatically add an appropriate import for the symbol. If instance, if I have "import Data.Maybe (isNothing)" in my module, and I try to call "isJust", the extension would automatically change the import to "import Data.Maybe (isJust, isNothing)". The Emacs part is easy, but the Haskell part has me kind of lost. Basically I want to figure out how to heuristically resolve a name, using an existing set of imports as hints and constraints. The main heuristic I'd like to implement is that, if some symbols are imported from a module M, consider importing additional symbols from M. A more advanced heuristic might suggest that if a symbol is exported from a module M in a visible package P, the symbol should be imported from M. Finally, if a symbol is exported by a module in the Haskell platform, I'd like to suggest adding the relevant package as a dependency in the .cabal and/or stack.yaml file, and adding an import for it in the .hs file. Here are some implementation options I'm considering: 1. Add a ghci command to implement my heuristics directly, since ghc already understands modules, packages and import statements. 2. Load a modified version of the source file into ghci where imports like "import M (...)" are replaced with "import M", and parse the error messages about ambiguous symbols. 3. Write a separate tool that reads Haskell imports and duplicates ghc and cabal's name resolution mechanisms. 4. Write a tool that reads Haskell imports and suggests imports from a list of commonly imported symbols, ignoring which packages are actually visible. Right now the options that look best to me are 2 and 4, because the don't require me to understand or duplicate big parts of ghc, but if modifying ghc isn't actually that hard, then maybe 1 is the way to go. Option 3 might be a good way to go if there are libraries I can use to do the hard work for me. Any thoughts?

That's cool, I'd love to have that feature in my editor. Have you seen this:
https://hackage.haskell.org/package/halberd
The readme says:
---cut---
Halberd is a tool to help you add missing imports to your Haskell source
files. With it, you can write your source without imports, call Halberd,
and just paste in the import lines.
Currently, it tries to automatically choose an import if there is a single
sensible option. If it can't, it will prompt you with a simple menu. After
running, it prints the imports, which you need to copy manually. Editor
integration is planned.
---cut---
I have been thinking of exactly the same problem. That's how found this. I
have not tried it yet but this could be a starting point even if its not
working perfectly. In the best case you will have to just do the editor
integration part.
-harendra
On 18 March 2016 at 23:57, John Williams
I have an idea for a tool I'd like to implement, and I'm looking for advice on the best way to do it.
Ideally, I want to write an Emacs extension where, if I'm editing Haskell code and I try to use a symbol that's not defined or imported, it will try to automatically add an appropriate import for the symbol. If instance, if I have "import Data.Maybe (isNothing)" in my module, and I try to call "isJust", the extension would automatically change the import to "import Data.Maybe (isJust, isNothing)".
The Emacs part is easy, but the Haskell part has me kind of lost. Basically I want to figure out how to heuristically resolve a name, using an existing set of imports as hints and constraints. The main heuristic I'd like to implement is that, if some symbols are imported from a module M, consider importing additional symbols from M. A more advanced heuristic might suggest that if a symbol is exported from a module M in a visible package P, the symbol should be imported from M. Finally, if a symbol is exported by a module in the Haskell platform, I'd like to suggest adding the relevant package as a dependency in the .cabal and/or stack.yaml file, and adding an import for it in the .hs file.
Here are some implementation options I'm considering:
1. Add a ghci command to implement my heuristics directly, since ghc already understands modules, packages and import statements. 2. Load a modified version of the source file into ghci where imports like "import M (...)" are replaced with "import M", and parse the error messages about ambiguous symbols. 3. Write a separate tool that reads Haskell imports and duplicates ghc and cabal's name resolution mechanisms. 4. Write a tool that reads Haskell imports and suggests imports from a list of commonly imported symbols, ignoring which packages are actually visible.
Right now the options that look best to me are 2 and 4, because the don't require me to understand or duplicate big parts of ghc, but if modifying ghc isn't actually that hard, then maybe 1 is the way to go. Option 3 might be a good way to go if there are libraries I can use to do the hard work for me.
Any thoughts?
_______________________________________________ Glasgow-haskell-users mailing list Glasgow-haskell-users@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users

I wrote halberd a while ago. It was mostly as an example of using the
haskell-names and haskell-packages packges (combined with
haskell-src-exts) that Roman Cheplyaka was working on at the time,
combined with a useful tool that I wanted for myself. The downside is
that haskell-packages doesn't use the same package database as ghc, so
you had to compile the packages twice: once with ghc, and once with
haskell-packages. That made the task of actually integrating it with
an editor in a useful way for large projects seem pretty hairy, so I
didn't continue working on it. Later there was a big API change in
haskell-names that I never took the time to support, so it currently
doesn't support the newest versions of its dependencies.
Let me know if you (or anyone else) wants to work on it or rip out
useful parts. Perhaps I can help out a bit.
Regards,
Erik
On 18 March 2016 at 20:12, Harendra Kumar
That's cool, I'd love to have that feature in my editor. Have you seen this:
https://hackage.haskell.org/package/halberd
The readme says:
---cut---
Halberd is a tool to help you add missing imports to your Haskell source files. With it, you can write your source without imports, call Halberd, and just paste in the import lines.
Currently, it tries to automatically choose an import if there is a single sensible option. If it can't, it will prompt you with a simple menu. After running, it prints the imports, which you need to copy manually. Editor integration is planned.
---cut---
I have been thinking of exactly the same problem. That's how found this. I have not tried it yet but this could be a starting point even if its not working perfectly. In the best case you will have to just do the editor integration part.
-harendra
On 18 March 2016 at 23:57, John Williams
wrote: I have an idea for a tool I'd like to implement, and I'm looking for advice on the best way to do it.
Ideally, I want to write an Emacs extension where, if I'm editing Haskell code and I try to use a symbol that's not defined or imported, it will try to automatically add an appropriate import for the symbol. If instance, if I have "import Data.Maybe (isNothing)" in my module, and I try to call "isJust", the extension would automatically change the import to "import Data.Maybe (isJust, isNothing)".
The Emacs part is easy, but the Haskell part has me kind of lost. Basically I want to figure out how to heuristically resolve a name, using an existing set of imports as hints and constraints. The main heuristic I'd like to implement is that, if some symbols are imported from a module M, consider importing additional symbols from M. A more advanced heuristic might suggest that if a symbol is exported from a module M in a visible package P, the symbol should be imported from M. Finally, if a symbol is exported by a module in the Haskell platform, I'd like to suggest adding the relevant package as a dependency in the .cabal and/or stack.yaml file, and adding an import for it in the .hs file.
Here are some implementation options I'm considering:
1. Add a ghci command to implement my heuristics directly, since ghc already understands modules, packages and import statements. 2. Load a modified version of the source file into ghci where imports like "import M (...)" are replaced with "import M", and parse the error messages about ambiguous symbols. 3. Write a separate tool that reads Haskell imports and duplicates ghc and cabal's name resolution mechanisms. 4. Write a tool that reads Haskell imports and suggests imports from a list of commonly imported symbols, ignoring which packages are actually visible.
Right now the options that look best to me are 2 and 4, because the don't require me to understand or duplicate big parts of ghc, but if modifying ghc isn't actually that hard, then maybe 1 is the way to go. Option 3 might be a good way to go if there are libraries I can use to do the hard work for me.
Any thoughts?
_______________________________________________ Glasgow-haskell-users mailing list Glasgow-haskell-users@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users
_______________________________________________ Glasgow-haskell-users mailing list Glasgow-haskell-users@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users

Hello John, In my opinion, the big question is whether or not your Emacs extension should know how to build your Haskell project. Without this knowledge, (1) and (3) are non-starters, since you have to pass the right set of -package flags to GHC to get the process started. If you do assume you have this knowledge, then I think writing a little stub program using the GHC API (the best fit is the recent frontend plugins feature: https://downloads.haskell.org/~ghc/8.0.1-rc2/docs/html/users_guide/extending... because it will handle command line parsing for you; unfortunately, it will also limit you to GHC 8 only) is your best bet. Edward Excerpts from John Williams's message of 2016-03-18 11:27:34 -0700:
I have an idea for a tool I'd like to implement, and I'm looking for advice on the best way to do it.
Ideally, I want to write an Emacs extension where, if I'm editing Haskell code and I try to use a symbol that's not defined or imported, it will try to automatically add an appropriate import for the symbol. If instance, if I have "import Data.Maybe (isNothing)" in my module, and I try to call "isJust", the extension would automatically change the import to "import Data.Maybe (isJust, isNothing)".
The Emacs part is easy, but the Haskell part has me kind of lost. Basically I want to figure out how to heuristically resolve a name, using an existing set of imports as hints and constraints. The main heuristic I'd like to implement is that, if some symbols are imported from a module M, consider importing additional symbols from M. A more advanced heuristic might suggest that if a symbol is exported from a module M in a visible package P, the symbol should be imported from M. Finally, if a symbol is exported by a module in the Haskell platform, I'd like to suggest adding the relevant package as a dependency in the .cabal and/or stack.yaml file, and adding an import for it in the .hs file.
Here are some implementation options I'm considering:
1. Add a ghci command to implement my heuristics directly, since ghc already understands modules, packages and import statements. 2. Load a modified version of the source file into ghci where imports like "import M (...)" are replaced with "import M", and parse the error messages about ambiguous symbols. 3. Write a separate tool that reads Haskell imports and duplicates ghc and cabal's name resolution mechanisms. 4. Write a tool that reads Haskell imports and suggests imports from a list of commonly imported symbols, ignoring which packages are actually visible.
Right now the options that look best to me are 2 and 4, because the don't require me to understand or duplicate big parts of ghc, but if modifying ghc isn't actually that hard, then maybe 1 is the way to go. Option 3 might be a good way to go if there are libraries I can use to do the hard work for me.
Any thoughts?

I did this about 5 or 6 years ago for vim, and I'm so used to it I
wouldn't want to live without it. So I definitely recommend just
doing it. It was surprisingly easy to implement, it only took a few
hours, and then a day or so to refine the details.
I used haskell-src-exts for parsing and ghc-pkg for all visible
modules. By far the slowest part is haskell-src-exts. I have some
basic local preferences to resolve name clashes, and with that using
the global package list has never been much of a problem. If it were
I could use a local sandbox.
I don't have any fancy suggestion list, just pick the most likely. I
edit the import by hand if it was wrong, which is rare enough that I
don't mind.
On Fri, Mar 18, 2016 at 11:27 AM, John Williams
I have an idea for a tool I'd like to implement, and I'm looking for advice on the best way to do it.
Ideally, I want to write an Emacs extension where, if I'm editing Haskell code and I try to use a symbol that's not defined or imported, it will try to automatically add an appropriate import for the symbol. If instance, if I have "import Data.Maybe (isNothing)" in my module, and I try to call "isJust", the extension would automatically change the import to "import Data.Maybe (isJust, isNothing)".
The Emacs part is easy, but the Haskell part has me kind of lost. Basically I want to figure out how to heuristically resolve a name, using an existing set of imports as hints and constraints. The main heuristic I'd like to implement is that, if some symbols are imported from a module M, consider importing additional symbols from M. A more advanced heuristic might suggest that if a symbol is exported from a module M in a visible package P, the symbol should be imported from M. Finally, if a symbol is exported by a module in the Haskell platform, I'd like to suggest adding the relevant package as a dependency in the .cabal and/or stack.yaml file, and adding an import for it in the .hs file.
Here are some implementation options I'm considering:
1. Add a ghci command to implement my heuristics directly, since ghc already understands modules, packages and import statements. 2. Load a modified version of the source file into ghci where imports like "import M (...)" are replaced with "import M", and parse the error messages about ambiguous symbols. 3. Write a separate tool that reads Haskell imports and duplicates ghc and cabal's name resolution mechanisms. 4. Write a tool that reads Haskell imports and suggests imports from a list of commonly imported symbols, ignoring which packages are actually visible.
Right now the options that look best to me are 2 and 4, because the don't require me to understand or duplicate big parts of ghc, but if modifying ghc isn't actually that hard, then maybe 1 is the way to go. Option 3 might be a good way to go if there are libraries I can use to do the hard work for me.
Any thoughts?
_______________________________________________ Glasgow-haskell-users mailing list Glasgow-haskell-users@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/glasgow-haskell-users

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA512 On 18/03/16 23:51, Evan Laforge wrote:
I did this about 5 or 6 years ago for vim, and I'm so used to it I wouldn't want to live without it. Is your plug-in free software? Do you have a link to it? It sounds very useful to us vim users.
- -- Alexander alexander@plaimi.net https://secure.plaimi.net/~alexander -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQIcBAEBCgAGBQJW7WxmAAoJENQqWdRUGk8BkvsQAIf+1UyESa5YqdXAgPMt33E0 iyEudGMdh86R8WboYf1HmI4LFYdVikKpA8NSEEnc41PbcEsTp0qUjcQ82mcfR+Lh zHwuZkPOQmlsTIcnOMerLHi+DjgSZRuEXwurBHp7MEpPxqR8nW2eGSq3s0vc4XDv gUHhqjZXfRs19XhbzBrDiMjD/UeSWgPljfy9VHE6l4yXtw4QwcfaI/RAnl1SvI+2 POddyKemMKtbNIq9zl7H25PhxYSjC0fdgu6OGByRpOu/SvL4WP7tXNEM0Oo/y+Q8 VL6vCCbCYiyhJWPiHJIwz2RJ5EUw973owLuHtazZwQdaKHARILqcdN5lNBza9wHU +zISRZ5Qsz5VCk7AQNXmBLssTJJAVqWr1lgHpj675n7uS6WjYdVDzNm3s8pG0m2F SipjVd7HRWdiA0+Eej/Afbuiltd7NDEeAtIlVsCvFX0annotY6I0ne9ZpoL5JWMi tos+APpoaSxwyiwYdx7+IoBYg5ZRZYCevg09aLR6ehGv8a14xqgZut+LPBjgZ7aU 3PfNlUNLRz39PcJXLR0GqxV2z8f4KD7EirCFj2IPW/yYQmw44ZcvXe96v19IHkiX yn08XLab/u1Xop4cBOBh5YgHrrjqhRJ6mATsVXFYOK1o2IsOO2rbv+CnJrcXRZxc 9gFDrfsG0EvjGNkR5VHP =wtXA -----END PGP SIGNATURE-----

On Sat, Mar 19, 2016 at 8:12 AM, Alexander Berntsen
On 18/03/16 23:51, Evan Laforge wrote:
I did this about 5 or 6 years ago for vim, and I'm so used to it I wouldn't want to live without it. Is your plug-in free software? Do you have a link to it? It sounds very useful to us vim users.
The catch is that it uses qualified imports, which makes everything easy. If you're still interested, it's fix-imports on hackage. There are lots of others too, but I haven't tried them.

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA512 On 19/03/16 20:08, Evan Laforge wrote:
The catch is that it uses qualified imports, which makes everything easy. If you're still interested, it's fix-imports on hackage. There are lots of others too, but I haven't tried them. Ah. I generally want explicit imports. But I'll have a look nonetheless.
- -- Alexander alexander@plaimi.net https://secure.plaimi.net/~alexander -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQIcBAEBCgAGBQJW7qubAAoJENQqWdRUGk8B39EQAKRTt43Tv+t8iIKZnQVxnJ93 /karzj0/cxte0cAH3pVnOvk3Zqu2LHf3DhmQepR5LY4UmSoxLDUguS+g220WgQjh CdMdMzlC6u4HkhKzhGg6Cp46vya7GGMVioSJ06qKM6Y5fSfEC6e225J6MuGpDsCW 097vkCwfkPwLmz64vaU3nsGZNmrUeZV9e9zHZhKRNnylQzw51gWNOS47vYKnuDe2 uLxaoSqu5Lj925tb0U6UzFdTfXItiaARUTauN9x1ukDNGVgI2Duas6dHmv4fhRWq FdA6Xv5sR2QdcUOIW9TczHGvFyete37hdk/S1RUF1br9Qk0k7oZUJSYU8pn3riTj s1i244yzgpK/+fQ+KznD3YUb2LLrxB6Njv+911w6Ld1r71CT+sKsIr0WUqq8cE2p 99RxWqgby1iOQBb8hICY630kF0oPItS0JiZiujY0hvPPjEOmAiZ/cpJAIxhr3aom 11uLG/8rLQAIF3E5sHLBefwa83oB6NhoO1J0ZNW53q0gg3o6LY59R2ijfkrPfGB0 rfhhzXBH6bj51Nj/Xri2GFDGN1f+fC1sAUE0s7yIZ4XQ5d1ncOU8N1G55+tvw6wC /r0h4OUN1AajU2KYjgbyqTz/0Qc+juAthMgZypT5Exww7qwZdQuYbCIDjLVx/J2S UcGW3PLEGmzaIFRl5xwz =1YHj -----END PGP SIGNATURE-----

Hi John, for the Haskell source modification part I've written hsimport[1]. For finding a certain symbol in the moduls of the dependencies of a project, there's the 'findsymbol' command of a more recent hdevtools[2]. There's also vim-hsimport[3] combining the two. Greetings, Daniel [1] https://hackage.haskell.org/package/hsimport [2] https://hackage.haskell.org/package/hdevtools [3] https://github.com/dan-t/vim-hsimport
participants (7)
-
Alexander Berntsen
-
Daniel Trstenjak
-
Edward Z. Yang
-
Erik Hesselink
-
Evan Laforge
-
Harendra Kumar
-
John Williams