When trying to use what are called "generic" list functions in the Haskell prelude, I get an out of scope error - for instance when trying to use genericDrop, genericTake and so on.
Perhaps I need to import a module or something (but I can see no mention of that in the prelude docs or by googling)
Yes, I believe you need:
import Data.List
In general, when I need to figure out which module to import, I find that hoogle is very useful. You can search for a particular function, and it will take you to hackage, which then will have the module the function belongs to at the top of the page. For example, here is the result of searching for genericDrop and the associated hackage page.
Related
This question already has answers here:
Is there a way to see the list of functions in a module, in GHCI?
(2 answers)
Closed 5 years ago.
I know I'm asking the wrong question here, but I'm coming to Haskell from Python, and I'd like to know how to get what would be the equivalent of a list of available methods for a class. For example, if I'm using HXT to parse an XML file, and there are some functions I can use on the resulting data structure, NTree, like the function getChildren, for instance, how would I go about getting a list of them from, say, ghci? In Python I can just import a module and type module. to get a list.
In Python, you can use dir to learn about the methods and fields of an object or function. There is no analogue in Haskell. :info is occasionally useful when used on a type or constructor, but its output is spare compared to Python's dir.
In Python, you can use dir to learn about the functions, classes, and values defined by a module. In Haskell, you can use :browse in ghci to do the same.
In Python, you can use help to get some programmer-written text describing a function or other object. In Haskell, you can browse the Haddocks on Hackage to do the same. There is a tool that ostensibly brings this capability to ghci, assuming you have the appropriate documentation installed locally, but it is not well maintained, and has broken for me several times.
https://www.haskell.org/hoogle/ can help a bit. Give it a module name or a desired signature.
Classes in Haskell are unlike Python's. Python's class instances are collections of partially-applied functions (bound to self). Haskell classes are more like interfaces from Java or even Go: if something conforms to a list of function signatures, it can "belong to the class".
Functions applicable to data defined in a module are usually described in that module. But a data item can also conform to other interfaces aka typeclasses (like Foldable, Traversable, Applicative, etc), and all functions defined for these typeclasses are also applicable.
More than that, you can define a typeclass in your own module and describe something already existing to conform to it by writing an implementation of the required functions. This makes finding "all applicable functions" even more context-dependent.
Ghci on acid defines in its .gchi
:set -XNoImplicitPrelude
What is the potential benefit/reason one might have for doing so ?
There is no other way to completely avoid importing the Prelude. Even the seemingly-effective
import Prelude ()
which is an explicit import (hence overrides the implicit one) and defines no names nevertheless puts a bunch of class instances in scope that may not be desired.
Avoiding the standard prelude completely is useful when you want to play around with alternate preludes; or when you want to overload syntax using other GHC extensions; or in other niche situations. Avoiding the prelude can also be useful if you plan on using many functions that happen to be named the same as the ones in the prelude, and would like to avoid qualifying them everywhere (though the lesser import Prelude () would suffice in many such situations).
When I am studying unfamiliar Haskell code, sometimes I come across type and data constructors that are defined elsewhere. I'll try grepping my code and searching Hoogle, which sometimes leads to a quick explanation, but is oftentimes unfruitful.
For example, I spent an hour trying to identify a reference to a data type called Object in the Yesod codebase and finally discovered that it was defined in the Data.Yaml package.
Is there a way to quickly follow a symbolic reference to its definition using GHCi? Perhaps loading the same modules in the interpreter and searching the namespace for the unidentified type constructor or symbolic reference?
If you can load the code in ghci, just run :i symbol (where symbol can be a type or value), and it will tell you where it is defined, as well as where any class instances are defined.
If you can't load the code in ghci, then you might be able to use hoogle to look it up. I recommend installing the hoogle search plugin and giving it a very short keyword like "h". Then you can just type "h symbol" to get a link to it's documentation (which will also take you to the source).
I just realized that I can define my own Prelude module and carefully control its exports.
Is this considered bad practice?
Advantages:
No need to repeatedly import a "Common" module in large projects.
No need to write "import Prelude hiding (catch)".
In general its a bad idea, as you end up with code written in your own idioms that isn't going to be easy to maintain by others.
To communicate with others you need a shared language of symbols. The Prelude is our core language, so if you redefine it, expect confusion.
The exception to this rule would be when developing an embedded domain-specific language. There, making a custom Prelude is entirely a good idea, and is indeed why it is possible to redefine the Prelude (and inbuilt syntax) in the first place.
By all means have your own additional modules, but don't override the Prelude.
I'm just starting to learn Haskell and would find it very helpful to see how Haskell functions are implemented. I've been able to find the Standard Prelude on different question, but I'm now interested in Data.List. Is there any way to find the source for these functions?
I would really like to see permutations and nub (and the rest, but these are the most interesting for me right now).
Here you go: http://hackage.haskell.org/packages/archive/base/latest/doc/html/src/Data-List.html
More generally, if you look at the documentation page for Data.List you'll see "Source" links to the right of the type signatures, which will take you directly to the source for that function.
You can find the source for the rest of the standard libraries the same way and, in fact, for nearly everything on Hackage.
The documentation of the Data.List module is found here:
http://www.haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html
And the source here:
http://www.haskell.org/ghc/docs/latest/html/libraries/base/src/Data-List.html
In GHCI, you can do :browse Data.List to find more about this module. Note that the basic list definitions and operations are also found in base packages e.g. GHC.Base, GHC.List.
Other links shared did not work for me, check this out
http://hackage.haskell.org/package/base-4.12.0.0/docs/src/Data.List.html
"Source" link is to the top right of the page.
To view any function implementation click on it.