CSE 505. Lecture #9. October 1, Lambda Calculus. Recursion and Fixed-points. Typed Lambda Calculi. Least Fixed Point

Similar documents
Polymorphism and Type Inference

Polymorphism and Type Inference

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

According to Larry Wall (designer of PERL): a language by geniuses! for geniuses. Lecture 7: Haskell. Haskell 98. Haskell (cont) - Type-safe!

cs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides.

Programming Languages. Programming with λ-calculus. Lecture 11: Type Systems. Special Hour to discuss HW? if-then-else int

Exercise 1 ( = 24 points)

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

Type Checking and Type Inference

F28PL1 Programming Languages. Lecture 11: Standard ML 1

CSE 341 Section 5. Winter 2018

SML A F unctional Functional Language Language Lecture 19

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

Functional Programming and λ Calculus. Amey Karkare Dept of CSE, IIT Kanpur

Lecture 9: More Lambda Calculus / Types

- M ::= v (M M) λv. M - Impure versions add constants, but not necessary! - Turing-complete. - true = λ u. λ v. u. - false = λ u. λ v.

Types, Type Inference and Unification

The Untyped Lambda Calculus

CSCC24 Functional Programming Typing, Scope, Exceptions ML

Recursive Definitions, Fixed Points and the Combinator

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Types and Type Inference

Lists. Adrian Groza. Department of Computer Science Technical University of Cluj-Napoca

Lambda Calculus. Variables and Functions. cs3723 1

CPS 506 Comparative Programming Languages. Programming Language Paradigm

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

Lambda Calculus. Concepts in Programming Languages Recitation 6:

Exercise 1 ( = 18 points)

Chapter 15. Functional Programming. Topics. Currying. Currying: example. Currying: example. Reduction

Chapter 15. Functional Programming Languages

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

Lambda Calculus.

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

Plan (next 4 weeks) 1. Fast forward. 2. Rewind. 3. Slow motion. Rapid introduction to what s in OCaml. Go over the pieces individually

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

Functional Languages and Higher-Order Functions

Types and Type Inference

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

CMSC 330: Organization of Programming Languages

Functional Programming and Haskell

COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland

Overloading, Type Classes, and Algebraic Datatypes

CSCI-GA Scripting Languages

Lists. Michael P. Fourman. February 2, 2010

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

CSE 3302 Programming Languages Lecture 8: Functional Programming

Introduction to Functional Programming in Haskell 1 / 56

Language Sequence. The Algol Family and ML. Algol 60 Sample. Algol 60. Some trouble spots in Algol 60. Algol Joke. John Mitchell

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

OCaml. History, Variants. ML s holy trinity. Interacting with ML. Base type: Integers. Base type: Strings. *Notes from Sorin Lerner at UCSD*

CS558 Programming Languages

More Lambda Calculus and Intro to Type Systems

A Brief Introduction to Standard ML

Exercise 1 ( = 22 points)

An introduction introduction to functional functional programming programming using usin Haskell

CS 320: Concepts of Programming Languages

So what does studying PL buy me?

Introduction to SML Getting Started

1.3. Conditional expressions To express case distinctions like

Programming Paradigms Languages F28PL, Lecture 1

A Fourth Look At ML. Chapter Eleven Modern Programming Languages, 2nd ed. 1

The Untyped Lambda Calculus

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

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

Example Scheme Function: equal

The type checker will complain that the two branches have different types, one is string and the other is int

CS558 Programming Languages

Lambda Calculus and Type Inference

CMSC 330: Organization of Programming Languages

Functional Programming

Programming Languages Lecture 15: Recursive Types & Subtyping

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

LECTURE 16. Functional Programming

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

CS558 Programming Languages

CSE-321 Programming Languages 2010 Midterm

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

Exercises on ML. Programming Languages. Chanseok Oh

If we have a call. Now consider fastmap, a version of map that uses futures: Now look at the call. That is, instead of

Functors signature Order = sig functor name type elem (structname:signature) = structure definition; val le : elem*elem -> bool end; elem

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

The Untyped Lambda Calculus

CS 11 Haskell track: lecture 1

Lecture 5: The Untyped λ-calculus

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Introduction to SML Basic Types, Tuples, Lists, Trees and Higher-Order Functions

