CSc 372 Comparative Programming Languages

Similar documents
CSc 372. Comparative Programming Languages. 17 : Prolog Introduction. Department of Computer Science University of Arizona

CSc 372 Comparative Programming Languages. Objects & Relationships. 9: Prolog Introduction

winter but it rained often during the summer

Knowledge Representation

Basic English. Robert Taggart

THE EARLY YEARS: BRINGING A ROCK AND ROLL ATTITUDE TO FOLK

6.034 Notes: Section 4.1

Algorithms, Lecture 3 on NP : Nondeterministic Polynomial Time

Part 1: Writing Identifying and Fixing Sentence Fragments and Run-on Sentences:

Doctor of Philosophy

melodic c2 melodic c3 melodic

CSC 373: Algorithm Design and Analysis Lecture 17

Chapter 8 Sequential Circuits

ENGLISH LANGUAGE EDUCATION PROGRAM FACULTY OF LANGUAGE AND LITERATURE SATYA WACANA CHRISTIAN UNIVERSITY SALATIGA 2015

Data Representation. signals can vary continuously across an infinite range of values e.g., frequencies on an old-fashioned radio with a dial

COMP Intro to Logic for Computer Scientists. Lecture 2

Melody classification using patterns

Yale University Department of Computer Science

Lab 2, Analysis and Design of PID

Specification. NGTS Issue 1 October 1993

COMP2611: Computer Organization. Introduction to Digital Logic

Sample Paper NAT IE. Instructions

IJMIE Volume 2, Issue 3 ISSN:

Chapter 2: Lines And Points

Control Unit. Arturo Díaz-Pérez Departamento de Computación Laboratorio de Tecnologías de Información CINVESTAV-IPN

LCD STIMULUS DISPLAY for ENV-007/008 CHAMBERS

Automatic Generation of Music Programs

Strand 6 English Language Arts and Reading

Chapter Contents. Appendix A: Digital Logic. Some Definitions

140 IEEE TRANSACTIONS ON VERY LARGE SCALE INTEGRATION (VLSI) SYSTEMS, VOL. 12, NO. 2, FEBRUARY 2004

Musical Harmonization with Constraints: A Survey. Overview. Computers and Music. Tonal Music

Capitalization after colon in apa Capitalization after colon in apa

Part 1: Writing. Fundamentals of Writing 2 Lesson 5. Sentence Structure: Complex Sentences

Latches, Flip-Flops, and Registers. Dr. Ouiem Bchir

Consistency and Completeness of OMEGA, a Logic for Knowledge Representation

Implementation of BIST Test Generation Scheme based on Single and Programmable Twisted Ring Counters

The reduction in the number of flip-flops in a sequential circuit is referred to as the state-reduction problem.

PROFESSOR: I'd like to welcome you to this course on computer science. Actually, that's a terrible way to start.

CSC384: Intro to Artificial Intelligence Knowledge Representation IV

Final Project [Tic-Tac-Toe]

COMP2611: Computer Organization Building Sequential Logics with Logisim

MLA IN-TEXT CITATIONS WORKSHEET


CPSC 121: Models of Computation Lab #5: Flip-Flops and Frequency Division

CRCT Study Guide 6 th Grade Language Arts PARTS OF SPEECH. 1. Noun a word that names a PERSON, PLACE, THING, or IDEA

ANNA KOWALSKA and ANDRZEJ F. NOWAK. Sample paper for Dissertationes Mathematicae

Principles of Computer Architecture. Appendix A: Digital Logic

Running head: SAMPLE APA PAPER FOR STUDENTS 1

DJ Darwin a genetic approach to creating beats

Cryptography CS 555. Topic 5: Pseudorandomness and Stream Ciphers. CS555 Spring 2012/Topic 5 1

asdfghjklqwertyuiopsdf

Sentences. Directions Write S if the group of words is a sentence. Write F if the group of words is a fragment. 1. There is nothing to do now.

Writing a paper. Volodya Vovk (with input from John Shawe-Taylor)

