CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping...

40
CS 476 – Programming Language Design William Mansky

Transcript of CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping...

Page 1: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

CS 476 – Programming Language DesignWilliam Mansky

Page 2: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables

• Next target language: expressions + variables

Page 3: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

E ::= <#>

| E + E | E – E | E * E

| <bool>

| E and E | E or E

| not E

| E = E

| if E then E else E

Adding Variables: Syntax

Page 4: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

E ::= <#> | <ident>

| E + E | E – E | E * E

| <bool>

| E and E | E or E

| not E

| E = E

| if E then E else E

Adding Variables: Syntax

Example terms:

x + 5

if y then 3 else z

Page 5: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables

• How do variables get their values?

• Option 1: local binding― fun x -> x + 5― let x = 2 + 3 in if x = 5 then true else false

• Option 2: assignment― x = 3; y = x + 5;― x = 4; z = x + 5; // y != z

Page 6: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables

• Option 2: assignment― x = 3; y = x + 5;― x = 4; z = x + 5; // y != z

• Some terms now don’t just compute values: they have side effects that change the meaning of later terms

• We call terms that compute values expressions, and terms with side effects commands

Page 7: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

E ::= <#> | <ident>

| E + E | E – E | E * E

| <bool>

| E and E | E or E

| not E

| E = E

| if E then E else E

Adding Variables: Syntax

C ::= <ident> := E

| C; C

| skip

Example terms:

x := 3; y := x = 2; z := true && y

x := 0; x := x + 1; y := x; x := x + 1

Page 8: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables: Types

• How do we type variables?

(𝑥 is an identifier)

𝑥 ∶ ?

𝑒1 ∶ int 𝑒2 ∶ int

𝑒1 + 𝑒2 ∶ int

(𝑛 is a number)

𝑛 ∶ int

Page 9: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Typing Variables, Approach #1

• How do we type variables?

(𝑥 is an identifier)

𝑥 ∶ int

𝑒1 ∶ int 𝑒2 ∶ int

𝑒1 + 𝑒2 ∶ int

(𝑛 is a number)

𝑛 ∶ int

(𝑥 is an identifier)

𝑥 ∶ bool

Page 10: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Typing Variables, Approach #1

• How do we type variables?

(𝑥 is an identifier)

𝑥 ∶ 𝜏

𝑒1 ∶ int 𝑒2 ∶ int

𝑒1 + 𝑒2 ∶ int

(𝑛 is a number)

𝑛 ∶ int

Page 11: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

• How do we type variables?

T ::= INT | BOOL

• Each tag has its own namespace

• add INT/BOOL tags in preprocessing

Typing Variables, Approach #2

𝑥 ∶ INT ∶ int

E ::= <#> | (<ident> : T)

| E + E | E – E | E * E

| <bool>

| E and E | E or E

| not E

| E = E

| if E then E else E

𝑥 ∶ BOOL ∶ bool

Page 12: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Typing Variables, Approach #3

• How do we type variables?

• Instead of putting tags in the syntax, we could store them separately, in a type context

• New typing judgment: Γ ⊢ 𝑒 ∶ 𝜏, “𝑒 has type 𝜏 in context Γ”

• Γ is a partial function from identifiers to types

Γ 𝑥 = 𝜏

Γ ⊢ 𝑥 ∶ 𝜏

Γ ⊢ 𝑒1 ∶ int Γ ⊢ 𝑒2 ∶ int

Γ ⊢ 𝑒1 + 𝑒2 ∶ int

(𝑛 is a number)

Γ ⊢ 𝑛 ∶ int

Page 13: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables: Types for Commands

• What types can a command have?

• Only one: “correct”, or “ok”

Γ ⊢ 𝑒 ∶ 𝜏 Γ 𝑥 = 𝜏

Γ ⊢ 𝑥 ≔ 𝑒 ∶ ok

Γ ⊢ 𝑐1 ∶ ok Γ ⊢ 𝑐2 ∶ ok

Γ ⊢ 𝑐1; 𝑐2 ∶ ok

Γ ⊢ skip ∶ ok

Page 14: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables: Semantics

• Our programs now have state!

x := 3 + 4;

y := x + 5;

b := if y = 12 then true else false;

x := 2;

z := x + 5;

c := b && true;

