CSE341: Programming Languages Lecture 11 Closures-ish Java & C Dan Grossman Fall 2011.

Slides:



Advertisements
Similar presentations
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 9 Delegates and Events.
Programming Languages Section 1 1 Programming Languages Section 1. SML Fundamentals Xiaojuan Cai Spring 2015.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
Object Orientation Chapter SixteenModern Programming Languages, 2nd ed.1 Spring 2012.
Object Orientation Chapter SixteenModern Programming Languages, 2nd ed.1.
CSE341: Programming Languages Lecture 27 Generics vs. Subtyping; Bounded Polymorphism Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 4 Records (“each of”), Datatypes (“one of”), Case Expressions Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 6 Tail Recursion, Accumulators, Exceptions Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 5 Pattern-Matching Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 3 Local bindings, Options, Benefits of No Mutation Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 7 Functions Taking/Returning Functions Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 24 Racket Modules, Abstraction with Dynamic Types; Racket Contracts Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Fall 2011.
Closure and Environment Compiler Baojian Hua
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
Programming LanguagesSection 31 Programming Languages Section 3. First-class Functions Xiaojuan Cai Spring 2015.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
An Advanced Code Pattern: Inner Classes CSE301 University of Sunderland Harry R. Erwin, PhD Half Lecture.
CSE 341 : Programming Languages Lecture 8 First Class Functions Zach Tatlock Spring 2014.
1.SML Docs Standard Basis 2.First-Class Functions Anonymous Style Points Higher-Order 3.Examples Agenda.
CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2017.
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 11 Type Inference
Agenda SML Docs First-Class Functions Examples Standard Basis
Agenda SML Docs First-Class Functions Examples Standard Basis
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2016.
Java Programming Language
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 11 Type Inference
CSE 341 Lecture 11 b closures; scoping rules
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
CSE341: Programming Languages Lecture 7 First-Class Functions
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 25 Subtyping for OOP; Comparing/Combining Generics and Subtyping Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2019.
Presentation transcript:

CSE341: Programming Languages Lecture 11 Closures-ish Java & C Dan Grossman Fall 2011

Higher-order programming Higher-order programming, e.g., with map and filter, is great Language support for closures makes it very pleasant Without closures, we can still do it more manually / clumsily –In OOP (e.g., Java) with one-method interfaces –In procedural (e.g., C) with explicit environment arguments Working through this: –Shows connections between languages and features –Can help you understand closures and objects Fall 20112CSE341: Programming Languages

Example in ML Fall 20113CSE341: Programming Languages datatype 'a mylist = Cons of 'a * ('a mylist) | Empty (* ('a -> 'b) -> 'a mylist -> 'b mylist *) fun map f xs = case xs of … (* ('a -> bool) -> 'a mylist -> 'a mylist *) fun filter f xs = case xs of … (* 'a mylist -> int *) fun length xs = case xs of … val doubleAll = map (fn x => x*2) val countNs xs = length (filter (fn x => x=n) xs)

Java Java 8 likely to have closures (like C#, Scala, Ruby, …) –Write like lst.map((x) => x.age).filter((x) => x > 21).length() –Make parallelism and collections much easier –Encourage less mutation –Hard parts for language designers: Implementation with other features and VM Evolving current standard library (else not worth it?) But how could we program in an ML style in Java today… –Won’t look like pseudocode above –Was even more painful before Java had generics Fall 20114CSE341: Programming Languages

One-method interfaces An interface is a named type [constructor] An object with one method can serve as a closure –Different instances can have different fields [possibly different types] like different closures can have different environments [possibly different types] So an interface with one method can serve as a function type Fall 20115CSE341: Programming Languages interface Func { B m(A x); } interface Pred { boolean m(A x); } interface Foo { String m(int x, int y); }

List types Creating a generic list class works fine –Assuming null for empty list here, a choice we may regret –null makes every type an option type with implicit valOf Fall 20116CSE341: Programming Languages class List { T head; List tail; List(T x, List xs){ head = x; tail = xs; } … }

Higher-order functions Let’s use static methods for map, filter, length Use our earlier generic interfaces for “function arguments” These methods are recursive –Less efficient in Java  –Much simpler than common previous-pointer acrobatics Fall 20117CSE341: Programming Languages static List map(Func f, List xs){ if(xs==null) return null; return new List (f.m(xs.head), map(f,xs.tail); } static List filter(Pred f, List xs){ if(xs==null) return null; if(f.m(xs.head)) return new List (xs.head), filter(f,xs.tail); return filter(f,xs.tail); } static length(List xs){ … }

Why not instance methods? A more OO approach would be instance methods: Can work, but interacts poorly with null for empty list –Cannot call a method on null –So leads to extra cases in all clients of these methods if a list might be empty An even more OO alternative uses a subclass of List for empty-lists rather than null –Then instance methods work fine! Fall 20118CSE341: Programming Languages class List { List map(Func f){…} List filter(Pred f){…} int length(){…} }

Clients To use map method to make a List from a List : –Define a class C that implements Func Use fields to hold any “private data” –Make an object of class C, passing private data to constructor –Pass the object to map As a convenience, can combine all 3 steps with anonymous inner classes –Mostly just syntactic sugar –But can directly access enclosing fields and final variables –Added to language to better support callbacks –Syntax an acquired taste? See lec11.java Fall 20119CSE341: Programming Languages

Now C [for C experts] In Java, objects, like closures, can have “parts” that do not show up in their types (interfaces) In C, a function pointer is just a code pointer, period –So without extra thought, functions taking function pointer arguments won’t be as useful as functions taking closures A common technique: –Always define function pointers and higher-order functions to take an extra, explicit environment argument –But without generics, no good choice for type of list elements or the environment Use void* and various type casts… Fall CSE341: Programming Languages

The C trick [ignore if not (yet) a C wizard; full implementation in lec11.c] Don’t do this: Do this to support clients that need private data: List libraries like this aren’t common in C, but callbacks are! –Lack of generics means lots of type casts in clients  Fall CSE341: Programming Languages list_t* map(void* (*f)(void*), list_t xs){ … f(xs->head) … } list_t* map(void* (*f)(void*,void*) void* env, list_t xs) { … f(env,xs->head) … }