19
1 Prof. Dr. Michael Pradel Software Lab, University of Stuttgart Summer 2020 Programming Paradigms Type Systems (Part 6)

Type Systems (Part 6) Programming Paradigms - software-lab.orgsoftware-lab.org/.../pp/slides_05_types_06_formal.pdf · Software Lab, University of Stuttgart Summer 2020 Programming

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Type Systems (Part 6) Programming Paradigms - software-lab.orgsoftware-lab.org/.../pp/slides_05_types_06_formal.pdf · Software Lab, University of Stuttgart Summer 2020 Programming

1

Prof. Dr. Michael Pradel

Software Lab, University of StuttgartSummer 2020

Programming Paradigms

Type Systems (Part 6)

Page 2: Type Systems (Part 6) Programming Paradigms - software-lab.orgsoftware-lab.org/.../pp/slides_05_types_06_formal.pdf · Software Lab, University of Stuttgart Summer 2020 Programming

2

Overview

� Introduction

� Types in Programming Languages

� Polymorphism

� Type Equivalence

� Type Compatibility

� Formally Defined Type Systems

Page 3: Type Systems (Part 6) Programming Paradigms - software-lab.orgsoftware-lab.org/.../pp/slides_05_types_06_formal.pdf · Software Lab, University of Stuttgart Summer 2020 Programming

3

Formally Defined Type Systems

� Type systems are� implemented in a compiler

� formally described

� and sometimes both

� Active research area with dozens ofpapers each year� Focus: New languages and strong type

guarantees

� Example here: Typed expressions

Page 4: Type Systems (Part 6) Programming Paradigms - software-lab.orgsoftware-lab.org/.../pp/slides_05_types_06_formal.pdf · Software Lab, University of Stuttgart Summer 2020 Programming

41

Page 5: Type Systems (Part 6) Programming Paradigms - software-lab.orgsoftware-lab.org/.../pp/slides_05_types_06_formal.pdf · Software Lab, University of Stuttgart Summer 2020 Programming

5

Not All Expressions Make Sense

� Only some expressions can beevaluated

� Other don’t make sense

� Implementation of the language would getstuck or throw a runtime error

Page 6: Type Systems (Part 6) Programming Paradigms - software-lab.orgsoftware-lab.org/.../pp/slides_05_types_06_formal.pdf · Software Lab, University of Stuttgart Summer 2020 Programming

7

Types to the Rescue

� Use types to check whether anexpression is meaningful

� If term t has a type T , then its evaluation won’t

get stuck

� Written as t : T

� Two types

� Nat .. natural numbers

� Bool .. Boolean values

”has type”

Page 7: Type Systems (Part 6) Programming Paradigms - software-lab.orgsoftware-lab.org/.../pp/slides_05_types_06_formal.pdf · Software Lab, University of Stuttgart Summer 2020 Programming

42

Page 8: Type Systems (Part 6) Programming Paradigms - software-lab.orgsoftware-lab.org/.../pp/slides_05_types_06_formal.pdf · Software Lab, University of Stuttgart Summer 2020 Programming

43

Page 9: Type Systems (Part 6) Programming Paradigms - software-lab.orgsoftware-lab.org/.../pp/slides_05_types_06_formal.pdf · Software Lab, University of Stuttgart Summer 2020 Programming

10

Type Checking Expressions

� Typing relation: Smallest binaryrelation between terms and types thatsatisfies all instances of the rules

� Term t is typable (or well typed) ifthere is some T such that t : T

� Type derivation: Tree of instances ofthe typing rules that shows t : T

Page 10: Type Systems (Part 6) Programming Paradigms - software-lab.orgsoftware-lab.org/.../pp/slides_05_types_06_formal.pdf · Software Lab, University of Stuttgart Summer 2020 Programming

44

Page 11: Type Systems (Part 6) Programming Paradigms - software-lab.orgsoftware-lab.org/.../pp/slides_05_types_06_formal.pdf · Software Lab, University of Stuttgart Summer 2020 Programming

45

Page 12: Type Systems (Part 6) Programming Paradigms - software-lab.orgsoftware-lab.org/.../pp/slides_05_types_06_formal.pdf · Software Lab, University of Stuttgart Summer 2020 Programming

12

Quiz: Typing Derivation

Find the typing derivation for thefollowing expression:

if false then (pred(pred 0)) else (succ 0)

How many axioms and rules do youneed?

Please vote in Ilias.

Page 13: Type Systems (Part 6) Programming Paradigms - software-lab.orgsoftware-lab.org/.../pp/slides_05_types_06_formal.pdf · Software Lab, University of Stuttgart Summer 2020 Programming

46

Page 14: Type Systems (Part 6) Programming Paradigms - software-lab.orgsoftware-lab.org/.../pp/slides_05_types_06_formal.pdf · Software Lab, University of Stuttgart Summer 2020 Programming

14

Type Inference

Some PLs are statically typed but allowprogrammers to omit some typeannotations

� Get guarantees of static type checking

� Without paying the cost of full type annotations

� Different from gradual typing, where programmer

decides when and where to annotate types

Page 15: Type Systems (Part 6) Programming Paradigms - software-lab.orgsoftware-lab.org/.../pp/slides_05_types_06_formal.pdf · Software Lab, University of Stuttgart Summer 2020 Programming

15 - 1

Example

// Scalavar businessName = "Montreux Jazz Cafe"

def squareOf(x: Int) = x * x

businessName = squareOf(23)

Page 16: Type Systems (Part 6) Programming Paradigms - software-lab.orgsoftware-lab.org/.../pp/slides_05_types_06_formal.pdf · Software Lab, University of Stuttgart Summer 2020 Programming

15 - 2

Example

// Scalavar businessName = "Montreux Jazz Cafe"

def squareOf(x: Int) = x * x

businessName = squareOf(23)

Inferred tobe a String

Inferred toreturn an Int

Page 17: Type Systems (Part 6) Programming Paradigms - software-lab.orgsoftware-lab.org/.../pp/slides_05_types_06_formal.pdf · Software Lab, University of Stuttgart Summer 2020 Programming

15 - 3

Example

// Scalavar businessName = "Montreux Jazz Cafe"

def squareOf(x: Int) = x * x

businessName = squareOf(23)

Inferred tobe a String

Inferred toreturn an Int

Compile-time type error:Can’t assign Int to String variable

Page 18: Type Systems (Part 6) Programming Paradigms - software-lab.orgsoftware-lab.org/.../pp/slides_05_types_06_formal.pdf · Software Lab, University of Stuttgart Summer 2020 Programming

16 - 1

Quiz: Types

Which of the following statements istrue?

� Types are compatible if and only if they are equal

� Coercions mean that a programmer casts a value

from one type to another type

� Type conversions are guaranteed to preserve the

meaning of a value

� PLs with type inference may provide static type

guaranteesPlease vote in Ilias.

Page 19: Type Systems (Part 6) Programming Paradigms - software-lab.orgsoftware-lab.org/.../pp/slides_05_types_06_formal.pdf · Software Lab, University of Stuttgart Summer 2020 Programming

16 - 2

Quiz: Types

Which of the following statements istrue?

� Types are compatible if and only if they are equal

� Coercions mean that a programmer casts a value

from one type to another type

� Type conversions are guaranteed to preserve the

meaning of a value

� PLs with type inference may provide static type

guaranteesPlease vote in Ilias.