Introduction to Prolog Reading and writing. CS181: Programming Languages



Similar documents
3 Abstract Data Types and Search

Computer Science 217

Outline. NP-completeness. When is a problem easy? When is a problem hard? Today. Euler Circuits

WAN Wide Area Networks. Packet Switch Operation. Packet Switches. COMP476 Networked Computer Systems. WANs are made of store and forward switches.

AI: A Modern Approach, Chpts. 3-4 Russell and Norvig

Prolog A Tutorial Introduction James Lu Jerud J. Mead

ProM 6 Exercises. J.C.A.M. (Joos) Buijs and J.J.C.L. (Jan) Vogelaar {j.c.a.m.buijs,j.j.c.l.vogelaar}@tue.nl. August 2010

Parallelization: Binary Tree Traversal

Moving from CS 61A Scheme to CS 61B Java

Social Media Mining. Graph Essentials

CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 313]

Data Structures in Java. Session 15 Instructor: Bert Huang

Solutions to Homework 6

Analysis of Algorithms, I

LOGICAL TOPOLOGY DESIGN Practical tools to configure networks

Understanding the IEC Programming Languages

1 Idioms, Patterns, and Programming

Introduction to Logic in Computer Science: Autumn 2006

Python for Rookies. Example Examination Paper

Course Outline Department of Computing Science Faculty of Science. COMP Applied Artificial Intelligence (3,1,0) Fall 2015

CompSci-61B, Data Structures Final Exam

Introduction to Data Structures

Approximation Algorithms

Midterm Practice Problems

CS MidTerm Exam 4/1/2004 Name: KEY. Page Max Score Total 139

Automata-based Verification - I

MATLAB Programming. Problem 1: Sequential

Data Structure [Question Bank]

Euler Paths and Euler Circuits

SAP BOBJ. Participants will gain the detailed knowledge necessary to design a dashboard that can be used to facilitate the decision making process.

CS 1133, LAB 2: FUNCTIONS AND TESTING

Introduction to Synoptic

Cost Model: Work, Span and Parallelism. 1 The RAM model for sequential computation:

Small Maximal Independent Sets and Faster Exact Graph Coloring

Teaching Introductory Artificial Intelligence with Pac-Man

Magic Word. Possible Answers: LOOSER WINNER LOTTOS TICKET. What is the magic word?

So today we shall continue our discussion on the search engines and web crawlers. (Refer Slide Time: 01:02)

Naming in Distributed Systems

GCE. Computing. Mark Scheme for January Advanced Subsidiary GCE Unit F452: Programming Techniques and Logical Methods

CSE331: Introduction to Networks and Security. Lecture 8 Fall 2006

Social Media Mining. Network Measures

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

CS 2302 Data Structures Spring 2015

Why? A central concept in Computer Science. Algorithms are ubiquitous.

Programming Using Python

An Improved ACO Algorithm for Multicast Routing

Simplifying Logic Circuits with Karnaugh Maps

8.1 Min Degree Spanning Tree

DEVELOPMENT OF A VULNERABILITY ASSESSMENT CODE FOR A PHYSICAL PROTECTION SYSTEM: SYSTEMATIC ANALYSIS OF PHYSICAL PROTECTION EFFECTIVENESS (SAPE)

Linear Motion and Assembly Technologies Pneumatics Service. Understanding the IEC Programming Languages

System Interconnect Architectures. Goals and Analysis. Network Properties and Routing. Terminology - 2. Terminology - 1

The Basics of Graphical Models

Naming in Distributed Systems

Network (Tree) Topology Inference Based on Prüfer Sequence

INTERNSHIP REPORT CSC410. Shantanu Chaudhary 2010CS50295

Formal Languages and Automata Theory - Regular Expressions and Finite Automata -

An Introduction to The A* Algorithm

How To Understand and Configure Your Network for IntraVUE

St Patrick s College Maynooth. Faculty of Theology. Essay Writing Guidelines for Students in BD, BATh, BTh, and Higher Diploma in Theological Studies

INTERNATIONAL JOURNAL OF COMPUTER ENGINEERING & TECHNOLOGY (IJCET)

CMSC 858T: Randomized Algorithms Spring 2003 Handout 8: The Local Lemma

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Graph Theory and Complex Networks: An Introduction. Chapter 06: Network analysis

OpenSta OpenSource for Web Load, HTTP Stress & Performance testing

FOPR-I1O23 - Fundamentals of Programming

CS 598CSC: Combinatorial Optimization Lecture date: 2/4/2010

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

Principles of Programming Languages Topic: Introduction Professor Louis Steinberg

Topology-based network security

Measurement-aware Monitor Placement and Routing

6.02 Practice Problems: Routing

Link Prediction in Social Networks

Introduction to Python

Introduction to Python

Install Java Development Kit (JDK) 1.8

Boulder Dash is NP hard

Python Lists and Loops

A permutation can also be represented by describing its cycles. What do you suppose is meant by this?

JustClust User Manual

Internet Packets. Forwarding Datagrams

Java Application Developer Certificate Program Competencies

Outline. EE 122: Interdomain Routing Protocol (BGP) BGP Routing. Internet is more complicated... Ion Stoica TAs: Junda Liu, DK Moon, David Zats

Distance Degree Sequences for Network Analysis

CS/COE

Chapter One Introduction to Programming

Mining Social-Network Graphs

Exploring Algorithms with Python

3. The Junction Tree Algorithms

