Declarative Programming Autumn 2014 Basic syntax and sample programs.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Chapter 11 :: Logic Languages
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
1 Basic Prolog ?- prolog prompt A :- B IMPLIES in rules; if B then A, logical AND ; logical OR -> if-then-else. END of predicate switch query/database.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
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).
Prolog Programming slides written by Dr W.F. Clocksin.
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
4. PROLOG Data Objects And PROLOG Arithmetic
Rules Simple rules. Processing rules. Multiple sub-goals. PROLOG syntax. Recursive rules.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Syntax and Meaning of Prolog Programs Notes for Ch.2 of Bratko For CSCE 580.
Artificial Intelligence Chapter 17 Knowledge-Based Systems Biointelligence Lab School of Computer Sci. & Eng. Seoul National University.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
LISP and PROLOG AI Programming Language Submitted To: Dr. Hesham El-Zoka Submitted By: Eng. Ismail Fathalla El-Gayar.
ISBN Chapter 16 Logic Programming Languages.
Logic Programming Languages
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 13 Slides Courtesy to: Peter LO.
CPS 506 Comparative Programming Languages
Formal Models of Computation Part II The Logic Model
ARTIFICIAL INTELLIGENCE [INTELLIGENT AGENTS PARADIGM] Professor Janis Grundspenkis Riga Technical University Faculty of Computer Science and Information.
1 Visual Prolog Programs LabLecture # 3 Lecturer : Sheriff Nafisa TA : Mubarakah Otbi, Duaa al Ofi, Huda al Hakami.
October 2004CSA4050 Advanced Techniques in NLP 1 CSA4050: Advanced Topics in NLP Semantics 6 Semantics of Questions and Assertions involving Quantification.
programmation en logique
Chapter 16 Logic Programming Languages. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 16 Topics Introduction A Brief Introduction to.
14/10/04 AIPP Lecture 7: The Cut1 Controlling Backtracking: The Cut Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 7 14/10/04.
1 COSC3306: Programming Paradigms Lecture 8: Declarative Programming Specifications Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.
Logic Programming Module 2AIT202 Website Lecturer: Dave Sharp Room: AG15
CSC 270 – Survey of Programming Languages Prolog Lecture 2 – Unification and Proof Search.
Programmaing in logic Apurva Patil Gayatri Kharavlikar Meenakshi Borse Manjiri Bankar Nikita Alai Shruti Govilkar.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
O A procedure: a set of axioms (rules and facts) with identical signature (predicate symbol and arity). o A logic program: a set of procedures defining.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
CIS2326 Week2: Logic and Prolog Lee McCluskey First term:
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?
Logic Programming Languages Session 13 Course : T Programming Language Concept Year : February 2011.
CS 363 Comparative Programming Languages Logic Programming Languages.
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.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 21.
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
CS 337 Programming Languages Logic Programming I (Logic, Intro to Prolog)
Programming Languages Third Edition Chapter 4 Logic Programming.
Programming Language Concepts Lecture 17 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Logic Programming.
Logic Programming Tarik Booker. What will we cover?  Introduction  Definitions  Predicate Calculus  Prolog  Applications.
ISBN Chapter 16 Logic Programming Languages.
Knowledge Based Information System
Artificial Intelligence CS370D
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
Artificial Intelligence CIS 342 The College of Saint Rose David Goldschmidt, Ph.D.
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”
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.
O A procedure: a set of axioms (rules and facts) with identical signature (predicate symbol and arity). o A logic program: a set of procedures (predicates),
Pengenalan Prolog Disampaikan Oleh : Yusuf Nurrachman, ST, MMSI.
A Conjunction is a logical operator that connects two logical terms.
Prolog a declarative language
Prolog Programming slides written by
Tests, Backtracking, and Recursion
Prolog fundamentals Module 14.2 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Logic Programming Languages
Prolog a declarative language
Prolog a declarative language
Prolog a declarative language
Logic Programming Language
Chapter 2: Prolog (Introduction and Basic Concepts)
Representations & Reasoning Systems (RRS) (2.2)
PROLOG.
Presentation transcript:

Declarative Programming Autumn 2014 Basic syntax and sample programs

