Dr. Yasser Nada Fall 2010/2011 Lecture 5

Slides:



Advertisements
Similar presentations
Credit hours: 4 Contact hours: 50 (30 Theory, 20 Lab) Prerequisite: TB143 Introduction to Personal Computers.
Advertisements

Prolog programming....Dr.Yasser Nada. Chapter 8 Parsing in Prolog Taif University Fall 2010 Dr. Yasser Ahmed nada prolog programming....Dr.Yasser Nada.
First Order Logic Logic is a mathematical attempt to formalize the way we think. First-order predicate calculus was created in an attempt to mechanize.
Prolog Programming (Volume 2) Dr W.F. Clocksin. Lists Lists are the same as other languages (such as ML) in that a list of terms of any length is composed.
Digital Circuits. Review – Getting the truth table The first step in designing a digital circuit usually is to get the truth table. That is, for every.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
1 Logic Programming School of Informatics, University of Edinburgh Transformations Specification-Program An introduction to moving between Prolog and First.
Lecture #9 EGR 277 – Digital Logic
1 Logic Programming. 2 A little bit of Prolog Objects and relations between objects Facts and rules. Upper case are variables. parent(pam, bob).parent(tom,bob).
08/07/041 CSE-221 Digital Logic Design (DLD) Lecture-8:
Michaelmas Term 2004 Discrete Mathematics CSC 141 Discrete Mathematics Dr. Corina Sas and Ms. Nelly Bencomo
CS1101: Programming Methodology Aaron Tan.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Functions Section 1.4. Relation The value of one variable is related to the value of a second variable A correspondence between two sets If x and y are.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
Prolog programming....Dr.Yasser Nada. Chapter 7 Control and Negation Taif University Fall 2010 Dr. Yasser Ahmed Nada prolog programming....Dr.Yasser Nada.
Logic Gates M. AL-Towaileb1. Introduction Boolean algebra is used to model the circuitry of electronic devices. Each input and each output of such a device.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
Logic Programming Dr. Yasser Nada Fall 2010/2011 Lecture 1 1 Logic Programming.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
2-2 The Distributive Property Distributive Property of Multiplication over Addition : Ex. 3(2+6) Multiplication Addition You can distribute a factor to.
1 Topic #3: Lambda CSE 413, Autumn 2007 Programming Languages.
Windows Programming Lecture 03. Pointers and Arrays.
1.1 Variables and Equations Objective: To Simplify numerical expressions and evaluate algebraic expressions.
Lisp S-Expressions: ATOMs
1-1 Logic and Syntax A computer program is a solution to a problem.
Prolog Concepts.
EI205 Lecture 5 Dianguang Ma Fall 2008.
Chapter 2 Assignment and Interactive Input
Relation: a set of ordered pairs Domain: the set of x-coordinates
7.1 The Distributive Property
Dr. Clincy Professor of CS
Think What will be the output?
Algebra 1 Section 2.3 Subtract real numbers
Dr. Clincy Professor of CS
Main Points of Haskell Functional programming Laziness
Prolog fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
More Selections BIS1523 – Lecture 9.
Phil Tayco Slide version 1.0 Created Oct 2, 2017
Intro to CSC270 Survey of Programming Languages
Computers & Programming Languages
Logic Gates L Al-zaid Math110.
Applications of Propositional Logic
Dr. Clincy Professor of CS
PROGRAMMING IN HASKELL
CS Chapter 3 (3A and ) Part 3 of 8
Scheme: Basic Functionality
Chapter 3: Prolog (Lists, Arithmetic, Operators)
CS Chapter 3 (3A and ) – Part 2 of 5
Chapter 2: Intro to Relational Model
1.1 Variables and Expressions
Dr. Yasser Nada Fall 2010/2011 Lecture 1
5.2 Relations and Functions
CS Chapter 3 (3A and ) – Part 3 of 5
Logic Gates Dr.Halimah Alshehri.
Evaluating expressions and Properties of operations
Programming Techniques
Objectives The student will be able to:
LISP: Basic Functionality
Prolog Concepts.
LISP: Basic Functionality
Digital Circuits.
LISP: Basic Functionality
PROLOG.
Agenda Lecture Content: Combinatorial Circuits Boolean Algebras
Lecture 2 - Names & Functions
Presentation transcript:

Dr. Yasser Nada Fall 2010/2011 Lecture 5 Logic Programming PROLOG Dr. Yasser Nada Fall 2010/2011 Lecture 5 Logic Programming

Reversibility of Prolog Programs Consider this program: square(1,1). square(2,4). square(3,9). square(4,16). square(5,25). square(6,36). ?- square(2,X). X=4 ? ? square(X,25). X=5 ? ? PROLOG Logic Programming

Reversibility of Prolog Programs Unlike many other programming languages: No need to write new procedure for finding each argument. Program square/2 is reversible. PROLOG Logic Programming

