CSCI 3136 Principles of Programming Languages Logic Languages - - PowerPoint PPT Presentation

csci 3136 principles of programming languages
SMART_READER_LITE
LIVE PREVIEW

CSCI 3136 Principles of Programming Languages Logic Languages - - PowerPoint PPT Presentation

CSCI 3136 Principles of Programming Languages Logic Languages (Prolog) Summer 2013 Faculty of Computer Science Dalhousie University 1 / 37 Logic Programming Concepts H B 1 , B 2 , . . . , B n 2 / 37 Logic Programming Concepts H


slide-1
SLIDE 1

CSCI 3136 Principles of Programming Languages

Logic Languages (Prolog)

Summer 2013 Faculty of Computer Science Dalhousie University

1 / 37

slide-2
SLIDE 2

Logic Programming Concepts

  • H ← B1, B2, . . . , Bn

2 / 37

slide-3
SLIDE 3

Logic Programming Concepts

  • H ← B1, B2, . . . , Bn

The semantics of this are that when Bi are all true , we can deduce that H is true as well.

3 / 37

slide-4
SLIDE 4

Logic Programming Concepts

  • H ← B1, B2, . . . , Bn

Horn clause The semantics of this are that when Bi are all true , we can deduce that H is true as well.

4 / 37

slide-5
SLIDE 5

Logic Programming Concepts

  • H ← B1, B2, . . . , Bn

Horn clause The semantics of this are that when Bi are all true , we can deduce that H is true as well.

  • C ← A, B

5 / 37

slide-6
SLIDE 6

Logic Programming Concepts

  • H ← B1, B2, . . . , Bn

Horn clause The semantics of this are that when Bi are all true , we can deduce that H is true as well.

  • C ← A, B

D ← C

6 / 37

slide-7
SLIDE 7

Logic Programming Concepts

  • H ← B1, B2, . . . , Bn

Horn clause The semantics of this are that when Bi are all true , we can deduce that H is true as well.

  • C ← A, B

D ← C D ← A, B

7 / 37

slide-8
SLIDE 8

Logic Programming Concepts

  • H ← B1, B2, . . . , Bn

Horn clause The semantics of this are that when Bi are all true , we can deduce that H is true as well.

  • C ← A, B

D ← C Resolution D ← A, B

8 / 37

slide-9
SLIDE 9

Logic Programming Concepts

  • H ← B1, B2, . . . , Bn

Horn clause The semantics of this are that when Bi are all true , we can deduce that H is true as well.

  • C ← A, B

D ← C Resolution D ← A, B During resolution, free variables may acquire values through unification with expressions in matching terms.

9 / 37

slide-10
SLIDE 10

Prolog

  • Prolog interpreter runs in the context of a database of clauses that

are assumed to be true.

10 / 37

slide-11
SLIDE 11

Prolog

  • Prolog interpreter runs in the context of a database of clauses that

are assumed to be true.

  • Clauses in a Prolog database can be classified as facts or rules, each
  • f which ends with a period.

11 / 37

slide-12
SLIDE 12

Prolog

  • Prolog interpreter runs in the context of a database of clauses that

are assumed to be true.

  • Clauses in a Prolog database can be classified as facts or rules, each
  • f which ends with a period.

takes(jane doe, his201). takes(jane doe, csci3136). takes(ajit chandra, art302). takes(ajit chandra, csci3136). classmates(X,Y) :- takes(X,Z), takes(Y,Z).

12 / 37

slide-13
SLIDE 13

Prolog

  • Prolog interpreter runs in the context of a database of clauses that

are assumed to be true.

  • Clauses in a Prolog database can be classified as facts or rules, each
  • f which ends with a period.
  • A variable looks like an identifier beginning with an uppercase letter.

takes(jane doe, his201). takes(jane doe, csci3136). takes(ajit chandra, art302). takes(ajit chandra, csci3136). classmates(X,Y) :- takes(X,Z), takes(Y,Z).

13 / 37

slide-14
SLIDE 14

Prolog

  • Prolog interpreter runs in the context of a database of clauses that

are assumed to be true.

  • Clauses in a Prolog database can be classified as facts or rules, each
  • f which ends with a period.
  • A variable looks like an identifier beginning with an uppercase letter.
  • The scope of a variable is limited to the clause in which it appears.

There are no declarations. takes(jane doe, his201). takes(jane doe, csci3136). takes(ajit chandra, art302). takes(ajit chandra, csci3136). classmates(X,Y) :- takes(X,Z), takes(Y,Z).

14 / 37

slide-15
SLIDE 15

Prolog

  • Prolog interpreter runs in the context of a database of clauses that

are assumed to be true.

  • Clauses in a Prolog database can be classified as facts or rules, each
  • f which ends with a period.
  • A variable looks like an identifier beginning with an uppercase letter.
  • The scope of a variable is limited to the clause in which it appears.

There are no declarations.

  • The token :- is the implication symbol; the comma indicates “and.”

takes(jane doe, his201). takes(jane doe, csci3136). takes(ajit chandra, art302). takes(ajit chandra, csci3136). classmates(X,Y) :- takes(X,Z), takes(Y,Z).

15 / 37

slide-16
SLIDE 16

Prolog

  • Prolog interpreter runs in the context of a database of clauses that

