• Tidak ada hasil yang ditemukan

Course: IKI30820 : Pemrograman Logika

N/A
N/A
Protected

Academic year: 2017

Membagikan "Course: IKI30820 : Pemrograman Logika"

Copied!
14
0
0

Teks penuh

(1)

IKI30820 – Logic Programming

Cut

Slide 05

Ari Saptawijaya (slides) Adila A. Krisnadhi (LATEXadaptation) Fakultas Ilmu Komputer

Universitas Indonesia

(2)

Outline

1 What is Cut?

(3)

Motivation

Consider the following program

grade(X,’A’) :- X>=90.

grade(X,’B’) :- 80=<X, X<90. grade(X,’C’) :- 65=<X, X<80. grade(X,’Fail’) :- X<65.

Query:

?- grade(92,Grade), Grade == ’C’.

(4)

Prolog will automatically backtrack if this is necessary to satisfying a goal.

Automatic backtracking is a useful programming concept

◮ It relieves the programmer of the burden of backtracking explicitly On the other hand, uncontrolled backtracking may cause

inefficiency in a program

(5)

Controlling backtracking with cut

’Cut’ is written as the character ’!’

Second version of previous program (nowwith cut to control backtrack)

grade(X,’A’) :- X>=90,!.

grade(X,’B’) :- 80=<X, X<90, !. grade(X,’C’) :- 65=<X, X<80, !. grade(X,’Fail’) :- X<65.

(6)

The meaning of cut

The “parent goal” is the goal that matched the head of the clause containing the cut.

When the cut is encountered as a goal ◮ it succeeds immediately

◮ it commits the system to all choices made between the time the

parent goal was invoked and the time the cut was encountered

(7)

Third version of previous program:

grade(X,’A’) :- X>=90, !. grade(X,’B’) :- X>=80, !. grade(X,’C’) :- X>=65, !. grade(X,’Fail’).

Query:

grade(95,X).

Compare it with:

(8)

Outline

1 What is Cut?

(9)

Example

p(X) :- a(X). % rule1

p(X) :- b(X),c(X),d(X),e(X). % rule2

(10)
(11)

.. another example

Variant 1:

wizard(harry) :- !. wizard(ron).

wizard(hermione).

Variant 2:

wizard(harry). wizard(ron) :- !. wizard(hermione).

Variant 3:

(12)

Advantages of Cut ...

Often improve the efficiency of the program

◮ Explicitly tell Prolog: do not try other alternatives as they are bound

to fail.

(13)

Using cut for membership

member2(X,[X|_]) :- !.

(14)

Using cut to define set

set([],[]).

Referensi

Dokumen terkait

On the other hand, if you want to keep the incoming data, you may need to convert it to a format understood by Hadoop or other processing tools, such as Avro or Parquet.. The

On the other hand, students that already have the proper understanding may hesitate in asking for feedback from the teaching staff when they realise that the staff

On the other hand, analytic causative construction in Deli Javanese dialect can be formed by using the verbs nggawe ‘make’, a verb stating a cause either a condition or an action verb..

On the other hand, the determination of the level of health literacy and pattern of information seeking behaviour among T2DM patients may assist policymakers and healthcare providers in

On the other hand, transfer pricing, corporate social responsibility, and leverage do not affect tax-avoidance since those are not factors that cause companies to avoid taxes.. Most

In this way the theory of causation has been criticized by Hume on the basis of the absence of any necessary connection between the cause and effect.. Similarly on the other hand for

On the other hand, this may reflect the perception of the university, or more specifically, the sustainability office, that sustainability is principally concerned with environmental

They have rather a very mild haemolysis that by itself may be not severe enough to cause jaundice but a combination of G6PD deficiency and other risk factors may be necessary to make