How to use / where to find Haskell API? - haskell

Is this the documentation I should use for learning about the various haskell functions: https://www.haskell.org/platform/doc/2013.2.0.0/ghc-api/GHC.html ?
Can search from here or other Haskell doc for how each type should be used? For example if I wanted to learn more about the Int type (without tying :t on command line) can this be searched upon ?
If above is the API it seems much more minimalist that say a Java/Scala API. But perhaps this is one of the strong points of Haskell provide a succinct, yet very powerful set of base functions to build my abstractions upon ?

ghc-api is the API for interacting with the GHC compiler. The "standard library" is documented at http://hackage.haskell.org/package/base.

Can search from here or other Haskell doc for how each type should be
used?
In Haskel you usually think functions first, i.e. you may want to know what functions are available, what input they accept and what output they produce. This is typically expressed by class constraints ("must be a list of something") and sometimes as concrete types.
If you want to find particular functions, then try Hoogle. If you enter Prelude into the search field, you will find module Prelude among the answers. You may see the Prelude as the "Haskell API", i.e. the functions which are always available.
Then there is the base package, which consists of a number of additional modules (as already pointed out by Rein Henrichs), which are also widely used.
Finally there is all the rest, i.e. special purpose modules. Many of the can also be found on Hoogle.
But frankly, I don't think that "learning the API" is a good way to approach Haskell. This may work in Java, where you're dealing with Classes, Object and Methods all the time.
In Haskell you are at a much higher level of abstaction. In Haskell you may find ways to implement Classes, Objects and Methods as one example showcase for a certain abstraction. However, it would not be abvious from reading the API that you can simulate OOP with it.

Hoogle is a Haskell API search engine, which allows you to search many standard Haskell libraries by either function name, or by approximate type signature. The Hoogle manual contains more details, including further details on search queries, how to install Hoogle as a command line application and how to integrate Hoogle with Firefox/Emacs/Vim etc.
Thanks,
http://www.thecheesyanimation.com/3d-home-interior-design.html

Related

How to get a list of functions defined in a Haskell source file (for semantic analysis)?

I've found https://github.com/github/semantic however this does not yet seem to support Haskell source code.
I highly suspect such a library exists, however it's quite difficult to get relevant results from a web search.
What library supports this, ideally something fairly simple?
I suppose the defacto parser for Haskell in Haskell is GHC itself, but I'd assume interfacing with this would be difficult.

How can I interrogate the current GHCi environment?

I want to produce revised, fresh versions of this diagram, which appears in the Haskell98 standard:
In 2019 I will do this by generating a description of the graph I want, and feeding it to Graphviz. A proof-of-concept implementation I did yesterday, told only that there is something called Monad, can automatically produce this diagram:
The proof-of-concept program is rather awful, because the way it traverses the graph is by sending :info commands to GHCi and attempting to parse the output. I am not interested in pushing this approach any further.
The right way to do this is to figure out how GHC represents the class and instance information internally, then use its API to interrogate those data structures directly.
I have spent quite some time looking around in the GHC API documents but I have not found the entry points I need.
I think I want to ask GHC for a list of the names of all the typeclass and instance information currently in scope, and for a description of the constraints for each one.
What are good ways to proceed with this? Where should I be looking?
Thanks.
I'd start by looking at template Haskell for this. The various reify functions let you extract information from the compiler environment. I don't have a proof of concept around for this, but it looks like it should be doable.

Creating libraries from machine readable specifications in Haskell

