1

How can I implement a switch statement equivalent to a nested set of if_s?

Ideally something like (don't mind the syntax):

compatible(X, Y) :-
    switch X
    a1 -> dif(Y, b2),
    a2 -> dif(Y, c2), dif(Y, c3),
    _  -> true 

working the same way as this one:

compatible(X, Y) :-
    if_(X = a1, 
        dif(Y, b2),
        if_(X = a2, 
            (dif(Y, c2), dif(Y, c3)),
            true
        )
    ).  
false
  • 10,264
  • 13
  • 101
  • 209
vasily
  • 2,850
  • 1
  • 24
  • 40

1 Answers1

-2
:- module(switch_, []).

:- use_module(library(reif)).

:- multifile goal_expansion/2.

user:goal_expansion(switch_(X, ;(->(H, C), T)), if_(X = H, C, switch_(X, T))).
user:goal_expansion(switch_(X, ->(H, C)),       if_(X = H, C, fail)).
user:goal_expansion(switch_(_, true),           true).
user:goal_expansion(switch_(_, false),          false).
:- use_module(switch_).

likes(A, B) :-
   switch_(A, (
      john -> B = mary ;
      mary -> dif(B, john) ;
      true
   )).

Example

?- likes(A, B).
A = john,
B = mary ;
A = mary,
dif(B, john) ;
dif(A, mary),
dif(A, john).

?- likes(mary, B).
dif(B, john).

?- likes(john, B).
B = mary.
vasily
  • 2,850
  • 1
  • 24
  • 40
  • 1
    `[H -> C|T]` is invalid syntax. [Only](https://www.complang.tuwien.ac.at/ulrich/iso-prolog/conformity_testing#75) SWI accepts it. – false Jul 17 '21 at 19:15
  • 1
    @false, I've changed it a bit, both SWI and Scryer are happy with the syntax now. – vasily Jul 18 '21 at 00:00
  • You will need a fitting meta_predicate declaration. But currently `:- meta_predicate( switch_(?, 0)).` will produce an error. So some reconsideration is inevitable. Further, remark that the last case with `_` will never resolve with `(=)/3`. – false Jul 18 '21 at 13:27
  • @false could you give me an example where it fails? – vasily Jul 18 '21 at 23:45
  • *You* need to add mentioned declaration to see that there is an error (in SWI). If it's not clear, say `make`. – false Jul 19 '21 at 06:41