SlideShare a Scribd company logo
ICS 313 - Fundamentals of Programming Languages 1
16. Logical Programming
Logic Programming
Neither imperative nor functional
Deals with relations, not functions
Arguments and results are treated uniformly
daughter(sue,john)
lessthan(3,10)
Separate logic from control
Programmer declares what facts and relations are true
System determines how to use facts to solve problems
ICS 313 - Fundamentals of Programming Languages 2
Relations (Predicates)
A table with n ≥ 0 columns and a possibly infinite set
of rows
A tuple (a1,a2,…,an) is in a relation if ai appears in
column i, 1 ≤ i ≤ n
Example: Relation append is a set of tuples of the
form (X,Y,Z), where Z consists of the elements of X
followed by the elements of Y
Relations can be specified using Horn clauses
(rules)
Horn Clauses (Rules)
A Horn clause:
P if Q1 and Q2 and … and Qk, k ≥ 0
can be interpreted as:
The consequent, P, is true if the antecedents Q1,
Q2,…, Qk , k ≥ 0 are all true
Antecedents: conjunction of zero or more conditions that are atomic
formulae in predicate logic
Consequent: an atomic formula in predicate logic
ICS 313 - Fundamentals of Programming Languages 3
Horn Clause Terminology
Horn Clause Clause
Consequent Goal Head
Antecedents Subgoals Tail
Horn clause with no tail Fact
Horn clause with tail Rule
P if Q1 and Q2 and … and Qk p :- q1 , …, qn
Example: Links Between Languages
Fortran
Algol 60
CPL
BCPL
C
C++
Simula 67
Smalltalk-80
ICS 313 - Fundamentals of Programming Languages 4
Example Prolog Facts and Rules
A prolog program starts with a collection of facts
link(fortran, algol60)
link(algol60, cpl)
link(cpl, bcpl)
link(bcpl, c)
link(c, cplusplus)
link(algol60, simula67)
link(simula67, cplusplus)
link(simula67, smalltalk80)
path(L, L)
path(L,M) :- link(L,X), path(X,M)
In Prolog
A simple term is a number, variable or an atom that
stands for itself
All variables start with capital letters
All constants are in lower case
A compound term consists of an atom followed by a
parenthesized sequence of subterms.
The atom is called a functor
Subterms are called arguments
All predicates are in lower case
ICS 313 - Fundamentals of Programming Languages 5
Horn Clauses with Variables
Variables may appear in the antecedents and the
consequent of a Horn clause:
p(X1,X2,…,Xn) :- h(X1,X2,…,Xm)
p(X1,X2,…,Xn) :- h(X1,X2,…,Xm,Y1,Y2,…,Yk)
Queries
Simplest form: Does a particular tuple belong to a
relation
path(algol60,smalltalk80).
Can’t ask: Does a particular tuple not belong to a relation
Yes/Fail rather then Yes/No answers, where Fail indicates a
failure to deduce an answer.
More interesting: Is there an X such that a specific
clause containing X evaluates to yes
path(X,cplusplus)
ICS 313 - Fundamentals of Programming Languages 6
Examples
1 ?- consult(myrules).
myrules compiled, 0.00 sec, 1,640 bytes.
Yes
2 ?- path(X,cplusplus).
X = cplusplus ;
X = fortran ;
X = fortran ;
X = algol60 ;
X = cpl ;
X = bcpl ;
X = c ;
X = algol60 ;
X = simula67 ;
No
3 ?- path(c,Y).
Y = c ;
Y = cplusplus ;
No
4 ?- path(algol60,smalltalk80).
Yes
More Examples
?- link(N,M), link(L,M).
N = fortran
M = algol60
L = fortran
Yes
?- link(N,M), link(L,M), not(L=N).
N = c
M = cplusplus
L = simula67
Yes

More Related Content

PPT
Maths in english
PPT
bisection method
PPTX
computer operating system:Greedy algorithm
PPT
Computer practical
PPT
Applications of numerical methods
PPTX
NUMERICAL & STATISTICAL METHODS FOR COMPUTER ENGINEERING
PPTX
Bisection method
PPT
Finding All Real Zeros Of A Polynomial With Examples
Maths in english
bisection method
computer operating system:Greedy algorithm
Computer practical
Applications of numerical methods
NUMERICAL & STATISTICAL METHODS FOR COMPUTER ENGINEERING
Bisection method
Finding All Real Zeros Of A Polynomial With Examples

What's hot (20)

