Programming Languages and Compilers (CS 421)

Similar documents
Programming Languages and Compilers (CS 421)

CS558 Programming Languages

Type Checking and Type Inference

Lecture 15 CIS 341: COMPILERS

CS558 Programming Languages

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4

Two Problems. Programming Languages and Compilers (CS 421) Type Inference - Example. Type Inference - Outline. Type Inference - Example

Programming Languages and Compilers (CS 421)

Lecture #23: Conversion and Type Inference

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Booleans and Short-Circuit Evaluation. Tuples as Values.

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Programming Languages and Compilers (CS 421)

COS 320. Compiling Techniques

Introduction to OCaml

Pierce Ch. 3, 8, 11, 15. Type Systems

CS558 Programming Languages

Types-2. Polymorphism

Programming Languages and Compilers (CS 421)

n (0 1)*1 n a*b(a*) n ((01) (10))* n You tell me n Regular expressions (equivalently, regular 10/20/ /20/16 4

l e t print_name r = p r i n t _ e n d l i n e ( Name : ^ r. name)

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Introduction to Programming Using Java (98-388)

CSCI-GA Scripting Languages

Agenda. CS301 Session 11. Common type constructors. Things we could add to Impcore. Discussion: midterm exam - take-home or inclass?

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Some instance messages and methods

Programming Languages and Compilers (CS 421)

Programming Languages & Compilers. Programming Languages and Compilers (CS 421) I. Major Phases of a Compiler. Programming Languages & Compilers

CS421 Spring 2014 Midterm 1

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

n Takes abstract syntax trees as input n In simple cases could be just strings n One procedure for each syntactic category

Programming Languages and Compilers (CS 421)

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Static Checking and Type Systems

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Types, Type Inference and Unification

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

Programming Languages and Compilers (CS 421)

Announcements. CSCI 334: Principles of Programming Languages. Lecture 5: Fundamentals III & ML

n What is its running time? 9/18/17 2 n poor_rev [1,2,3] = n (poor_rev [1] = n ((poor_rev [1] =

Programming Languages Assignment #7

Programming Languages & Compilers. Programming Languages and Compilers (CS 421) Programming Languages & Compilers. Major Phases of a Compiler

Goal. CS152: Programming Languages. Lecture 15 Parametric Polymorphism. What the Library Likes. What The Client Likes. Start simpler.

Motivation for typed languages

Case by Case. Chapter 3

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

Lambda Calculus. Variables and Functions. cs3723 1

Mini-ML. CS 502 Lecture 2 8/28/08

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

CSE-321: Assignment 8 (100 points)

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

Tracing Ambiguity in GADT Type Inference

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

CMSC 330: Organization of Programming Languages

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

Programming Languages

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

Harvard School of Engineering and Applied Sciences Computer Science 152

On the Logical Foundations of Staged Computation

Cunning Plan. One-Slide Summary. Functional Programming. Functional Programming. Introduction to COOL #1. Classroom Object-Oriented Language

First-Class Type Classes

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Scope and Introduction to Functional Languages. Review and Finish Scoping. Announcements. Assignment 3 due Thu at 11:55pm. Website has SML resources

Begin at the beginning

Gradual Typing with Inference

Type systems. Static typing

Lecture 2: Big-Step Semantics

CMSC 330: Organization of Programming Languages

Higher-Order Logic. Specification and Verification with Higher-Order Logic

Semantic Processing (Part 2)

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

Data Types The ML Type System

Handout 2 August 25, 2008

A Brief Introduction to Standard ML

CSE 341 Section 5. Winter 2018

Type Systems, Type Inference, and Polymorphism

Questions? Static Semantics. Static Semantics. Static Semantics. Next week on Wednesday (5 th of October) no

Core-TyCO The Language Definition Version 0.1

Simply-Typed Lambda Calculus

Functional Programming. Introduction To Cool

Abstraction Elimination for Kappa Calculus

Lambda Calculus and Type Inference

Programming Languages and Compilers (CS 421)

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Pattern Matching and Abstract Data Types

Introduction to ML. Based on materials by Vitaly Shmatikov. General-purpose, non-c-like, non-oo language. Related languages: Haskell, Ocaml, F#,

CS558 Programming Languages

ML Type Inference and Unification. Arlen Cox

Flang typechecker Due: February 27, 2015

Static typing, polymorphism and type synthesis

GADTs meet Subtyping

Where is ML type inference headed?

Topic 16: Issues in compiling functional and object-oriented languages

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Transcription:

Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC http://courses.engr.illinois.edu/cs421 Based in part on slides by Mattox Beckman, as updated by Vikram Adve and Gul Agha 10/3/17 1

Nested Recursive Types # type 'a labeled_tree = TreeNode of ('a * 'a labeled_tree list);; type 'a labeled_tree = TreeNode of ('a * 'a labeled_tree list) 10/3/17 2

Nested Recursive Type Values # let ltree = TreeNode(5, [TreeNode (3, []); TreeNode (2, [TreeNode (1, []); TreeNode (7, [])]); TreeNode (5, [])]);; 10/3/17 3

Nested Recursive Type Values val ltree : int labeled_tree = TreeNode (5, [TreeNode (3, []); TreeNode (2, [TreeNode (1, []); TreeNode (7, [])]); TreeNode (5, [])]) 10/3/17 4

Nested Recursive Type Values Ltree = TreeNode(5) :: :: :: [ ] TreeNode(3) TreeNode(2) TreeNode(5) [ ] :: :: [ ] [ ] TreeNode(1) TreeNode(7) [ ] [ ] 10/3/17 5

Nested Recursive Type Values 5 3 2 5 1 7 10/3/17 6

Mutually Recursive Functions # let rec flatten_tree labtree = match labtree with TreeNode (x,treelist) -> x::flatten_tree_list treelist and flatten_tree_list treelist = match treelist with [] -> [] labtree::labtrees -> flatten_tree labtree @ flatten_tree_list labtrees;; 10/3/17 7

Mutually Recursive Functions val flatten_tree : 'a labeled_tree -> 'a list = <fun> val flatten_tree_list : 'a labeled_tree list -> 'a list = <fun> # flatten_tree ltree;; - : int list = [5; 3; 2; 1; 7; 5] n Nested recursive types lead to mutually recursive functions 10/3/17 8

Why Data Types? n Data types play a key role in: n Data abstraction in the design of programs n Type checking in the analysis of programs n Compile-time code generation in the translation and execution of programs n Data layout (how many words; which are data and which are pointers) dictated by type 10/3/17 9

Terminology n Type: A type t defines a set of possible data values n E.g. short in C is {x 2 15-1 x -2 15 } n A value in this set is said to have type t n Type system: rules of a language assigning types to expressions 10/3/17 10

Types as Specifications n Types describe properties n Different type systems describe different properties, eg n Data is read-write versus read-only n Operation has authority to access data n Data came from right source n Operation might or could not raise an exception n Common type systems focus on types describing same data layout and access methods 10/3/17 11

Sound Type System n If an expression is assigned type t, and it evaluates to a value v, then v is in the set of values defined by t n SML, OCAML, Scheme and Ada have sound type systems n Most implementations of C and C++ do not 10/3/17 12

Strongly Typed Language n When no application of an operator to arguments can lead to a run-time type error, language is strongly typed n Eg: 1 + 2.3;; n Depends on definition of type error 10/3/17 13

Strongly Typed Language n C++ claimed to be strongly typed, but n Union types allow creating a value at one type and using it at another n Type coercions may cause unexpected (undesirable) effects n No array bounds check (in fact, no runtime checks at all) n SML, OCAML strongly typed but still must do dynamic array bounds checks, runtime type case analysis, and other checks 10/3/17 14

Static vs Dynamic Types Static type: type assigned to an expression at compile time Dynamic type: type assigned to a storage location at run time Statically typed language: static type assigned to every expression at compile time Dynamically typed language: type of an expression determined at run time 10/3/17 15

Type Checking n When is op(arg1,,argn) allowed? n Type checking assures that operations are applied to the right number of arguments of the right types n Right type may mean same type as was specified, or may mean that there is a predefined implicit coercion that will be applied n Used to resolve overloaded operations 10/3/17 16

Type Checking n Type checking may be done statically at compile time or dynamically at run time n Dynamically typed (aka untyped) languages (eg LISP, Prolog) do only dynamic type checking n Statically typed languages can do most type checking statically 10/3/17 17

Dynamic Type Checking n Performed at run-time before each operation is applied n Types of variables and operations left unspecified until run-time n Same variable may be used at different types 10/3/17 18

Dynamic Type Checking n Data object must contain type information n Errors aren t detected until violating application is executed (maybe years after the code was written) 10/3/17 19

Static Type Checking n Performed after parsing, before code generation n Type of every variable and signature of every operator must be known at compile time 10/3/17 20

Static Type Checking n Can eliminate need to store type information in data object if no dynamic type checking is needed n Catches many programming errors at earliest point n Can t check types that depend on dynamically computed values n Eg: array bounds 10/3/17 21

Static Type Checking n Typically places restrictions on languages n Garbage collection n References instead of pointers n All variables initialized when created n Variable only used at one type n Union types allow for work-arounds, but effectively introduce dynamic type checks 10/3/17 22

Type Declarations n Type declarations: explicit assignment of types to variables (signatures to functions) in the code of a program n Must be checked in a strongly typed language n Often not necessary for strong typing or even static typing (depends on the type system) 10/3/17 23

Type Inference n Type inference: A program analysis to assign a type to an expression from the program context of the expression n Fully static type inference first introduced by Robin Miller in ML n Haskle, OCAML, SML all use type inference n Records are a problem for type inference 10/3/17 24

Format of Type Judgments n A type judgement has the form Γ - exp : τ n Γ is a typing environment n Supplies the types of variables (and function names when function names are not variables) n Γ is a set of the form { x :σ,... } n For any x at most one σ such that (x : σ Γ) n exp is a program expression n τ is a type to be assigned to exp n - pronounced turnstyle, or entails (or satisfies or, informally, shows ) 10/3/17 25

Axioms - Constants Γ - n : int (assuming n is an integer constant) Γ - true : bool Γ - false : bool n n These rules are true with any typing environment Γ, n are meta-variables 10/3/17 26

Axioms Variables (Monomorphic Rule) Notation: Let Γ(x) = σ if x : σ Γ Note: if such σ exits, its unique Variable axiom: Γ - x : σ if Γ(x) = σ 10/3/17 27

Simple Rules - Arithmetic Primitive operators ( { +, -, *, }): Γ - e 1 :τ 1 Γ - e 2 :τ 2 ( ):τ 1 τ 2 τ 3 Γ - e 1 e 2 : τ 3 Relations ( Γ - e 1 : τ { <, >, =, <=, >= }): Γ - e 2 : τ Γ - e 1 e 2 :bool For the moment, think τ is int 10/3/17 28

Example: {x:int} - x + 2 = 3 :bool What do we need to show first? {x:int} - x:int {x:int} - 2:int {x : int} - x + 2 : bool {x:int} - 3 :int {x:int} - x + 2 = 3 : bool 10/3/17 29

Example: {x:int} - x + 2 = 3 :bool What do we need for the left side? {x:int} - x:int {x:int} - 2:int {x : int} - x + 2 : int {x:int} - 3 :int {x:int} - x + 2 = 3 : bool Rel 10/3/17 30

Example: {x:int} - x + 2 = 3 :bool How to finish? {x:int} - x:int {x:int} - 2:int AO {x : int} - x + 2 : int {x:int} - 3 :int {x:int} - x + 2 = 3 : bool Rel 10/3/17 31

Example: {x:int} - x + 2 = 3 :bool Complete Proof (type derivation) Var Const {x:int} - x:int {x:int} - 2:int AO {x : int} - x + 2 : int {x:int} - 3 :int {x:int} - x + 2 = 3 : bool Const Rel 10/3/17 32

Simple Rules - Booleans Connectives Γ - e 1 : bool Γ - e 2 : bool Γ - e 1 && e 2 : bool Γ - e 1 : bool Γ - e 2 : bool Γ - e 1 e 2 : bool 10/3/17 33

Type Variables in Rules n If_then_else rule: Γ - e 1 : bool Γ - e 2 : τ Γ - e 3 : τ Γ - (if e 1 then e 2 else e 3 ) : τ n τ is a type variable (meta-variable) n Can take any type at all n All instances in a rule application must get same type n Then branch, else branch and if_then_else must all have same type 10/3/17 34

Function Application n Application rule: Γ - e 1 : τ 1 τ 2 Γ - e 2 : τ 1 Γ - (e 1 e 2 ) : τ 2 n If you have a function expression e 1 of type τ 1 τ 2 applied to an argument e 2 of type τ 1, the resulting expression e 1 e 2 has type τ 2 10/3/17 35

Fun Rule n Rules describe types, but also how the environment Γ may change n n Can only do what rule allows! fun rule: {x : τ 1 } + Γ - e : τ 2 Γ - fun x -> e : τ 1 τ 2 10/3/17 36

Fun Examples {y : int } + Γ - y + 3 : int Γ - fun y -> y + 3 : int int {f : int bool} + Γ - f 2 :: [true] : bool list Γ - (fun f -> f 2 :: [true]) : (int bool) bool list 10/3/17 37

(Monomorphic) Let and Let Rec n let rule: Γ - e 1 : τ 1 {x : τ 1 } + Γ - e 2 : τ 2 Γ - (let x = e 1 in e 2 ) : τ 2 n let rec rule: {x: τ 1 } + Γ - e 1 :τ 1 {x: τ 1 } + Γ - e 2 :τ 2 Γ - (let rec x = e 1 in e 2 ) : τ 2 10/3/17 38

Example n Which rule do we apply?? - (let rec one = 1 :: one in let x = 2 in fun y -> (x :: y :: one) ) : int int list 10/3/17 39

Example n Let rec rule: 2 {one : int list} - 1 (let x = 2 in {one : int list} - fun y -> (x :: y :: one)) (1 :: one) : int list : int int list - (let rec one = 1 :: one in let x = 2 in fun y -> (x :: y :: one) ) : int int list 10/3/17 40

Proof of 1 n Which rule? {one : int list} - (1 :: one) : int list 10/3/17 41

Proof of 1 n Application 3 4 {one : int list} - {one : int list} - ((::) 1): int list int list one : int list {one : int list} - (1 :: one) : int list 10/3/17 42

Proof of 3 Constants Rule Constants Rule {one : int list} - {one : int list} - (::) : int int list int list 1 : int {one : int list} - ((::) 1) : int list int list 10/3/17 43

Proof of 4 n Rule for variables {one : int list} - one:int list 10/3/17 44

Proof of 2 n Constant fun y -> {one : int list} - 2:int 5 {x:int; one : int list} - (x :: y :: one)) : int int list {one : int list} - (let x = 2 in fun y -> (x :: y :: one)) : int int list 10/3/17 45

Proof of 5? {x:int; one : int list} - fun y -> (x :: y :: one)) : int int list 10/3/17 46

Proof of 5? {y:int; x:int; one : int list} - (x :: y :: one) : int list {x:int; one : int list} - fun y -> (x :: y :: one)) : int int list 10/3/17 47

Proof of 5 6 7 {y:int; x:int; one:int list} {y:int; x:int; one:int list} - ((::) x):int list int list - (y :: one) : int list {y:int; x:int; one : int list} - (x :: y :: one) : int list {x:int; one : int list} - fun y -> (x :: y :: one)) : int int list 10/3/17 48

