sokras
sokras

Reputation: 629

Finding type signature of a function in Haskell

I have created two functions that basically parse and input and I need to find their type signatures so that ghc -Wall in the terminal won't give me a warning. This is the code:

import Text.Parsec.Prim
import Text.Parsec.Char
import Text.Parsec.Error
import Text.Parsec.String
import Text.Parsec.Combinator
cToken c = try (many space >> char c >> many space)

sToken s = try (many space >> string s >> many space)

If i write in the terminal: :t cToken :t sToken

It gives back:

Prelude CurvySyntax> :t sToken
sToken
  :: Text.Parsec.Prim.Stream s m Char =>
     String -> Text.Parsec.Prim.ParsecT s u m [Char]
Prelude CurvySyntax> :t cToken
cToken
  :: Text.Parsec.Prim.Stream s m Char =>
     Char -> Text.Parsec.Prim.ParsecT s u m [Char]

If I put these types in my code then it can't compile. What are their types?

Thanks.

Upvotes: 3

Views: 1362

Answers (1)

J. Abrahamson
J. Abrahamson

Reputation: 74394

GHCi cheats a little bit with imports: it allows you to refer to public modules by their full name at any point in time. For instance

$ ghci
$ [ ... ]
Prelude> :t Data.List.sortBy (Data.Ord.comparing snd)
Data.List.sortBy (Data.Ord.comparing snd)
  :: Ord a => [(a1, a)] -> [(a1, a)]

As you can see here, I was able to refer to sortBy and comparing by their fully qualified module+symbol names. If you try to do the same in a concrete Haskell source file it will fail unless I also import those modules (qualified). So, GHCi takes some liberties.

Likewise, when you ask for the type of some function it may need to refer to certain types or typeclasses which have not yet been imported. GHCi takes some liberties and just displays those types/classes using fully qualified module+symbol names. In your example these include

Text.Parsec.Prim.Stream
Text.Parsec.Prim.ParsecT

If you were to just copy these to your source file it will complain because you've not yet imported the module Text.Parsec.Prim.

So what's the resolution? Just import it!

import Text.Parsec.Prim

If you add that to your source file and :reload GHCi then check the types once more the new results will reflect the fact that you have access to these types

Prelude CurvySyntax> :t sToken
sToken :: Stream s m Char => String -> ParsecT s u m [Char]

and this new type can be directly pasted into your source file.

Upvotes: 3

Related Questions