How can I view the definition of a function in Haskell/GHCi? - haskell

I'm using Haskell 2010.1.0.0.1 with GHC 6. Typing :t at the GHCi prompt followed by the name of a function shows us the type of the function. Is there a way to view the function definition as well?

Not currently.
The closest command to what you want is :info
:info name ...
Displays information about the given name(s). For example, if name is a class, then the class methods and their types will be printed; if name is a type constructor, then its definition will be printed; if name is a function, then its type will be printed. If name has been loaded from a source file, then GHCi will also display the location of its definition in the source.
For types and classes, GHCi also summarises instances that mention them. To avoid showing irrelevant information, an instance is shown only if (a) its head mentions name, and (b) all the other things mentioned in the instance are in scope (either qualified or otherwise) as a result of a :load or :module commands.
like so:
Prelude> :info ($)
($) :: (a -> b) -> a -> b -- Defined in GHC.Base
infixr 0 $
You can though, see the source for identifiers generated by the haddock tool, on Hackage.
Look up the module on Hackage
Click on the source link
Note that "?src" is a valid command in lambdabot, on the #haskell IRC channel, and does what you'd expect.
> ?src ($)
> f $ x = f x

I don't think so. You can use :i for a little bit more information (more useful for infix operators and data constructions, etc.), but not the definition:
ghci> :i repeat
repeat :: a -> [a] -- Defined in GHC.List
You can use hoogle to quickly find the documentation for a standard library function, which on the right has a link to go to the source. It's still a few clicks away though.

Nope, can't do that. Some fun things you, the Haskell beginner, can do:
On the HTML haddock documents, click on "source"... study the source.
:browse to find all of the definitions exported by a module
Use :help for the obvious result
use the web interface of hoogle to search for functions, or install hoogle locally!
?
Profit!

Related

how to see the implemented code of a function of a module in haskell?

