<div dir="ltr">Julian, Peter,<div><br></div><div>@Peter ---> you hit the nail on the head and...<br></div><div>@Julian ---> explained how the nail was constructed in great detail.<br></div><div><br></div><div>Thank you both, I now fully get it...my problem then somewhere is actually reading the documentation and seeing the relevant bits! [ or uint32-s even ;) ]</div><div><br></div><div>The fundamental problem here was just not knowing how to express a literal value as a different type I guess.</div><div>I knew that changing my C code was merely a feeble way to "remove" the problem where you have not both provided me with the correct information to "do things properly".</div><div><br></div><div>I have since read <a href="https://www.mercurylang.org/information/doc-latest/mercury_ref/Primitive-types.html#Primitive-types">https://www.mercurylang.org/information/doc-latest/mercury_ref/Primitive-types.html#Primitive-types</a> much much more closely! When learning things I sometimes have a tendency to skip the details in order to get to the coalface a little quicker...I am intrigued my Mercury and it's ability to cross comple to Erlang and Java... I use both in my day job and the prospect of slipping a little mercury into the mix in the future, having gained at leat basic competency, would be really interesting indeed!</div><div><br></div><div>Thanks again everybody.</div><div>Sean.</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, 7 Jul 2019 at 03:46, Julian Fondren <<a href="mailto:jfondren@minimaltype.com">jfondren@minimaltype.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On 2019-07-06 15:05, emacstheviking wrote:<br>
> Getting to grips with the FFI and starting to create/port my GNU<br>
> Prolog SDL2 wrapper from long ago. The "extras/graphics" folder is<br>
> amazing amount of work but I liked SDL so I figured if I ever complete<br>
> it to standard then it might complete the set!<br>
> <br>
> I can't figure out why I can't use "uint32" in the predicate<br>
> declaration, currently I use int but cast the value:<br>
> <br>
> :- pred sdl_delay(int::in, io::di, io::uo) is det.<br>
> :- pragma foreign_proc("C",<br>
> sdl_delay(MSecs::in, _IO0::di, _IO::uo),<br>
> [promise_pure,will_not_call_mercury,does_not_affect_liveness],<br>
> "<br>
> SDL_Delay((Uint32)MSecs);<br>
> ").<br>
> <br>
> What I *want* to do is this:<br>
> <br>
> :- pred sdl_delay(uint32::in, io::di, io::uo) is det.<br>
> :- pragma foreign_proc("C",<br>
> sdl_delay(MSecs::in, _IO0::di, _IO::uo),<br>
> [promise_pure,will_not_call_mercury,does_not_affect_liveness],<br>
> "<br>
> SDL_Delay(MSecs);<br>
> ").<br>
> <br>
> But when I do thois the compiler is angry with me: :(<br>
<br>
It seems like you think you're fixing this by changing the C code, but<br>
really you are correcting the error by changing your Mercury.<br>
Consider:<br>
<br>
:- module uintx.<br>
:- interface.<br>
:- import_module io.<br>
:- pred main(io::di, io::uo) is det.<br>
:- implementation.<br>
:- import_module uint32.<br>
<br>
:- pred do_nothing(uint32::in) is det.<br>
do_nothing(_).<br>
<br>
main(!IO) :-<br>
do_nothing(42).<br>
<br>
This fails to compile with a very similar error:<br>
<br>
$ mmc uintx<br>
uintx.m:012: In clause for predicate `main'/2:<br>
uintx.m:012: in argument 1 of call to predicate <br>
`uintx.do_nothing'/1:<br>
uintx.m:012: type error: argument has type `int',<br>
uintx.m:012: expected type was `uint32'.<br>
<br>
do_nothing/1 and your wanted sdl_delay accept an int32, but here and<br>
in your code an int being passed to it. The type error,<br>
<br>
uintx.m:012: type error: argument has type `int',<br>
uintx.m:012: expected type was `uint32'.<br>
<br>
is between the pred definition (expected type) and a given parameter<br>
(argument has type) in a given invocation (line 12 in main/2):<br>
<br>
uintx.m:012: In clause for predicate `main'/2:<br>
uintx.m:012: in argument 1 of call to predicate <br>
`uintx.do_nothing'/1:<br>
<br>
You've fixed this by changing your predicate definition to no longer<br>
expect an int32 -- the change in your C code is incidental to thix<br>
fix. The other way to fix this is of course to change the type of the<br>
argument that is passed where this predicate is called:<br>
<br>
This compiles without error:<br>
<br>
:- module uintx.<br>
:- interface.<br>
:- import_module io.<br>
:- pred main(io::di, io::uo) is det.<br>
:- implementation.<br>
:- import_module uint32.<br>
<br>
:- pred do_nothing(uint32::in) is det.<br>
do_nothing(_).<br>
<br>
main(!IO) :-<br>
do_nothing(cast_from_int(42)).<br>
<br>
> sdl2.m:067: In clause for predicate `main'/2:<br>
> sdl2.m:067: in argument 1 of call to predicate `sdl2.sdl_delay'/3:<br>
> sdl2.m:067: type error: argument has type `int',<br>
> sdl2.m:067: expected type was `uint32'.<br>
> sdl2.m:067: The partial type assignment was:<br>
> sdl2.m:067: IsUp_4: bool.bool<br>
> sdl2.m:067: V_5: string<br>
> sdl2.m:067: STATE_VARIABLE_IO_0_7: io.state<br>
> sdl2.m:067: STATE_VARIABLE_IO_8: io.state<br>
> sdl2.m:067: V_9: string<br>
> sdl2.m:067: V_10: list.list(string.poly_type)<br>
> sdl2.m:067: STATE_VARIABLE_IO_11_11: io.state<br>
> sdl2.m:067: STATE_VARIABLE_IO_12_12: io.state<br>
> sdl2.m:067: STATE_VARIABLE_IO_19_19: io.state<br>
> sdl2.m:067: V_20: string<br>
> <br>
> i can see it's a type error but I thought that uint32 was a built in<br>
> mercury type that would translate into the C code without issue. ?!?!<br>
<br>
A bare literal like 42 is an int, though. I haven't looked at Haskell<br>
in years and years, so this might've changed, but I remember its<br>
networking library had a type called 'Port' which was an integer type<br>
without its own literal syntax, so you had to use fromIntegral to<br>
avoid a similar type error.<br>
<br>
> Sean.<br>
> _______________________________________________<br>
> users mailing list<br>
> <a href="mailto:users@lists.mercurylang.org" target="_blank">users@lists.mercurylang.org</a><br>
> <a href="https://lists.mercurylang.org/listinfo/users" rel="noreferrer" target="_blank">https://lists.mercurylang.org/listinfo/users</a><br>
_______________________________________________<br>
users mailing list<br>
<a href="mailto:users@lists.mercurylang.org" target="_blank">users@lists.mercurylang.org</a><br>
<a href="https://lists.mercurylang.org/listinfo/users" rel="noreferrer" target="_blank">https://lists.mercurylang.org/listinfo/users</a><br>
</blockquote></div>