Overview. Elements of Programming Languages. Advanced constructs. Motivating inner class example



Similar documents
Tutorial on Writing Modular Programs in Scala

Analyse et Conception Formelle. Lesson 5. Crash Course on Scala

Scala Programming Language

Chapter 7: Functional Programming Languages

Moving from CS 61A Scheme to CS 61B Java

Chapter 15 Functional Programming Languages

Comp 411 Principles of Programming Languages Lecture 34 Semantics of OO Languages. Corky Cartwright Swarat Chaudhuri November 30, 20111

CSE 373: Data Structure & Algorithms Lecture 25: Programming Languages. Nicki Dell Spring 2014

Java (12 Weeks) Introduction to Java Programming Language

The Needle Programming Language

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Concepts and terminology in the Simula Programming Language

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Poor Man's Type Classes

Scala type classes and machine learning. David Andrzejewski Bay Area Scala Enthusiasts - 1/14/2013, 1/16/2013

Fundamentals of Java Programming

Semester Review. CSC 301, Fall 2015

Computer Programming I

Computer Programming I

AP Computer Science Java Subset

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

Programming Language Rankings. Lecture 15: Type Inference, polymorphism & Type Classes. Top Combined. Tiobe Index. CSC 131! Fall, 2014!

core. Volume I - Fundamentals Seventh Edition Sun Microsystems Press A Prentice Hall Title ULB Darmstadt

Introduction to Python

The D Programming Language

by Mario Fusco Monadic

Visual C# 2012 Programming

Dart a modern web language

Software Engineering Techniques

Introducing Variance into the Java Programming Language DRAFT

Programming Languages

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

The Scala Language Specification Version 2.9

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Object-Oriented Programming in Java

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

Taming Wildcards in Java s Type System

Exploring Algorithms with Python

2! Multimedia Programming with! Python and SDL

Programming Languages CIS 443

History OOP languages Year Language 1967 Simula Smalltalk

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

Fun with Phantom Types

Basic Programming and PC Skills: Basic Programming and PC Skills:

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Adding GADTs to OCaml the direct approach

Java SE 8 Programming

1.1 WHAT IS A PROGRAMMING LANGUAGE?

CSCI 3136 Principles of Programming Languages

POLYTYPIC PROGRAMMING OR: Programming Language Theory is Helpful

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

Big Data Frameworks: Scala and Spark Tutorial

Class 16: Function Parameters and Polymorphism

Advanced compiler construction. General course information. Teacher & assistant. Course goals. Evaluation. Grading scheme. Michel Schinz

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

#820 Computer Programming 1A

TypeScript for C# developers. Making JavaScript manageable

Getting Started with the Internet Communications Engine

Formal Engineering for Industrial Software Development

TOWARDS A GREEN PROGRAMMING PARADIGM FOR MOBILE SOFTWARE DEVELOPMENT

6.170 Tutorial 3 - Ruby Basics

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Ruby in the context of scientific computing

1. What are Data Structures? Introduction to Data Structures. 2. What will we Study? CITS2200 Data Structures and Algorithms

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

Functional Programming. Functional Programming Languages. Chapter 14. Introduction

Aspect-Oriented Programming

Web development... the server side (of the force)

A Scala Tutorial for Java programmers

Chapter 6: Programming Languages

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

[Refer Slide Time: 05:10]

CSE 307: Principles of Programming Languages

The C Programming Language course syllabus associate level

Redesigning the language for business logic in the Maconomy ERP system and automatic translation of existing code

Single Page Web App Generator (SPWAG)

The Clean programming language. Group 25, Jingui Li, Daren Tuzi

Architectural Analysis of Microsoft Dynamics NAV

Redesign and Enhancement of the Katja System

S4 Classes in 15 pages, more or less

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

Massachusetts Institute of Technology 6.005: Elements of Software Construction Fall 2011 Quiz 2 November 21, 2011 SOLUTIONS.

The Interface Concept

Java Interview Questions and Answers

Timber: A Programming Language for Real-Time Embedded Systems

