Programming Languages Prolog Programming Project Due Wednesday, December 5 th, 2001

Size: px
Start display at page:

Download "Programming Languages Prolog Programming Project Due Wednesday, December 5 th, 2001"

Transcription

1 Programming Languages Prolog Programming Project Due Wednesday, December 5 th, 2001 How Prolog Works Although Prolog is a very powerful and expressive programming environment, it is surprisingly easy to implement. All that is required is recursion, resolution, and unification. Unification A unifier of two terms is a set of bindings of variables that make the two terms equal. The Most General Unifier (or MGU) of two terms is the unifier that puts the smallest possible set of restrictions on the variables in the terms. For instance, the terms father(fred, X) and father(y, sue) have an MGU of {X/sue, Y/fred}. Of course, not all terms have a unifier. There is no binding of variables to values that make the terms father(fred, john) and father(x, mike) equal. There is a very simple algorithm to find the MGU of two terms A and B: To find the MGU of A and B, given a binding list L: If A and B are both simple terms (like fred, mike, or sue), then if A=B, the MGU of A and B is L. If A!= B, then there is no MGU of A and B. Consider the following examples: fred fred {} {} fred mike {} NONE mike mike {X/a} {X/a} If A is an unbound variable that does not occur in B, then the MGU of A and B is L {A/B}. Consider the following examples X fred {} {X/fred} X father(y,john) {Y/mike} {X/father(Y,john), Y/mike} X father(x) {} NONE If B is an unbound variable that does not occur in A then the MGU of A and B is {B/A}. fred X {} {X/fred} num(0) Z { } {Z/num(0)} num(x) X {} NONE If A is a variable bound to the term t, then the MGU of A and B given L is the MGU of t and B given L. Consider the following examples: X mike {X/mike} {X/mike} X mike {X/Y} {X/Y, Y/mike} X father(a) {X/father(Y)} {X/father(Y), Y/a} X john {X/mike} FAIL X john {X/Y, Y/Z} {X/Y, Y/Z, Z/john} Looking at the last example in more depth: To find the MGU of X and john given {X/Y, Y/Z}: Since X is bound to Y in {X/Y, Y/Z}, the MGU of X and john given {X/Y, Y/Z} is the MGU of Y and john given {X/Y, Y/Z}. Since Y is bound to Z in {X/Y, Y/Z}, the MGU of Y and john given {X/Y, Y/Z} is the MGU of Z and john given {X/Y, Y/Z}, which is {X/Y, Y/Z, Z/john}

2 If B is a variable bound to the term t, then the MGU of A and B given L is the MGU of A and t given L. Consider the following examples: f(y) X {X/Z} {X/Z, Z/f(Y)} f(y) X {X/Y} FAIL f(a) X {X/f(Z)} {X/f(Z), Z/a} If A and B are complex terms (like mother(julie, rachel) or father(mike, adam)), first check to see that A and B are they same complex term. If they are, then recursively find the MGU of the first argument of the two terms, then use that binding to find the MGU of the second term, and so on. If A and B are different terms, then there is no MGU of A and B. For instance, consider the following examples: mother(x,john) mother(sue,y) {} {X/sue, Y/john} mother(x,x) mother(a,b) {} FAIL f(g(x,x), Y) f(y,g(a,a)) {} {Y/g(X,X), X/a} Looking at the second example above in more detail: to find the MGU of mother(x, X) and mother(a,b) given {}: since the two complex terms are both "mother" terms: first, find the MGU of X and a given {}, to get {X/ a} next, find the MGU of X and b given {X/ a} the MGU of X and b given {X/a} is the MGU of a and b given {X/a} which FAILS Thus there is no MGU of mother(x, X) and mother(a,b) given {} Looking at the third example in more detail: to find the MGU of f(g(x,x),y) and f(y,g(a,a)) given {}: since the two complex terms are both "f" terms: first, find the MGU of g(x,x) and Y given {}, to get {Y/g(X,X)} next, find the MGU of Y and g(a,a) given {Y/g(X,X)} : Since Y is bound to g(x,x), find the MGU of g(a,a) and g(x,x) given {Y/g(X,X)} to find the MGU of g(a,a) and g(x,x) given {Y/g(X,X)} first, find the MGU of a and X given {Y/g(X,X)}, to get {Y/g(X,X), X/a} next, find the MGU of a and X given {Y/g(X,X), X/a} to get {Y/g(X,X), X/a} Thus, the MGU of f(g(x,x),y) and f(y,g(a,a)) given {} is {Y/g(X,X), X/a} Resolution To prove the preposition p(a,b) given the binding list {} using the fact p(a,b), all we need to do is find a MGU for p(a,b) and p(a,b) given {}, which is {A/a, B/b}. To prove the preposition p(a,b) given the binding list {} using the rule p(w,x) :- q(x) and the fact q(z): first, unify p(a,b) and p(w,x) using {} to get {A/w, B/X} next, recursively prove q(x) using the binding list {A/w, B/X} to get {A/w, B/X, X/z} To prove a list of prepositions { p(a,b), q(b,c) } given a binding list {}, first prove p(a,b) using {} to get a new binding list L, then prove q(b,c) using L. For instance: To prove { p(a,b), q(b,c) } given {}, using the facts p(w,x) and q(x,z): prove p(a,b) given {} to get {A/w, B/x} then prove q(b,c) given {A/w, B/x} to get {A/w, B/x, C/z} To prove the preposition p(a,b) given the binding list {}, using the rule p(a,x) :- q(x), r(x) and the facts q(d) and r(d): first, unitfy p(a,b) and p(a,x) to get the binding list {A/a, B/X} next, recursively prove {q(x), r(x)} using the binding list {A/a, B/X} to get the binding list {A/a, B/X, X/d } Rule Renaming

