在Haskell中查找类型签名

时间:2015-12-18 22:06:45

标签: haskell type-inference ghci

我正在玩a small server in Haskell。作为练习,我手动添加类型签名。

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

那么应该有一个简单的方法来解决这个问题,让我们看看GHCI的类型推断是什么意思:

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

男人是个大人物。好的,让我们添加它:

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

应该这样做。 GHCI很酷吗?

*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.

呸。也许名字太长了。让我们尝试所有的孩子类型:

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

不,他们都给我相同的结果。

找到这些类型的正确方法是什么? GHCI? Hoogle?还有别的吗?

0 个答案:

没有答案