Syntax of terms Term Constant Variable Structure AtomNumber alpha17 gross_pay john_smith dyspepsia + =/= ’12Q&A’ e likes(john, mary) book(dickens, Z, cricket) f(x) [1, 3, g(a), 7, 9] -(+(15, 17), t) t X Gross_pay Diagnosis _257 _ Names an individual Stands for an individual unable to be named when program is written Names an individual that has parts

Symbols used Uppercase letters A,B,C,...,Z Lowercase letters a,b,c,...,z Digits 0,1,2,...,9 Special symbols +,–,*,/,\,,=,:,.,,,&,_,~,[,],(,),... (there are some that may not be allowed - e.g. %)

Atoms string of letters, digits and _, starting with a lowercase letter string of special symbols string of any symbols, delimited by ‘

Numbers Depend from implementation... Some examples: e

Variables string of letters, digits and _, starting with an uppercase letter or _ variable _ is called anonymous variable and has a special semantic meaning

Structures parents(spot, fido, rover) The parents of Spot are Fido and Rover. Functor (an atom) of arity 3. components (any terms) It is possible to depict the term as a tree: parents roverfidospot

Structures =/=(15+X, (0*a)+(2<<5)) Some atoms have built-in operator declarations so they may be written in a syntactically convenient form. The meaning is not affected. This example looks like an arithmetic expression, but might not be. It is just a term. << 2 + a * 0 X + 15 =/= 5

More about operators Any atom may be designated an operator. The only purpose is for convenience; the only effect is how the term containing the atom is parsed. Operators are ‘syntactic sugar’. Operators have three properties: position, precedence and associativity.

Examples of operator properties PositionOperator SyntaxNormal Syntax Prefix:-2-(2) Infix:5+17+(17,5) Postfix:N!!(N) Associativity: left, right, none. X+Y+Z is parsed as (X+Y)+Z because addition is left-associative. Precedence: an integer. X+Y*Z is parsed as X+(Y*Z) because multiplication has higher precedence.

The last point about structures… Constants are structures of arity 0. badger means the same as badger()

Prolog programs Programs consist of procedures. Procedures consist of clauses. Each clause is a fact or a rule. Programs are executed by posing queries. An example…

Example elephant(george). elephant(mary). elephant(X) :- grey(X), mammal(X), hasTrunk(X). Procedure for elephant Predicate Clauses Rule Facts

Example ?- elephant(george). yes ?- elephant(jane). no Queries Replies

Clauses: Facts and Rules Head :- Body.This is a rule. Head.This is a fact. ‘if’ ‘provided that’ Full stop at the end.

Body of a (rule) clause contains goals likes(mary, X) :- human(X), honest(X). Head Body Goals

Clauses can be given a declarative reading or a procedural reading. H :- G 1, G 2, …, G n. “That H is provable follows from goals G 1, G 2, …, G n being provable.” Declarative reading: Procedural reading: Form of clause: Interpretation of clauses “To execute procedure H, the procedures called by goals G 1, G 2, …, G n are executed first.”

Sample program 1 likes(john,mary). likes(john,julie). likes(julie,john). likes(peter,jane). likes(jane,icecream). likes(jane,cat).

Sample program 1 likes(john,mary). likes(john,julie). likes(julie,john). likes(peter,jane). likes(jane,icecream). likes(jane,cat). ?-likes(john,mary).

Sample program 1 likes(john,mary). likes(john,julie). likes(julie,john). likes(peter,jane). likes(jane,icecream). likes(jane,cat). ?-likes(john,mary). yes

Sample program 1 likes(john,mary). likes(john,julie). likes(julie,john). likes(peter,jane). likes(jane,icecream). likes(jane,cat). ?-likes(jane,cat). yes

Sample program 1 likes(john,mary). likes(john,julie). likes(julie,john). likes(peter,jane). likes(jane,icecream). likes(jane,cat). ?-likes(julie,cat). no

Sample program 1 likes(john,mary). likes(john,julie). likes(julie,john). likes(peter,jane). likes(jane,icecream). likes(jane,cat). ?-likes(jane,X). X = icecream ? yes

Sample program 1 likes(john,mary). likes(john,julie). likes(julie,john). likes(peter,jane). likes(jane,icecream). likes(jane,cat). ?-likes(jane,X). X = icecream ? ; X = cat ? ; no

