Bibliography. Analyse et Conception Formelle. Lesson 5. Crash Course on Scala. Scala in a nutshell. Outline

Similar documents
G Programming Languages - Fall 2012

Programming in Scala Second Edition

n n Official Scala website n Scala API n

Notes from a Short Introductory Lecture on Scala (Based on Programming in Scala, 2nd Ed.)

CS 2340 Objects and Design - Scala

Scala. Fernando Medeiros Tomás Paim

CS205: Scalable Software Systems

Introduction to Programming Using Java (98-388)

...something useful to do with the JVM.

Imperative Programming 2: Traits

Life in a Post-Functional World

CS Programming Languages: Scala

An Introduction to Scala for Spark programming

Scala : an LLVM-targeted Scala compiler

Programs as Data. The Scala language

CSE 130, Fall 2006: Final Examination

Programming Languages and Techniques (CIS120)

Advanced Object-Oriented Languages Scala

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

Topic VIII. The state of the art Scala < > Scala By Example by M. Odersky. Programming Methods Laboratory, EPFL, 2008.

University of Tartu Faculty of Mathematics and Computer Science Institute of Computer Science

Scala - Some essential concepts -

CS558 Programming Languages

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

CompSci 220. Programming Methodology 12: Functional Data Structures

Announcements. CSCI 334: Principles of Programming Languages. Lecture 16: Intro to Scala. Announcements. Squeak demo. Instructor: Dan Barowy

CS558 Programming Languages

CS205: Scalable Software Systems

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

Using Scala in CS241

Encoding Scala Programs for the Boogie Verifier. Valentin Wüstholz

INF121: Functional Algorithmic and Programming

CS558 Programming Languages

CMSC 330: Organization of Programming Languages. Rust Basics

Overview. Elements of Programming Languages. Objects. Self-Reference

High-level Parallel Programming: Scala on the JVM

Values (a.k.a. data) representation. Advanced Compiler Construction Michel Schinz

Values (a.k.a. data) representation. The problem. Values representation. The problem. Advanced Compiler Construction Michel Schinz

CSCI-GA Scripting Languages

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

The fringe of a binary tree are the values in left-to-right order. For example, the fringe of the following tree:

CS558 Programming Languages

Swift. Introducing swift. Thomas Woodfin

CSC Java Programming, Fall Java Data Types and Control Constructs

Linked lists and the List class

