Difference between revisions of "Language Reference/Clauses"
m (1 revision(s)) |
m (consistenty) |
||
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
{{languageReferenceNavbar|Clauses}} | {{languageReferenceNavbar|Clauses}} | ||
== Clauses | === Clauses Sections === | ||
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 == | See also {{lang2|Monitors|Guards|Guards}} in {{lang|Monitors|Monitors}}. | ||
=== 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 38: | ||
:- <Term></vipbnf> | :- <Term></vipbnf> | ||
== Goal Section == | See also {{lang2|Monitors|Guards|Guards}} in {{lang|Monitors|Monitors}}. | ||
=== 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. | ||
Line 45: | Line 49: | ||
The <vp>goal</vp> section consists of a clause body. The <vp>goal</vp> section defines its own scope, therefore all invocations should contain class qualifications. | The <vp>goal</vp> section consists of a clause body. The <vp>goal</vp> section defines its own scope, therefore all invocations should contain class qualifications. | ||
The goal must have <vp>procedure</vp> mode. |
Latest revision as of 09:47, 26 October 2012
Clauses Sections
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.
The goal must have procedure mode.