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