Podcast
Questions and Answers
In logic programming, what is the primary focus of the programmer?
In logic programming, what is the primary focus of the programmer?
- Defining the user interface.
- Specifying relations between objects and facts that are true. (correct)
- Optimizing memory allocation.
- Specifying the exact steps to solve a problem.
How does the Prolog system determine the solution to a query?
How does the Prolog system determine the solution to a query?
- By instantiating variables to make relations true based on the provided facts and rules. (correct)
- By ignoring facts and relations and relying solely on the query.
- By following the procedural steps outlined by the programmer.
- By using mathematical functions to compute the answer.
If male(charlie).
and male(bob).
are facts in a Prolog program, what will be the response to the query ?- male(eve).
?
If male(charlie).
and male(bob).
are facts in a Prolog program, what will be the response to the query ?- male(eve).
?
- charlie
- eve
- true
- false (correct)
Given the Prolog rule sibling(X, Y) :- parent(P, X), parent(P, Y).
, what does this rule define?
Given the Prolog rule sibling(X, Y) :- parent(P, X), parent(P, Y).
, what does this rule define?
What is the fundamental difference between programming with functions (FP) and programming with relations (LP)?
What is the fundamental difference between programming with functions (FP) and programming with relations (LP)?
Which of the following is NOT a core process in how a logic programming language, like Prolog, proves a query?
Which of the following is NOT a core process in how a logic programming language, like Prolog, proves a query?
Which of the following is a valid Prolog constant?
Which of the following is a valid Prolog constant?
According to Prolog syntax, which of the following defines a variable?
According to Prolog syntax, which of the following defines a variable?
In the context of Prolog syntax, what are 'terms' primarily composed of?
In the context of Prolog syntax, what are 'terms' primarily composed of?
How are atomic formulae used in Prolog programming?
How are atomic formulae used in Prolog programming?
In Prolog, what is the role of a query?
In Prolog, what is the role of a query?
What does the :-
symbol indicate in Prolog syntax?
What does the :-
symbol indicate in Prolog syntax?
Which of the following statements accurately describes a Horn clause?
Which of the following statements accurately describes a Horn clause?
In Prolog, what is a 'fact' in the context of Horn clauses?
In Prolog, what is a 'fact' in the context of Horn clauses?
How does Prolog handle free variables in a rule?
How does Prolog handle free variables in a rule?
What is the purpose of 'unification' in Prolog?
What is the purpose of 'unification' in Prolog?
What would be the result of unifying parent(bob, Y)
with parent(X, sue)
?
What would be the result of unifying parent(bob, Y)
with parent(X, sue)
?
What is a 'most general unifier' (MGU)?
What is a 'most general unifier' (MGU)?
What is the term for searching systematically for all possible solutions via unification and backchaining?
What is the term for searching systematically for all possible solutions via unification and backchaining?
In Prolog search trees, what does an edge typically represent?
In Prolog search trees, what does an edge typically represent?
What is a key difference between Logic Programming and Prolog concerning rule and subgoal ordering?
What is a key difference between Logic Programming and Prolog concerning rule and subgoal ordering?
How do you represent structure in a list in Prolog?
How do you represent structure in a list in Prolog?
What is list unification in Prolog?
What is list unification in Prolog?
Given the Prolog code member(X,[X|_]).
and member(X,[_|Rest]):-member(X,Rest).
, what does the predicate member(X, List)
check?
Given the Prolog code member(X,[X|_]).
and member(X,[_|Rest]):-member(X,Rest).
, what does the predicate member(X, List)
check?
What happens in Prolog's 'negation as failure' when something is 'unprovable'?
What happens in Prolog's 'negation as failure' when something is 'unprovable'?
What does the use of Cut
in Prolog accomplish?
What does the use of Cut
in Prolog accomplish?
How 'bottom-up' reasoning work?
How 'bottom-up' reasoning work?
Which method accurately characterizes Prolog's built-in reasoning mechanism?
Which method accurately characterizes Prolog's built-in reasoning mechanism?
What does Prolog's sequential execution and backtracking tend to simulate, even if it can't truly achieve it?
What does Prolog's sequential execution and backtracking tend to simulate, even if it can't truly achieve it?
In the Towers of Hanoi, what is its usual setup?
In the Towers of Hanoi, what is its usual setup?
Which of these queries is syntactically wrong in Prolog?
Which of these queries is syntactically wrong in Prolog?
Given the clause mylength([_|T],N):-mylength(T,NT), N is NT+1. mylength([),0).
, how is the number of element determined in Prolog?
Given the clause mylength([_|T],N):-mylength(T,NT), N is NT+1. mylength([),0).
, how is the number of element determined in Prolog?
Assuming some Prolog clauses for woman(X)
and famous(X)
, when is hates(john,X) is considered 'safe'?
Assuming some Prolog clauses for woman(X)
and famous(X)
, when is hates(john,X) is considered 'safe'?
What primarily causes Prolog's non-declarative stack overflow?
What primarily causes Prolog's non-declarative stack overflow?
What happens given ?-5 = 2+3.
in Prolog?
What happens given ?-5 = 2+3.
in Prolog?
What statement holds true for the anonymous variable, _
, in Prolog?
What statement holds true for the anonymous variable, _
, in Prolog?
What do internal nodes in Prolog search mostly contain?
What do internal nodes in Prolog search mostly contain?
Flashcards
Logic Programming
Logic Programming
Logic programming languages are neither procedural nor functional; they specify relations between objects.
Facts
Facts
In Prolog, these are assertions that a relation is true.
Queries
Queries
In Prolog, these check whether a relation is true.
Rules
Rules
Signup and view all the flashcards
Prolog Atoms
Prolog Atoms
Signup and view all the flashcards
Prolog Variables
Prolog Variables
Signup and view all the flashcards
Prolog Terms
Prolog Terms
Signup and view all the flashcards
Atomic Formulae
Atomic Formulae
Signup and view all the flashcards
Prolog Query
Prolog Query
Signup and view all the flashcards
Implications
Implications
Signup and view all the flashcards
Conjunction
Conjunction
Signup and view all the flashcards
Horn Clause
Horn Clause
Signup and view all the flashcards
Clause
Clause
Signup and view all the flashcards
Consequent
Consequent
Signup and view all the flashcards
Antecedents
Antecedents
Signup and view all the flashcards
Fact
Fact
Signup and view all the flashcards
Rule
Rule
Signup and view all the flashcards
Prolog Variables
Prolog Variables
Signup and view all the flashcards
Unification
Unification
Signup and view all the flashcards
Substitution
Substitution
Signup and view all the flashcards
Instantiation
Instantiation
Signup and view all the flashcards
Unifier
Unifier
Signup and view all the flashcards
Most General Unifier (MGU)
Most General Unifier (MGU)
Signup and view all the flashcards
Unification
Unification
Signup and view all the flashcards
Backward Chaining
Backward Chaining
Signup and view all the flashcards
Backtracking
Backtracking
Signup and view all the flashcards
Prolog Search Trees
Prolog Search Trees
Signup and view all the flashcards
List Unification
List Unification
Signup and view all the flashcards
List Unification
List Unification
Signup and view all the flashcards
Negation as Failure
Negation as Failure
Signup and view all the flashcards
Cut (!)
Cut (!)
Signup and view all the flashcards
Bottom-Up Reasoning
Bottom-Up Reasoning
Signup and view all the flashcards
Top-Down Reasoning
Top-Down Reasoning
Signup and view all the flashcards
Nondeterministic Progamming
Nondeterministic Progamming
Signup and view all the flashcards
Study Notes
Logic Programming and Prolog
- Logic programming differs from procedural and functional programming, focusing on relations between objects.
- Programmers define facts and relations as true.
- The system determines how to use these facts to solve problems and instantiates variables to make relations true.
Prolog Facts and Queries
- Facts examples include
male(charlie).
,female(alice).
,parent(charlie,bob).
- When queries are made, Prolog either returns true, false, or appropriate values of variables (called a substitution).
- Example query
?- male(charlie).
returnstrue
.?- female(Person).
returnsPerson = alice
.
Prolog Rules
- Rules define relationships, such as
sibling(X, Y) :- parent(P, X), parent(P, Y).
- This rule infers siblings based on shared parents
- With appropriate facts established queries can be run to determine truth
Logic vs Functional Programming
- Functional Programming programs with functions and asks for a function’s value given arguments.
f(x, y, z) = x * y + z - 2
- Logic Programming programs with relations and asks for arguments to make a true statement.
r(x, y, z) = {(1, 2, 3), (9, 5, 13), (0, 0, 4)}
Logic Programming Characteristics
- Programs are composed of facts and rules.
- Running a program means querying the system.
- The language itself attempts to prove the query's truth.
- This may freeze variable values.
- The system uses unification, resolution, and backtracking.
Prolog Syntax: Constants and Variables
- Constants: Either atoms, any string of alphanumeric/underscore starting with lowercase or Numbers.
- Variables: Strings that begin with '_' or an uppercase letter.
Prolog Syntax: Terms
- Terms are inductively defined
- Constants and variables are terms
- Compound terms are applications of n-ary functors to n terms.
- Prolog distinguishes numbers and variables.
Prolog Syntax: Atomic Formulae
- Atomic formulae, also called predicates, consist of an n-ary relation applied to n terms
- Formulae are true or false and used as facts or queries and components of statements
Prolog Queries
- A query is a proposed fact to be proven.
- Queries without variables return true/false.
- Queries with variables return variable values (substitution).
- Example: the query
?- female(Person).
may return "Person = alice".
Prolog Syntax: Complex Formulae
- Complex formulae combine simpler formulae.
- Conjunction: "in Prolog, looks like ' , ' "
- Can be used as queries, but are not able to be used as facts
Prolog Syntax: Implications
- Implications are written backwards (conclusion first) in Prolog as ':-', named a rule.
- Rules assert implications but not for queries.
- Example:
sibling(X, Y) :- parent(P, X), parent(P, Y).
Horn Clauses (Rules)
- Horn Clause:
c ← h1 ∧ h2 ∧ h3 ∧... ∧ hn
- Antecedents: conjunction of zero or more atomic formulae.
- Consequent: atomic formula.
- Meaning: the consequent is true if the antecedents are all true.
Horn Clauses: In Prolog
- Horn Clause = Clause
- Consequent = Goal = Head
- Antecedents = Subgoals = Tail
- Horn Clause with No Tail = Fact
- Horn Clause with Tail = Rule
- In Prolog
c :- h1, ..., hn.
- The syntax elements are
':-' , ',' , '.'
Prolog: Examples of Horn Clauses
- Fact Example:
male(charlie).
stating charlie is male without other conditions. - Rule Example:
father(charlie,bob):- male(charlie), parent(charlie, bob).
states that charlie is the father of bob if he is male and a parent.
Meaning of Prolog Rules
- Prolog rule form:
c :- a1, a2, a3, ..., an.
- Logic Equivalent:
a1 ∧ a2 ∧ a3 ∧...∧ an → c
- Restrictions: Zero or more conjoined antecedents and only one consequent.
Non-Horn Clauses
- Many non-Horn formulae can be converted into logically equivalent Horn-formulae.
- ¬¬a ⇔ a (double negation)
- ¬(a ∨ b) ⇔ ¬a ∧¬b (DeMorgan)
- a∨ (b ∧ c) ⇔ (a ∨ b) ∧ (a ∨ c) (distributivity)
- a → b ⇔ ¬a∨b (implication)
Prolog Variables
- Variables in Horn clauses are interpreted universally
- A query
?-q(X1, ..., Xn)
is interpreted as ∃X1,..., Xn . q(X1,..., Xn)
Horn Clauses with Variables
isaMother(X) :- female(X), parent(X, Y).
- In first-order logic:
∀X . isaMother(X) ← ∃Y · parent(X, Y) ∧ female(X).
Execution of Prolog Programs
- Core processes include:
- Unification: variable bindings.
- Backward Chaining/ Top-Down Reasoning/ Goal-Directed Reasoning: Reduces a goal to one or more subgoals.
- Backtracking: Searches all options using unification and backchaining.
Unification
- Atomic formulae unify if identical after variable replacement.
parent(bob,Y)
unifies withparent(bob,sue)
by replacingY
withsue
.- Parent(bob, Y) unifies with parent(X, sue) by replacing Y by sue and X by bob
- A substitution maps variables to Prolog terms.
- Instantiation is application of substitution to a formula or term.
Unification: Common Instance
- Common instance of A and B exists substitutions S1 and S2 where C = S1(A) = S2(B).
- A and B are unifiable if they have a common instance.
- A substitution that makes a common instance is a unifier.
Unification: Examples
- p(a,a) unifies with p(a,a)
- p(X,X) unifies with p(b,b) and with p(c,c).
- p(X,b) unifies with p(Y,Y) with unifier X/b, Y/b to become p(b,b).
Unification: More Examples
P(f(X),X)
unifies withp(Y,b)
with unifier{X\b, Y\f(b)}
to becomep(f(b),b)
.p(b,f(X,Y),c)
unifies withp(U,f(U,V),V)
with unifier{X\b, Y\c, U\b, V\c}
to becomep(b,f(b,c),c)
.
Unification: Lack of Unification Example
p(b,f(X,X),c)
does not unify withp(U,f(U,V),V)
- must replace
U
byb
. - must replace
V
byc
. - However, no substitution for
X
will convertp(b,f(X,X),c)
intop(b,f(b,c),c)
.
Unification: Singleton Variables
- A singleton variable is a variable that appears only once and can be replaced with _.
- The anonymous variable '_' matches anything and refers to a different unique variable each instance.
Most General Unifier (MGU)
p(X, f(Y))
andp(g(U), V)
have infinite unifiers.- General unifier:
{X\g(U), V\f(Y)}
Unites top(g(U),f(Y))
. - Uses variables to fill fewest details.
Execution of Prolog Programs
- Core processes include:
- Unification: variable bindings.
- Backward Chaining/ Top-Down Reasoning/ Goal-Directed Reasoning: Reduces a goal to one or more subgoals.
- Backtracking: Searches all options using unification and backchaining.
Prolog Search Trees
- Encapsulate unification, backward chaining, and backtracking.
- Internal nodes are ordered list of subgoals/
- Leaves are success nodes or failures
- Edges have variable bindings from unification.
- They describe all computation paths, with possible success and infinite branches.
Logic Programming vs. Prolog: Determinism
- Logic Programming is nondeterministic:
- Arbitrarily choose which rule or subgoal to expand first.
- Results are independent of ordering.
- Prolog is deterministic:
- Expands the first rule and explores the first subgoal first.
- Results may depend on the rule and subgoal ordering.
Prolog List Syntax
- "[]" is the empty list
- "[Head | Tail]" is a list with first element Head and a Tail containing a list of the remaining elements
- Abbreviated as "[e1, e2,... en ]"
- Two lists unify if and only if have the same structure and the corresponding elements unify
Lists in Prolog
% member(?X,?List) iff X is an element of List
member(X,[X|_]).
member(X,[_|Rest]):-member(X,Rest).
Lists in Prolog: Append
% append(?X,?Y,?Z) iff Z is the result of appending list Y to list X
append([],Y,Y).
append([H|T],Y,[H|Z]):-append(T,Y,Z).
Lists in Prolog: Mlength
% mylength(?L,?N) iff N is the length of list L
mylength([],0).
mylength([_|T],N):-mylength(T,N-1).
Arithmetic in Prolog
- Arithmetic Expression Evaluation requires:
expr1 is expr2
where expr2 is fully grounded.expr1 < expr2 or expr1 > expr2
where both are instantiated
Negation as Failure
- Prolog cannot assert something as false, but says a given facts and rules do not allow something to be proven true
- Assuming something unprovable is false, is called negation as failure under a closed world assumption
\+(G)
(ornot G
) succeeds whenever the goalG
fails
Negation as Failure: Proper Use
not(G)
works properly only if:- G is fully instantiated at the time prolog processes the goal not(G).
- variables in G are unique to G
- "Guard" is a method called to ensure these criteria are met
Execution of Prolog Programs
- Execution relies on:
- Unification for variable bindings.
- Backward Chaining, Top-Down Reasoning, and Goal-Directed Reasoning to reduce goals.
- Backtracking to search all solutions.
Cut
- The goal "!" (cut") succeeds immediately and disallows backtracking over the cut, or applying a different clause of the same predicate
- Cuts the derivation tree of all other choices where the cut was introduced
- Improves efficiency by reducing the search space
Reasoning: Bottom up and Top Down
- Bottom-up (or forward) reasoning: uses given facts and rules to infer everything that is true.
- Top-down (or backward) reasoning: starts from query and applies rules in reverse.
- Prolog uses backtracking reduction to perform top-down search
Nondeterministic Programming
- Powerful way to define and implement algorithms
- Nondeterministic machines chooses next operation correctly with several alternatives
- Simulated with Prolog's sequential search and backtracking
Towers of Hanoi
- Task: moving N disks from the left peg to the right peg using the centre peg, without placing a larger disk upon a smaller disk
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.