:-mode alternatives(+). alternatives(X):- q1(X), !, r1(X). alternatives(X):- q2(X), !, r2(X). alternatives(X):- qn(X), rn(X).
The errors which are introduced if a cut to commit to a choice is left out are very hard to debug, and may only show after long execution. Much better to always cut after each guard.
When adding new parameters it is important to ensure that they are added to all clauses of the predicate. If a parameter is not used in some clause, then it should be added as a singleton variable. If we miss an argument on one of the clauses in the middle, the compiler will create an error message about non consecutive clauses. But if we miss an argument for either the first or the last clause, the compiler will just treat this as another predicate definition with the same name, but a different arity. Errors of this form are very hard to spot.
:-mode interface_type(+,+,-). interface_type(_Node,local,local):- !. interface_type(Node,_Interface,backbone_net):- node(Node,net), !. interface_type(Node,Interface,backbone):- backbone_line(Node,Interface,_,_), !. interface_type(Node,Interface,backbone):- backbone_line(_,_,Node,Interface), !. interface_type(Node,Interface,interconnection):- group(interconnection,_,Node,Interface), !. interface_type(_Node,_Interface,customer).Here we branch on information passed in the first two arguments, and return a result in the last argument. The last clause is a default rule, saying that the interface type is customer, if none of the other rules applied.
Some programmers perfer to make the output unification explicit, like so:
:-mode interface_type(+,+,-). interface_type(_Node,local,Result):- !, Result = local. interface_type(Node,_Interface,Result):- node(Node,net), !, Result = backbone_net. interface_type(Node,Interface,Result):- backbone_line(Node,Interface,_,_), !, Result = backbone. interface_type(Node,Interface,Result):- backbone_line(_,_,Node,Interface), !, Result = backbone. interface_type(Node,Interface,Result):- group(interconnection,_,Node,Interface), !, Result = interconnection. interface_type(_Node,_Interface,Result):- Result = customer.This has advantages if the predicate may be called with the last argument instantiated.