gpt4 book ai didi

Haskell primPutChar 定义

转载 作者:行者123 更新时间:2023-12-01 06:01:17 30 4
gpt4 key购买 nike

我想弄清楚基本的 IO定义了 Haskell 函数,所以我使用了 this reference然后我到了 putChar函数定义:

putChar    :: Char -> IO ()
putChar = primPutChar

但是,现在我找不到关于此 primPutChar 的更多信息。随时随地发挥作用。也许它可能指的是一个预编译的函数,可以从共享对象中作为二进制文件使用?如果是这样,是否可以查看其源代码?

最佳答案

什么 prim*方法

既然你是从报告的角度问这个问题,那我们也从报告的角度来回答这个问题:

Primitives that are not definable in Haskell , indicated by names starting with "prim", are defined in a system dependent manner in module PreludeBuiltin and are not shown here



这在 Haskell2010 by the way 中仍然相同.

它是如何在 GHC 中实现的

但是,您可以 have a look at base 's source看看它是如何在 GHC 中实现的:
putChar         :: Char -> IO ()
putChar c = hPutChar stdout c

从那里你将深入兔子洞。怎么样 hPutChar 知道如何打印东西吗?好吧,它没有。它只“缓冲”并检查您是否可以编写:
hPutChar :: Handle -> Char -> IO ()
hPutChar handle c = do
c `seq` return ()
wantWritableHandle "hPutChar" handle $ \ handle_ -> do
hPutcBuffered handle_ c

写作是在 writeCharBuffer 中完成的它填充一个内部缓冲区,直到它满了(或者已经到达一行——它实际上取决于缓冲模式):
writeCharBuffer h_@Handle__{..} !cbuf = do
-- much code omitted, like buffering
bbuf'' <- Buffered.flushWriteBuffer haDevice bbuf'
-- more code omitted, like buffering

那么 flushWriteBuffer 在哪里?定义?它实际上是 stdout 的一部分:
stdout :: Handlestdout = unsafePerformIO $ do   setBinaryMode FD.stdout   enc <- getLocaleEncoding   mkHandle FD.stdout "<stdout>" WriteHandle True (Just enc)                nativeNewlineMode{-translate newlines-}                (Just stdHandleFinalizer) Nothing
stdout :: FD
stdout = stdFD 1

而文件描述符( FD )是 BufferedIO 的一个实例:
instance BufferedIO FD where
-- some code omitted
flushWriteBuffer fd buf = writeBuf' fd buf

writeBuf用途 instance GHC.IO.Device.RawIO FD 's write ,最终 leads to :
writeRawBufferPtr loc !fd buf off len  | isNonBlocking fd = unsafe_write -- unsafe is ok, it can't block  | otherwise   = do r <- unsafe_fdReady (fdFD fd) 1 0 0                     if r /= 0                        then write                        else do threadWaitWrite (fromIntegral (fdFD fd)); write  where    do_write call = fromIntegral `fmap`                      throwErrnoIfMinus1RetryMayBlock loc call                        (threadWaitWrite (fromIntegral (fdFD fd)))    write         = if threaded then safe_write else unsafe_write unsafe_write = do_write (c_write (fdFD fd) (buf `plusPtr` off) len) safe_write = do_write (c_safe_write (fdFD fd) (buf `plusPtr` off) len)

where we can see c_safe_write and c_write, which are usually bindings to C library functions:

foreign import capi unsafe "HsBase.h write"
c_write :: CInt -> Ptr Word8 -> CSize -> IO CSsize

所以, putChar用途 write .至少在 GHC 的实现中。然而,该报告不需要该实现,因此允许另一个编译器/运行时使用其他函数。

TL; 博士

GHC 的实现使用 write 用内部缓冲区来写东西,包括单个字符。

关于Haskell primPutChar 定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45133494/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com