PDF
PPT
Zeroes and roots
PPTX
Roots of polynomials
PPTX
Bisection & Regual falsi methods
DOCX
PPTX
Chapter 5 Polynomials
PDF
B02110105012
PDF
3.3 Zeros of Polynomial Functions
PDF
2. polynomial interpolation
PPTX
Nams- Roots of equations by numerical methods
PDF
SMIU Discrete Structure Lecture 3 Section 3E.pdf
PPT
Math task 3
PPT
Task 4
PDF
Lecture1
PPT
13 Boolean Algebra
PDF
AP Calculus Slides December 7, 2007
PPTX
newton raphson method
PDF
VonNeumannAlgebraPresentation
PDF
PPT
Blackbox task 2
Zeroes and roots
Roots of polynomials
Bisection & Regual falsi methods
Chapter 5 Polynomials
B02110105012
3.3 Zeros of Polynomial Functions
2. polynomial interpolation
Nams- Roots of equations by numerical methods
SMIU Discrete Structure Lecture 3 Section 3E.pdf
Math task 3
Task 4
Lecture1
13 Boolean Algebra
AP Calculus Slides December 7, 2007
newton raphson method
VonNeumannAlgebraPresentation
Blackbox task 2
Ad

Viewers also liked (20)

PPTX
Generics in .NET, C++ and Java
PPTX
PDF
9 subprograms
PDF
Auteursrecht in academische omgeving: DPO Professionaliseringsbijeenkomst, 23...
PPTX
Collections in-csharp
PDF
Raspuns MS Subprogram FIV 2016
PDF
The essence of Reactive Programming
ODP
1.7 functional programming
PDF
A basic course on Research data management: part 1 - part 4
ODP
Elm & Elixir: Functional Programming and Web
PDF
The taste of F#
PDF
Designing with Capabilities
PDF
Doge-driven design
PDF
Real-World Functional Programming @ Incubaid
PDF
The Theory of Chains
DOC
3963066 pl-sql-notes-only
PDF
16 exception handling - i
PDF
Slick 3.0 functional programming and db side effects
PDF
Functional programming
Generics in .NET, C++ and Java
9 subprograms
Auteursrecht in academische omgeving: DPO Professionaliseringsbijeenkomst, 23...
Collections in-csharp
Raspuns MS Subprogram FIV 2016
The essence of Reactive Programming
1.7 functional programming
A basic course on Research data management: part 1 - part 4
Elm & Elixir: Functional Programming and Web
The taste of F#
Designing with Capabilities
Doge-driven design
Real-World Functional Programming @ Incubaid
The Theory of Chains
3963066 pl-sql-notes-only
16 exception handling - i
Slick 3.0 functional programming and db side effects
Functional programming
Ad

Similar to 16 logical programming (20)