I am a newbie to Haskell and I know The Haskell standard library is split into modules, each of them contains functions and types that are somehow related and serve some common purpose. I would like to see the implementation(code) of those library functions.where can I see that ? is there any command in ghci so that I can see the implementation or provide me any resources to learn about modules.
Thank you
Probably the most convenient way to do it, is use Hackage. You can for instance inspect the map function, by clicking Source on the right side of the function signature. This then will show the highlighted code fragment. For instance:
map :: (a -> b) -> [a] -> [b]
{-# NOINLINE [0] map #-}
-- We want the RULEs "map" and "map/coerce" to fire first.
-- map is recursive, so won't inline anyway,
-- but saying so is more explicit, and silences warnings
map _ [] = []
map f (x:xs) = f x : map f xs
You can also use Hoogle to search functions by name or signature, and by clicking the results, you will be redirected to the relevant hackage page.

Where does Haskell keep its libraries? [duplicate]

Is there a way to see Typeclass definition in ghci for a specific type?
For example, Maybe is defined like this:
instance Functor Maybe where
fmap f (Just x) = Just (f x)
fmap f Nothing = Nothing
Can I see this in ghci ?
When, I use :info in ghci, I get this:
Prelude> :i Maybe
data Maybe a = Nothing | Just a -- Defined in `Data.Maybe'
instance Eq a => Eq (Maybe a) -- Defined in `Data.Maybe'
instance Monad Maybe -- Defined in `Data.Maybe'
instance Functor Maybe -- Defined in `Data.Maybe'
instance Ord a => Ord (Maybe a) -- Defined in `Data.Maybe'
instance Read a => Read (Maybe a) -- Defined in `GHC.Read'
instance Show a => Show (Maybe a) -- Defined in `GHC.Show'
In the above output, I want to see how it is defined in Data.Maybe as an instance for Functor. Anyway to see that in ghci ?
No, it's not possible – not just for instances but for anything. GHC only registers the compiled version of a package, so the source code generally won't be available to ghci.
Probably, most often you'll be using stuff from Hackage; in that case it's very simple to find the source code of such instances by hoogling the module, locating the class or data declaration, and clicking on source.
When you don't have internet access or whatever else reason you can't hoogle online, you first need to find out in what package the module is included. The easiest way to do that:
$ ghc-pkg find-module Data.Maybe
/usr/local/haskell/lib/ghc-7.6.2/package.conf.d
   base-4.6.0.1
   haskell2010-1.1.1.0
~/.ghc/x86_64-linux-7.6.2/package.conf.d
Then, as I said, GHC doesn't know where the source code to these packages is located – in fact it might not even be available on your system! But if you've installed the package (or one that depends on it) with cabal install, it will be there, by default in ~/.cabal/packages/hackage.haskell.org/PᴀᴄᴋᴀɢᴇNᴀᴍᴇ (as a compressed archive, but that's not a big hurdle). Within the package project folder, you can simply locate the module via the directory structure, which represents the module hierarchy.
Other packages, like your example of Data.Maybe (package haskell2010), may have come right with your installation of GHC, e.g. the Haskell platform. In that case, I believe the easiest thing is to search there for the Haddock documentation file. In my case,
$ find /usr/local/haskell -name 'Data-Maybe.html' | head -n1 | xargs firefox
That'll open up the equivalent to what hoogle links you to (but on your local HD), where you can also browse the source code in a user-friendly way.

Function names with symbol characters makes Googling difficult

In Haskell, many function names contain only symbol characters. Like $$, >>=, >>, :, ->, =>, =~.
Since I am new to Haskell, I am finding it difficult to search their meanings in Google. For example, to understand what -> means in Haskell, I need to use the search string hyphen followed by greater than which is not the best approach, as per me.
Is there a place that I could search for functions with symbols only?
Yes, this is a known bug with Google. You might consider a better search engine like Hoogle.
In general you need to look up the documentation for the actual function. To do this, you need to know what module it's defined in. The easiest way to determine this is to load up your source file in GHCi (so that you have all of its imports etc.) and then ask for the operator's :info thusly:
Prelude> :info (>>=)
class Monad (m :: * -> *) where
(>>=) :: m a -> (a -> m b) -> m b
...
-- Defined in ‘GHC.Base’
infixl 1 >>=
Prelude>
If the type signature is not enough, then this also tells you that you need to google the GHC.Base module, and the Monad typeclass. By itself that's pretty googleable, but if that typeclass keyword weren't there, what you would do is to google GHC.Base, the first result leading to the base package overview page. Once you are there1 then you look for a little link labeled [Index] beneath the module listing (GHC.Base has a huge module listing so in this case it's easier to miss).
Clicking that link takes you to an index of all the public symbols in that package; you can click the > character to find all operators beginning with a greater than sign. You will then have three links of modules which export that function; click on one and Ctrl-F to find the following documentation:
(>>=) :: forall a b. m a -> (a -> m b) -> m b | infixl 1 | Source
Sequentially compose two actions, passing any value produced by the first
as an argument to the second.
Again, Hoogle does all of this rigamarole for you and has some other nifty features like searching-by-type-signature.
For things like <-, ->, and => which are not functions, you will just have to know the language. The meaning of <- ("from") is from "do-notation", which you can Google; the meaning of -> ("to") varies depending on whether it appears in lambda-notation (like \a b -> b), case-expressions, or the type signature of a function (where a -> b -> c means "a function which takes an a and returns a function which takes a b and returns some c". The meaning of => is from "constraints" or "type classes" in Haskell.
Other than ->, you can sometimes see operators appearing in type signatures, too. These should be searchable by the above procedure.
This is assuming a stable API for the package. If the API has changed you will need to look up with ghc -v which package version the file is using, then click on that version.

How to view Typeclass definitions and a type's Typeclass implementations [duplicate]

Is there a way to see Typeclass definition in ghci for a specific type?
For example, Maybe is defined like this:
instance Functor Maybe where
fmap f (Just x) = Just (f x)
fmap f Nothing = Nothing
Can I see this in ghci ?
When, I use :info in ghci, I get this:
Prelude> :i Maybe
data Maybe a = Nothing | Just a -- Defined in `Data.Maybe'
instance Eq a => Eq (Maybe a) -- Defined in `Data.Maybe'
instance Monad Maybe -- Defined in `Data.Maybe'
instance Functor Maybe -- Defined in `Data.Maybe'
instance Ord a => Ord (Maybe a) -- Defined in `Data.Maybe'
instance Read a => Read (Maybe a) -- Defined in `GHC.Read'
instance Show a => Show (Maybe a) -- Defined in `GHC.Show'
In the above output, I want to see how it is defined in Data.Maybe as an instance for Functor. Anyway to see that in ghci ?
No, it's not possible – not just for instances but for anything. GHC only registers the compiled version of a package, so the source code generally won't be available to ghci.
Probably, most often you'll be using stuff from Hackage; in that case it's very simple to find the source code of such instances by hoogling the module, locating the class or data declaration, and clicking on source.
When you don't have internet access or whatever else reason you can't hoogle online, you first need to find out in what package the module is included. The easiest way to do that:
$ ghc-pkg find-module Data.Maybe
/usr/local/haskell/lib/ghc-7.6.2/package.conf.d
   base-4.6.0.1
   haskell2010-1.1.1.0
~/.ghc/x86_64-linux-7.6.2/package.conf.d
Then, as I said, GHC doesn't know where the source code to these packages is located – in fact it might not even be available on your system! But if you've installed the package (or one that depends on it) with cabal install, it will be there, by default in ~/.cabal/packages/hackage.haskell.org/PᴀᴄᴋᴀɢᴇNᴀᴍᴇ (as a compressed archive, but that's not a big hurdle). Within the package project folder, you can simply locate the module via the directory structure, which represents the module hierarchy.
Other packages, like your example of Data.Maybe (package haskell2010), may have come right with your installation of GHC, e.g. the Haskell platform. In that case, I believe the easiest thing is to search there for the Haddock documentation file. In my case,
$ find /usr/local/haskell -name 'Data-Maybe.html' | head -n1 | xargs firefox
That'll open up the equivalent to what hoogle links you to (but on your local HD), where you can also browse the source code in a user-friendly way.

HASKELL - Change Type

I need to create a function f:: Log->[String] that does that (((o, i ,d),s) = [(o, i ,d)]
type Log = (Plate, [String])
type Plate = (Pin, Pin, Pin)
type Pin = (Char, Int)
If you're on a page like this, click "Source" on the fir right side, next to the function that you're interested in.
If you need to look up a function, Hayoo! and Hoogle will link you to documentation pages like the one above.
An important thing to note, though is that show doesn't have one definition. show is a function defined for all data types in the Show (with a capital "S") typeclass. So for example, here is the full source for the Show typeclass. Show is defined within the typeclass as just show :: a -> String. But if you search for "instance Show Bool" or "instance Show Int", you'll find specific definitions.
For the second part of your question, the easiest way to get a show function for a new type is to simply write deriving (Show) below it. For example,
data Foo = Foo Int Int
deriving (Show)
Now I can use show on data with type Foo.
g :: Log -> [String]
g (plate, _) = [show plate]
Use hoogle to find this sort of information.
Example: http://www.haskell.org/hoogle/?hoogle=show
Once you've found the function you'd like in the list, click and there you'll find a Source link on the right hand side of the page.
NB this is an answer to the original question:
Where can I see the codes of the predefined functions in haskell ?? Mainly the function SHOW?
It's true that you can use Hoogle to search for functions defined in the Prelude (and other modules), but the source code itself is located at Hackage.
Hackage is a database of Haskell packages. You can download new packages from it, and also view the Haddock documentation for every package in the database.
This is the Haddock page for the standard Prelude. It documents the type classes, data types, types, and top-level functions exported by the Prelude module. To the right of each definition header is a link that says "Source". You can click this to be taken to an online copy of the source code for the module you're viewing.
On preview, you're now asking a different question entirely, and on preview again in fact the original question has been edited out of this post.
Your new question is unclear, but this solution will work to produce the output in your example.
> [fst ((('O',0),('I',0),('D',1)),"O->D")]
[(('O',0),('I',0),('D',1))]
I think you're using list notation instead of double quotes to identify Strings, by the way, so I fixed that around 0->D above. So you might also try this instead.
> show (fst ((('O',0),('I',0),('D',1)),"O->D"))
"(('O',0),('I',0),('D',1))"
This works because you have only defined type synonyms (by using type in your declarations instead of data) on data structures which already have Show instances.

Resources