[Haskell-cafe] How to write a pure String to String function in Haskell FFI to C++

Ting Lei tinlyx at gmail.com
Mon Jun 3 04:19:13 CEST 2013


Thanks for your answers so far.

It seems that the laziness of String or [char] is the problem.

My question boils then down to this. There are plenty of Haskell FFI
examples where simple things like sin/cos in <math.h> can be imported into
Haskell as pure functions. Is there a way to extend that to String without
introducing an IO (), but maybe sacrificing laziness?
If String has to be lazy, is there another Haskell data type convertible to
String that can do the job?

The C++/C function (e.g. toUppers) is computation-only and as pure as cos
and tan. The fact that marshaling string incurs an IO monad in current
examples is kind of unintuitive and like a bug in design. I don't mind
making redundant copies under the hood from one type to another..




On Sun, Jun 2, 2013 at 8:08 PM, Brandon Allbery <allbery.b at gmail.com> wrote:

> On Sun, Jun 2, 2013 at 8:01 PM, Thomas Davie <tom.davie at gmail.com> wrote:
>
>> On 2 Jun 2013, at 16:48, Brandon Allbery <allbery.b at gmail.com> wrote:
>>
>> (String is a linked list of Char, which is also not a C char; it is a
>> constructor and a machine word large enough to hold a Unicode codepoint.
>> And because Haskell is non-strict, any part of that linked list can be an
>> unevaluated thunk which requires forcing the evaluation of arbitrary
>> Haskell code elsewhere to "reify" the value; this obviously cannot be done
>> in the middle of random C code, so it must be done during marshalling.)
>>
>>
>> I'm not convinced that that's "obvious" – though it certainly requires
>> functions (that go through the FFI) to grab each character at a time.
>>
>
> I think you underestimate the complexity of the Haskell runtime and the
> interactions between it and the FFI. Admittedly it is probably not
> "obvious" in the sense of "anyone can tell without knowing anything about
> it that it can't possibly work", but it should be at least somewhat obvious
> to someone who sees why there needs to be an FFI in the first place that
> the situation is not trivial, and that they probably should not blindly
> assume that the only reason one can't just pass Haskell values directly to
> C is that some GHC developer was feeling lazy at the time.
>
> --
> brandon s allbery kf8nh                               sine nomine
> associates
> allbery.b at gmail.com
> ballbery at sinenomine.net
> unix, openafs, kerberos, infrastructure, xmonad
> http://sinenomine.net
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20130602/193028be/attachment.htm>


More information about the Haskell-Cafe mailing list