<div dir="ltr">I'll read and digest everybodies replies. Partially it's leanring curve with Mercury I guess.<div>I have to stop thinking like a pattern matching Haskell coder at times and an imperative C hacker the rest :)</div><div><br></div><div>@Zoltan "event_loop" is the name because it is called from inside a "loop", ultimately it will be called event_handler() or something; names change over time as the perception and understanding changes over time. Also, "Every time I use multiple predicates with (_,..)" was meant to indicate (inadequately, clearly) that when I declare multiple predicate handlers for a "det" predicate that is using !IO, it doesn't work; I am forced to use the if-then-else construct and I am close but not 100% to understanding why.<br></div><div><br></div><div><br></div><div>Once again, thank you all.</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, 19 Aug 2019 at 19:40, 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-08-19 12:20, Zoltan Somogyi wrote:<br>
> The obvious way to handle that is to start with a type that has one <br>
> function<br>
> symbol for each of the events you know you need, with a complete switch<br>
> in the event loop on the values of that type. Then, as you find the <br>
> need for<br>
> more kinds of events, add them to the type and to the switch at the <br>
> same time.<br>
> <br>
> The event loop should look something like this:<br>
> <br>
> event_loop(!AppState, !IO) :-<br>
> sdl_event(MaybeEvent, !IO),<br>
> (<br>
> MaybeEvent = no,<br>
> Quit = no<br>
> ;<br>
> MaybeEvent = yes(Event),<br>
> (<br>
> Event = event_quit,<br>
> Quit = yes<br>
> ;<br>
> Event = event_1(...),<br>
> Quit = no,<br>
> ... handle event 1 ...<br>
> ;<br>
> Event = event_2(...),<br>
> Quit = no,<br>
> ... handle event 2 ...<br>
> ;<br>
> ( Event = event_x(...)<br>
> ; Event = event_y(...)<br>
> ; Event = event_z(...)<br>
> ),<br>
> Quit = no,<br>
> io.format("events x, y and z not yet implemented", !IO)<br>
> ),<br>
> ),<br>
> (<br>
> Quit = yes<br>
> ;<br>
> Quit = no,<br>
> event_loop(!AppState, !IO)<br>
> ).<br>
> <br>
<br>
It didn't occur to me until just now that this might work:<br>
<br>
:- module semid2.<br>
:- interface.<br>
:- import_module io.<br>
:- pred main(io::di, io::uo) is det.<br>
:- implementation.<br>
<br>
:- type events<br>
---> event_1<br>
; event_2<br>
; event_quit<br>
; event_x<br>
; event_y<br>
; event_z.<br>
<br>
main(!IO) :-<br>
handle(event_z, !IO).<br>
<br>
:- pred handle(events::in, io::di, io::uo) is det.<br>
handle(event_quit, !IO).<br>
handle(event_1, !IO).<br>
handle(event_2, !IO).<br>
handle(X, !IO) :-<br>
( X = event_x ; X = event_y ; X = event_z ),<br>
io.write_string("events x, y and z not yet implemented\n", !IO).<br>
</blockquote></div>