作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想弄清楚基本的 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 modulePreludeBuiltin
and are not shown here
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 的实现中。然而,该报告不需要该实现,因此允许另一个编译器/运行时使用其他函数。
write
用内部缓冲区来写东西,包括单个字符。
关于Haskell primPutChar 定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45133494/
我试图弄清楚基本的IO Haskell 函数是定义好的,所以我使用了this reference我到了putChar函数定义: putChar :: Char -> IO () putChar
我想弄清楚基本的 IO定义了 Haskell 函数,所以我使用了 this reference然后我到了 putChar函数定义: putChar :: Char -> IO () putCha
我是一名优秀的程序员,十分优秀!