1 COMP313A Programming Languages Logic Programming (6)

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Files Used to transfer data to and from disk. Opening an Output File Stream #include // File stream library. ofstream outfile;// Declare file stream variable.
Logic Gates.
Internship in DASAN networks C programing language Chapter 7 : Input and Output Present by Le Thi Hien 1/14.
C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on filesofstream ifstream:
4. Using Structures: Example Programs. Contents l Retrieving structured information from a database l Doing data abstraction l Simulating a non-deterministic.
Notes for CS3310 Artificial Intelligence Part 5: Prolog arithmetic and lists Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009.
Introduction to PROLOG ME 409 Lab - 1. Introduction to PROLOG.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
Introduction to ProLog File Manipulations Prof. Dr. Nagi Soliman 2009.
C++ Classes & Data Abstraction
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).
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
Logon then turn your monitor off! Creating a database A way of keeping information.
PROLOG I/O I/O and the relational model. PROLOG I/O operators. –Not really predicates. I/O with characters. I/O with terms. I/O with lists. I/O.
Switchgears Control Using SCADA System Based on PLC
CS 330 Programming Languages 12 / 12 / 2006 Instructor: Michael Eckmann.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Measuring With Jugs A Solution in Finite Domain. Intro to Mozart - Oz variables -- a set of alphanumeric characters statring with a CAPITOL letter – you.
16/27/ :53 PM6/27/ :53 PM6/27/ :53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Using Structures: Example Programs Notes for Ch.4 of Bratko For CSCE 580 Sp03.
CS510AP Quick Check Monads. QuickCheck Quick check is a Haskell library for doing random testing. You can read more about quickcheck at –