USE OF EIGENVALUES AND EIGENVECTORS TO ANALYZE BIPARTIVITY OF NETWORK GRAPHS

Data Structures and Algorithms Written Examination

A Review And Evaluations Of Shortest Path Algorithms

Transcription:

Introduction to Prolog Reading and writing CS181: Programming Languages

Topics: Reading and writing Prolog in action: Searching a maze Prolog in action: Searching directed graphs Vladimir Vacic, Christos Koufogiannakis, University of California at Riverside 2

Write predicate write( ) predicate writes a single term to the terminal. For example: write(a). The term can be a list (as long as it is one list, and not more): write([a, b]). Or something in the lines of: writemyname(x):- write([my, name, is, X]), nl. nl is (not surprisingly) the new line predicate. Vladimir Vacic, Christos Koufogiannakis, University of California at Riverside 3

Write predicate And from this point on, you can use write() in the same way as you would have used any other Prolog predicate: writelist([ ]):- nl. writelist([h T]):- write(h), tab(1), writelist(t). tab( ) writes a number of spaces to the terminal. Vladimir Vacic, Christos Koufogiannakis, University of California at Riverside 4

Read predicate read(x) predicate reads a term from the keyboard and instantiates variable X to the value of the read term. This term has to be followed by a dot. and a white space character (such as an enter or space). For example: hello :- writelist([what, is, your, name,? ], read(x), writelist([hello, X]). Vladimir Vacic, Christos Koufogiannakis, University of California at Riverside 5

Put predicate put( ) predicate prints its argument as a character on the terminal:?- put(104), put(101), put(108), put(108), put(111). writes: hello Of course, we can do more sophisticated things: printstring([ ]). printstring([h T]):-put(H), printstring(t).?- printstring( Vladimir Vacic ). Note the quotation marks list of character codes is a string Vladimir Vacic, Christos Koufogiannakis, University of California at Riverside 6

Prolog in action: Searching Vladimir Vacic, Christos Koufogiannakis, University of California at Riverside 7

Searching a maze Let s say we have a simple maze like the one below: door(a, b). door(b, e). door(b, c). door(d, e). door(c, d). door(e, f). door(g, e). f d g e c b a Goal is to find a path from a room to another room. Vladimir Vacic, Christos Koufogiannakis, University of California at Riverside 8

Searching a maze Since door(a, b) is the same as door(b,a), the maze can be modeled with an undirected graph. Using the standard approach: we are either in the goal room (base case), or we have to pass through a door to get closer to the goal room (recursive case). To avoid going in circles (b-c-d-e or a-b-a-b), we need to remember where we have been so far (does this phrase sound familiar?) Vladimir Vacic, Christos Koufogiannakis, University of California at Riverside 9

Searching a maze So, the solution would be something in the lines of: go(x, X, T). go(x, Y, T):- door(x, Z), not(member(z, T)), go(z, Y, [Z T]). go(x, Y, T):- door(z, X), not(member(z, T)), go(z, Y, [Z T]). Or, using the semicolon (logical or): go(x, Y, T):- (door(x, Z) ; door(z, X)), not(member(z, T)), go(z, Y, [Z T]). go(z, Y, [Z T]). Vladimir Vacic, Christos Koufogiannakis, University of California at Riverside 10

Searching a maze Let s add a twist to the story: in one of the rooms (we do not know exactly which one), there is phone which is ringing. We need to get to the room in which the phone is, and pick it up (this actually sound like an AI problem :-). So, we ask the question:?- go(a, X, [ ]), phone(x). This follows the generate and test paradigm: we first generate a solution to the problem of how to get the room, then check if the phone is in the room. Vladimir Vacic, Christos Koufogiannakis, University of California at Riverside 11

Searching a graph Let s say we have a directed graph as the one below: edge(g, h). edge(g, d). edge(e, d). edge(h, f). edge(e, f). edge(a, e). edge(a, b). edge(b, f). edge(b, c). edge(f, c). edge(d, a). d a g ee b f h c Again, goal is to find a path from a node to another node. Vladimir Vacic, Christos Koufogiannakis, University of California at Riverside 12

Searching a graph The simplest solution is: cango(x, X). cango(x, Y):- edge(x, Z), cango(z, Y). or cango(x, Y):- edge(x, Y). cango(x, Y):- cango(z, Y), edge(x, Z). However, the graph has loops (a-e-d), so Prolog might never be able to resolve this (remember the airplane routing problem from assignment 1?) Vladimir Vacic, Christos Koufogiannakis, University of California at Riverside 13

Searching a graph We can use a solution similar to the maze search. However, let s say we are also interested in the route from X to Y: route(start, Dest, Route):- go(start, Dest, [ ], R), rev(r, [ ], Route). go(x, X, T, [X T]). go(place, Dest, T, R):- edge(place, Next), not(member(next, T)), go(next, Dest, [Place T], R). Vladimir Vacic, Christos Koufogiannakis, University of California at Riverside 14

Searching a graph This algorithm performs breadth-first search. You can of course make this more complex by adding weights to the edges. Searching is actually a big topic in AI: breadth-first search depth-first search best-first search (uses a heuristic do decide what is the best way to go) A* search (uses the sum of the path so far and the heuristic to estimate the path left) Vladimir Vacic, Christos Koufogiannakis, University of California at Riverside 15

Reference Clocksin, W.F., and Mellish C.S. Programming in Prolog. 4th edition. New York: Springer-Verlag. 1994. Vladimir Vacic, Christos Koufogiannakis, University of California at Riverside 16