[m-users.] FFI C Include files not appearing

jfondren at minimaltype.com jfondren at minimaltype.com
Wed Jun 26 02:58:10 AEST 2019


(sorry about duplicate emails. I am still learning how this 'electronic
mail' stuff works.)

On 2019-06-25 05:56, emacstheviking wrote:
> Hi Peter,
> I did wonder about that ...you can tell I am new to mercury for sure.
> My plan is to create as simple an interface to SDL2 as possible. I
> produced a full wrapper for GNU Prolog and it took ages...this time
> the "plan" is to produce something akin to a "display list" (my Atari
> days!) and then have a single C function that walks the display list
> and turns each entry into the appropriate SDL draw line, point, image
> etc etc
> 
> Can you explain why I need to pass in !IO if I am not actually going
> to modify it? I am a bit confused about the
> 
>    sdl_up(Result::out, _IO0::di, _IO::uo)

For technical reasons (nobody can find the appropriate headers that
describe the exact data structures) the Mercury compiler doesn't 
actually
modify the state of the universe directly. It just pretends to. Your
code can include a ``_IO = _IO0;''. It won't result in work at runtime.

Consider:

   sdl_up(Res),
   sdl_draw_text_or_something("hi"),
   sdl_draw_text_or_something("hi"). % same thing again

which of those should happen first? Should "hi" be drawn once or twice?

If you thread IO through them then the data dependency introduced by
the IO intermediate variables forces an ordering and forces the repeated
call. As is, if you promise that these predicates are pure, then Mercury
is free to perform sdl_up(Res) at the end instead of the beginning, and
it's free to perform sdl_draw_text_or_something("hi") only once.

> Or is this something that the compiler will be making use of...like I
> say, I am new to mercury but I am pretty familiar with Haskell and
> Prolog if that helps to explain anything.
> 
> I will need some extra support for fonts and textures but that's the
> fun of exploratory hacking!
> 
> Thanks again,
> Sean.
> 
> On Tue, 25 Jun 2019 at 11:42, Peter Wang <novalazy at gmail.com> wrote:
> 
>> On Tue, 25 Jun 2019 11:03:53 +0100, emacstheviking
>> <objitsu at gmail.com> wrote:
>>> Hi,
>>> I have these two lines of code...
>>> 
>>> :- pragma foreign_decl("C", "#include <SDL/SDL.h>").
>>> :- pragma foreign_decl("C", "#include <SDL/SDL_version.h>").
>>> 
>>> at the top of my module in the implementation section and this
>> code too:
>>> :- func sdl_up = int.
>>> :- pragma foreign_proc("C",
>>> sdl_up = (Result::out),
>>> [ will_not_call_mercury
>>> , not_thread_safe
>>> , promise_pure
>>> ],
>>> "Result = SDL_Init(SDL_INIT_EVERYTHING);").
>>> 
>> 
>> Hi,
>> 
>> SDL_Init() is impure, so wrapping it up in pure function is
>> incorrect.
>> Rather, you should wrap it in a predicate that takes the I/O state:
>> 
>> :- pred sdl_up(int::out, io::di, io::uo) is det.
>> 
>> :- pragma foreign_proc("C",
>> sdl_up(Result::out, _IO0::di, _IO::uo),
>> [will_not_call_mercury, promise_pure],
>> "
>> Result = SDL_Init(SDL_INIT_EVERYTHING);
>> ").
>> 
>> Peter
> _______________________________________________
> users mailing list
> users at lists.mercurylang.org
> https://lists.mercurylang.org/listinfo/users


More information about the users mailing list