The Scala Programming Language

Slides:



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

ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
More about functions Plus a few random things. 2 Tail recursion A function is said to be tail recursive if the recursive call is the very last thing it.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
1 CSC 551: Web Programming Spring 2004 client-side programming with JavaScript  scripts vs. programs  JavaScript vs. JScript vs. VBScript  common tasks.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Generic programming in Java
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Java Generics.
1 SWE Introduction to Software Engineering Lecture 23 – Architectural Design (Chapter 13)
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
AspectJ2EE/Clasa Israel Institute of Technology The Computer Science department Itay Maman.
1 Gentle Introduction to Programming Session 5: Memory Model, Object Oriented Programming.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Programming in Scala Chapter 1. Scala: both object-oriented and functional Scala blends –object-oriented and –functional programming in a –statically.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
JavaScript, Third Edition
System-Level Types for Component-Based Design Paper by: Edward A. Lee and Yuhong Xiong Presentation by: Dan Patterson.
Abstract Data Types and Encapsulation Concepts
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#
Introduction to Ruby CSE 413 Autumn 2008 Credit: Dan Grossman, CSE341.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Programming Languages and Paradigms Object-Oriented Programming.
Katanosh Morovat.   This concept is a formal approach for identifying the rules that encapsulate the structure, constraint, and control of the operation.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Static and Dynamic Behavior CMPS Power of OOP Derives from the ability of objects to change their behavior dynamically at run time. Static – refers.
The Scala Programming Language presented by Donna Malayeri.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
UMBC CMSC 331 Traits in Scala. 2 Basic Properties Traits are used to define object types by specifying the signature of the supported methods. Unlike.
Scala Technion – Institute of Technology Software Design (236700) Based on slides by: Sagie Davidovich, Assaf Israel Author: Gal Lalouche - Technion 2015.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Scala Overview Brandon Clopton CSCI 431. Scala fuses object-oriented and functional programming in a statically typed programming language. It is aimed.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Introduction to Scala Lecture 1 CMSC 331 SD Vick.
George Michelogiannakis, Prof. William J. Dally Concurrent architecture & VLSI group Stanford University Elastic Buffer Flow Control for On-chip Networks.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
A Little Language for Surveys: Constructing an Internal DSL in Ruby H. Conrad Cunningham Computer and Information Science University of Mississippi.
- Neeraj Chandra.  It’s language written by by Martin Odersky at EPFL  It’s language written by by Martin Odersky at EPFL (École Polytechnique Fédérale.
Chapter 6 Programming Languages © 2007 Pearson Addison-Wesley. All rights reserved.
E FFECTIVE C# 50 Specific Ways to Improve Your C# Second Edition Bill Wagner محمد حسین سلطانی.
Slide: 1 Copyright © AdaCore Subprograms Presented by Quentin Ochem university.adacore.com.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Specialized systems are  Inevitable  Already the norm  Practical.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Object Oriented Software Development
Types in programming languages1 What are types, and why do we need them?
Session 7 Methods Strings Constructors this Inheritance.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
The Scala API. Scala has a reputation of being a difficult language Some people feel that Scala is beyond what the average programmer can master Others.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
What’s Ahead for Embedded Software? (Wed) Gilsoo Kim
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
CMSC 330: Organization of Programming Languages Operational Semantics.
CSE 341 Section 10 Subtyping, Review, and The Future.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Subtying Kangwon National University 임현승 Programming Languages These slides were originally created by Prof. Sungwoo Park at POSTECH.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
Functional Programming
ML: a quasi-functional language with strong typing
Introduction to Scala SD Vick 1.
Java Primer 1: Types, Classes and Operators
Java Programming Language
The Scala API.
Presentation transcript:

The Scala Programming Language Original Slides Donna Malayeri

Java fibonocci import java.math.BigInteger; public class FiboJava { private static BigInteger fibo(int x) { BigInteger a = BigInteger.ZERO; BigInteger b = BigInteger.ONE; BigInteger c = BigInteger.ZERO; for (int i = 0; i < x; i++) { c = a.add(b); a = b; b = c; } return a; } public static void main(String args[]) { System.out.println(fibo(1000)); } }

Scala fibonacci object FiboScala extends App { def fibo(x: Int): BigInt = { var a: BigInt = 0 var b: BigInt = 1 var c: BigInt = 0 for (_ <- 1 to x) { c = a + b a = b b = c } return a } println(fibo(1000)) }

Scala functional fibonacci object FiboFunctional extends App { val fibs:Stream[BigInt] = 0 #:: 1 #:: (fibs zip fibs.tail).map{ case (a,b) => a+b } println(fibs(1000)) }

Qsort in Scala def qsort: List[Int] => List[Int] = { case Nil => Nil case pivot :: tail => val (smaller, rest) = tail.partition(_ < pivot) qsort(smaller) :: pivot :: qsort(rest) }

Higher Order Functions def apply(f: Int => String, v: Int) = f(v) class Decorator(left: String, right: String) { def layout[A](x: A) = left + x.toString() + right } object FunTest extends Application { def apply(f: Int => String, v: Int) = f(v) val decorator = new Decorator("[", "]") println(apply(decorator.layout, 7)) }

Why a new language? Adoption is key for testing this hypothesis Goal was to create a language with better support for component software Two hypotheses: Programming language for component software should be scalable The same concepts describe small and large parts Rather than adding lots of primitives, focus is on abstraction, composition, and decomposition Language that unifies OOP and functional programming can provide scalable support for components Adoption is key for testing this hypothesis Scala interoperates with Java and .NET

Features of Scala Scala is both functional and object-oriented every value is an object every function is a value--including methods Scala is statically typed includes a local type inference system

More features Supports lightweight syntax for anonymous functions, higher-order functions, nested functions, currying ML-style pattern matching Integration with XML can write XML directly in Scala program can convert XML DTD into Scala class definitions Support for regular expression patterns

Other features Allows defining new control structures without using macros, and while maintaining static typing Any function can be used as an infix or postfix operator Can define methods named +, <= or ::

Automatic Closure Construction Allows programmers to make their own control structures Can tag the parameters of methods with the modifier def When method is called, the actual def parameters are not evaluated and a no-argument function is passed

While loop example object TargetTest1 with Application { def loopWhile(def cond: Boolean)(def body: Unit): Unit = if (cond) { body; loopWhile(cond)(body) } var i = 10; loopWhile (i > 0) { Console.println(i); i = i - 1 Define loopWhile method Use it with nice syntax

Lazy Values case class Employee(id: Int, name: String, managerId: Int) { val manager: Employee = Db.get(managerId) val team: List[Employee] = Db.team(id) case class Employee(id: Int, name: String, managerId: Int) { lazy val manager: Employee = Db.get(managerId) lazy val team: List[Employee] = Db.team(id) }

Scala class hierarchy

Scala object system Class-based Single inheritance Can define singleton objects easily Subtyping is nominal Traits, compound types, mixin, and views allow for more flexibility Since traits do not encapsulate state, inheriting twice from a trait is legal in Scala.

Classes and Objects trait Nat; object Zero extends Nat { def isZero: boolean = true; def pred: Nat = throw new Error("Zero.pred"); } class Succ(n: Nat) extends Nat { def isZero: boolean = false; def pred: Nat = n; Traits are similar to interfaces in Java. Nat defines a trait that’s just a placeholder. Zero has only one instance, so it’s an object rather than a class.

Traits Similar to interfaces in Java They may have implementations of methods But can’t contain state Can be multiply inherited from

Example of traits trait Similarity { def isSimilar(x: Any): Boolean; def isNotSimilar(x: Any): Boolean = !isSimilar(x); } class Point(xc: Int, yc: Int) with Similarity { var x: Int = xc; var y: Int = yc; def isSimilar(obj: Any) = obj.isInstanceOf[Point] && obj.asInstanceOf[Point].x == x; Traits can have code, unlike Java interfaces, but can’t have any state associated with them. Scala allows multiple inheritance of traits

Views Defines a coercion from one type to another Similar to conversion operators in C++/C# trait Set { def include(x: int): Set; def contains(x: int): boolean } def view(list: List) : Set = new Set { def include(x: int): Set = x prepend xs; def contains(x: int): boolean = !isEmpty && (list.head == x || list.tail contains x)

Views Views are inserted automatically by the Scala compiler If e is of type T then a view is applied to e if: expected type of e is not T (or a supertype) a member selected from e is not a member of T Compiler uses only views in scope Suppose xs : List and view above is in scope Note that changes to the result returned by ‘view’ are not reflected in the original function. val s: Set = xs; xs contains x val s: Set = view(xs); view(xs) contains x

Variance annotations Array[String] is not a subtype of Array[Any] class Array[a] { def get(index: int): a def set(index: int, elem: a): unit; } Array[String] is not a subtype of Array[Any] If it were, we could do this: val x = new Array[String](1); val y : Array[Any] = x; y.set(0, new FooBar()); // just stored a FooBar in a String array!

Variance Annotations Covariance is ok with functional data structures trait GenList[+T] { def isEmpty: boolean; def head: T; def tail: GenList[T] } object Empty extends GenList[All] { def isEmpty: boolean = true; def head: All = throw new Error("Empty.head"); def tail: List[All] = throw new Error("Empty.tail"); class Cons[+T](x: T, xs: GenList[T]) extends GenList[T] { def isEmpty: boolean = false; def head: T = x; def tail: GenList[T] = xs

Variance Annotations Can also have contravariant type parameters Useful for an object that can only be written to Scala checks that variance annotations are sound covariant positions: immutable field types, method results contravariant: method argument types Type system ensures that covariant parameters are only used covariant positions (similar for contravariant)

Types as members abstract class AbsCell { type T; val init: T; private var value: T = init; def get: T = value; def set(x: T): unit = { value = x } } def createCell : AbsCell { new AbsCell { type T = int; val init = 1 } Clients of createCell cannot rely on the fact that T is int, since this information is hidden from them

Sumary Scala is a very regular language when it comes to composition: Everything can be nested: classes, methods, objects, types Everything can be abstract: methods, values, types The type of this can be declared freely, can thus express dependencies This gives great flexibility for SW architecture, allows us to attack previously unsolvable problems

Going further: Parallel DSLs Mid term, research project: How do we keep tomorrow’s computers loaded? How to find and deal with 10000+ threads in an application? Parallel collections and actors are necessary but not sufficient for this. Our bet for the mid term future: parallel embedded DSLs Find parallelism in domains: physics simulation, machine learning, statistics, ... Joint work with Kunle Olukuton, Pat Hanrahan @ Stanford

EPFL / Stanford Research Virtual Worlds Personal Robotics Data informatics Scientific Engineering Applications Physics (Liszt) Scripting Probabilistic (RandomT) Machine Learning (OptiML) Rendering Domain Specific Languages Domain Embedding Language (Scala) Polymorphic Embedding Staging Static Domain Specific Opt. DSL Infrastructure Parallel Runtime (Delite, Sequoia, GRAMPS) This leads to our vision, applications driven by a set of interoperable DSLs. We are developing DSLs to provide evidence as to their effectiveness in extracting parallel performance. But we are also very interested in empowering other to easily build such DSLs, so we are investing heavily in developing frameworks and runtimes to make parallel DSL development easier. And the goal is to run single source programs on a variety of very different hardware targets. Dynamic Domain Spec. Opt. Task & Data Parallelism Locality Aware Scheduling Hardware Architecture Heterogeneous Hardware OOO Cores SIMD Cores Threaded Cores Specialized Cores Programmable Hierarchies Scalable Coherence Isolation & Atomicity On-chip Networks Pervasive Monitoring 27

Example: Liszt - A DSL for Physics Simulation Fuel injection Transition Thermal Turbulence Combustion Mesh-based Numeric Simulation Huge domains millions of cells Example: Unstructured Reynolds-averaged Navier Stokes (RANS) solver Liszt is another language we have implemented. It is designed to support the creation of solvers for mesh-based partial differential equations. Problems in this domain typically simulate complex physical systems such as fluid flow or mechanics by breaking up space into discrete cells. A typical mesh may contain hundreds of millions of these cells (here we are visualizing a scram-jet designed to work at hypersonic speeds). Liszt is an ideal candidate for a DSL because while the problems are large and highly parallel, the mesh introduces many data-dependencies that are difficult to reason about, making writing solvers tedious.

Liszt as Virtualized Scala val // calculating scalar convection (Liszt) val Flux = new Field[Cell,Float] val Phi = new Field[Cell,Float] val cell_volume = new Field[Cell,Float] val deltat = .001 ... untilconverged { for(f <- interior_faces) { val flux = calc_flux(f) Flux(inside(f)) -= flux Flux(outside(f)) += flux } for(f <- inlet_faces) { Flux(outside(f)) += calc_boundary_flux(f) for(c <- cells(mesh)) { Phi(c) += deltat * Flux(c) /cell_volume(c) for(f <- faces(mesh)) Flux(f) = 0.f AST Optimisers Generators … Schedulers … Hardware DSL Library GPU, Multi-Core, etc