Difference between revisions of "Language Reference/Clauses"

From wiki.visual-prolog.com
m (1 revision(s))
m (header levels)
Line 1: Line 1:
{{languageReferenceNavbar|Clauses}}
{{languageReferenceNavbar|Clauses}}


== Clauses Section ==
=== Clauses Section ===


A <vp>clauses</vp> section consists of a set of clauses. The <vp>clauses</vp> section contains implementations of predicates or initial values of facts.
A <vp>clauses</vp> section consists of a set of clauses. The <vp>clauses</vp> section contains implementations of predicates or initial values of facts.
Line 10: Line 10:
   clauses <Clause>-dot-term-list-opt</vipbnf>
   clauses <Clause>-dot-term-list-opt</vipbnf>


== Clauses ==
=== Clauses ===


Clauses are used to define predicates. A single predicate is defined by a set of clauses. Each clause is executed in turn until one of them '''''succeeds''''', or there is no more clauses left to execute. If no clause succeeds the predicate '''''fails'''''.
Clauses are used to define predicates. A single predicate is defined by a set of clauses. Each clause is executed in turn until one of them '''''succeeds''''', or there is no more clauses left to execute. If no clause succeeds the predicate '''''fails'''''.
Line 36: Line 36:
   :- <Term></vipbnf>
   :- <Term></vipbnf>


== Goal Section ==
=== Goal Section ===


The <vp>goal</vp> section is the entry to a program. When the program starts it executes the <vp>goal</vp>, when the <vp>goal</vp> is executed, the program exits.
The <vp>goal</vp> section is the entry to a program. When the program starts it executes the <vp>goal</vp>, when the <vp>goal</vp> is executed, the program exits.

Revision as of 00:12, 15 October 2008

Clauses Section

A clauses section consists of a set of clauses. The clauses section contains implementations of predicates or initial values of facts.

A single clause section can have clauses for several predicates and facts. On the other hand, all clauses for one predicate/fact (the same name and arity) must be grouped together in one clauses section and without intervening clauses of other predicates/facts.

ClausesSection :
   clauses Clause-dot-term-list-opt

Clauses

Clauses are used to define predicates. A single predicate is defined by a set of clauses. Each clause is executed in turn until one of them succeeds, or there is no more clauses left to execute. If no clause succeeds the predicate fails.

If a clause succeeds and there are more relevant clauses in a predicate left, the program control can later backtrack to the clauses of this predicate to search for other solutions.

Thus, a predicate can fail, succeed, and even succeed multiple times.

Each clause has a head and optionally a body.

When a predicate is called the clauses are tried in turn (from top to bottom). For each clause the head is unified with the arguments from the call. If this unification succeeds then the body of the clause (if such one exist) is executed. The clause succeeds if the match of the head succeeds and the body succeeds. Otherwise it fails.

A clause consists of a head and an optional body.

Clause :
   ClauseHead ReturnValue-opt ClauseBody-opt .
ClauseHead :
   LowercaseIdentifier ( Term-comma-sep-list-opt )
ReturnValue :
   = Term
ClauseBody :
   :- Term

Goal Section

The goal section is the entry to a program. When the program starts it executes the goal, when the goal is executed, the program exits.

GoalSection :
   goal Term.

The goal section consists of a clause body. The goal section defines its own scope, therefore all invocations should contain class qualifications.

Normally, the goal must have procedure mode.