Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "!" in Prolog

Could somebody explain me what does "!" do in Prolog ? I don't understand it. Here I have a code that counts how many sublists of a heterogeneous list have mountain aspect.

nrSubliste([], 0).
nrSubliste([H|T], R):-
    is_list(H),
    munteMain(H),!,
    nrSubliste(T, R1),
    R is R1 + 1.
nrSubliste([_|T], R):-
    nrSubliste(T, R).

munteMain verifies if a linear list has a mountain aspect.

like image 650
LauraW Avatar asked Jan 09 '16 12:01

LauraW


People also ask

What is Prolog in simple words?

Prolog | An Introduction. Introduction : Prolog is a logic programming language. It has important role in artificial intelligence. Unlike many other programming languages, Prolog is intended primarily as a declarative programming language. In prolog, logic is expressed as relations (called as Facts and Rules).

What is the Prolog operator in JavaScript?

The prolog operator is a function to operate and works on the programming operand or variable. The prolog operator is a symbolic character to work arithmetic, logical, and comparison operations.

Is Prolog declarative or imperative?

However, other languages like Prolog, have declarative and also imperative properties. This may also include procedural statements like “To solve the problem H, perform B1, B2 and B3”. ALF (algebraic logic functional programming language).

What is Prolog's run time system?

The prolog's run time system provides the service of an interface engine. A basic logic programming environment has no literal values. An identifier with upper case letters and other identifiers denote variables. Identifiers that start with lower-case letters denote data values.


1 Answers

Exclamation point ! denotes Cut in Prolog, a special goal that always succeeds, and blocks backtracking for all branches above it that may have alternatives.

In your case it means that once a solution to munteMain/1 has been found, the program will never backtrack and look for an alternative solution. Specifically, Prolog will never consider the third clause of your nrSubliste/2 rule, i.e. the one ignoring list head with _, if H in the second clause is such that munteMain(H) succeeds.

Note that using ! makes your code is somewhat harder to read and maintain, because the logic in the third clause depends on the logic of the second clause. You can rewrite your program without a cut using the not provable operator \+:

nrSubliste([H|T], R):-
    is_list(H),
    munteMain(H),
    nrSubliste(T, R1),
    R is R1 + 1.

nrSubliste([H|T], R):-
    is_list(H),
    \+ munteMain(H),
    nrSubliste(T, R).
like image 113
Sergey Kalinichenko Avatar answered Oct 01 '22 18:10

Sergey Kalinichenko