Ocean Sensor Systems, Inc. Wave Staff III, OSSI With 0-5V & RS232 Output and A Self Grounding Coaxial Staff

Chapter 6. sequential logic design. This is the beginning of the second part of this course, sequential logic.

First Draft Chapter 5. AERE Harwell. (To the end of January 1956 when RHEL is created under NIRNS)

PLEASE SCROLL DOWN FOR ARTICLE

For an alphabet, we can make do with just { s, 0, 1 }, in which for typographic simplicity, s stands for the blank space.

FORMAT FOR PREPARATION OF PROJECT REPORT FOR PGDCA

Synchronous sequential circuits

CPSC 121: Models of Computation Lab #5: Flip-Flops and Frequency Division

Varieties of Nominalism Predicate Nominalism The Nature of Classes Class Membership Determines Type Testing For Adequacy

Part 1: Introduction to Computer Graphics

Digital Logic Design ENEE x. Lecture 24

CM-T10-PRO and PRO-E. Wireless Control for ColorMaker Series LED Fixtures with ColorRoll Technology User s Manual

Flip Flop. S-R Flip Flop. Sequential Circuits. Block diagram. Prepared by:- Anwar Bari

This paper is a preprint of a paper accepted by Electronics Letters and is subject to Institution of Engineering and Technology Copyright.

Impro-Visor. Jazz Improvisation Advisor. Version 2. Tutorial. Last Revised: 14 September 2006 Currently 57 Items. Bob Keller. Harvey Mudd College

Name. and. but. yet. nor

High Performance Raster Scan Displays

Figure 1: segment of an unprogrammed and programmed PAL.

Digital Logic Design: An Overview & Number Systems

Running head: SAMPLE APA PAPER FOR STUDENTS 1

MTL Software. Overview

Chapter 1 Lesson 1 Sentences

Symbolization and Truth-Functional Connectives in SL

Jumpstarters for Capitalization & Punctuation

Chapter 4. Logic Design

Rubrics & Checklists

We are here. Assembly Language. Processors Arithmetic Logic Units. Finite State Machines. Circuits Gates. Transistors

Lesson 18: Sentence Structure

EIGHTH GRADE RELIGION


English Skills Practice and Apply: Grade 5

FPGA Implementation of DA Algritm for Fir Filter

2018 YEAR-END REPORT MARKETPLACE ANALYSIS & DATABASE HIGHLIGHTS

Melody Retrieval On The Web

How To Cite A Scholarly Journal

Reality According to Language and Concepts Ben G. Yacobi *

Failure to acknowledge sources or how to plagiarise - 1. Referencing and references. Failure to acknowledge sources or how to plagiarise - 2

To the Instructor Acknowledgments What Is the Least You Should Know? p. 1 Spelling and Word Choice p. 3 Your Own List of Misspelled Words p.

TOMELLERI ENGINEERING MEASURING SYSTEMS. TUBO Version 7.2 Software Manual rev.0

CSc 466/566. Computer Security. 4 : Cryptography Introduction

Combinational / Sequential Logic

Computer Architecture and Organization

Part 4: Introduction to Sequential Logic. Basic Sequential structure. Positive-edge-triggered D flip-flop. Flip-flops classified by inputs

Musical Creativity. Jukka Toivanen Introduction to Computational Creativity Dept. of Computer Science University of Helsinki

The PeRIPLO Propositional Interpolator

Introduction to EndNote

Intensional Relative Clauses and the Semantics of Variable Objects

Transcription:

CSc 372 Comparative Programming Languages 15 : Prolog Introduction Christian Collberg collberg+372@gmail.com Department of Computer Science University of Arizona Copyright c 2008 Christian Collberg [1]

What is Prolog? Prolog is a language which approaches problem-solving in a declarative manner. The idea is to define what the problem is, rather than how it should be solved. In practice, most Prolog programs have a procedural as well as a declarative component the procedural aspects are often necessary in order to make the programs execute efficiently. [2]

