Type Systems
&
Type Inference
Usman Masood Ashraf
Maj Nawaz
What is a Type System?
Type Systems: “In programming, a type system is a set of
rules and constraints that govern the usage of data types. It
defines how different types of data can be used and
manipulated in a program”
How do we evaluate programming
languages?
We Evaluate Languages By…
1. Domain
2. Community
3. Type System
The More You Use
Your Type System
The More It Works
For You
Fundamental Type System
Classifications
Fundamental Type System Classifications
◦ Type Safety
◦ Static vs Dynamic Type Checking
◦ Type Inference
Type Safety
“A safe language is one that protects
its own abstractions.” - Pierce (TaPL)
Type Safety
Unsafe Behavior
● Dereferencing null pointer
● Accessing array out of
bounds
● Using uninitialized variables
● Casting between types
● Not checking parameter lists
Safe Behavior
● Runtime Exceptions
○ NullPointerException
○ ArrayIndexOutOfBoundsException
● Not allowing uninitialized
variables
● Compile time parameter list
agreement checks
Static vs Dynamic
Static Type Checking
Verify safety of program before
execution.
Dynamic Type Checking
Verify safety of program at
execution.
Question: Why do people
get into religious
arguments over static vs
dynamic type checking?
More seriously, what some of the
pros/cons of static vs dynamic type
systems?
Static vs Dynamic
Static Type Checking
◦ Early feedback
◦ More reliable
◦ More optimizable
◦ Longer edit-compile-test cycle
Dynamic Type Checking
◦ Shorter edit-compile-test cycle
◦ More flexible
◦ Good for prototyping
◦ Better at metaprogramming
◦ Get ready for lots and lots of unit testing
Static vs Dynamic: Language Examples
Static
◦ C
◦ C++
◦ Java
◦ C#
◦ Scala
◦ Haskell
◦ Rust
◦ Kotlin
◦ Go
Dynamic
◦ JavaScript
◦ Ruby
◦ Python
◦ Perl
◦ PHP
◦ Lisp
◦ Clojure
◦ R
◦ Bash
Dynamic Static
Weak
Strong
Erlang
Clojure
Python
Groovy
Ruby
Magik
Perl
VB
PHP
JavaScript
C#
F# Haskell
Scala
Java
C
C++
Type Inference
Automatic detection of data type
◦ Feature of some strongly statically typed
languages
◦ Especially prevalent in functional
programming languages
Explicitly Type Annotated
val x: Int = 100
val y: Seq[String] = Seq(“apple”, “orange”)
val z: User = new User(“Jordan Parmer”)
val a: Option[String] = findKey(“type”)
Type Inference
val x = 100
val y = Seq(“apple”, “orange”)
val z = new User(“Jordan Parmer”)
val a = findKey(“type”)
Type Inference
Automatic detection of data type
◦ Feature of some strongly statically typed
languages
◦ Especially prevalent in functional
programming languages
Explicitly Type Annotated
val x: Int = 100
val y: Seq[String] = Seq(“apple”, “orange”)
val z: User = new User(“Jordan Parmer”)
val a: Option[String] = findKey(“type”)
Type Inference
val x = 100
val y = Seq(“apple”, “orange”)
val z = new User(“Jordan Parmer”)
val a = findKey(“type”)
Still statically typed at
compile time
Strong type inference paves the way
for expressions over statements.
Type Driven Development
Focus on type signatures instead of implementation. With pure-
ish functional languages, if it compiles, you know it is likely
correct.
Lets you fill in the details later.
contribution to program correctness and code maintainability
◦ Check for bad program behavior
◦ Early detection of program errors
◦ Readability
◦ Focus on Logic
◦ Protect integrity of user defined abstractions
◦ Documentation
▫ Easy to reason code’s purpose
▫ Doesn’t drift like code comments
Thanks!
ANY QUESTIONS?