3 Let's say you have the following fact in your database: f(b,x). This fact says that, f(b,x) is true for any value of X. Now, if you make the query:?- f(x,a) You are asking if there is any value of X such that f(x,a) is true. We'd like the system to come back with X = b, since the X in the query and the X in the rule are different X's just as we can have different variables in different functions that have the same names. How can we allow different variables to have the same name? Before we use a rule to prove a query, we can rename all the variables. For instance, before trying to unify f(b,x) and f(x,a), we can rename the variables in the database fact to get f(x_1, a), then unify f(b, X) and f(x_1, a) to get {X_1/b, X/a}. Likewise, if you have the fact: equal(x,x) in your database, and you make the query equal(a,x), before trying to unify eqaul(a,x) and equal(x,x), rename the variables in the fact to get equal(x_1, X_1), and then unify equal(a,x) and equal(x_1, X_1) to get {X_1/a, X/X_1}. Recursion & Backtracking Once you know the rules and facts that need to be used to prove a query, you can use unification and resolution as described above. However, how do you know which facts and rules to use? Try them all! Let's say you have an array R of rules, a binding list L, and a set of P predicates to prove. How do you go about proving if the set of predicates (query terms) P can be solved given the binding list L and list of rules R? To prove predicates P using array of rules R and binding list L: for(i=0; i < NumRules;i++) Rename the variables in R[i] to get RNew Try to unify the consequent of RNew with the first element of L to get a new binding list L' If you succeed, then recursively prove the list of terms consisting of the antecedent of RNew concatenated with the rest of the predicates in P, to get L'' If you succeed, return the new binding list L'' Library Files To help you out, and to keep this project from being too long, I have included some library files to assist with reading, writing, and renaming variables in terms and rules. The interface file for this library is the file term.h, reproduced below: typedef struct _termlist *termlist; typedef struct _term *term; typedef struct _rule *rule; Datatypes terms, rules, and lists of terms struct _rule { term consequent; int numantecedent; termlist antecedent; }; struct _term { int variable; /* Boolean value -- true (1) if term is a variable */ char * name; int arity; termlist args; }; struct _termlist{ term first;

4 termlist rest; }; /* Function printrule */ /* Purpose : Prints a rule to standard out in format */ /* consequent :- antecedent */ /* Input Param: rule r, the rule to print */ /* Output: the rule r */ void printrule(rule r); /* Function printterm */ /* Purpose : Prints a term to standard out */ /* Input Param: term t, the term to print */ /* Output: the term t */ void printterm(term t); /* Function getterm */ /* Purpose: Reads a term from the file f, skipping comments */ /* and whitespace. If an EOF is reached before a */ /* term is read, then the function returns NULL */ /* Input Param : The file f from which to read the terms */ /* Return value: The term that was read in */ term getterm(file *f); /* Function gettermlist */ /* Purpose: Reads a list of terms (separated by commas) */ /* from the file f, skipping comments and whitespace */ /* If an EOF is reached before a term is read, */ /* then the function returns NULL */ /* Input Param : The file f from which to read the terms */ /* Return value: The list of terms that were read, or NULL */ termlist gettermlist(file *f); /* Function getrule */ /* Purpose: Reads a rule from the file f, skipping comments */ /* and whitespace. If an EOF is reached before a */ /* rule is read, then the function returns NULL */ /* Input Param : The file f from which to read the terms */ /* Return value: The rule that was read in, or NULL */ rule getrule(file *f); /* Function renamevars */ /* Purpose: Renames all of the variables in a rule. If the */ /* same variable appears twice in the rule, it will */ /* be given the same name each time it appears. */ /* The original rule is not modified in any way, */ /* the new rule is a duplicate made from newly */ /* malloc'd memory */ /* Input Param : The rule r for which we want to rename variables */ /* Return value: the rule r, with variables renamed. */ rule renamevars(rule r); /* Function freerule */ /* Purpose: Frees all the heap memory used by rule r, including all */ /* subterms and strings in r. */ /* Input Param : The rule r which we wish to delete */ void freerule(rule r); /* Function freeterm */ /* Purpose: Frees all the heap memory used by term t, including all */ /* subterms and strings in t. */ /* Input Param : The term t which we wish to delete */ void freeterm(term t); Example Uses of Library Files The file usingfunctions.c gives some examples of using these functions. The command 'make example' will compile this example code, and create the executable 'example' This code uses the library functions to

5 read in all the rules from an input file, rename the variables in the rules, and then print the rules (with the variables renamed) out to standard out (the screen). Then, term lists are repeatedly read from standard input (the keyboard) and printed to the screen, until a halt term is read in A (very sketchy) skeleton main program is also provided the command 'make prolog' will compile this code and create the executable named prolog. All you need to do is modify this file to create a working prolog interpreter The command 'make clean' removes all obect (.o) files and executables. How to Proceed This is a long project, and seems daunting at the start, but if you break it down into manageable chunks, then it is not so bad. Here is how I recommend attacking the problem: First, write code to read in a list of rules. Test this code by writing out the rules after they have been read in! Next, write a unify function. This function should take as input parameters two terms and a binding list, and it should return either a sentinel value or a new binding list, depending upon whether the unification was successful. Next, test your unify function. Write a main that inputs two terms, tries to unify them, and then writes out the binding list Next, write the prove function, following the pseudo-code above. After reading in the list of rules, your main should read in a list of terms, call unify to get a binding list, and either print out the binding list and yes, or no. Your binding lists can be quite long it is OK at this point to have the query?- plus(0,s(s(s(0))),z) print out something like : Z_11 = X_14 X_14 = 0 Z_7 = s(z_11) Y_11 = 0 X_11 = 0 Z_3 = s(z_7) Y_7 = s(0) X_7 = 0 Z = s(z_3) Y_3 = s(s(0)) X_3 = 0 yes Note that the order of the variables in the binding list is not important. Your code does not need to produce exactly these variables in this order this is just the general idea. Next, write a function to compact the binding list, so that, for example, {X/Y, Y/Z, Z/a} would be reduced to {X/a, Y/a, Z/a}. Note that you only need to compact the binding list after you have finished proving the term, and right before the binding list is printed to the screen. So the query?- plus(0,s(s(s(0))),z) might print out something like : Z_11 = 0 X_14 = 0 Z_7 = s(0) Y_11 = 0 X_11 = 0 Z_3 = s(s(0)) Y_7 = s(0) X_7 = 0 Z = s(s(s(0)))) Y_3 = s(s(0)) X_3 = 0 yes