What is Prolog? Algorithm = Logic + Control Robert A. Kowalski Prescriptive Languages: Describe how to solve problem Pascal, C, Ada,... Also: Imperative, Procedural Descriptive Languages: Describe what should be done Also: Declarative Kowalski s equation says that Logic is the specification (what the program should do) Control what we need to do in order to make our logic execute efficiently. This usually includes imposing an execution order on the rules that make up our program. [3]

Objects & Relationships objects, and Prolog programs deal with relationships between objects English: Christian likes the record Prolog: likes(christian, record). [4]

Record Database Here s an excerpt from Christian s record database: is record(planet waves). is record(desire). is record(slow train). recorded by(planet waves, bob dylan). recorded by(desire, bob dylan). recorded by(slow train, bob dylan). recording year(planet waves, 1974). recording year(desire, 1975). recording year(slow train, 1979). [5]

Record Database... The data base contains unary facts (is record) and binary facts (recorded by, recording year). The fact can be interpreted as is record(slow train) slow train is-a-record The fact recording year(slow train, 1979) can be interpreted as the recording year of slow_train was 1979. [6]

Conditional Relationships Prolog programs deal with conditional relationships between objects. English: C. likes Bob Dylan records recorded before 1979 Prolog: likes(christian, X) :- is record(x), recorded by(x, bob dylan), recording year(x, Year), Year < 1979. [7]

Conditional Relationships... The rule likes(christian, X) :- is record(x), recorded by(x, bob dylan), recording year(x, Year), Year < 1979. can be restated as Christian likes X, if X is a record, and X is recorded by Bob Dylan, and the recording year is before 1979. Variables start with capital letters. Comma (, ) is read as and. [8]

Asking Questions Prolog programs solve problems by asking questions. English: Does Christian like the albums Planet Waves & Slow Train? Prolog:?- likes(christian, planet waves). yes?- likes(christian, slow train). no [9]

Asking Questions... English: Was Planet Waves recorded by Bob Dylan? When was Planet Waves recorded? Which album was recorded in 1974? Prolog:?- recorded by(planet waves, bob dylan). yes?- recording year(planet waves, X). X = 1974?- recording year(x, 1974). X = planet waves [10]

Asking Questions... In Prolog "," (a comma), means "and English: Did Bob Dylan record an album in 1974? Prolog:?- is record(x), recorded by(x, bob dylan), recording year(x, 1974). yes [11]

Asking Questions... Sometimes a query has more than one answer: Use ";" to get all answers. English: What does Christian like? Prolog:?- likes(christian, X). X = planet waves ; no X = desire ; [12]

Asking Questions... Sometimes answers have more than one part: English: List the albums and their artists! Prolog:?- is record(x), recorded by(x, Y). X = planet waves, Y = bob dylan ; X = desire, Y = bob dylan ; X = slow train, Y = bob dylan ; no [13]

Recursive Rules People are influenced by the music they listen to. People are influenced by the music listened to by the people they listen to. listens to(bob dylan, woody guthrie). listens to(arlo guthrie, woody guthrie). listens to(van morrison, bob dylan). listens to(dire straits, bob dylan). listens to(bruce springsteen, bob dylan). listens to(björk, bruce springsteen). influenced by(x, Y) :- listens to(x, Y). influenced by(x, Y) :- listens to(x,z), influenced by(z,y). [14]

Asking Questions... English: Is Björk influenced by Bob Dylan? Is Björk influenced by Woody Guthrie? Is Bob Dylan influenced by Bruce Springsteen? Prolog:?- influenced by(bjork, bob dylan). yes?- influenced by(bjork, woody guthrie). yes?- influenced by(bob dylan, bruce s). no [15]

Visualizing Logic Comma (,) is read as and in Prolog. Example: The rule person(x) :- has bellybutton(x), not dead(x). is read as X is a person if X has a bellybutton and X is not dead. Semicolon (;) is read as or in Prolog. The rule person(x) :- X=adam ; X=eve ; has bellybutton(x). is read as X is a person if X is adam or X is eve or X has a bellybutton. [16]