PROLOG Evaluation in Prolog Difference between: Y is 1+2. and Y = 1+2. In Y is 1+2, the term 1+2 is evaluated to 3 and assigned to Y. In Y=1+2, the term 1+2 is unevaluated and assigned to Y. PROLOG Logic Programming

List Construction Example successor(X,Y) :- Y is X+1. is/2 is a two argument predicate. It is in the form: is(Y,X+1). is/2 is not reversible. ?- successor(3,Y). Y=4 ? ?- successor(X,4). *** Error: uninstantiated variable in arithmetic expression ?- successor(3,5). no ?- PROLOG Logic Programming

PROLOG Calling Patterns Mode successor(+,?). Mode is(?,+). +: input argument. -: output argument. ?: input or output (+ or -). Mode successor(+,?). Mode is(?,+). PROLOG Logic Programming

PROLOG List Processing Test for existence. Test All elements. Return a result – having processed one element. Return a result – having processed all elements. PROLOG Logic Programming

PROLOG Test for Existence Determine one object with a desired property among a list of objects. list_existence_test(Info, [H|T]) :- element_has_property(Info, H). list_existence_test(Info,[H|T]) :- list_existence_test(Info, T). PROLOG Logic Programming

Test for Existence Example Test if a list is a multi-list. nested_list([H|T]) :- iflist(H). nested_list([_H|T]) :- nested_list(T). iflist([]). iflist([H|T]). ?- nested_list([a, [b], c, [d], []]). yes. ?- nested_list([a,b,c]). ?- no PROLOG Logic Programming

Test for Existence Example ?- nested_list([a, [b], c, [d], []), write(y), fail. yyyno ?- If we want to display one y, then we need to use a cut (explained later). Mode nested_list(+). PROLOG Logic Programming

Test for Existence Example member(E, [E|T]). member(E,[H|T]) :- member(E,T). Equivalent to: member(E,[H|T]) :- E=H. element_has_property is E=H. Program fits in this pattern when used with mode member(+,+). PROLOG Logic Programming

Test for Existence Example an_integer([H|T]) :- integer(H). an_integer([H|T]) :- an_integer(T). ?- an_integer([a, fred, 5, yes]). Yes ?- an_integer([a,b,c]). No ?- an_integer([]). ?- PROLOG Logic Programming

PROLOG Test All Elements The elements of a list all satisfy some properties. test_all_have_property(Info,[]). test_all_have_property(Info,[Head|Tail]):- element_has_property(Info,Head), test_all_have_property(Info,Tail). PROLOG Logic Programming

Test All Elements Example testing that a list of elements consists of digits only digits([0,1,2,3,4,5,6,7,8,9]). all_digits([]). all_digits([Head|Tail]):- digits(D), member(Head,D), all_digits(Tail). member(E,[E|T]). membet(E,[H|T]) :- member(E,T). PROLOG Logic Programming

Test All Elements Example Ex 6.1 (2b): no_consonant([]). no_consonant([H|T]) :- member(H, [a,e,o,i,u]), no_consonant(T). ?- no_consonant([a,b,e]). no ?- no_consonant([a,e,i]). yes ?- PROLOG Logic Programming

Return a Result —Having Processed One Element Returning an element that satisfies a property. PROLOG return_after_event(Info,[H|T],Result):- property(Info,H), result(Info,H,T,Result). return_after_event(Info,[Head|Tail],Ans):- return_after_event(Info,Tail,Ans). Logic Programming

Return a Result —Having Processed One Element everything_after_a([H|T],R) :- H = a, R=T. everything_after_a(T,R). ?- everything_after_a([d,a,s,a,f],R). R=[s,a,f] ? ; R=[f] ? ; no ?- This can be written as: everything_after_a([a|T],T). PROLOG Logic Programming

Return a Result —Having Processed All Elements Transform each element from a list into a new element in a new list. process_all(Info,[],[]). process_all(Info,[H1|T1],[H2|T2]):- process_one(Info,H1,H2), process_all(Info,T1,T2). PROLOG Logic Programming

Return a Result —Having Processed All Elements triple([],[]). triple([H1|T1],[H2|T2]):- H2 is 3*H1, triple(T1,T2). ?- triple([1,2,3,4],R). R=[3,6,9,12] ? ?- PROLOG Logic Programming

Return a Result —Having Processed All Elements Another way to write predicate triple is: triple([],Y,Y). triple([H1|T1],L,R):- H2 is 3*H1, triple(T1,[H2|L],R). ?- triple([1,2,3,4],[],R). R=[3,6,9,12] ? ?- PROLOG Logic Programming

Homework Do exercise 6.1 (1b), (2a), 3,4 page 63-64 in the book. PROLOG Logic Programming

Questions PROLOG Logic Programming 22