3f7d9a4c610f5b23e20e31e31f2e534d.ppt
- Количество слайдов: 24
Logic Programming OLOG Mr. Khalil Aluslbi Summer 2016 Lecture 1 Logic Programming 1
Text Book • Prolog Programming, A First Course, by Paula Brna, 2001 OLOG Logic Programming 2
Course Description • This course will introduce theory and concepts of logic programming. Prolog will be studied as a logic programming approach for declarative logic programming. OLOG Logic Programming 3
Course Objectives • Upon completion of the course, student should: OLOG – Knowledge of concepts and theories of logic programming. – Become familiar with Prolog language syntax. – Be able to solve problems in Prolog. – Be able to use knowledge in a suitable form to be used in Prolog language. . Logic Programming 4
Course Policy • Students are expected to participate and attend the class in time. • Students are responsible to submit assignments in time. • Exams will be a combination of lectures in class and homework assignments. • Late homework submission will be subject to penalties. If an assignment is submitted late, a penalty of 10 percent of that assignment's grade will be assessed for each day it is late. • A homework paper will not be accepted after graded papers have been returned, after a solution has been distributed, or after the final examination. OLOG Logic Programming 5
Examination • There will be no makeup exams except under emergencies. If a student cannot attend the exam, then student must make arrangement with the instructor prior to the planned absence. The emergency makeup exam will be either written or oral. OLOG Logic Programming 6
Grading Assignment 20% project 10% Midterm 30% Final 50% OLOG Logic Programming 7
OLOG Logic Programming 8
What is Logic Programming • Two types of programming languages: – Imperative languages (C, C++, VB, C#, Java, …). – Declarative languages (prolog, lisp, …). OLOG • Logic programming is a type of programming called declarative programming. Logic Programming 9
Imperative Languages • They are also called procedural languages. • Programmer gives all steps to solve a problem. He must know an algorithm to solve a problem. • Example: find average of list of numbers: • Input total • Input number • Average = total/number • Print Average OLOG Logic Programming 10
Declarative Languages • Programmer describe the problem without the control flow, then the system will solve the problem. • Programmer must know the relations between objects to solve the problem. • Programmer does not need to know an algorithm to solve the problem. OLOG • Declarative languages consists of: – Program or theory. – Computation which is deduction. Logic Programming 11
First Order Predicate • Represent relation between objects: • • own(ahmed, car). friend(ali, ahmed). father(sami, ali). brother(kareem, ali). OLOG Logic Programming 12
Prolog • It is a declarative language (not completely declarative) based on first order logic. • Prolog means Programming in Logic. • It consists of: – Facts. – Rules. – Goal • Used in AI: NLP, expert systems, games, automated answering system, …. OLOG Logic Programming 13
Prolog • Prolog Program consist of: – Facts: asserts a property to an object, or relation between two or more objects: • parent(ali, salem). • own(ali, car). – Rules: allow to infer a relationship or a property based on a precondition: • parent(X, Y) : - father(F, X), father(P, Y). – Goal: Query or questions asked by user. • parent(ali, Y). OLOG Logic Programming 14
Atoms • Atom is – a sequence of alphanumeric characters – usually starting with lower case letter – or, a string enclosed in single quotes OLOG – They represent constants. • Examples: – ali, salem, a, b, c, a 1, a 2, b 3, c 5, … – ‘Mr. Ali’, ’Dr. Sultan’ Logic Programming 15
Variables • A variable is • a sequence of alphanumeric characters • usually starting with an uppercase letter OLOG • Examples: • X, Y, Z, Parent, Child, Foo, X 1, Y 1, X 2, X 5, X 6, …. Logic Programming 16
Predicates • A predicate has the form – p(t 1, . . . , tn) – where p is an atom and t 1. . . tn are variables or atoms. – n is the number of arguments of predicate p (written as p/n which represents the signature of predicate). – Predicates can be of 0 -arg, 1 -arg, 2 -arg, … OLOG • Examples: – father(ali, ahmed). Logic Programming 17
Prolog Program Example Predicate name like(ali, car). like(ahmed, car). Predicate arguments father(salem, ali). Predicate End father(salem, ahmed). brother(X, Y) : - father(P, X), father(P, Y). friend(X, Y) : - like(X, C), like(Y, C). OLOG and if Logic Programming 18
Goals • A goal is a conjunction of predicates – p(X, Y), q(Y, Z), t(Z, W). OLOG – A goal is the question or query asked by the user. Prolog will try to find an answer for the goal. Logic Programming 19
Answers • Given a goal, Prolog searches for answer(s): – “yes” (possibly with answer substitution) – “no” (if no answer or no more answer found). – Substitutions are bindings of variables that make goal true. OLOG Logic Programming 20
Examples • ? - father(X, ali). – X = salem ; – yes • ? - father(X, Y), brother(Y, Z). – X = salem, Y = ali, Z = ahmed – yes OLOG male(ali). male(ahmed). male(salem). father(salem, ali). father(salem, ahmed). brother(X, Y) : - father(P, X), father(P, Y), male(X), male(Y). – X = salem, Y=ahmed, Z=ali – Yes Logic Programming 21
Examples 1. Here are some simple clauses. likes(sara, food). likes(sara, song). likes(ali, sara). The following queries yield the specified answers ; ? - likes(sara, food). yes. ? - likes(ali, song). yes. ? - likes(ali, food). no. OLOG Logic Programming 22
Rules • A rule is an assertion of the form – p(ts) : - q(ts’), . . . , r(ts’’). – where ts, ts’’ are atoms or variables OLOG • “p(ts) holds if q(ts’) holds and. . . And r(ts’’) holds” • Example: – brother(X, Y) : - father(Z, X), father(Z, Y). Logic Programming 23
Miscellaneous • Comments – % single line comment – /* multiple line OLOG comment */ Logic Programming 24
3f7d9a4c610f5b23e20e31e31f2e534d.ppt