Visualizing Logic... To visualize what happens when Prolog executes (and this can often be very complicated!) we use the following two notations: AND OR? first, second.? first; second. first second first second For AND, both legs have to succeed. For OR, one of the legs has to succeed. [17]

Visualizing Logic... Here are two examples: AND OR? has_bellybutton(x), not_dead(x).? X=adam ; X=eve ; has_bellybutton(x). has_bellybutton(x) not_dead(x) X=adam X=eve has_bellybutton(x) [18]

Visualizing Logic... and and or can be combined:? (X=adam ; X=eve ; has_bellybutton(x)), not_dead(x). not_dead(x) X=adam X=eve has_bellybutton(x) This query asks Is there a person X who is adam, eve, or who has a bellybutton, and who is also not dead? [19]

Answering Questions (1) scientist(helder). (2) scientist(ron). (3) portuguese(helder). (4) american(ron). (5) logician(x) :- scientist(x). (6)?- logician(x), american(x). The rule (5) states that Every scientist is a logician The question (6) asks Which scientist is a logician and an american? [20]

Answering Questions... Logicians Portugese nationals helder Scientists ron American nationals [21]

Answering Questions...? logician(x), american(x). logician(x) (6) scientist(x) (1) X=helder american(x) american(helder) scientist(helder) fail (1) scientist(helder). (2) scientist(ron). (3) portuguese(helder). (4) american(ron). (5) logician(x) :- scientist(x). (6)?- logician(x), american(x). [22]

Answering Questions...? logician(x), american(x). logician(x) american(x) (6) scientist(x) (1) (2) X=ron american(helder) fail american(ron) scientist(helder)scientist(ron) [23]

Answering Questions... is record(planet waves). is record(slow train). is record(desire). recorded by(planet waves, bob dylan). recorded by(desire, bob dylan). recorded by(slow train, bob dylan). recording year(planet waves, 1974). recording year(desire, 1975). recording year(slow train, 1979). likes(christian, X) :- is record(x), recorded by(x, bob dylan), recording year(x, Year), Year < 1979. [24]

Answering Questions...? likes(christian, X) ; is_record(x) artist(x, bob_d) recording_year(x, Y) Y<1979 X = planet_waves Y=1979 succeed X = desire Y=1975 succeed X = slow_train Y=1974 fail [25]

Answering Questions... listens to(bob dylan, woody guthrie). listens to(arlo guthrie, woody guthrie). listens to(van morrison, bob dylan). listens to(dire straits, bob dylan). listens to(bruce springsteen, bob dylan). listens to(björk, bruce springsteen). (1) influenced by(x, Y) :- listens to(x, Y). (2) influenced by(x, Y) :- listens to(x, Z), influenced by(z, Y).?- influenced by(bjork, bob dylan).?- inf by(bjork, woody guthrie). [26]

Answering Questions...? inf_by(bjork, bob_d). l_to(bjork, bob_d) fail (1) (2) l_to(bjork, Z) Z=bruce_s Z=bruce_s inf_by(z, bob_d) (1) l_to(bruce_s, bob_d) succeed [27]

Answering Questions...? inf_by(bjork, woody_g). (1) l_to(bjork, woody_g) fail Z=bruce_s (2) l_to(bjork, Z) inf_by(z, woody_g) Z=bruce_s Z=bob_d (1) (2) l_to(bruce_s, woody_g) fail l_to(bruce_s, Z) Z=bob_d inf_by(z,woody_g) l_to(bob_d, woody_g) succeed [28]

Map Coloring 3 4 6 2 5 1 Color a planar map with at most four colors, so that contiguous regions are colored differently. [29]

Map Coloring... A coloring is OK iff 1. The color of Region 1 the color of Region 2, and 2. The color of Region 1 the color of Region 3,... color(r1, R2, R3, R4, R5, R6) :- diff(r1, R2), diff(r1, R3), diff(r1, R5), diff(r1, R6), diff(r2, R3), diff(r2, R4), diff(r2, R5), diff(r2, R6), diff(r3, R4), diff(r3, R6), diff(r5, R6). diff(red,blue). diff(red,green). diff(red,yellow). diff(blue,red). diff(blue,green). diff(blue,yellow). diff(green,red). diff(green,blue). diff(green,yellow). diff(yellow, red).diff(yellow,blue). diff(yellow,green). [30]

