Chapter 7 Turbo Prolog Chapter 7 Turbo Prolog 323-670 Artificial Intelligence ดร. วิภาดา เวทย์ประสิทธิ์ ภาควิชาวิทยาการคอมพิวเตอร์ คณะ วิทยาศาสตร์ มหาวิทยาลัยสงขลานครินทร์

Slides:



Advertisements
Similar presentations
Chapter 4 Predicate Logics
Advertisements

Computer Programming w/ Eng. Applications
Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2.
Chapter 4 Knowledge Representation Artificial Intelligence ดร. วิภาดา เวทย์ประสิทธิ์ ภาควิชาวิทยาการคอมพิวเตอร์ คณะ วิทยาศาสตร์ มหาวิทยาลัยสงขลานครินทร์
1 COMP313A Programming Languages Logic Programming (3)
Prolog programming Introduction to Prolog
Introduction to Programming CSCE 110 Drawn from James Tam’s material.
 2005 Pearson Education, Inc. All rights reserved Introduction.
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Data types and variables
1 Key Concepts:  Data types in C.  What is a variable?  Variable Declaration  Variable Initialization  Printf()  Scanf()  Working with numbers in.
Formatted Output What we know: write(*,*) x print *, x General Form: write (unit, format, options) list unit = * : write to the monitor format = *: no.
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
Natural Language Processing Chapter Artificial IntelligenceChapter 42 NLP Language translation / multilingual translation Language understanding.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
LECTURE 3 PROLOG.
AI/ES (Artificial Intelligence / Expert System) Visual Prolog: Part Fall. SME., Pukyong Nat ’ l Univ. Kim, Minsoo.
1 The CONST definition CONST Pi = , City = ‘New York’; Constant identifiers are used when you do not want the value of an identifier to change why.
Pascal Programming Strings, Arithmetic operators and output formatting National Certificate – Unit 4 Carl Smith.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright 1999 by Larry Fuhrer. Pascal Programming Getting Started...
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lecture 6: Computer Languages. Programming Environments (IDE) COS120 Software Development Using C++ AUBG, COS dept.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Chapter 2 Syntax and meaning of Prolog Programs LP and Prolog Chapter 22 PROLOG domains variable name = type predicates relation(variable name,
Introduction Prolog program consists of clauses which are facts and rules. Predicate is the name given to the relation likes(bill, tennis) predicate name.
Introduction to Pascal The Basics of Program writing.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Programming, an introduction to Pascal
1 st semester Basic Pascal Elements อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Chapter 2 Variables.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Chapter2 Constants, Variables, and Data Types. 2.1 Introduction In this chapter, we will discuss –constants (integer, real, character, string, enum),symbolic.
1 Natural Language Processing Chapter AI & ESChapter 6 NLP 2 NLP Language translation / multilingual translation Language understanding Figure.
So now we’re programming What do programs do? Manipulate (process) data Math Read files Write to files Create files.
Artificial Intelligence CS370D
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
1 DAI-C5-UD 1 Main Objects in Programming Languages Academic Year DAI. Credit 5 (Structured and modular programming) Ferran Chic (Ref: TR )
1 TP #1: What is a Prolog program? n The SWI Prolog interpreter; n How to load a Prolog program in the interpreter; n Asking questions to the program (execution).
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Basic Elements of C++
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Chapter 2: Introduction to C++.
CS 432: Compiler Construction Lecture 11
C++ Programming Lecture 3 C++ Basics – Part I
Introduction to C Programming
How To Think Like a Prolog ?
Chapter 1 c++ structure C++ Input / Output
Presentation transcript:

Chapter 7 Turbo Prolog Chapter 7 Turbo Prolog Artificial Intelligence ดร. วิภาดา เวทย์ประสิทธิ์ ภาควิชาวิทยาการคอมพิวเตอร์ คณะ วิทยาศาสตร์ มหาวิทยาลัยสงขลานครินทร์

Artificial Intelligence Lecture 20-22Page 2 ftp:// /older/DosProgram/TPROLOG Alt + Enter = Big Screen F1 : Help F2 : Save F3 : Load F6 : Next/Switch F8 : Previous Goal F9 : Compile F10 : Step (For trace) / End Alt + T : Trace ON/OFF Set up  window size  edit Use arrow key to adjust the size TURBO PROLOG

Artificial Intelligence Lecture 20-22Page 3 Use the example from the EXAMPLE directory to try to program. Start with EX03EX01.PRO predicates likes(symbol,symbol) clauses likes(ellen, tennis). likes(john, football). likes(tom, baseball). likes(eric, swimming). FACTS likes(mark, tennis). likes(bill, Activity) if likes(tom, Activity). likes(mark, Activity) :- likes(ellen, Activity). RULES TURBO PROLOG

Artificial Intelligence Lecture 20-22Page 4 ARITHMETIC Arithmetic operators: +, -, *, /, mod, div Relational operators: >, =,, >< Functions: sin, cos, tan, arctan, ln, log, exp, sqrt, round, trunc, abs EX: = 2 + 1, X = 5/2, X = 5 mod 2, 5 <> 9 PROLOG.HELP

Artificial Intelligence Lecture 20-22Page 5 char1 byte characters integer2 byte integer numbers real8 byte floating point numbers symbolstrings inserted in the internal symbol table stringsequences of chars "hello world\n" PREDEFINED DOMAINS

Artificial Intelligence Lecture 20-22Page 6 Options must precede the other sections CONSTANTS const1 = definition const2 = definition [GLOBAL] DOMAINS dom [,dom] = [reference] declaration1; declaration2 listdom = dom* dom = [GLOBAL] DATABASE [ - ] [determ] pred1(....) pred2(.....) GLOBAL PREDICATES [determ|nondeterm] pred1( ) -(i,i,o,..)(i,o,i,..) [ language c|pascal|fortran ] [ as "name" ] pred2( ) PREDICATES [determ|nondeterm] pred1( ) pred2( ) CLAUSES p(....):-p1(...), p2(.....),.... include "filename" Include a file during compilation. SUMMARY OF PROGRAM SECTIONS

Artificial Intelligence Lecture 20-22Page 7 random(RealVariable) (real) - (o) random(MaxValue,RandomInt) (integer,integer) - (i,o) sound(Duration,Frequency) (integer,integer) - (i,i) beep date(Year,Month,Day) (integer,integer,integer) - (o,o,o) (i,i,i) time(Hours,Minutes,Seconds,Hundredths) (integer,integer,integer,integer) - (o,o,o,o) (i,i,i,i) trace(on/off) (string) - (i) (o) MISCELLANEOUS

Artificial Intelligence Lecture 20-22Page 8 trap (PredicateCall,ExitCode,Predicate ToCallOnError) exit exit (ExitCode) (integer) - (i) if exit to DOS then the DOS errorlevel task processing variable will contain the value given to the exit predicate. break (on/off) (string) - (i) (o) ERROR & BREAK CONTROL

Artificial Intelligence Lecture 20-22Page 9 display(String) (string) - (i) edit(InputString,OutputString) (string,string) - (i,o) edit(InputString,OutputString,Headstr,Headstr2,Ms g,Pos,Helpfilename, EditMode,Indent,Insert,TextMode,RetPos,RetStatu s) (string,string,string,string,string,integer,str ing,integer,integer,integer,integer,integer,int eger) - (i,o,i,i,i,i,i,i,i,i,i,o,o) If the user saves the text from the editor, HeadStr2 will be used as the file name. editmsg(InputString,OutputString,Headstr,Headstr2,Msg,Pos,Helpfilename,RetStatus) (string,string,string,string,string,integer,str ing,integer)- (i,o,i,i,i,i,i,o) EDITOR

Artificial Intelligence Lecture 20-22Page 10 makewindow(WindowNo,ScrAtt,FrameAtt,Framestr,Row,Col umn,Height,Width) (integer,integer,integer,string,integer,integer,in teger,integer) shiftwindow(WindowNo) (integer) - (i) (o) gotowindow(WindowNo) (integer) - (i) resizewindow(StartRow,NoOfRows,StartCol,NoOfCols) (integer,integer,integer,integer) - (i,i,i,i) colorsetup(Main_Frame) (integer) - (i) WINDOW SYSTEM

Artificial Intelligence Lecture 20-22Page 11 readln(StringVariable) (string) - (o) readint(IntgVariable) (integer) - (o) readreal(RealVariable) (real) - (o) readchar(CharVariable) (char) - (o) keypressed unreadchar(CharToBePushedBack) (Char) - (i) readterm( Domain, Variable ) (DomainName,Domain) - (i,_) INPUT

Artificial Intelligence Lecture 20-22Page 12 write( Variable|Constant * ) nl writef( FormatString, Variable|Constant* ) In the format string the following options are known after a percentage sign: %d Normal decimal number. (chars and integers) %u As an unsigned integer. (chars and integers) %R As a database reference number. (database reference numbers) %X As a long hexadecimal number. (strings, database reference numb). %x As a hexadecimal number. (chars and integers). %s Strings. (symbols and strings). %c As a char. (chars and integers). %g Reals in shortest posible format (default for reals) %e Reals in exponetial notation %f Reals in fixed notation %lf Only for C compatibility (fixed reals) \n - newline \t - tabulator \nnn - character with code nnn OUTPUT

Artificial Intelligence Lecture 20-22Page 13 predicates isletter(char) clauses /* When applied to characters, '<=' means */ /* "alphabetically precedes or is the same as" */ isletter(Ch) :- 'a' <= Ch, Ch <= 'z'. isletter(Ch) :- 'A' <= Ch, Ch <= 'Z'. goal : isletter( ‘ K ’ ) yes arithematics = + - * / not char : put ‘.... ’ when asking EX04EX05 is letter

Artificial Intelligence Lecture 20-22Page 14 predicates type(symbol, symbol) is_a(symbol, symbol) lives(symbol, symbol) can_swim(symbol) goal can_swim(What), write("A ", What, " can swim."). clauses type(ungulate, animal). type(fish, animal). is_a(zebra, ungulate). is_a(herring, fish). is_a(shark, fish). lives(zebra, on_land). lives(frog, on_land). lives(frog, in_water). lives(shark, in_water). can_swim(Y) :- type(X, animal), is_a(Y, X), lives(Y, in_water). write( “ ” ) write( “ \n ”,Variable, ”..... \n\n\n ” ) EX05EX05 can_swim

Artificial Intelligence Lecture 20-22Page 15 Z X Y parent sister For any X and Y, X is a sister of Y if 1. Both X and Y have the same parent, and 2. X is a female. Sister(X,Y) :- parent(Z,X), parent(Z,Y), female(X). predecessor

Artificial Intelligence Lecture 20-22Page 16 A B C E D parent predecessor

Artificial Intelligence Lecture 20-22Page 17 A B C E D predecessor(X,Z) :- parent(X,Y), parent(Y,Z). parent predecessor(X,Z) :- parent(X,Y1), parent(Y1,Y2), parent(Y2,Z). predecessor(X,Z) :- parent(X,Y1), parent(Y1,Y2), parent(Y2,Y3), parent(Y3,Z). predecessor

Artificial Intelligence Lecture 20-22Page 18 predicates parent(symbol,symbol) predecessor(symbol,symbol) clauses parent(pam,bob). parent(tom,bob). parent(bob,ann). parent(ann,jim). parent(jim,joe). parent(joe,john). parent(john,jack). parent(tom,liz). predecessor(X,Z) :- parent(X,Z). predecessor(X,Z) :- parent(X,Y), parent(Y,Z). predecessor(X,Z) :- parent(X,Y1),parent(Y1,Y2),parent(Y2,Z). predecessor(X,Z) :- parent(X,Y1),parent(Y1,Y2),parent(Y2,Y3) parent(Y3,Z). annbobpam jim john joe jack Parent1.Pro

Artificial Intelligence Lecture 20-22Page 19 A B C E D parent For all X and Z, X is a predecessor of Z if there is a Y such that 1. X is a parent of Y and 2. Y is a predecessor of Z. predecessor

Artificial Intelligence Lecture 20-22Page 20 predecessor(X,Z) :- parent(X,Z). predecessor(X,Z) :- parent(X,Y), predecessor(Y,Z). predicates parent(symbol,symbol) predecessor(symbol,symbol) clauses parent(pam,bob). parent(tom,bob). parent(bob,ann). parent(ann,jim). parent(jim,joe). parent(joe,john). parent(john,jack). parent(tom,liz). Parent2.Pro

Artificial Intelligence Lecture 20-22Page 21 Animal kingdom Plant kingdom AnimalHuman DogCat Toop Suwit Mew Sunee plant Flower RoseCarnation isa ISA Relationship

Artificial Intelligence Lecture 20-22Page 22 is(X,Z) :- isa(X,Z). is(X,Z) :- isa(X,Y), is(Y,Z). predicates isa(symbol,symbol) is(symbol,symbol) clauses isa(human,animal_kingdom). isa(plant,plant_kingdon). isa(flower,plant). isa(rose,flower). isa(carnation,flower). isa(suwit,human). isa(sunee,human). isa(dog,animal). isa(animal,animal_kingdom). isa(cat,animal). isa(toop,dog). isa(mew,cat). isa(white,cat). isa1.Pro

Artificial Intelligence Lecture 20-22Page 23 predicates big(symbol) small(symbol) brown(symbol) black(symbol) gray(symbol) dark(symbol) clauses big(bear). big(elephant). small(cat). brown(bear). black(cat). gray(elephant). dark(Z) :- black(Z). dark(Z) :- brown(Z). ?black(X),big(X) ?brown(X),big(X) ?big(X),black(X) ?black(X),big(X) No solution ?brown(X),big(X) X=bear ?big(X),black(X) No solution bear.Pro

Artificial Intelligence Lecture 20-22Page 24 domains variable name = type predicates relation(variable name, variable name, … ) goal run(X). clauses relation(value,value, … ) conclusion :- condition type symbol, integer, real, char PROLOG

Artificial Intelligence Lecture 20-22Page 25 domains brand, color = symbol age, price = integer mileage = real predicates car(brand, mileage, age, color, price) clauses car(chrysler, , 3, red, 12000). car(ford, 90000, 4, gray, 25000). car(datsun, 8000, 1, red, 30000). EX04EX02.PRO

Artificial Intelligence Lecture 20-22Page 26 domains product, sum = integer predicates add_em_up(sum, sum, sum) multiply_em(product, product, product) clauses add_em_up(X, Y, Sum) :- Sum = X + Y. multiply_em(X, Y, Product) :- Product = X * Y. SUM, MULTIPLY : EX04EX01

Artificial Intelligence Lecture 20-22Page 27 predicates run(char) goal run(X). /* This is a internal goal. */ clauses run(X) :- makewindow(1, 7, 7, " Hello World Program ", 0, 0, 25, 80), write("Hello World (first)"), readchar(X), removewindow. run(X) :- write("hello World (second)"), readchar(X). Window1.PRO = EX04EX03.PRO

Artificial Intelligence Lecture 20-22Page 28 predicates run(char) goal run(X). /* This is a internal goal. */ clauses run(X) :- makewindow(1,7,7,"hello",0,0,25,80), write("hello World (second)"), readchar(X). run(X) :- makewindow(1, 7, 7, " Hello World Program ", 0, 0, 25, 80), write("Hello World (first)"), readchar(X), removewindow. Window2.PRO

Artificial Intelligence Lecture 20-22Page 29 predicates run1(char) run2(char) goal run1(X), run2(Y). /* This is a internal goal. */ clauses run2(X) :- makewindow(1,7,7,"hello",0,0,25,80), write("hello World (second)"), readchar(X). run1(X) :- makewindow(1, 7, 7, " Hello World Program ", 0, 0, 25, 80), write("Hello World (first)"), readchar(X), removewindow. Window3.PRO : show 2 windows

Artificial Intelligence Lecture 20-22Page 30 End chapter 7