9/23/2014. Why study? Lambda calculus. Church Rosser theorem Completeness of Lambda Calculus: Turing Complete

Programming Languages

Functional Programming. Big Picture. Design of Programming Languages

COMP80 Lambda Calculus Programming Languages Slides Courtesy of Prof. Sam Guyer Tufts University Computer Science History Big ideas Examples:

CS 242. Fundamentals. Reading: See last slide

Lecture #23: Conversion and Type Inference

INTRODUCTION TO FUNCTIONAL PROGRAMMING

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

CSE-321 Programming Languages 2012 Midterm

More Lambda Calculus and Intro to Type Systems

Programming Languages and Compilers (CS 421)

Transcription:

Lambda Calculus CSE 505 Lecture #9 October 1, 2012 Expr ::= Var λ Var. Expr (Expr Expr) Key Concepts: Bound and Free Occurrences Substitution, Reduction Rules: α, β, η Confluence and Unique Normal Form Nontermination and Leftmost Reduction Church-Rosser Property (==>* and <==>*) Encoding Things in Lambda Calculus Recursion and Fixed-Point Operator (Y) 2 Recursion and Fixed-points fact = λn.(((if (is0 n)) 1) ((mult n) (fact (pred n))) t = λf. λn.(((if (is0 n)) 1) ((mult n) (f (pred n)))) Why does the fixed-point of t capture f? Fixed point g has the property: g = (t g) g = λn.(((if (is0 n)) 1) ((mult n) (g (pred n))) 3 Y = λf. (λx.(f (x x)) 4 λx.(f (x x))) In Lambda Calculus, Y is fixed-point operator, because (for any t): Proof: (Y t) <==>* (t (Y t)) (Y t) = (λf. (λx.(f (x x)) λx.(f (x x))) t) ==> (λx.(t (x x)) λx.(t (x x))) ==> (t (λx.(t (x x)) λx.(t (x x))))) <==> (t (Y t)) Consider: Fixed-point f1(n) = Fixed-point f2(n) = Least Fixed Point letrec f(n) = if n=0 then 0 else f(n); 0, if n=0 1, if n 0 0, if n=0 2, if n 0 Typed Lambda Calculi Thus far, we have studied the untyped lambda calculus, i.e., no types associated with vars. There are two well-known calculi: - the simply-type lambda calculus - the second-order (polymorphic) lambda calculus Least fixed-point g(n) = 0, if n=0?, if n 0 Interesting, adding types causes all lambda expressions to terminate! Cannot have (x x). 5 6

Lambda Calculus in PLs Lisp: (lambda (f g) (lambda (x) (f (g x)))) Javascript: function(f g) {return function (x) return f(g(x))}} Python: comp = lambda f,g: lambda x: f(g(x)) double = lambda x:x*2 triple = lambda y:y*3 comp(double, triple)(100) = 600 Functional languages such as Scheme, ML and Haskell also support lambda calculus. Types in PLs Concrete and Abstract types Strong Typing Type equivalence and security Exceptions Polymorphism Type inference Advanced Type Systems 8 What is a type? Simple Answer: A type is a set of values. e.g. type int = {, -2, -1, 0, 1, 2 } Better Answer: A type is a set + operations. e.g. type int = {, -2, -1, 0, 1, 2 } together with operations such as +, -, *, etc. That is, a type is more like an algebra. (Even better characterization is possible.) What is a type (cont d) Consider type int = {, -2, -1, 0, 1, 2 } with operations such as +, -, *, etc. Are numerals, -2, -1, 0, 1, 2 necessary in defining int? Can t we use, -10, -01, 0, 01, 10, 11,? Choice of literals not crucial We can define the values of a type using operations called constructors. But literals are useful in practice. 10 9 A type is a set of operations! zero: int succ: int int + : int * int int * : int * int int % : int * int int < : int * int int Abstract Data Type Refined int type exception overflow, underflow, dividebyzero zero: int succ: int int + : int * int int * : int * int int % : int * int int < : int * int bool 11 12

