COMP307 Artificial Intelligence Xiaoying Gao Victoria University of Wellington Lecture 2:1 

Slides:



Advertisements
Similar presentations
Introduction to Recursion and Recursive Algorithms
Advertisements

Prolog.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
Comp 307 Lecture 4:1 Prolog I, II Prolog was taught as a procedural language Control structures: if, while, recursion Data structures: structured terms,
LING 388: Language and Computers Sandiway Fong Lecture 2: 8/24.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
CS 101 Course Summary December 5, Big Ideas Abstraction Problem solving Fundamentals of programming.
Shell Programming Learning Objectives: 1. To understand the some basic utilities of UNIX File 2. To compare UNIX shell and popular shell 3. To learn the.
Guide To UNIX Using Linux Third Edition
Programming Paradigms Imperative programming Functional programming Logic programming Event-driven programming Object-oriented programming A programming.
Prolog Programming Lecture Module 13. Objective ● What is Prolog? ● Prolog program ● Syntax of Prolog ● Prolog Control Strategy ● Execution of Prolog.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
Languages and Environments Higher Computing Unit 2 – Software Development.
School of Computing and Mathematics, University of Huddersfield Computing Science: WEEK 17 Announcement: next few weeks… 9 nd Feb: Comparative Programming.
High-Level Programming Languages: C++
Formal Models of Computation Part II The Logic Model
CIS Computer Programming Logic
Programming. What is a Program ? Sets of instructions that get the computer to do something Instructions are translated, eventually, to machine language.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
1 Lecture 15: Introduction to Logic Programming with Prolog (Section 11.3) A modification of slides developed by Felix Hernandez-Campos at UNC Chapel Hill.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
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.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
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.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
CT214 – Logical Foundations of Computing Lecture 8 Introduction to Prolog.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Programming Languages. A Logic Programming Language: Prolog This lesson describes the basic structures and functions of the logic programming language:
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
CS4026 Formal Models of Computation Part II The Logic Model Lecture 2 – Prolog: History and Introduction.
CS 603: Programming Languages Lecture 25 Spring 2004 Department of Computer Science University of Alabama Joel Jones.
C++ Lecture 1 Friday, 4 July History of C++ l Built on top of C l C was developed in early 70s from B and BCPL l Object oriented programming paradigm.
I Power Higher Computing Software Development Development Languages and Environments.
Lecture 9b: Prolog Heshaam Faili University of Tehran The language of logic Concepts Examples.
Prolog Program Style (ch. 8) Many style issues are applicable to any program in any language. Many style issues are applicable to any program in any language.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
Introduction Copyright © Software Carpentry This work is licensed under the Creative Commons Attribution License See
Logical and Functional Programming
UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:
Introduction to Prolog. Outline What is Prolog? Prolog basics Prolog Demo Syntax: –Atoms and Variables –Complex Terms –Facts & Queries –Rules Examples.
Comp 307: Artificial Intelligence Lecture 3:1 COMP307 Assignment 1 is handed out. Tutorial this week: – Prolog: lists – Help with Q1, Q2.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Logic Programming Dr. Yasser Nada Fall 2010/2011 Lecture 1 1 Logic Programming.
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.
For Friday No reading Prolog Handout 2. Homework.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
A Short History of PL's (MacLennan 1999) “Zeroth Generation” (1940's-50's): machine language / assembly: if/then, read/write, store, goto
Done By :- -Nesreen Basem -Sara nabil -Rawan Prolog Program.
1 Artificial Intelligence CS370D Prolog programming Introduction to Prolog.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
Recursion Salim Arfaoui.
Chapter 15 Recursion.
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Java 4/4/2017 Recursion.
Recursion, Tail Recursion
CS Programming I Jim Williams, PhD.
Dr. Yasser Nada Fall 2010/2011 Lecture 1
Main() { int fact; fact = Factorial(4); } main fact.
Representations & Reasoning Systems (RRS) (2.2)
Presentation transcript:

COMP307 Artificial Intelligence Xiaoying Gao Victoria University of Wellington Lecture 2:1 

