Type: What?
(Lol see what I did there?)
Anyways, a type is a classification of data that says:
- What values it can hold (integers, booleans etc)
- The operations that can be performed on it. (concatenation, arithmetic etc)
- The memory layout and how it’s stored on the system.
What’re Type Systems:
A set of rules that governs how types are assigned to variables, functions and expressions. Ideally, the goals are:
- Correctness (e.g. preventing adding a string to an integer - detecting semantic errors).
- Optimisation
- Safety (preventing Buffer Overflow Attack)
Where:
- We operate on the AST
Types of Types (Types)
-
Statically Typed: We check the types at compile time (C, Java, etc.) - Check before executing the program.
-
Dynamically Typed: We the types at runtime (Python)
-
Weak Type: Allows implicit type conversions (JS -
"10" + 5 = "105"
). -
Strong Type: Strictly enforces type rules (Python)
-
Subtyping: When a variable
A
is a subtype of variableB
, thenA
can be used anywhereB
is expected.
Typing Rule:
It defines how types are assigned to expressions, variables and functions. We denote it with your assumptions at the top and the conclusion at the bottom.
For example:
Simply: If is declared in the environment with type , then can be used with type , with return type R.
- The means entrails
- The environment stores the type information for variables in scope at a given time.
Environment:
When talking about the environment, we use the following notation:
Where:
- indicates that variable is in the local environment and has type
- are the types of the function parameters
- is the return type.
- are the names of the function parameters
- are the identifiers and types.
Type Checking:
Imagine someone asked you to check the following type:
And you’re only given these 3 rules:
We could prove it like so: