Introduction (Part 3) Programming...

30
1 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Summer 2020 Programming Paradigms Introduction (Part 3)

Transcript of Introduction (Part 3) Programming...

Page 1: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

1

Prof. Dr. Michael Pradel

Software Lab, University of StuttgartSummer 2020

Programming ParadigmsIntroduction (Part 3)

Page 2: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

2

Overview

� Motivation� What the course is about� Why it is interesting� How it can help you

� Organization� Exercises� Grading

� Introduction to ProgrammingLanguages� History, paradigms, compilation, interpretation

Page 3: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

3 - 1

History: From Bits ...

First electronic computers: Programmedin machine language� Sequence of bits

� Example: Calculate greatest common divisor

Page 4: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

3 - 2

History: From Bits ...

First electronic computers: Programmedin machine language� Sequence of bits

� Example: Calculate greatest common divisor

Machine time more valuable thandeveloper time

Page 5: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

4 - 1

... over Assembly ...

Human-readable abbreviations formachine language instructions

� Less error-prone, but still very machine-centered

� Each new machine: Different assembly language

� Developer thinks in terms of low-level operations

Page 6: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

4 - 2

... over Assembly ...

Greatest common divisor in x86:

Page 7: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

5 - 1

... to High-level Languages

� 1950s: First high-level languages� Fortran, Lisp, Algol

� Developer thinks in mathematical andlogical abstractions

Page 8: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

5 - 2

... to High-level Languages

subroutine gcd_iter(value, u, v)integer, intent(out) :: valueinteger, intent(inout) :: u, vinteger :: t

do while( v /= 0 )t = uu = vv = mod(t, v)

enddovalue = abs(u)

end subroutine gcd_iter

Greatest common divisor in Fortran:

Page 9: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

6 - 1

Today: 1000s of Languages

� New languages gain traction regularly

� Some long-term survivors

� Fortran, Cobol, C

Page 10: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

6 - 2

Today: 1000s of Languages

� New languages gain traction regularly

� Some long-term survivors

� Fortran, Cobol, C

Poll (in Ilias):Your favorite programming language?

Page 11: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

7

What Makes a PL Successful?

� Expressive power� But: All PLs are Turing-complete

� Ease of learning (e.g., Basic, Python)� Open source� Standardization: Ensure portability

across platforms� Excellent compilers� Economics

� E.g., C# by Microsoft, Objective-C by Apple

Page 12: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

8

PL Spectrum

� Broad classification� Declarative (”what to compute”):

E.g., Haskell, SQL, spreadsheets

� Imperative (”how to compute it”):E.g., C, Java, Perl

� Various PL paradigms:

� Most languages combine multipleparadigms

Functional

Logic

Statically typedDynamically typed

Sequential

Shared-memoryparallel

Distributed-memoryparallel

Dataflow

Page 13: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

9 - 1

Example: Imperative PL

C implementation for GCD:

int gcd(int a, int b) {

while (a != b) {

if (a > b) a = a - b;

else b = b - a;

}

return a;

}

Page 14: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

9 - 2

Example: Imperative PL

C implementation for GCD:

int gcd(int a, int b) {

while (a != b) {

if (a > b) a = a - b;

else b = b - a;

}

return a;

}

Statements thatinfluence subsequentstatements

Page 15: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

9 - 3

Example: Imperative PL

C implementation for GCD:

int gcd(int a, int b) {

while (a != b) {

if (a > b) a = a - b;

else b = b - a;

}

return a;

}

Statements thatinfluence subsequentstatements

Assignments withside effect ofchanging memory

Page 16: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

10 - 1

Example: Functional PL

OCaml implementation of GCD

let rec gcd a b =

if a = b then a

else if a > b then gcd b (a - b)

else gcd a (b - a)

Page 17: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

10 - 2

Example: Functional PL

OCaml implementation of GCD

let rec gcd a b =

if a = b then a

else if a > b then gcd b (a - b)

else gcd a (b - a)

Recursive functionwith two arguments

Page 18: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

10 - 3

Example: Functional PL

OCaml implementation of GCD

let rec gcd a b =

if a = b then a

else if a > b then gcd b (a - b)

else gcd a (b - a)

Recursive functionwith two arguments

Focus onmathematicalrelationship betweeninputs and outputs

Page 19: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

11 - 1

Example: Logic PL

Prolog implementation of GCD

gcd(A,B,G) :- A = B, G = A.

gcd(A,B,G) :- A > B, C is A-B, gcd(C,B,G).

gcd(A,B,G) :- B > A, C is B-A, gcd(C,A,G).

Page 20: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

11 - 2

Example: Logic PL

Prolog implementation of GCD

gcd(A,B,G) :- A = B, G = A.

gcd(A,B,G) :- A > B, C is A-B, gcd(C,B,G).

gcd(A,B,G) :- B > A, C is B-A, gcd(C,A,G).

Facts and rules

Page 21: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

11 - 3

Example: Logic PL

Prolog implementation of GCD

gcd(A,B,G) :- A = B, G = A.

gcd(A,B,G) :- A > B, C is A-B, gcd(C,B,G).

gcd(A,B,G) :- B > A, C is B-A, gcd(C,A,G).

Facts and rules

Focus on logicalrelationshipsbetween variables

Page 22: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

12

Compilation and Interpretation

Different ways of executing a program

� Pure compilation

� Pure interpretation

� Mixing compilation and interpretation

� Virtual machines

� Just-in-time compilation

Page 23: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

1

Page 24: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

2

Page 25: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

3

Page 26: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

17

PL Design vs. Implementation

� Some PLs are easier to compile thanothers

� E.g., runtime code generation

� Code to execute: Unknown at compile time

� Hard to compile

� Easy to interpret

Page 27: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

19

Other Tools

� Linkers

� Preprocessors

� Source-to-source compilers

Page 28: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

4

Page 29: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

5

Page 30: Introduction (Part 3) Programming Paradigmssoftware-lab.org/teaching/summer2020/pp/slides_01_introduction_03... · Programming Paradigms Introduction (Part 3) 2 Overview Motivation

6