Proof of 6 Constant Variable { } - (::) : int int list int list { ; x:int; } - x:int {y:int; x:int; one : int list} - ((::) x) :int list int list 10/3/17 49

Proof of 7 Pf of 6 [y/x] Variable {y:int; } - ((::) y) { ; one: int list} - :int list int list one: int list {y:int; x:int; one : int list} - (y :: one) : int list 10/3/17 50

Curry - Howard Isomorphism n Type Systems are logics; logics are type systems n Types are propositions; propositions are types n Terms are proofs; proofs are terms n Function space arrow corresponds to implication; application corresponds to modus ponens 10/3/17 51

Curry - Howard Isomorphism n Modus Ponens A B A B Application Γ - e 1 : α β Γ - e 2 : α Γ - (e 1 e 2 ) : β 10/3/17 52

Mea Culpa n The above system can t handle polymorphism as in OCAML n No type variables in type language (only metavariable in the logic) n Would need: n Object level type variables and some kind of type quantification n let and let rec rules to introduce polymorphism n Explicit rule to eliminate (instantiate) polymorphism 10/3/17 53

Support for Polymorphic Types n Monomorpic Types (τ): n Basic Types: int, bool, float, string, unit, n Type Variables: α, β, γ, δ, ε n Compound Types: α β, int * string, bool list, n Polymorphic Types: n Monomorphic types τ n Universally quantified monomorphic types n A α 1,, α n. τ n Can think of τ as same as. τ A 10/3/17 54