What are the values of the variables at this point?{b = true, x = 7, y = 12}

Page 15: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables: Semantics

• Our programs now have state!

• Small-step relation is 𝑡, 𝜎 → 𝑡′, 𝜎′ , where state 𝜎 is a map from variables to values

𝜎 𝑥 = 𝑣

𝑥 , 𝜎 → (𝑣, 𝜎)

𝑒1, 𝜎 → (𝑒1′ , 𝜎′)

𝑒1 + 𝑒2, 𝜎 → (𝑒1′ + 𝑒2, 𝜎

′)

(𝑣1 + 𝑣2 = 𝑣)

𝑣1 + 𝑣2, 𝜎 → (𝑣, 𝜎)

Page 16: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Textbook Notation Warning

• The Formal Semantics of Programming Languages, Winskel

• Where we write 𝑒 ⇓ 𝑣, Winskel writes 𝑒 → 𝑣

• Where we write 𝑒 → 𝑒′, Winskel writes 𝑒 →1 𝑒′

Page 17: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables: Semantics

• Our programs now have state!

• Small-step relation is 𝑡, 𝜎 → 𝑡′, 𝜎′ , where state 𝜎 is a map from variables to values

𝜎 𝑥 = 𝑣

𝑥 , 𝜎 → (𝑣, 𝜎)

𝑒1, 𝜎 → (𝑒1′ , 𝜎)

𝑒1 + 𝑒2, 𝜎 → (𝑒1′ + 𝑒2, 𝜎)

(𝑣1 + 𝑣2 = 𝑣)

𝑣1 + 𝑣2, 𝜎 → (𝑣, 𝜎)

Page 18: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables: Semantics

• Our programs now have state!

• Small-step relation is 𝑡, 𝜎 → 𝑡′, 𝜎′ , where state 𝜎 is a map from variables to values

𝑒, 𝜎 → (𝑒′, 𝜎)

𝑥 ≔ 𝑒, 𝜎 → (𝑥 ≔ 𝑒′, 𝜎)

𝑥 ≔ 𝑣, 𝜎 → (skip, 𝜎 𝑥 ↦ 𝑣 )

𝑐1, 𝜎 → (𝑐1′ , 𝜎′)

𝑐1; 𝑐2, 𝜎 → (𝑐1′ ; 𝑐2, 𝜎

′)

skip; 𝑐2, 𝜎 → (𝑐2, 𝜎)

Page 19: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Structural Rule for Sequencing

• What if we had

x := 3; x := x + 1; x := 4

→ x := x + 1; x := 4, {x = 3}

→ x := x + 1, {x = 4}

𝑐2, 𝜎 → (𝑐2′ , 𝜎′)

𝑐1; 𝑐2, 𝜎 → (𝑐1; 𝑐2′ , 𝜎′)

Page 20: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables: Semantics

• What does an expression evaluate to?

• What does a command evaluate to?

𝜎 𝑥 = 𝑣

𝑥, 𝜎 ⇓ 𝑣

𝑒1, 𝜎 ⇓ 𝑣1 𝑒2, 𝜎 ⇓ 𝑣2 (𝑣1 + 𝑣2 = 𝑣)

𝑒1 + 𝑒2, 𝜎 ⇓ 𝑣

𝑒, 𝜎 ⇓ 𝑣

𝑐, 𝜎 ⇓ 𝜎′

𝑒, 𝜎 ⇓ 𝑣

𝑥 ≔ 𝑒, 𝜎 ⇓ 𝜎[𝑥 ↦ 𝑣]

𝑐1, 𝜎 ⇓ 𝜎′ 𝑐2, 𝜎′ ⇓ 𝜎′′

𝑐1; 𝑐2, 𝜎 ⇓ 𝜎′′

skip, 𝜎 ⇓ 𝜎

Page 21: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables: Hybrid Semantics

• Expressions don’t change the state, commands do

• So we might only care about intermediate steps for commands

𝜎 𝑥 = 𝑣

𝑥, 𝜎 ⇓ 𝑣

𝑒1, 𝜎 ⇓ 𝑣1 𝑒2, 𝜎 ⇓ 𝑣2 (𝑣1 + 𝑣2 = 𝑣)

𝑒1 + 𝑒2, 𝜎 ⇓ 𝑣

