Reputation: 310
I am relatively new to Haskell and trying to get some experience by building a website using the Yesod framework. The biggest struggle I am facing right now is that I am often unsure what type is required by one function or what type is returned by another function. I also have trouble interpreting the type errors that are produced when I make a mistake.
In particular, I would like to be able to do the following things but I currently do not know a good way to do them.
Upvotes: 3
Views: 237
Reputation: 4984
You already have some nice answers about how to look up documentation, so I'll just mention a little trick about dealing with confusing type errors.
I find it helpful to break apart my incorrect function into many small ones that are all introduced at the top level. When the issue was merely an operator precedence problem, this may solve it immediately, but sometimes there is a deeper problem.
You can then remove your type annotation on a broken function, change its definition to undefined
, and load it into ghci. This will usually allow ghci to infer the correct type for your component function, and you can find that type with :t. Having the correct type for a simple sub-component function will usually lead directly to the right implementation, after which you can replace the type annotation and/or re-compose things as desired.
A more principled approach to having Haskell help you out in finding the correct types of sub-components is demonstrated here: http://matthew.brecknell.net/post/hole-driven-haskell/
Upvotes: 1
Reputation: 11963
You asked several questions, so I'll try to answer each of them seperately:
You don't need to know the package name, only the module name. If you only use Yesod functions, import Yesod
at the GHCi prompt will import most functions of it. Other than that, you'll have to use (3) to find and import all the neccessary modules for your expression.
If you use an editor like vim or emacs, you can generate TAGS for your project, and then use the editor's jump-to-definition feature based on the TAGS. To generate tags for all loaded modules from GHCi, you can use the one of the following commands
:ctags -- ctags for vim
:etags -- etags for emacs
If you want to generate tags for a whole project, you can use a tag generator such as hasktags. There is also a Haskell Wiki Page about this.
If you have the modules with the definition loaded in GHCi, you can also use :i symbol to find the place of it's definition:
Prelude> :i maybe
maybe :: b -> (a -> b) -> Maybe a -> b -- Defined in `Data.Maybe'
You can use Hayoo. It indexes most of hackage, I haven't found any package which it doesn't index yet (except the GHC API documentation, but that isn't on hackage). Alternatively, there is Hoogle which is also able to search for functions given a type signature, but it doesn't index all of hackage. In particular, Yesod doesn't seem to be in it's index.
Upvotes: 3