A one week course on Scala and Domain-Specific Languages


All lectures except for Tuesday and Friday will be held in room 3364. Tuesday’s and Friday’s lectures will be in room FL71 (Physics).

David’s email address is drc at (itu’s domain).

Slides, example code and other resources will be linked to from this page throughout the week. Please check back regularly.

Each day, a group room is reserved. It will be advantageous if you can bring a laptop to this room so that we can work on the exercises together, taking up any issues as they arise.

Useful links


Please make sure that you have access to a computer with a JVM and SBT installed. Installation instructions for SBT are available from the project’s home page. Alternatively, you can simply install Typesafe Stack, which will also get you SBT.

Course plan

Monday, 3 December

Introduction to Scala (10:00 room 3364)

  • Motivation and concepts of the language

  • Classes, objects, and traits

  • Case classes as (G)ADTs

  • Pattern matching and extractors

  • for-expressions

Tools and practicalities (13:15 room 3364)

  • SBT - Scala’s answer to Cabal

  • Tests with Scalacheck (a port of QuickCheck)

  • Combinator parsers

  • Scaladoc

Monday Materials

Monday Homework

Implement simply-typed call-by-name lambda-calculus.

  • Write a typechecker and evaluator

  • Define a GADT-based representation that is type-correct by construction, and write an evaluator for them.

  • Bonus: write an interactive toplevel with a combinator parser front-end that typechecks and evaluates terms.

Note: This exercise has been modified to reduce difficulty. The original version wanted you to generate the type-safe terms from the untyped terms, which is not feasible given the amount of Scala knowledge we expect. However, this can be done for simple arithmetic expressions.

Alternate assignment for those who haven’t seen lambda calculus and GADTs before (from ITU course).

Group room 4128 is available all day Monday.

Tuesday, 4 December

Implicits (10:00 room FL71 (Physics))

  • Exercise followup

  • Implicit conversions and views

  • Implicit conversions as extension methods

  • Implicit arguments

  • Understanding the Scala collections library

Functional programming in Scala (Tuesday, 4 December 13.15 room FL71)

  • Methods, functions and partial functions

  • Type classes

  • Functor and bounded functor

  • Monad and bounded monad

Tuesday Materials

Tuesday Homework

  • Implement Monad type class, and instances for Option, List, etc.

  • Find a way to enable instances of your monad to be used with for-notation.

  • Implement a set datatype as a tree, having the type parameter F-bounded by Ordered. Don’t worry about integrating it into the Scala collections hierarchy.

  • Implement BoundedMonad type class, and instance for Set.

  • Implement a BoundedMonad, analagous to the BoundedFunctor from the lecture. Create an instance for your tree.

Group room 6128 is available all day Tuesday

Wednesday, 5 December

Scala API design (10:00 room 3364)

  • Exercise followup

  • Using the type inferencer

  • Method argument lists and currying

  • More about method call syntax

Embedding languages in Scala (13:15 room 3364)

  • By-name parameters for embedded control structures

  • Laziness

  • Modular languages and smart constructors

  • Combinator library hints

Wednesday Materials

Wednesday Homework

  • Define a While loop for Scala that works the same as the built-in while loop. This syntax should work: While(true) {println("yes")}

  • Implement assertions as a library. Control whether they are checked or not by importing an identifier.

  • Design and implement a simple declarative embedded language for surveys, which define a number of questions. The implementation should be able to ask the questions to a user and return the responses somehow. Keep this language simple - it’s just for this evening, and we’ll return to it later for a new challenge!

Group room 4128 is available all day Wednesday

Thursday, 6 December

Polymorphic embedding of DSLs (10:00 room 3364)

  • The polymorphic embedding technique

  • Dependent methods

  • Example: embedded language for insurance payment streams

Virtualized Scala (13:15 room 3364)

  • How to get ahold of scala-virtualized

  • Virtualized if

  • Virualized match

  • Virtualized var

  • Infix methods

Version notes

  • Make sure you’re using the lastest Scala 2.10 RC (RC3 at this time).

  • Use sbt version 0.12.0. If you’re using an Ubuntu package or need an older version in parallel, the easiest way is to create the file $PROJECT/project/build.properties containing the string sbt.version=0.12.0, and SBT will take care of using the correct version for this project. $PROJECT here refers to the directory that contains your main build.sbt file.

Thursday Materials

Thursday Homework

  • Do a polymorphic embedding of the survey language. The same survey definition should be able to be executed as a command-line application and it should be able to generate an HTML form.

  • Use scala-virtualized to improve the syntax of your first survey language. For example, you could use if to construct guarded blocks.

Group room 4128 is available all day Thursday

Friday, 7 December

Lightweight modular staging (10:00 room FL71)

  • Description of the technique

  • Delite as case study

Discussion session (13.15-15.00 room FL71)

  • Look at polymorphically-embedded survey DSL submissions

  • Questions and answers about the course contents

  • What is good and what is not so good about Scala for various tasks, especially DSLs?

  • Project ideas

Group room 6128 is available all day Friday

Friday Materials