Lesson 10 Type Reconstruction 226 Chapter 22 Lesson
![Lesson 10 Type Reconstruction 2/26 Chapter 22 Lesson 10: Type Reconstruction Lesson 10 Type Reconstruction 2/26 Chapter 22 Lesson 10: Type Reconstruction](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-1.jpg)
![Type Reconstruction • • • substitutions typing with constraint sets (type equations) unification: solving Type Reconstruction • • • substitutions typing with constraint sets (type equations) unification: solving](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-2.jpg)
![Type substitutions Language: [Bool, Nat] with type variables A type substitution is a finite Type substitutions Language: [Bool, Nat] with type variables A type substitution is a finite](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-3.jpg)
![Type substitutions Language: [Bool, Nat] with type variables A type substitution is a finite Type substitutions Language: [Bool, Nat] with type variables A type substitution is a finite](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-4.jpg)
![Substitutions and typing Thm: If |- t: T, then |- t: T for any Substitutions and typing Thm: If |- t: T, then |- t: T for any](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-5.jpg)
!["Solving" typing problems Given and t, we can ask: 1. For every , does "Solving" typing problems Given and t, we can ask: 1. For every , does](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-6.jpg)
![Example: solutions of a typing problem ( , x: X. y: Y. z: Z. Example: solutions of a typing problem ( , x: X. y: Y. z: Z.](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-7.jpg)
![Constraints A constraint set C is a set of equations between types. C = Constraints A constraint set C is a set of equations between types. C =](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-8.jpg)
![Constraint inference rules Inference rule for application |- t 1: T 1 | C Constraint inference rules Inference rule for application |- t 1: T 1 | C](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-9.jpg)
![Constraint solutions Defn: Suppose |- t: S | C . A solution for ( Constraint solutions Defn: Suppose |- t: S | C . A solution for (](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-10.jpg)
![Unification Defn: < ' if ' = o for some . Defn: A principle Unification Defn: < ' if ' = o for some . Defn: A principle](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-11.jpg)
![Unification algorithm unify C = if C = then [ ] else let {S Unification algorithm unify C = if C = then [ ] else let {S](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-12.jpg)
![Principal Types Defn: A principal solution for ( , t, S, C) is a Principal Types Defn: A principal solution for ( , t, S, C) is a](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-13.jpg)
![Implicit Annotations We can extend the syntax to allow lambda abstractions without type annotations: Implicit Annotations We can extend the syntax to allow lambda abstractions without type annotations:](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-14.jpg)
![Let Polymorphism let double = f: Nat -> Nat. x: Nat. f(f x) in Let Polymorphism let double = f: Nat -> Nat. x: Nat. f(f x) in](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-15.jpg)
![Macro-like let rule let double = f: X -> X. x: X. f(f x) Macro-like let rule let double = f: X -> X. x: X. f(f x)](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-16.jpg)
![Macro-like let rule |- t 1: T 1 |- [x => t 1]t 2: Macro-like let rule |- t 1: T 1 |- [x => t 1]t 2:](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-17.jpg)
![Type schemes Add quantified type schemes: T : : = X | Bool | Type schemes Add quantified type schemes: T : : = X | Bool |](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-18.jpg)
![let-polymorphism rules |- t 1: T 1 , x : . T 1 |- let-polymorphism rules |- t 1: T 1 , x : . T 1 |-](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-19.jpg)
![let-polymorphism example double : X. (X -> X) -> X let double = f. let-polymorphism example double : X. (X -> X) -> X let double = f.](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-20.jpg)
![let-polymorphism and references Let ref, !, and : = be polymorphic functions with types let-polymorphism and references Let ref, !, and : = be polymorphic functions with types](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-21.jpg)
![The value restriction We correct this unsoundness by only allowing polymorphic generalization at let The value restriction We correct this unsoundness by only allowing polymorphic generalization at let](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-22.jpg)
![Let polymorphism with recursive values Another problem comes when we add recursive value definitions. Let polymorphism with recursive values Another problem comes when we add recursive value definitions.](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-23.jpg)
![Polymorphic Recursion What can we do about recursive function definitions where the function is Polymorphic Recursion What can we do about recursive function definitions where the function is](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-24.jpg)
- Slides: 24
![Lesson 10 Type Reconstruction 226 Chapter 22 Lesson 10 Type Reconstruction Lesson 10 Type Reconstruction 2/26 Chapter 22 Lesson 10: Type Reconstruction](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-1.jpg)
Lesson 10 Type Reconstruction 2/26 Chapter 22 Lesson 10: Type Reconstruction
![Type Reconstruction substitutions typing with constraint sets type equations unification solving Type Reconstruction • • • substitutions typing with constraint sets (type equations) unification: solving](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-2.jpg)
Type Reconstruction • • • substitutions typing with constraint sets (type equations) unification: solving constraint sets principlal types let polymorphism Lesson 10: Type Reconstruction 2
![Type substitutions Language Bool Nat with type variables A type substitution is a finite Type substitutions Language: [Bool, Nat] with type variables A type substitution is a finite](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-3.jpg)
Type substitutions Language: [Bool, Nat] with type variables A type substitution is a finite mapping from type variables to types. = [X => Nat -> Nat, Y => Bool, Z => X -> Nat] Type substitutions can be applied to types: T (X -> Z) = (Nat -> Nat) -> (X -> Nat) This extends pointwise to contexts: Lesson 10: Type Reconstruction 3
![Type substitutions Language Bool Nat with type variables A type substitution is a finite Type substitutions Language: [Bool, Nat] with type variables A type substitution is a finite](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-4.jpg)
Type substitutions Language: [Bool, Nat] with type variables A type substitution is a finite mapping from type variables to types. = [X => Nat -> Nat, Y => Bool, Z => X -> Nat] Type substitutions can be applied to types: T (X -> Z) = (Nat -> Nat) -> (X -> Nat) This extends pointwise to contexts: Composition of substitutions o (X) = ( X) if X dom = X otherwise Lesson 10: Type Reconstruction 4
![Substitutions and typing Thm If t T then t T for any Substitutions and typing Thm: If |- t: T, then |- t: T for any](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-5.jpg)
Substitutions and typing Thm: If |- t: T, then |- t: T for any type subst. . Prf: induction on type derivation for |- t: T. Lesson 10: Type Reconstruction 5
![Solving typing problems Given and t we can ask 1 For every does "Solving" typing problems Given and t, we can ask: 1. For every , does](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-6.jpg)
"Solving" typing problems Given and t, we can ask: 1. For every , does there exist a T s. t. |- t: T? 2. Does there exist a and a T s. t. |- t: T? 3. Question 1 leads to polymorphism, where T = T' and |- t: T'. The type variables are "quantified". Question 2 is the basis for type reconstuction: we think of the type variables as unknowns to be solved for. Defn: A solution for ( , t) is a pair ( , T) s. t. |- t: T. Lesson 10: Type Reconstruction 6
![Example solutions of a typing problem x X y Y z Z Example: solutions of a typing problem ( , x: X. y: Y. z: Z.](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-7.jpg)
Example: solutions of a typing problem ( , x: X. y: Y. z: Z. (x z) (y z)) has solutions [X => Nat -> Bool -> Nat, Y => Nat -> Bool, Z => Nat] [X => X 1 -> X 2 -> X 3, Y => X 1 -> X 2, Z => X 1] Lesson 10: Type Reconstruction 7
![Constraints A constraint set C is a set of equations between types C Constraints A constraint set C is a set of equations between types. C =](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-8.jpg)
Constraints A constraint set C is a set of equations between types. C = {Si = Ti | i 1, . . , n}. A substitution unifies (or satisfies) a constraint set C if Si = Ti for every equation Si = Ti in C. A constraint typing relation |- t: T | C where is a set of "fresh" type variables used in the constraint set C. This relation (or judgement) is defined by a set of inference rules. Lesson 10: Type Reconstruction 8
![Constraint inference rules Inference rule for application t 1 T 1 C Constraint inference rules Inference rule for application |- t 1: T 1 | C](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-9.jpg)
Constraint inference rules Inference rule for application |- t 1: T 1 | C 1 1 |- t 2: T 2 | C 2 2 1 2 = 1 FV(T 2) = 2 FV(T 1) = X 1, 2, t 1, t 2, T 1, T 2, C 1, C 2, C = C 1 C 2 {T 1 = T 2 -> X} = 1 2 {X} |- t 1 t 2: X | C Lesson 10: Type Reconstruction 9
![Constraint solutions Defn Suppose t S C A solution for Constraint solutions Defn: Suppose |- t: S | C . A solution for (](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-10.jpg)
Constraint solutions Defn: Suppose |- t: S | C . A solution for ( , t, S, C) is a pair ( , T) s. t. satisfies C and T = S. Thm: [Soundness of Constraint Typing] Suppose |- t: S | C X. If ( , T) is a solution for ( , t, S, C) then it is also a solution for ( , t), i. e. |- t: T. Thm: [Completeness of Constraint Typing] Suppose |- t: S | C . If ( , T) is a solution for ( , t) then there is a solution ( ', T) for ( , t, S, C) s. t. ' = . Cor: Suppose |- t: S | C . There is a soln for ( , t) iff there is a solution for ( , t, S, C). Lesson 10: Type Reconstruction 10
![Unification Defn if o for some Defn A principle Unification Defn: < ' if ' = o for some . Defn: A principle](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-11.jpg)
Unification Defn: < ' if ' = o for some . Defn: A principle unifier (most general unifier) for a constraint set C is a substitution that satisfies C s. t. < ' for any other ' that satifies C. Lesson 10: Type Reconstruction 11
![Unification algorithm unify C if C then else let S Unification algorithm unify C = if C = then [ ] else let {S](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-12.jpg)
Unification algorithm unify C = if C = then [ ] else let {S = T} C' = C in if S = T then unify(C') else if S = X and X FV(T) then unify([X => T]C') o [X => T] else if T = X and X FV(S) then unify([X => S]C') o [X => S] else if S = S 1 -> S 2 and T = T 1 -> T 2 then unify(C' {S 1 = T 1, S 2 = T 2}) else fail Thm: unify always terminates, and either fails or returns the principal unifier if a unifier exists. Lesson 10: Type Reconstruction 12
![Principal Types Defn A principal solution for t S C is a Principal Types Defn: A principal solution for ( , t, S, C) is a](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-13.jpg)
Principal Types Defn: A principal solution for ( , t, S, C) is a solution ( , T) s. t. for any other solution ( ', T') we gave < '. Thm: [Principal Types] If ( , t, S, C) has a solution, then it has a principal one. The unify algorithm can be used to determine whether ( , t, S, C) has a solution, and if so it calculates a principal one. Lesson 10: Type Reconstruction 13
![Implicit Annotations We can extend the syntax to allow lambda abstractions without type annotations Implicit Annotations We can extend the syntax to allow lambda abstractions without type annotations:](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-14.jpg)
Implicit Annotations We can extend the syntax to allow lambda abstractions without type annotations: x. t. The corresponding type constraint rule supplies a fresh type variable as an implicit annotation. X , x: X |- t 1: T | C , |- x: X. t 1: X -> T | C ( {X}) Lesson 10: Type Reconstruction (CT-Abs. Inf) 14
![Let Polymorphism let double f Nat Nat x Nat ff x in Let Polymorphism let double = f: Nat -> Nat. x: Nat. f(f x) in](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-15.jpg)
Let Polymorphism let double = f: Nat -> Nat. x: Nat. f(f x) in double ( x: Nat. succ x) 2 let double = f: Bool -> Bool. x: Bool. f(f x) in double ( x: not x) false An attempt at a generic double: let double = f: X -> X. x: X. f(f x) in let a = double ( x: Nat. succ x) 2 in let b = double ( x: not x) false ==> X -> X = Nat -> Nat = Bool -> Bool Lesson 10: Type Reconstruction 15
![Macrolike let rule let double f X X x X ff x Macro-like let rule let double = f: X -> X. x: X. f(f x)](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-16.jpg)
Macro-like let rule let double = f: X -> X. x: X. f(f x) in let a = double ( x: Nat. succ x) 2 in let b = double ( x: not x) false could be typed as: let a = ( f: X -> X. x: X. f(f x)) ( x: Nat. succ x) 2 in let b = ( f: X' -> X'. x: X'. f(f x)) ( x: not x) false or, using implicit type annotations: let a = ( f. x. f(f x)) ( x: Nat. succ x) 2 in let b = ( f. x. f(f x)) ( x: not x) false Lesson 10: Type Reconstruction 16
![Macrolike let rule t 1 T 1 x t 1t 2 Macro-like let rule |- t 1: T 1 |- [x => t 1]t 2:](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-17.jpg)
Macro-like let rule |- t 1: T 1 |- [x => t 1]t 2: T 2 |- let x = t 1 in t 2: T 2 (T-Let. Poly) The substitution can create multiple independent copies of t 1, each of which can be typed independently (assuming implicit annotations, which introduce separate type variables for each copy). Lesson 10: Type Reconstruction 17
![Type schemes Add quantified type schemes T X Bool Type schemes Add quantified type schemes: T : : = X | Bool |](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-18.jpg)
Type schemes Add quantified type schemes: T : : = X | Bool | Nat | T -> T P : : = T | X. P Contexts become finite mappings from term variables to type schemes: : : = | , x : P Examples of type schemes: Nat, X -> Nat, X. Y. X -> Y -> X Lesson 10: Type Reconstruction 18
![letpolymorphism rules t 1 T 1 x T 1 let-polymorphism rules |- t 1: T 1 , x : . T 1 |-](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-19.jpg)
let-polymorphism rules |- t 1: T 1 , x : . T 1 |- t 2: T 2 |- let x = t 1 in t 2: T 2 (T-Let. Poly) where ' are the type variables free in T 1 but not free in , x : . T |- x: [ => ']T (T-Poly. Inst) where ' is a set of fresh type variables Lesson 10: Type Reconstruction 19
![letpolymorphism example double X X X X let double f let-polymorphism example double : X. (X -> X) -> X let double = f.](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-20.jpg)
let-polymorphism example double : X. (X -> X) -> X let double = f. x. f(f x) in let a = double ( x: Nat. succ x) 2 in let b = double ( x: not x) false in (a, b) (Y -> Y) -> Y (Z -> Z) -> Z Then unification yields [Y => Nat, Z => Bool]. Lesson 10: Type Reconstruction 20
![letpolymorphism and references Let ref and be polymorphic functions with types let-polymorphism and references Let ref, !, and : = be polymorphic functions with types](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-21.jpg)
let-polymorphism and references Let ref, !, and : = be polymorphic functions with types ref : X. X -> Ref(X) ! : X. Ref(X) -> X : = : X. Ref(X) *X -> Unit let r = ref( x. x) r : X. Ref(X -> X) in let a = r : = ( x: Nat. succ x) in let b = !r false in () Ref(Nat -> Nat) Ref(Bool -> Bool) We've managed to apply ( x: Nat. succ x) to false! Lesson 10: Type Reconstruction 21
![The value restriction We correct this unsoundness by only allowing polymorphic generalization at let The value restriction We correct this unsoundness by only allowing polymorphic generalization at let](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-22.jpg)
The value restriction We correct this unsoundness by only allowing polymorphic generalization at let declarations if the expression is a value. This is called the value restriction. let r = ref( x. x) r : Ref(X -> X) in let a = r : = ( x: Nat. succ x) in let b = !r false in () Ref(Nat -> Nat) [X => Nat] Ref(Nat -> Nat) Now we get a type error in " !r false ". Lesson 10: Type Reconstruction 22
![Let polymorphism with recursive values Another problem comes when we add recursive value definitions Let polymorphism with recursive values Another problem comes when we add recursive value definitions.](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-23.jpg)
Let polymorphism with recursive values Another problem comes when we add recursive value definitions. let rec f = x. t in. . . is typed as though it were written let f = fix( f. x. t) in. . . where fix : X. (X -> X) -> X except that the type of the outer f can be generalized. Note that the inner f is -bound, not let bound, so it cannot be polymorphic within the body t. Lesson 10: Type Reconstruction 23
![Polymorphic Recursion What can we do about recursive function definitions where the function is Polymorphic Recursion What can we do about recursive function definitions where the function is](https://slidetodoc.com/presentation_image_h2/dbae52315c61ff12fabf3282a05d0d8f/image-24.jpg)
Polymorphic Recursion What can we do about recursive function definitions where the function is polymorphic and is used polymorphically in the body of it's definition? (This is called polymorphic recursion. ) let rec f = x. (f true; f 3; x) Have to use a fancier form of type reconstruction: the iterative Mycroft-Milner algorithm. Lesson 10: Type Reconstruction 24
226 curepipe
Linea 1503
Al quran muka surat 226
Psy 226
Cmpe 226
Comm 226 quiz 1
Comm 226
Lesson 1 planning reconstruction
The politics of reconstruction chapter 12 section 1
The collapse of reconstruction chapter 12 section 3
Reconstruction and its effects
Ordeal chapter 22
Ordeal chapter 22
Chapter 2 reconstruction america's unfinished revolution
Chapter 6 civil war and reconstruction
Chapter 12 reconstruction and its effects
Is hyper v type 1 or type 2
Order of recruitment of muscle fiber types
Pregnancy test keeps saying error
Type 1 error vs type 2 error example
Rock cycle
Narrow band theory in sport
Mbti breakdown
Myotonic dystrophy.
Deflection type instruments