[Haskell-cafe] C binding <-> safer haskell API

Viktor Dukhovni ietf-dane at dukhovni.org
Thu Feb 10 14:28:27 UTC 2022


On Thu, Feb 10, 2022 at 10:39:26AM +0100, PICCA Frederic-Emmanuel wrote:

> Then it seems to me that I just need to define a merge function like this
> 
>    merge :: PrimMonad m => MCube (PrimState m) -> Frame -> m () 
> 
> and then an unsafeFreeze to avoid copy.  It that correct?

Well, the `unsafeFreeze` would typicall only be appropriate in pure
functions that *construct* a pure `Cube` from scratch by allocating and
mutating an `MCube`.  As for merge itself, it runs in IO and mutates
an `MCube`.  It is the caller's responsibility to ensure that that
object is not "shared".

If the entire computation runs in `IO` then you just use MCube(s)
throughout.  Once you have a pure `Cube`, you would typically not mutate
it in place, except in "linear" use-cases that *consume* its *sole*
reference while mutating it in place (possibly returning a fresh
Cube based on the modified copy).  Ensuring "linearity" is not trivial
(a key reason for the existence of "Rust").

Use of `unsafeThaw` is atypical.

-- 
    Viktor.


More information about the Haskell-Cafe mailing list