(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
4.1 Retrieving structured information from a database
ALGORITHMS AND FLOWCHARTS
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Introduction to Prolog Terms & Matching Math. Atoms and Terms n mark, alex, di, bob are atoms –Not variables –Not strings –Just things – simple things.
FATIH UNIVERSITY Department of Computer Engineering Using Structures: Example Programs Notes for Ch.4 of Bratko For CENG 421 Fall03 Zeynep Orhan.
S2008Final_part1.ppt CS11 Introduction to Programming Final Exam Part 1 S A computer is a mechanical or electrical device which stores, retrieves,
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Copyright © Cengage Learning. All rights reserved. CHAPTER 10 GRAPHS AND TREES.
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
Partitions not Working as Expected BY: LLOYD ALBIN 8/6/2013.
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.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 6: Object-Oriented Programming.
Comp763: Modern Computer Games Using Constraint Logic Programming to Analyze the Chronology in a William Faulkner Story Jennifer BurgSheau-Dong Lang Irwin.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
CS 403: Programming Languages Lecture 18 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Logic Gates. AND gate Produces an output only if both inputs are on Input AInput BOutput (Q) Q=
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.
For Friday No reading Prolog Handout 2. Homework.
AND Gate Inputs Output Input A (Switch) Input B (Switch) Output Y (Lamp) 0 (Open) 0 (OFF) A B Lamp.
1 TP #2: How does Prolog answer questions? n Miscellaneous info; n Last TP exercises solved; n How does Prolog answer questions? n Recursive Prolog programs;
Files A collection of related data treated as a unit. Two types Text
Sebastián Álvarez Henao.. It refers to all physical parts of a computer system; its components are: electrical, electronic, electromechanical and mechanical.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
1 Intro to the Shell with Fork, Exec, Wait Sarah Diesburg Operating Systems CS 3430.
Chapter One Problem Solving
Application: Digital Logic Circuits
Chapter One Problem Solving
Unit 5 Lesson 6: User Input and Strings
Programming Fundamental
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
PROLOG.
List of Topics.
Object Oriented Analysis and Design
Data and Flowcharts Session
Gates Type AND denoted by X.Y OR denoted by X + Y NOR denoted by X + Y
Early 19c Women Unable to vote. Legal status of a child.
Data and Flowcharts Session
Data and Flowcharts Session
Logically, the output will be 1
Presentation transcript:

1 COMP313A Programming Languages Logic Programming (6)

2 Some More Prolog structures

3 I/O in Prolog Files associated with input and output streams I/O from terminal – user In some Prologs only two files/streams are active at any one time –current input stream –current output stream see switches input streams tell switches output streams In prolog I/O is a side effect of the predicate

4 Tkeclipse Prolog stdin, stdout –standard input and output streams ?- write("fred"). Yes (0.00s cpu) open a stream for input or output –open(SourceSink, Mode, Stream) ? open('//C/Documents and Settings/mjeff/My Documents/prolog/fred.txt', read, Fred), read_string(Fred, "\r\n ", 10, X). Fred = 31 X = "freddy" Yes (0.00s cpu)

5 Bits and pieces Anonymous variables family(person(tom, fox, date(7,may, 1950), employed), person(ann, fox, date(9, may, 1951), employed), [person(pat, fox, date(5, may, 1973), unemployed), person(jim, fox, date(5, may, 1973), unemployed)]). husband(X) :- family(X,Y, Z). Y and Z are singleton variables husband(X) :- family(X, _, _)

6 More bits and pieces Comparison operators –X > Y –X < Y –X >= Y –X =< Y –X = Y1 + A = B A = 2 + B –X \= Y –X =:= Y1 + 2 =:= A =:= B + 2 –X =\= Y

7 Structures a family database family(person(tom, fox, date(7,may, 1950), employed), person(ann, armstrong, date(9, may, 1951), employed), [person(pat, fox, date(5, may, 1973, unemployed), person(jim, fox, date(5, may, 1973), unemployed)]). family(_,_,_). % any family family(person(_, fox, _, _), _, _). % the fox family family(_, person(_,armstrong, _, _), _)% or the armstrong family family(_, _, [_, _, _]). % any three child family family(_,person(Name, Surname, _, _), [_, _, _, | _]). % all married women who have at least three children

8 husband(X) :- family(X, _, _). wife(X) :- f amily(_,X,_). ?- husband(X). X = person(tom, fox, date(7, may, 1950), employed) Yes (0.00s cpu) ?- wife(X). X = person(ann, armstrong, date(9, may, 1951), employed) Yes (0.00s cpu) Problem – this may be too simplistic

9 husband2(X,Y) :- family(person(X,Y,_,_), _, _). married(X,Y) :- family(person(X, _, _, _), person(Y,_, _, _),_). married(X,Y) :- married(Y,X). child(X) :- family(_, _, Children), member(X, Children). ?? child(X) ??

10 child(X) :- family(_, _, Children), mem_children (X, Children). mem_children (X, [person(X, _, _, _) | Tail]). mem_children (X, [person(Y, _, _, _) | Tail]) :- mem_children1(X, Tail). but ?? child(pat)

11 Selectors husband(family(Husband, _, _), Husband). wife(family(_, Wife, _), Wife). children(family(_, _, Childlist), Childlist). Define relation which allow us to access particular components of a structure without knowing the details the structure This is data abstraction These selectors are useful when we want to create instances of families, for example

12 Selectors… firstchild(Family, First) :- children(Family, [First | _]). secondchild(Family, Second) :- children(Family, [_, Second | _]). nthchild(N, Family, Child) :- children(Family, ChildList), nth_member(ChildList, N, Child). firstname(person(Name, _, _,_), Name). surname(person(_, Surname, _, _), Surname).

13 nth_member([X|_], 1, X). nth_member([_| L], N, Child) :- N1 is N-1, nth_member(L, N1, Child).

14 Using them.. Tom Fox and Jim Fox belong to the same family and Jim is the second child of Tom firstname(Person1, tom), surname(Person1, fox), % person1 is Tom Fox firstname(Person2, jim), surname(Person2, fox), %person2 is Jim Fox husband(Family, Person1), secondchild(Family, Person2). Person1 = person(tom, fox, _, _) Person2 = person(jim, fox, _, _) Family = family(person(tom, fox, _, _), _, [_, person(jim, fox, _,_) | _])

15 Logic Puzzles Use the following clues to write a prolog program to determine the movies the robots tinman, r2d2, johnny five, and a dalek starred in. –Neither Tinman nor Johnny five were one of the daleks in Dr Who and the Daleks –The movie Short Circuit did not star Tinman. –R2d2 wowed the audiences in Star Wars. –A dalek did not star in the Wizard of Oz.

16 Structure is important Solution(L) :- We want a binding for L which will contain the result we are after What is the result we want?

17 L = [movie(X1, wizardofoz), movie(X2, drwhoandtheDaleks), movie(X3, starwars), movie(X4, shortcircuit)], Now we have to supply a mechanism for instantiating X1..X4 We need a way of selecting a value and then checking it against some constraints

18 L = [movie(X1, wizardofoz), movie(X2, drwhoandtheDaleks), movie(X3, starwars), movie(X4, shortcircuit)],

19 L = [movie(X1, wizardofoz), movie(X2, drwhoandtheDaleks), movie(X3, starwars), movie(X4, shortcircuit)], Robotslist = [tinman, dalek, r2d2, johnnyfive], We will draw the values for X1..X2, from Robotslist We do this using the member predicate

20 L = [movie(X1, wizardofoz), movie(X2, drwhoandtheDaleks), movie(X3, starwars), movie(X4, shortcircuit)], Robotslist = [tinman, dalek, r2d2, johnnyfive],

21 L = [movie(X1, wizardofoz), movie(X2, drwhoandtheDaleks), movie(X3, starwars), movie(X4, shortcircuit)], Robotslist = [tinman, dalek, r2d2, johnnyfive], member(X1, Robotslist), X1 \= dalek, member(X2, Robotslist), X2 \= tinman, X2 \= johnnyfive, member(X3, Robotslist), X3 = r2d2, member(X4, Robotslist), X4 \= tinman, There are just two more things left to do

22 L = [movie(X1, wizardofoz), movie(X2, drwhoandtheDaleks), movie(X3, starwars), movie(X4, shortcircuit)], Robotslist = [tinman, dalek, r2d2, johnnyfive], member(X1, Robotslist), X1 \= dalek, member(X2, Robotslist), X2 \= tinman, X2 \= johnnyfive, member(X3, Robotslist), X3 = r2d2, member(X4, Robotslist), X4 \= tinman, X2 \= X1, X2 \= X3, X2 \= X4, X3 \= X1, X3 \= X4, X4 \= X1, print_movies(L). solution(L):-

23 print_movies([A|B]) :- !, write(A), nl, print_movies(B). print_movies([]).