[m-users.] switch confusion with cc_multi

Julian Fondren jfondren at minimaltype.com
Tue Aug 20 00:07:38 AEST 2019


On 2019-08-19 08:17, emacstheviking wrote:
> And here is the individual event processing.
> 
>  
> %--------------------------------------------------------------------------
>  %
>  % HandleA Single Event
>  %
> :- pred handle_event(sdl_event::in, app_state::in, app_state::out,
> io::di, io::uo) is det.
> handle_event(E, A, A, !IO) :-
>  ( if E = key_up(_A, _B) then
>  format("keyup:\n", [], !IO)
>  else if E = filedrop_event(F) then
>  format("File drop: %s\n", [s(F)], !IO)
> 
>  % ... more if then else checks as events are added
>  else
>  format("dont care:\n", [], !IO)
>  ).
> 
> Every time I use multiple predicates with (_,..) as the event it says
> I have multiple solutions but then I tried to make handle_event
> cc_multi as there can be only one event at a time and it can only be
> one type of event.

You can't have a 'don't care' case with a deterministic
disjunction. You can have a semidet disjunction though:

   :- module semid.
   :- interface.
   :- import_module io.
   :- pred main(io::di, io::uo) is det.
   :- implementation.

   :- type colors
       --->    red
       ;       white
       ;       blue
       ;       gold.

   main(!IO) :-
       io.print_line(colorcode(red), !IO).

   :- func colorcode(colors) = int.
   colorcode(C) = N :-
       ( if patriotic(C, N0) then
           N = N0
       else
           N = 0  % don't care
       ).

   :- pred patriotic(colors::in, int::out) is semidet.
   patriotic(red, 1).
   patriotic(white, 2).
   patriotic(blue, 3).



More information about the users mailing list