48
Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

Embed Size (px)

Citation preview

Page 1: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

Principles of Programming Languages

Lecture 1

Slides by Yaron Gonen, based on slides by Daniel Deutch andlecture notes by Prof. Mira Balaban

Page 2: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

4

٤ IV 100

Page 3: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

4 + 5

4 + “hello”

+

Page 4: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

So What have We Seen?

• Syntax• Values• Types• Operators

Page 5: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

Introduction• We will study Computational Processes• Design Principles

– Modularity, abstraction, contracts…• Programming Languages Paradigms

– Functional Programming• E.g. Racket, Scheme, ML, JavaScript (partially), Java (Since Java 8)• Functions are first-class objects

– Logic Programming• E.g. Prolog• “Declarative Programming”

– Imperative Programming• E.g. C, Java• Focuses on change of state

– Not always a crisp distinction – for instance scheme can be used for imperative programming.

Page 6: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

More topics

• Types– Type Inference and Type Checking– Static and Dynamic Typing

• Different Semantics (e.g. Operational)

• Interpreters vs. Compilers– Lazy and applicative evaluation

Page 7: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

Languages that will be studied

• Racket (dialect of Scheme)– Dynamically Typed– Functions are first-class citizens– Simple (though LOTS of parenthesis ) – Allows to show different programming styles

• Prolog– Declarative, Logic programming language

• Languages are important, but we will focus on the principles

Page 8: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

Administrative Issues

• Websites prefix http://www.cs.bgu.ac.il/– Course: /~ppl152– My (for presentations): /~yarongon

• Weeks: 14• Exercises: 6• Mid-term• Exam • Grade

Page 9: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

Use of Slides

• Slides are teaching-aids, i.e. by nature incomplete

• Compulsory material:– Lecture notes (see course website)– Everything taught in class and practical sessions– Compulsory reading if mentioned

• Will be on my site (I’ll try as early as possible)

Page 10: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

“Wax on, wax off”

Page 11: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

Functional Programming

• What is a non-functional (imperative) programming?– Imperative computation is a sequence of states

(remember automata?)– Statements or side effects can modify the state

(e.g. changing value of variable, display output)

Page 12: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

Functional Programming

• Expressions (no statements)• No State (no mutable data)• No side-effects• Has variables, but denote values (no location)• We learn as we go

This is not a course in functional programming!! (for that you have APL)

Page 13: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

Why Functional Programming?

• Small paradigm (but powerful)• Excellent for learning PPL• Is making a comeback in recent years (Java 8,

Javascript, NodeJS, MapReduce…)• There’s FUN in Functional Programming…

Page 14: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

The Power of Abstraction

Page 15: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

15

Racket (Scheme)

• LISP = LISt Processing– Invented in 1959 by John McCarthy– Scheme is a dialect of LISP – invented by Gerry

Sussman and Guy Steele– Racket is a dialect of Scheme– Small and powerful– Technical stuff in PS and in course website (how

to install etc.)

Page 16: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

A Word about Values

• Value is an abstract concept– User typed “4” get the value 4– Computer calculated the value 4 need to display

to user (toString)

Page 17: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

17

The Racket Interpreter

• The Read/Evaluate/Print Loop– Read an expression– Compute its value– Print the result

– Repeat the above• The Global Environment

– Function from names to values

score 23

total 25

percentage 92

Name Value

Page 18: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

18

Expressions: Language Elements

Means of Abstraction

(define score 23) Associates score with 23 in environment table

Syntax Semantics

Means of Combination(composites)

(+ 3 17 5) Application of proc to arguments Result = 25

Primitives 23+*#t, #f

23

Primitive Proc (add)Primitive Proc (mult)Boolean

Page 19: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

19

Computing in Scheme

> 23

23

> (+ 3 17 5)

25

> (+ 3 (* 5 6) 8 2)

43

> (define score 23)

Name Value

Environment Table

23score