PDF
Hak ontoforum
PPT
KNOWLEDGE Representation unit 3 for data mining
PPT
Theory of computing
PPTX
Unit-1 (Mathematical Notations) Theory of Computation PPT
PDF
Herbrand-satisfiability of a Quantified Set-theoretical Fragment (Cantone, Lo...
PPTX
FUZZY LOGIC
DOCX
AI Lab Manual.docx
PPSX
lecture03.ppsxlecture03.ppsxlecture03.ppsxlecture03.ppsx
PDF
Du Calcul des prédicats vers Prolog
PDF
Predicate Calculus
PPT
multipleSeqAlignment.ppta4432455344534534
PDF
Programmable PN Sequence Generators
PPT
Per6 basis2_NUMBER SYSTEMS
PDF
D024025032
PDF
On observer design methods for a
PDF
The Chase in Database Theory
PDF
Programming modulo representations
PDF
Iswc 2016 completeness correctude
PDF
Automata
Hak ontoforum
KNOWLEDGE Representation unit 3 for data mining
Theory of computing
Unit-1 (Mathematical Notations) Theory of Computation PPT
Herbrand-satisfiability of a Quantified Set-theoretical Fragment (Cantone, Lo...
FUZZY LOGIC
AI Lab Manual.docx
lecture03.ppsxlecture03.ppsxlecture03.ppsxlecture03.ppsx
Du Calcul des prédicats vers Prolog
Predicate Calculus
multipleSeqAlignment.ppta4432455344534534
Programmable PN Sequence Generators
Per6 basis2_NUMBER SYSTEMS
D024025032
On observer design methods for a
The Chase in Database Theory
Programming modulo representations
Iswc 2016 completeness correctude
Automata

More from jigeno (19)

PDF
Access2007 part1
PDF
Basic introduction to ms access
PPSX
Bsit1
PDF
15 functional programming
PDF
15 functional programming
PDF
14 exception handling
PDF
13 concurrency
PDF
12 object oriented programming
PDF
11 abstract data types
PDF
8 statement-level control structure
PDF
7 expressions and assignment statements
PDF
6 data types
PDF
5 names
PDF
4 lexical and syntax analysis
PDF
3 describing syntax and semantics
PDF
2 evolution of the major programming languages
PDF
1 preliminaries
PDF
Access2007 m2
PDF
Access2007 m1
Access2007 part1
Basic introduction to ms access
Bsit1
15 functional programming
15 functional programming
14 exception handling
13 concurrency
12 object oriented programming
11 abstract data types
8 statement-level control structure
7 expressions and assignment statements
6 data types
5 names
4 lexical and syntax analysis
3 describing syntax and semantics
2 evolution of the major programming languages
1 preliminaries
Access2007 m2
Access2007 m1

Recently uploaded (20)

PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
cuic standard and advanced reporting.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PPTX
Tartificialntelligence_presentation.pptx
PDF
Empathic Computing: Creating Shared Understanding
Advanced methodologies resolving dimensionality complications for autism neur...
Digital-Transformation-Roadmap-for-Companies.pptx
Group 1 Presentation -Planning and Decision Making .pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
NewMind AI Weekly Chronicles - August'25-Week II
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
cuic standard and advanced reporting.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
MIND Revenue Release Quarter 2 2025 Press Release
MYSQL Presentation for SQL database connectivity
Building Integrated photovoltaic BIPV_UPV.pdf
Assigned Numbers - 2025 - Bluetooth® Document
Encapsulation_ Review paper, used for researhc scholars
A comparative analysis of optical character recognition models for extracting...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Tartificialntelligence_presentation.pptx
Empathic Computing: Creating Shared Understanding

16 logical programming

  • 1. ICS 313 - Fundamentals of Programming Languages 1 16. Logical Programming Logic Programming Neither imperative nor functional Deals with relations, not functions Arguments and results are treated uniformly daughter(sue,john) lessthan(3,10) Separate logic from control Programmer declares what facts and relations are true System determines how to use facts to solve problems
  • 2. ICS 313 - Fundamentals of Programming Languages 2 Relations (Predicates) A table with n ≥ 0 columns and a possibly infinite set of rows A tuple (a1,a2,…,an) is in a relation if ai appears in column i, 1 ≤ i ≤ n Example: Relation append is a set of tuples of the form (X,Y,Z), where Z consists of the elements of X followed by the elements of Y Relations can be specified using Horn clauses (rules) Horn Clauses (Rules) A Horn clause: P if Q1 and Q2 and … and Qk, k ≥ 0 can be interpreted as: The consequent, P, is true if the antecedents Q1, Q2,…, Qk , k ≥ 0 are all true Antecedents: conjunction of zero or more conditions that are atomic formulae in predicate logic Consequent: an atomic formula in predicate logic
  • 3. ICS 313 - Fundamentals of Programming Languages 3 Horn Clause Terminology Horn Clause Clause Consequent Goal Head Antecedents Subgoals Tail Horn clause with no tail Fact Horn clause with tail Rule P if Q1 and Q2 and … and Qk p :- q1 , …, qn Example: Links Between Languages Fortran Algol 60 CPL BCPL C C++ Simula 67 Smalltalk-80
  • 4. ICS 313 - Fundamentals of Programming Languages 4 Example Prolog Facts and Rules A prolog program starts with a collection of facts link(fortran, algol60) link(algol60, cpl) link(cpl, bcpl) link(bcpl, c) link(c, cplusplus) link(algol60, simula67) link(simula67, cplusplus) link(simula67, smalltalk80) path(L, L) path(L,M) :- link(L,X), path(X,M) In Prolog A simple term is a number, variable or an atom that stands for itself All variables start with capital letters All constants are in lower case A compound term consists of an atom followed by a parenthesized sequence of subterms. The atom is called a functor Subterms are called arguments All predicates are in lower case
  • 5. ICS 313 - Fundamentals of Programming Languages 5 Horn Clauses with Variables Variables may appear in the antecedents and the consequent of a Horn clause: p(X1,X2,…,Xn) :- h(X1,X2,…,Xm) p(X1,X2,…,Xn) :- h(X1,X2,…,Xm,Y1,Y2,…,Yk) Queries Simplest form: Does a particular tuple belong to a relation path(algol60,smalltalk80). Can’t ask: Does a particular tuple not belong to a relation Yes/Fail rather then Yes/No answers, where Fail indicates a failure to deduce an answer. More interesting: Is there an X such that a specific clause containing X evaluates to yes path(X,cplusplus)
  • 6. ICS 313 - Fundamentals of Programming Languages 6 Examples 1 ?- consult(myrules). myrules compiled, 0.00 sec, 1,640 bytes. Yes 2 ?- path(X,cplusplus). X = cplusplus ; X = fortran ; X = fortran ; X = algol60 ; X = cpl ; X = bcpl ; X = c ; X = algol60 ; X = simula67 ; No 3 ?- path(c,Y). Y = c ; Y = cplusplus ; No 4 ?- path(algol60,smalltalk80). Yes More Examples ?- link(N,M), link(L,M). N = fortran M = algol60 L = fortran Yes ?- link(N,M), link(L,M), not(L=N). N = c M = cplusplus L = simula67 Yes