Prolog Logic Programming Principles and Practice LOGIC FOR COMPSCI

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/36

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

37 Terms

1
New cards

Explain the difference between a fact and a rule in Prolog. Provide a simple example of each

A fact in Prolog is an unconditionally true statement representing a basic piece of knowledge. For example: dog(fido). Conversely, a rule is a conditional statement that defines a relationship between facts or other rules. For example: happy(X) :- plays(X). (X is happy if X plays).

2
New cards

Describe the three ways in which atoms can be written in Prolog. Give one example for each type.

Atoms can be written as: (a) A sequence of one or more alphanumeric characters and underscores, starting with a lowercase letter (e.g., john, today_is_tuesday). (b) Any sequence of characters enclosed in single quotes (e.g., 'Today is Tuesday', '32abc'). (c) A sequence of one or more special characters (e.g., +, -->, `<=').

3
New cards

What is unification in Prolog, and when does it succeed or fail? Provide a brief example of a successful unification.

Unification is making two Prolog terms identical by finding appropriate variable bindings. It succeeds if the terms can be identical by substituting variables with other terms, and it fails if no such substitution can make them the same. Example of success: Unifying X and john results in X being bound to john.

4
New cards

Explain the concept of backtracking in Prolog. How does the system attempt to find solutions when a goal fails?

Backtracking is how Prolog explores alternative solutions when a goal fails. If a goal in a sequence fails, Prolog returns to the most recent goal with multiple alternative clauses or rules that could have been tried, and it attempts to resatisfy that goal using the next available option.

5
New cards

What is the purpose of the is/2 predicate in Prolog? How does it differ from the = operator?

The is/2 predicate is used for arithmetic evaluation in Prolog. The second argument must be an arithmetic expression that can be evaluated to a number, and the first argument is unified with the result. It differs from =, which performs syntactic unification and does not evaluate arithmetic expressions. For example, X is 2 + 3. will bind X to 5, while X = 2 + 3. will bind X to the term 2 + 3

6
New cards

Describe the functionality of the cut (!) predicate. How does it affect backtracking in a Prolog program?

The cut predicate (!) is a goal that always succeeds when first encountered but prevents backtracking past the point where the cut was introduced within the current clause. It effectively commits Prolog to the choices made before the cut in that clause, pruning the search tree.

7
New cards

Explain the concept of "Negation as Failure" in Prolog and how the not/1 or \+/1 operator works.

"Negation as Failure" is Prolog's way of handling negation. The not/1 (or \+/1) operator succeeds if its argument (a goal) fails and fails if its argument succeeds. Prolog assumes that anything that cannot be proven true is false, based on the closed world assumption.

8
New cards

What are lists in Prolog, and how are they represented internally? Describe the notation for the head and tail of a list.

Lists in Prolog are ordered collections of terms. Internally, they are represented as compound terms using the functor . (dot). The empty list is represented by []. The notation [Head|Tail] denotes a list where Head is the first element, and Tail is the rest of the list (which is also a list).

9
New cards

Explain the use of the built-in predicate member/2. Provide an example of how it can be used.

The member/2 predicate checks if a term is an element of a list. member(X, L) succeeds if X can be unified with one of the elements in the list L. For example, member(b, [a, b, c]). will succeed. It can also find all list elements through backtracking: member(X, [a, b, c]). will yield X = a, then X = b, then X = c.

10
New cards

Describe the purpose and syntax of the op/3 predicate. How can it be used to define new operators in Prolog?

The op/3 predicate is used to define or redefine operators in Prolog. Its syntax is op(Precedence, Type, Name), where Precedence is an integer indicating the operator's precedence, Type specifies its associativity and position (e.g., xfx for infix, fx for prefix), and Name is the operator's name (an atom). For example, op(200, xfx, is_greater_than). defines is_greater_than as an infix operator with precedence 200.

11
New cards

Atom

A constant in Prolog that does not have a numerical value. It can be a sequence of characters, a quoted string, or a sequence of special characters

12
New cards

Backtracking

The process by which Prolog tries alternative ways to satisfy goals when the current path fails. It involves returning to a previous goal and trying a different clause or rule

13
New cards

Binding

The assignment of a value (a term) to a variable. Once a variable is bound, it retains that value within its lexical scope until backtracking occurs.

14
New cards

Built-in Predicate (BIP)

Predefined predicates in the Prolog system provide fundamental functionalities such as input/output, arithmetic, and list manipulation

15
New cards

Clause

A fundamental unit of a Prolog program, which can be either a fact or a rule.

16
New cards

Closed World Assumption

The assumption in Prolog that any statement that cannot be proven true based on the program's facts and rules is considered false

17
New cards

Compound Term

A term in Prolog that has a functor (name) and one or more arguments enclosed in parentheses (e.g., person(john, 30)). Lists are a special type of compound term

18
New cards

Consult

A built-in predicate (consult/1) loads a Prolog program (a file containing clauses) into the Prolog interpreter's database.

19
New cards

Cut (!)

A control predicate that, when executed, commits Prolog to the choices made so far in the current clause and prevents backtracking to previous choice points within that clause

20
New cards

Fact

A clause in Prolog that asserts a statement as unconditionally true (e.g., likes(john, prolog).).

21
New cards

Functor

The name of a compound term or predicate, identifying its structure and meaning.

22
New cards

Goal

A query or a part of a rule's body that Prolog attempts to satisfy (prove true).

23
New cards

Head

The left-hand side of a clause (a fact or the conclusion of a rule)

24
New cards

List

An ordered sequence of terms enclosed in square brackets (e.g., [a, b, c]). It can be empty ([]) or have a head and a tail.

25
New cards

Member

A built-in predicate (member/2) that checks if a term is an element of a list.

26
New cards

Neck (:-)

The symbol that separates the head of a rule from its body; read as "if".

27
New cards

Negation as Failure

Prolog's approach to negation, where not(Goal) succeeds if Goal fails and fails if Goal succeeds

28
New cards

Operator

Special symbols or keywords used in Prolog expressions with defined precedence and associativity (e.g., arithmetic operators like +, relational operators like <, logical operators like not)

29
New cards

Predicate

A named relation defined by a set of clauses (facts and rules) that share the exact functor and arity (number of arguments).

30
New cards

Query

A goal or sequence of goals entered by the user at the Prolog prompt to be evaluated by the Prolog system

31
New cards

Recursion

A programming technique where a predicate is defined in terms of itself, allowing for repetitive operations, especially common in list processing in Prolog.

32
New cards

Rule

A clause in Prolog defines a conditional relationship between a head (conclusion) and a body (one or more goals must be true for the head to be true)

33
New cards

Side Effect

An action performed by a predicate not directly related to logical inference, such as printing output to the screen or modifying the program database.

34
New cards

Stream (Input/Output)

Channels through which Prolog can read data from (input stream) or write data to (output stream), such as the user's terminal or files.

35
New cards

Term

A fundamental building block of Prolog, a variable, a constant (atom or number), or a compound term

36
New cards

Unification

Making two Prolog terms identical by finding a common instance by substituting variables

37
New cards

Variable

A symbol (starting with an uppercase letter or underscore) that can stand for any term and become bound to a term during unification.