Logic Programming II & Revision

Similar documents
Thanks! Review. Course Goals. General Themes in this Course. There are many programming languages. Teaching Assistants. John Mitchell.

8/27/17. CS-3304 Introduction. What will you learn? Semester Outline. Websites INTRODUCTION TO PROGRAMMING LANGUAGES

Lecture 09. Ada to Software Engineering. Mr. Mubashir Ali Lecturer (Dept. of Computer Science)

Organization of Programming Languages (CSE452) Why are there so many programming languages? What makes a language successful?

General Concepts. Abstraction Computational Paradigms Implementation Application Domains Influence on Success Influences on Design

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

Com S 541. Programming Languages I

Programming Languages, Summary CSC419; Odelia Schwartz

G Programming Languages - Fall 2012

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

Programming Language Concepts 1982, 1987, Outline. Period

Programming Paradigms

Control Flow February 9, Lecture 7

Topic I. Introduction and motivation References: Chapter 1 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.

Concepts in Programming Languages

Imperative Programming

Programmiersprachen (Programming Languages)

Introduction. A. Bellaachia Page: 1

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Principles in Programming: Orientation & Lecture 1. SWE2004: Principles in Programming Spring 2014 Euiseong Seo

Outline. Programming Languages 1/16/18 PROGRAMMING LANGUAGE FOUNDATIONS AND HISTORY. Current

Chapter 5 Names, Binding, Type Checking and Scopes

Languages october 22, 2017 Éric Lévénez < FORTRAN III end-1958 FORTRAN II FORTRAN I october 1956

Programming Languages 2nd edition Tucker and Noonan"

Chapter 2 Preview. Preview. History of Programming Languages. History of Programming Languages. History of Programming Languages

Concepts of Programming Languages

COSC 2P90 Programming Languages & Object-Orientation

Course outline. CSE 341: Programming Languages. Why study programming languages? Course motivation and objectives. 1 lecture: Concepts

Types and Type Inference

LECTURE 18. Control Flow

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Lecture 1: Course Introduction

Chapter 15. Functional Programming Languages

SOFTWARE ARCHITECTURE 6. LISP

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

CS508-Modern Programming Solved MCQ(S) From Midterm Papers (1 TO 22 Lectures) BY Arslan

Why are there so many programming languages? Why do we have programming languages? What is a language for? What makes a language successful?

Evolution of PL s. EECS 476, Prog. Lang. Design/ page 1

Example Scheme Function: equal

Low-Level Languages. Computer Programs and Programming Languages

Lecture 1: Course Introduction

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

CSE 307: Principles of Programming Languages

Functional Programming. Big Picture. Design of Programming Languages

The Algol family and ML

A Tour of Language Implementation

Continuations provide a novel way to suspend and reexecute

Introduction to Computer Science Lecture 6: Programming Languages

The Algol family and ML

Introduction to Scientific Computing Languages

Comp 333: Concepts of Programming Languages Fall 2016

CS558 Programming Languages

Discovering Computers 2008

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given

Lecture 9: Control Flow

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

Part (04) Introduction to Programming

Chapter 3:: Names, Scopes, and Bindings (cont.)

2. Evolution of the Major Programming languages

Chapter 3:: Names, Scopes, and Bindings (cont.)

Early computers (1940s) cost millions of dollars and were programmed in machine language. less error-prone method needed

HANDLING NONLOCAL REFERENCES

Chapter 2. 5 * B => A V 6 7 (subscripts) S 1.n 1.n (data types)

CS558 Programming Languages

CS 565: Programming Languages. Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106

Introduction to Scientific Computing Languages

Language Translation, History. CS152. Chris Pollett. Sep. 3, 2008.

Chapter 2. Chapter 2

CS 3304 Comparative Languages. Lecture 1: Introduction

Chapter 11 :: Functional Languages

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

Types and Type Inference

Polymorphism and Type Inference

CS 320: Concepts of Programming Languages

NOTE: Answer ANY FOUR of the following 6 sections:

Types. What is a type?

Lecture 19: Functions, Types and Data Structures in Haskell

Principles in Programming: Orientation & Lecture 1. SWE2004: Principles in Programming Spring 2015 Euiseong Seo

6. Names, Scopes, and Bindings

LECTURE 1. Overview and History

Lecture 2. A Historical View

Programming Languages Third Edition. Chapter 7 Basic Semantics

Imperative languages

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

CSC 533: Organization of Programming Languages. Spring 2005

Lecture 13: Expression Evaluation

Control Structures. Outline. In Text: Chapter 8. Control structures Selection. Iteration. Gotos Guarded statements. One-way Two-way Multi-way

Lecture 15: Iteration and Recursion

MIDTERM EXAM (Solutions)

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

CA341 - Comparative Programming Languages

Programming Language Pragmatics

Names, Scopes, and Bindings II. Hwansoo Han

LECTURE 17. Expressions and Assignment

Programming Languages 1. Introduction. Oscar Nierstrasz

CSc 520. Course Outline (Subject to change) Course Outline (Subject to change)... Principles of Programming Languages. Christian Collberg

Principles of Programming Languages. Lecture Outline

Transcription:

Logic Programming II & Revision Gerardo Schneider Department of Informatics University of Oslo 1

Some corrections (1) hsiblings(x,y) :- child(x,parent), child(y,parent), X \== Y, child(x,parent1), child(y,parent2), Parent \== Parent2, Parent \== Parent2, Parent1 \== Parent2. hsiblings(x,y) :- child(x,parent), child(y,parent), X \== Y, child(x,parent1), child(y,parent2), Parent \== Parent1, Parent \== Parent2, Parent1 \== Parent2. 2