Support for Polymorphic Types n Typing Environment Γ supplies polymorphic types (which will often just be monomorphic) for variables n Free variables of monomorphic type just type variables that occur in it n Write FreeVars(τ) n Free variables of polymorphic type removes variables that are universally quantified A n FreeVars( α 1,, α n. τ) = FreeVars(τ) {α 1,, α n } n FreeVars(Γ) = all FreeVars of types in range of Γ 10/3/17 55

n Given: Monomorphic to Polymorphic n type environment Γ n monomorphic type τ n τ shares type variables with Γ n Want most polymorphic type for τ that doesn t break sharing type variables with Γ A n Gen(τ, Γ) = α 1,, α n. τ where {α 1,, α n } = freevars(τ) freevars(γ) 10/3/17 56

Polymorphic Typing Rules n A type judgement has the form Γ - exp : τ n n Γ uses polymorphic types τ still monomorphic n Most rules stay same (except use more general typing environments) n Rules that change: n n n Variables Let and Let Rec Allow polymorphic constants n Worth noting functions again 10/3/17 57

Polymorphic Let and Let Rec n let rule: Γ - e 1 : τ 1 {x : Gen(τ 1,Γ)} + Γ - e 2 : τ 2 Γ - (let x = e 1 in e 2 ) : τ 2 n let rec rule: {x : τ 1 } + Γ - e 1 :τ 1 {x:gen(τ 1,Γ)} + Γ - e 2 :τ 2 Γ - (let rec x = e 1 in e 2 ) : τ 2 10/3/17 58