𝑒, 𝜎 ⇓ 𝑣

𝑥 ≔ 𝑒, 𝜎 → (skip, 𝜎 𝑥 ↦ 𝑣 )

𝑐1, 𝜎 → (𝑐1′ , 𝜎′)

𝑐1; 𝑐2, 𝜎 → (𝑐1′ ; 𝑐2, 𝜎

′)

skip; 𝑐2, 𝜎 → (𝑐2, 𝜎′)

Page 22: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables: Interpreter

• Follow the big-step semantics

let rec eval_exp (e : exp) : value option =

match e with

| Id x ->

| Add (e1, e2) ->

| …

Page 23: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables: Interpreter

• Follow the big-step semantics

type state = string -> value option

let rec eval_exp (e : exp) (s : state) : value option =

match e with

| Id x ->

| Add (e1, e2) ->

| …

𝜎 𝑥 = 𝑣

𝑥, 𝜎 ⇓ 𝑣

Page 24: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables: Interpreter

• Follow the big-step semantics

type state = string -> value option

let rec eval_exp (e : exp) (s : state) : value option =

match e with

| Id x -> s x

| Add (e1, e2) ->

| …

𝜎 𝑥 = 𝑣

𝑥, 𝜎 ⇓ 𝑣

Page 25: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables: Interpreter

• Follow the big-step semantics

type state = string -> value option

let rec eval_exp (e : exp) (s : state) : value option =

match e with

| Add (e1, e2) ->

(match eval_exp e1 s, eval_exp e2 s with

| Some (IntV i1), Some (IntV i2) -> Some (IntV (i1 + i2))

| _, _ -> None)

𝑒1, 𝜎 ⇓ 𝑣1 𝑒2, 𝜎 ⇓ 𝑣2 (𝑣1 + 𝑣2 = 𝑣)

𝑒1 + 𝑒2, 𝜎 ⇓ 𝑣

Page 26: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables: Interpreter

let update (s : state) (x : string) (v : value) : state =

fun y -> if y = x then Some v else s y

let rec eval_cmd (c : cmd) (s : state) : state option =

match c with

| Assign (x, e) ->

| Seq (c1, c2) ->

| Skip ->

𝑒, 𝜎 ⇓ 𝑣

𝑥 ≔ 𝑒, 𝜎 ⇓ 𝜎[𝑥 ↦ 𝑣]

Page 27: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables: Interpreter

let rec eval_cmd (c : cmd) (s : state) : state option =

match c with

| Assign (x, e) ->

(match eval_exp e s with

| Some v -> Some (update s x v)

| None -> None)

| Seq (c1, c2) ->

| Skip ->

𝑒, 𝜎 ⇓ 𝑣

𝑥 ≔ 𝑒, 𝜎 ⇓ 𝜎[𝑥 ↦ 𝑣]

Page 28: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables: Interpreter

let rec eval_cmd (c : cmd) (s : state) : state option =

match c with

| …

| Seq (c1, c2) ->

| Skip ->

𝑐1, 𝜎 ⇓ 𝜎′ 𝑐2, 𝜎′ ⇓ 𝜎′′

𝑐1; 𝑐2, 𝜎 ⇓ 𝜎′′

Page 29: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables: Interpreter

let rec eval_cmd (c : cmd) (s : state) : state option =

match c with

| …

| Seq (c1, c2) ->

(match eval_cmd c1 s with

| Some s’ -> eval_cmd c2 s’

| None -> None)

| Skip ->

𝑐1, 𝜎 ⇓ 𝜎′ 𝑐2, 𝜎′ ⇓ 𝜎′′

𝑐1; 𝑐2, 𝜎 ⇓ 𝜎′′

Page 30: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables: Interpreter

let rec eval_cmd (c : cmd) (s : state) : state option =

match c with

| …

| Seq (c1, c2) ->

(match eval_cmd c1 s with

| Some s’ -> eval_cmd c2 s’

| None -> None)

| Skip -> Some s skip, 𝜎 ⇓ 𝜎

Page 31: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables: Interpreter

let empty_state = fun x -> None;;

eval_cmd (Seq (Assign (“x”, Int 2),

Assign (“y”, Add (Ident “x”, Int 3)))) empty_state;;

