Where can I see how Haskell modules have been restructured? - haskell

Since the recent update of Haskell on my system, I'm getting numerous import errors. For example, I get:
Crypto/Enigma.hs:64:1: error:
Could not find module ‘Data.List.Split’
Use -v to see a list of the files searched for.
|
64 | import Data.List.Split (splitOn)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Crypto/Enigma.hs:70:1: error:
Could not find module ‘Data.String.Utils’
Use -v to see a list of the files searched for.
|
70 | import Data.String.Utils (replace)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Is there a place I can go to see what changes have been made to the package structure in the recent update? Im particular, where the functions I'm looking for have gone (to what modules)?

Related

Which package to install to run this code successfully

I am trying following simple code from here :
import Data.GI.Base
import qualified GI.Gtk as Gtk
main :: IO()
main = do
Gtk.init Nothing
Gtk.main
However, I am getting following error:
Could not find module `Data.GI.Base'
Use -v to see a list of the files searched for.
|
1 | import Data.GI.Base
| ^^^^^^^^^^^^^^^^^^^
simplewin.hs:2:1: error:
Could not find module `GI.Gtk'
Use -v to see a list of the files searched for.
|
2 | import qualified GI.Gtk as Gtk
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Following attempts to install packages do not find any matches:
>cabal list Data.GI.Base
No matches found.
>cabal list GI.Gtk
No matches found.
Which packages do I install with cabal to correct above errors?
You can search Stackage (the FPComplete package repository, the de-facto standard for Haskell) for the module, type, or function name.
For example, when I type in Data.GI.Base, I get its documentation page, which tells me (at the very top) that the module is available in the package haskell-gi-base.
Not every package is available on Stackage though. If you can't find something, try Hackage next. It's less regulated and controlled, but has more stuff because of it.

Python3 import - running local vs from pip discrepancy

