The Scala Programming Language Original Slides Donna Malayeri.
-
Upload
letitia-barber -
Category
Documents
-
view
221 -
download
0
Transcript of The Scala Programming Language Original Slides Donna Malayeri.
![Page 1: The Scala Programming Language Original Slides Donna Malayeri.](https://reader036.fdocuments.us/reader036/viewer/2022062408/56649e625503460f94b5dc9a/html5/thumbnails/1.jpg)
The Scala Programming Language
Original SlidesDonna Malayeri
![Page 2: The Scala Programming Language Original Slides Donna Malayeri.](https://reader036.fdocuments.us/reader036/viewer/2022062408/56649e625503460f94b5dc9a/html5/thumbnails/2.jpg)
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)); } }
![Page 3: The Scala Programming Language Original Slides Donna Malayeri.](https://reader036.fdocuments.us/reader036/viewer/2022062408/56649e625503460f94b5dc9a/html5/thumbnails/3.jpg)
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)) }
![Page 4: The Scala Programming Language Original Slides Donna Malayeri.](https://reader036.fdocuments.us/reader036/viewer/2022062408/56649e625503460f94b5dc9a/html5/thumbnails/4.jpg)
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)) }
![Page 5: The Scala Programming Language Original Slides Donna Malayeri.](https://reader036.fdocuments.us/reader036/viewer/2022062408/56649e625503460f94b5dc9a/html5/thumbnails/5.jpg)
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) }
![Page 6: The Scala Programming Language Original Slides Donna Malayeri.](https://reader036.fdocuments.us/reader036/viewer/2022062408/56649e625503460f94b5dc9a/html5/thumbnails/6.jpg)
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)) }
![Page 7: The Scala Programming Language Original Slides Donna Malayeri.](https://reader036.fdocuments.us/reader036/viewer/2022062408/56649e625503460f94b5dc9a/html5/thumbnails/7.jpg)
Why a new language?
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
![Page 8: The Scala Programming Language Original Slides Donna Malayeri.](https://reader036.fdocuments.us/reader036/viewer/2022062408/56649e625503460f94b5dc9a/html5/thumbnails/8.jpg)
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
![Page 9: The Scala Programming Language Original Slides Donna Malayeri.](https://reader036.fdocuments.us/reader036/viewer/2022062408/56649e625503460f94b5dc9a/html5/thumbnails/9.jpg)
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
![Page 10: The Scala Programming Language Original Slides Donna Malayeri.](https://reader036.fdocuments.us/reader036/viewer/2022062408/56649e625503460f94b5dc9a/html5/thumbnails/10.jpg)
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 ::
![Page 11: The Scala Programming Language Original Slides Donna Malayeri.](https://reader036.fdocuments.us/reader036/viewer/2022062408/56649e625503460f94b5dc9a/html5/thumbnails/11.jpg)
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
![Page 12: The Scala Programming Language Original Slides Donna Malayeri.](https://reader036.fdocuments.us/reader036/viewer/2022062408/56649e625503460f94b5dc9a/html5/thumbnails/12.jpg)
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
![Page 13: The Scala Programming Language Original Slides Donna Malayeri.](https://reader036.fdocuments.us/reader036/viewer/2022062408/56649e625503460f94b5dc9a/html5/thumbnails/13.jpg)
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)}
![Page 14: The Scala Programming Language Original Slides Donna Malayeri.](https://reader036.fdocuments.us/reader036/viewer/2022062408/56649e625503460f94b5dc9a/html5/thumbnails/14.jpg)
Scala class hierarchy
![Page 15: The Scala Programming Language Original Slides Donna Malayeri.](https://reader036.fdocuments.us/reader036/viewer/2022062408/56649e625503460f94b5dc9a/html5/thumbnails/15.jpg)
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
![Page 16: The Scala Programming Language Original Slides Donna Malayeri.](https://reader036.fdocuments.us/reader036/viewer/2022062408/56649e625503460f94b5dc9a/html5/thumbnails/16.jpg)
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;}
![Page 17: The Scala Programming Language Original Slides Donna Malayeri.](https://reader036.fdocuments.us/reader036/viewer/2022062408/56649e625503460f94b5dc9a/html5/thumbnails/17.jpg)
Traits
Similar to interfaces in Java They may have implementations of
methods But can’t contain state Can be multiply inherited from
![Page 18: The Scala Programming Language Original Slides Donna Malayeri.](https://reader036.fdocuments.us/reader036/viewer/2022062408/56649e625503460f94b5dc9a/html5/thumbnails/18.jpg)
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;}
![Page 19: The Scala Programming Language Original Slides Donna Malayeri.](https://reader036.fdocuments.us/reader036/viewer/2022062408/56649e625503460f94b5dc9a/html5/thumbnails/19.jpg)
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)}
![Page 20: The Scala Programming Language Original Slides Donna Malayeri.](https://reader036.fdocuments.us/reader036/viewer/2022062408/56649e625503460f94b5dc9a/html5/thumbnails/20.jpg)
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
val s: Set = xs;xs contains x
val s: Set = view(xs);view(xs) contains x
![Page 21: The Scala Programming Language Original Slides Donna Malayeri.](https://reader036.fdocuments.us/reader036/viewer/2022062408/56649e625503460f94b5dc9a/html5/thumbnails/21.jpg)
Variance annotations
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!
![Page 22: The Scala Programming Language Original Slides Donna Malayeri.](https://reader036.fdocuments.us/reader036/viewer/2022062408/56649e625503460f94b5dc9a/html5/thumbnails/22.jpg)
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}
![Page 23: The Scala Programming Language Original Slides Donna Malayeri.](https://reader036.fdocuments.us/reader036/viewer/2022062408/56649e625503460f94b5dc9a/html5/thumbnails/23.jpg)
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)
![Page 24: The Scala Programming Language Original Slides Donna Malayeri.](https://reader036.fdocuments.us/reader036/viewer/2022062408/56649e625503460f94b5dc9a/html5/thumbnails/24.jpg)
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
![Page 25: The Scala Programming Language Original Slides Donna Malayeri.](https://reader036.fdocuments.us/reader036/viewer/2022062408/56649e625503460f94b5dc9a/html5/thumbnails/25.jpg)
Sumary
Scala is a very regular language when it comes to composition:Everything can be nested: classes, methods, objects, types
1.Everything can be abstract: methods, values, types
2.The type of this can be declared freely, can thus express dependencies
3.This gives great flexibility for SW architecture, allows us to attack previously unsolvable problems
25
![Page 26: The Scala Programming Language Original Slides Donna Malayeri.](https://reader036.fdocuments.us/reader036/viewer/2022062408/56649e625503460f94b5dc9a/html5/thumbnails/26.jpg)
Going further: Parallel DSLsMid 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
26
![Page 27: The Scala Programming Language Original Slides Donna Malayeri.](https://reader036.fdocuments.us/reader036/viewer/2022062408/56649e625503460f94b5dc9a/html5/thumbnails/27.jpg)
EPFL / Stanford Research
Domain Embedding Language (Scala)
Virtual Worlds
Personal Robotics
Datainformatics
ScientificEngineering
Physics(Liszt)
ScriptingProbabilistic(RandomT)
Machine Learning(OptiML)
Rendering
Parallel Runtime (Delite, Sequoia, GRAMPS)
Dynamic Domain Spec. Opt. Locality Aware Scheduling
StagingPolymorphic Embedding
Applications
DomainSpecificLanguages
HeterogeneousHardware
DSLInfrastructure
Task & Data Parallelism
Hardware Architecture
OOO CoresOOO Cores SIMD CoresSIMD Cores Threaded CoresThreaded Cores Specialized CoresSpecialized Cores
Static Domain Specific Opt.
ProgrammableHierarchiesProgrammableHierarchies
Scalable CoherenceScalable Coherence
Isolation & AtomicityIsolation & Atomicity
On-chipNetworksOn-chipNetworks
Pervasive MonitoringPervasive Monitoring 27
![Page 28: The Scala Programming Language Original Slides Donna Malayeri.](https://reader036.fdocuments.us/reader036/viewer/2022062408/56649e625503460f94b5dc9a/html5/thumbnails/28.jpg)
Fuel injection
Transition Thermal
Turbulence
Turbulence
Combustion
Example: Liszt - A DSL for Physics Simulation
Mesh-based Numeric Simulation Huge domains
millions of cells Example: Unstructured
Reynolds-averaged Navier Stokes (RANS) solver 28
![Page 29: The Scala Programming Language Original Slides Donna Malayeri.](https://reader036.fdocuments.us/reader036/viewer/2022062408/56649e625503460f94b5dc9a/html5/thumbnails/29.jpg)
Liszt as Virtualized Scalaval // 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
Hardware
DSL Library
Optimisers Generators…
…Schedulers
GPU, Multi-Core, etc 29