[m-users.] switch confusion with cc_multi

emacstheviking objitsu at gmail.com
Tue Aug 20 20:14:22 AEST 2019


I'll read and digest everybodies replies. Partially it's leanring curve
with Mercury I guess.
I have to stop thinking like a pattern matching Haskell coder at times and
an imperative C hacker the rest  :)

@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.


Once again, thank you all.


On Mon, 19 Aug 2019 at 19:40, Julian Fondren <jfondren at minimaltype.com>
wrote:

> On 2019-08-19 12:20, Zoltan Somogyi wrote:
> > The obvious way to handle that is to start with a type that has one
> > function
> > symbol for each of the events you know you need, with a complete switch
> > in the event loop on the values of that type. Then, as you find the
> > need for
> > more kinds of events, add them to the type and to the switch at the
> > same time.
> >
> > The event loop should look something like this:
> >
> > event_loop(!AppState, !IO) :-
> >     sdl_event(MaybeEvent, !IO),
> >     (
> >         MaybeEvent = no,
> >         Quit = no
> >     ;
> >         MaybeEvent = yes(Event),
> >         (
> >             Event = event_quit,
> >             Quit = yes
> >         ;
> >             Event = event_1(...),
> >             Quit = no,
> >             ... handle event 1 ...
> >         ;
> >             Event = event_2(...),
> >             Quit = no,
> >             ... handle event 2 ...
> >         ;
> >             ( Event = event_x(...)
> >             ; Event = event_y(...)
> >             ; Event = event_z(...)
> >             ),
> >             Quit = no,
> >             io.format("events x, y and z not yet implemented", !IO)
> >         ),
> >     ),
> >     (
> >         Quit = yes
> >     ;
> >         Quit = no,
> >         event_loop(!AppState, !IO)
> >     ).
> >
>
> It didn't occur to me until just now that this might work:
>
>    :- module semid2.
>    :- interface.
>    :- import_module io.
>    :- pred main(io::di, io::uo) is det.
>    :- implementation.
>
>    :- type events
>        --->    event_1
>        ;       event_2
>        ;       event_quit
>        ;       event_x
>        ;       event_y
>        ;       event_z.
>
>    main(!IO) :-
>        handle(event_z, !IO).
>
>    :- pred handle(events::in, io::di, io::uo) is det.
>    handle(event_quit, !IO).
>    handle(event_1, !IO).
>    handle(event_2, !IO).
>    handle(X, !IO) :-
>        ( X = event_x ; X = event_y ; X = event_z ),
>        io.write_string("events x, y and z not yet implemented\n", !IO).
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20190820/7087cae3/attachment.html>


More information about the users mailing list