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

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Advertisements

Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
Compiler Construction
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
6.001 SICP SICP – September Processes, Substitution, Recusion, and Iteration Trevor Darrell 32-D web page:
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)
1 Functional languages (e.g. Scheme, ML) Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
1 Extended Introduction to Computer Science 2 Administration סגל הקורס: –מרצים: ד"ר דניאל דויטש, איל כהן –מתרגלת:לבנת ג'רבי –בודק: ינון פלד Book: Structure.
ISBN Chapter 15 Functional Programming Languages.
PPL Static Verification: Type Inference Lecture Notes: Chapter 2.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
Principles of Programming Languages Lecture 1 Slides by Daniel Deutch, based on lecture notes by Prof. Mira Balaban.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
Albert Gatt LIN3021 Formal Semantics Lecture 4. In this lecture Compositionality in Natural Langauge revisited: The role of types The typed lambda calculus.
PPL Applicative and Normal Form Verification, Type Checking and Inference.
ISBN Chapter 15 Functional Programming Languages.
Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
What is the main focus of this course? This course is about Computer Science Geometry was once equally misunderstood. Term comes from ghia & metra or earth.
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1 Programming Languages (CS 550) Lecture 4 Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
CMSC 330: Organization of Programming Languages Operational Semantics.
Functional Programming. Some Functional Languages Lisp Scheme - a dialect of Lisp Haskell Miranda.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
Operational Semantics of Scheme
Functional Programming
Functional Programming Languages
Functional Programming
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Introduction to Scheme
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Env. Model Implementation
Mini Language Interpreter Programming Languages (CS 550)
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
The Metacircular Evaluator
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018.
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
Extended Introduction to Computer Science
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture 12: Message passing The Environment Model
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
6.001 SICP Variations on a Scheme
Lecture 2 מבוא מורחב.
6.001 SICP Interpretation Parts of an interpreter
Assignments and Procs w/Params
Introduction to the Lab
topics interpreters meta-linguistic abstraction eval and apply
Lecture 2 מבוא מורחב.
Presentation transcript:

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

4 ٤ IV100

“hello” +

So What have We Seen? Syntax Values Types Operators

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.

More topics Types – Type Inference and Type Checking – Static and Dynamic Typing Different Semantics (e.g. Operational) Interpreters vs. Compilers – Lazy and applicative evaluation

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

Administrative Issues Websites prefix – Course: /~ppl152 – My (for presentations): /~yarongon Weeks: 14 Exercises: 6 Mid-term Exam Grade

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)

“Wax on, wax off”

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)

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)

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…

The Power of Abstraction

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.)

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)

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 score23 total25 percentage92 Name Value

18 Expressions: Language Elements Means of Abstraction (define score 23) Associates score with 23 in environment table Syntax Semantics Means of Combination (composites) ( ) Application of proc to arguments Result = 25 Primitives 23 + * #t, #f 23 Primitive Proc (add) Primitive Proc (mult) Boolean

19 Computing in Scheme > > ( ) 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

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

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

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)

23 Using Evaluation Rules > (define score 23) > (* (+ 5 6 ) (- score (* ))) Special Form (second sub- expression is not evaluated) * *

24 Abstraction – Compound Procedures How does one describe procedures? (lambda (x) (* x x)) To process something multiply it by itself formal parametersbody Internal representation Special form – creates a “procedure object” (also called closure) and returns it as a “value” Proc (x) (* x x)

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?

26 Syntactic Sugar for naming procedures (define square (lambda (x) (* x x)) (define (square x) (* x x)) Instead of writing: We can write:

27 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 ==> ((lambda(x)(* x x)) 5) Proc(x)(* x x) 5 (* 5 5) 25

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

29 Using Abstractions > (square 3) 9 > (+ (square 3) (square 4)) > (define square (lambda(x)(* x x))) (* 3 3) (* 4 4) Environment Table NameValue squareProc (x)(* x x)

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

31 Lets not forget The Environment > (define x 8) > (+ x 1) 9 > (define x 5) > (+ x 1) 6 The value of (+ x 1) depends on the environment!

32 Using the substitution model (define square (lambda (x) (* x x))) (define average (lambda (x y) (/ (+ x y) 2))) (average 5 (square 3)) (average 5 (* 3 3)) (average 5 9)first evaluate operands, then substitute (/ (+ 5 9) 2) (/ 14 2)if operator is a primitive procedure, 7replace by result of operation

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

34 Values and types Values have types. For example: In scheme almost every expression has a value Examples: 1)The value of 23 is 23 2)The value of + is a primitive procedure for addition 3)The value of (lambda (x) (* x x)) is the compound procedure proc(x) (* x x) (also denoted 1)The type of 23 is numeral 2)The type of + is a primitive procedure 3)The type of proc (x) (* x x) is a compound procedure 4)The type of (> x 1) is a boolean (or logical)

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

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!

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 ]

38 More examples > (define x 8) Name Value Environment Table 8x > (define x (* x 2)) > x 16 > (define x y) reference to undefined identifier: y > (define + -) # + > (+ 2 2) 0 Bad practice, disalowed by some interpreters

39 The IF special form ERROR2 (if ) If the value of is #t, Evaluate and return it Otherwise Evaluate and return it (if ( 2 (if (

40 IF is a special form In a general form, we first evaluate all arguments and then apply the function (if ) is different: determines whether we evaluate or. We evaluate only one of them !

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

cond is a Special Form (cond (... )... (else... ))

Expressions: Summary Atomic Primitives – Numbers – Booleans – Procedures Non-primitives – Variables – Special operators symbols Composite Specials forms – define, lambda, if, cond Forms

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)

Symbol Type > (quote a) a > ’a a > (define a ’a) > a a > (define b a) > b a > (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

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++)

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..).

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

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