Why use types? Three important reasons: -faster execution: e.g. for (i=1; i++; i<=n) { x * y } -better program readability/documentation: e.g. void f(x) { } vs void f(tree x) { } -early error checking: e.g. int x; void f(string y){ }; f(x) Strongly Typed Language Popular Answer: every variable has a type declared for it Above criterion not sufficient (or even necessary!): int i, j, k; i := j ** k Somewhat Better Answer: A language is strongly typed if compiler can perform all type-checking 13 14 Strong Typing Defined By a static (source code) analysis of the program: 1. the type of every expression can be unambiguously determined; and 2. all type equivalence tests can be unambiguously decided. Pascal Type System Unstructured built-in: integer, real, char, boolean user-defined: enumerations and subranges Structured user-defined: record, array, set, file, Note: Pascal does not have abstract types. Algol 68 preceded Pascal, and was the first language to introduce user-defined types ( modes ). 15 16 Polymorphic Types: Motivation Pascal is an important milestones in PLs, because of its type system. But Pascal has first-order types, also known as monomorphic types. Many modern PLs have higher-order types, or polymorphic types. They also support abstract data types, a concept that was not fully developed when Pascal was invented. Polymorphic Types: Motivation Pascal is an important milestones in PLs, because of its type system. But Pascal has first-order types, also known as monomorphic types. Many modern PLs have higher-order types, or polymorphic types. They also support abstract data types, a concept that was not fully developed when Pascal was invented. 17 18