Polymorphic Variables (Identifiers) Variable axiom: Γ - x : ϕ(τ) if Γ(x) = α 1,, α n. τ n Where ϕ replaces all occurrences of α 1,, α n by monotypes τ 1,, τ n n Note: Monomorphic rule special case: Γ - x : τ if Γ(x) = τ n Constants treated same way 10/3/17 59 A

Fun Rule Stays the Same n fun rule: {x : τ 1 } + Γ - e : τ 2 Γ - fun x -> e : τ 1 τ 2 n Types τ 1, τ 2 monomorphic n Function argument must always be used at same type in function body 10/3/17 60

Polymorphic Example n Assume additional constants: n hd : α. α list -> α A A n tl: α. α list -> α list n is_empty : α. α list -> bool A n :: : α. α -> α list -> α list A A n [] : α. α list 10/3/17 61

Polymorphic Example n Show:? {} - let rec length = fun l -> if is_empty l then 0 else 1 + length (tl l) in length ((::) 2 []) + length((::) true []) : int 10/3/17 62

Polymorphic Example: Let Rec Rule n Show: (1) (2) {length:α list -> int} {length: α. α list -> int} - fun l -> - length ((::) 2 []) + : α list -> int length((::) true []) : int {} - let rec length = fun l -> if is_empty l then 0 else 1 + length (tl l) in length ((::) 2 []) + length((::) true []) : int A 10/3/17 63

