You are on page 1of 109

ARTIFICIAL INTELLIGENCE

Dr. Anand M
Assistant Professor
Department of Data Science and Business Systems
SRM Institute of Science and Technology
Adversarial Search
Game Playing
Games
• Multi agent environments : any given agent will need to consider the actions of
other agents and how they affect its own welfare.
• The unpredictability of these other agents can introduce many possible
contingencies

• There could be competitive or cooperative environments

• Competitive environments, in which the agent’s goals are in conflict require


adversarial search – these problems are called as games
What kind of games?
• Abstraction: To describe a game we must capture every relevant aspect of the
game. Such as:
• Chess
• Tic-tac-toe
• …
• Accessible environments: Such games are characterized by perfect information
• Search: game-playing then consists of a search through possible game positions
• Unpredictable opponent: introduces uncertainty thus game-playing must deal
with contingency problems
Type of Games
Games
• In game theory (economics), any multi-agent environment (either cooperative or competitive) is a
game provided that the impact of each agent on the other is significant*

• AI games are a specialized kind - deterministic, turn taking, two-player, zero sum games of perfect
information

• a zero-sum game is a mathematical representation of a situation in which a participant's gain (or loss) of
utility is exactly balanced by the losses (or gains) of the utility of other participant(s)

• In our terminology – deterministic, fully observable environments with two agents whose actions
alternate and the utility values at the end of the game are always equal and opposite (+1 and –1)
• If a player wins a game of chess (+1), the other player necessarily loses (-1)

• * Environments with very many agents are best viewed as economies rather than games
Deterministic Games
• Many possible formalizations, one is:
• States: S (start at s0)
• Players: P={1...N} (usually take turns)
• Actions: A (may depend on player / state)
• Transi on Func on: SxA →S
• Terminal Test: S → {t,f}
• Terminal U li es: SxP → R

• Solu on for a player is a policy: S → A


Games vs. search problems
• “Unpredictable" opponent  solution is a strategy specifying a move for every
possible opponent reply

• Time limits  unlikely to find goal, must approximate

• Plan of attack:
• Computer considers possible lines of play (Babbage, 1846)
• Algorithm for perfect play (Zermelo, 1912; Von Neumann, 1944)
• Finite horizon, approximate evaluation (Zuse, 1945; Wiener, 1948; Shannon, 1950)
• First chess program (Turing, 1951)
• Machine learning to improve evaluation accuracy (Samuel, 1952-57)
• Pruning to allow deeper search (McCarthy, 1956)
Deterministic Single-Player?
• Deterministic, single player, perfect information:
• Know the rules
• Know what actions do
• Know when you win
• E.g. Freecell, 8-Puzzle, Rubik’s cube
• … it’s just search!
• Slight reinterpretation:
• Each node stores a value: the best outcome it can
reach
• This is the maximal outcome of its children (the max
value)
• Note that we don’t have path sums as before
(utilities at end)
• After search, can pick move that leads to best node
Deterministic Two-Player
• E.g. tic-tac-toe, chess, checkers
• Zero-sum games
• One player maximizes result
• The other minimizes result
• Minimax search
• A state-space search tree
• Players alternate
• Each layer, or ply, consists of a round of moves
• Choose move to position with highest minimax value
= best achievable utility against best play
Searching for the next move
• Complexity: many games have a huge search space
• Chess: b = 35, m=100 nodes = 35 100
• if each node takes about 1 ns to explore then each move will take about 1050 millennia to
calculate.

• Resource (e.g., time, memory) limit: optimal solution not feasible/possible, thus
must approximate

• 1. Pruning: makes the search more efficient by discarding portions of the search
tree that cannot improve quality result.

• 2. Evaluation functions: heuristics to evaluate utility of a state without exhaustive


search.
Two-player Games
• A game formulated as a search problem:
Example: Tic-Tac-Toe
The minimax algorithm
• Perfect play for deterministic environments with perfect information

• Basic idea: choose move with highest minimax value


= best achievable payoff against best play

• Algorithm:
1. Generate game tree completely
2. Determine utility of each terminal state
3. Propagate the utility values upward in the three by applying MIN and MAX operators on the
nodes in the current level
4. At the root node use minimax decision to select the move with the max (of the min) utility
value

• Steps 2 and 3 in the algorithm assume that the opponent will play perfectly.
Generate Game Tree
Minimax Example
Minimax value
• Given a game tree, the optimal strategy can be determined by
examining the minimax value of each node (MINIMAX-VALUE(n))