Integrating Formal Models into the Programming Languages Course

CEC225 COURSE COMPACT

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

OpenACC 2.0 and the PGI Accelerator Compilers

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Functional Programming

Curriculum Map. Discipline: Computer Science Course: C++

Appendix... B. The Object Constraint

Transcription:

Overview Elements of Programming Languages Lecture 12: Object-oriented functional programming James Cheney University of Edinburgh November 6, 2015 We ve now covered: basics of functional and imperative programming (with semantics) basics of modular and OO programming (via Scala examples) Today, finish discussion of programming in the large : some more advanced OO constructs and how they co-exist with/support functional programming in Scala list comprehensions as an extended example Advanced constructs Motivating inner class example So far, we ve covered the basic OOP model (circa Java 1.0), plus some Scala-isms Modern languages extend this model in several ways We can define a structure (class/object/trait) inside another: As a member of the enclosing class (tied to a specific instance) or as a static member (shared across all instances) As a local definition inside a method As an anonymous local definition Java (since 1.5) and Scala support generics (parameterized types as well as polymorphic functions) Some languages also support mixins (e.g. Scala traits) A nested/inner class has access to the private/protected members of the containing class So, we can use nested classes to expose an interface associated with a specific object: class List<A> { private A head; private List<A> tail; class ListIterator<A> implements Iterator<A> {... (can access head, tail)

Classes/objects as members Local classes In Scala, classes and objects (and traits) can be nested arbitrarily class A { object B { var x = 1 scala> val a = new A object C {class D { var x = 1 scala> val d = new C.D class E { class F { var x = 1 scala> val e = new E scala> val f = new e.f A local class (Java terminology) is a class that is defined inside a method def foo(): Int = { var z = 1 class X { var x = z z = 2 return (new X).x scala> foo() res0: Int = 2 Anonymous classes/objects Given an interface or parent class, we can define an anonymous instance without giving it an explicit name In Java, called an anonymous local class In Scala, looks like this: abstract class Foo { def foo() : Int val foo1 = new Foo { def foo() = 42 We can also give a local name to the instance (useful since this may be shadowed) val foo2 = new Foo { self => val x = 42 def foo() = self.x Parameterized types As mentioned earlier, types can take parameters For example, List[A] has a type parameter A This is related to (but different from) polymorphism A polymorphic function (like map) has a type that is parameterized by a given type. A parameterized type (like List[_]) is a type constructor: for every type T, it constructs a type List[T].

Defining parameterized types In Scala, there are basically three ways to define parameterized types: In a type abbreviation (NB: multiple parameters) type Pair[A,B] = (A,B) in a (abstract) class definition abstract class List[A] case class Cons[A](head: A, tail: List[A]) extends List[A] in a trait definition trait Stack[A] {... Using parameterized types inside a structure The type parameters of a structure are implicitly available to all components of the structure. Thus, in the List[A] class, map, flatmap, filter are declared as follows: abstract class List[A] {... def map[b](f: A => B): List[B] def filter(p: A => Boolean): List[A] def flatmap[b](f: A => List[B]): List[B] // applies f to each element of this, // and concatenates results Parameterized types and subtyping By default, a type parameter is invariant That is, neither covariant nor contravariant To indicate that a type parameter is covariant, we can prefix it with + abstract class List[+A] // see tutorial 6 To indicate that a type parameter is contravariant, we can prefix it with - trait Fun[-A,+B] // see next few slides... Scala checks to make sure these variance annotations make sense! Type bounds Type parameters can be given subtyping bounds For example, in an interface (that is, trait or abstract class) I: type T <: C says that abstract type member T is constrained to be a subtype of C. This is checked for any module implementing I Similarly, type parameters to function definitions, or class/trait definitions, can be bounded: fun f[a <: C](...) =... class D[A <: C] {... Upper bounds A >: U are also possible...

Traits as mixins So far we have used Scala s trait keyword for interfaces (which can include type members, unlike Java) However, traits are considerably more powerful: Traits can contain fields Traits can provide ( default ) method implementations This means traits provide a powerful form of modularity: mixin composition Idea: a trait can specify extra fields and methods providing a behavior Multiple traits can be mixed in, with fewer of the problems of multiple inheritance Java 8 s support for default methods in interfaces also allows a form of mixin composition. Pay no attention to the man behind the curtain... Tastes great, and look at that shine! Shimmer is a floor wax! trait FloorWax { def clean(f: Floor) {... No, it s a delicious dessert topping! trait TastyDessertTopping { val calories = 1000 def addto(d: Dessert) { d.addcal(calories) In Scala, it can be both: object Shimmer extends FloorWax with TastyDessert {... Function types as interfaces Scala bills itself as a multi-paradigm or object-oriented, functional language How do the paradigms actually fit together? Some features, such as case classes, are more obviously object-oriented versions of functional constructs Until now, we have pretended pairs, λ-abstractions, etc. are primitives in Scala They aren t; and they need to be implemented in a way compatible with Java/JVM assumptions But how do they really work? Suppose we define the following interface: trait Fun[-A,+B] { // A contravariant, B covariant def apply(x: A): B This says: an object implementing Fun[A,B] has an apply method Note: This is basically the Function trait in the Scala standard library! Scala translates f(x) to f.apply(x) Also, {x: T => e is essentially syntactic sugar for new Function[Int,Int] {def apply(x:t) = e!

Iterators and collections in Java Java provides standard interfaces for iterators and collections interface Iterator<E> { boolean hasnext() E next()... interface Collection<E> { Iterator<E> iterator()... These allow programming over different types of collections in a more abstract way than indexed for loop Iterators and foreach loops Since Java 1.5, one can write the following: for(element x : coll) {... do stuff with x... Provided coll implements the Collection<Element> interface This is essentially syntactic sugar for: for(iterator<element> i = coll.iterator(); i.hasnext(); ) { Element x = i.next();... do stuff with x... foreach in Scala foreach in Scala Scala has a similar for construct (with slightly different syntax) for (x <- coll) {... do something with x... For example: scala> for (x <- List(1,2,3)) { println(x) 1 2 3 The construct for (x <- coll) yield e is syntactic sugar for: coll.foreach{x =>... do something with x... if x: T and coll has method foreach: (A => ()) => () Scala expands for loops before checking that coll actually provides foreach of appropriate type If not, you get a somewhat mysterious error message... scala> for (x <- 42) {println(x) <console>:11: error: value foreach is not a member of Int

Comprehensions: Mapping Comprehensions: Filtering Scala (in common with Haskell, Python, C#, F# and others) supports a rich comprehension syntax Example: scala> for(x <- List("a","b","c")) yield (x + "z") res0: List[Int] = List(az,bz,cz) This is shorthand for: List("a","b","c").map{x => x + "z" where map[b](f: A => B): List[B] is a method of List[A]. (In fact, this works for any object implementing such a method.) Comprehensions can also include filters scala> for(x <- List("a","b","c"); if (x!= "b")) yield (x + "z") res0: List[Int] = List(az,cz) This is shorthand for: List("a","b","c").filter{x => x!= "b".map{x => x + "z" where filter(f: A => Boolean): List[A] is a method of List[A]. Comprehensions: Multiple Generators Summary Comprehensions can also iterate over several lists scala> for(x <- List("a","b","c"); y <- List("a","b","c"); if (x!= y)) yield (x + y) res0: List[Int] = List(ab,ac,ba,bc,ca,cb) This is shorthand for: List("a","b","c").flatMap{x => List("a","b","c").flatMap{y => if (x!= y) List(x + y) else {Nil where flatmap(f: A => List[B]): List[B] is a method of List[A]. In the last few lectures we ve covered Modules and interfaces Objects and classes How they interact with subtyping, type abstraction and how they can be used to implement functional features (particularly in Scala) This concludes our tour of programming in the large (though there is much more that could be said) Next time: small step semantics and type soundness