what is Type Systems and Type Inference.pptx

  • 1.
    Type Systems & Type Inference UsmanMasood Ashraf Maj Nawaz
  • 2.
    What is aType System? Type Systems: “In programming, a type system is a set of rules and constraints that govern the usage of data types. It defines how different types of data can be used and manipulated in a program”
  • 3.
    How do weevaluate programming languages?
  • 4.
    We Evaluate LanguagesBy… 1. Domain 2. Community 3. Type System
  • 5.
    The More YouUse Your Type System The More It Works For You
  • 6.
  • 7.
    Fundamental Type SystemClassifications ◦ Type Safety ◦ Static vs Dynamic Type Checking ◦ Type Inference
  • 8.
    Type Safety “A safelanguage is one that protects its own abstractions.” - Pierce (TaPL)
  • 9.
    Type Safety Unsafe Behavior ●Dereferencing null pointer ● Accessing array out of bounds ● Using uninitialized variables ● Casting between types ● Not checking parameter lists Safe Behavior ● Runtime Exceptions ○ NullPointerException ○ ArrayIndexOutOfBoundsException ● Not allowing uninitialized variables ● Compile time parameter list agreement checks
  • 10.
    Static vs Dynamic StaticType Checking Verify safety of program before execution. Dynamic Type Checking Verify safety of program at execution.
  • 11.
    Question: Why dopeople get into religious arguments over static vs dynamic type checking?
  • 12.
    More seriously, whatsome of the pros/cons of static vs dynamic type systems?
  • 13.
    Static vs Dynamic StaticType Checking ◦ Early feedback ◦ More reliable ◦ More optimizable ◦ Longer edit-compile-test cycle Dynamic Type Checking ◦ Shorter edit-compile-test cycle ◦ More flexible ◦ Good for prototyping ◦ Better at metaprogramming ◦ Get ready for lots and lots of unit testing
  • 14.
    Static vs Dynamic:Language Examples Static ◦ C ◦ C++ ◦ Java ◦ C# ◦ Scala ◦ Haskell ◦ Rust ◦ Kotlin ◦ Go Dynamic ◦ JavaScript ◦ Ruby ◦ Python ◦ Perl ◦ PHP ◦ Lisp ◦ Clojure ◦ R ◦ Bash
  • 15.
  • 16.
    Type Inference Automatic detectionof data type ◦ Feature of some strongly statically typed languages ◦ Especially prevalent in functional programming languages Explicitly Type Annotated val x: Int = 100 val y: Seq[String] = Seq(“apple”, “orange”) val z: User = new User(“Jordan Parmer”) val a: Option[String] = findKey(“type”) Type Inference val x = 100 val y = Seq(“apple”, “orange”) val z = new User(“Jordan Parmer”) val a = findKey(“type”)
  • 17.
    Type Inference Automatic detectionof data type ◦ Feature of some strongly statically typed languages ◦ Especially prevalent in functional programming languages Explicitly Type Annotated val x: Int = 100 val y: Seq[String] = Seq(“apple”, “orange”) val z: User = new User(“Jordan Parmer”) val a: Option[String] = findKey(“type”) Type Inference val x = 100 val y = Seq(“apple”, “orange”) val z = new User(“Jordan Parmer”) val a = findKey(“type”) Still statically typed at compile time
  • 18.
    Strong type inferencepaves the way for expressions over statements.
  • 19.
    Type Driven Development Focuson type signatures instead of implementation. With pure- ish functional languages, if it compiles, you know it is likely correct. Lets you fill in the details later.
  • 20.
    contribution to programcorrectness and code maintainability ◦ Check for bad program behavior ◦ Early detection of program errors ◦ Readability ◦ Focus on Logic ◦ Protect integrity of user defined abstractions ◦ Documentation ▫ Easy to reason code’s purpose ▫ Doesn’t drift like code comments
  • 21.

Editor's Notes

  • #9 refers to the property of a programming language or system that prevents unintended operations on data. In a type-safe language, the compiler or runtime environment enforces strict adherence to the rules of the type system, ensuring that only operations permissible on a particular data type are allowed.
  • #10 Type safety is not binary. Languages vary in safety on a per feature basis (e.g. memory safety, abstraction safety, runtime safety, cast safety, etc.)
  • #17 Type inference is a feature in some programming languages that allows the compiler or interpreter to automatically deduce and assign data types to variables without explicit type declarations from the programmer. Instead of requiring the programmer to explicitly specify the types of variables, the type inference system analyzes the code and infers the types based on the context and usage.
  • #19 Strong Type Inference: In a language with strong type inference, the compiler or interpreter can deduce variable types without explicit declarations. This promotes a more flexible and expressive coding style.