[m-users.] IO argument clobbering

Volker Wysk post at volker-wysk.de
Tue Jul 18 00:37:51 AEST 2023


Am Montag, dem 17.07.2023 um 15:27 +0100 schrieb Sean Charles
(emacstheviking):
> Hi,

Hi there.

> 
> I am trying to pass a predicate to a rendering loop so the loop can manage
> the system but call back each frame to get the application to render its
> state.
> 
> I have this as the callback definition:
> 
> 
> :- type callback == (pred(world, world, io, io)).
> :- inst callback == (pred(in, out, di, uo) is det).
> 
> :- pred run_loop(
>     callback::in, world::in, world::out, io::di, io::uo
> ) is det.

This should be:

:- pred run_loop(
    callback::in(callback), world::in, world::out, io::di, io::uo
) is det.

You need a higher order inst, when you want to call a variable as a
predicate.


> My call site code looks like this, nothing spectacular:
> 
> :- pred app_loop(world::in, world::out, io::di, io::uo) is det.
> 
> app_loop(!W, !IO) :-
>  % do stuff
>  .
> 
> The game loop set up and call is this:
> 
>     mk_world(60, World, !IO),
>     run_loop(app_loop, World, _, !IO),
>     close_window(!IO).
> 
> The idea was that, at that point, the 'control' of the application is
> managed by my emerging framework, dare I call it that, and I wanted to be
> able to pass in various callbacks, starting with basic frame rendering,
> the implementation code is this:
> 
> run_loop(AppHandler, !W, !IO) :-
>     window_should_close(X, !IO),
>     (
>         X = yes,
>         trace_info("Stopped by user", [], !IO)
>     ;
>         X = no,
>            :
>            :
>         %%%%%%%%%%
>         % RENDER %
>         %%%%%%%%%%
>         begin_drawing(!IO),
> 193:        AppHandler(!W, !IO),
> 194:    end_drawing(!IO),
> 195:
> 196:    run_loop(AppHandler, !W, !IO)
>     ).
> 
> The compiler error centres around line 196, which I wasn't expecting, of
> course!
> 
> ui.m:196: In clause for `run_loop(in, in, out, di, uo)':
> ui.m:196:   mode error in conjunction. The next 3 error messages indicate
> ui.m:196:   possible causes of this error.
> ui.m:196:   
> ui.m:193:   In clause for `run_loop(in, in, out, di, uo)':
> ui.m:193:   in argument 1 (i.e. the predicate term) of higher-order
> predicate
> ui.m:193:   call:
> ui.m:193:   mode error: variable `AppHandler' has instantiatedness
> `ground',
> ui.m:193:   expecting higher-order pred inst of arity 4.
> ui.m:196:   
> ui.m:194:   In clause for `run_loop(in, in, out, di, uo)':
> ui.m:194:   in argument 1 of call to predicate `raylib.end_drawing'/2:
> ui.m:194:   unique-mode error: the called procedure would clobber its
> argument,
> ui.m:194:   but variable `STATE_VARIABLE_IO_29' is still live.
> ui.m:196:   
> ui.m:196:   In clause for `run_loop(in, in, out, di, uo)':
> ui.m:196:   in argument 4 of call to predicate `ui.run_loop'/5:
> ui.m:196:   unique-mode error: the called procedure would clobber its
> argument,
> ui.m:196:   but variable `STATE_VARIABLE_IO_30' is still live.
> 
> 
> I don't understand what it means by 'still live'. I am threading the !IO
> world state through this run loop, passing it into the callback code, but
> on the tail-back into the next iteration, well, I don't understand what
> the actual issue is.
> 
> Seeking explanation!


Cheers,
Volker
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <http://lists.mercurylang.org/archives/users/attachments/20230717/80d64d15/attachment.sig>


More information about the users mailing list