Good Advice for Type-directed Programming Aspect-oriented Programming and Extensible Generic Functions Geoffrey Washburn [ ] Joint.

Slides:



Advertisements
Similar presentations
Sml2java a source to source translator Justin Koser, Haakon Larsen, Jeffrey Vaughan PLI 2003 DP-COOL.
Advertisements

Optional Static Typing Guido van Rossum (with Paul Prescod, Greg Stein, and the types-SIG)
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function can return.
Type Analysis and Typed Compilation Stephanie Weirich Cornell University.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Abstraction and Modular Reasoning for the Verification of Software Corina Pasareanu NASA Ames Research Center.
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function F can return.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
WEAVING CODE EXTENSIONS INTO JAVASCRIPT Benjamin Lerner, Herman Venter, and Dan Grossman University of Washington, Microsoft Research.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Type inference and modern type systems Stephanie Weirich University of Pennsylvania.
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function can return.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Data Abstraction COS 441 Princeton University Fall 2004.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Introduction to ML You will be responsible for learning ML on your own. Today I will cover some basics Read Robert Harper’s notes on “an introduction to.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions, Polymorphism.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 7: Polymorphism.
Scrap your boilerplate with class Ralf Lämmel, Simon Peyton Jones Microsoft Research.
Harmless Advice Daniel S Dantas Princeton University with Prof. David Walker.
Semantics for MinML COS 441 Princeton University Fall 2004.
Abstract Data Types and Encapsulation Concepts
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
A Formal Model of Modularity in Aspect-Oriented Programming Jonathan Aldrich : Objects and Aspects Carnegie Mellon University.
Generic Programming with Dependent Types Stephanie Weirich University of Pennsylvania.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Programming Languages and Paradigms Object-Oriented Programming.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Aspect Oriented Programming Sumathie Sundaresan CS590 :: Summer 2007 June 30, 2007.
CSE 341 Programming Languages Racket Datatype Style Programming Zach Tatlock Spring 2014.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
AOP-1 Aspect Oriented Programming. AOP-2 Aspects of AOP and Related Tools Limitation of OO Separation of Concerns Aspect Oriented programming AspectJ.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Object-Oriented Design CSC 212. Announcements This course is speeding up and we are starting new material. Please see me if you feel this is going too.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University IWPSE 2003 Program.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Copyright © 2009 – Curt Hill Standard Template Library An Introduction.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Heath Carroll Bill Hanczaryk Rich Porter.  A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the.
CSE341: Programming Languages Lecture 11 Type Inference
Chapter 5 - Functions Outline 5.1 Introduction
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
CSE-321 Programming Languages Introduction to Functional Programming
CSE341: Programming Languages Lecture 11 Type Inference
CSE 341 Lecture 11 b closures; scoping rules
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
Presentation transcript:

Good Advice for Type-directed Programming Aspect-oriented Programming and Extensible Generic Functions Geoffrey Washburn [ ] Joint work with Stephanie Weirich [ ]

Good Advice for Type-directed Programming 2006/09/16 2  Type-directed programming is a form of generic programming.  operation determined by the “shape” of data  many applications (serialization, iteration, etc…)  improves reusability  Key idea: AOP is compelling mechanism for specializing type- directed functions. Introduction

Good Advice for Type-directed Programming 2006/09/16 3 Outline  Aspect-oriented programming in Aspect ML.  Type-directed programming in Aspect ML.  Specializing type-directed operations via advice.  Comparison of generic extension via aspects with “Scrap Your Boilerplate With Class”.

Good Advice for Type-directed Programming 2006/09/16 4 AOP in Aspect ML  Aspects cut across the boundaries of other sorts of abstractions.  Aspects are coherent collections of advice.  Advice specifies when and where to perform a computation.

