How library functions in Haskell are implemented - haskell

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.

Related

Haskell prelude generic functions out of scope

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.

Haskell: quickly track down a type or data constructor definition for an external package

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).

custom Prelude module -- bad idea?

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.

Data.Lens or Control.Lens [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
lenses, fclabels, data-accessor - which library for structure access and mutation is better
I'm going to use and learn a Lens package on my next Haskell project. I had almost decided on the Data.Lens package when I found this post which mentions van Laarhoven Lenses in the Control.Lens package.
I don't really understand the differences enough yet to decide which one to use. Which package would you suggest I learn/use on a real world project?
Thanks.
lenses, fclabels, data-accessor - which library for structure access and mutation is better
Control.Lens is almost certainly what you want. Data.Lens came first, and is simpler, but Control.Lens has many advantages, and is being actively developed.
Other than lenses, Control.Lens has many related types, like traversals (a traversal is like a lens that can refer to n values instead of just one), folds, read/modify-only lenses, indexed lenses, isomorphisms... It also comes with a much larger library of useful functions and predefined lenses for standard library types, Template Haskell to derive lenses, and a bunch of code for other things like generic zippers and uniplate-style generic traversal.
It's a big library -- you don't have to use all of it, but it's nice to have the thing you want already written.
The main advantage of Data.Lens is that it's simpler, and as such doesn't require extensions beyond Haskell 98. But note that if you just want to export a Control.Lens-style lens from a library, you can do it without leaving Haskell 98 -- in fact, without depending on the package at all.
If you're dealing with a Real World Project (tm), I'd highly recommend Control.Lens. Edwardk has put a lot of recent effort into it, and I'm sure he'd love to hear about your use case. In my opinion, this is going to become the canonical Lens library. I believe it's safe to say that everything you can do with Data.Lens, you can do with Control.Lens.
Data.Lens is much simpler and easier to work with. Control.Lens has a very large number of modules and uses language extensions to get the job done.

Get module contents

The :browse, :info and :type GHCi commands are very convenient.
Is it possible to get the same information programmaticaly in a Haskell program? That is, to get the exported functions from a module, the types of stuff, etc.
:browse - when a Haskell program is compiled, no (useful) information is kept about which module something came from, so your program wouldn't be able to access that information.
:type - Unless you're using Data.Typeable, types aren't visible at all at runtime. Types in Haskell are mostly for the compiler to check to correctness/safety of code.
:info - See above.
for getting functions of a module at compile time - the language-haskell-extract package could be interesting to you. It helps you extracting functions according to a regular expression.
http://hackage.haskell.org/package/language-haskell-extract-0.2.1
Daniel Fischer commented:
You can use the GHC API. I'm not aware of a simpler way.
Seems to be fiddly but to work fine. And I guess this is how :info works in GHCi. Thanks for the suggestion.

Resources