• The minimax value of a node is the utility of being in the


corresponding state, assuming that both players play optimally from
there to the end of the game

• Given a choice, MAX prefer to move to a state of maximum value,


whereas MIN prefers a state of minimum value
Minimax: Recursive implementation
The Minimax Algorithm Properties
• Performs a complete depth-first exploration of the game tree
• Optimal against a perfect player.
• Time complexity?
• O(bm)
• Space complexity?
• O(bm)
• For chess, b ~ 35, m ~ 100
• Exact solution is completely infeasible
• But, do we need to explore the whole tree?
• Minimax serves as the basis for the mathematical analysis of games and for more
practical algorithms
Resource Limits
• Cannot search to leaves
• Depth-limited search
• Instead, search a limited depth of tree
• Replace terminal utilities with an eval function for non-
terminal positions
• Guarantee of optimal play is gone
• More plies makes a BIG difference
• Example:
• Suppose we have 100 seconds, can explore 10K nodes / sec
• So can check 1M nodes per move
• α-β reaches about depth 8 – decent chess program
α-β pruning
α-β pruning: example
α-β pruning: example
α-β pruning: example
α-β pruning: example
α-β pruning: example
α-β pruning: example
α-β pruning: example
α-β pruning: example
α-β pruning: General Principle
Why is it called α-β?
• α is the value of the best (i.e.,
highest-value) choice found so far
at any choice point along the path
for max

• If v is worse than α, max will avoid
it

 prune that branch

• Define β similarly for min



α-β pruning
• Alpha-beta search updates the values of α and β as it goes along and
prunes the remaining branches at a node as soon as the value of the
current node is known to be worse than the current α or β value for
MAX or MIN, respectively.

• The effectiveness of alpha-beta pruning is highly dependent on the


order in which the successors are examined.
Properties of α-β
• Pruning does not affect final result

• Good move ordering improves effectiveness of pruning


• With "perfect ordering," time complexity = O(bm/2)


 doubles depth of search

• A simple example of the value of reasoning about which computations are


relevant (a form of metareasoning)

The α-β algorithm
The α-β algorithm
Games : State-of-the-Art
• Checkers: Chinook ended 40-year-reign of human world champion Marion Tinsley in 1994. Used an
endgame database defining perfect play for all positions involving 8 or fewer pieces on the board, a
total of 443,748,401,247 positions. Checkers is now solved!
• Chess: Deep Blue defeated human world champion Gary Kasparov in a six-game match in 1997. Deep
Blue examined 200 million positions per second, used very sophisticated evaluation and undisclosed
methods for extending some lines of search up to 40 ply. Current programs are even better, if less
historic.
• Othello: In 1997, Logistello defeated human champion by six games to none. Human champions
refuse to compete against computers, which are too good.
• Go: Human champions are beginning to be challenged by machines, though the best humans still
beat the best machines. In Go, b > 300, so most programs use pattern knowledge bases to suggest
plausible moves, along with aggressive pruning.
• Backgammon: Neural-net learning program TDGammon one of world’s top 3 players.
Knowledge, Reasoning, and Planning
A Model-Based Agent
Knowledge and Reasoning
Knowledge and Reasoning:
humans are very good at acquiring new information by
combining raw knowledge, experience with reasoning.
AI-slogan: “Knowledge is power” (or “Data is power”?)
Examples:
Medical diagnosis --- physician diagnosing a patient
infers what disease, based on the knowledge he/she
acquired as a student, textbooks, prior cases
Common sense knowledge / reasoning ---
common everyday assumptions / inferences
e.g., “lecture starts at four” infer pm not am;
when traveling, I assume there is some way to get from the
airport to the hotel.
Logical agents:
Agents with some representation of the
complex knowledge about the world / its environment,
and uses inference to derive new information from that
knowledge combined with new inputs (e.g. via perception).

Key issues:
1- Representation of knowledge
What form? Meaning / semantics?
2- Reasoning and inference processes
Efficiency.
Knowledge-base Agents
• Key issues:
• Representation of knowledge  knowledge base
• Reasoning processes  inference/reasoning

Knowledge base = set of sentences in a formal language representing facts about the world(*)

(*) called Knowledge Representation (KR) language


Knowledge
• Key aspects:
bases
• How to add sentences to the knowledge base
• How to query the knowledge base

Both tasks may involve inference – i.e. how to derive new sentences from old sentences

Logical agents – inference must obey the fundamental requirement that when one asks a
question to the knowledge base, the answer should follow from what has been told to the
knowledge base previously. (In other words the inference process should not “make
things” up…)
A simple knowledge-based agent