are assumed to be true.

  • Clauses in a Prolog database can be classified as facts or rules, each
  • f which ends with a period.
  • A variable looks like an identifier beginning with an uppercase letter.
  • The scope of a variable is limited to the clause in which it appears.

There are no declarations.

  • The token :- is the implication symbol; the comma indicates “and.”
  • One builds a database of facts and rules and then initiates execution

by giving the Prolog interpreter/compiled program a query to be answered (i.e., goal) takes(jane doe, his201). takes(jane doe, csci3136). takes(ajit chandra, art302). takes(ajit chandra, csci3136). classmates(X,Y) :- takes(X,Z), takes(Y,Z).

16 / 37

slide-17
SLIDE 17

Example Program 1

takes(jane, his201). takes(jane, csci3136). takes(ajit, art302). takes(ajit, csci3136). classmates(X,Y) :- takes(X,Z), takes(Y,Z).

17 / 37

slide-18
SLIDE 18

Example Program 1

takes(jane, his201). takes(jane, csci3136). takes(ajit, art302). takes(ajit, csci3136). classmates(X,Y) :- takes(X,Z), takes(Y,Z). What would be the output if we run: ?- classmates(jane,X).

18 / 37

slide-19
SLIDE 19

Example Program 1

takes(jane, his201). takes(jane, csci3136). takes(ajit, art302). takes(ajit, csci3136). classmates(X,Y) :- takes(X,Z), takes(Y,Z). What would be the output if we run: ?- classmates(jane,X). X = jane

19 / 37

slide-20
SLIDE 20

Example Program 1

takes(jane, his201). takes(jane, csci3136). takes(ajit, art302). takes(ajit, csci3136). classmates(X,Y) :- takes(X,Z), takes(Y,Z). What would be the output if we run: ?- classmates(jane,X). X = jane ;

20 / 37

slide-21
SLIDE 21

Example Program 1

takes(jane, his201). takes(jane, csci3136). takes(ajit, art302). takes(ajit, csci3136). classmates(X,Y) :- takes(X,Z), takes(Y,Z). What would be the output if we run: ?- classmates(jane,X). X = jane ; X = jane

21 / 37

slide-22
SLIDE 22

Example Program 1

takes(jane, his201). takes(jane, csci3136). takes(ajit, art302). takes(ajit, csci3136). classmates(X,Y) :- takes(X,Z), takes(Y,Z). What would be the output if we run: ?- classmates(jane,X). X = jane ; X = jane ;

22 / 37

slide-23
SLIDE 23

Example Program 1

takes(jane, his201). takes(jane, csci3136). takes(ajit, art302). takes(ajit, csci3136). classmates(X,Y) :- takes(X,Z), takes(Y,Z). What would be the output if we run: ?- classmates(jane,X). X = jane ; X = jane ; X = ajit.

23 / 37

slide-24
SLIDE 24

Example Program 1-1

pr(0). pr(N) :- X is N-1, pr(X), write(N),nl.

24 / 37

slide-25
SLIDE 25

Example Program 1-1

pr(0). pr(N) :- X is N-1, pr(X), write(N),nl. What would be the output if we run: ?- pr(3).

25 / 37

slide-26
SLIDE 26

Example Program 2

mem(X,[X|_]). mem(X,[_|T]):-mem(X,T).

26 / 37

slide-27
SLIDE 27

Example Program 2

mem(X,[X|_]). mem(X,[_|T]):-mem(X,T). What would be the output if we run: ?- mem(2,[1,2,3]).

27 / 37

slide-28
SLIDE 28

Example Program 2

mem(X,[X|_]). mem(X,[_|T]):-mem(X,T). What would be the output if we run: ?- mem(2,[1,2,3]). true.

28 / 37

slide-29
SLIDE 29

Example Program 3

app([],A,A). app([H|T],A,[H|L]):-app(T,A,L).

29 / 37

slide-30
SLIDE 30

Example Program 3

app([],A,A). app([H|T],A,[H|L]):-app(T,A,L). What would be the output if we run: ?- app([a,b],[d,e],L).

30 / 37

slide-31
SLIDE 31

Example Program 3

app([],A,A). app([H|T],A,[H|L]):-app(T,A,L). What would be the output if we run: ?- app([a,b],[d,e],L). L = [a, b, d, e].

31 / 37

slide-32
SLIDE 32

Example Program 4

len([ ], 0). len([_ | T], N) :- len(T, M), N is M+1.

32 / 37

slide-33
SLIDE 33

Example Program 4

len([ ], 0). len([_ | T], N) :- len(T, M), N is M+1. What would be the output if we run: ?- len([a,b], L).

33 / 37

slide-34
SLIDE 34

Example Program 4

len([ ], 0). len([_ | T], N) :- len(T, M), N is M+1. What would be the output if we run: ?- len([a,b], L). L = 2.

34 / 37

slide-35
SLIDE 35

Example Program 5

len([ ], 0). len([H |T], N) :- len(T, M), N is M+H.

35 / 37

slide-36
SLIDE 36

Example Program 5

len([ ], 0). len([H |T], N) :- len(T, M), N is M+H. What would be the output if we run: ?- len([2,3], L).

36 / 37

slide-37
SLIDE 37

Example Program 5

len([ ], 0). len([H |T], N) :- len(T, M), N is M+H. What would be the output if we run: ?- len([2,3], L). L = 5.

37 / 37