Why Mono Types not Enough int length(l) { int len = 0; while (l!== nil) { len := len + 1; l := l.next; } return len; } Desired type of length is: ( t) t List int 19 Polymorphic Types (cont d) Another example: List app(l1, l2) { if (l1 = nil) return l2 else return cons(l1.val, app(l1.next, l2)); } Desired type of app is: ( t) t List * t List t List 20 Polymorphic Types (cont d) The generic sorting procedure: void sort(var a: array[t]) { } might be given the type: ( t) t array void This type is not quite correct for sort. Robin Milner Inventor of ML Introduced polymorphic types and type inference Received ACM Turing Award 21 22 Characteristics of ML Strongly typed language (with type inference): - (parametric) polymorphic types - concrete type - abstract types Higher-order functional language: - expression-oriented (like Lisp) - rule-based definitions, with pattern matching - higher-order functions (output can also be function) - static scoping, with nested function definitions Modular language: - signatures, structures, and functors 23 Type variable ::= N-tuple ML Type System int real bool string unit * * { f 1 :,, f k : list ' id '' id user_defined_concrete_type user_defined_abstract_type 24 Function Type Record

Overloaded op r Type Name Operations Sample Literals int +, -, *, div, mod, ~2, ~1, 0, 1, 2 =, >, >=, <>, real +, -, *, /, ~ 3.14, 0.0, 3E2, 9.99... =, >, >=, <>, bool andalso, orelse, true, false if then else, =, <>, string size, ^, =, <>, "", "abcd", unit =, <> () short-circuit op r 25 negative literal ML is a Functional Language Program = set of function definitions No imperative features, especially no assignments and updating. Computation is essentially the reduction of expression to a value. Keywords: functions, types, values, expressions, reduction strategies, := 26 Type Inference Examples: fun f(x) = x + 1; int int Resolving overloaded operators fun f(x:int, y) = x*x + y; fun f(x,y) = (x+y, y*2.5); fun f(x,y) = x*x + y real*real real*real Unresolved overloaded operators + and * fun f(x, y:int) = x*x + y; fun f(x, y):int = x*x + y; fun f(x, y) = x*x + y:int; 27 28 if-then-else if <expr 1 > then <expr 2 > else <expr 3 > ( t) bool * t * t t Examples: fun f(x,y) = if x > y then x*2 else y*2; f: int*int int fun f(x,y) = if x > y then x*x else y*y; Unresolved overloaded ops 29 fact: int int Recursive Definitions fun fact(n) = if n=0 then 1 else n*fact(n-1); agrees with fact: int->int 30 n:int

Another recursive definition fun h(x,y) = if x then y else h(y,x); x:bool Hence, h: bool*bool bool y:bool Type inference is a process of propagating type information (bottom-up, top-down, and side-ways) in order to determine the types of all identifiers variables or functions. Type Inference - Remarks In the absence of overloaded operators, types for all identifiers in an ML program can be unambiguously determined without any type declarations (assuming no type errors). 31 32 Local Vars & Nested Scopes let <local_value_bindings> in <expression> let val a = 10; val b = f(a) in let val a = 20; val c = g(a, b) in (a+b) / (a - c) fun quad(a,b,c)= let val disc = b*b - 4.0*a*c; val den = 2.0*a in if disc = 0.0 then ~b/den else if disc > 0.0 then let val sq = sqrt(disc) in ((sq-b)/den, (~sq-b)/den) else "imaginary" 34 Concrete Data Types datatype root = imaginary one of real two of real*real; Types for the constructors: imaginary : root one : real -> root two : real * real -> root 35 fun quad(a,b,c)= let val disc = b*b - 4.0*a*c; val den = 2.0*a in if disc = 0.0 then one(-b/den) else if disc > 0.0 then let val sq = sqrt disc in two((sq-b)/den, (-sq-b)/den) else imaginary Almost correct, except for equality test 36

fun quad(a,b,c)= let val disc = b*b - 4.0*a*c; val den = 2.0*a; fun realeq(x,y) = abs(x-y) < 0.0001 in if realeq(disc,0.0) then one(-b/den) else if disc > 0.0 then let val sq = sqrt disc in two((sq-b)/den, (-sq-b)/den) else imaginary val quad = fn: real * real * real -> root 37 Using quad - quad(1.0, 5.0, 6.0); val it = two(~2.0, ~3.0) : root - quad(1.0, 2.0, 1.0); val it = one(~1.0) : root - quad(1.0, 1.0, 1.0); val it = imaginary : root 38 Pattern Matching fun nroots(imaginary) = 0 nroots(one(x)) = 1 nroots(two(x,y)) = 2; fun fib(1) = 1 fib(2) = 1 fib(n) = fib(n-1) + fib(n-2); root -> int int -> int Polymorphic Datatypes datatype 'a list = [ ] :: of 'a * a list; cons Constructors: [ ] : 'a list :: : 'a * 'a list 'a list 39 40 List Constants (literals) [1, 2, 3] = 1 :: 2 :: 3 :: [ ] : int list [ abc, def ] = abc :: de :: [ ] : string list [[1, 2],[3]] = ((1::2::[]) :: (3::[]) :: []) : int list list [1, apple, 3.14] badly typed list [1, [2], [1,2,3]] badly typed list Polymorphically Typed Functions fun length([ ]) = 0 length(h::t) = 1 + length(t); What is the type of length? - Output Type: int, since 0:int, and this agrees with the second case 1 + length(t); - Input Type: the terms [ ] and h::t have types a list and b list, but a=b since they must be compatible. Hence the type of length: a list int. 42

Polymorphic Functions (cont d) fun member(x, []) = false member(x, h::t) = if x=h then true else member(x,t) a * a list bool Note: The following is incorrect: More examples fun mystery([ ]) = [] mystery(h::t) = h @ mystery(t) Mystery type analysis: - input and output types must be lists; a list * a list -> a list - h @ mystery(t) indicates that h must be a list; Nonlinear Pattern Disallowed! fun member(x, []) = false member(x, x::t) = true member(x, y::t) = false mystery: a list list -> a list 44 43 A simple tree datatype datatype 'a tree= leaf of 'a node of 'a tree * 'a tree Sample tree literals: leaf(true) node(leaf(1), leaf(2)) node(node(leaf(3.5), leaf(4.5)), leaf(1.5)) node(node(leaf("ada"), leaf( C")), node(leaf( Java"), leaf( Prolog"))) a tree int anonymous variable fun depth(leaf(_)) = 0 depth(node(t1, t2)) = let val d1 = depth(t1); val d2 = depth(t2) in if d1>d2 then 1+d1 else 1+d2 45 46 The Quicksort Algorithm fun qsort([]) = [] qsort(h::t) = let val (l, r) = partition(h, t) in qsort(l) @ [h] @ qsort(r) fun partition(pivot, [ ]) = ([ ], [ ]) partition(pivot, h::t) = let val (l, r) = partition(pivot, t) in if h < pivot then (h::l, r) else (l, h::r) app unresolved overloaded op r fun partition(pivot, [ ]) = ([ ], [ ]) partition(pivot:int, h::t) = let val (l, r) = partition(pivot, t) in if h < pivot then (h::l, r) else (l, h::r) fun qsort([]) = [] qsort(h::t) = let val (l, r) = partition(h, t) in qsort(l) @ [h] @ qsort(r) int list int list must specify type 47 48