• The agent must be able to:


• Represent states, actions, etc.
• Incorporate new percepts
• Update internal representations of the world
• Deduce hidden properties of the world
• Deduce appropriate actions

KR language candidate:
logical language (propositional / first-order) combined
with a logical inference mechanism

How close to human thought? (mental-models / Johnson-Laird).

What is “the language of thought”?

Why not use natural language (e.g. English)?

Greeks / Boole / Frege --- Rational thought: Logic?

We want clear syntax & semantics (well-defined


meaning), and, mechanism to infer new information.
Soln.: Use a formal language.
Consider: to-the-right-of(x,y)
Procedural style:

Knowledge-based alternative:
The “symbol grounding problem.”
Representations and Mappings
• To solve complex problems encountered in artificial intelligence, one
needs both a large amount of knowledge and some mechanism for
manipulating that knowledge to create solutions.
• Knowledge and Representation are two distinct entities. They play
central but distinguishable roles in intelligent system.
• Knowledge is a description of the world. It determines a system’s
competence by what it knows.
• Representation is the way knowledge is encoded. It defines a system’s
performance in doing something.
Representations and Mappings
• The knowledge Representation models/mechanisms are often based on:
• Logic
• Rules
• Frames
• Semantic Net
• Knowledge is categorized into two major types.
• A variety of ways of representing knowledge have been exploited in AI
programs.
• There are two different kinds of entities, we are dealing with.
• Facts: Truth in some relevant world. Things we want to represent.
• Representation of facts in some chosen formalism. Things we will actually be able to
manipulate.
Representations and Mappings
• These entities are structured at two levels:
• The knowledge level, at which facts are described.
• The symbol level, at which representation of objects are defined in terms of
symbols that can be manipulated by programs.
Note: KB defines exactly the set of worlds we are interested
in.

I.e.: Models(KB) Models( )


Note: (1) This was Aristotle’s original goal ---
Construct infallible arguments based purely
on the form of statements --- not on the “meaning”
of individual propositions.
(2) Sets of models can be exponential size or worse,
compared to symbolic inference (deduction).
Predicate Calculus
• Introduction through an example (Zohar Manna, 1974):
• Problem: A, B and C belong to the Himalayan club. Every member in the club
is either a mountain climber or a skier or both. A likes whatever B dislikes and
dislikes whatever B likes. A likes rain and snow. No mountain climber likes
rain. Every skier likes snow. Is there a member who is a mountain climber and
not a skier?
• Given knowledge has:
• Facts
• Rules
Predicate Calculus: Example contd.
• Let mc denote mountain climber and sk denotes skier. Knowledge representation in the given
problem is as follows:
1. member(A)
2. member(B)
3. member(C)
4. ∀x[member(x) → (mc(x) ∨ sk(x))]
5. ∀x[mc(x) → ~like(x,rain)]
6. ∀x[sk(x) → like(x, snow)]
7. ∀x[like(B, x) → ~like(A, x)]
8. ∀x[~like(B, x) → like(A, x)]
9. like(A, rain)
10. like(A, snow)
11. Question: ∃x[member(x) ∧ mc(x) ∧ ~sk(x)]
• We have to infer the 11th expression from the given 10.
• Done through Resolution Refutation.
Inferencing in Predicate Calculus

• Forward chaining
• Given P, , to infer Q
• P, match L.H.S of PQ
• Assert Q from R.H.S
• Backward chaining
• Q, Match R.H.S of
PQ
• assert P
• Check if P exists
• Resolution – Refutation
• Negate goal
• Convert all pieces of knowledge into clausal form (disjunction of literals)
• See if contradiction indicated by null clause can be derived
Terminology
• Pair of clauses being resolved is called the Resolvents.
The resulting clause is called the Resolute.
• Choosing the correct pair of resolvents is a matter of
search.
Forward & Backward Chaining
•Rule : man(x)  mortal(x)
•Data : man(Shakespeare)
To prove : mortal(Shakespeare)

•Forward Chaining:
man(Shakespeare) matches LHS of Rule.
X = Shakespeare
 mortal( Shakespeare) added
-Forward Chaining used by design expert systems

•Backward Chaining: uses RHS matching


- Used by diagnostic expert systems
Predicate Calculus
• Well Known Example:

• Man is mortal : rule


∀x[man(x) → mortal(x)]

• shakespeare is a man
man(shakespeare)

• To infer shakespeare is mortal