Page 32: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables: Interpreter

eval_cmd (Seq (Assign (“x”, Int 2),

Assign (“y”, Add (Ident “x”, Int 3)))) empty_state;;

let rec eval_cmd (c : cmd) (s : state) : state option =

match c with

| Seq (c1, c2) ->

(match eval_cmd c1 s with

| Some s’ -> eval_cmd c2 s’

| None -> None)

Page 33: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables: Interpreter

eval_cmd (Seq (Assign (“x”, Int 2),

Assign (“y”, Add (Ident “x”, Int 3)))) empty_state;;

let rec eval_cmd (c : cmd) (s : state) : state option =

match c with

| Seq (c1, c2) ->

(match eval_cmd (Assign (“x”, 2)) empty_state with

| Some s’ -> eval_cmd (Assign (“y”, Add (Ident “x”, Int 3))) s’

| None -> None)

Page 34: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables: Interpreter

eval_cmd (Seq (Assign (“x”, Int 2),

Assign (“y”, Add (Ident “x”, Int 3)))) empty_state;;

match eval_cmd (Assign (“x”, 2)) empty_state with

| Some s’ -> eval_cmd (Assign (“y”, Add (Ident “x”, Int 3))) s’

| Assign (x, e) ->

(match eval_exp e s with

| Some v -> Some (update s x v)

Page 35: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables: Interpreter

eval_cmd (Seq (Assign (“x”, Int 2),

Assign (“y”, Add (Ident “x”, Int 3)))) empty_state;;

match eval_cmd (Assign (“x”, 2)) empty_state with

| Some s’ -> eval_cmd (Assign (“y”, Add (Ident “x”, Int 3))) s’

| Assign (x, e) ->

(match eval_exp (Int 2) empty_state with

| Some v -> Some (update empty_state “x” v)

Page 36: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables: Interpreter

eval_cmd (Seq (Assign (“x”, Int 2),

Assign (“y”, Add (Ident “x”, Int 3)))) empty_state;;

match eval_cmd (Assign (“x”, 2)) empty_state with

| Some s’ -> eval_cmd (Assign (“y”, Add (Ident “x”, Int 3))) s’

| Assign (x, e) ->

(match Some (IntV 2) with

| Some v -> Some (update empty_state “x” v)

Page 37: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables: Interpreter

eval_cmd (Seq (Assign (“x”, Int 2),

Assign (“y”, Add (Ident “x”, Int 3)))) empty_state;;

match Some {“x” = IntV 2} with

| Some s’ -> eval_cmd (Assign (“y”, Add (Ident “x”, Int 3))) s’

| Assign (x, e) ->

(match Some (IntV 2) with

| Some v -> Some (update empty_state “x” v)

Page 38: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables: Interpreter

eval_cmd (Seq (Assign (“x”, Int 2),

Assign (“y”, Add (Ident “x”, Int 3)))) empty_state;;

eval_cmd (Assign (“y”, Add (Ident “x”, Int 3))) {“x” = IntV 2}

| Assign (x, e) ->

(match eval_exp (Add (Ident “x”, Int 3)) {“x” = IntV 2} with

| Some v -> Some (update {“x” = IntV 2} “y” v)

Page 39: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables: Interpreter

eval_cmd (Seq (Assign (“x”, Int 2),

Assign (“y”, Add (Ident “x”, Int 3)))) empty_state;;

eval_cmd (Assign (“y”, Add (Ident “x”, Int 3))) {“x” = IntV 2}

| Assign (x, e) ->

(match Some (IntV 5) with

| Some v -> Some (update {“x” = IntV 2} “y” v)

Page 40: CS 476 – Programming Language Designmansky/teaching/cs476/fa18/lectures/imperative.pdfTyping Variables, Approach #3 •How do we type variables? •Instead of putting tags in the

Adding Variables: Interpreter

eval_cmd (Seq (Assign (“x”, Int 2),

Assign (“y”, Add (Ident “x”, Int 3)))) empty_state;;

Some {“x” = IntV 2, “y” = IntV 5}

match eval_cmd (Seq (Assign (“x”, Int 2),

Assign (“y”, Add (Ident “x”, Int 3)))) empty_state

with | Some res -> res “y” | None -> None;;

- : value option = Some (IntV 5)