





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






In order to express the meaning of a program, we need a formal language to capture these meanings. One way to express meaning is to say something about the types of the expressions. By the end of lecture, you should know
Strongly Typed Languages
Weakly Typed Languages
Advantages of Strongly Typed Languages
Engineering
Safety • Ever try casting an integer into a pointer in C?
1 int i = 5; 2 *((int *)i) = 10;
Theory
To create a formal system, you must specify the following:
You do NOT need:
Example
Symbols S, (, ), Z, P , x, y.
Definition of a furbitz
Definition of the gloppit relation
True Sentences If αgβ, then also
Does the Furbitz look familiar?
Constants ` n : int (assuming n is an int)
` true : bool
` false : bool
Variables Γ ` x : τ , if x : τ ∈ Γ
Arithmetic
Γ e 1 : int Γ e 2 : int Γ ` e 1 ⊕ e 2 : int
Relations
Γ e 1 : int Γ e 2 : int Γ ` e 1 ∼ e 2 : bool
Booleans
Γ e 1 : bool Γ e 2 : bool Γ ` e 1 && e 2 : bool
Γ e 1 : bool Γ e 2 : bool Γ ` e 1 || e 2 : bool
Suppose we want to prove that Γ ` (x ∗ 5 > 7)&& y : bool. Assume that Γ = [x : int ; y : bool ]
First thing: Write down the thing you are trying to prove, and put a bar over it.
Γ ` (x ∗ 5 > 7)&& y : bool
Look at the outermost expression. What kind of expression is this?
Use the rule
Γ e 1 : bool Γ e 2 : bool Γ ` e 1 && e 2 : bool
You need the “and” rule. It will tell you which parts of the goal need to be proved next.
Suppose we want to prove that Γ ` (x ∗ 5 > 7)&& y : bool. Assume that Γ = [x : int ; y : bool ]
Write parts on top and put a bar over them as well.
Γ x ∗ 5 > 7 : bool Γ y : bool Γ ` (x ∗ 5 > 7)&& y : bool
What to do next? Let’s work left to right. The expression we want next is a “greater” expression. (Besides, the y expression is already an axiom.)
Suppose we want to prove that Γ ` (x ∗ 5 > 7)&& y : bool. Assume that Γ = [x : int ; y : bool ]
Following the “greater” rule, we break the x * 5 > 7 into two parts.
Γ x ∗ 5 : int Γ 7 : int Γ x ∗ 5 > 7 : bool Γ y : bool Γ ` (x ∗ 5 > 7)&& y : bool
We will turn our attention to the multiplication now.
Suppose we want to prove that Γ ` (x ∗ 5 > 7)&& y : bool. Assume that Γ = [x : int ; y : bool ]
At this point, there are no more subtrees to expand out. We are done.
Γ x : int Γ 5 : int Γ x ∗ 5 : int Γ 7 : int Γ x ∗ 5 > 7 : bool Γ y : bool Γ ` (x ∗ 5 > 7)&& y : bool
If
Γ e 1 : bool Γ e 2 : τ Γ e 3 : τ Γ if e 1 then e 2 else e 3 : τ
Γ′^ = Γ ∪ {f : (α → β); g : (δ → α); x : δ} f (g x) : β Γ fun f g x −> f (g x) : (α → β) → (δ → α) → δ → β After using the function rule: Γ ∪ [x 1 : τ 1 ; · · · ; xn : τn] e : τ Γ fun x 1 · · · xn−>e : τ 1 → · · · → τn → τ Next, we use application. Γ e : τ 1 → τ 2 → · · · → τn → τ Γ e 1 : τ 1 · · · Γ en : τn Γ e e 1 e 2 · · · en : τ This give us...
Γ′^ f : (α → β) Γ′^ (g x) : (α) Γ′^ = Γ ∪ {f : (α → β); g : (δ → α); x : δ} f (g x) : β Γ fun f g x −> f (g x) : (α → β) → (δ → α) → δ → β The rule for parentheses is trivial... can you write it? Next, we’ll use application again.
Γ′^ ` f : (α → β)
Γ′^ g : (δ → α) Γ′^ x : δ Γ′^ (g x) : α Γ′^ = Γ ∪ {f : (α → β); g : (δ → α); x : δ} f (g x) : β Γ ` fun f g x −> f (g x) : (α → β) → (δ → α) → δ → β We are done. These rules are meant mainly to verify a type, but they can be used to infer a type as well.
At first, give everything a separate type.
Γ ` fun f g x −> f (g x) : α → β → δ → γ
Γ′^ = Γ ∪ {f : α; g : β; x : δ} f (g x) : γ Γ fun f g x −> f (g x) : α → β → δ → γ
Γ′^ f : α Γ′^ (g x) :??? Γ′^ = Γ ∪ {f : α; g : β; x : δ} f (g x) : γ Γ fun f g x −> f (g x) : α → β → δ → γ From here we see that α needs to be a function type. Also, we need to decide a type for (g x).
We make the appropriate substitutions....
Γ′^ f : (ν → γ) Γ′^ (g x) : ν Γ′^ = Γ ∪ {f : (ν → γ); g : β; x : δ} f (g x) : γ Γ fun f g x −> f (g x) : (ν → γ) → β → δ → γ
Now we use the application rule for (g x).
Γ′^ ` f : (ν → γ)
Γ′^ g : β Γ′^ x : δ Γ′^ (g x) : ν Γ′^ = Γ ∪ {f : (ν → γ); g : β; x : δ} f (g x) : γ Γ ` fun f g x −> f (g x) : (ν → γ) → β → δ → γ
Substituting, we get...
Γ′^ ` f : (ν → γ)
Γ′^ g : (δ → ν) Γ′^ x : δ Γ′^ (g x) : ν Γ′^ = Γ ∪ {f : (ν → γ); g : (δ → ν); x : δ} f (g x) : γ Γ ` fun f g x −> f (g x) : (ν → γ) → (δ → ν) → δ → γ
Now we’re done.
Here’s an example I showed one time I gave this lecture.
Γ ∪ [x : τ ] x : τ Γ fun x −>x : τ → τ
Γ^1 ` id : τ → τ
Γ^1 id : τ → τ Γ^1 10 : int Γ^1 (id 10) : int Γ^1 id (id 10) : int Γ ` let id = fun x −>x in id (id 10) : int