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

Size: px
Start display at page:

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

Transcription

1 CS 360: Programming Languages Lecture 10: Logic Programming with Prolog Geoffrey Mainland Drexel University

2 Section 1 Administrivia

3 Midterm Tuesday Midterm is Tuesday, February 14! Study guide is on the web site. You will have 80 minutes for the exam. No Prolog on the midterm. You may bring a single, hand-written, two-sided, in sheet of notes to the exam. This sheet must have your name on it, and it will be collected with your exam.

4 Section 2 Reconsidering the amb Language

5 Solving a logic puzzle with amb Now that we know how the implementation of amb works, how is the following program actually executed? (define (multiple-dwelling) (let ((baker (amb )) (cooper (amb )) (fletcher (amb )) (miller (amb )) (smith (amb ))) (require (distinct? (list baker cooper fletcher miller smith))) (require (not (= baker 5))) (require (not (= cooper 1))) (require (not (= fletcher 5))) (require (not (= fletcher 1))) (require (> miller cooper)) (require (not (= (abs (- smith fletcher)) 1))) (require (not (= (abs (- fletcher cooper)) 1))) (list (list 'baker baker) (list 'cooper cooper) (list 'fletcher fletcher) (list 'miller miller) (list 'smith smith))))

6 Section 3 Prolog

7 Prolog In the previous lecture, we saw a language and interpreter that automated search. Prolog is the most prevalent language that implements automatic search. We called the amb language nondeterministic, but the evaluator itself was actually deterministic it always evaluates the same program in the same way via a systematic search. Prolog also implements systematic search. Becoming proficient in Prolog requires that you really understand how Prolog performs search it s not a magic bullet. See the Online programming language resources link under Resources on the course home page for Prolog tutorials and references.

8 Prolog Syntax A variable is a sequence of letters and digits that begins with an uppercase letter. Examples: X, Ys. An atom is a sequence of letters and digits that begins with a lowercase letter. Examples: bart, lisa. A single underscore, _, represents an anonymous variable and means I don t care what this value is.

9 Facts about the Simpsons father(grampa,homer). father(homer,bart). father(homer,lisa). father(homer,maggie). mother(marge,bart). mother(marge,lisa). mother(marge,maggie).?- father(homer,x). % X = bart % yes?- mother(x,bart). % X = marge % yes

10 Using facts about the Simpsons father(grampa,homer). father(homer,bart). father(homer,lisa). father(homer,maggie). mother(marge,bart). mother(marge,lisa). mother(marge,maggie). parent(x,y) :- father(x,y). parent(x,y) :- mother(x,y).?- parent(x,bart). % X = homer? ; % X = marge % yes The program above says that parent(x,y) is a fact if father(x,y) is a fact. How else can parent(x,y) be derived as a fact? You can read :- as =, or right-to-left implication. Entering ; into the interpreter after a query has been answered tells the interpreter to give us the next answer to the query.

11 Clauses father(grampa,homer). father(homer,bart). father(homer,lisa). father(homer,maggie). mother(marge,bart). mother(marge,lisa). mother(marge,maggie). parent(x,y) :- father(x,y). parent(x,y) :- mother(x,y). Prolog is based on Horn clauses. A clause has a head and a body. The above program has 9 clauses. The first 7 have no body. A clause without a body is also called a fact, as we have seen. The last two clauses have the same head, parent(x,y). For the head of a clause to hold, all of the facts in its body must hold. Facts in the body of a clause are separated by a comma, which can be read as and. In Prolog, we express programs using relations, not functions.

12 An exercise Let s try to write a predicate mated(x,y) that holds when X and Y are both parents of the same person. Remember, the body of a clause can contain multiple facts.

13 How Prolog performs search parent(marge,bart). ancestor(x,y) :- parent(x,y). ancestor(x,y) :- parent(z,y), ancestor(x,z). We can trace the execution of a query using the trace command in gnuprolog (tracing can be turned off with notrace). Let s see how Prolog executes the query ancestor(x,bart)..

14 How Prolog performs search parent(marge,bart). ancestor(x,y) :- parent(x,y). ancestor(x,y) :- parent(z,y), ancestor(x,z). Let s see how Prolog executes the query ancestor(x,bart)... ancestor(_16,bart) parent(_16,bart) parent(_85,bart), ancestor(_16,_85) parent(marge,bart) Success! {_16 = marge} parent(marge,bart) {_85 = marge} parent(_16,marge) Fail! parent(_134,marge), ancestor(_16,_134) parent(_134,marge) Fail!

15 How Prolog performs search How would search progress this time given the query ancestor(x,bart).? Note the minor change in our program. parent(marge,bart). ancestor(x,y) :- parent(x,y). ancestor(x,y) :- ancestor(x,z), parent(z,y). For reference, here is the old path through the search space. ancestor(_16,bart) parent(_16,bart) parent(_85,bart), ancestor(_16,_85) parent(marge,bart) Success! {_16 = marge} parent(marge,bart) {_85 = marge} parent(_16,marge) Fail! parent(_134,marge), ancestor(_16,_134) parent(_134,marge) Fail!

16 Writing programs in Prolog Prolog programs are relations, not functions. By convention, the result is the last argument in a relation. Think of each clause in a definition as a different case. Try to simplify code by matching only the specific case you are interested in.

17 Lists in Prolog In Prolog... [X,Y] is a list with two elements. [H T] is a list whose head is H and whose tail is T. We can also write, for example, [0 [1,2,3]]. This is the list [0,1,2,3]. As an exercise, let s write member, append, and reverse in Prolog.

18 Unification in Prolog Consider the member function we just wrote. We used the variable X twice in a pattern this expressed the constraint that these two values must be equal. Prolog uses unification to instantiate variables, i.e., to determine what values variables have. The expression s = t attempts to unify the expressions s and t. Unification takes two terms, s and t, and attempts to find a substitution for the variables in s and t that make s and t equal. The underscore variable, _, is special: it does not represent the same term everywhere it is used.

19 Unification in Prolog: examples bart = bart bart = lisa bart = X f(a, X) = f(y, b) f(x) = g(x) f(x) = f(a, b) f(a, g(x)) = f(y, b) f(a, g(x)) = f(y, g(b)) Unification is an extremely important algorithm. In particular, it is used for type inference in many languages, e.g., ML, Haskell, F#.

20 Arithmetic in Prolog Recall that = means terms should be unified. To test for term equality, use ==. For term inequality, use \=. Les-than-or-equal is =<. What do you think X = does? How about X == 2 + 3? How about == 2 + 3? To force evaluation of arithmetic expressions, use is. For example, X is What do you think X =< 2 does? Variables are only instantiated via unification.

21 Arithmetic in Prolog: gcd gcd(u,0,u). gcd(u,v,w) :- V \= 0, R is U mod V, gcd(v,r,w).

22 Implementing ; in the amb Language How could we implement ; in our amb interpreter? That is, how can we get the next solution? (define (ambeval exp env succeed fail) ((analyze exp) env succeed fail)) Calling ambeval: (ambeval <exp> the-global-environment (lambda (value fail) value) (lambda () 'failed))

23 Running programs backwards In Scheme, how would you write a program that takes a list xs, the result, zs, of appending that list to some (unknown) list ys, and computes zs? That is, I want (appendee '(1 2 3) '( )) to return the result '(4). How could we solve this problem in Prolog using a program we already wrote? This is one advantage of expressing programs as relations instead of functions.

24 The power of declarative programming? Prolog is often termed a declarative language because programmers declare what a program is supposed to do, but not how it is supposed to be done. That is, the Prolog implementation has to search for a solution. The original idea was that programmers could write a declarative specification for a program, and the language would find an implementation for that specification. Let s see an example... sort(x,y) :- permute(x,y), sorted(y). insert(x,ys,[x YS]). insert(x,[y YS],[Y ZS]) :- insert(x,ys,zs). permute([],[]). permute([x XS],YS) :- permute(xs,zs), insert(x,zs,ys). sorted([]). sorted([_]). sorted([xa,xb XS]) :- XA =< XB, sorted([xb XS]).

25 The power of declarative programming? How efficient is the sort that we just specified declaratively? How declarative is Prolog? Can the programmer really write a fully declarative specification? Recall our ancestor predicate. How declarative is a language when order matters so much? parent(marge,bart). ancestor(x,y) :- ancestor(x,z), parent(z,y). ancestor(x,y) :- parent(x,y).

26 Cut Consider the following program for using association lists: assoc(x,[[x,y] _],Y). assoc(x,[_ YS],Z) :- assoc(x,ys,z). lookup(x,y) :- assoc(x,[[break,statement], [double,type],[int,type], [return,statement]],y),!. What do you expect lookup(break,x). to do? How about lookup(x,statement).? The cut prunes all further sibling nodes in the search tree. If Prolog encounters a cut node while backtracking, search continues with the grandparent node.

27 Cut: another example Consider the following program: a(x, Y) :- b(x),!, c(y). b(1). b(2). b(3). c(1). c(2). c(3). If we ask?- a(q, R)., what should we expect?

28 Cut: a third example Consider the following slightly different program: a(x) :- b(x),!, c(x). b(1). b(2). b(3). c(2). If we ask?- a(x)., what should we expect? What about?- a(2).?

29 Prolog vs. amb What are some of the big differences between Prolog and the amb language? amb is a general-purpose language with built-in support for search. Prolog programs must be expressed as Horn clauses. Prolog implements pattern matching and unification.

30 Prolog: Summary Prolog programs are expressed as a sequence of clauses. Prolog predicates are relations, not functions. A Prolog implementation searches for a solution to a query systematically: clauses are processed top-to-bottom, and the terms in the body of a clause are processed left-to-right.

CS 360: Programming Language Concepts Lecture 15 Logic Programming

CS 360: Programming Language Concepts Lecture 15 Logic Programming Reference: Ch. 12.1-12.5 CS 360: Programming Language Concepts Lecture 15 Logic Programming I. Logic Programming (Generally) What is logic programming? A logic program is a collection of declarations,

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

Chapter 16. Logic Programming Languages

Chapter 16. Logic Programming Languages Chapter 16 Logic Programming Languages Chapter 16 Topics Introduction A Brief Introduction to Predicate Calculus Predicate Calculus and Proving Theorems An Overview of Logic Programming The Origins of

More information

Logic Programming Languages

Logic Programming Languages Logic Programming Languages Introduction Logic programming languages, sometimes called declarative programming languages Express programs in a form of symbolic logic Use a logical inferencing process to

More information

Chapter 5. Pure PROLOG. Foundations of Logic Programming

Chapter 5. Pure PROLOG. Foundations of Logic Programming Chapter 5 1 Outline vs. logic programming Lists in Adding Arithmetics to Adding the Cut to 2 Syntax of Pure Prolog p(x,a) :- q(x), r(x,yi). p(x, a) q(x), r(x,y i ) % Comment Ambivalent syntax: p(p(a,b),

More information

Chapter 16. Logic Programming Languages ISBN

Chapter 16. Logic Programming Languages ISBN Chapter 16 Logic Programming Languages ISBN 0-321-49362-1 Chapter 16 Topics Introduction A Brief Introduction to Predicate Calculus Predicate Calculus and Proving Theorems An Overview of Logic Programming

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

The Idea Underlying Logic Programming. CSci 8980, Fall 2012 Specifying and Reasoning About Computational Systems An Introduction to Logic Programming

The Idea Underlying Logic Programming. CSci 8980, Fall 2012 Specifying and Reasoning About Computational Systems An Introduction to Logic Programming The Idea Underlying CSci 8980, Fall 2012 Specifying and Reasoning About Computational Systems An Introduction to Department of Computer Science and Engineering University of Minnesota Lectures in Fall

More information

Derived from PROgramming in LOGic (1972) Prolog and LISP - two most popular AI languages. Prolog programs based on predicate logic using Horn clauses

Derived from PROgramming in LOGic (1972) Prolog and LISP - two most popular AI languages. Prolog programs based on predicate logic using Horn clauses Prolog Programming Derived from PROgramming in LOGic (1972) Good at expressing logical relationships between concepts Prolog and LISP - two most popular AI languages Execution of a Prolog program is a

More information

CS 360: Programming Languages Lecture 10: Introduction to Haskell

CS 360: Programming Languages Lecture 10: Introduction to Haskell CS 360: Programming Languages Lecture 10: Introduction to Haskell Geoffrey Mainland Drexel University Thursday, February 5, 2015 Adapted from Brent Yorgey s course Introduction to Haskell. Section 1 Administrivia

More information

Declarative Programming Prolog CS360

Declarative Programming Prolog CS360 Declaratie Programming Prolog CS360 Terms Numerical literals String literals Ø By default, any string that starts is all lowercase Ø Use single quotes for anything else Atoms Ø Essentially, strings Ø Also,

More information

Topic 1: Introduction to Knowledge- Based Systems (KBSs)

Topic 1: Introduction to Knowledge- Based Systems (KBSs) Topic 1: Introduction to Knowledge- Based Systems (KBSs) 1.5 Introduction to Logic Programming (Prolog) 32 Simple example: Algorithm for checking sorted list? We all know how to check if a list is sorted:

More information

CPS 506 Comparative Programming Languages. Programming Language Paradigm

CPS 506 Comparative Programming Languages. Programming Language Paradigm CPS 506 Comparative Programming Languages Logic Programming Language Paradigm Topics Introduction A Brief Introduction to Predicate Calculus Predicate Calculus and Proving Theorems An Overview of Logic

More information

CS 4700: Artificial Intelligence

CS 4700: Artificial Intelligence CS 4700: Foundations of Artificial Intelligence Fall 2017 Instructor: Prof. Haym Hirsh Lecture 16 Cornell Cinema Thursday, April 13 7:00pm Friday, April 14 7:00pm Sunday, April 16 4:30pm Cornell Cinema

More information

Fall Semester, Lecture Notes { November 12, Variations on a Scheme Nondeterministic evaluation

Fall Semester, Lecture Notes { November 12, Variations on a Scheme Nondeterministic evaluation 1 MASSACHUSETTS INSTITUTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs Fall Semester, 1996 Lecture Notes { November 12,

More information

Agenda. CS301 Session 20. A logic programming trick. A simple Prolog program. Introduction to logic programming Examples Semantics

Agenda. CS301 Session 20. A logic programming trick. A simple Prolog program. Introduction to logic programming Examples Semantics CS301 Session 20 Introduction to logic programming Examples Semantics Agenda 1 2 A logic programming trick A two-way translator in two lines of code: translate([],[]). translate([word Words],[Mot Mots])

More information

CSEN403 Concepts of Programming Languages. Topics: Logic Programming Paradigm: PROLOG Search Tree Recursion Arithmetic

CSEN403 Concepts of Programming Languages. Topics: Logic Programming Paradigm: PROLOG Search Tree Recursion Arithmetic CSEN403 Concepts of Programming Languages Topics: Logic Programming Paradigm: PROLOG Search Tree Recursion Arithmetic Prof. Dr. Slim Abdennadher 8.2.2015 c S. Abdennadher 1 Logic Programming versus Prolog

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

Topic B: Backtracking and Lists

Topic B: Backtracking and Lists Topic B: Backtracking and Lists 1 Recommended Exercises and Readings From Programming in Prolog (5 th Ed.) Readings: Chapter 3 2 Searching for the Answer In order for a Prolog program to report the correct

More information

Quick n Dirty Prolog Tutorial

Quick n Dirty Prolog Tutorial CSc 245 Introduction to Discrete Structures Quick n Dirty Prolog Tutorial (McCann) Last Revised: February 2014 Background: Prolog, whose name is from the phrase PROgramming in LOGic, is a special purpose

More information

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

CS 320: Concepts of Programming Languages

CS 320: Concepts of Programming Languages CS 320: Concepts of Programming Languages Wayne Snyder Computer Science Department Boston University Lecture 04: Basic Haskell Continued o Polymorphic Types o Type Inference with Polymorphism o Standard

More information

Chapter 16. Logic Programming Languages ISBN

Chapter 16. Logic Programming Languages ISBN Chapter 16 Logic Programming Languages ISBN 0-321-49362-1 Chapter 16 Topics Introduction A Brief Introduction to Predicate Calculus Predicate Calculus and Proving Theorems An Overview of Logic Programming

More information

will take you everywhere.

will take you everywhere. Prolog COMP360 Logic will get you from A to B. Imagination will take you everywhere. Albert Einstein Prolog Assignment A programming assignment in Prolog has been posted on Blackboard Upload your.pl file

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

Advanced Prolog Programming

Advanced Prolog Programming 1 Introduction CIS335, Assignment 4 Advanced Prolog Programming Geraint Wiggins November 12, 2004 This practical is the second self-assessed exercise for MSc students on the Prolog module. It is intended

More information

ACSC300: Logic Programming

ACSC300: Logic Programming ACSC300: Logic Programming Lecture 1: Introduction to Logic Programming and Prolog Harris Papadopoulos Course Aims This course aims to help you to: Understand a radically different programming paradigm:

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

Logic Programming and Resolution Lecture notes for INF3170/4171

Logic Programming and Resolution Lecture notes for INF3170/4171 Logic Programming and Resolution Lecture notes for INF3170/4171 Leif Harald Karlsen Autumn 2015 1 Introduction This note will explain the connection between logic and computer programming using Horn Clauses

More information

Lecture 11: Feb. 10, 2016

Lecture 11: Feb. 10, 2016 CS323: AI (Hands on with Prolog) Spring 2016 Lecturer: K.R. Chowdhary Lecture 11: Feb. 10, 2016 : Professor of CS (VF) Disclaimer: These notes have not been subjected to the usual scrutiny reserved for

More information

Introduction to Programming, Aug-Dec 2006

Introduction to Programming, Aug-Dec 2006 Introduction to Programming, Aug-Dec 2006 Lecture 3, Friday 11 Aug 2006 Lists... We can implicitly decompose a list into its head and tail by providing a pattern with two variables to denote the two components

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

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

Exercises on the Fundamentals of Prolog

Exercises on the Fundamentals of Prolog 1 Introduction Exercises on the Fundamentals of Prolog These exercises are intended to help reinforce material taught in the lectures of CIS335 course in Prolog. They do not contribute any marks to the

More information

PROgramming in LOGic PROLOG Recursion, Lists & Predicates

PROgramming in LOGic PROLOG Recursion, Lists & Predicates PROgramming in LOGic PROLOG Recursion, Lists & Predicates CSC9Y4 1 Recursion Recursion in Prolog means placing in the body of a rule a call to the predicate which occurs in the head of the rule. Here is

More information

simplicity hides complexity flow of control, negation, cut, 2 nd order programming, tail recursion procedural and declarative semantics and & or

simplicity hides complexity flow of control, negation, cut, 2 nd order programming, tail recursion procedural and declarative semantics and & or simplicity hides complexity flow of control, negation, cut, 2 nd order programming, tail recursion simple and/or composition of goals hides complex control patterns not easily represented by traditional

More information

Introduction to Prolog

Introduction to Prolog Introduction to Prolog David Woods dwoods@scss.tcd.ie Week 3 - HT Declarative Logic The Prolog programming language is, at its theoretical core, a declarative language. This is unlike more commonly used

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

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

Lecture 16: Logic Programming in Prolog

Lecture 16: Logic Programming in Prolog Lecture 16: Logic Programming in Prolog COMP 524 Programming Language Concepts Stephen Olivier March 26, 2009 Based on slides by A. Block, notes by N. Fisher, F. Hernandez-Campos, and D. Stotts Goal of

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

Logic Programming: Lecture 1

Logic Programming: Lecture 1 Logic Programming: Lecture 1 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in PLC, 3 April 2017 Logic programming Programming with relations Variables Names starting with a capital letter

More information

CS 360: Programming Languages Lecture 12: More Haskell

CS 360: Programming Languages Lecture 12: More Haskell CS 360: Programming Languages Lecture 12: More Haskell Geoffrey Mainland Drexel University Adapted from Brent Yorgey s course Introduction to Haskell. Section 1 Administrivia Administrivia Homework 5 due

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

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

Multi-paradigm Declarative Languages

Multi-paradigm Declarative Languages Michael Hanus (CAU Kiel) Multi-paradigm Declarative Languages ICLP 2007 1 Multi-paradigm Declarative Languages Michael Hanus Christian-Albrechts-University of Kiel Programming Languages and Compiler Construction

More information

CS 320 Midterm Exam. Fall 2018

CS 320 Midterm Exam. Fall 2018 Name: BU ID: CS 320 Midterm Exam Fall 2018 Write here the number of the problem you are skipping: You must complete 5 of the 6 problems on this exam for full credit. Each problem is of equal weight. Please

More information

An introduction to Prolog. P. Cabalar ( Department Prolog of Computer Science University of Corunna, FebruarySPAIN 8, 2016)

An introduction to Prolog. P. Cabalar ( Department Prolog of Computer Science University of Corunna, FebruarySPAIN 8, 2016) An introduction to Prolog Pedro Cabalar Department of Computer Science University of Corunna, SPAIN February 8, 2016 P. Cabalar ( Department Prolog of Computer Science University of Corunna, FebruarySPAIN

More information

1 of 5 5/11/2006 12:10 AM CS 61A Spring 2006 Midterm 2 solutions 1. Box and pointer. Note: Please draw actual boxes, as in the book and the lectures, not XX and X/ as in these ASCII-art solutions. Also,

More information

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder Outline 1 2 3 4 What is Haskell? Haskell is a functional programming language. Characteristics functional non-strict ( lazy ) pure (no side effects*) strongly statically typed available compiled and interpreted

More information

CSE 452: Programming Languages. Prolog Statements. Loading the Knowledge Base. Logical Programming Languages Part 2

CSE 452: Programming Languages. Prolog Statements. Loading the Knowledge Base. Logical Programming Languages Part 2 CSE 452: Programming Languages Logical Programming Languages Part 2 Prolog Statements Prolog statements consist of facts, rules, and queries. Example of facts (written as headless Horn clauses) male(tony).

More information

6.034 Artificial Intelligence, Fall 2006 Prof. Patrick H. Winston. Problem Set 1

6.034 Artificial Intelligence, Fall 2006 Prof. Patrick H. Winston. Problem Set 1 6.034 Artificial Intelligence, Fall 2006 Prof. Patrick H. Winston Problem Set 1 This problem set is due Wednesday, September 20. If you have questions about it, ask the TA email list. Your response will

More information

Advanced Logic and Functional Programming

Advanced Logic and Functional Programming Advanced Logic and Functional Programming Lecture 1: Programming paradigms. Declarative programming. From first-order logic to Logic Programming. Programming paradigms Programming paradigm (software engineering)

More information

IN112 Mathematical Logic

IN112 Mathematical Logic Institut Supérieur de l Aéronautique et de l Espace IN112 Mathematical Logic Lab session on Prolog Christophe Garion DMIA ISAE Christophe Garion IN112 IN112 Mathematical Logic 1/ 31 License CC BY-NC-SA

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

7COM1023 Programming Paradigms

7COM1023 Programming Paradigms 7COM1023 Programming Paradigms Practical 7: The Logic Paradigm Tutorial: Exercises Propositional and Predicate Logic form the basis for logic programming. 1. Explain what is meant by propositional logic.

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

Implementação de Linguagens 2016/2017

Implementação de Linguagens 2016/2017 Implementação de Linguagens Ricardo Rocha DCC-FCUP, Universidade do Porto ricroc @ dcc.fc.up.pt Ricardo Rocha DCC-FCUP 1 Logic Programming Logic programming languages, together with functional programming

More information

IN112 Mathematical Logic

IN112 Mathematical Logic Institut Supérieur de l Aéronautique et de l Espace IN112 Mathematical Logic Lab session on Prolog Christophe Garion DMIA ISAE Christophe Garion IN112 IN112 Mathematical Logic 1/ 31 License CC BY-NC-SA

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

Write a procedure powerset which takes as its only argument a set S and returns the powerset of S.

Write a procedure powerset which takes as its only argument a set S and returns the powerset of S. Answers to CS61 A Final of May 23, 1997 We repeat the questions; some have typos corrected with respect to the actual exam handed out. Question 1 (5 points): Let us represent a set S of unique expressions,

More information

Recursion, Structures, and Lists

Recursion, Structures, and Lists Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 4 04/10/04 30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists 1 The central ideas of Prolog

More information

Logic - CM0845 Introduction to Prolog

Logic - CM0845 Introduction to Prolog Logic - CM0845 Introduction to Prolog Diego Alejandro Montoya-Zapata EAFIT University Semester 2016-1 Diego Alejandro Montoya-Zapata (EAFIT University) Logic - CM0845 Introduction to Prolog Semester 2016-1

More information

UNIVERSITY OF TORONTO AT MISSISSAUGA April 2006 Examination CSC 324H5 S Instructor: Richard Krueger Duration 3 hours No Aids Allowed

UNIVERSITY OF TORONTO AT MISSISSAUGA April 2006 Examination CSC 324H5 S Instructor: Richard Krueger Duration 3 hours No Aids Allowed UNIVERSITY OF TORONTO AT MISSISSAUGA April 2006 Examination CSC 324H5 S Instructor: Richard Krueger Duration 3 hours No Aids Allowed Student Number: Last (Family) Name(s): First (Given) Name(s): SOLUTIONS

More information

The Metalanguage λprolog and Its Implementation

The Metalanguage λprolog and Its Implementation The Metalanguage λprolog and Its Implementation Gopalan Nadathur Computer Science Department University of Minnesota (currently visiting INRIA and LIX) 1 The Role of Metalanguages Many computational tasks

More information

Shell CSCE 314 TAMU. Haskell Functions

Shell CSCE 314 TAMU. Haskell Functions 1 CSCE 314: Programming Languages Dr. Dylan Shell Haskell Functions 2 Outline Defining Functions List Comprehensions Recursion 3 Conditional Expressions As in most programming languages, functions can

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

Using Prolog as a CAS

Using Prolog as a CAS Using Prolog as a CAS Kharkiv Pedagogical University A. Stolyarevska July 2002 References 1) Robinson J.A. A machine-oriented logic based on the resolution principle. J. ACM, 12, 23-41. 2) Colmerauer A.

More information

10. Logic Programming With Prolog

10. Logic Programming With Prolog Copyright (C) R.A. van Engelen, FSU Department of Computer Science, 2000 10. Logic Programming With Overview Logic Programming Logic Programming Logic programming is a form of declarative programming A

More information

CSC4504/Prolog. : Formal Languages & Applications. J Paul Gibson, D311. An Introduction To Prolog

CSC4504/Prolog. : Formal Languages & Applications. J Paul Gibson, D311. An Introduction To Prolog CSC4504/Prolog. : Formal Languages & Applications J Paul Gibson, D311 paul.gibson@telecom-sudparis.eu http://www-public.telecom-sudparis.eu/~gibson/teaching/csc4504/ An Introduction To Prolog /~gibson/teaching/csc4504/problem8-prolog.pdf

More information

UNIVERSITETET I OSLO

UNIVERSITETET I OSLO Exam in INF3110, December 12, 2013 Page 1 UNIVERSITETET I OSLO Det matematisk-naturvitenskapelige fakultet Exam in: INF3110 Programming Languages Day of exam: December 12, 2013 Exam hours: 14:30 18:30

More information

Operational Semantics

Operational Semantics 15-819K: Logic Programming Lecture 4 Operational Semantics Frank Pfenning September 7, 2006 In this lecture we begin in the quest to formally capture the operational semantics in order to prove properties

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

Introduction to Logic Programming in Prolog 1 / 39

Introduction to Logic Programming in Prolog 1 / 39 Introduction to Logic Programming in Prolog 1 / 39 Outline Programming paradigms Logic programming basics Introduction to Prolog Predicates, queries, and rules Understanding the query engine Goal search

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

simplicity hides complexity flow of control, negation, cut, 2 nd order programming, tail recursion procedural and declarative semantics and & or

simplicity hides complexity flow of control, negation, cut, 2 nd order programming, tail recursion procedural and declarative semantics and & or simplicity hides complexity flow of control, negation, cut, 2 nd order programming, tail recursion simple and/or composition of goals hides complex control patterns not easily represented by traditional

More information

Questions & Answers 28 Sept.

Questions & Answers 28 Sept. Questions & Answers 28 Sept. Functional Programming 2017/18 Alejandro Serrano 0 Administrativia 1 Format of the exam Similar to the previous exams in the web The exam is on paper 2 Some places (wrongly)

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

COMP219: Artificial Intelligence. Lecture 6: Recursion in Prolog

COMP219: Artificial Intelligence. Lecture 6: Recursion in Prolog COMP219: Artificial Intelligence Lecture 6: Recursion in Prolog 1 Overview Last time Introduction to Prolog: facts, rules, queries; family tree program Today: Recursive rules in Prolog; writing and evaluating

More information

CSE Programming Languages Final exam - Spring Answer Key

CSE Programming Languages Final exam - Spring Answer Key CSE 341 - Programming Languages Final exam - Spring 2018 - Answer Key 1. (8 points) Write a Racket function multicons that takes an item x, a non-negative integer n, and a list xs; and returns a new list

More information

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions 1 CSCE 314: Programming Languages Dr. Flemming Andersen Haskell Functions 2 Outline Defining Functions List Comprehensions Recursion 3 Conditional Expressions As in most programming languages, functions

More information

CILOG User Manual Version 0.14

CILOG User Manual Version 0.14 CILOG User Manual Version 0.14 David Poole Department of Computer Science, University of British Columbia, Vancouver, B.C. Canada V6T 1Z4 Email: poole@cs.ubc.ca URL: http://www.cs.ubc.ca/spider/poole November

More information

This lecture covers: Prolog s execution strategy explained more precisely. Revision of the elementary Prolog data types

This lecture covers: Prolog s execution strategy explained more precisely. Revision of the elementary Prolog data types This lecture covers: Prolog s execution strategy explained more precisely The execution strategy has been presented as simply placing all matching clauses onto the stack. In fact, Prolog is slightly more

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

The Logic Paradigm. Joseph Spring. 7COM1023 Programming Paradigms

The Logic Paradigm. Joseph Spring. 7COM1023 Programming Paradigms The Logic Paradigm Joseph Spring 7COM1023 Programming Paradigms 1 Discussion The Logic Paradigm Propositional and Predicate Logic See also notes and slides on PP website Horn Clauses Definition, Examples

More information

Programming Language Concepts Logic Programming Prolog. Janyl Jumadinova 14 March, 2017

Programming Language Concepts Logic Programming Prolog. Janyl Jumadinova 14 March, 2017 Programming Language Concepts Logic Programming Prolog Janyl Jumadinova 14 March, 2017 Background Japan s Fifth Generation project 1982: (https: //en.wikipedia.org/wiki/fifth_generation_computer) These

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp-16/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 24! Type inference in ML / Haskell 1 Type Checking vs

More information

CMSC 331 Final Exam Section 0201 December 18, 2000

CMSC 331 Final Exam Section 0201 December 18, 2000 CMSC 331 Final Exam Section 0201 December 18, 2000 Name: Student ID#: You will have two hours to complete this closed book exam. We reserve the right to assign partial credit, and to deduct points for

More information

Programming Language Concepts: Lecture 22

Programming Language Concepts: Lecture 22 Programming Language Concepts: Lecture 22 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2009 PLC 2009, Lecture 22, 15 April 2009 Logic programming

More information

Overview. Declarative Languages. operation of del. Deleting an element from a list. functions using del. inserting elements with del

Overview. Declarative Languages. operation of del. Deleting an element from a list. functions using del. inserting elements with del Overview Declarative Languages D7012E: Arithmetic and Backtracking Fredrik Bengtsson Some standard functions Operators Arithmetic The eight queens problem Controlling backtracking cut Deleting an element

More information

Module 7. Knowledge Representation and Logic (Rule based Systems) Version 2 CSE IIT, Kharagpur

Module 7. Knowledge Representation and Logic (Rule based Systems) Version 2 CSE IIT, Kharagpur Module 7 Knowledge Representation and Logic (Rule based Systems) Lesson 18 Rule based Systems - II 7.2.5 Programs in PROLOG These minimal notes on Prolog show only some of its flavor. Here are facts plays(ann,fido).

More information

Family Example: Some Facts. respects(barb,dan). respects(barb,katie). respects(dan,brian). respects(dan,barbara).

Family Example: Some Facts. respects(barb,dan). respects(barb,katie). respects(dan,brian). respects(dan,barbara). Family Example: Some Facts respects(barb,dan). respects(barb,katie). respects(dan,brian). respects(dan,barbara). Some Queries?- respects(barb,katie). yes?- respects(barb,x). X=dan; % you type ; and RETURN

More information

Type Systems, Type Inference, and Polymorphism

Type Systems, Type Inference, and Polymorphism 6 Type Systems, Type Inference, and Polymorphism Programming involves a wide range of computational constructs, such as data structures, functions, objects, communication channels, and threads of control.

More information

CAP 5602 Summer, Lesson 5: Lists. The topics 1. updating a counter 2. the list structure 3. some useful built-in predicates for lists

CAP 5602 Summer, Lesson 5: Lists. The topics 1. updating a counter 2. the list structure 3. some useful built-in predicates for lists CAP 5602 Summer, 20 Lesson 5: Lists The topics. updating a counter 2. the list structure 3. some useful built-in predicates for lists. Updating a counter Let us try to implement the increse the counter

More information

Introduction to Logic Programming. Ambrose

Introduction to Logic Programming. Ambrose Introduction to Logic Programming Ambrose Bonnaire-Sergeant @ambrosebs abonnairesergeant@gmail.com Introduction to Logic Programming Fundamental Logic Programming concepts Related to FP General implementation

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

Wan Hussain Wan Ishak

Wan Hussain Wan Ishak February 2 nd Session 2014/2015 (A142) Wan Hussain Wan Ishak School of Computing UUM College of Arts and Sciences Universiti Utara Malaysia (P) 04-9285150 (E) hussain@uum.edu.my (U) http://wanhussain.com

More information

Computation Club: Gödel s theorem

Computation Club: Gödel s theorem Computation Club: Gödel s theorem The big picture mathematicians do a lot of reasoning and write a lot of proofs formal systems try to capture the ideas of reasoning and proof in a purely mechanical set

More information

1 / Types and Programming Languages

1 / Types and Programming Languages 1 / 65 263-2200 Types and Programming Languages 2 / 65 Outline Control Flow During Execution Tree Traversal A Detailed Trace Search Space Analysis Modelling Equations and Operations Lists 3 / 65 Outline

More information