mortal(shakespeare)
Forward Chaining/ Inferencing
• man(x) → mortal(x)
• Dropping the quantifier, implicitly Universal quantification assumed
• man(shakespeare)
• Goal mortal(shakespeare)
• Found in one step
• x = shakespeare, unification
Backward Chaining/ Inferencing

• man(x) → mortal(x)
• Goal mortal(shakespeare)
• x = shakespeare
• Travel back over and hit the fact asserted
• man(shakespeare)
Resolution - Refutation

• man(x) → mortal(x)
• Convert to clausal form
• ~man(shakespeare) mortal(x)

• Clauses in the knowledge base
• ~man(shakespeare) mortal(x)
• man(shakespeare) 
• mortal(shakespeare)
Resolution – Refutation contd

• Negate the goal


• ~man(shakespeare)
• Get a pair of resolvents
~ mortal ( shakespeare) ~ man( shakespeare)  mortal ( shakespeare)

~ man( shakespeare) ~ man( shakespeare)


Resolution Tree

Re solvent1 Re solvent 2

Re solute
Search in resolution
• Heuristics for Resolution Search
• Goal Supported Strategy
• Always start with the negated goal
• Set of support strategy
• Always one of the resolvents is the most recently produced resolute
Natural Deduction
Natural Deduction
• Proof system for first-order logic
• Designed to mimic the natural reasoning process
• Process:
• Make assumptions (“A” is true)
• Letters like “A” can represent larger propositional phrases
• The set of assumptions being relied on at a given step is called the context.
• Use rules to draw conclusions.
• Discharge assumptions as they become no longer necessary.
Natural Deduction
• Natural deduction is done in step by step:
• Rule
• Premises
• Conclusion
• …
Logical Connectives
Truth Tables for Logical Connectives
Making Conclusions
• The rules used to draw conclusions consist mostly of the introduction
(I) and elimination (E) of these connectives.
• Several of the rules serve to discharge earlier assumptions.
• The result does not rely on the assumption being true.
• If the assumption is used by itself again somewhere else, it must be
discharged again in a step that follows.
Introduction and Elimination
• Introduction builds the conclusion out of the logical connective and
the premises.
• Elimination eliminates the logical connective from a premise.
Rules: AND/OR

Rule “or E” discharges S and T.


Rules: IF

Rule “if I” discharges S


Rules: C
• Proof by contradiction
• If by assuming S is false, you
reach a contradiction, S is true.
• Discharges (not S)
Rules: forall ( )

• Rule “ I” requires that “a” does not occur in S(x) or any premise on
which S(a) may depend.
Rules: exists ( )

• Rule “ E” requires that “a” does not occur in S(x) or T or any


assumption other than S(a) on which the derivation of T from S(a)
depends.
• Rule “ E” also discharges S(a).
Tautology
• Always true.
• The proof of a tautology ultimately relies on no assumptions.
• The assumptions are discharged throughout the proof.
Sample proof: a tautology
Sample proof: a tautology

A is discharged using the ->I rule.


Sample proof: a tautology

B is discharged using the ->I rule.


Example using Quantifiers
• “Imagine how you would convince someone else, who didn’t know
any formal logic, of the validity of the entailment you are trying to
demonstrate.”
• a.k.a. That a knowledge base entails a sentence.
Example using Quantifiers

• Ex. We want to prove this: Take an arbitrary object a


• {forall x (F(x) -> G(x)) Suppose a is an F
Since all Fs are Gs, a is a G
• forall x (G(x) -> H(x))} Since all Gs are Hs, a is an H
|- forall x (F(x) -> H(x)) So if a is an F then a is an H
But this argument works for
any a
So all Fs are Hs
Proof using Natural Deduction
Rule exists ( ): Revisited

• Rule “ E” requires that “a” does not occur in S(x) or T or any


assumption other than S(a) on which the derivation of T from S(a)
depends.
• Rule “ E” also discharges S(a).
Logic Programming

94
Logic Programming

• We now examine a radically different paradigm for programming:


