principles of programming languages lecture 13: paradigms...
TRANSCRIPT
Principles of Programming LanguagesLecture 13: Paradigms: Logic Programming.
Andrei Arusoaie1
1Department of Computer Science
January 14, 2019
Outline
Paradigms
Logic Programming
Outline
Paradigms
Logic Programming
Paradigms
I Imperative Programming:
I Object Oriented Programming:I Functional Programming:I Logic Programming
Paradigms
I Imperative Programming:I Object Oriented Programming:
I Functional Programming:I Logic Programming
Paradigms
I Imperative Programming:I Object Oriented Programming:I Functional Programming:
I Logic Programming
Paradigms
I Imperative Programming:I Object Oriented Programming:I Functional Programming:I Logic Programming
General facts about PLs
We have seen PLs where the computations are based on:
I state + modifiable variable + assignmentI higher-order + recursion
Are there other approaches?
I Yes: Logic programming
General facts about PLs
We have seen PLs where the computations are based on:
I state + modifiable variable + assignmentI higher-order + recursion
Are there other approaches?
I Yes: Logic programming
General facts about PLs
We have seen PLs where the computations are based on:
I state + modifiable variable + assignment
I higher-order + recursion
Are there other approaches?
I Yes: Logic programming
General facts about PLs
We have seen PLs where the computations are based on:
I state + modifiable variable + assignmentI higher-order + recursion
Are there other approaches?
I Yes: Logic programming
General facts about PLs
We have seen PLs where the computations are based on:
I state + modifiable variable + assignmentI higher-order + recursion
Are there other approaches?
I Yes: Logic programming
General facts about PLs
We have seen PLs where the computations are based on:
I state + modifiable variable + assignmentI higher-order + recursion
Are there other approaches?
I Yes
: Logic programming
General facts about PLs
We have seen PLs where the computations are based on:
I state + modifiable variable + assignmentI higher-order + recursion
Are there other approaches?
I Yes: Logic programming
Some background
I Computation vs. DeductionI Computation: 10 + 0 = 10 (using some predefined rules,
i.e., n + 0 = 0)I Deduction: requires more than automatic application of
rules, i.e., needs human ingenuity to completeI Example: a2 + b2 = c2 ... Pythagoras→ first proof
Some background
I Computation vs. Deduction
I Computation: 10 + 0 = 10 (using some predefined rules,i.e., n + 0 = 0)
I Deduction: requires more than automatic application ofrules, i.e., needs human ingenuity to complete
I Example: a2 + b2 = c2 ... Pythagoras→ first proof
Some background
I Computation vs. DeductionI Computation: 10 + 0 = 10 (using some predefined rules,
i.e., n + 0 = 0)
I Deduction: requires more than automatic application ofrules, i.e., needs human ingenuity to complete
I Example: a2 + b2 = c2 ... Pythagoras→ first proof
Some background
I Computation vs. DeductionI Computation: 10 + 0 = 10 (using some predefined rules,
i.e., n + 0 = 0)I Deduction: requires more than automatic application of
rules
, i.e., needs human ingenuity to completeI Example: a2 + b2 = c2 ... Pythagoras→ first proof
Some background
I Computation vs. DeductionI Computation: 10 + 0 = 10 (using some predefined rules,
i.e., n + 0 = 0)I Deduction: requires more than automatic application of
rules, i.e., needs human ingenuity to complete
I Example: a2 + b2 = c2 ... Pythagoras→ first proof
Some background
I Computation vs. DeductionI Computation: 10 + 0 = 10 (using some predefined rules,
i.e., n + 0 = 0)I Deduction: requires more than automatic application of
rules, i.e., needs human ingenuity to completeI Example: a2 + b2 = c2
... Pythagoras→ first proof
Some background
I Computation vs. DeductionI Computation: 10 + 0 = 10 (using some predefined rules,
i.e., n + 0 = 0)I Deduction: requires more than automatic application of
rules, i.e., needs human ingenuity to completeI Example: a2 + b2 = c2 ... Pythagoras→ first proof
Some background: FOL
Reminder:I Terms:
I f (t1, . . . , tn) – where f has arity n, and can contain variablesI variables
I Predicates: p(t1, . . . , tn) – where p is a predicate of arity nI Formulas: φ := p(t1, . . . , tn) | ¬φ |φ ∧ φ | ∃x .φ
Some background: FOL
Reminder:
I Terms:I f (t1, . . . , tn) – where f has arity n, and can contain variablesI variables
I Predicates: p(t1, . . . , tn) – where p is a predicate of arity nI Formulas: φ := p(t1, . . . , tn) | ¬φ |φ ∧ φ | ∃x .φ
Some background: FOL
Reminder:I Terms:
I f (t1, . . . , tn) – where f has arity n, and can contain variables
I variablesI Predicates: p(t1, . . . , tn) – where p is a predicate of arity nI Formulas: φ := p(t1, . . . , tn) | ¬φ |φ ∧ φ | ∃x .φ
Some background: FOL
Reminder:I Terms:
I f (t1, . . . , tn) – where f has arity n, and can contain variablesI variables
I Predicates: p(t1, . . . , tn) – where p is a predicate of arity nI Formulas: φ := p(t1, . . . , tn) | ¬φ |φ ∧ φ | ∃x .φ
Some background: FOL
Reminder:I Terms:
I f (t1, . . . , tn) – where f has arity n, and can contain variablesI variables
I Predicates: p(t1, . . . , tn) – where p is a predicate of arity n
I Formulas: φ := p(t1, . . . , tn) | ¬φ |φ ∧ φ | ∃x .φ
Some background: FOL
Reminder:I Terms:
I f (t1, . . . , tn) – where f has arity n, and can contain variablesI variables
I Predicates: p(t1, . . . , tn) – where p is a predicate of arity nI Formulas: φ := p(t1, . . . , tn) | ¬φ |φ ∧ φ | ∃x .φ
Some background
Reminder:I Inference rules:
C1 C2 . . . Cn
HRULENAME
I Examples:
nat(0)ZERO
nat(N)
nat(s(N))SUCC
even(0)EVEN0
even(N)
even(s(s(N)))EVENSUCC
Some background
Reminder:
I Inference rules:C1 C2 . . . Cn
HRULENAME
I Examples:
nat(0)ZERO
nat(N)
nat(s(N))SUCC
even(0)EVEN0
even(N)
even(s(s(N)))EVENSUCC
Some background
Reminder:I Inference rules:
C1 C2 . . . Cn
HRULENAME
I Examples:
nat(0)ZERO
nat(N)
nat(s(N))SUCC
even(0)EVEN0
even(N)
even(s(s(N)))EVENSUCC
Some background
Reminder:I Inference rules:
C1 C2 . . . Cn
HRULENAME
I Examples:
nat(0)ZERO
nat(N)
nat(s(N))SUCC
even(0)EVEN0
even(N)
even(s(s(N)))EVENSUCC
Some background
Reminder:I Inference rules:
C1 C2 . . . Cn
HRULENAME
I Examples:
nat(0)ZERO
nat(N)
nat(s(N))SUCC
even(0)EVEN0
even(N)
even(s(s(N)))EVENSUCC
Proofs
I Proof that even(s(s(s(s(0)))) holds:
·even(0)
EVEN0
even(s(s(0)))EVENSUCC
even(s(s(s(s(0)))))EVENSUCC
I Proof strategies: goal-directed vs. forward-reasoningI Logic programming uses goal-directed: for the current
goal, search the rule that might have been applied to arriveat this conclusion
even(0)EVEN0
even(N)
even(s(s(N)))EVENSUCC
Proofs
I Proof that even(s(s(s(s(0)))) holds:
·even(0)
EVEN0
even(s(s(0)))EVENSUCC
even(s(s(s(s(0)))))EVENSUCC
I Proof strategies: goal-directed vs. forward-reasoning
I Logic programming uses goal-directed: for the currentgoal, search the rule that might have been applied to arriveat this conclusion
even(0)EVEN0
even(N)
even(s(s(N)))EVENSUCC
Proofs
I Proof that even(s(s(s(s(0)))) holds:
·even(0)
EVEN0
even(s(s(0)))EVENSUCC
even(s(s(s(s(0)))))EVENSUCC
I Proof strategies: goal-directed vs. forward-reasoningI Logic programming uses goal-directed: for the current
goal, search the rule that might have been applied to arriveat this conclusion
even(0)EVEN0
even(N)
even(s(s(N)))EVENSUCC
Logic programming: general facts
I This paradigm is based on formal logic
I Programs are sets of sentences in the logical form ofclauses (referred to as rules):I H :−C1, . . . ,C2.I H is the head of the clauseI C1, . . . ,C2 is the bodyI H. denotes a clause without body, a.k.a. facts
I Logic programming can be seen as “controlled” deductionI Logic programming languages (or families of PL): Prolog,
ASP (Answer set programming), Datalog
Logic programming: general facts
I This paradigm is based on formal logicI Programs are sets of sentences in the logical form of
clauses (referred to as rules):
I H :−C1, . . . ,C2.I H is the head of the clauseI C1, . . . ,C2 is the bodyI H. denotes a clause without body, a.k.a. facts
I Logic programming can be seen as “controlled” deductionI Logic programming languages (or families of PL): Prolog,
ASP (Answer set programming), Datalog
Logic programming: general facts
I This paradigm is based on formal logicI Programs are sets of sentences in the logical form of
clauses (referred to as rules):I H :−C1, . . . ,C2.
I H is the head of the clauseI C1, . . . ,C2 is the bodyI H. denotes a clause without body, a.k.a. facts
I Logic programming can be seen as “controlled” deductionI Logic programming languages (or families of PL): Prolog,
ASP (Answer set programming), Datalog
Logic programming: general facts
I This paradigm is based on formal logicI Programs are sets of sentences in the logical form of
clauses (referred to as rules):I H :−C1, . . . ,C2.I H is the head of the clause
I C1, . . . ,C2 is the bodyI H. denotes a clause without body, a.k.a. facts
I Logic programming can be seen as “controlled” deductionI Logic programming languages (or families of PL): Prolog,
ASP (Answer set programming), Datalog
Logic programming: general facts
I This paradigm is based on formal logicI Programs are sets of sentences in the logical form of
clauses (referred to as rules):I H :−C1, . . . ,C2.I H is the head of the clauseI C1, . . . ,C2 is the body
I H. denotes a clause without body, a.k.a. factsI Logic programming can be seen as “controlled” deductionI Logic programming languages (or families of PL): Prolog,
ASP (Answer set programming), Datalog
Logic programming: general facts
I This paradigm is based on formal logicI Programs are sets of sentences in the logical form of
clauses (referred to as rules):I H :−C1, . . . ,C2.I H is the head of the clauseI C1, . . . ,C2 is the bodyI H. denotes a clause without body, a.k.a. facts
I Logic programming can be seen as “controlled” deductionI Logic programming languages (or families of PL): Prolog,
ASP (Answer set programming), Datalog
Logic programming: general facts
I This paradigm is based on formal logicI Programs are sets of sentences in the logical form of
clauses (referred to as rules):I H :−C1, . . . ,C2.I H is the head of the clauseI C1, . . . ,C2 is the bodyI H. denotes a clause without body, a.k.a. facts
I Logic programming can be seen as “controlled” deduction
I Logic programming languages (or families of PL): Prolog,ASP (Answer set programming), Datalog
Logic programming: general facts
I This paradigm is based on formal logicI Programs are sets of sentences in the logical form of
clauses (referred to as rules):I H :−C1, . . . ,C2.I H is the head of the clauseI C1, . . . ,C2 is the bodyI H. denotes a clause without body, a.k.a. facts
I Logic programming can be seen as “controlled” deductionI Logic programming languages (or families of PL): Prolog,
ASP (Answer set programming), Datalog
Logic programming: how it works
I Prolog: early 1970, Alain Colmerauer and Robert KowalskiI Demo in Prolog: goals.
nat(0)ZERO
nat(N)
nat(s(N))SUCC
even(0)EVEN0
even(N)
even(s(s(N)))EVENSUCC
I Proof search: goal-directed
Logic programming: how it works
I Prolog: early 1970, Alain Colmerauer and Robert KowalskiI Demo in Prolog: goals.
nat(0)ZERO
nat(N)
nat(s(N))SUCC
even(0)EVEN0
even(N)
even(s(s(N)))EVENSUCC
I Proof search: goal-directed
Substitutions
I σ = {x 7→ 3, y 7→ y ′, z 7→ f (a,b)} - substitution
I Let T1 = g(h(z, y), x , y) be a termI Then, σ(T1) = σ(g(h(z, y), x , y)) = g(h(f (a,b), y ′),3, y ′)).
Substitutions
I σ = {x 7→ 3, y 7→ y ′, z 7→ f (a,b)} - substitutionI Let T1 = g(h(z, y), x , y) be a term
I Then, σ(T1) = σ(g(h(z, y), x , y)) = g(h(f (a,b), y ′),3, y ′)).
Substitutions
I σ = {x 7→ 3, y 7→ y ′, z 7→ f (a,b)} - substitutionI Let T1 = g(h(z, y), x , y) be a termI Then, σ(T1) = σ(g(h(z, y), x , y)) = g(h(f (a,b), y ′),3, y ′)).
Unification
I When proving, the conclusion of an inference rule matchesthe current goal.
I How?I Unification
I A substitution σ is the unifier of t1 and t2 if σ(t1) = σ(t2).I Various algorithms for unification (Robinson, . . . )
Unification
I When proving, the conclusion of an inference rule matchesthe current goal.
I How?I Unification
I A substitution σ is the unifier of t1 and t2 if σ(t1) = σ(t2).I Various algorithms for unification (Robinson, . . . )
Answer substitutions
I Unification→ variables in goals, i.e, even(s(s(X )).
I Demo in Prolog: queries.
even(0)EVEN0
even(N)
even(s(s(N)))EVENSUCC
Other aspects:I Backtracking: more than one possible rule applicableI Subgoal order: which of the premises to consider?
Answer substitutions
I Unification→ variables in goals, i.e, even(s(s(X )).I Demo in Prolog: queries.
even(0)EVEN0
even(N)
even(s(s(N)))EVENSUCC
Other aspects:I Backtracking: more than one possible rule applicableI Subgoal order: which of the premises to consider?
Answer substitutions
I Unification→ variables in goals, i.e, even(s(s(X )).I Demo in Prolog: queries.
even(0)EVEN0
even(N)
even(s(s(N)))EVENSUCC
Other aspects:
I Backtracking: more than one possible rule applicableI Subgoal order: which of the premises to consider?
Answer substitutions
I Unification→ variables in goals, i.e, even(s(s(X )).I Demo in Prolog: queries.
even(0)EVEN0
even(N)
even(s(s(N)))EVENSUCC
Other aspects:I Backtracking: more than one possible rule applicable
I Subgoal order: which of the premises to consider?
Answer substitutions
I Unification→ variables in goals, i.e, even(s(s(X )).I Demo in Prolog: queries.
even(0)EVEN0
even(N)
even(s(s(N)))EVENSUCC
Other aspects:I Backtracking: more than one possible rule applicableI Subgoal order: which of the premises to consider?
Demo
Light meal problem
I Facts:meat(steak, 10).meat(pork, 12).fish(tuna, 2).fish(trout, 4).starter(salad, 1).starter(soup, 3).desert(fruit, 1).desert(cake, 10).desert(icecream, 4).
Demo
Light meal problem
I Main course:main_course(N, K) :- meat(N, K);fish(N, K).
I A simple meal:meal(S, M, D) :- starter(S, SK),
main_course(M, MK),desert(D, DK).
I A light meal:light_meal(S, M, D) :- starter(S, SK),
main_course(M, MK),desert(D, DK),SK + MK + DK < 10.
Demo
Light meal problem
I Main course:main_course(N, K) :- meat(N, K);fish(N, K).
I A simple meal:meal(S, M, D) :- starter(S, SK),
main_course(M, MK),desert(D, DK).
I A light meal:light_meal(S, M, D) :- starter(S, SK),
main_course(M, MK),desert(D, DK),SK + MK + DK < 10.
Demo
Light meal problem
I Main course:main_course(N, K) :- meat(N, K);fish(N, K).
I A simple meal:meal(S, M, D) :- starter(S, SK),
main_course(M, MK),desert(D, DK).
I A light meal:light_meal(S, M, D) :- starter(S, SK),
main_course(M, MK),desert(D, DK),SK + MK + DK < 10.
Resources
Reading material:I Chapter/Lecture 1: https://people.mpi-sws.org/
~dg/teaching/lis2014/modules/lp-fp-07.pdf