principles of programming languages lecture 13: paradigms...

60
Principles of Programming Languages Lecture 13: Paradigms: Logic Programming. Andrei Arusoaie 1 1 Department of Computer Science January 14, 2019

Upload: others

Post on 18-Aug-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

Principles of Programming LanguagesLecture 13: Paradigms: Logic Programming.

Andrei Arusoaie1

1Department of Computer Science

January 14, 2019

Page 2: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

Outline

Paradigms

Logic Programming

Page 3: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

Outline

Paradigms

Logic Programming

Page 4: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

Paradigms

I Imperative Programming:

I Object Oriented Programming:I Functional Programming:I Logic Programming

Page 5: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

Paradigms

I Imperative Programming:I Object Oriented Programming:

I Functional Programming:I Logic Programming

Page 6: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

Paradigms

I Imperative Programming:I Object Oriented Programming:I Functional Programming:

I Logic Programming

Page 7: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

Paradigms

I Imperative Programming:I Object Oriented Programming:I Functional Programming:I Logic Programming

Page 8: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: 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

Page 9: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: 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

Page 10: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: 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

Page 11: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: 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

Page 12: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: 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

Page 13: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: 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

Page 14: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: 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

Page 15: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: 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

Page 16: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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

Page 17: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: 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 ofrules, i.e., needs human ingenuity to complete

I Example: a2 + b2 = c2 ... Pythagoras→ first proof

Page 18: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: 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

Page 19: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: 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 complete

I Example: a2 + b2 = c2 ... Pythagoras→ first proof

Page 20: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: 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

Page 21: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: 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

Page 22: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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 .φ

Page 23: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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 .φ

Page 24: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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 .φ

Page 25: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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 .φ

Page 26: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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 .φ

Page 27: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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 .φ

Page 28: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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

Page 29: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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

Page 30: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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

Page 31: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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

Page 32: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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

Page 33: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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

Page 34: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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

Page 35: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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

Page 36: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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

Page 37: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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

Page 38: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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

Page 39: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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

Page 40: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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

Page 41: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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

Page 42: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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

Page 43: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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

Page 44: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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

Page 45: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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

Page 46: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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 ′)).

Page 47: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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 ′)).

Page 48: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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 ′)).

Page 49: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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, . . . )

Page 50: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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, . . . )

Page 51: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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?

Page 52: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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?

Page 53: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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?

Page 54: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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?

Page 55: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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?

Page 56: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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).

Page 57: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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.

Page 58: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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.

Page 59: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

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.

Page 60: Principles of Programming Languages Lecture 13: Paradigms ...arusoaie.andrei/lectures/PLP/2018/week14… · Principles of Programming Languages Lecture 13: Paradigms: Logic Programming

Resources

Reading material:I Chapter/Lecture 1: https://people.mpi-sws.org/

~dg/teaching/lis2014/modules/lp-fp-07.pdf