declarative programming
• rather than writing control constructs (loops, selection statements,
subroutines)
• you specify knowledge and how that knowledge is to be applied through a
series of rules
• the programming language environment uses one or more built-in methods to
reason over the knowledge and prove things (or answer questions)
• in logic programming, the common approach is to apply the methods of resolution and
unification
Logic Programming
We now examine a radically different paradigm for While these
languages have numerous flaws, they can build powerful problem
solving systems with little programming expertise
• they have been used extensively in AI research
Terminology
• Logic Programming is a specific type of a more general class:
production systems (also called rule-based systems)
• a production system is a collection of facts (knowledge), rules (which are
another form of knowledge) and control strategies
• we often refer to the collection of facts (what we know) as working memory
Terminology
• the rules are simple if-then statements where the condition tests values
stored in working memory and the action (then clause) manipulates working
memory (adds new facts, deletes old facts, modifies facts)
• the control strategies help select among a set of rules that match – that is, if
multiple rules have matching conditions, the control strategies can help decide
which rule we select this time through
• there are other control strategies as well – whether we work from conditions
to conclusions or from conclusions to conditions (forward, backward chaining
respectively)
Logic Programming

• Also known as declarative programming


• Mostly synonymous with the Prolog language because it is the only widely used
language for logic programming
• A declarative program does not have code, instead it defines two pieces of
knowledge
• facts – statements that are true
• rules – if-then statements that are truth preserving
• We use the program to prove if a statement is true and/or answer questions
• The reason this works is that Prolog has built-in problem solving processes
called resolution and unification
• in fact Prolog is not so much a programming language as it is a
tool, but it does have some programming language features
that make it mildly programmable
Background for Logic
• A proposition is a logical statement that is only made if it is true
• Today is Tuesday
• The Earth is round
• Symbolic logic uses propositions to express ideas, relationships
between ideas and to generate new ideas based on the given
propositions
• Two forms of propositions are
• Atomic propositions
• Compound terms (multiple propositions connected through the logical
operators of and, or, not, and implies)
• Propositions will either be true (if stated) or something to prove or
disprove (determine if it is true) – we do not include statements
which are false
• For symbolic logic, we use 1st order predicate calculus
• statements include predicates like round(x) where this is true if we can find
an x that makes it true such as round(Earth) or round(x) when x = Earth
• you might think of a predicate as a Boolean function except that rather
than returning T or F, it finds an x that makes it true
Logic Operators • Equivalence means
that both expressions
have identical truth
Name Symbol Example Meaning tables
• Implication is like an if-
negation  a not a then statement
conjunction  ab a and b • if a is true then b is true
• note that this does not
disjunction  ab a or b necessarily mean that if a
is false that b must also be
equivalence  ab a is equivalent false
to b • Universal quantifier
says that this is true no
implication  ab a implies b matter what x is
 ab b implies a • Existential quantifier
universal  X.P For all X, P is says that there is an X
that fulfills the
true statement
existential  X.P There exists a X.(woman(X)  human(X))
value of X – if X is a woman, then X is a human
such that P is
true X.(mother(mary, X)  male(X))
– Mary has a son (X)
Clausal Form
• To use resolution, all statements must be in clausal form
• B1  B2  …  Bn  A1  A2  …  Am
• B1 or B2 or … or Bn is true if A1and A2 and … and Am are all true
• the left hand side is the consequent (what we are trying to prove) and the
right hand side is the antecedent (what conditions must hold true)
• notice we have turned the if-then statement around,
• We must modify our knowledge so that:
• existential quantifiers are not required (eliminate all of them)
• universal quantifiers are implied (by replacing each with a specific
variable)
• no negations (all negations must be removed)
• We break down our statements to have a single item on the
left
• the above statement is broken into multiple statements such as
• B1  A1  A2  …  Am
• B2  A1  A2  …  Am …
• Bn  A1  A2  …  Am
• note that propositions and predicates by themselves are already in
clausal form, such as round(Earth) and Sunny
Example Statements
Consider the following knowledge:
Bob is Fred’s father  father(Bob, Fred)
Sue is Fred’s mother  mother(Sue, Fred)
Barbara is Fred’s sister  sister(Barbara, Fred)
Jerry is Bob’s father  father(Jerry, Bob)

And the following rules:


A person’s father’s father is the person’s grandfather
A person’s father or mother is that person’s parent
A person’s sister or bother is that person’s sibling
If a person has a parent and a sibling, then the sibling has the same parent
These might be captured in first-order predicate calculus as:
 x, y, z : if father(x, y) and father(y, z) then grandfather(x, z)
 x, y : if father(x, y) or mother(x, y) then parent(x, y)
 x, y : if sister(x, y) or brother(x, y) then sibling(x, y) and sibling(y, x)
 x, y, z : if parent(x, y) and sibling(y, z) then parent(x, z)
