Language Reference/Clauses

From wiki.visual-prolog.com

< Language Reference

Revision as of 09:47, 26 October 2012 by Thomas Linder Puls (talk | contribs) (consistenty)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

See also Guards in 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.

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

See also Guards in Monitors.

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.