140 likes | 337 Views
Prolog. A Prolog "program" is a collection of facts and rules. This collection is sometimes called a knowledge base . Facts represent unconditional assertions. Rules represent conditional assertions. Prolog facts.
E N D
Prolog • A Prolog "program" is a collection of facts and rules. • This collection is sometimes called a knowledge base. • Facts represent unconditional assertions. • Rules represent conditional assertions.
Prolog facts • A fact is a relational expression -- a predicate name followed by a parenthesized argument list and a period, e.g. • mother(liz, chuck). • Each of these arguments must be a term.
Prolog terms • A term represents an entity (as opposed to a relation or rule). • Terms may be variables, literals, or structures. • Variables are those terms whose name begins with a capital letter or underscore.
Prolog rules • A rule consists of (in order): • a head, a neck, a body, and a period. • The head is a relational expression, and represents a conclusion. • The body represents an assumption. • The neck means "if", and is spelled • :=
A sample rule • One legal rule is • parent(X,Y):-mother(X,Y). • This rule says that X is the parent of Y if X is the mother of Y.
Quantification • All variables in a fact are quantified universally. • Variables appearing to the left of the neck in a rule are quantified universally. • Other variables are quantified existentially.
Conjunction and disjunction • A comma is used to represent conjunction of expressions. • This is common in bodies (but not heads) of rules. • Disjunction is represented by a semicolon. • But disjunctive assumptions are more naturally represented by multiple rules that share a head.
Multiple rules sharing a head • Another legal rule is • parent(X,Y):-father(X,Y). • This can be added to the earlier rule • parent(X,Y):-mother(X,Y). • Both rules together say that X is the parent of Y if X is the father or the mother of Y.
Logical operators • There are some subtle differences between the logical operators and, or, and not and their Prolog equivalents. • These differences are somewhat similar to those seen in short-circuit evaluation.
Queries • Queries are answered based not just on what's explicitly in the knowledge base, but also on what can be inferred from the knowledge base. • The simplest form of Prolog query is syntactically identical to a fact, e.g. • mother(X, chuck). • Any argument may be a variable.
Answering queries • The Prolog interpreter searches for a proof that some collection of bindings for the variables satisfies the query. • To satisfy a query means to makes the query true. • If such a collection of bindings is found, the interpreter will return it, e.g. • X = liz
Multiple answers • After getting one answer, a user may ask for another answer, or quit. • A semicolon is used to continue • A return is used to quit • If there are no variables in the query, the interpreter will just answer yes or no.
Bidirectionality • There is no directionality in a query. • Each of the following queries is acceptable, and can be answered • mother(liz, chuck). • mother(X, chuck). • mother(liz, X). • mother(X, Y).
Data types • Prolog has numeric and atomic types (like Lisp symbols) for literals. • Variables are untyped. • A list data type is available.