We would rewrite these as
grandfather(x, z)  father(x, y) and father(y, z)
parent(x, y)  father(x, y)
parent(x, y)  mother(x, y) etc
Resolution and Unification
• Given a collection of knowledge
• we will want to prove certain statements are true or answer questions
• For instance, from the previous example, we might ask
• who is Bob’s grandfather?
• is Sue Barbara’s parent?
• How can this be done? Through backward chaining through rules
• Here is how backward chaining works
• we want to prove that A is true
• find a rule with A on its LHS and whatever is on the rule’s RHS must now be
proven to be true, so we add the items on the RHS to a list of things we are
trying to prove
• repeat until
• we match something that we know is true to our list, then remove the item
• we run out of items on our list, then we are done, we have proven A is true
• To complicate matters, predicates (e.g., round(X)) need to be unified,
that is, to prove round(X) is true, we have to find some X where we
know it is true, for instance, round(Earth)
Complete Logic Example
Assume that we know the following about pets: If we want to find out what would
make a good indoor pet, we ask
poodle(COCOA) indoorpet(?)
setter(BIG)
terrier(SCOTTY) This requires finding pet(X) and small(X)
(find an X to make both predicates true)
dog(X)  poodle(X) (poodles are dogs) pet(X) is implied by dog(X),
dog(X)  setter(X) (setters are dogs) dog(X) is implied by terrier(X),
SCOTTY is a terrier so SCOTTY
dog(X)  terrier(X) (terriers are dogs) is a dog so SCOTTY is a pet
small(X)  poodle(X) (poodles are small)
small(X)  terrier(X) (terriers are small) Can we find if SCOTTY is small?
big(X)  setter(X) (setters are big) small(SCOTTY) is implied
by terrier(SCOTTY) which we
pet(X)  dog(X) (dogs are pets) already know is true, therefore,
indoorpet(X)  pet(X) and small(X) since terrier(SCOTTY) is true, small(SCOTTY)
(small pets are indoor pets) and pet(SCOTTY)
outdoorpet(X)  pet(X) and big(X) are true, so indoorpet(SCOTTY) is
(big pets are outdoor pets) True

Continuing with this process


will also prove that indoorpet(COCOA)
is true.
PROLOG
• PROLOG is a programming language that allows the programmer to specify
declarative statements only
• declarative statements (things you are declaring) fall into 2
categories
• predicates/propositions that are true
• clauses (truth preserving rules in clausal form)
• once specified, the programmer then introduces questions to
be answered
• PROLOG uses resolution (backward chaining) and unification to
perform the problem solving automatically
• PROLOG was developed in France and England in the late 70s
• the intent was to provide a language that could accommodate
logic statements and has largely been used in AI but also to a
lesser extent as a database language or to solve database
related problems
Elements of Prolog
• Terms – constant, variable, structure
• constants are atoms or integers (atoms are like those symbols
found in Lisp)
• variables are not bound to types, but are bound to values
when instantiated (via unification)
• an instantiation will last as long as it takes to complete a goal
• proving something is true, or reaching a dead end with the current
instantiation
• structures are predicates and are represented as
• functor(parameter list) where functor is the name of the predicate
• All statements in Prolog consist of clauses
• headed clauses are rules
• headless clauses are statements that are always true
• in reality, a headless clause is a rule whose condition is always true
• all clauses must end with a period
Rules
• All rules are stated in Horn clause form
• the consequence comes first
• the symbol :- is used to separate the consequence from the antecedent
• And is denoted by , and Or is denoted by ; or separating the
rule into two separately rules
• variables in rules are indicated with upper-case letters
• rules end with a .
• examples:
• parent(X, Y) :- mother(X, Y).
• parent(X, Y) :- father(X, Y).
• grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
• sibling(X, Y) :- mother(M, X), mother(M, Y), father(F, X), father(F, Y).
• we can use _ as a wildcard meaning this is true if we can find
any clause that fits
• father(X) :- father(X, _), male(X).
• X is a father if X is male and is someone’s father
Other Language Features
• Assignment statements are available using the is operator
• A is B / 17 + C.
• this works if B and C are instantiated and A is not
• however, is does not work like a true assignment statement
• you can not do x is x + y – this can never be true!
• we might use the assignment operator in a rule such as
• distance(X,Y) :- speed(X,Speed), time(X,Time), Y is Speed * Time
• List structures are also available using [ ] marks
• as in new_list([apple, prune, grape, kumquat]).
• this is not a binding of new_list to the values, but instead
new_list is a predicate with a true instance of the predicate
being the parameter [apple, prune, grape, kumquat]
• lists can also be represented as a head and tail using | to separate the
two parts similar to how Lisp uses CAR and CDR

You might also like