Dynamically Typed Programming Languages Part 2: Dynamic PCF Jim - - PowerPoint PPT Presentation

dynamically typed programming languages
SMART_READER_LITE
LIVE PREVIEW

Dynamically Typed Programming Languages Part 2: Dynamic PCF Jim - - PowerPoint PPT Presentation

Dynamically Typed Programming Languages Part 2: Dynamic PCF Jim Royer CIS 352 April 16, 2019 Reference Practical Foundations for Programming Languages, 1/e, Part VI: Dynamic Types , by Robert Harper, Cambridge University Press, 2013,


slide-1
SLIDE 1

Dynamically Typed Programming Languages

Part 2: Dynamic PCF Jim Royer

CIS 352

April 16, 2019

slide-2
SLIDE 2

Reference

Practical Foundations for Programming Languages, 1/e, “Part VI: Dynamic Types”, by Robert Harper, Cambridge University Press, 2013, pages 127–148.

slide-3
SLIDE 3

(Ordinary) PCF

PCF (Programming Computable Functions) Plotkin 1977 A theoretical cousin of ML and Haskell (and close to LFP+). PCF Raw Syntax

Expr ::= X | λX.Expr | Expr(Expr) | N | zero | succ(Expr) | fix X is Expr | ifz Expr {zero ⇒ Expr | succ(X) ⇒ Expr }

Note: For fix, think rec. PCF Types: Type ::= nat | Type → Type PCF Typing Rules: The usual thing. Gordon Plotkin

slide-4
SLIDE 4

Dynamic PCF

DPCF Raw Syntax (the same as PCF syntax)

Expr ::= X | λX.Expr | Expr(Expr) | N | zero | succ(Expr) | ifz Expr {zero ⇒ Expr | succ(X) ⇒ Expr } | fix X is Expr

DPCF Statics x1 : ok, . . . xn : ok ⊢ e : ok asserts that e is a well-formed expression with free variables ⊆ { x1, . . . , xn }. However: ⊢ 3(4) : ok is true but nonsensical (i.e., and error).

slide-5
SLIDE 5

DPCF Dynamics

Expr ::= X | λX.Expr | Expr(Expr) | N | zero | succ(Expr) | ifz Expr {zero ⇒ Expr | succ(X) ⇒ Expr } | fix X is Expr

DPCF “classes”: num and fun DPCF judgment forms Note: d is abstract syntax d val d is a (closed) value d → d′ d evaluates to d′ in one step d err d incurs a run-time error d isNum n d is of class num with value n d isNotNum d is not of class num d isFun (Fun x d) d is of class fun with value (Fun x d) d isNotFun d is not of class fun

slide-6
SLIDE 6

DPCF Dynamics Rules

Class checking (Num n) val (Num n) isNum n (Num n) isNotFun (Fun x d) val (Fun x n) (Fun x d) isNotNum (Fun x d) isFun (Fun x d) Transition (→) rules Zero → (Num 0) d → d′ (Succ d) → (Succ d′) d err (Succ d) err d isNum n (Succ d) → (Num (n + 1)) d isNotNum (Succ d) err

slide-7
SLIDE 7

DPCF Dynamics Rules, continued

More transition (→) rules [See Harper for the five ifz rules] d1 → d′

1

(App d1 d2) → (App d′

1 d2)

d1 err (App d1 d2) err) d1 isFun (Fun x d′) (App d1 d2) → d[d2/x] d isNotFun (App d1 d2) err (Fix x d) → d[(Fix x d)/x]

slide-8
SLIDE 8

DPCF Dynamics, safety (such as it is)

Lemma (Class Checking) If (d val), then: either (d isNum n) for some n, or (d isNotNum). either (d isFun (Fun x d′)) for some x and d′, or (d isNotFun). Theorem (Progress) If ⊢ d ok, then either d val or d err or d → d′ for some d′.

slide-9
SLIDE 9

Static vs. Dynamic Typing

In a statically typed language (e.g., Java, Haskell, . . . ) Type checking happens before run time. Errors such as 4(3) get caught in type-checking. But you are stuck with the type-system of the language. In a dynamically typed language (e.g., Scheme, Python, . . . ) d(d′) may sometimes be just fine and other times an error (say when d has the value 4). Type/class checking happens in every step of running the program! This is a mild runtime overhead, but it complicates implementing and reasoning about programs a lot! But you get to make up (& enforce) your own type system with every program you write. Compromise position: Work in a language with a two-fisted type system.