I have a specification and I wish to transform it into a library. I can write a program that writes out Haskel source. However is there a cleaner way that would allow me to compile the specification directly (perhaps using templates)?
References to manuals and tutorials would be greatly appropriated.
Yes, you can use Template Haskell. The are a couple of approaches to using it.
One approach is to use quasiquotation to embed (parts of) the text of the specification in a quasiquotation within a source file. To implement it, you need to write a parser of the machine specification that outputs Haskell AST. This might be useful if the specification is relatively static, it makes sense to have subsets of the specification, or you want to manually map parts of the specification to different modules. This may also be useful, in addition to a different approach perhaps, to provide tools for users of the library to express things in terms of the specification.
Another approach is to execute IO in a normal Template Haskell splice. This would allow you to read the specification from a file (see addDependentFile too in this case), the network (don't do this), or to execute an arbitrary program to produce the Haskell AST needed. This might be more useful if the specification changes more often, or you want to keep a strict separation between the specification and code.
If it's much easier to produce Haskell source than Haskell AST, you can use a library like haskell-src-meta which will parse a string into Template Haskell AST.

options library like Google GFlags for Haskell

I'm interested in having something very similar to Google's flags library for Haskell.
Here is the small introduction to gflags that demonstrates why I love it: http://gflags.googlecode.com/svn/trunk/doc/gflags.html
I looked into the various getopt like libraries on Hackage and I haven't found one that matches the simplicity and flexibility of gflags.
Namely, I'd like to have these features:
generates --help (with default values mentioned in the help),
besides parsing the options given by the user, it should also err on unmatched options, so the user has a chance to note typos,
flags can be declared in any module easily (hopefully at the top-level, Template Haskell hackery acceptable if needed),
no need in main to call out to all the modules where I declared flags, instead the flags somehow register themselves at startup/linking/whatever time,
it's OK if main has to call a general initialization function, like in gflags' google::ParseCommandLineFlags(&argc, &argv, true);
flags can be used purely (yeah, I think this is an appropriate usage of unsafePerformIO to make the API more simple).
After looking around without success, I played with the idea of doing this myself (and of course sharing it on Hackage). However, I have absolutely no idea for the implementation of the registration part. I need something similar to GCC's ((constructor)) attribute or to C++'s static initialization, but in Haskell. Standard top-level unsafePerformIO is not enough, because that is lazy, so it won't be called before main starts to run.
After investigating all the solutions on Hackage (thanks for all the tips!), I went ahead with Don's typeclass implementation idea and created a library named HFlags.
It's on hackage: http://hackage.haskell.org/package/hflags
I also have a blog post, describing it: http://blog.risko.hu/2012/04/ann-hflags-0.html
You might like CmdArgs, though I haven't used it enough to tell if it satisfies all your constraints.

Is there any working implementation of reverse mode automatic differentiation for Haskell?

The closest-related implementation in Haskell I have seen is the forward mode at http://hackage.haskell.org/packages/archive/fad/1.0/doc/html/Numeric-FAD.html.
The closest related related research appears to be reverse mode for another functional language related to Scheme at http://www.bcl.hamilton.ie/~qobi/stalingrad/.
I see reverse mode in Haskell as kind of a holy grail for a lot of tasks, with the hopes that it could use Haskell's nested data parallelism to gain a nice speedup in heavy numerical optimization.
In response to this question, I've uploaded a package named ad to Hackage for handling reverse-mode automatic differentiation in Haskell.
Internally, it leverages a trick from Andy Gill's Kansas Lava to observe sharing in the tape it records for back propagation purposes, and uses type level branding to avoid confusing sensitivities.
I've tried to keep the API relatively close to that of Barak Pearlmutter and Jeffrey Mark Siskind's fad package, but I couldn't resist making a couple of minor tweaks here and there for generality.
I still need to go through and finish up the remaining unimplemented fad combinators, figure out a nice way to build a reverse-mode AD tower, validate that I didn't screw up my recollection of basic calculus, and provide a nice API for using this approach to get local reverse mode checkpoints in an otherwise forward mode AD program, but I am quite happy with how things have progressed thus far.
We have a bunch of forward mode AD implementations (I even have one in my monoids library!), but reverse mode AD for all of Haskell seems to be intractable.
Sadly while Pearlmutter and Siskind give a translation for a lambda calculus, it doesn't map into something you can do for arbitrary Haskell lambdas, you don't get the right introspection properties and given the way the shape of the types change in the translation you don't get something that is amenable to being packed into a monad, arrow, or other control structure.
I had a go at it via a series of email exchanges with Pearlmutter, but ultimately the best I was able to obtain was a reverse mode AD solution for a small EDSL in Haskell, and not a solution for Haskell itself.
Not that I'm aware of. I do know that some Haskell folks are interested in automatic differentiation, but some quick digging found little more than short asides mentioning the reverse mode; I expect you already found the same material I did.
I also note that the fad package and Stalingrad project you found are in fact the work of the same two people, and that at least Prof. Pearlmutter has posted to the haskell-cafe mailing list. You may want to consider contacting him directly about his work--it's possible he has something in progress, or hit serious obstacles while attempting to implement reverse-mode AD.
Sorry I couldn't turn up anything more useful; if someone else wants to dig further, at least the links above are a starting point.
I think forward is the way to go in Haskell. You shouldn't be able to do reverse mode on arbitrary functions, as Edward pointed out. But you responded that you should be able to do it on certain constrained functions. And said constraints can lead readily to forward mode. Eg. if you have a function:
foo :: Num a => a -> a -> a
Then you can instantiate a with a differentiable type, and thus differentiate foo in forward mode.
See the vector-space library on Hackage for very elegant forward mode automatic differentiation. It might not be entirely clear how to use it at first. Read the paper about it, Beautiful Differentiation by Conal Elliott.

Resources