6 Finally, modify your print function so that it only prints out bindings of variables that exist in the original query. Thus the query.?- plus(0,s(s(s(0))),z) should print out something like : Z = s(s(s(0))) yes If you wrote an exists function as part if doing unification, that will help here... Note that you can get partial credit for finishing some (but not all) of the above steps. Testing your implementation Once you finish your interpreter, you will need to test it. I have given you a Peano Arithmetic prolog file to help out. You also need to write some prolog functions of your own. Write the following prolog functions (which assume the existence of a parent(x,y), male(x,y), and female(x,y) database) : uncle(x,y) true if X is the uncle of Y married(x,y) true if X and Y share a child (OK, this isn't the same as married in real life...) cousin(x,y) true if X and Y are cousins. X and Y are cousins if they share a grandparent, or if X is the descendant of a cousin of Y, or Y is the descendant of a cousin of X greataunt(x,y) true if X is the great aunt (mother of an aunt or uncle) of Y You do not have to worry about negation, so it is OK for married(john,john) to be true if john has a child. A file family.pl, with parent, male, and female facts, is available online. The Assignment For this assignment, you need to : Write a Prolog interpreter using the provided library files. A makefile, and skeleton main.c are provided for you. Your interpreter needs to work just like the standard gprolog, though you do not need to implement the "ask for more" feature. For maximal partial credit, attack the problem following the advice in the previous section. Write prolog rules for uncle, married, cousin, and greataunt, as defined above. Add these rules to the provided family.pl file. Extra Credit You can receive up to 25 points of extra credit by modifying your interpreter to have the "ask for more answers" functionality. See me for pointers if you wish to try the extra credit. What to turn in A hardcopy of your source code is due by 5:00 p.m. on Wednesday, December 5 th. You need to turn in a printout of main.c, as well as your uncle, married, cousin, and greataunt rules In addition, copy all of the files necessary to compile and run your interpreter to your submit directory, under the subdirectory prolog. Make sure that all permissions are set to be group readable and group executable!

Prolog Programming. Lecture Module 8

Prolog Programming. Lecture Module 8 Prolog Programming Lecture Module 8 Prolog Language Prolog is unique in its ability to infer facts from the given facts and rules. In Prolog, an order of clauses in the program and goals in the body of

More information

For Wednesday. No reading Chapter 9, exercise 9. Must be proper Horn clauses

For Wednesday. No reading Chapter 9, exercise 9. Must be proper Horn clauses For Wednesday No reading Chapter 9, exercise 9 Must be proper Horn clauses Same Variable Exact variable names used in sentences in the KB should not matter. But if Likes(x,FOPC) is a formula in the KB,

More information

CS 321 Programming Languages and Compilers. Prolog

CS 321 Programming Languages and Compilers. Prolog CS 321 Programming Languages and Compilers Prolog Prolog PROgramming LOGic Algorithm = Logic + Control Logic programming deals with computing relations rather than functions. To understand Prolog one must

More information

The current topic: Prolog. Announcements. Meaning of a Prolog rule. Prolog syntax. Reminder: The deadline for Lab 2 re-mark requests is Friday.

The current topic: Prolog. Announcements. Meaning of a Prolog rule. Prolog syntax. Reminder: The deadline for Lab 2 re-mark requests is Friday. The current topic: Prolog! Introduction! Object-oriented programming: Python! Functional programming: Scheme! Python GUI programming (Tkinter)! Types and values Logic programming: Prolog! Introduction

More information

Brief Introduction to Prolog

Brief Introduction to Prolog CSC384: Intro to Artificial Intelligence Brief Introduction to Prolog Prolog Programming for Artificial Intelligence by Ivan Bratko. Prolog is a language that is useful for doing symbolic and logic based

More information

Chapter 16. Logic Programming. Topics. Unification. Resolution. Prolog s Search Strategy. Prolog s Search Strategy

Chapter 16. Logic Programming. Topics. Unification. Resolution. Prolog s Search Strategy. Prolog s Search Strategy Topics Chapter 16 Logic Programming Summary (resolution, unification, Prolog search strategy ) Disjoint goals The cut operator Negative goals Predicate fail Debugger / tracer Lists 2 Resolution Resolution

More information

INTRODUCTION TO PROLOG

INTRODUCTION TO PROLOG INTRODUCTION TO PROLOG PRINCIPLES OF PROGRAMMING LANGUAGES Norbert Zeh Winter 2018 Dalhousie University 1/44 STRUCTURE OF A PROLOG PROGRAM Where, declaratively, Haskell expresses a computation as a system

More information

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Fall Programming Assignment 1 (updated 9/16/2017)

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Fall Programming Assignment 1 (updated 9/16/2017) UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Fall 2017 Programming Assignment 1 (updated 9/16/2017) Introduction The purpose of this programming assignment is to give you

More information

Briefly describe the purpose of the lexical and syntax analysis phases in a compiler.

Briefly describe the purpose of the lexical and syntax analysis phases in a compiler. Name: Midterm Exam PID: This is a closed-book exam; you may not use any tools besides a pen. You have 75 minutes to answer all questions. There are a total of 75 points available. Please write legibly;

More information

PROgramming in LOGic. Part II. By Forrest Pepper 12/7/07

PROgramming in LOGic. Part II. By Forrest Pepper 12/7/07 PROgramming in LOGic Part II By Forrest Pepper 12/7/07 Anatomy of a Program We discussed the three main constructs of a Prolog program Facts contain a property or state a relationship between two or more

More information

CS 360: Programming Languages Lecture 10: Logic Programming with Prolog

CS 360: Programming Languages Lecture 10: Logic Programming with Prolog CS 360: Programming Languages Lecture 10: Logic Programming with Prolog Geoffrey Mainland Drexel University Section 1 Administrivia Midterm Tuesday Midterm is Tuesday, February 14! Study guide is on the

More information

Logic Languages. Hwansoo Han

Logic Languages. Hwansoo Han Logic Languages Hwansoo Han Logic Programming Based on first-order predicate calculus Operators Conjunction, disjunction, negation, implication Universal and existential quantifiers E A x for all x...

More information

Visual Prolog Tutorial

Visual Prolog Tutorial Visual Prolog Tutorial Jim Mims April 2008 (with modification by Danjie Zhu) Preface What is Prolog? Programming in Logic. Edinburgh syntax is the basis of ISO standard. High-level interactive language.

More information

Logic Programming. Let us have airline flight information of the form: 1. Application Domains: 2. Definitions

Logic Programming. Let us have airline flight information of the form: 1. Application Domains: 2. Definitions Logic Programming 1. Application Domains: Logic programming language application areas include natural language processing, expert systems, specifications checking, theorem proving, and control systems

More information

Roman Barták Charles University, Prague (CZ)

Roman Barták Charles University, Prague (CZ) 1 Programming with Logic and Constraints Roman Barták Charles University, Prague (CZ) roman.bartak@mff.cuni.cz http://ktiml.mff.cuni.cz/~bartak Constraint programming represents one of the closest approaches

More information

Prolog. Intro to Logic Programming

Prolog. Intro to Logic Programming Prolog Logic programming (declarative) Goals and subgoals Prolog Syntax Database example rule order, subgoal order, argument invertibility, backtracking model of execution, negation by failure, variables

More information

Types, Type Inference and Unification

Types, Type Inference and Unification Types, Type Inference and Unification Mooly Sagiv Slides by Kathleen Fisher and John Mitchell Cornell CS 6110 Summary (Functional Programming) Lambda Calculus Basic ML Advanced ML: Modules, References,

More information

Prolog - 3 Prolog search trees + traces

Prolog - 3 Prolog search trees + traces Prolog - 3 Prolog search trees + traces 1 Review member(a, [A B]). member(a, [B C]) :- member (A,C). goal-oriented semantics: can get value assignment for goal member(a,[b C]) by showing truth of subgoal

More information

Prolog-2 nd Lecture. Prolog Predicate - Box Model

Prolog-2 nd Lecture. Prolog Predicate - Box Model Prolog-2 nd Lecture Tracing in Prolog Procedural interpretation of execution Box model of Prolog predicate rule How to follow a Prolog trace? Trees in Prolog use nested terms Unification Informally Formal

More information

6. Inference and resolution

6. Inference and resolution Computer Science and Software Engineering University of Wisconsin - Platteville 6. Inference and resolution CS 3030 Lecture Notes Yan Shi UW-Platteville Read: Textbook Chapter 8 Part of the slides are

More information

If a program is well-typed, then a type can be inferred. For example, consider the program

If a program is well-typed, then a type can be inferred. For example, consider the program CS 6110 S18 Lecture 24 Type Inference and Unification 1 Type Inference Type inference refers to the process of determining the appropriate types for expressions based on how they are used. For example,

More information

Data Structure and Algorithm Homework #3 Due: 2:20pm, Tuesday, April 9, 2013 TA === Homework submission instructions ===

Data Structure and Algorithm Homework #3 Due: 2:20pm, Tuesday, April 9, 2013 TA   === Homework submission instructions === Data Structure and Algorithm Homework #3 Due: 2:20pm, Tuesday, April 9, 2013 TA email: dsa1@csientuedutw === Homework submission instructions === For Problem 1, submit your source code, a Makefile to compile

More information

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Summer 2016 Programming Assignment 1 Introduction The purpose of this

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Summer 2016 Programming Assignment 1 Introduction The purpose of this UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Summer 2016 Programming Assignment 1 Introduction The purpose of this programming assignment is to give you some experience

More information

COMP2411 Lecture 20: Logic Programming Examples. (This material not in the book)

COMP2411 Lecture 20: Logic Programming Examples. (This material not in the book) COMP2411 Lecture 20: Logic Programming Examples (This material not in the book) There are several distinct but often equivalent ways to think about logic programs 1. As computing logical consequences of

More information

Compilers Project 3: Semantic Analyzer

Compilers Project 3: Semantic Analyzer Compilers Project 3: Semantic Analyzer CSE 40243 Due April 11, 2006 Updated March 14, 2006 Overview Your compiler is halfway done. It now can both recognize individual elements of the language (scan) and

More information

Prolog Interpreter in Rewriting Logic

Prolog Interpreter in Rewriting Logic Interpreter in Rewriting Logic Based upon: The Art of by Leon Sterling and Ehud Shapiro Introductory Class CS422 Outline 1 Motivation Details 2 Motivation Motivation Details Logic as a formal way of thinking:

More information

Logic Programming. Efficiency Issues. Temur Kutsia

Logic Programming. Efficiency Issues. Temur Kutsia Logic Programming Efficiency Issues Temur Kutsia Research Institute for Symbolic Computation Johannes Kepler University of Linz, Austria kutsia@risc.uni-linz.ac.at Efficiency Issues in Prolog Narrow the

More information

CSE341 Spring 2017, Final Examination June 8, 2017

CSE341 Spring 2017, Final Examination June 8, 2017 CSE341 Spring 2017, Final Examination June 8, 2017 Please do not turn the page until 8:30. Rules: The exam is closed-book, closed-note, etc. except for both sides of one 8.5x11in piece of paper. Please

More information

CSE 130, Fall 2005: Final Examination

CSE 130, Fall 2005: Final Examination CSE 130, Fall 2005: Final Examination Name: ID: Instructions, etc. 1. Write your answers in the space provided. 2. Wherever it says explain, write no more than three lines as explanation. The rest will

More information

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University Lecture 3 COMP1006/1406 (the Java course) Summer 2014 M. Jason Hinek Carleton University today s agenda assignments 1 (graded) & 2 3 (available now) & 4 (tomorrow) a quick look back primitive data types

More information

Practice Problems For Review Session CISC 220, Winter 2010

Practice Problems For Review Session CISC 220, Winter 2010 Practice Problems For Review Session CISC 220, Winter 2010 Each of these problems is taken from a previous final exam, which can be found on the web. They are gathered here for convenience. Question 1:

More information

Lecture Overview Prolog 1. Introduction Interaction Terms 2. Clauses and predicates Clauses Predicates Variables 3.

Lecture Overview Prolog 1. Introduction Interaction Terms 2. Clauses and predicates Clauses Predicates Variables 3. 1 Lecture Overview Prolog 1. Introduction Interaction Terms 2. Clauses and predicates Clauses Predicates Variables 3. Satisfying goals 2 Prolog A standard free Prolog can be downloaded from http://www.swi-prolog.org

More information

Trees, Part 1: Unbalanced Trees

Trees, Part 1: Unbalanced Trees Trees, Part 1: Unbalanced Trees The first part of this chapter takes a look at trees in general and unbalanced binary trees. The second part looks at various schemes to balance trees and/or make them more

More information

Types and Type Inference

Types and Type Inference CS 242 2012 Types and Type Inference Notes modified from John Mitchell and Kathleen Fisher Reading: Concepts in Programming Languages, Revised Chapter 6 - handout on Web!! Outline General discussion of

More information

CSE341, Spring 2013, Final Examination June 13, 2013

CSE341, Spring 2013, Final Examination June 13, 2013 CSE341, Spring 2013, Final Examination June 13, 2013 Please do not turn the page until 8:30. Rules: The exam is closed-book, closed-note, except for both sides of one 8.5x11in piece of paper. Please stop

More information

Computational Logic Introduction to Logic Programming

Computational Logic Introduction to Logic Programming Computational Logic Introduction to Logic Programming 1 Overview 1. Syntax: data 2. Manipulating data: Unification 3. Syntax: code 4. Semantics: meaning of programs 5. Executing logic programs 2 Syntax:

More information

Page 1. Where Have We Been? Chapter 2 Representing and Manipulating Information. Why Don t Computers Use Base 10?

Page 1. Where Have We Been? Chapter 2 Representing and Manipulating Information. Why Don t Computers Use Base 10? Where Have We Been? Class Introduction Great Realities of Computing Int s are not Integers, Float s are not Reals You must know assembly Memory Matters Performance! Asymptotic Complexity It s more than

More information

Syntax: Terms Arity: is the number of arguments of a structure. Constructors are represented

Syntax: Terms Arity: is the number of arguments of a structure. Constructors are represented Computational Logic Introduction to Logic Programming 1 Overview 1. Syntax: data 2. Manipulating data: Unification 3. Syntax: code 4. Semantics: meaning of programs 5. Executing logic programs 2 Syntax:

More information

Parser Tools: lex and yacc-style Parsing

Parser Tools: lex and yacc-style Parsing Parser Tools: lex and yacc-style Parsing Version 5.0 Scott Owens June 6, 2010 This documentation assumes familiarity with lex and yacc style lexer and parser generators. 1 Contents 1 Lexers 3 1.1 Creating

More information

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator. .00 SICP Interpretation part Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment Environment as explicit parameter Defining new procedures Why do

More information

Part I Logic programming paradigm

Part I Logic programming paradigm Part I Logic programming paradigm 1 Logic programming and pure Prolog 1.1 Introduction 3 1.2 Syntax 4 1.3 The meaning of a program 7 1.4 Computing with equations 9 1.5 Prolog: the first steps 15 1.6 Two

More information

What s the problem? fib(1,1). fib(2,1). fib(n,x) :- N>2, N1 is N-1, N2 is N-2, fib(n1,x2), fib(n2,x2).

What s the problem? fib(1,1). fib(2,1). fib(n,x) :- N>2, N1 is N-1, N2 is N-2, fib(n1,x2), fib(n2,x2). 39 What s the problem? A Fibonacci Implementation fib(1,1). fib(2,1). fib(n,x) :- N>2, N1 is N-1, N2 is N-2, fib(n1,x2), fib(n2,x2). List Concatenation conc([],l,l). conc([x L1], L2, [X L3]) :- conc(l1,l2,l3).

More information

CS 470 Operating Systems Spring 2013 Shell Project

CS 470 Operating Systems Spring 2013 Shell Project CS 470 Operating Systems Spring 2013 Shell Project 40 points Out: January 11, 2013 Due: January 25, 2012 (Friday) The purpose of this project is provide experience with process manipulation and signal

More information

Type Systems. Seman&cs. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class

Type Systems. Seman&cs. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class Type Systems Seman&cs CMPT 379: Compilers Instructor: Anoop Sarkar anoopsarkar.github.io/compilers-class 1 Equality of types Main seman&c tasks involve liveness analysis and checking equality Equality

More information

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8 Question 1. (12 points) For each of the following, what value is printed? (Assume that each group of statements is executed independently in a newly reset Scheme environment.) (a) (define x 1) (define

More information

COMPUTER SCIENCE TRIPOS

COMPUTER SCIENCE TRIPOS CST.2014.3.1 COMPUTER SCIENCE TRIPOS Part IB Monday 2 June 2014 1.30 to 4.30 pm COMPUTER SCIENCE Paper 3 Answer five questions. Submit the answers in five separate bundles, each with its own cover sheet.

More information

Backtracking. Backtracking. Backtracking. Backtracking. Backtracking. Backtracking. Functional & Logic Programming - Backtracking October, 01

Backtracking. Backtracking. Backtracking. Backtracking. Backtracking. Backtracking. Functional & Logic Programming - Backtracking October, 01 Functional & Logic Programming - October, 01 already seen how to control execution ordering the clauses and goals can affect the speed of execution and the order of evaluation of the clauses now going

More information

CSCI2100B Data Structures Trees

CSCI2100B Data Structures Trees CSCI2100B Data Structures Trees Irwin King king@cse.cuhk.edu.hk http://www.cse.cuhk.edu.hk/~king Department of Computer Science & Engineering The Chinese University of Hong Kong Introduction General Tree

More information

Logical reasoning systems

Logical reasoning systems Logical reasoning systems Theorem provers and logic programming languages Production systems Frame systems and semantic networks Description logic systems CS 561, Session 19 1 Logical reasoning systems

More information

Parser Tools: lex and yacc-style Parsing

Parser Tools: lex and yacc-style Parsing Parser Tools: lex and yacc-style Parsing Version 6.11.0.6 Scott Owens January 6, 2018 This documentation assumes familiarity with lex and yacc style lexer and parser generators. 1 Contents 1 Lexers 3 1.1

More information

Prolog. Logic Programming vs Prolog

Prolog. Logic Programming vs Prolog Language constructs Prolog Facts, rules, queries through examples Horn clauses Goal-oriented semantics Procedural semantics How computation is performed? Comparison to logic programming 1 Logic Programming

More information

Plan of the lecture. G53RDB: Theory of Relational Databases Lecture 14. Example. Datalog syntax: rules. Datalog query. Meaning of Datalog rules

Plan of the lecture. G53RDB: Theory of Relational Databases Lecture 14. Example. Datalog syntax: rules. Datalog query. Meaning of Datalog rules Plan of the lecture G53RDB: Theory of Relational Databases Lecture 14 Natasha Alechina School of Computer Science & IT nza@cs.nott.ac.uk More Datalog: Safe queries Datalog and relational algebra Recursive

More information

Fundamentals of Prolog

Fundamentals of Prolog Fundamentals of Prolog Prof. Geraint A. Wiggins Centre for Cognition, Computation and Culture Goldsmiths College, University of London Contents Summary of Lecture 1 What makes a good Prolog program? What

More information

Lab 5: SDC Virtual Machine

Lab 5: SDC Virtual Machine Lab 5: SDC Virtual Machine Due Date: Thursday 3/9/2017 11:59PM This lab covers the material in lectures 10-12. You will be creating a virtual (software-based) implementation of a basic, decimal-based Von

More information

CSE341 Spring 2017, Final Examination June 8, 2017

CSE341 Spring 2017, Final Examination June 8, 2017 CSE341 Spring 2017, Final Examination June 8, 2017 Please do not turn the page until 8:30. Rules: The exam is closed-book, closed-note, etc. except for both sides of one 8.5x11in piece of paper. Please

More information

First-Order Logic (FOL)

First-Order Logic (FOL) First-Order Logic (FOL) FOL consists of the following parts: Objects/terms Quantified variables Predicates Logical connectives Implication Objects/Terms FOL is a formal system that allows us to reason

More information

Module 6. Knowledge Representation and Logic (First Order Logic) Version 2 CSE IIT, Kharagpur

Module 6. Knowledge Representation and Logic (First Order Logic) Version 2 CSE IIT, Kharagpur Module 6 Knowledge Representation and Logic (First Order Logic) 6.1 Instructional Objective Students should understand the advantages of first order logic as a knowledge representation language Students

More information

CSE341 Autumn 2017, Final Examination December 12, 2017

CSE341 Autumn 2017, Final Examination December 12, 2017 CSE341 Autumn 2017, Final Examination December 12, 2017 Please do not turn the page until 2:30. Rules: The exam is closed-book, closed-note, etc. except for both sides of one 8.5x11in piece of paper. Please

More information

Programming Languages Week 6 Exercises

Programming Languages Week 6 Exercises Programming Languages Week 6 Exercises 1 Logic programming in Python 1.1 Unification Unification is the process of making two things the same. Unifying an unbound variable and a value makes them the same

More information

LOWELL WEEKLY JOURNAL

LOWELL WEEKLY JOURNAL Y $ b b Y b Y F Q Q Y 2 F {»» ( 2 Y b b b ] F F b / b b F q x x ) b Y b? F ( ) x _ q ( b b» ZZ F $ b b» b 6 2 q b x =2 2 6 2 b 2 2 bb b b? [ b q {» ( b b b ( x b $ b F b b q b b b q F b Y F b Y Y z b b

More information

6.034 Notes: Section 11.1

6.034 Notes: Section 11.1 6.034 Notes: Section 11.1 Slide 11.1.1 We've now spent a fair bit of time learning about the language of first-order logic and the mechanisms of automatic inference. And, we've also found that (a) it is

More information

Prolog. Artificial Intelligence. Lecture 2 Karim Bouzoubaa

Prolog. Artificial Intelligence. Lecture 2 Karim Bouzoubaa Prolog Artificial Intelligence Lecture 2 Karim Bouzoubaa Content Introduction Declarative and logic programming Example Computational model Prolog reasoning Structure of prolog programs Prolog concepts

More information

Signature: ECE 551 Midterm Exam

Signature: ECE 551 Midterm Exam Name: ECE 551 Midterm Exam NetID: There are 7 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly. This exam must be individual work.

More information

Chapter 6: Bottom-Up Evaluation

Chapter 6: Bottom-Up Evaluation 6. Bottom-Up Evaluation 6-1 Deductive Databases and Logic Programming (Winter 2009/2010) Chapter 6: Bottom-Up Evaluation Evaluation of logic programs with DB techniques. Predicate dependency graph. Seminaive

More information

(Refer Slide Time: 06:01)

(Refer Slide Time: 06:01) Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture 28 Applications of DFS Today we are going to be talking about

More information

CSE 374 Midterm Exam 2/6/17 Sample Solution. Question 1. (12 points, 4 each) Suppose we have the following files and directories:

CSE 374 Midterm Exam 2/6/17 Sample Solution. Question 1. (12 points, 4 each) Suppose we have the following files and directories: Question 1. (12 points, 4 each) Suppose we have the following files and directories: $ pwd /home/user $ ls -l -rw-r--r-- 1 user uw 10 Feb 4 15:49 combine.sh drwxr-xr-x 2 user uw 2 Feb 4 15:51 hws -rw-r--r--

More information

MP 3 A Lexer for MiniJava

MP 3 A Lexer for MiniJava MP 3 A Lexer for MiniJava CS 421 Spring 2010 Revision 1.0 Assigned Tuesday, February 2, 2010 Due Monday, February 8, at 10:00pm Extension 48 hours (20% penalty) Total points 50 (+5 extra credit) 1 Change

More information

CSE341, Fall 2011, Midterm Examination October 31, 2011

CSE341, Fall 2011, Midterm Examination October 31, 2011 CSE341, Fall 2011, Midterm Examination October 31, 2011 Please do not turn the page until the bell rings. Rules: The exam is closed-book, closed-note, except for one side of one 8.5x11in piece of paper.

More information

Trees. A tree is a directed graph with the property

Trees. A tree is a directed graph with the property 2: Trees Trees A tree is a directed graph with the property There is one node (the root) from which all other nodes can be reached by exactly one path. Seen lots of examples. Parse Trees Decision Trees

More information

Principles of Programming Languages Topic: Logic Programming Professor Lou Steinberg

Principles of Programming Languages Topic: Logic Programming Professor Lou Steinberg Principles of Programming Languages Topic: Logic Programming Professor Lou Steinberg 1 Logic Programming True facts: If I was born in year B, then in year Y on my birthday I turned Y-B years old I turned

More information

(Provisional) Lecture 22: Rackette Overview, Binary Tree Analysis 10:00 AM, Oct 27, 2017

(Provisional) Lecture 22: Rackette Overview, Binary Tree Analysis 10:00 AM, Oct 27, 2017 Integrated Introduction to Computer Science Hughes (Provisional) Lecture 22: Rackette Overview, Binary Tree Analysis 10:00 Contents 1 Announcements 1 2 An OCaml Debugging Tip 1 3 Introduction to Rackette

More information

CS 314 Principles of Programming Languages. Lecture 11

CS 314 Principles of Programming Languages. Lecture 11 CS 314 Principles of Programming Languages Lecture 11 Zheng Zhang Department of Computer Science Rutgers University Wednesday 12 th October, 2016 Zheng Zhang 1 eddy.zhengzhang@cs.rutgers.edu Class Information

More information

COMPUTER SCIENCE TRIPOS

COMPUTER SCIENCE TRIPOS CST.2011.3.1 COMPUTER SCIENCE TRIPOS Part IB Monday 6 June 2011 1.30 to 4.30 COMPUTER SCIENCE Paper 3 Answer five questions. Submit the answers in five separate bundles, each with its own cover sheet.

More information

R13 SET Discuss how producer-consumer problem and Dining philosopher s problem are solved using concurrency in ADA.

R13 SET Discuss how producer-consumer problem and Dining philosopher s problem are solved using concurrency in ADA. R13 SET - 1 III B. Tech I Semester Regular Examinations, November - 2015 1 a) What constitutes a programming environment? [3M] b) What mixed-mode assignments are allowed in C and Java? [4M] c) What is

More information

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath UNIT - I Introduction to C Programming Introduction to C C was originally developed in the year 1970s by Dennis Ritchie at Bell Laboratories, Inc. C is a general-purpose programming language. It has been

More information

Logic Programming. CITS 3242 Programming Paradigms. Topic 16: Part IV: Advanced Topics

Logic Programming. CITS 3242 Programming Paradigms. Topic 16: Part IV: Advanced Topics CITS 3242 Programming Paradigms Part IV: Advanced Topics Topic 16: Logic Programming Logic Programming is a paradigm for programming by declaring facts and rules. Programs are executed by querying whether

More information

CS 6353 Compiler Construction Project Assignments

CS 6353 Compiler Construction Project Assignments CS 6353 Compiler Construction Project Assignments In this project, you need to implement a compiler for a language defined in this handout. The programming language you need to use is C or C++ (and the

More information

What needs to be kept track of ffl The current substitution ffl The current goal ffl The clause currently in use ffl Backtrack Information 2

What needs to be kept track of ffl The current substitution ffl The current goal ffl The clause currently in use ffl Backtrack Information 2 Prolog Implementation ffl The Generic Idea: Everything except unification. ffl An Introduction to the WAM: Flat unification only. 1 What needs to be kept track of ffl The current substitution ffl The current

More information

CSE 431S Type Checking. Washington University Spring 2013

CSE 431S Type Checking. Washington University Spring 2013 CSE 431S Type Checking Washington University Spring 2013 Type Checking When are types checked? Statically at compile time Compiler does type checking during compilation Ideally eliminate runtime checks

More information

Logic As a Query Language. Datalog. A Logical Rule. Anatomy of a Rule. sub-goals Are Atoms. Anatomy of a Rule

Logic As a Query Language. Datalog. A Logical Rule. Anatomy of a Rule. sub-goals Are Atoms. Anatomy of a Rule Logic As a Query Language Datalog Logical Rules Recursion SQL-99 Recursion 1 If-then logical rules have been used in many systems. Most important today: EII (Enterprise Information Integration). Nonrecursive

More information

Implementation of Lambda-Free Higher-Order Superposition. Petar Vukmirović

Implementation of Lambda-Free Higher-Order Superposition. Petar Vukmirović Implementation of Lambda-Free Higher-Order Superposition Petar Vukmirović Automatic theorem proving state of the art FOL HOL 2 Automatic theorem proving challenge HOL High-performance higher-order theorem

More information

CS 231 Data Structures and Algorithms, Fall 2016

CS 231 Data Structures and Algorithms, Fall 2016 CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard

More information

CS Introduction to Artificial Intelligence

CS Introduction to Artificial Intelligence CS 540-1 -- Introduction to Articial Intelligence Exam 2 - November 4, 1992 CLOSED BOOK 90 minutes Write your answers on these pages and show your work. If you feel that a question is not fully specied,

More information

Project 4 Due 11:59:59pm Thu, May 10, 2012

Project 4 Due 11:59:59pm Thu, May 10, 2012 Project 4 Due 11:59:59pm Thu, May 10, 2012 Updates Apr 30. Moved print() method from Object to String. Apr 27. Added a missing case to assembler.ml/build constants to handle constant arguments to Cmp.

More information

Week 7 Prolog overview

Week 7 Prolog overview Week 7 Prolog overview A language designed for A.I. Logic programming paradigm Programmer specifies relationships among possible data values. User poses queries. What data value(s) will make this predicate

More information

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT C Review MaxMSP Developers Workshop Summer 2009 CNMAT C Syntax Program control (loops, branches): Function calls Math: +, -, *, /, ++, -- Variables, types, structures, assignment Pointers and memory (***

More information

Lecture 9: A closer look at terms

Lecture 9: A closer look at terms Lecture 9: A closer look at terms Theory Introduce the == predicate Take a closer look at term structure Introduce strings in Prolog Introduce operators Exercises Exercises of LPN: 9.1, 9.2, 9.3, 9.4,

More information

Declaring Numbers. Bernd Braßel, Frank Huch and Sebastian Fischer. Department of Computer Science, University of Kiel, Germany

Declaring Numbers. Bernd Braßel, Frank Huch and Sebastian Fischer. Department of Computer Science, University of Kiel, Germany Declaring Numbers Bernd Braßel, Frank Huch and Sebastian Fischer Department of Computer Science, University of Kiel, Germany WFLP 2007, Paris, France I m going to present joint work with my colleagues

More information

Types and Type Inference

Types and Type Inference Types and Type Inference Mooly Sagiv Slides by Kathleen Fisher and John Mitchell Reading: Concepts in Programming Languages, Revised Chapter 6 - handout on the course homepage Outline General discussion

More information

UW CSE 351, Winter 2013 Final Exam

UW CSE 351, Winter 2013 Final Exam Full Name: Student ID #: UW CSE 351, Winter 2013 Final Exam March 20, 2013 2:30pm - 4:20pm Instructions: Write your full name and UW student ID number on the front of the exam. When the exam begins, make

More information

Unit 2: Boolean Logic

Unit 2: Boolean Logic AP Computer Science Mr. Haytock Unit 2: Boolean Logic Topics: I. Syllogisms and propositional logic II. Logical operators III. Constructing truth tables IV. Laws of Boolean algebra V. Boolean operations

More information

Postfix (and prefix) notation

Postfix (and prefix) notation Postfix (and prefix) notation Also called reverse Polish reversed form of notation devised by mathematician named Jan Łukasiewicz (so really lü-kä-sha-vech notation) Infix notation is: operand operator

More information

Class Information ANNOUCEMENTS

Class Information ANNOUCEMENTS Class Information ANNOUCEMENTS Third homework due TODAY at 11:59pm. Extension? First project has been posted, due Monday October 23, 11:59pm. Midterm exam: Friday, October 27, in class. Don t forget to

More information

EE 109 Lab 8a Conversion Experience

EE 109 Lab 8a Conversion Experience EE 109 Lab 8a Conversion Experience 1 Introduction In this lab you will write a small program to convert a string of digits representing a number in some other base (between 2 and 10) to decimal. The user

More information

A language is a subset of the set of all strings over some alphabet. string: a sequence of symbols alphabet: a set of symbols

A language is a subset of the set of all strings over some alphabet. string: a sequence of symbols alphabet: a set of symbols The current topic:! Introduction! Object-oriented programming: Python! Functional programming: Scheme! Python GUI programming (Tkinter)! Types and values! Logic programming: Prolog! Introduction! Rules,

More information

Introduction to predicate calculus

Introduction to predicate calculus Logic Programming Languages Logic programming systems allow the programmer to state a collection of axioms from which theorems can be proven. Express programs in a form of symbolic logic Use a logical

More information

CMPSCI 187 / Spring 2015 Postfix Expression Evaluator

CMPSCI 187 / Spring 2015 Postfix Expression Evaluator CMPSCI 187 / Spring 2015 Postfix Expression Evaluator Due on Thursday, 05 March, 8:30 a.m. Marc Liberatore and John Ridgway Morrill I N375 Section 01 @ 10:00 Section 02 @ 08:30 1 CMPSCI 187 / Spring 2015

More information

CS 342 Software Design Spring 2018 Term Project Part III Saving and Restoring Exams and Exam Components

CS 342 Software Design Spring 2018 Term Project Part III Saving and Restoring Exams and Exam Components CS 342 Software Design Spring 2018 Term Project Part III Saving and Restoring Exams and Exam Components Due: Wednesday 13 March. Electronic copy due at 3:30 P.M. Optional paper copy may be handed in during

More information

The object level in Prolog. Meta-level predicates and operators. Contents. The flow of computation. The meta level in Prolog

The object level in Prolog. Meta-level predicates and operators. Contents. The flow of computation. The meta level in Prolog Lecture 8 Meta-level predicates and operators Contents Object level vs. meta level Controlling flow of computation Checking and dismantling expressions Comparison operators The object level in Prolog Prolog

More information

CS 387: GAME AI TACTIC AND STRATEGY

CS 387: GAME AI TACTIC AND STRATEGY CS 387: GAME AI TACTIC AND STRATEGY 5/16/2017 Instructor: Santiago Ontañón so367@drexel.edu Class website: https://www.cs.drexel.edu/~santi/teaching/2017/cs387/intro.htm Outline Projects 1/2 Tactic and

More information