Opening parenthesis

Expression whose value is a procedure

Other expressions

Closing parenthesis

Page 20: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

20

Computing in Scheme

> score

23

> (define total 25)

> (* 100 (/ score total))

92

> (define percentage (* 100 (/ score total))

Name Value

Environment

23score

25total

92percentage

Atomic (can’t decompose) but not primitive

A name-value pair in the env. is called binding

Page 21: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

21

Evaluation of Expressions• The value of a numeral: number• The value of a boolean: true or false• The value of a built-in operator (primitive

procedure): machine instructions to execute• The value of any name: the associated value in

the environment

Page 22: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

22

Evaluation of Expressions• To Evaluate a combination: (as opposed to

special form)– Evaluate all of the sub-expressions in some order– Apply the procedure that is the value of the

leftmost sub-expression to the arguments (the values of the other sub-expressions)

Page 23: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

23

Using Evaluation Rules

> (define score 23)

> (* (+ 5 6 ) (- score (* 2 3 2 )))

Special Form (second sub-expression is not evaluated)

* + 5 6

11

- 23 * 3 22

12

11

121

Page 24: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

24

Abstraction – Compound Procedures

How does one describe procedures?

(lambda (x) (* x x))

To process something multiply it by itself

formal parameters

body

Internal representation

• Special form – creates a “procedure object” (also called closure) and returns it as a “value”

Proc (x) (* x x)

Page 25: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

25

More on lambdas

• The use of the word “lambda” is taken from lambda calculus.

• A lambda body can consist of a sequence of expressions

• The value returned is the value of the last one

• So why have multiple expressions at all?

Page 26: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

26

Syntactic Sugar for naming procedures

(define square (lambda (x) (* x x))

(define (square x) (* x x) )

Instead of writing:

We can write:

Page 27: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

27

Evaluation of An ExpressionTo Apply a compound procedure: (to a list of arguments) Evaluate the body of the procedure with the formal parameters

replaced by the corresponding actual values

(( >==lambda(x)(* x x) )5)

Proc(x)(* x x) 5

*( 5 5)

25

Page 28: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

Evaluation of An Expression

• To Apply a compound procedure: (to a list of arguments)– Evaluate the body of the procedure with the

formal parameters replaced by the corresponding actual values

28

Page 29: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

29

Using Abstractions

> (square 3)

9

> (+ (square 3) (square 4))

> (define square (lambda(x)(* x x)))

(* 3 3) (* 4 4)

9 16+

25

Environment Table

Name Value

square Proc (x)(* x x)

Page 30: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

30

Yet More Abstractions

> (define f (lambda(a) (sum-of-two-squares (+ a 3) (* a 3))))

> (sum-of-two-squares 3 4)

25

> (define sum-of-two-squares (lambda(x y)(+ (square x) (square y))))

Try it out…compute (f 3) on your own

Page 31: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

31

Lets not forget The Environment

> (define x 8)

> (+ x 1)

9

> (define x 5)

> (+ x 1)

6The value of (+ x 1) depends on the environment!

Page 32: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

33

Booleans

Two distinguished values denoted by the constants

#t and #f

The type of these values is boolean

> (< 2 3)

#t

> (< 4 3)

#f

Page 33: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

34

Values and types

Values have types. For example:

In scheme almost every expression has a value

Examples:

1) The value of 23 is 232) The value of + is a primitive procedure for addition3) The value of (lambda (x) (* x x)) is the compound

procedure proc(x) (* x x) (also denoted <Closure (x) (* x x)>

1) The type of 23 is numeral 2) The type of + is a primitive procedure3) The type of proc (x) (* x x) is a compound procedure4) The type of (> x 1) is a boolean (or logical)

Page 34: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

Atomic and Compound Types

• Atomic types– Numbers, Booleans, Symbols (TBD)

• Composite types– Types composed of other types– So far: only procedures– We will see others later

Page 35: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

36

