Finding type signatures in Haskell

I'm playing with a small server in Haskell. As an exercise, I am manually adding type signatures.

import System.Posix (installHandler, sigPIPE, getFileStatus, fileSize, Handler(..))
import Network (listenOn, accept, PortID(..))
import qualified Data.ByteString.Lazy as B
import Data.Binary
import Network.Socket (close)
import Control.Monad (forever, replicateM_)
import System.Environment (getArgs)
import System.IO (hGetContents, hPutStr, hSetBinaryMode, withFile, hClose, IOMode(..))

...

--here's what I want to add a type signature to
processRequest h = do
    s <- hGetContents h
    let req = takeWhile (/= "r") $ lines s 
    mapM_ (x -> putStrLn $ "> "++x) req
    return $ words (head req) !! 1

Well there should be an easy way to figure this out, let's see what GHCI's type inference says:

Prelude> :l tinyserver.hs 
[1 of 1] Compiling Main             ( tinyserver.hs, interpreted )
Ok, modules loaded: Main.
*Main> :info processRequest
processRequest :: GHC.IO.Handle.Types.Handle -> IO String
    -- Defined at tinyserver.hs:26:1

Man that's a big type. Ok, let's add it:

processRequest :: GHC.IO.Handle.Types.Handle -> [Char] -> IO ()
processRequest h = do
   ...

That should do it. Is GHCI cool with this?

*Main> :l tinyserver.hs 
[1 of 1] Compiling Main             ( tinyserver.hs, interpreted )
tinyserver.hs:26:19:
    Not in scope:
      type constructor or class ‘GHC.IO.Handle.Types.Handle’
Failed, modules loaded: none.

Yuck. Maybe the name is too long. Let's try all the child types:

processRequest :: IO.Handle.Types.Handle -> [Char] -> IO ()
processRequest :: Handle.Types.Handle -> [Char] -> IO ()
processRequest :: Types.Handle -> [Char] -> IO ()
processRequest :: Handle -> [Char] -> IO ()

No, they all give me the same result.

What is the right way to find these types? GHCI? Hoogle? Something else?

链接地址: http://www.djcxy.com/p/43060.html

上一篇: Haskell类型的构造函数可以不是

下一篇: 在Haskell中查找类型签名