






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to type systems, their origins in mathematics, and their implementation in functional programming languages, specifically the l-calculus. It covers the concept of types, typing rules, and natural deduction systems. The document also discusses the relationship between untyped and simply typed l-calculus, the type reconstruction algorithm, and robin milner's creative leaps in extending the simply typed l-calculus with polymorphism.
Typology: Study notes
1 / 12
This page cannot be seen from the preview
Don't miss anything!







COMP 311 8
Question: what is the relationship between the (untyped) l-calculus and the (simply)
typed l-calculus?
_ Many expressions (and complete programs) in the (untyped) l-calculus cannot
be typed in the (simply) typed l-calculus!
_ Example: (l x ( x x))
x requires a type s = s Æ t, but no such type exists
_ If the constant operations terminate for all inputs, then every typable program
terminates for all inputs!
Question: is there a tractable algorithm for determining the type of an expression in the (untyped) l-calculus and rejecting the expression if no such type exists? Yes! The standard algorithm (called the type reconstruction algorithm) is based on a very simple idea:
_ generate a distinct type variable for every subexpression of the given expression M,
_ record the equality constraints between these variables dictated by the typing rule that matches the program context in which the subexpression associated with each variable appears, and
_ solve these constraints.
What is unification? Tree pattern matching where match variables only appear as leaves. _ A naïve recursive algorithm can be written in a few lines of Scheme or ML. _ The common practical algorithm relies on a union-find representation of finite sets to record equivalent symbolic types. Every set contains at least one symbolic type that is just a variable. This algorithm runs in essentially linear time. _ A linear algorithm exists but it is not as efficient for problems of practical size as the union-find based algorithm.
_ Question: is the reconstructed type unique? Many l - calculus programs have multiple typings. Consider the program (l x x). It can be assigned the type sÆs, for any type s Œ T , e.g., (int Æ int), (i n t Æ int) Æ (i n t Æ int), …
The type reconstruction algorithm deftly addresses this problem by returning the most general symbolic typing; all of the possible ground typings (containing no type variables) are substitution instances of this typing. For this reason, the typing produced by the type reconstruction algorithm is called the principal typing (or type) of the program.
Robin Milner’s Creative Leaps
_ The simple type system for the l - calculus is truly onerous because
polymorphic functions (those with variables in their principal types) have to be rewritten for each different typing. The original Pascal language suffers from precisely this problem.
Milner recognized that a surprisingly useful form of polymorphism could be added to the (simply) typed l - calculus by adding a let construct to the language family. The extension adds one new form to the family syntax M ::= … | (let ( x M) M) Given an expression of the form (let ( x M) N) x can be used polymorphically in N without breaking the principal typing property. Type reconstruction can first infer the principal type of M and subsequently use a renamed version of this type (a fresh name for each distinct type variable) for each distinct occurrence of x in N.