Some corrections (2) I said in the previous lecture No types in Prolog. I should have said Prolog does not use types in the way common prog. lang. do Indeed, there are some built-in types Very dependent on the implementation E.g., You might have functions for testing whether a given term is a floating point in some implementations but not in others (most implementations do not distinguish integers from reals) 3

Outline Prolog append revisited Arithmetic Reminder 1 Reminder 2 (Birger) 4

Append two lists Prolog program: append([], Ys, Ys). append([hd Tl], Ys, [Hd Zs]) :- append(tl, Ys, Zs). 5

Append two lists Take two lists Xs and Ys and append them into a third list Zs?- append([a, b, c], [d, e], Zs). Should give Zs = [a, b, c, d, e]. 6

Outline Prolog append revisited Arithmetic Reminder 1 Reminder 2 (Birger) 7

Arithmetic in Prolog Prolog programs presented so far were declarative: they admitted a dual reading as a formula Operations of arithmetic are functional, not relational Arithmetic compromises Prolog s declarativeness Solved in constraint logic programming languages 8

Arithmetic operators Built-in data structures: Integers: 1,2,3,... (+, -, *, //) Floating points: 2.3, 3.4456, 5.4e-13,... (+, -, *, /) Infix vs prefix notation* 45+35 + (45,35) It is possible to have user-defined operators with specified priority, associativity, etc *We will see later how to evaluate expressions 9

Arithmetic comparison relations Prolog allows comparison of ground arithmetic expressions (gae, i.e. expressions without variables) Built-in comparison relations: <, =<, =:= ( equal ), \== ( different ), >= and > Queries?- 6*3 =:= 9*2. yes?- 8 > 5+3. no?- 34>=X+4. uncaught exception: error(instantiation_error,(>=)/2 10

Example: ordered lists ordered([]). ordered([x]). ordered([x,y Ys]) :- X =< Y, ordered([y Ys]). Queries?- ordered([3,4,67,8]). no?- ordered([3,4,67,88]). yes 11

Evaluation of arithmetic expressions We need to introduce a way to evaluate expressions?- X=:=3+4.?- X=3+4. X = 3+4 yes yields an error Evaluation is done using is?- X is 3+4. X = 7 yes 12

Example: Factorial factorial(0,1). factorial(n,f) :- N>0, N1 is N-1, factorial(n1,f1), Queries?- factorial(5,x). X = 120 Yes F is N*F1. The following query gives, however, an error?- factorial(x,5). uncaught exception: error(instantiation_error,(>)/2) X>0 is not allowed! 13

Example: Length of lists An intuitive definition length([],0). length([_ Ts], N+1) :- length(ts,n). Query?- length([3,5,56,7],x). X = 0+1+1+1+1 Yes What s the problem? but wrong Expressions are not automatically evaluated in Prolog! 14

Example: Length of lists A good definition length([],0). length([_ Ts], N) :- length(ts,m), N is M+1. Queries?- length([3,5,56,7],x). X = 4 Yes?- length(x,5). X = [_,_,_,_,_] yes 15

Outline Prolog append revisited Arithmetic Reminder 1 Reminder 2 (Birger) 16

Paradigms: Overview Procedural/imperative Programming A program execution is regarded as a sequence of operations manipulating a set of registers (programmable calculator) Functional Programming A program is regarded as a mathematical function Object-Oriented Programming A program execution is regarded as a physical model simulating a real or imaginary part of the world Constraint-Oriented/Declarative (Logic) Programming A program is regarded as a set of equations 17

Many different languages Early languages Fortran, Cobol, APL,... Algol family Algol 60, Algol 68, Pascal,, PL/1, Clu, Ada, Modula, Cedar/Mesa,... Functional languages Lisp, FP, SASL, ML, Miranda, Haskell, Scheme, Setl,... Object-oriented languages Simula, Smalltalk, Self, Cecil, Modula-3, Eiffel, Sather, C++, Objective C,. Java 18

Languages are still evolving Object systems based on asynchronous calls Adoption of garbage collection Need of language support for Concurrency primitives; abstract view of concurrent systems Data-access; ex. XML Security Contracts; ex. in virtual organizations Domain-specific languages Aspect-oriented programming Concurrent and Distributed Systems (a lot to be done here!) Network programming 19

Algol family and ML (Mitchell s chapter 5) Evolution of Algol family Recursive functions and parameter passing Evolution of types and data structuring ML: Combination of Lisp and Algol-like features Expression-oriented Higher-order functions Garbage collection Abstract data types Module system Exceptions 20

Types (Mitchell s chapter 6) Types are important in modern languages Program organization and documentation Prevent program errors Provide important information to compiler Type inference Determine best type for an expression, based on known information about symbols in the expression Polymorphism Single algorithm (function) can have many types Overloading Symbol with multiple meanings, resolved at compile time 21

Control (Mitchell s chapter 8) Structured Programming goto considered harmful Exceptions in ML A different kind of entity than types Structured jumps that may return a value Dynamic scoping of exception handler Need to declare exceptions before use Pattern matching is used to determine the appropriate handler (C++ uses type matching) 22

Logic Prog. (Mitchell s chapter 15) N. Wirth Program = data structure + algorithm R. Kowalski Algorithm = Logic + Control In traditional programming Programmer takes care of both aspects In declarative programming The programmer only worries about the Logic The interpreter takes care of Control 23