An Overview of Scala. Philipp Haller, EPFL. (Lots of things taken from Martin Odersky's Scala talks)

Compilers and computer architecture: Semantic analysis

CSCC24 Functional Programming Typing, Scope, Exceptions ML

Semantic Processing (Part 2)

The SCAlable LAnguage

SECOND EDITION SOURCE CODE ONLINE

Programming Kotlin. Familiarize yourself with all of Kotlin s features with this in-depth guide. Stephen Samuel Stefan Bocutiu BIRMINGHAM - MUMBAI

Overview. Elements of Programming Languages. Another example. Consider the humble identity function

Introduction to Scala

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

CS 251 Intermediate Programming Java Basics

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Java Object Oriented Design. CSC207 Fall 2014

Optimizing Higher-Order Functions in Scala

An introduction introduction to functional functional programming programming using usin Haskell

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

CSCI 3155: Homework Assignment 3

Functional programming

INTRODUCTION. SHORT INTRODUCTION TO SCALA INGEGNERIA DEL SOFTWARE Università degli Studi di Padova

Type Conversion. and. Statements

Programming Paradigms, Fall 06

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Overview. Elements of Programming Languages. Objects. Self-Reference

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

A Whirlwind Tour of Scala. Kevin Kilroy October 2014

Static type safety guarantees for the operators of a relational database querying system. Cédric Lavanchy

Scala. Introduction. Scala

Kotlin, Start? Start! (pluu) Android Developer GDG Korea Android Organizer

Arne Brüsch Philipp Wille. Pattern Matching Syntax

Learning Scala Seminar 2

Scala, Your Next Programming Language

Recap: Functions as first-class values

Lab 9: More Sorting Algorithms 12:00 PM, Mar 21, 2018

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Practical Scala. Dianne Marsh Emerging Technology for the Enterprise Conference 03/26/2009

CSCI-GA Final Exam

GADTs. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 7. [Faculty of Science Information and Computing Sciences]

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Type Checking and Type Equality

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

GADTs. Alejandro Serrano. AFP Summer School. [Faculty of Science Information and Computing Sciences]

JAVA MOCK TEST JAVA MOCK TEST II

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

Lecture #23: Conversion and Type Inference

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Semantic Processing (Part 2)

A π-calculus Internal Domain-Specic Language for Scala

An introduction to functional programming. July 23, 2010

Parallel Operations on Collections. Parallel Programming in Scala Viktor Kuncak

The type checker will complain that the two branches have different types, one is string and the other is int

Adding support for CLR Generics

Transcription:

Bibliography Analyse et Conception Formelle Lesson 5 Crash Course on Scala Simply Scala. Onlinetutorial: http://www.simply.com/fr http://www.simply.com/ Programming in Scala, M. Odersky, L. Spoon, B. Venners. Artima. http://www.artima.com/pins1ed/index.html An Overview of the Scala Programming Language, M. Odersky & al. http://www.-lang.org/docu/files/scalaoverview.pdf Scala web site. http://www.-lang.org Acknowledgements Many thanks to J. Noyé and J. Richard-Foy for providing material, answering questions and for fruitful discussions. T. Genet (ISTIC/IRISA) ACF-5 1/ 36 T. Genet (ISTIC/IRISA) ACF-5 2/ 36 Scala in a nutshell Scalable language : small scripts to architecture of systems Designed by Martin Odersky at EPFL I I Programming language expert One of the designers of the current Java compiler Pure object model: only objects and method calls ( = Java) With functional programming: higher-order, pattern-matching,... Fully interoperable with Java (in both directions) Concise smart syntax ( = Java) A compiler and a read-eval-print loop integrated into the IDE (select expression and type CTRL+SHIFT+X) T. Genet (ISTIC/IRISA) ACF-5 3/ 36 T. Genet (ISTIC/IRISA) ACF-5 4/ 36

Base types and type annotations 1:Int, "toto":string, a :Char, ():Unit Every data is an object, including base types! e.g. 1 is an object and Int is its class Every access/operation on an object is a method call! e.g. 1 + 2 executes: 1.+(2) (o.x(y) is equivalent to o x y) Exercise 1 Use the max(int) method of class Int to compute the maximum of 1+2 and 4. T. Genet (ISTIC/IRISA) ACF-5 5/ 36 T. Genet (ISTIC/IRISA) ACF-5 6/ 36 Class hierarchy Class hierarchy Any Subtype Implicit Conversion AnyVal ScalaObject AnyRef «java.lang.object» Double Unit Float Long Int Boolean Char Iterable Seq List java.lang String... (other Java classes)... Any Subtype Implicit Conversio Short Byte... (other Scala classes)... Null AnyVal ScalaObject AnyRef «java.lang.object» Nothing T. Genet (ISTIC/IRISA) ACF-5 7/ 36 a at Unit Boolean java.lang Iterable Strin T. Genet (ISTIC/IRISA) ACF-5 8/ 36

Class hierarchy Class hierarchy AnyVal ScalaObj ScalaObject AnyRef «java.lang.object» Double Float Long Int Unit Boolean Char Iterable Seq a it Iterable Seq List... (other Scala classes)... java.lang String... (other Java classes)... Short Byte T. Genet (ISTIC/IRISA) ACF-5 9/ 36 Null T. Genet (ISTIC/IRISA) ACF-5 10 / 36 Char Class hierarchy Seq List... (other Scala classes)...... (other Java classes)... val and var val associates an object to an identifier and cannot be reassigned var associates an object to an identifier and can be reassigned Scala philosophy is to use val instead of var whenever possible Types are (generally) automatically inferred Null > val x=1 // or val x:int = 1 x: Int = 1 Nothing > x=2 <console>:8: error: reassignment to val x=2 ˆ > var y=1 y: Int = 1 > y=2 y: Int = 2 T. Genet (ISTIC/IRISA) ACF-5 11 / 36 T. Genet (ISTIC/IRISA) ACF-5 12 / 36

if expressions Syntax is similar to Java if statements... but that they are not statements but typed expressions if ( condition ) e1 else e2 Remark: the type of this expression is the supertype of e1 and e2 if ( condition ) e1 Remark: the type of this expression is the supertype of e1 and Unit Exercise 2 What are the results and types of the corresponding if expressions: if (1==2) 1 else 2 if (1==2) 1 else "toto" if (1==1) 1 if (1==1) println(1) T. Genet (ISTIC/IRISA) ACF-5 13 / 36 match - case expressions Replaces (and extends) the usual switch - case construction The syntax is the following: e match { case pattern1 => r1 //patterns can be constants case pattern2 => r2 //or terms with variables... //or terms with holes: case => rn Remark: the type of this expression is the supertype of r1, r2,... rn Example 1 (Match-case expressions) x match { case "bonjour" => "hello" case "au revoir" => "goodbye" case => "don t know" T. Genet (ISTIC/IRISA) ACF-5 14 / 36 for loops Arrays: Array[A] for ( ident <- s ) e Remark: s has to be a subtype of Traversable (Arrays, Collections, Tables, Lists, Sets, Ranges,... ) Usual for-loops can be built using.to(...) (1).to(5) 1 to 5 results in Range(1, 2, 3, 4, 5) Exercise 3 Using for and println print the square of all elements contained in a set sb defined by val sb=set(1,2,3,4,5). Exercise 4 Using for and println build a usual 10 10 multiplication table. Array definition val t:array[string]= new Array[String](3) Using type inference results in less verbose expressions val t= new Array[String](3) Array update is standard (arrays are mutable objects) t(0)="zero"; t(1)="un"; t(2)="deux" Using type inference and simplified constructor may be handy: val t= Array("zero","un","deux") T. Genet (ISTIC/IRISA) ACF-5 15 / 36 T. Genet (ISTIC/IRISA) ACF-5 16 / 36

(Immutable) Lists: List[A] List definition (with type inference) val l= List(1,2,3,4,5) Adding an element to the head of a list val l1= 0::l Adding an element to the queue of a list val l2= l1:+6 Concatenating lists val l3= l1++l2 Getting the element at a given position val x= l2(2) Doing pattern-matching over lists l2 match { case Nil => 0 case e:: => e T. Genet (ISTIC/IRISA) ACF-5 17 / 36 (Immutable) Tuples : (A,B,C,...) Tuple definition (with type inference) > val t= (1,"toto",18.3) t: (Int, String, Double) = (1,toto,18.3) Tuple getters: t. 1, t. 2, etc.... or with match - case: t match { case (2,"toto", ) => "found!" case (,x, ) => x The above expression evaluates in "toto" T. Genet (ISTIC/IRISA) ACF-5 18 / 36 (Immutable) maps : Map[A,B] Map definition (with type inference) val m= Map( C -> "Carbon", H -> "Hydrogen") Remark: inferred type of m is Map[Char,String] Map application (finding the element associated to a key in a map) m( C ) Adding an association in a map val m1= m+( O -> "Oxygen") Catching the exception when the element is not found: try m1( K ) catch { case : NoSuchElementException => "Unknown" A Map[A,B] can be traversed as a Collection of Tuple[A,B] Exercise 5 Print all the keys of map m1 T. Genet (ISTIC/IRISA) ACF-5 19 / 36 T. Genet (ISTIC/IRISA) ACF-5 20 / 36

def f ( arg1: Type1,..., argn:typen ): Typef = { e Remark 1: type of e (the type of the last expression of e) is Typef Remark 2: Typef can be inferred for non recursive functions Remark 3: The type of f is : (Type1,...,Typen) => Typef Example 2 def plus(x:int,y:int):int={ println("sum of "+x+" and "+y+" is equal to "+(x+y)) x+y // no return keyword // the result of the function is the last expression Exercise 6 Using a map, define a phone book and the functions addname(name:string,tel:string), gettel(name:string), getuserlist:list[string] and gettellist:list[string]. T. Genet (ISTIC/IRISA) ACF-5 21 / 36 Anonymous functions and Higher-order functions The anonymous Scala function adding one to x is: ((x:int) => x + 1) Remark: it is written ( x. x + 1) in Isabelle/HOL A higher order function takes a function as a parameter e.g. method/function map called on a List[A] takes a function (A =>B) and results in a List[B] > val l=list(1,2,3) l: List[Int] = List(1, 2, 3) > l.map ((x:int) => x+1) res1: List[Int] = List(2, 3, 4) Exercise 7 Using map and the capitalize method of the class String, define the capuserlist function returning the list of capitalized user names. T. Genet (ISTIC/IRISA) ACF-5 22 / 36 Partial application The symbol permits to partially apply a function e.g. gettel( ) returns the function associated to gettel Example 3 (Other examples of partial application) ( :String).size ( :Int) + ( :Int) ( :String) == "toto" Exercise 8 Using map and partial application on capitalize, redefine the function capuserlist. Exercise 9 Using the higher order function filter on Lists, define a function above(n:string):list(string) returning the list of users having a capitalized name greater to name n. T. Genet (ISTIC/IRISA) ACF-5 23 / 36 T. Genet (ISTIC/IRISA) ACF-5 24 / 36

class C(v1: type1,..., vn:typen) {... the primary constructor e.g. class Rational(n:Int,d:Int){ val num=n // can use var instead val den=d // to have mutable objects def isnull:boolean=(num==0) Objects instances can be created using new: val r1= new Rational(3,2) Exercise 10 Complete the Rational class with an add(r:rational):rational function. T. Genet (ISTIC/IRISA) ACF-5 25 / 36 Overriding, operator definitions and implicit conversions Overriding is explicit: override def f(...) Exercise 11 Redefine the tostring method of the Rational class. All operators +, *, ==, >,... can be used as function names e.g. def +(x:int):int=... Remark: when using the operator recall that x.+(y) x + y Exercise 12 Define the + and * operators for the class Rational. It is possible to define implicit (automatic) conversions between types e.g. implicit def bool2int(b:boolean):int= if b 1 else 0 Exercise 13 Add an implicit conversion from Int to Rational. T. Genet (ISTIC/IRISA) ACF-5 26 / 36 Traits Traits stands for interfaces (as in Java) trait IntQueue { def get:int def put(x:int) The keyword extends defines trait implementation class MyIntQueue extends IntQueue{ private var b= List[Int]() def get= {val h=b(0); b=b.drop(1); h def put(x:int)= {b=b:+x Singleton objects Singleton objects are defined using the keyword object trait IntQueue { def get:int def put(x:int) object InfiniteQueueOfOne extends IntQueue{ def get=1 def put(x:int)={ A singleton object does not need to be created by new InfiniteQueueOfOne.put(10) InfiniteQueueOfOne.put(15) val x=infinitequeueofone.get T. Genet (ISTIC/IRISA) ACF-5 27 / 36 T. Genet (ISTIC/IRISA) ACF-5 28 / 36

Type abstraction and Polymorphism Parameterized function/class/trait can be defined using type parameters trait Queue[T]{ def get:t def push(x:t) class MyQueue[T] extends Queue[T]{ protected var b= List[T]() // more generic than IntQueue def get={val h=b(0); b=b.drop(1); h def put(x:t)= {b=b:+x def first[t1,t2](pair:(t1,t2)):t1= pair match case (x,y) => x Case classes Case classes provide a natural way to encode Algebraic Data Types e.g. binary expressions built over rationals: 18 27 + ( 1 2 ) trait Expr case class BinExpr(o:String,l:Expr,r:Expr) extends case class Constant(r:Rational) extends Expr case class Inv(e:Expr) extends Expr Instances of case classes are built without new e.g. the object corresponding to 18 27 + ( 1 2 ) is built using: BinExpr("+",Constant(new Rational(18,27)), Inv(Constant(new Rational(1,2)))) Expr T. Genet (ISTIC/IRISA) ACF-5 29 / 36 T. Genet (ISTIC/IRISA) ACF-5 30 / 36 trait Expr case class BinExpr(o:String,l:Expr,r:Expr) extends Expr case class Constant(r:Rational) extends Expr case class Inv(e:Expr) extends Expr match case can directly inspect objects built with case classes def getoperator(e:expr):string= { e match { case BinExpr(o,_,_) => o case _ => "No operator" Exercise 14 Define an eval(e:expr):rational function computing the value of any expression. T. Genet (ISTIC/IRISA) ACF-5 31 / 36 T. Genet (ISTIC/IRISA) ACF-5 32 / 36

Interoperablity between Java and Scala In Scala, it is possible to build objects from Java classes e.g. val txt:jtextarea=new JTextArea("") And to define classes/objects implementing Java interfaces e.g. object Window extends JFrame There exists conversions between Java and Scala data structures import.collection.javaconverters._ val l1:java.util.list[int]= new java.util.arraylist[int]() l1.add(1); l1.add(2); l1.add(3) // l1: java.util.list[int] val sb1= l1.asscala.tolist val sl1= sb1.asjava // sl1: List[Int] // sl1: java.util.list[int] Remark: it is also possible to use Scala classes and Object into Java T. Genet (ISTIC/IRISA) ACF-5 33 / 36 T. Genet (ISTIC/IRISA) ACF-5 34 / 36 Isabelle/HOL exports Scala case classes and functions... theory tp [...] datatype a tree= Leaf Node " a * a tree * a tree" fun member:: " a a tree bool" where "member _ Leaf = False" "member x (Node(y,l,r)) = (if x=y then True else ((member x l) (member x r)))" to Scala object tp { abstract sealed class tree[+a] // similar to traits case object Leaf extends tree[nothing] case class Node[A](a: (A, (tree[a], tree[a]))) extends tree[a] def member[a : HOL.equal](uu: A, x1: tree[a]): Boolean = (uu, x1) match { case (uu, Leaf) => false case (x, Node((y, (l, r)))) => (if (HOL.eq[A](x, y)) true else member[a](x, l) member[a](x, r)) T. Genet (ISTIC/IRISA) ACF-5 35 / 36... and some more cryptic code for Isabelle/HOL equality object HOL { trait equal[a] { val HOL.equal : (A, A) => Boolean def equal[a](a: A, b: A)(implicit A: equal[a]): Boolean = A. HOL.equal (a, b) def eq[a : equal](a: A, b: A): Boolean = equal[a](a, b) To link Isabelle/HOL code and Scala code, it can be necessary to add: implicit def equal_t[t]: HOL.equal[T] = new HOL.equal[T] { val HOL.equal = (a: T, b: T) => a==b Which defines HOL.equal[T] for all types T as the Scala equality == T. Genet (ISTIC/IRISA) ACF-5 36 / 36