lecture_introduction
lecture_introduction
2
4
The Role of Programming
□
Programming: Essential form of
expression for a computer
scientist
”The limits of my language mean the limits of
my world.” (Ludwig Wittgenstein)
□
Programming languages determine
what algorithms and ideas you
can express
3
Goal of this Lecture
Understand how programming
languages (PLs) work
□
How are languages defined?
□
What language design choices
exist?
□
How are languages implemented?
4
Why Learn About PLs?
Enables you to
□
choose right PL for a specific purpose
□
choose among alternative ways to express things
□
make best use of tools (e.g., debuggers, IDEs,
analysis tools)
□
understand obscure language features
□
simulate useful features in languages that lack
them
5
Isn’t Knowing {Pick a PL} Enough?
□
Complex systems: Built in various
languages
E.g., Facebook: Wild mix of languages
covering various language paradigms
□
New languages arrive regularly (and
old ones fade away)
9-1
History: From Bits ...
First electronic computers: Programmed
in machine language
□
Sequence of bits
□
Example: Calculate greatest common divisor
21 - 1
History: From Bits ...
First electronic computers: Programmed
in machine language
□
Sequence of bits
□
Example: Calculate greatest common divisor
22 - 1
... over Assembly ...
Greatest common divisor in x86:
... to High-level Languages
□
1950s: First high-level languages
Fortran, Lisp, Algol
□
Developer thinks in mathematical and
logical abstractions
23 - 1
... to High-level Languages
Greatest common divisor in Fortran:
subroutine gcd_iter(value, u,
v) integer, intent(out) ::
value integer,
intent(inout) :: u, v
integer :: t
do while( v /= 0 )
t = u
u = v
v = mod(t,
v) enddo
value =
abs(u)
end subroutine
gcd_iter
23 - 2
Today: 1000s of Languages
□
New languages gain traction regularly
□
Some long-term survivors
Fortran, Cobol, C
24 - 1
Today: 1000s of Languages
□
New languages gain traction regularly
□
Some long-term survivors
Fortran, Cobol, C
24 - 2
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
15
25
Apple
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: Sequential
Statically typed Distributed-
Functional Shared-memory memory
Dynamically typed parallel
parallel
Logic Dataflow
□
Most languages combine multiple
16
26
paradigms
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;
}
27 - 1
Example: Imperative PL
C implementation for GCD:
int gcd(int a, int Statements that
b) { while (a != influence subsequent
b) {
statements
if (a > b) a = a -
b; else b = b - a;
}
return a;
}
27 - 2
Example: Imperative PL
C implementation for GCD:
int gcd(int a, int Statements that
b) { while (a != influence subsequent
b) {
statements
if (a > b) a = a -
b; else b = b - a;
}
} return a;
Assignments with
side effect of
changing memory
27 - 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)
28 - 1
Example: Functional PL
OCaml implementation of GCD
Recursive function
let rec gcd a b
= if a = b then with two arguments
a
else if a > b then gcd b (a
- b) else gcd a (b - a)
28 - 2
Example: Functional PL
OCaml implementation of GCD
Recursive function
let rec gcd a b
= if a = b then with two arguments
a
else if a > b then gcd b (a
- b) else gcd a (b - a)
Focus on mathematical
relationship between
inputs and outputs
28 - 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).
29 - 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).
29 - 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).
Focus on logical
relationships
between variables
29 - 3
Compilation and Interpretation
Virtual machines
Just-in-time compilation
26
30
PL Design vs. Implementation
□
Some PLs are easier to compile than
others
□
E.g., runtime code generation
Code to execute: Unknown at compile
time
Hard to compile
Easy to interpret
27
35
Other Tools
□
Linkers
□
Preprocessors
□
Source-to-source compilers
28
37