Lecture content. Course goals. Course Introduction. TDDA69 Data and Program Structure Introduction

Similar documents
Lecture content. Course goals. Course Introduction. TDDA69 Data and Program Structure Introduction

Lecture content. Course goals. Course Introduction. TDDA69 Data and Program Structure Introduction Cyrille Berger

List of lectures. Lecture content. Imperative Programming. TDDA69 Data and Program Structure Imperative Programming and Data Structures

61A Lecture 2. Wednesday, September 4, 2013

CS 360 Programming Languages Interpreters

61A Lecture 2. Friday, August 28, 2015

CS 314 Principles of Programming Languages

Programming Languages, Summary CSC419; Odelia Schwartz

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

CS558 Programming Languages

CS558 Programming Languages

Concepts of Programming Languages

CS 242. Fundamentals. Reading: See last slide

Com S 541. Programming Languages I

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

Functional Programming Lecture 1: Introduction

CSCI 3136 Principles of Programming Languages

CS 314 Principles of Programming Languages

Parsing Scheme (+ (* 2 3) 1) * 1

Introduction. A. Bellaachia Page: 1

Computer Components. Software{ User Programs. Operating System. Hardware

Seminar on Languages for Scientific Computing Aachen, 6 Feb Navid Abbaszadeh.

SOFTWARE ARCHITECTURE 6. LISP

Part (04) Introduction to Programming

Lecture 16: Static Semantics Overview 1

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

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Programmiersprachen (Programming Languages)

Functional programming in LISP

List of lectures. Lecture content. Symbolic Programming. TDDA69 Data and Program Structure Symbolic and Logic Programming Cyrille Berger

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Programming (Econometrics)

Introduction to Engineering Using Robotics Experiments. Dr. Yinong Chen

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

G Programming Languages - Fall 2012

A Brief Introduction to Common Lisp

Chapter 1. Fundamentals of Higher Order Programming

Semester Review CSC 301

Functional Languages. Hwansoo Han

LECTURE 18. Control Flow

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

Functional Programming. Pure Functional Programming

The role of semantic analysis in a compiler

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

LECTURE 16. Functional Programming

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

CS558 Programming Languages

Introduction to Functional Programming

Seminar in Programming Languages

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

Data Types. (with Examples In Haskell) COMP 524: Programming Languages Srinivas Krishnan March 22, 2011

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

BEAMJIT, a Maze of Twisty Little Traces

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

CMSC 330: Organization of Programming Languages

Programming language design and analysis

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

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

FP Foundations, Scheme

Chapter 7:: Data Types. Mid-Term Test. Mid-Term Test (cont.) Administrative Notes

Final-Term Papers Solved MCQS with Reference

Chapter 1. Preliminaries

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

Why are there so many programming languages?

Organization of Programming Languages CS3200/5200N. Lecture 11

CS558 Programming Languages

Comp 333: Concepts of Programming Languages Fall 2016

Extending Jython. with SIM, SPARQL and SQL

Programming (ERIM) Lecture 1: Introduction to programming paradigms and typing systems. Tommi Tervonen

Overview. Rationale Division of labour between script and C++ Choice of language(s) Interfacing to C++

G Programming Languages - Fall 2012

Functional Programming and Haskell

CS101 Introduction to Programming Languages and Compilers

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

Recursion. What is Recursion? Simple Example. Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem

MIDTERM EXAM (Solutions)

INTRODUCTION PRINCIPLES OF PROGRAMMING LANGUAGES. Norbert Zeh Winter Dalhousie University 1/10

Programming Paradigms and Languages Introduction. dr Robert Kowalczyk WMiI UŁ

SD314 Outils pour le Big Data

Introduction to the course and basic programming concepts

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

Functional Programming. Big Picture. Design of Programming Languages

Functional Programming Lecture 13: FP in the Real World

Principles of Compiler Construction ( )

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

More Lambda Calculus and Intro to Type Systems

Programming Language Pragmatics

Principles of Compiler Construction ( )

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

An Introduction to Scheme

CSCC24 Functional Programming Scheme Part 2

A Brief Introduction to Scheme (I)

CS 231 Data Structures and Algorithms, Fall 2016

Ruby: Introduction, Basics

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

Denotational Semantics. Domain Theory

CSCI 2041: Advanced Language Processing

Control in Sequential Languages

Transcription:

Lecture content TDDA69 Data and Program Structure Introduction Cyrille Berger Course Introduction to the different Programming Paradigm The different programming paradigm Why different paradigms? Introduction to Functional Expressions Functions Control Recursion Advantages and inconvenient of functional programming 2 / 66 Course goals Course Introduction Describe aspects of evaluation and execution in different language models Explain and demonstrate how design choices affect the expressiveness and efficacy of a programming language Analyze and value programming languages based on their evaluation and compilation strategies Implement programming languages in the form of an interpreter and a compiler 4

Why do you need to know how program are interpreted? It will help you understand why programming language works a certain way and what are the limits New programming languages and interpreters are constantly being developed Existing interpreters are constantly being developed to improve performance, security, add new features... Programming languages General purposes: C, C++, Java, Python... Special purposes: Prolog, Matlab, R, Agent0... Scripting: JavaScript, VBA... Historical: Fortran, Lisp... 5 6 Evolution of programming languages How is a program interpreted? Source code Parser Parser Abstract Syntax Tree Tree visitor Generator Source code... Bytecode Virtual Machine Assembler Assembly Operating System CPU 7 8

Facebook's work on the PHP intepreter Why did Facebook need to develop their own PHP interpreter? Facebook started with PHP in 2004 Back at the time, PHP was the gold standard for website programming and prototyping But this is causing problems and for practical reasons they cannot change programming language 9 10 What did Facebook do? The standard PHP interpreter is using Abstract Syntax Tree execution They developed a tool to convert PHP to C++ Then they developed a new interpreter that do Just-In-Time (JIT) compilation, called HHVM They introduced Hack, a variant of PHP with a typing system And other examples... Google with Java, Dalvik, Python with CPython vs Qt's JavaScript, switching from AST Interpretation to JIT and to a mix of JIT and AST Interpretation... 11 12

List of lectures 1 Introduction and Functional Programming 2 Imperative Programming and Data Structures 3 Environment 4 Evaluation 5 Object Oriented Programming 6 Macros and 7 Virtual Machines and Bytecode 8 Garbage Collection and Native Code 9 Distributed Computing 10 Logic 11 Guest lecture 12 Summary Book(s) Structure and Interpretation of Computer Programs by Hal Abelson, Jerry Sussman and Julie Sussman Structure and Interpretation of Computer Programs in Python by Hal Abelson, Jerry Sussman, Julie Sussman and John Denero 13 14 List of labs 1Functional Programming 2Supporting class for an interpreter 3ECMAScript Interpreter 4Macros 5Supporting class for a Virtual Machine 6Bytecode and Virtual Machine The labs are entirely new and using Python Division of time 24h lectures (in 12 sessions) 40h labs (in 20 sessions) 8h tutorials (in 4 88h homework 15 16

Introduction to the different Programming Paradigm The different programming paradigm Programming Paradigm (2/2) declarative Imperative functional Symbolic Logic Object-Oriented 19 20

Declarative Expresses logic of computation without control flow: What should be computed and not how it should be computed. Examples: XML/HTML, antlr4/yacc, make/ants... Imperative Express how computation are executed Describes computation in term of statements that change the internal state Examples: C/C++, Pascal, Java, Python, JavaScript... 21 22 Functional Computation are treated as mathematical function without changing any internal state Examples: Lisp, Scheme, Object-Oriented Based on the concept of objects, which are data structures containing fields and methods Programs are designed by making objects interact with each others Examples: C++, Java, C#, Python, Ruby, JavaScript... 23 24

Others paradigm languages Logic Based on Formal logic: expressing facts and rules Symbolic A program can manipulate its own formulas and components as if they are data Example: prolog Why different paradigms? 25 Can you do everything in imperative programming? Is there a paradigm to rule them all? In theory you can program everything in C/C++ and impertive programming But is that And is that 27 28

Functional vs Imperative Declarative vs Imperative Double all the numbers in an array var numbers = Imperative: var doubled = [] for(var i = 0; i < numbers.length; i++) var newnumber = numbers[i] * 2 doubled.push(newnumber) Functional: var doubled = numbers.map(function(n) return n * 2 ) Select all the dogs that belongs to a specific Declarative: SELECT * from dogs INNER JOIN owners WHERE dogs.owner_id = owners.id Imperative: var dogswithowners = [] var dog, owner for(var dog in dogs) for(var owner in owners) if (owner && dog.owner_id == owner.id) dogswithowners.push( dog: dog, owner: owner ) 29 30 Functional vs Imperative Imperative language (C/C++, Basic constructs are imperative Change existing values, states x = x + Functional language Basic constructs are Declare new values function f(x) return x + 1; Computations are primarily done by evaluating expressions Pure if all constructs are declarative Introduction to Functional Programming 31

Expressions (1/2) Expressions primitive expressions to capture the simplest elements we want to describe means of combining simple elements into coumpound ones means of abstracting elements by naming and manipulating them 34 Expressions (2/2) Primitive expression: numbers, arithmetic expressions... 2 plus 'hello' number operator string Call max ( 2, 3 ) operator operand operand Compound expressions max(min(pow(3, 5), -4), min(1, -2) * 2) + 6 Names Expressions can be named 2 plus 'hello' name a plus b name name name max ( 2, 3 ) name 35 36

Assignment Binds names to a := 2 Now a has the value 2 a plus 2 evaluates to 4 Question What is the value of the final expression in this sequence? >>> f = min >>> g = max >>> g, h = min, max >>> max = g >>> max(f(2, g(h(1,5), 3)), 4) www.govote.at enter code 42 13 08 37 38 What is a function? Functions Assignment is a simple means of abstraction: binds names to values Function definition is a more powerful means of abstraction: binds names to expressions 40

Function definition A function definition contains: A signature which defines how many arguments a function takes A body which defines the computation performed when the function is called function <name> ( parameters list ) return <return expression> ; Pure and non-pure function Pure functions: just return values: Math.abs(-2) -> Math.pow(2, 100) -> 1267650600228229401496703205376 Non-pure functions: have side effects: print(-2) -> But print '-2' in the A side effect is not a value, it is anything that happens as a consequence of calling a 41 42 What is a closure/lambda? Closure (or lambda) A closure is a function with no var doubled = numbers.map(function(n) return n * 2 ) This seem simple, but this is actually rather powerful! function create_function_multiplication(number) return function(x) return x * number; var doubled = numbers.map(create_function_multiplication(2)) 43 44

Closure in Python Python has limited support for closures: single statement: numbers.map(lambda v: v * 2) But support nested def create_function_multiplication(number): def function_addition(x): return x * number return function_addition doubled = numbers.map(create_function_multiplication(2)) Control 45 Control flow (1/2) In imperative programming, a control flow statement execution result in a choice between two paths Exemple: if, while... In functional programming, it is done using a special function. For instance, in Lisp: (COND (condition1 result1 ) (condition2 result2 )... (T resultn ) ) Control flow (2/2) The functional way in imperative languages: In JavaScript, C++, Java, Ruby...): condition? result1 : result2; In Python: condition if result1 else result2 47 48

What about loops? Loop constructs is imperative How would you implement the equivalent of a loop in functional? function factorial(n) var r = 1; for(var i = 2; i <= n; ++i) r *= i; return r; Recursion 49 What is recursion? A function is called recursive if the body of that function calls itself, either directly or indirectly. Factorial: the classical example (1/2) Factorial in factorial :: Integral -> Integral factorial 0 = 1 factorial n = n * factorial (n-1) Factorial in Common LISP: (define (factorial n) (cond ((= n 0) 1) (t (* n (factorial (- n 1)))))) 51 52

Factorial: the classical example (1/2) Recursion vs loops With a loop: function factorial(n) var r = 1; for(var i = 2; i <= n; ++i) r *= i; return r; With a recursive function factorial(n) return (n===0)? 1 : n * factorial(n-1) while(expression) do_something(); function loop_something(args...) if(expression) return; else do_something(); loop_something(args...); (define (loop_something args...) (cond (expression) value) (t (do_something) (loop_something args...)) 53 54 When to use recursion rather than iteration? (1/3) When to use recursion rather than iteration? (2/3) def factorial(n): if(n == 0): return 1 else: return n * factorial(n-1) factorial(10) -> 3628800 factorial(1000) -> RuntimeError: maximum recursion depth exceeded in comparison In most programming language, the number of function call is limited by the size of the stack sys.getrecursionlimit() -> 1000 sys.setrecursionlimit(1003) factorial(1000) -> 4023872600...00000 Tail-call optimisation Calling a function is usually more expensive than a loop 55 56

When to use recursion rather than iteration? (3/3) It is a matter of Recursion is a bit more general than loops When walking through a tree Advantages and inconvenient of functional programming 57 No side-effects pure functional In pure functional, calling a function only return a value The implication is that calling a function with the same arguments will always return the same value Is the withdraw function pure functional? (define balance 100) (define (withdraw amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds")) www.govote.at enter code 31 57 37 Verification and proving To prove a program correct, must consider everything a program depends on In pure functional programs, depdence on any data structure is explicit 59 60

Proving properties in functional programming Proving properties in imperative programming (define (power b n) (cond ((= n 0) 1) (t (* (power b (- n 1)))))) Claim: for any integer n 0 and any number b, (power b n) = bⁿ Proof: 1) Verify the base case: (power b 2) Assume that (power b (- n 1)) is correct 3) Verify that (power b n) is correct assuming that (power b (- n 1)) is correct function power(b, n) int result = 1; for(int i = 0; i < n; + +i) result *= b; return result; Devise a loop invariant: (n i) (result = bⁱ) Prove that it is true for the first loop iteration Prove that each loop iteration preserves it Assume that (n i) (result = bⁱ) Prove that (n j) (result = bʲ) with j = i + 61 62 Concurency Concurency is one of the current hot topic in programming The main challenge is data-race Imperative programs are very sensible to data-race because of states There is no data-race in pure functional languages all data is immutable all functions are pure, without side-effects Summary on the upside of functional programming The main advantage is no sideeffects Verification and proving Concurency Productivity? Ericsson claims an increase in productivity between 9 and 25 times when using their home-grown version of Erlang 63 64

The downside of functional programming My Key message about programming paradigms In practice, there is very limited need for proving a program Mostly in critical applications: rocket control, hospital... And how do you prove hardware? Be pragmatic, there is no one answer! Performance issues (remember function call are expensive) Very limited support Most programming tasks require 65 66