Polymorphic Example (1) n Show:? {length:α list -> int} - fun l -> if is_empty l then 0 else 1 + length (tl l) : α list -> int 10/3/17 64

Polymorphic Example (1): Fun Rule n Show: (3) {length:α list -> int, l: α list } - if is_empty l then 0 else length (hd l) + length (tl l) : int {length:α list -> int} - fun l -> if is_empty l then 0 else 1 + length (tl l) : α list -> int 10/3/17 65

Polymorphic Example (3) n Let Γ ={length:α list -> int, l: α list } n Show Γ - if is_empty l then 0? else 1 + length (tl l) : int 10/3/17 66

Polymorphic Example (3):IfThenElse n Let Γ ={length:α list -> int, l: α list } n Show (4) (5) (6) Γ - is_empty l Γ - 0:int Γ - 1 + : bool length (tl l) : int Γ - if is_empty l then 0 else 1 + length (tl l) : int 10/3/17 67

Polymorphic Example (4) n Let Γ ={length:α list -> int, l: α list } n Show? Γ - is_empty l : bool 10/3/17 68

Polymorphic Example (4):Application n Let Γ ={length:α list -> int, l: α list } n Show?? Γ - is_empty : α list -> bool Γ - l : α list Γ - is_empty l : bool 10/3/17 69

