Quick Q: do all FFI (non-primop) calls involve State# and RealWorld?

Christopher Done chrisdone at gmail.com
Mon Oct 28 09:59:31 UTC 2019

Hi all,

I tried compiling this file:

{-# LANGUAGE NoImplicitPrelude #-}-- | Demonstrate various use of the
FFI.module Foreign whereimport Foreign.Cforeign import ccall "math.h
sin" sin :: CDouble -> CDoubleit :: CDoubleit = sin 1

And I’ve noticed that the annotated type given for this foreign op in Core,
is (# State# RealWorld, CDouble #), whereas I would have expected e.g.

Meanwhile, the foreign op call is passed a RealWorld argument.

Additionally, code that consumes the result of this foreign call expects a (#
CDouble #) as a return value.

So there are some assumptions I put in my interpreter to test this FFI call

   1. Despite claiming to return the real world in a tuple, it actually
   should just return an unboxed tuple of the value.
   2. It should ignore the RealWorld argument entirely.

I assume, if I were to lift this function into returning IO, that I should
indeed return the RealWorld argument given. So the lesson is:

All FFI functions accept a RealWorld, and may return a 2-tuple of State#
RealWorld *if* it’s impure, else it’ll return a 1-tuple of the value.

Can someone confirm that my observations are right? Also, if so, is there
somewhere I can read more about this?


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20191028/98b2ce47/attachment.html>

More information about the ghc-devs mailing list