No Value?• In scheme most expressions have values• Not all! Those that don’t usually have side effects

Example : what is the value of the expression(define x 8)And of (display x) [display is a primitive function that prints the value of its argument to the screen]

• In scheme, the value of a define, display expression is “undefined”.

Never write code that relies on such value!

Page 36: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

Dynamic Typing

• Note that we never specify explicitly types of variables

• However primitive functions expect values of a certain type!– E.g. “+” expects numeral values

• So will our procedures (To be discussed soon)• The Scheme interpreter checks type correctness

at run-time: dynamic typing– [As opposed to static typing verified by a compiler ]

Page 37: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

38

More examples

> (define x 8)

Name Value

Environment Table

8x

> (define x (* x 2))

> x

16 16

> (define x y)

reference to undefined identifier: y

> (define + -)

>-<#+

> (+ 2 2)

0

Bad practice, disalowed by some interpreters

Page 38: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

39

The IF special form

ERROR2

(if <predicate> <consequent> <alternative)>

If the value of <predicate> is #t,

Evaluate <consequent> and return it

Otherwise

Evaluate <alternative> and return it

(if (< 2 3) 2 3) ==> 2

(if (< 2 3) 2 (/ 1 0)) ==>

Page 39: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

40

IF is a special form

• In a general form, we first evaluate all arguments and then apply the function

• (if <predicate> <consequent> <alternative>) is different:

<predicate> determines whether we evaluate <consequent> or <alternative>.

We evaluate only one of them !

Page 40: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

Condition

(lambda (a b) (cond ( (> a b) a) ( (< a b) b) (else -1 )))

Page 41: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

cond is a Special Form

(cond (<p1> <e11> ... <e1k1>)

(<p2> <e21> ... <e2k2>)

...(else <en1> ... <enkn>))

Page 42: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

Expressions: Summary

Atomic• Primitives

– Numbers– Booleans– Procedures

• Non-primitives– Variables– Special operators symbols

Composite• Specials forms

– define, lambda, if, cond

• Forms

Page 43: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

Evaluation: Summary

Atomic• Number• Boolean• Built-in Primitive• Variable

Composite• Primitive operator• Operator is a procedure

(value of lambda)• Special form (define, if,

lambda, cond)

Page 44: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

Symbol Type> (quote a)a> ’aa> (define a ’a)> aa> (define b a)> ba> (eq? a b)#t> (symbol? a)#t> (define c 1)> (symbol? c)#f> (number? c)#t

Symbols are atomic types, their values

unbreakable:‘abc is just a symbol

Primitive procedure that compares two values

Primitive procedure that checks if the value is of type symbol

Page 45: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

More on Types

• A procedure type is a composite type, as it is composed of the types of its inputs (domain) and output (range)

• In fact, the procedure type can be instantiated with any type for domain and range, resulting in a different type for the procedure (=data)

• Such types are called polymorphic– Another polymorphic type: arrays of values of

type X (e.g. STL vectors in C++)

Page 46: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

Type constructor

• Defines a composite type out of other types• The type constructor for functions is denoted “->”• Example: [Number X Number –> Number]

is the type of all procedures that get as input two numbers, and return a number

• Note: there is nothing in the syntax for defining types! This is a convention we manually enforce (for now..).

Page 47: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

Scheme Type Grammar

Type --> ’Unit’ | Non-Unit [Unit=Void]Non-unit -> Atomic | Composite | Type-variableAtomic --> ’Number’ | ’Boolean’ | ’Symbol’Composite --> Procedure | UnionProcedure --> ’Unit ’->’ Type | ’[’ (Non-Unit ’*’)* Non-Unit ’->’ Type ’]’Union --> Type ’union’ TypeType-variable -> A symbol starting with an upper case letter

Page 48: Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban

Value constructor

• Means of defining an instance of a particular type.

• The value constructors for procedures is lambda– Each lambda expression generates a new

procedure