Polymorphic Example (4) n Let Γ ={length:α list -> int, l: α list } n Show By Const since α list -> bool is instance of α. α list -> bool? A Γ - is_empty : α list -> bool Γ - l : α list Γ - is_empty l : bool 10/3/17 70

Polymorphic Example (4) n Let Γ ={length:α list -> int, l: α list } n Show By Const since α list -> bool is instance of α. α list -> bool A By Variable Γ(l) = α list Γ - is_empty : α list -> bool Γ - l : α list Γ - is_empty l : bool n This finishes (4) 10/3/17 71

Polymorphic Example (5):Const n Let Γ ={length:α list -> int, l: α list } n Show By Const Rule Γ - 0:int 10/3/17 72

Polymorphic Example (6):Arith Op n Let Γ ={length:α list -> int, l: α list } n Show By Variable Γ - length (7) By Const : α list -> int Γ - (tl l) : α list Γ - 1:int Γ - length (tl l) : int Γ - 1 + length (tl l) : int 10/3/17 73

Polymorphic Example (7):App Rule n Let Γ ={length:α list -> int, l: α list } n Show By Const Γ - (tl l) : α list -> α list Γ - (tl l) : α list By Variable Γ - l : α list By Const since α list -> α list is instance of A α. α list -> α list 10/3/17 74

Polymorphic Example: (2) by ArithOp n Let Γ = {length: α. α list -> int} n Show: A (8) (9) Γ - Γ - length ((::) 2 []) :int length((::) true []) : int {length: α. α list -> int} - length ((::) 2 []) + length((::) true []) : int 10/3/17 75

Polymorphic Example: (8)AppRule n Let Γ = {length: α. α list -> int} n Show: A Γ - length : int list ->int Γ - ((::)2 []):int list Γ - length ((::) 2 []) :int 10/3/17 76

Polymorphic Example: (8)AppRule n Let Γ = {length: α. α list -> int} n Show: By Var since int list -> int is instance of A α. α list -> int A (10) Γ - length : int list ->int Γ - ((::)2 []):int list Γ - length ((::) 2 []) :int 10/3/17 77

Polymorphic Example: (10)AppRule n Let Γ = {length: α. α list -> int} n Show: n By Const since α list is instance of A α. α list (11) A Γ -((::) 2) : int list -> int list Γ - [] : int list Γ - ((::) 2 []) :int list 10/3/17 78

Polymorphic Example: (11)AppRule n Let Γ = {length: α. α list -> int} n Show: n By Const since α list is instance of A A α. α list By Const Γ - (::) : int -> int list -> int list Γ - 2 : int Γ - ((::) 2) : int list -> int list 10/3/17 79

Polymorphic Example: (9)AppRule n Let Γ = {length: α. α list -> int} n Show: A Γ - Γ - length:bool list ->int ((::) true []):bool list Γ - length ((::) true []) :int 10/3/17 80

Polymorphic Example: (9)AppRule n Let Γ = {length: α. α list -> int} n Show: By Var since bool list -> int is instance of A α. α list -> int A (12) Γ - Γ - length:bool list ->int ((::) true []):bool list Γ - length ((::) true []) :int 10/3/17 81

Polymorphic Example: (12)AppRule n Let Γ = {length: α. α list -> int} n Show: n By Const since α list is instance of A α. α list (13) A Γ -((::)true):bool list ->bool list Γ - []:bool list Γ - ((::) true []) :bool list 10/3/17 82

Polymorphic Example: (13)AppRule n Let Γ = {length: α. α list -> int} n Show: By Const since bool list is instance of α. α list A A Γ - Γ - By Const (::):bool ->bool list ->bool list true : bool Γ - ((::) true) : bool list -> bool list 10/3/17 83