I have a small pip-package (let's call it my_package) that I wrote in python3 with the following directory structure. I am confused as to a discrepancy I'm seeing in running my_package.py locally vs when I'm testing it by downloading it from PyPI, importing it into some other code, and then running it.
.
| README.md
| LICENSE
| setup.py
| build
| dist
| my_package
| -- __init__.py
| -- my_package.py
| -- helpers
| ---- __init__.py
| ---- helper1.py
| ---- helper2.py
| ---- helper3.py
| ---- helper4.py
In my_package.py I have the following imports:
from helpers import helper1
from helpers import helper2
from helpers import helper3
from helpers import helper4
Obviously these are just filler names, but the point remains that I am trying to import some code from the helpers directory from the my_package.py script.
If I were to run my_package.py locally my code executes without any issue - I think this is the expected behavior for python3. However, if I upload this to PyPI and then import the package, I receive the following error:
Traceback (most recent call last):
File "test.py", line 1, in <module>
import my_package
File "/Users/fakeUser/.virtualenvs/pip-testing/lib/python3.7/site-packages/my_package/__init__.py", line 1, in <module>
from . my_package import main_function
File "/Users/fakeUser/.virtualenvs/pip-testing/lib/python3.7/site-packages/my_package/my_package.py", line 6, in <module>
from helpers import helper1
ModuleNotFoundError: No module named 'helpers'
To resolve this issue, I modified the imports in my_package.py to look like this:
from .helpers import helper1
from .helpers import helper2
from .helpers import helper3
from .helpers import helper4
As far as I understand, python3 uses the . to help resolve relative imports. That made sense to try to me because if I am running my_package.py, adding the . should make it clear that the helpers dir is in the same directory as my_package.py. Making this modification does in fact resolve the issue for downloading the package from pip, but now introduces the following issue if I were to run this code locally:
Traceback (most recent call last):
File "my_package.py", line 6, in <module>
from .helpers import helper1
ModuleNotFoundError: No module named '__main__.helpers'; '__main__' is not a package
I am trying to understand what's going on here. In particular if someone could explain the following:
Why does adding the . make the code incompatible for local use?
Why does removing the . make the code incompatible for use from pip?
I really want to understand why these imports aren't working to avoid similar issues in the future.
To begin with, read up on modules
The start by using the following pattern
my_package
| README.md
| LICENSE
| setup.py
| build
| dist
| src
| --my_package
| ---- __init__.py
| ---- helpers
| ------__init__.py
| ------ helper1.py
| ------ helper2.py
| ------helper3.py
| ------helper4.py
You can define the top level __init__.py empty for now, and the inner __init__.py according to how your helperx.py looks like, and then when you install the module, you can call helper1 accordingly, for e.g.
from my_package.helpers import helper1
Original poster here:
The issue was caused by the fact that pip will install the package at level my_package, therefore relying on the imports to be setup either as .helpers or my_package.helpers, whereas running the script my_package.py does not install the package, and therefore the imports need to be written differently.
I will mark this is the correct answer asap (I believe that will be tomorrow)

I can t import Data.Matrix

I am trying to import Data.Matrix and I am getting this error:
Could not find module `Data.Matrix'
Perhaps you meant Data.Ratio (from base-4.12.0.0)
Use -v to see a list of the files searched for.
|
10 | import Data.Matrix
I don't know if I should install it or something like this and if I should do it, then how?

Which library is the simpleHTTP function in?

I have installed using cabal install network, HTTP, http-conduit, and http-client. When I run ghc it still throws this error:
testRun.hs:1:1: error:
Could not find module `Network.HTTP.Simple'
Perhaps you meant
Network.HTTP.Base (from HTTP-4000.3.12)
Network.HTTP.Cookie (from HTTP-4000.3.12)
Network.HTTP.Stream (from HTTP-4000.3.12)
Use -v to see a list of the files searched for.
|
1 | import Network.HTTP.Simple
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
or this error if I use Conduit:
testRun.hs:1:1: error:
Could not find module `Network.HTTP.Conduit'
Perhaps you meant Network.HTTP.Cookie (from HTTP-4000.3.12)
Use -v to see a list of the files searched for.
|
1 | import Network.HTTP.Conduit (simpleHttp)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
What do in need to install?
It's using a different approach (Stack as a script interpreter), but most likely the following command will run the script with the necessary libraries available:
stack --resolver lts-12.9 script testRun.hs

Finding explicit imports for ghc's -Wmissing-import-lists

GHC generates warnings when there are implicit imports (see below). The goal is to replace these with explicit imports. Is there an automated way to generated the list (instead of manually finding it in code)?
/Users/srid/code/slownews/backend/src/SlowNews/Main.hs:10:1: warning: [-Wmissing-import-lists]
The module ‘Control.Exception’ does not have an explicit import list
|
10 | import Control.Exception
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/Users/srid/code/slownews/backend/src/SlowNews/Main.hs:13:1: warning: [-Wmissing-import-lists]
The module ‘Control.Monad.IO.Class’ does not have an explicit import list
|
13 | import Control.Monad.IO.Class
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
GHC has a -ddump-minimal-imports flag which will do the trick.
There's an open pull request un importify tool which I'm working on. After this is done you will be able to convert implicit imports to explicit automatically:
https://github.com/serokell/importify/pull/82
The Haskell Tools project has some nice looking tooling for that. Apparently it works in Atom but I couldn't get it to work with atom or atom-beta on macOS. However, it does work at the command line. I have a simple example stack project set up. The Main.hs uses an explicit import list:
module Main where
import System.Environment
doMain = print =<< getEnvironment
main = doMain
BTW, I installed the Haskell Tools with:
$ stack install haskell-tools-daemon haskell-tools-cli fswatch
Then execute:
$ ht-refact -e 'ProjectOrganizeImports' .
Now git tells me that I have the following diff:
-import System.Environment
+import System.Environment (getEnvironment)
Check out the Haskell Tools website for an interactive example of how it should work in an editor. Looks like a very promising set of refactoring tools.

Resources