Probably a stupid question, but I can't find any documentation anywhere for it. Is there a way to do an if in prolog, e.g. if a variable is 0, then to do some actions (write text to the terminal). An else isn't even needed, but I can't find any implementation of if.
相关问题
- Creating a SPARQL parameterized query using append
- How to join rules and print out outputs in prolog
- Splitting list and iterating in prolog
- Accumulating while in recursion/backtracking
- prolog trace how to use
相关文章
- What are the problems associated to Best First Sea
- How can I fix this circular predicate in Prolog?
- How to negate in Prolog
- Remove incorrect subsequent solutions without once
- prolog two lists are exactly the same
- Simplify Expressions in Prolog
- Check if any element's frequency is above a li
- Prolog — symetrical predicates
The else part is required
You should read Learn Prolog Now! Chapter 10.2 Using Cut. This provides an example:
max(X,Y,Z) :- X =< Y,!, Y = Z.
to be said,
Z
is equal toY
IF!
is true (which it always is) ANDX
is<= Y
.I found this helpful for using an if statement in a rule.
Thanks to http://cs.union.edu/~striegnk/learn-prolog-now/html/node89.html
The best thing to do is to use the so-called
cuts
, which has the symbol!
.The above is the basic structure of a condition function.
To exemplify, here's the
max
function:I suggest reading more documentation on cuts, but in general they are like breakpoints. Ex.: In case the first
max
function returns a true value, the second function is not verified.PS: I'm fairly new to Prolog, but this is what I've found out.
First, let's recall some classical first order logic:
How can we express "if-then-else" like that in Prolog?
Let's take the following concrete example:
We can match above pattern ("If P then Q else R") if ...
P
islist_member([1,2],X)
,non_P
isnon_member([1,2],X)
,Q
isX=2
, andR
isX=4
.To express list (non-)membership in a pure way, we define:
Let's check out different ways of expressing "if-then-else" in Prolog!
(P,Q ; non_P,R)
Correctness score 5/5. Efficiency score 3/5.
(P -> Q ; R)
Correctness score 2/5. Efficiency score 2/5.
(P *-> Q ; R)
Correctness score 3/5. Efficiency score 1/5.
(Preliminary) summary:
(P,Q ; non_P,R)
is correct, but needs a discrete implementation ofnon_P
.(P -> Q ; R)
loses declarative semantics when instantiation is insufficient.(P *-> Q ; R)
is "less" incomplete than(P -> Q ; R)
, but still has similar woes.Luckily for us, there are alternatives: Enter the logically monotone control construct
if_/3
!We can use
if_/3
together with the reified list-membership predicatememberd_t/3
like so:Correctness score 5/5. Efficiency score 4/5.
Yes, there is such a control construct in ISO Prolog, called
->
. You use it like this:Here is an example that uses a chain of else-if-clauses:
Note that if you omit the else-clause, the condition failing will mean that the whole if-statement will fail. Therefore, I recommend always including the else-clause (even if it is just
true
).