csci 3136 principles of programming languages
play

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


  1. CSCI 3136 Principles of Programming Languages Logic Languages (Prolog) Summer 2013 Faculty of Computer Science Dalhousie University 1 / 37

  2. Logic Programming Concepts • H ← B 1 , B 2 , . . . , B n 2 / 37

  3. Logic Programming Concepts • H ← B 1 , B 2 , . . . , B n The semantics of this are that when B i are all true , we can deduce that H is true as well. 3 / 37

  4. Logic Programming Concepts • H ← B 1 , B 2 , . . . , B n Horn clause The semantics of this are that when B i are all true , we can deduce that H is true as well. 4 / 37

  5. Logic Programming Concepts • H ← B 1 , B 2 , . . . , B n Horn clause The semantics of this are that when B i are all true , we can deduce that H is true as well. • C ← A, B 5 / 37

  6. Logic Programming Concepts • H ← B 1 , B 2 , . . . , B n Horn clause The semantics of this are that when B i are all true , we can deduce that H is true as well. • C ← A, B D ← C 6 / 37

  7. Logic Programming Concepts • H ← B 1 , B 2 , . . . , B n Horn clause The semantics of this are that when B i are all true , we can deduce that H is true as well. • C ← A, B D ← C D ← A, B 7 / 37

  8. Logic Programming Concepts • H ← B 1 , B 2 , . . . , B n Horn clause The semantics of this are that when B i are all true , we can deduce that H is true as well. • C ← A, B D ← C Resolution D ← A, B 8 / 37

  9. Logic Programming Concepts • H ← B 1 , B 2 , . . . , B n Horn clause The semantics of this are that when B i 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

  10. Prolog • Prolog interpreter runs in the context of a database of clauses that are assumed to be true. 10 / 37

  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 of which ends with a period. 11 / 37

  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 of 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

  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 of 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

  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 of 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

  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 of 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

  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 of 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

  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

  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

  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

  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

  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

  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

  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

  24. Example Program 1-1 pr(0). pr(N) :- X is N-1, pr(X), write(N),nl. 24 / 37

  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

  26. Example Program 2 mem(X,[X|_]). mem(X,[_|T]):-mem(X,T). 26 / 37

  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

  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

  29. Example Program 3 app([],A,A). app([H|T],A,[H|L]):-app(T,A,L). 29 / 37

  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

  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

  32. Example Program 4 len([ ], 0). len([_ | T], N) :- len(T, M), N is M+1. 32 / 37

  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

  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

  35. Example Program 5 len([ ], 0). len([H |T], N) :- len(T, M), N is M+H. 35 / 37

  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

  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

Recommend


More recommend