from hoogle, through stack, to import

177 Views Asked by At

I am trying to understand the tools to find and install packages with useful functions in Haskell

Say I do a query in hoogle and find an interesting function: https://hoogle.haskell.org/?hoogle=a+-%3E+%5Ba%5D+-%3E+Int&scope=set%3Astackage

In the example, I am interested in the function

countElem :: Eq a => a -> [a] -> Int
MissingH Data.List.Utils

How to I find the package names to install?

I wound up guessing MissingH as a package name, and it did install (using stack install MissingH) After that, how do I find the name of the package to import?

(I ask in part because I did install MissingH, and cannot import Data.List.Utils, which is strange, because I did manage to install and import other packages before. As the current answer makes me believe I got the names right, I will latter ask another question trying to understand what is going on)

(my need is mostly with understanding how one would find out what to install and what to import in a repeatable way. The function itself is easily replaceable, of course)

1

There are 1 best solutions below

0
On BEST ANSWER

How to I find the package names to install?

That is the first item, so MissingH.

how do I find the name of the package to import?

You import the module, a package can export multiple modules, and a function can be exported by multiple modules. As we can see in Hoogle:

countElem :: Eq a => a -> [a] -> Int
MissingH Data.List.Utils

the module is thus Data.List.Utils, so we can import this with:

import Data.List.Utils(countElem)

-- …

It is possible that multiple packages export modules with the same name. To avoid ambiguity, you can make use of the PackageImports extension [ghc-doc] and specify the name of the package:

{-# LANGUAGE PackageImports #-}

import "MissingH" Data.List.Utils(countElem)

-- …