AI Unit 4 Lecture Slides
AI Unit 4 Lecture Slides
HARSHITA SHARMA
Syllabus
overview ● Fundamentals and concepts of
programming languages
Unit 4 : ●
●
Languages like prolog or lisp
Relationship of languages with knowledge
Programming representation or interfaces
Languages
Logic Programming
Introduction
Logic programming is a paradigm of computer programming that is based on the principles of mathematical logic. It differs
from traditional imperative programming paradigms in that it emphasizes the logical relationships and rules that govern a
problem domain rather than focusing on explicit sequences of commands or operations. In logic programming, programs
are expressed as sets of logical statements and rules, and computation is performed through logical inference.
At the core of logic programming is the concept of logical deduction, where conclusions are derived from a set of logical
premises using formal inference rules.
Basic elements
Facts: Basic statements about the problem domain that are assumed to be true. These facts represent the initial
knowledge base upon which the logic program operates.
Rules: Logical statements that define relationships between facts and other logical statements. Rules typically take
the form of logical implications, specifying that if certain conditions are met, then certain conclusions can be drawn.
Queries: Goals or questions posed to the logic program. The program attempts to find solutions or proofs for these
queries by applying rules and performing logical inference.
Logic programming languages provide mechanisms for representing and manipulating logical propositions, variables,
quantifiers, and logical connectives (such as conjunction, disjunction, and negation). The most well-known logic
programming language is Prolog (Programming in Logic), which was developed in the 1970s. Prolog uses a form of logical
inference called resolution to derive conclusions from the rules and facts provided in a program.
Logic programming is particularly well-suited for tasks that involve symbolic reasoning, constraint satisfaction, and search
problems. It is widely used in areas such as artificial intelligence, expert systems, natural language processing, and
automated theorem proving. By providing a declarative and expressive way to specify problems, logic programming enables
developers to focus on the logic of the problem domain rather than the details of algorithmic implementation.
Fundamental concepts
Logic programming languages are built on the principles of mathematical logic and aim to provide a declarative
approach to problem-solving. Here are some fundamental concepts of logic programming languages:
1. Logical Variables: Variables in logic programming languages can stand for any value. They are not assigned
values as in imperative programming but are unified with values that satisfy certain conditions. This unification
process is a central mechanism in logic programming.
2. Predicates and Relations: Logic programs are composed of predicates, which are logical statements that can be
true or false. Predicates can represent relations between objects or properties of objects.
3. RuleS: Rules in logic programming define relationships between predicates. They typically take the form of
logical implications, such as "if A and B then C." Rules are used to derive new facts from existing facts or to perform
computations.
4. Facts: Facts are basic statements that are assumed to be true. They represent the initial
knowledge base upon which the logic program operates. Facts can be used directly in
queries or as the basis for inference.
5. Queries: Queries are goals or questions posed to the logic program. The program
attempts to find solutions or proofs for these queries by applying rules and performing
logical inference.
6. Backtracking: Backtracking is a fundamental mechanism in logic programming that allows the program to explore multiple possible solutions
to a problem. If a particular path leads to a dead-end or fails to satisfy the constraints, the program backtracks and explores alternative paths.
7. Recursion: Recursion is commonly used in logic programming languages to define repetitive processes or to express rules that involve
iteration. Recursive predicates allow for elegant and concise solutions to certain types of problems.
8. Unification: Unification is the process of finding substitutions for logical variables that make two terms identical. It is a key operation in logic
programming, used to match patterns and instantiate variables in rules and queries.
9. Horn Clauses: Horn clauses are a restricted form of logic programming rules that consist of a
head and a body. The head is a single predicate, and the body is a conjunction of predicates.
Prolog, one of the most well-known logic programming languages, uses Horn clauses.
10. Resolution: Resolution is a fundamental inference rule in logic programming, based on the
principles of predicate logic. It is used to prove the validity of queries by attempting to derive a
contradiction from the negation of the query.
Applications
● Artificial Intelligence (AI): Logic programming languages like Prolog are widely used in AI for tasks such as expert
systems, knowledge representation and reasoning, natural language processing, planning, and constraint
satisfaction problems.
● Expert Systems: Expert systems are computer systems that emulate the decision-making ability of a human expert in
a specific domain. Logic programming is used to encode the rules and knowledge base of the domain, allowing the
system to make inferences and provide expert advice or solutions.
● Natural Language Processing (NLP): Logic programming languages are used in NLP for tasks such as parsing,
semantic analysis, and question-answering systems. Prolog, for example, can be used to define grammars and rules
for processing natural language sentences.
● Database Systems: Datalog, a logic programming language derived from Prolog, is used in database systems for
declarative querying and rule-based reasoning. Datalog allows users to express complex queries and recursive rules
for data manipulation and inference.
● Automated Theorem Proving: Logic programming is used in automated theorem proving systems to prove
mathematical theorems and verify the correctness of logical formulas. Prolog and other logic programming
languages can be used to define formal proof strategies and search for proofs in large logical spaces.
● Semantic Web: Logic programming languages such as RDF (Resource Description Framework) and OWL (Web
Ontology Language) are used in the Semantic Web to represent and reason about knowledge and relationships
between resources on the web.
● Symbolic Mathematics: Logic programming can be used for symbolic computation and mathematics, such as
solving algebraic equations, symbolic differentiation and integration, and theorem proving in mathematical logic.
● Configuration and Design: Logic programming is used in configuration and design applications where there are
complex constraints and relationships between components. It allows for the specification of rules and constraints
governing valid configurations or designs.
● Planning and Scheduling: Logic programming can be used for planning and scheduling tasks in domains such as
logistics, manufacturing, and project management. It allows for the representation of goals, resources, and
constraints, and the generation of plans to achieve those goals.
● Constraint Satisfaction: Logic programming is used for solving constraint satisfaction problems, where variables
have to be assigned values subject to constraints. Examples include scheduling problems, timetabling, and resource
allocation.
Relationship with Knowledge Representation
Logic programming and knowledge representation (KR) have a deep and intertwined relationship. Logic programming languages, such as Prolog, provide a
powerful framework for representing and reasoning with knowledge. Here's how logic programming relates to knowledge representation:
1. Declarative Representation:
● Logic programming languages allow for the declarative representation of knowledge. Knowledge is expressed as logical facts and rules, which
describe relationships, constraints, and properties in the problem domain.
● Declarative representation makes it easier for domain experts to understand, validate, and maintain the knowledge base.
2. Rule-Based Representation:
● Logic programming relies on rules to define relationships and infer new knowledge. These rules are expressed in logical form, typically using
predicates, variables, and logical operators.
● Rule-based representation enables the encoding of complex patterns of inference and deduction, facilitating reasoning over the knowledge base.
3. Pattern Matching and Unification:
● Logic programming languages employ pattern matching and unification to match queries against the knowledge base and infer new information.
● Pattern matching allows for the retrieval of relevant facts and rules that match the query, while unification enables the instantiation of variables to satisfy the query
conditions.
5. Domain-Specific Knowledge:
● Logic programming languages are well-suited for representing domain-specific knowledge in various fields, including expert systems, natural language processing,
semantic web, and database querying.
● Domain-specific knowledge is encoded as logical facts and rules, allowing for flexible and expressive representation of relationships and constraints.
6. Integration with KR Formalisms:
● Logic programming languages can integrate with formalisms and standards for knowledge representation, such as
RDF (Resource Description Framework), OWL (Web Ontology Language), and Description Logics.
● Integration with KR formalisms enables logic programs to interact with external knowledge sources and ontologies,
enhancing their expressiveness and interoperability.
In functional programming, we have to mention how one problem can be solved, but in logic programming we have to
specify for which problem we actually want the solution. Then the logic programming automatically finds a suitable
solution that will help us solve that specific problem.
Some logic programming languages
ALF (algebraic logic functional programming language).
ASP (Answer Set Programming)
CycL
Datalog
FuzzyCLIPS
Janus
Parlog
Prolog
Prolog++
ROOP
Prolog
Introduction
Prolog is a declarative logic programming language that is widely used in artificial intelligence and computational
linguistics. Its name stands for "Programming in Logic." Prolog is based on a formal system called Horn clauses, which are
logical implications in the form of "head :- body," where the head is a single predicate and the body is a conjunction of
predicates.
It is one major example of the fourth generation language that supports the declarative programming paradigm. This
is particularly suitable for programs that involve symbolic or non-numeric computation. This is the main reason to
use Prolog as the programming language in Artificial Intelligence, where symbol manipulation and inference
manipulation are the fundamental tasks.
In Prolog, we need not mention the way how one problem can be solved, we just need to mention what the problem
is, so that Prolog automatically solves it. However, in Prolog we are supposed to give clues as the solution method.
One of the key features of Prolog is its ability to handle uncertain or incomplete information. In Prolog, a
programmer can specify a set of rules and facts that are known to be true, but they can also specify rules and
facts that might be true or false.
The Prolog interpreter will then use those rules and facts to automatically reason about the problem domain
and find solutions that are most likely to be correct, given the available information.
Key features
● Declarative Nature: Prolog programs are declarative, meaning they describe relationships and rules rather than
specifying a sequence of steps to achieve a result. This makes Prolog well-suited for expressing complex logical
relationships and solving problems through logical inference.
● Logic-Based: Prolog is based on a subset of first-order predicate logic. It allows the programmer to define relations,
facts, and rules using predicates, logical connectives, and quantifiers.
● Rules and Facts: In Prolog, programs are composed of rules and facts. Facts are assertions about the world, while
rules define logical relationships between facts. These rules and facts form the knowledge base that Prolog uses to
answer queries.
● Unification: Prolog's execution mechanism is based on unification, which is the process of finding substitutions for
logical variables that make two terms identical. Unification is used to match patterns in rules and queries and
instantiate variables.
● Backtracking: Prolog uses a depth-first search strategy with backtracking to find solutions to queries. If a path leads
to a dead-end, Prolog will backtrack and explore alternative paths until a solution is found or all possibilities have
been exhausted.
● Recursion: Prolog supports recursion, allowing predicates to call themselves recursively. Recursive predicates are
commonly used to define iterative processes and solve problems involving repetition.
● Pattern Matching: Prolog uses pattern matching to match queries against rules and facts in the knowledge base.
This allows Prolog to determine which rules are applicable to a given query and instantiate variables accordingly.
● Interactive Querying: Prolog provides an interactive environment where users can pose queries and receive answers
based on the knowledge base. This interactive querying makes Prolog well-suited for exploratory analysis and
prototyping.
Basic Elements
Facts − The fact is predicate that is true, for example, if we say, “Tom is the son of Jack”, then this is a fact.
Rules − Rules are extinctions of facts that contain conditional clauses. To satisfy a rule these conditions should be
met. For example, if we define a rule as −
This implies that for X to be the grandfather of Y, Z should be a parent of Y and X should be father of Z.
Questions − And to run a prolog program, we need some questions, and those questions can be answered by the
given facts and rules.
Syntax
Unification:
● Unification is the process of finding substitutions for variables in predicates to make them equal.
● It is the fundamental operation in Prolog's inference mechanism.
● Example: parent(X, mary) :- father(X, mary).
Backtracking:
● Prolog uses backtracking to explore different paths in the search for solutions.
● If a goal fails, Prolog backtracks to the most recent choice point and explores alternative paths.
● Example: ancestor(X, Y) :- parent(X, Y).
Horn Clauses:
● Prolog rules are often written in the form of Horn clauses, which consist of a head and a body.
● The head is a predicate, and the body is a conjunction of goals.
● Example: grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
Recursion:
● Prolog supports recursion, allowing predicates to be defined in terms of themselves.
● Recursive predicates are used for iterative processes and defining complex relationships.
● Example: factorial(0, 1). factorial(N, F) :- N > 0, N1 is N - 1, factorial(N1, F1), F is N * F1.
Cut Operator:
● The cut operator ! is used to control backtracking behavior and prune search paths.
● It commits Prolog to the current choice point, preventing further backtracking beyond that point.
● Example: factorial(N, F) :- N > 0, !, N1 is N - 1, factorial(N1, F1), F is N * F1.
Negation:
● Prolog supports negation through the \+ operator, which is used to negate goals.
● It succeeds if the goal it negates fails, and fails if the goal succeeds.
● Example: not_equal(X, Y) :- \+(X = Y).
These syntax and semantics define how Prolog programs are written and interpreted. Understanding them is essential for writing
correct and efficient Prolog code.
Softwares
DEsktop Softwares:
SWI-Prolog
GNU Prolog
SICStus Prolog
Online platforms:
Repl.it
OnlineGDB
How facts work in Prolog?
In Prolog, facts are statements that are assumed to be true. They are used to provide the interpreter with information
about the problem domain, and the interpreter will use this information to automatically infer solutions to problems.
Facts are written using a predicate name followed by a list of arguments enclosed in parentheses. For example:
man(john).
woman(mary).
capital_of(france, paris).
In this example, the first line states that john is a man, the second line states that mary is a woman, and the
third line states that paris is the capital of france.
Prolog facts can have any number of arguments, and the arguments can be variables or constants. For
example, the following fact has two arguments, one of which is a variable:
problems. For example, you could ask the interpreter to find the capital of France by using the following
query:
capital_of(france, X)?
In this query, the interpreter will use the capital_of/2 fact that you defined earlier to determine that the
capital of France is Paris, and it will return the value paris as the solution.
How do rules work in Prolog?
In Prolog, rules are logical statements that describe the relationships between different facts. They are used to
specify the conditions that must be met in order for a certain fact to be true.
Rules are written using the predicate name followed by a list of arguments enclosed in parentheses followed
by a colon and a hyphen (:-) and the body of the rule. For example:
example, the following rule has three arguments, two of which are variables:
bigger_than(X,Y,Z)?
In this query, the interpreter will use the bigger_than/3 rule that you defined earlier to determine which
object is bigger than the other, and it will return the appropriate value for Z as the solution.
How do variables work in Prolog?
In Prolog, variables are used to represent values that can change or be determined by the interpreter. They are written using a
name that begins with an uppercase letter, such as X or Y.
Variables can be used in both facts and rules to represent values that are not known at the time the program is written. For
example, the following fact uses a variable to represent the capital of a country:
capital_of(Country, Capital).
In this case, the fact states that the Capital of a given Country is unknown, and the interpreter will use other facts and rules to
determine the value of Capital when a query is made.
How do Queries work in Prolog?
Queries are used to ask the interpreter to find solutions to problems based on the rules and facts in the program. In Prolog, queries are written
using the same syntax as facts, followed by a question mark (?). For example:
capital_of(france, X)?
In this query, the interpreter will use the capital_of/2 fact that was defined earlier to determine that the capital of France is Paris, and it will
return the value paris for the variable X as the solution.
You can use queries to ask the interpreter to find solutions to a wide range of problems, based on the rules and facts that you have defined in
your Prolog program. The interpreter will use these rules and facts to automatically reason about the problem domain and find solutions that
are most likely to be correct, given the available information.
Knowledge Representation using Prolog
Knowledge in Prolog is represented by predicates. A predicate is a structure which is analogous to a relation in the Relational Model.
This is a prototype (or a model) which should be filled with data. A predicate is identified uniquely by a pair: its name, which is a functor,
and number of arguments, which is called arity. It is usually denoted as: name/arity.. Each single, atomic fact or rule is given as a
clause of the certain predicate. Knowledge is stored in two ways: as simple clauses and complex ones.
A simple clause represents a fact. There could be many simple clauses of a single predicate which are similar to many tuples in a
single relation. Simple clauses define Extensional Knowledge.
An example of family relationships is given. There are seven simple clauses of predicate parent/2. The first clause denotes that james
is john's parent, the second one denotes that james is mary's parent, and so on.
A complex clause describes relationships in knowledge, thus it provides Intensional Knowledge. Such a clause is a structure or set of
structures which form a logical expression. It consists of a head and body. The head is a conclusion. The body contains preconditions.
These structures are valid predicates which are defined by simple or complex clauses. Complex clauses could also use build-in
predicates, which provide for example logical and arithmetical operations.
The following example shows complex claues which provide Intensional Knowledge regarding family relationships such as: a sibling
and cousin. A comma stands for logical AND, \== stands for not equal. Complex clauses may be recursive, as it is shown for the last
clause.
Operators in Prolog
In Prolog, operators are symbols that are used to define the syntax and precedence of terms in Prolog programs. These operators are categorized into different
types based on their functionality and usage. Here are some common types of operators in Prolog:
1. Arithmetic Operators:
● Arithmetic operators are used for performing arithmetic operations in Prolog.
● Examples: + (addition), - (subtraction), * (multiplication), / (division), // (integer division), mod (modulo).
2. Comparison Operators:
● Comparison operators are used to compare terms in Prolog.
● Examples: == (equality), \== (inequality), < (less than), > (greater than), =< (less than or equal to), >= (greater than or equal to).
3. Logical Operators:
● Logical operators are used for logical operations in Prolog.
● Examples: , (conjunction), ; (disjunction), \+ (negation), -> (implication).
4. Assignment Operator:
● The assignment operator = is used to unify terms in Prolog.
● It is used to assign values to variables or to check if terms are equal.
5. List Operators:
● List operators are used for constructing and manipulating lists in Prolog.
● Examples: [] (empty list), | (list construction), [Head|Tail] (list decomposition).
●
6. Term Operators:
● Term operators are used for creating compound terms in Prolog.
● Examples: . (dot), , (comma), :- (rule definition), :- (clause definition), --> (grammar rule definition).
7. Custom Operators:
● Prolog allows users to define custom operators to extend the language syntax.
● Custom operators can be defined using the :- op/3 directive.
8. Mode Operators:
● Mode operators are used in some Prolog systems to specify argument modes for predicates.
● Examples: + (input), - (output), ? (input or output).
9. Module Operators:
● Module operators are used for module qualification in Prolog.
● Examples: : (module qualification), :: (module qualification for metapredicates).
man(john).
woman(mary).
capital_of(france, paris).
% Rule
% Query 1
not(john, mary)?
% Query 2
capital_of(france, X)?
In this example, the program defines three facts: man(john), woman(mary), and capital_of(france, paris)
. These facts state that John is a man, Mary is a woman and Paris is the capital of France.
The program also defines a rule using the not/2 predicate. This rule states that if X is a man and Y is a woman, then X is not Y.
Finally, the program includes two queries. The first query, not(john, mary)?, asks the interpreter to determine whether John is not Mary, based on the not/2 rule and the man/1 and woman/1 facts. The
interpreter will use these rules and facts to deduce that John is not Mary, and it will return true as the solution to the query.
Overall, this Prolog program demonstrates how to define rules and facts about a problem domain, and how to use those rules and facts to automatically infer solutions to problems.
Example AI application
One possible example of an AI application in Prolog is a simple diagnostic tool for medical conditions. In this
application, the Prolog program would define a set of rules and facts about different medical conditions and
their symptoms, and then use those rules and facts to diagnose a patient’s condition based on their reported
symptoms.
Here is a simple example of a Prolog program that could be used for this purpose:
% Facts
has_symptom(flu, fever).
has_symptom(flu, headache).
has_symptom(flu, body_aches).
has_symptom(flu, cough).
has_symptom(flu, sore_throat).
has_symptom(flu, runny_nose).
has_symptom(allergy, sneezing).
has_symptom(allergy, watery_eyes).
has_symptom(allergy, runny_nose).
has_symptom(allergy, itchy_eyes).
has_symptom(cold, sneezing).
has_symptom(cold, watery_eyes).
has_symptom(cold, runny_nose).
has_symptom(cold, cough).
has_symptom(cold, sore_throat).
% Rule
has_condition(X,C) :- has_symptom(C,X).
% Query
has_condition(sneezing, X)?
In this example, the program defines a set of facts that describe the symptoms of three different medical
conditions: the flu, allergies and the common cold. The program also defines a rule using the has_condition/2
predicate, which states that if a patient has a certain symptom, then they have the medical condition that is
associated with that symptom.
Finally, the program includes a query that asks the interpreter to determine which medical condition a patient
has based on their reported symptoms. In this case, the query specifies that the patient has the symptom of
sneezing, and it asks the interpreter to determine which medical condition the patient has. The interpreter will
use the has_condition/2 rule and the has_symptom/2 facts to deduce that the patient has either the flu,
allergies or the common cold, and it will return one of these conditions as the solution to the query.
This simple Prolog program demonstrates how the language can be used to develop an AI application that can
diagnose medical conditions based on symptoms. Of course, in a real-world application, the program would
need to be much more comprehensive and sophisticated, with a larger set of rules and facts and the ability to
handle a wider range of symptoms and conditions.
Application Domains of Prolog
Intelligent Database Retrieval
Natural Language Understanding
Specification Language
Machine Learning
Robot Planning
Automation System
Problem Solving
Advantages
● Declarative Paradigm: Prolog follows a declarative programming paradigm, allowing developers to focus on
describing the problem domain rather than specifying step-by-step instructions. This can lead to concise and elegant
code.
● Built-in Backtracking: Prolog's built-in backtracking mechanism allows it to explore multiple solutions to a problem
automatically. This can be particularly useful for search problems and constraint satisfaction.
● Pattern Matching: Prolog's pattern matching capabilities enable powerful and flexible rule-based programming. It
allows developers to specify relationships and constraints concisely using pattern matching rules.
● Logical Reasoning: Prolog is well-suited for implementing systems that require logical reasoning, such as expert
systems, natural language processing, and theorem proving.
● Interoperability: Prolog can be easily integrated with other programming languages, databases, and external libraries,
allowing developers to leverage existing code and infrastructure.
Shortcomings
● Efficiency: Prolog may not be as efficient as other programming languages for certain types of tasks, especially
those requiring intensive computation or data manipulation. Its execution model, based on backtracking and
unification, can lead to inefficiencies in some cases.
● Steep Learning Curve: Prolog's paradigm and syntax, particularly its non-standard syntax and logical reasoning
approach, can present a steep learning curve for developers accustomed to imperative or object-oriented
programming.
● Limited Tooling and Libraries: Compared to mainstream programming languages, Prolog has a smaller ecosystem of
tools, libraries, and frameworks. This can make it challenging to find resources and support for Prolog development.
● Difficulty in Debugging: Prolog's declarative nature and implicit control flow can make debugging challenging,
especially for complex programs. Understanding the sequence of goals and the effects of backtracking can be
non-trivial.
● Performance Concerns: While Prolog is suitable for certain types of problems, it may not be the best choice for
performance-critical applications or those requiring low-level system interaction. Its execution model may introduce
overhead compared to more traditional languages.