Good Advice for Type-directed Programming 2006/09/16 5 val f : Int → Bool val g : Int → Int (* trace the argument to f & g *) advice before (|f,g|) (in:Int,stk,info): Int = print ((getFunName info) ^ “ ” ^ (intToString in)); in advice is a declaration form. “where” the advice applies: functions f and g “when” the advice applies: before f and g are called the argument that was passed to f and g advice also gets the current call stack and function metadata. Example advice

Good Advice for Type-directed Programming 2006/09/16 6 around f:a→b before after in out advice after (|f|) (out:b, stk, info) : b = … proceed advice around (|f|) (in:a, stk, info) : b = … proceed … advice before (|f|) (in:a, stk, info) : a = … Advice times

Good Advice for Type-directed Programming 2006/09/16 7  “where” is specified by a pointcut.  sets of in-scope function identifiers, (|f 1,…,f n |)  the any pointcut  “when” is specified by a time: before, after, or around.  Syntax of advice: advice time expression (x,y,z)= expression Advice in Aspect ML

Good Advice for Type-directed Programming 2006/09/16 8 Aspect ML is dynamic  advice installed during program execution  fun init_module () = let advice before … advice after … in … end  advice has global scope  installation order determines execution order  pointcuts are first-class values  let val x = (| f, g |) advice before x …

Good Advice for Type-directed Programming 2006/09/16 9 val f : Int → Bool val g : Int → Int (* trace the return value of f & g *) advice after (|f,g|) (out:???,stk,info):??? = print ((getFunName info) ^ “ ” ^ (???)); in Polymorphic advice  Return type could either be Bool or Int.

Good Advice for Type-directed Programming 2006/09/16 10  Solution: polymorphic pointcuts and advice  In general pointcuts have a type pc ( σ 1 →σ 2 ).  σ 1 corresponds to argument type of before and around advice.  σ 2 corresponds to argument type of after advice.  Type of (|f,g|) is pc ( Int→a).  Int→a is least common instance of Int→Bool and Int→Int. Polymorphic pointcuts

Good Advice for Type-directed Programming 2006/09/16 11 (* trace the return value of f & g *) advice after (|f,g|) (out:a,stk,info):a = print ((getFunName info) ^ “ ” ^ (???)); in (* trace the return value of f & g *) advice after (|f,g|) (out:a,stk,info):a = print ((getFunName info) ^ “ ” ^ (typecase a of | Bool ⇒ boolToString out | Int ⇒ intToString out)); in a = Bool a = Int Type analysis

Good Advice for Type-directed Programming 2006/09/16 12 (* trace the return value of f & g *) advice after (|f,g|) (out:a,stk,info):a = print ((getFunName info) ^ “ ” ^ (typecase a of | Bool ⇒ boolToString out | Int ⇒ intToString out)); in (* type-directed serialization *) fun toString (x : a) : String = typecase a of | Bool ⇒ boolToString x | Int ⇒ intToString x | ??? Generic programming

Good Advice for Type-directed Programming 2006/09/16 13  Need generic way to handle arbitrary data constructors.  Adapt spines, as developed by Hinze, Löh, and Oliveira. datatype Spine = | SCons : a → Spine a | SApp : Spine (a → b) → a → Spine b  Spines are a generic representation of data type structure Data type generic programming

Good Advice for Type-directed Programming 2006/09/16 14  Aspect ML primitive toSpine : a → Option (Spine a).  Spines are recursive in argument, not data type. datatype Foo = Bar : Int → Bool → Char → Foo toSpine (Bar 3 True ‘a’) ⇒ Some (SApp (SApp (SApp (SCons Bar) 3) True) ‘a’) Creating spines SApp SCons ‘a’BarTrue SApp 3

Good Advice for Type-directed Programming 2006/09/16 15  Only produces Spine if applied to a data constructor.  toSpine 1 ⇒ None List spine toSpine [1,2] ⇒ Some (SApp (SApp (SCons ::) 1) [2]) SAppSCons ::[2] SApp 1

