Programming Languages. A Logic Programming Language: Prolog This lesson describes the basic structures and functions of the logic programming language:

Slides:



Advertisements
Similar presentations
© Johan Bos Logic Programming About the course –Taught in English –Tuesday: mostly theory –Thursday: practical work [lab] Teaching material –Learn Prolog.
Advertisements

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.
Software Development Languages and Environments. Programming languages High level languages are problem orientated contain many English words are easier.
Prolog.
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.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Answer Set Programming Overview Dr. Rogelio Dávila Pérez Profesor-Investigador División de Posgrado Universidad Autónoma de Guadalajara
CSE 425: Logic Programming I Logic and Programs Most programs use Boolean expressions over data Logic statements can express program semantics –I.e., axiomatic.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
PROLOG Basics A Prolog program consists of a database of facts and rules, and queries (questions). Fact:.... Rule:... :-.... Query: ?-.... Variables: must.
CPSC 322 Introduction to Artificial Intelligence September 15, 2004.
CPSC 322, Lecture 23Slide 1 Logic: TD as search, Datalog (variables) Computer Science cpsc322, Lecture 23 (Textbook Chpt 5.2 & some basic concepts from.
Introduction to Prolog What is Prolog? Application Areas of Prolog How does Prolog work? (Syntax of Prolog) Program Structure.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
CPSC 322, Lecture 23Slide 1 Logic: TD as search, Datalog (variables) Computer Science cpsc322, Lecture 23 (Textbook Chpt 5.2 & some basic concepts from.
Prolog Programming Lecture Module 13. Objective ● What is Prolog? ● Prolog program ● Syntax of Prolog ● Prolog Control Strategy ● Execution of Prolog.
ISBN Chapter 16 Logic Programming Languages.
Logic Programming Languages
DEDUCTIVE DATABASE.
Formal Models of Computation Part II The Logic Model
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
Programming Paradigms
COP4020 Programming Languages Logical programming with Prolog Prof. Xin Yuan.
CS 403: Programming Languages Lecture 19 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Declarative vs Procedural Programming  Procedural programming requires that – the programmer tell the computer what to do. That is, how to get the output.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
F28PL1 Programming Languages Lecture 16: Prolog 1.
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/04.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now Chapter 9, exs 4 and 6. –6 must be in Horn clause form Prolog Handout 2.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
CSC 270 – Survey of Programming Languages Prolog Lecture 2 – Unification and Proof Search.
CHAPTER 15 & 16 Functional & Logic Programming Languages.
CT214 – Logical Foundations of Computing Lecture 8 Introduction to Prolog.
PROLOG SYNTAX AND MEANING Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
Prolog Kyle Marcotte. Outline What is Prolog? Origins of Prolog (History) Basic Tutorial TEST!!! (sort of…actually not really at all) My example Why Prolog?
3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.
The AI War LISP and Prolog Basic Concepts of Logic Programming
Rules Statements about objects and their relationships Expess ◦ If-then conditions  I use an umbrella if there is a rain  use(i, umbrella) :- occur(rain).
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
1 Knowledge Based Systems (CM0377) Lecture 6 (last modified 20th February 2002)
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.
ISBN Chapter 16 Logic Programming Languages.
Knowledge Based Information System
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
The International RuleML Symposium on Rule Interchange and Applications Visualization of Proofs in Defeasible Logic Ioannis Avguleas 1, Katerina Gkirtzou.
For Friday No reading Prolog Handout 2. Homework.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
Introduction to Prolog Asst. Prof. Dr. Senem Kumova Metin Revised lecture notes of “Concepts of Programmig Languages, Robert W. Sebesta, Ch. 16”
Abdul Rahim Ahmad MITM 613 Intelligent System Chapter 10: Tools.
Prolog Fundamentals. 2 Review Last Lecture A Prolog program consists of a database of facts and rules, and queries (questions). –Fact:.... –Rule:... :-....
Logic Programming Logic programming Candidates should be familiar with the concept of logic programming for declaring logical relationships.
Logic Programming Lecture 2: Unification and proof search.
Functional Programming
By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Prolog programming Introduction to Prolog
PROLOG.
Tests, Backtracking, and Recursion
Chapter 15 QUERY EXECUTION.
PROLOG PROgramming in LOGic
Dr. Yasser Nada Fall 2010/2011 Lecture 1
Chapter 2: Prolog (Introduction and Basic Concepts)
Representations & Reasoning Systems (RRS) (2.2)
PROLOG.
Presentation transcript:

Programming Languages

A Logic Programming Language: Prolog This lesson describes the basic structures and functions of the logic programming language: Prolog.

Overview Prolog, which stands for PROgramming in LOGic, is the most widely available language in the logic programming paradigm.Prolog, which stands for PROgramming in LOGic, is the most widely available language in the logic programming paradigm. Prolog is based the mathematical notions of relations and logical inference.Prolog is based the mathematical notions of relations and logical inference. Prolog is a declarative language meaning that rather than describing how to compute a solution, a program consists of a data base of facts and logical relationships (rules) which describe the relationships which hold for the given application.Prolog is a declarative language meaning that rather than describing how to compute a solution, a program consists of a data base of facts and logical relationships (rules) which describe the relationships which hold for the given application.

Overview (2) Rather then running a program to obtain a solution, the user asks a question.Rather then running a program to obtain a solution, the user asks a question. When asked a question, the run time system searches through the data base of facts and rules to determine (by logical deduction) the answer.When asked a question, the run time system searches through the data base of facts and rules to determine (by logical deduction) the answer. Among the features of Prolog are `logical variables' meaning that they behave like mathematical variables, a powerful pattern- matching facility (unification), a backtracking strategy to search for proofs, uniform data structures, and input and output are interchangeable.Among the features of Prolog are `logical variables' meaning that they behave like mathematical variables, a powerful pattern- matching facility (unification), a backtracking strategy to search for proofs, uniform data structures, and input and output are interchangeable.

Overview(3) Often there will be more than one way to deduce the answer or there will be more than one solution,Often there will be more than one way to deduce the answer or there will be more than one solution, in such cases the run time system may be asked find other solutions. backtracking to generate alternative solutions.in such cases the run time system may be asked find other solutions. backtracking to generate alternative solutions. Prolog is a weakly typed language with dynamic type checking and static scope rules. Prolog is a weakly typed language with dynamic type checking and static scope rules. Prolog is used in artificial intelligence applications such as natural language interfaces, automated reasoning systems and expert systems. Prolog is used in artificial intelligence applications such as natural language interfaces, automated reasoning systems and expert systems.

The Structure of Prolog Programs A Prolog program consists of a database of facts and rules, and queries (questions).A Prolog program consists of a database of facts and rules, and queries (questions). Fact:....Fact:.... Rule:... :-....Rule:... :-.... Query: ?-.... Query: ?-.... Variables: must begin with an upper case letter. Variables: must begin with an upper case letter. Constants: numbers, begin with lowercase letter, or enclosed in single quotes. Constants: numbers, begin with lowercase letter, or enclosed in single quotes.

Clauses: Facts, rules, and queries A Prolog program consists of a data base of facts and rules. A Prolog program consists of a data base of facts and rules. There is no structure imposed on a Prolog program, there is no main procedure, and there is no nesting of definitions. There is no structure imposed on a Prolog program, there is no main procedure, and there is no nesting of definitions. All facts and rules are global in scope and the scope of a variable is the fact or rule in which it appears. All facts and rules are global in scope and the scope of a variable is the fact or rule in which it appears. A Prolog program is executed by asking a question. The question is called a query. Facts, rules, and queries are called clauses. A Prolog program is executed by asking a question. The question is called a query. Facts, rules, and queries are called clauses.

Syntax Facts:Facts: 'It is sunny'. 'It is summer'. 'It is sunny'. 'It is summer'. Queries: Queries: ?- ?- 'It is sunny'. Yes ?- ?- 'It is cold'. No ?- ?- ?- 'It is sunny'. Yes ?- ?- 'It is cold'. No ?-

Rules: propositional logic Rules extend the capabilities of a logic program.Rules extend the capabilities of a logic program. They are what give Prolog the ability to pursue its decision-making process.They are what give Prolog the ability to pursue its decision-making process. 'It is sunny'. 'It is summer'. 'It is hot' :- 'It is summer', 'It is sunny'. 'It is cold' :- 'It is winter', 'It is snowing'. 'It is sunny'. 'It is summer'. 'It is hot' :- 'It is summer', 'It is sunny'. 'It is cold' :- 'It is winter', 'It is snowing'. ?- 'It is hot'. Yes ?- ?- 'It is hot'. Yes ?-

Rules: Predicate Logic Facts and rules may be parameterized to produce programs in predicate logic.Facts and rules may be parameterized to produce programs in predicate logic. The parameters may be variables, atoms, numbers, or terms.The parameters may be variables, atoms, numbers, or terms. Parameterization permits the definition of more complex relationships.Parameterization permits the definition of more complex relationships.

A family's genelogical relationships. female(amy). female(johnette). male(anthony). male(bruce). male(ogden). parentof(amy,johnette). parentof(amy,anthony). parentof(amy,bruce). parentof(ogden,johnette). parentof(ogden,anthony). parentof(ogden,bruce).female(amy). female(johnette). male(anthony). male(bruce). male(ogden). parentof(amy,johnette). parentof(amy,anthony). parentof(amy,bruce). parentof(ogden,johnette). parentof(ogden,anthony). parentof(ogden,bruce).

A family's genelogical relationships. siblingof(X,Y) :- parentof(Z,X), parentof(Z,Y), X <> Y.siblingof(X,Y) :- parentof(Z,X), parentof(Z,Y), X <> Y. brotherof(X,Y) :- parentof(Z,X), male(X), parentof(Z,Y), X <> Y.brotherof(X,Y) :- parentof(Z,X), male(X), parentof(Z,Y), X <> Y.

Composite Types Prolog does not provide an array type.Prolog does not provide an array type. Arrays may be represented as a list and multidimensional arrays as a list(s) of lists. Arrays may be represented as a list and multidimensional arrays as a list(s) of lists. An alternate representation is to represent an array as a set of facts in a the data base. An alternate representation is to represent an array as a set of facts in a the data base. A list is designated by square brackets ([ ]+). [dog,cat,mouse] A list is designated by square brackets ([ ]+). [dog,cat,mouse] Elements in a Prolog list are ordered, even though there are no indexes.Elements in a Prolog list are ordered, even though there are no indexes.

Records or tuples Records or tuples are represented as patterns. Records or tuples are represented as patterns. book( author( aaby, anthony ), title( labmanual ), data( 1991 )) book( author( aaby, anthony ), title( labmanual ), data( 1991 )) The elements of a tuple are accessed by pattern matching. The elements of a tuple are accessed by pattern matching. book(Title,Author,Publisher,Date). author(LastName,FirstName,MI). publisher(Company,City). book(Title,Author,Publisher,Date). author(LastName,FirstName,MI). publisher(Company,City). book(T,A,publisher(C,rome),Date) book(T,A,publisher(C,rome),Date)

Type Predicates Since Prolog is a weakly typed language, it is important for the user to be able to determine the type of a parameter.Since Prolog is a weakly typed language, it is important for the user to be able to determine the type of a parameter. The following built in predicates are used to determine the type of a parameter.The following built in predicates are used to determine the type of a parameter. var(V), nonvar(NV), atom(A), integer(I) real(R), number(N), atomic(A), functor(T,F,A) T is a term with functor F and arity A clause(H,T) H :- T is a rule in the program T =..L T is a term, L is a listvar(V), nonvar(NV), atom(A), integer(I) real(R), number(N), atomic(A), functor(T,F,A) T is a term with functor F and arity A clause(H,T) H :- T is a rule in the program T =..L T is a term, L is a list

functor The predicate, functor is used as follows. The predicate, functor is used as follows. functor(T,F,A) T is a term, F is its functor, and A is its arity.functor(T,F,A) T is a term, F is its functor, and A is its arity. ?- functor(t(a,b,c),F,A). F = t A = 3 yes?- functor(t(a,b,c),F,A). F = t A = 3 yes t is the functor of the term t(a,b,c), and 3 is the arity (number of arguments) of theterm.t is the functor of the term t(a,b,c), and 3 is the arity (number of arguments) of theterm.

Unification and Pattern Matching The arguments in a query are matched (or unified in Prolog terminology) to select the appropriate rule.The arguments in a query are matched (or unified in Prolog terminology) to select the appropriate rule. Prolog code is often bidirectional.Prolog code is often bidirectional. In bidirectional code, the arguments may be use either for input or output.In bidirectional code, the arguments may be use either for input or output.

Functions That is, both the arguments to the function and the result of the function must be parameters to the relation.That is, both the arguments to the function and the result of the function must be parameters to the relation. This means that composition of two functions cannot be constructed.This means that composition of two functions cannot be constructed. Prolog does not provide for a function type therefore, functions must be defined as relations.Prolog does not provide for a function type therefore, functions must be defined as relations.

Lists Lists are a recursive data structure so recursion occurs naturally in the definitions of various list operations.Lists are a recursive data structure so recursion occurs naturally in the definitions of various list operations. Operations on lists must consider the empty list as a case.Operations on lists must consider the empty list as a case. Lists are the basic data structure used in logic (and functional) programming.Lists are the basic data structure used in logic (and functional) programming. Predicates on lists are often written using multiple rules. One rule for the empty list (the base case) and a second rule for non empty lists.Predicates on lists are often written using multiple rules. One rule for the empty list (the base case) and a second rule for non empty lists.

Iteration Recursion is the only iterative method available in Prolog.Recursion is the only iterative method available in Prolog.

Extra-Logical Predicates Program Access and Manipulation: assert(Clause), retract(Clause_Head)...Program Access and Manipulation: assert(Clause), retract(Clause_Head)... System Access: system(Command) Execute Command in the operating systemSystem Access: system(Command) Execute Command in the operating system Input/Output: Most Prolog implementations provide the predicates read and write...Input/Output: Most Prolog implementations provide the predicates read and write...