Skip to content

Latest commit

 

History

History
72 lines (49 loc) · 3.23 KB

NOTES.md

File metadata and controls

72 lines (49 loc) · 3.23 KB

https://amzi.com/AdventureInProlog/a3simple.php#Chapter3

Prolog queries work by pattern matching. The query pattern is called a goal. If there is a fact that matches the goal, then the query succeeds and the listener responds with 'yes.' If there is no matching fact, then the query fails and the listener responds with 'no.'

Prolog's pattern matching is called unification. In the case where the logicbase contains only facts, unification succeeds if the following three conditions hold.

  • The predicate named in the goal and logicbase are the same.
  • Both predicates have the same arity.
  • All of the arguments are the same.

https://amzi.com/AdventureInProlog/a4comqry.php#Chapter4

Simple goals can be combined to form compound queries. For example, we might want to know if there is anything good to eat in the kitchen. In Prolog we might ask

?- location(X, kitchen), edible(X).

Whereas a simple query had a single goal, the compound query has a conjunction of goals. The comma separating the goals is read as "and."

https://amzi.com/AdventureInProlog/a5rules.php

We said earlier a predicate is defined by clauses, which may be facts or rules. A rule is no more than a stored query. Its syntax is

head :- body.

where

head a predicate definition (just like a fact)

:- the neck symbol, sometimes read as "if"

body one or more goals (a query)

With rules, Prolog unifies the goal pattern with the head of the clause. If unification succeeds, then Prolog initiates a new query using the goals in the body of the clause.

Rules, in effect, give us multiple levels of queries. The first level is composed of the original goals. The next level is a new query composed of goals found in the body of a clause from the first level.

Each level can create even deeper levels. Theoretically, this could continue forever. In practice it can continue until the listener runs out of space.

Unification

Backtracking

Other