Docsity

Static vs. Dynamic Typing in Programming Languages, Lecture notes of Programming Languages

Partial preview of the text

Download Static vs. Dynamic Typing in Programming Languages and more Lecture notes Programming Languages in PDF only on Docsity!

CSE341: Programming Languages

Lecture 18

Static vs. Dynamic Typing

Dan Grossman

Spring 2016

Key differences

  • Racket and ML have much in common
  • Key differences
    • Syntax
    • Pattern-matching vs. struct-tests and accessor-functions
    • Semantics of various let-expressions
    • …
  • Biggest difference: ML’s type system and Racket’s lack thereof *
  • There is Typed Racket, which interacts well with Racket so you can have typed and untyped modules, but we won’t study it, and it differs in interesting ways from ML

ML from a Racket perspective

  • Syntax, etc. aside, ML is like a well-defined subset of Racket
  • Many of the programs it disallows have bugs 
    • In fact, in what ML allows, I never need primitives like number?
  • But other programs it disallows I may actually want to write 

(define (f x) (if (> x 0) #t (list 1 2))) (define xs (list 1 #t "hi")) (define y (f (car xs)))

(define (g x) (+ x x)) ; ok (define (f y) (+ y (car y))) (define (h z) (g (cons z 2)))

Racket from an ML Perspective

One way to describe Racket is that it has “one big datatype”

  • All values have this type
  • Constructors are applied implicitly (values are tagged )
  • 42 is really like Int 42
  • Primitives implicitly check tags and extract data , raising errors for wrong constructors

fun car v = case v of Pair(a,b) => a | _ => raise … fun pair? v = case v of Pair _ => true | _ => false

datatype theType = Int of int | String of string | Pair of theType * theType | Fun of theType -> theType | …

inttag 42

Static checking

  • Static checking is anything done to reject a program after it (successfully) parses but before it runs
  • Part of a PL’s definition: what static checking is performed
    • A “helpful tool” could do more checking
  • Common way to define a PL’s static checking is via a type system
    • Approach is to give each variable, expression, etc. a type
    • Purposes include preventing misuse of primitives (e.g., 4/"hi"), enforcing abstraction, and avoiding dynamic checking - Dynamic means at run-time
  • Dynamically-typed languages do (almost) no static checking
    • Line is not absolute

Example: ML, what types prevent

In ML, type-checking ensures a program (when run) will never have:

  • A primitive operation used on a value of the wrong type
    • Arithmetic on a non-number
    • e1 e2 where e1 does not evaluate to a function
    • A non-boolean between if and then
  • A variable not defined in the environment
  • A pattern-match with a redundant pattern
  • Code outside a module call a function not in the module’s signature
  • …

(First two are “standard” for type systems, but different languages’ type systems ensure different things)

Purpose is to prevent something

Have discussed facts about what the ML type system does and does not prevent

  • Separate from how (e.g., one type for each variable) though previously studied many of ML’s typing rules

Language design includes deciding what is checked and how

Hard part is making sure the type system “achieves its purpose”

  • That “the how” accomplishes “the what”
  • More precise definition next

A question of eagerness

“Catching a bug before it matters” is in inherent tension with “Don’t report a bug that might not matter”

Static checking / dynamic checking are two points on a continuum

Silly example: Suppose we just want to prevent evaluating 3 / 0

  • Keystroke time: disallow it in the editor
  • Compile time: disallow it if seen in code
  • Link time: disallow it if seen in code that may be called to evaluate main
  • Run time: disallow it right when we get to the division
  • Later: Instead of doing the division, return +inf.0 instead
    • Just like 3.0 / 0.0 does in every (?) PL (it’s useful!)

Incompleteness

A few functions ML rejects even though they do not divide by a string

fun f1 x = 4 div "hi" (* but f1 never called *)

fun f2 x = if true then 0 else 4 div "hi"

fun f3 x = if x then 0 else 4 div "hi" val x = f3 true

fun f4 x = if x <= abs x then 0 else 4 div "hi"

fun f5 x = 4 div x val y = f5 (if true then 1 else "hi")

Why incompleteness

  • Almost anything you might like to check statically is undecidable:
    • Any static checker cannot do all of: (1) always terminate, (2) be sound, (3) be complete
    • This is a mathematical theorem!
  • Examples:
    • Will this function terminate on some input?
    • Will this function ever use a variable not in the environment?
    • Will this function treat a string as a function?
    • Will this function divide by zero?
  • Undecidability is an essential concept at the core of computing
    • The inherent approximation of static checking is probably its most important ramification

Why weak typing (C/C++)

Weak typing: There exist programs that, by definition, must pass static checking but then when run can “set the computer on fire”?

  • Dynamic checking is optional and in practice not done
  • Why might anything happen?
  • Ease of language implementation: Checks left to the programmer
  • Performance: Dynamic checks take time
  • Lower level: Compiler does not insert information like array sizes, so it cannot do the checks

Weak typing is a poor name: Really about doing neither static nor dynamic checks

  • A big problem is array bounds, which most PLs check dynamically

What weak typing has caused

  • Old now-much-rarer saying: “strong types for weak minds”
    • Idea was humans will always be smarter than a type system (cf. undecidability), so need to let them say “trust me”
  • Reality: humans are really bad at avoiding bugs
    • We need all the help we can get!
    • And type systems have gotten much more expressive (fewer false positives)
  • 1 bug in a 30-million line operating system written in C can make an entire computer vulnerable - An important bug like this was probably announced this week (because there is one almost every week)

Another misconception

What operations are primitives defined on and when an error?

  • Example: Is “foo” + “bar” allowed?
  • Example: Is “foo” + 3 allowed?
  • Example: Is arr[10] allowed if arr has only 5 elements?
  • Example: Can you call a function with too few or too many arguments?

This is not static vs. dynamic checking (sometimes confused with it)

  • It is “what is the run-time semantics of the primitive”
  • It is related because it also involves trade-offs between catching bugs sooner versus maybe being more convenient

Racket generally less lenient on these things than, e.g., Ruby

Now can argue…

Having carefully stated facts about static checking,

can now consider arguments about which is better :

static checking or dynamic checking

Remember most languages do some of each

  • For example, perhaps types for primitives are checked statically, but array bounds are not