Good Advice for Type-directed Programming 2006/09/16 16 (* type-directed serialization *) fun toString (x: a) : String = typecase a of | Bool ⇒ boolToString x | Int ⇒ intToString x | (b → c) ⇒ “ ” | _ ⇒ (case (toSpine x) | Some x’ ⇒ spineToString x’ | None ⇒ raise Error) and spineToString (x: Spine a) : String = case x of | SCons c ⇒ consToString c | SApp spn arg ⇒ “(“ ^ (spineToString spn) ^ “ ” ^ (toString arg) ^ “)” Total type-directed function

Good Advice for Type-directed Programming 2006/09/16 17  toString works for all values now.  But toString [1,2] produces “(((::) 1) ((:: 2) Nil)))”.  Would like to override the default printing behavior for lists.  Unrealistic to edit toString every time we want to refine the behavior.  Need some kind of specialization mechanism. Overriding defaults

Good Advice for Type-directed Programming 2006/09/16 18 (* extend toString for lists *) advice around (|toString|) (in:a,stk,info)= typecase a of | [b] ⇒ “[” ^ (concat “,” (map toString in)) ^ “]”) | _ ⇒ proceed in) (* extend toString for lists *) case-advice around (|toString|) (in:[a],stk,info) = “[” ^ (concat “,” (map toString in)) ^ “]”) Overriding with advice

Good Advice for Type-directed Programming 2006/09/16 19  Pay as you go: original function does not need to be designed for specialization in advance.  Specialized may occur in separate modules from definition.  Separation of function author from data type author.  Specialization without access to the source code.  Specialization at run-time, by dynamically loaded code. Benefits of using advice

Good Advice for Type-directed Programming 2006/09/16 20 Related approaches to extensibility  How do aspects compare for generic extension with type-classes, ala “Scrap Your Boilerplate with Class”?  Type-classes traditionally a very static mechanism, while aspects in Aspect ML are very dynamic.  Trade-off: more aggressive optimization may be possible with type-classes versus dynamic extension.

Good Advice for Type-directed Programming 2006/09/16 21  Each type-directed operation defined as type-class. class Show a where show :: a → String  Default type-directed operation is implemented by SYB library using explicit dictionary. instance Data ShowD t ⇒ Show t where show v = showConstr (toConstr v) ++ (concat “ “ (gmapQ showPrxy (showD dict) v))  Specialized behavior by specifying an instance for a type. instance Show a ⇒ Show [a] where show xs = “[” ++ (concat “,” (map show xs)) ++ “]” Scrapping your boilerplate with class

Good Advice for Type-directed Programming 2006/09/16 22 Problems with existentials  Can’t directly write a specialized case instance Show Exists where show (Ex (x :: a)) = “pack “ ++ (show x)  Ill typed because don’t know if a has instance for Show.  Could rewrite data type as data Exists = Ex :: forall a. Show a ⇒ a → Exists  Requires unsupported compiler extension.  Unrealistic to modify Exists for every type-directed operation. data Exists = Ex :: forall a. a → Exists

Good Advice for Type-directed Programming 2006/09/16 23  Type-classes an entirely compile-time mechanism, not possible to construct new instances at runtime.  Aspect ML can install aspects at any time.  Useful when working with mobile or dynamically loaded code that may export new data types. More comparison

Good Advice for Type-directed Programming 2006/09/16 24 More comparison  Type-class constraints in SYB can become complicated for the user, over- constrained, non-terminating, or unsolvable in practice.  Type-classes have the advantage of enforcing and describing the domain of type-directed functions.

Good Advice for Type-directed Programming 2006/09/16 25 Summary  Advice can be used to specify when and where a computation should occur.  Aspects are symbiotic with type analysis.  Writing extensible type-directed operations with advice avoids limitations of type-class based SYB.

Good Advice for Type-directed Programming 2006/09/16 26 The future  Using information-flow to reason about the use of type analysis and aspects.  Extending Aspect ML implementation.  Writing large scale software with these techniques.  Ask me if you would like a demo or snapshot of Aspect ML.