1 / 26

CS 476 – Programming Language Design

CS 476 – Programming Language Design. William Mansky. From Typed Lambda Calculus to OCaml. User-friendly syntax Basic types, tuples, records Inductive datatypes and pattern-matching Local declarations References Type inference Generics/polymorphism. Universal Polymorphism.

jstevens
Download Presentation

CS 476 – Programming Language Design

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 476 – Programming Language Design William Mansky

  2. From Typed Lambda Calculus to OCaml • User-friendly syntax • Basic types, tuples, records • Inductive datatypes and pattern-matching • Local declarations • References • Type inference • Generics/polymorphism

  3. Universal Polymorphism • What’s the inferred type for fun x -> x? • , with no constraints • In other words, for any ! let id = fun x -> x in (id 1 = 1) && (id true)

  4. Universal Polymorphism • What’s the inferred type for fun x -> x? • , with no constraints • In other words, for any ! let id = fun x -> x;; (* val id : ‘a -> ‘a = <fun> *) • ‘a is OCaml’s way of writing a type variable

  5. Universal Polymorphism: Examples let id = fun x -> x;; (* val id : ‘a -> ‘a = <fun> *) let f x y = y;; (* val f : ‘a -> b -> ‘b = <fun> *) let g f x = f x;; (* val g : (‘a -> ‘b) -> ‘a -> ‘b = <fun> *)

  6. Universal Polymorphism: Examples let id = fun x -> x;; (* id : *) let f x y = y;; (* f : *) let g f x = f x;; (* g : *)

  7. Universal Polymorphism: Examples let update f x v = fun y -> if y = x then Some v else f y;; (* update : ('a -> 'b option) -> 'a -> 'b -> ('a -> 'b option) *) type context = ident -> typ option type env = ident -> value option update (gamma : context) x t update (r : env) x v …

  8. Universal Polymorphism • Universal polymorphism (also generic, or parametric): a type can have any number of universally quantified variables • A function can be applied at any instantiation of its type • Happens when a function doesn’t care about the type of an argument • So the function will do the same thing with an input of any type • Contrast with OO polymorphism

  9. Universal Polymorphism

  10. Universal Polymorphism where Unsolvable!

  11. Universal Polymorphism: Typing • There are now two kinds of types: • A monomorphic type, or monotype, doesn’t have quantifiers • A polymorphic type, or polytype, is where is a monotype (that uses ) • When should we assign a polytype to a term? • Let-polymorphism: only at local definitions

  12. Universal Polymorphism: Typing • There are now two kinds of types: • A monomorphic type, or monotype, doesn’t have quantifiers • A polymorphic type, or polytype, is where is a monotype (that uses ) • When should we assign a polytype to a term? • Let-polymorphism: only at local definitions

  13. Let-Polymorphism: Typing What variables should we quantify? let id = fun x -> x in (id 1 = 1) && (id bool)

  14. Let-Polymorphism: Typing where let id = fun x -> x in (id 1 = 1) && (id bool)

  15. Let-Polymorphism: Typing where fun y -> (let f = fun x -> y in y + f 3)

  16. Let-Polymorphism: Typing where fun y -> (let f = fun x -> y in y + f 3)

  17. Let-Polymorphism: Typing where fun y -> (let f = fun x -> y in y + f 3)

  18. Let-Polymorphism: Typing

  19. Let-Polymorphism: Typing • We can have polytypes in , but in , is a monotype

  20. Let-Polymorphism: Type Inference • We can have polytypes in , but in , is a monotype

  21. Let-Polymorphism: Type Inference • We can have polytypes in , but in , is a monotype

  22. More Universal Polymorphism • With let-polymorphism, we can have polytypes in , but when, is a monotype • This is what OCaml does let g f x y = (f x = 1) && (f y);; (* Type error: f takes an int, not a bool *) • In let-polymorphism, a polytype never appears as an argument

  23. More Universal Polymorphism • With let-polymorphism, we can have polytypes in , but when, is a monotype • With full universal polymorphism, polytypes are first-class types let g f x y = (f x = 1) && (f y);; (* g : *) • There is no type inference algorithm for full universal polymorphism! • We need to explicitly instantiate the polytypes at each use

  24. System F T ::= TT | <tident> | <tident>. T L ::= <ident>:T. L | LL | <ident> | <tident>. L | L [T] let id = (id [int] 1 = 1) && (id [bool] true) • Used in some versions of Haskell, dependently-typed languages

  25. Universal Polymorphism • In OCaml, a function with free type variables gets a universal type, and can be used at any instantiation of its type • Happens when a function doesn’t care about the type of an argument, and will do the same thing with an input of any type • Requires only a small change to type checking/inference • More general universal polymorphism is possible, but if we go too general, we lose type inference

  26. From Typed Lambda Calculus to OCaml • User-friendly syntax • Basic types, tuples, records • Inductive datatypes and pattern-matching • Local declarations • References • Type inference • Generics/polymorphism

More Related