Map Coloring...?- color(r1, R2, R3, R4, R5, R6). R1 = R4 = red, R2 = blue, R3 = R5 = green, R6 = yellow ; R1 = red, R2 = blue, R3 = R5 = green, R4 = R6 = yellow 3 green yellow 6 4 blue 2 red green 5 red 1 [31]

Map Coloring Backtracking color(r1, R2, R3, R4, R5, R6) (1) (1) (1) (1) R2=blue R3=blue diff(r1,r2) R1=red R2=blue diff(r1,r3) R3=blue diff(r1,r5) R5=blue diff(r1,r6) R6=blue diff(r2,r3) fail color(r1, R2, R3, R4, R5, R6) (1) (1) (1) (2) R2=blue R3=blue diff(r1,r2) R1=red R2=blue diff(r1,r3) R3=blue diff(r1,r5) R5=blue diff(r1,r6) R6=green R6=yellow fail diff(r2,r3) fail [32]

Map Coloring Backtracking color(r1, R2, R3, R4, R5, R6) (1) (1) diff(r1,r2) diff(r1,r3) R1=red R3=blue R2=blue (2 3) (1 3) diff(r1,r5) R5=green R5=yellow fail diff(r1,r6) R6=blue,... R2=blue R3=blue diff(r2,r3) fail color(r1, R2, R3, R4, R5, R6) (1) (2) (1) (1) R2=blue R3=green diff(r1,r2) R1=red R2=blue diff(r1,r3) R3=green diff(r1,r5) R5=blue diff(r1,r6) R6=blue diff(r2,r3) [33]

Working with gprolog gprolog can be downloaded from here: http://gprolog.inria.fr/. gprolog is installed on lectura (it s also on the Windows machines) and is invoked like this: > gprolog GNU Prolog 1.2.16?- [color].?- listing. go(a, B, C, D, E, F) :- next(a, B),...?- go(a,b,c,d,e,f). A = red... [34]

Working with gprolog... The command [color] loads the prolog program in the file color.pl. You should use the texteditor of your choice (emacs, vi,...) to write your prolog code. The command listing lists all the prolog predicates you have loaded. [35]

Working with gprolog... [36]

Readings and References Read Clocksin-Mellish, Chapter 1-2. http://dmoz.org/computers/programming/languages/prolog Prolog by Example Prolog: Programming for AI Programming in Prolog The Craft of Prolog Prolog for Programmers Prolog The Art of Prolog Coelho & Cotta Bratko Clocksin & Mellish O Keefe Kluzniak & Szpakowicz Alan G. Hamilton Sterling & Shapiro [37]

Readings and References... Computing with Logic Knowledge Systems Through Prolog Natural Language Processing in Prolog Language as a Cognitive Process Prolog and Natural Language Analysis Computers and Human Language Introduction to Logic Beginning Logic Maier & Warren Steven H. Kim Gazdar & Mellish Winograd Pereira and Shieber George W. Smith Irving M. Copi E.J.Lemmon [38]

Prolog So Far A Prolog program consists of a number of clauses: Rules Have head + body: head {}}{ likes(chris, X) :- girl(x), black hair(x) }{{} body Can be recursive Facts Head but no body. Always true. [39]

Prolog So Far... A clause consists of atoms Start with lower-case letter. variables Start with upper-case letter. Prolog programs have a Declarative meaning The relations defined by the program Procedural meaning The order in which goals are tried [40]

Prolog So Far... A question consists of one or more goals:?- likes(chris, X), smart(x). "," means and Use ";" to get all answers Questions are either Satisfiable (the goal succeeds) Unsatisfiable (the goal fails) Prolog answers questions (satisfies goals) by: instantiating variables searching the database sequentially backtracking when a goal fails [41]