Comp 307: Artificial IntelligenceLecture 2:2 Prolog (I) Introduction to Prolog – Facts, rules, queries – Matching and backtracking The procedural meaning of Prolog – “if” in Prolog – “while” in Prolog – Recursion in Prolog Reading: Prolog documentation on course home page – The SWI-Prolog user’s manual How to start Prolog on Unix, see 2.1 –edit a file: myfile.pl –command line “prolog” or “swi-prolog” –load a file and compile ?-[myfile]. How to use it with emacs, see 2.5 – Learning Prolog online course

Comp 307: Artificial IntelligenceLecture 2:3 Introduction to Prolog A logic programming language human(john). human(mary). alive(john). alive(mary). age(mary, 20). breathe(X):- human(X), alive(X). ?- breathe(mary). yes

Comp 307: Artificial IntelligenceLecture 2:4 Prolog Basics Program: – Facts predicate(arguments). – Rules Head:- Body. Arguments: – Constants begin with lower case letter, or in ‘ ’ – Variables begin with upper case letter or _ – Free Variable: _ Queries: the questions that a program answers

Comp 307: Artificial IntelligenceLecture 2:5 Prolog Prolog Program – is not a sequence of commands. – like a knowledge base--a collection of assertions. The Prolog system performs mechanical reasoning based on these assertions. – Matching & Backtracking ?-human(john). yes ?-human(jim). no. ?-human(X). X=john ; X=mary ; no. ?-breathe(john). yes ?-breathe(jim). no ?-breathe(X). X=john; X=mary; no

Comp 307: Artificial IntelligenceLecture 2:6 Java vs Prolog OOP Event-driven Array While If Recursion Data Fields & local variables Data type Files Input/Output Comments

Comp 307: Artificial IntelligenceLecture 2:7 Procedural vs Declarative Procedural Numeric Programming = Data Structure + Algorithm Java C, C++ Pascal Fortran Basic … Declarative Non-numeric, Symbolic Programming = facts + rules Prolog

Comp 307: Artificial IntelligenceLecture 2:8 The Procedural meaning of Prolog Input/Output, “call a method” in Prolog /* rectangle example*/ rectangle_example:- write('Enter the width: '), read(W), write('Enter the height: '), read(H), calculate_area(W, H, Area), format("The area is ~w~n", [Area]). calculate_area(W, H, X):- X is W*H. |?-rectangle_example.

Comp 307: Artificial IntelligenceLecture 2:9 “if” in Prolog Multiple clauses instead of “if” rectangle_example:- write('Enter the width: '), read(W), write('Enter the height: '), read(H), rectangle_or_square(W, H). rectangle_or_square(X, X):- write('this is a square'). rectangle_or_square(X, Y):- X=\=Y, write('this is a rectangle'). |?-rectangle_example.

Comp 307: Artificial IntelligenceLecture 2:10 “if– else” in Prolog Multiple clauses and “cut” instead of “if-else” rectangle_example:- write('Enter the width: '), read(W), write('Enter the height: '), read(H), rectangle_or_square(W, H). rectangle_or_square(X, X):-!, write('this is a square'). rectangle_or_square(_, _):- write('this is a rectangle'). |?-rectangle_example.

Comp 307: Artificial IntelligenceLecture 2:11 “While” and Recursion Loop: Break problem into a sequence of sub-problems, all sub- problems are the same. Recursion: Break problem into sub-problems, some of which are smaller versions of the same problem Every loop is a recursion

Comp 307: Artificial IntelligenceLecture 2:12 “while” in prolog “Tail Recursion” instead of “while” Base case: stop Normal case: do one step change the arguments call the same clause using different arguments print_stars(0):-!. print_stars(N):- write(‘*’), N1 is N-1, print_stars(N1). |?-print_stars(5).

Comp 307: Artificial IntelligenceLecture 2:13 Recursion in Java Recursive method in Java: a method that calls itself public int factorial(int n) { if (n == 0) return 1; else { int f = factorial (n-1); return n * f; } n! = 1 * 2 * 3 *...* n n! = n * (n-1)! 0!=1

Comp 307: Artificial IntelligenceLecture 2:14 Recursion in Prolog factorial(0, 1):-!. factorial(N, X):- N1 is N-1, factorial(N1, X1), X is N * X1. |?-factorial(4, X). X=24. Base case: do something Normal case: change the arguments call the same clause using different arguments do one step