unit-5.1
unit-5.1
• Definition - S1-SLO1
• First-class function, Higher-order function, Pure functions, Recursion- S1-
SLO2
• Packages: Kanren, SymPy – S2-SLO 1
• PySWIP, PyDatalog – S2-SLO 2
• Other languages: Prolog, ROOP, Janus S3-SLO 1
Introduction
• A logic is a language.
• It has syntax and semantics.
• More than a language, it has inference rules.
• Syntax:
• The rules about how to form formulas;
• This is usually the easy part of a logic.
• Semantics:
• about the meaning carried by the formulas,
• mainly in terms of logical consequences.
• Inference rules
• Inference rules describe correct ways to derive
Features of Logical paradigms
• Computing takes place over the domain of all terms defined over a “universal”
alphabet.
• Values are assigned to variables by means of automatically generated substitutions,
called most general unifiers.
• These values may contain variables, called logical variables.
• The control is provided by a single mechanism: automatic backtracking.
• Strength - simplicity and Conciseness
• Weakness - has to do with the restrictions to one control
mechanism and the use of a single data type.
History of Logic Programming (LP)
• Logic Programming has roots going back to early AI researchers like John
McCarthy in the 50s & 60s
• Alain Colmerauer (France) designed Prolog as the first LP language in the early
1970s
• Bob Kowalski and colleagues in the UK evolved the language to its current form
in the late 70s
• It’s been widely used for many AI systems, but also for systems that need a fast,
efficient and clean rule based engine
• The prolog model has also influenced the database community –
• Exp datalog
General Overview
• The system solves the problem, so the programming steps themselves are kept to a
minimum;
• Proving the validity of a given program is simple.
Perspectives on logic programming
• Computations as Deduction
• Theorem Proving
• Non-procedural Programming
• Algorithms minus Control
• A Very High Level Programming Language
• A Procedural Interpretation of Declarative Specifications
Computation as Deduction
predicates
animal(being) % all animals are beings
dog(being) % all dogs are beings
die(being) % all beings die
clauses
animal(X) :- dog(X) % all dogs are animals
dog(fido). % fido is a dog
die(X) :- animal(X) % all animals die
Logic Operators
• Equivalence means that both
expressions have identical truth tables
Name Symbol Example Meaning
• Implication is like an if-then statement
negation ¬ ¬a not a • if a is true then b is true
conjunction ∩ a∩b a and b • note that this does not necessarily
mean that if a is false that b must
disjunction ∪ a∪b a or b also be false
equivalence ≡ a≡b a is equivalent • Universal quantifier says that this is true
to b no matter what x is
implication ⊃ a⊃b a implies b • Existential quantifier says that there is
an X that fulfills the statement
⊂ a⊂b b implies a
universal X.P For all X, P is
true
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)
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
Kanren
• Reference :
• https://pypi.org/project/pyswip/