type systems (part 6) programming paradigms -...
TRANSCRIPT
![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](https://reader034.vdocument.in/reader034/viewer/2022042321/5f0bc6ec7e708231d43229a2/html5/thumbnails/1.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042321/5f0bc6ec7e708231d43229a2/html5/thumbnails/2.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042321/5f0bc6ec7e708231d43229a2/html5/thumbnails/3.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042321/5f0bc6ec7e708231d43229a2/html5/thumbnails/4.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042321/5f0bc6ec7e708231d43229a2/html5/thumbnails/5.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042321/5f0bc6ec7e708231d43229a2/html5/thumbnails/6.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042321/5f0bc6ec7e708231d43229a2/html5/thumbnails/7.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042321/5f0bc6ec7e708231d43229a2/html5/thumbnails/8.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042321/5f0bc6ec7e708231d43229a2/html5/thumbnails/9.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042321/5f0bc6ec7e708231d43229a2/html5/thumbnails/10.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042321/5f0bc6ec7e708231d43229a2/html5/thumbnails/11.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042321/5f0bc6ec7e708231d43229a2/html5/thumbnails/12.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042321/5f0bc6ec7e708231d43229a2/html5/thumbnails/13.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042321/5f0bc6ec7e708231d43229a2/html5/thumbnails/14.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042321/5f0bc6ec7e708231d43229a2/html5/thumbnails/15.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042321/5f0bc6ec7e708231d43229a2/html5/thumbnails/16.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042321/5f0bc6ec7e708231d43229a2/html5/thumbnails/17.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042321/5f0bc6ec7e708231d43229a2/html5/thumbnails/18.jpg)
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](https://reader034.vdocument.in/reader034/viewer/2022042321/5f0bc6ec7e708231d43229a2/html5/thumbnails/19.jpg)
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.