If else if else in a prolog similar to C / C ++

In c, I have something like:

if(cond1) {} else if(cond2) {} else {} 

How is this possible in Prolog?

+6
source share
4 answers
 ( If1 -> Then1 ; If2 -> Then2 ; ... ; otherwise ). 

Please note that if-then-else is only necessary if you cannot express different conditions by matching patterns in different sentences. Everything that can be expressed by pattern matching must be expressed by pattern matching, as this usually leads to a more general and more efficient code.

+8
source
 (cond1 -> consequent1 ; cond2 -> consequent2 ; alternative ) 

For the record, this is called conditional.

+5
source

The β†’ / 2 is required only if you want to impose a certain determinism. It acts as a local incision. But if you want your code to maintain some non-determinism, there is no need to use β†’ / 2.

Take the following imperative code:

 boolean listOfBool(Object obj) { if (obj instanceof ConsCell) { if (((ConsCell)ob).head() instanceof Boolean) { return listOfBool(((ConsCell)ob).tail()); } else { return false; } } else if (obj == null) { return true; } else { return false; } 

}

This can be encoded in Prolog without β†’ / 2 as follows:

 % bool(+Bool) % bool(-Bool) bool(0). bool(1). % list_of_bool(+List) % list_of_bool(-List) list_of_bool(L) :- (L = [X|Y], bool(X), list_of_bool(Y); L = []). 

The advantage is that it can be used to check boolean lists and generate boolean lists:

 ?- list_of_bool([0,1,0]). Yes ?- list_of_bool([0,1,2]). No ?- List=[_,_,_], list_of_bool(List). List = [0, 0, 0] ; List = [0, 0, 1] ; List = [0, 1, 0] ; List = [0, 1, 1] Etc.. 

In the general case, the disjunction (;) / 2 can be distributed over multiple entries. If this is combined with moving the union (=) / 2 to the head, then you can get some speed, since then the predicate is usually better indexed.

Here's what an alternative list_of_bool would look like, for example, by excluding (;) / 2 and (=) / 2:

 % list_of_bool2(+List) % list_of_bool2(-List) list_of_bool2([X|Y]) :- bool(X), list_of_bool2(Y). list_of_bool2([]). 

The above works in exactly the same way (it really works better since there is no choice point left in the first request that (;) / 2 is usually not detected without (->) / 2):

 ?- list_of_bool2([0,1,0]). Yes ?- list_of_bool2([0,1,2]). No ?- List=[_,_,_], list_of_bool(List). List = [0, 0, 0] ; List = [0, 0, 1] ; List = [0, 1, 0] ; List = [0, 1, 1] Etc.. 

It is also possible to launch Prolog. Only with the rules and no disjunction (;) / 2 and without unification (=) / 2. the later two already exist in the basic positions of Horn.

Suppose you have a Prolog without (;) / 2 and no (=) / 2, and you do not need a transparent (;) / 2 section, then you can define these constructions by yourself:

 X = X. (A ; _) :- A. (_ ; B) :- B. 

Bye

Horn Point
http://en.wikipedia.org/wiki/Horn_clause

+2
source

This is not very easy to find, in part because (as @mat noted) there is an idiomatic alternative in Prolog. You can find the SWI-Prolog Documentation here , although it is too purely, that’s for sure. I quote the relevant question:

Note that (If β†’ Then) acts like (If β†’ Then; fail), which makes the construct fail if the condition fails. This unusual semantics is part of the ISO standard and all de facto Prolog standards.

+1
source

Source: https://habr.com/ru/post/913027/


All Articles