Presentation is loading. Please wait.

Presentation is loading. Please wait.

Organization of Programming Languages

Similar presentations


Presentation on theme: "Organization of Programming Languages"— Presentation transcript:

1 Organization of Programming Languages
(Midterm Review) Hongwei Xi 2/16/2019 Hongwei Xi

2 Inductive Reasoning Mathematical Induction
Base step: establishing P(0) Inductive Step: establishing that P(n) implies P(n+1) for every natural number n Conclusion: P(n) holds for every natural number n 2/16/2019 Hongwei Xi

3 Inductive Reasoning Inductive definition Rule based
Each rule has one conclusion and finitely many (possibly zero) premisses 2/16/2019 Hongwei Xi

4 Inductive Reasoning Structural Induction
Proving properties on inductively defined structures Inductive step: establishing P for a term t while assuming that P holds for each *immediate* substructure of t 2/16/2019 Hongwei Xi

5 UFPL An Untyped Functional Programming Language
Booleans b := true | false Integers i := | -2 | -1 | 0 |1 | 2 | . . . Expressions e := x | b | I | op (e1,…, en) | if e then e1 else e2 fi | <> | <e1, e2> | fst(e) | snd (e) | fun f(x) is e | app(e1, e2) | let x = e1 in e2 end 2/16/2019 Hongwei Xi

6 UFPL Free variables: FV(e) Bound variables: Substitution:
fun f(x) is e let x = e1 in e2 end Substitution: Protect bound variables from being substituted for Avoid capturing free variables 2/16/2019 Hongwei Xi

7 UFPL alpha-conversion
fun f(x) is e  fun f’(x’) is e[f->f’][x->x’] let x = e1 in e2 end  let x’ = e1 in e2[x->x’] end Values v := x | b | i | <> | <v, v> | fun f(x) is v 2/16/2019 Hongwei Xi

8 UFPL Dynamic Semantics Evaluation Semantics (big-step)
Presentation is straightforward Cannot distinguish non-termination from type errors Not suitable for compilation Reduction Semantics (small-step) Presentation is more involved Can distinguish non-termination from type errors Can lead to efficient implementation 2/16/2019 Hongwei Xi

9 UFPL: Evaluation Semantics
Evaluation Rules b | b i | i e1 | v1 … en | vn op (v1,…,vn) = v op (e1,…,en) | v 2/16/2019 Hongwei Xi

10 UFPL: Evaluation Semantics
Evaluation Rules e | true e1 | v if e then e1 else e2 fi | v e | false e2 | v if e then e1 else e2 fi | v 2/16/2019 Hongwei Xi

11 UFPL: Evaluation Semantics
Evaluation Rules e1 | v1 e2 | v <> | <> <e1, e2> | <v1, v2> e | <v1, v2> e | <v1, v2> fst(e) | v snd(e) | v2 2/16/2019 Hongwei Xi

12 UFPL: Evaluation Semantics
Evaluation Rules fun f(x) is e | fun f(x) is e e1 | fun f(x) is e e2 | v e[f -> fun f(x) is e, x -> v2] app(e1, e2) | v 2/16/2019 Hongwei Xi

13 UFPL: Evaluation Semantics
Evaluation Rules e1 | v1 e2[x -> v1] let x = e1 in e2 end | v 2/16/2019 Hongwei Xi

14 UFPL: Reduction Semantics
Redexes: op(v1,…,vn) -> v (v is the result of op(v1,…,vn)) if true then e1 else e2 fi -> e1 if false then e1 else e2 fi -> e2 fst(<v1, v2>) -> v1 snd(<v1, v2>) -> v2 2/16/2019 Hongwei Xi

15 UFPL: Reduction Semantics
Redexes: app(fun f(x) is e, v) -> e[f -> fun f(x) is e, x -> v] let x = v in e end -> e[x -> v] 2/16/2019 Hongwei Xi

16 UFPL: Reduction Semantics
Evaluation Contexts E := [] | op (v,…,v,E, e1,…,en) | if E then e else e | <E, e> | <v, E> | fst(E) | snd(E) | app(E, e) | app(v, E) | let x=E in e end e1 -> e2 if e1 = E[r] and e2 = E[e], where e is the reduction of r. 2/16/2019 Hongwei Xi

17 UFPL: Reduction Semantics
Splitting: (E, r) is a splitting of e if E[r] = e. A value cannot have a splitting Each expression can have at most one splitting Many-step reduction ->*: e | v if and only if e ->* v 2/16/2019 Hongwei Xi

18 TFPL Typed functional programming language
Types tau := bool | int | unit | tau * tau | tau -> tau Expressions e := … | fun f(x:tau):tau is e Type erasure: |e| is the result from erasing all the types in e 2/16/2019 Hongwei Xi

19 TFPL Dynamic Semantics
Types are needed to be carried around during evaluation and reduction Types are indifferent to evaluation and reduction: e | v if and only if |e| | |v| e ->* v if and only if |e| ->* |v| 2/16/2019 Hongwei Xi

20 TFPL: Static Semantics
Typing rules: Gamma |- b : bool Gamma |- i : int Gamma(x) = tau Gamma |- x : tau 2/16/2019 Hongwei Xi

21 TFPL: Static Semantics
Typing rules: Sigma(op) = tau1 * … * taun -> tau Gamma |- e1: tau1 … Gamma |- en : taun Gamma |- op(e1,…,en) : tau Gamma |- <> : 1 2/16/2019 Hongwei Xi

22 TFPL: Static Semantics
Typing rules: Gamma |- e: bool Gamma |- e1: tau Gamma |- e2: tau Gamma |- if e then e1 else e2 fi: tau Gamma |- e1: tau Gamma |- e2: tau Gamma |- <e1, e2>: tau1 * tau2 2/16/2019 Hongwei Xi

23 TFPL: Static Semantics
Typing rules: Gamma |- e: tau1 * tau Gamma |- fst(e): tau1 Gamma |- e: tau1 * tau Gamma |- snd(e): tau2 2/16/2019 Hongwei Xi

24 TFPL: Static Semantics
Typing rules: Gamma, f:tau1 -> tau2, x: tau1 |- e: tau Gamma |- fun f(x) is e: tau1 -> tau2 Gamma |- e1: tau1 -> tau2 Gamma |- e2: tau Gamma |- app(e1, e2): tau2 2/16/2019 Hongwei Xi

25 TFPL: Static Semantics
Typing rules: Gamma |- e1: tau1 Gamma, x: tau1 |- e2: tau Gamma |- let x = e1 in e2 end: tau2 2/16/2019 Hongwei Xi

26 TFPL Subject Reduction: If Gamma |- e1: tau is derivable and e1 -> e2 holds, then Gamma |- e2: tau is also derivable. Type Preservation: If Gamma |- e: tau is derivable and e | v holds, then Gamma |- v: tau is also derivable. 2/16/2019 Hongwei Xi

27 TFPL Progress Theorem: Suppose that |- e: tau is derivable. Then e is either a value or e -> e’ holds for some e’. Milner’s Slogan: Well-typed Programs can never go wrong! 2/16/2019 Hongwei Xi


Download ppt "Organization of Programming Languages"

Similar presentations


Ads by Google