Sample program 1 likes(john,mary). likes(john,julie). likes(julie,john). likes(peter,jane). likes(jane,icecream). likes(jane,cat). ?-likes(X,Y),likes(Y,X). X = john, Y = julie ? yes

Sample program 1 likes(john,mary). likes(john,julie). likes(julie,john). likes(peter,jane). likes(jane,icecream). likes(jane,cat). friends(X,Y) :- likes(X,Y),likes(Y,X). ?-friends(X,Y). X = john, Y = julie ? yes

Sample program 2 parent(john,peter). parent(christine,peter). parent(jane,peter). parent(john,mary). parent(christine,mary). parent(jane,mary). parent(mary,frances). parent(mary,george). parent(frances,edward). parent(edward,isabel).

Sample program 2 parent(john,peter). parent(christine,peter). parent(jane,peter). parent(john,mary). parent(christine,mary). parent(jane,mary). parent(mary,frances). parent(mary,george). parent(frances,edward). parent(edward,isabel). ?-parent(jane,peter). yes

Sample program 2 parent(john,peter). parent(christine,peter). parent(jane,peter). parent(john,mary). parent(christine,mary). parent(jane,mary). parent(mary,frances). parent(mary,george). parent(frances,edward). parent(edward,isabel). siblings(X,Y) :- parent(X,Z),parent(Y,Z). ?-siblings(john,X). X = christine ? yes

Sample program 2 parent(john,peter). parent(christine,peter). parent(jane,peter). parent(john,mary). parent(christine,mary). parent(jane,mary). parent(mary,frances). parent(mary,george). parent(frances,edward). parent(edward,isabel). grandparent(X,Y) :- parent(X,Z),parent(Z,Y). ?-grandparent(christine,X). X = frances ? ; X = george ? ; no

Sample program 2 parent(john,peter). parent(christine,peter). parent(jane,peter). parent(john,mary). parent(christine,mary). parent(jane,mary). parent(mary,frances). parent(mary,george). parent(frances,edward). parent(edward,isabel). ancestor(X,Y) :- parent(X,Y). ancestor(X,Y) :- parent(X,Z),ancestor(Z,Y). ?-ancestor(christine,isabel). yes

Sample program 2 parent(john,peter). parent(christine,peter). parent(jane,peter). parent(john,mary). parent(christine,mary). parent(jane,mary). parent(mary,frances). parent(mary,george). parent(frances,edward). parent(edward,isabel). ancestor(X,Y) :- parent(X,Y). ancestor(X,Y) :- ancestor(X,Z),parent(Z,Y). ?-ancestor(christine,isabel). yes

Sample program 2 parent(john,peter). parent(christine,peter). parent(jane,peter). parent(john,mary). parent(christine,mary). parent(jane,mary). parent(mary,frances). parent(mary,george). parent(frances,edward). parent(edward,isabel). ancestor(X,Y) :- ancestor(X,Z),parent(Z,Y). ancestor(X,Y) :- parent(X,Y). ?-ancestor(christine,isabel).

How PROLOG answers queries How to answer a query ?-Q.? find the first fact P. or rule P:-R 1,...,R n. with lhs P unifiable with Q if P corresponds to fact, return yes together with the instantiation values of variables from Q if P corresponds to rule, answer the queries R 1,..,R n (in this order, keeping the instantiated variables from the previous ones) if all queries R1,..,Rn are successful, return yes otherwise find next fact or rule unifiable with Q if there are no more such facts or rules, return no

How PROLOG answers queries Goals and sub-goals: A logical sentence to be proved: succeed (satisfy) or fail. A goal could be a query or the conditions of a rule: the relationship between queries and rules Rule: “goal:-goal”, the relationship is “true”, but the truth values of condition and conclusion are unknown Prolog breaks a complex goal into a sequence of sub-goals to evaluate the truth value of a (complex) goal

How PROLOG answers queries Searching, Matching (unifying) and backtracking S-M-B is the way in which goals are established A goal is always broken into ground sub-goals consisting of individual predicates, which are then searched from left to right with due regard for the logical meaning. ?-a,b;c,d. Searching: systematically (depth-first) searches the knowledge to find a match for a ground sub-goal.