1

如何实现等效于嵌套的一组if_s 的 switch 语句?

理想情况下是这样的(不要介意语法):

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

工作方式与此相同:

compatible(X, Y) :-
    if_(X = a1, 
        dif(Y, b2),
        if_(X = a2, 
            (dif(Y, c2), dif(Y, c3)),
            true
        )
    ).  
4

1 回答 1

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

例子

?- 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.
于 2021-07-17T13:00:42.257 回答