Macro-based type providers in Scala Eugene Burmako and Travis Brown École Polytechnique Fédérale de Lausanne University of Maryland, College Park

5 april 2014

What are type providers, anyway?

2

What are type providers, anyway? Compile-time metaprogramming facilities for “information rich programming”

2

What are type providers, anyway? Compile-time metaprogramming facilities for “information rich programming”* *to borrow a phrase from the F# community

2

A type provider… 1 reads information from a data source 2 makes that information available to the program in types

3

Examples of information sources I

An XSD schema for XML

I

A JSON-LD context

I

A Web Service Description Language file

I

SQL table definitions and stored procedures

4

Motivation I

You’ve got schemas that describe your data

I

You want to use these descriptions in your code

I

You don’t want to repeat yourself!

5

Example: Schema bindings for RDF @prefix @prefix @prefix @prefix

dc: . dct: . sga: . wiki: .

sga:ms-abinger-c57 a dct:Text. sga:ms-abinger-c57 dc:title "Frankenstein Draft Notebook B"@en. sga:ms-abinger-c57 dc:creator wiki:Mary_Shelley.

6

Example: Schema bindings for RDF @prefix rdf: . @prefix rdfs: . @prefix dc: . dc:title a rdf:Property; rdfs:comment "A name given to the resource."@en; rdfs:isDefinedBy dcterms:; rdfs:label "Title"@en; rdfs:range rdfs:Literal. dc:creator a rdf:Property; # and on and on...

7

Example: Schema bindings for RDF val frankensteinNotebookB = ( URI("http://shelleygodwinarchive.org/ms-abinger-c57") .a(dct.Text) -- dc.title ->- "Frankenstein Draft Notebook B" -- dc.creator ->- URI( "https://en.wikipedia.org/wiki/Mary_Shelley" ) )

8

Example: Schema bindings for RDF Follow along with example code and documentation: We’ll be using the W3C’s Banana RDF library throughout:

9

Low-tech solutions

10

Defining schema bindings manually object dc extends PrefixBuilder("http://purl.org/dc/terms/") { val title = apply("title") val creator = apply("creator") // and on and on... }

11

Defining schema bindings manually object dc extends PrefixBuilder("http://purl.org/dc/terms/") { val title = apply("title") val creator = apply("creator") // and on and on... }

But we’re just repeating the RDF Schema we’ve seen above…

11

Defining schema bindings manually I

These vocabularies can be large (hundreds of terms)

I

We’re just repeating information from the RDF Schema

I

We don’t want to repeat ourselves!

12

Traditional solution: textual code generation I

Tied to a specific (often ad-hoc) build process

I

Concatenating strings is unpleasant and error-prone

I

Oblivious to semantics, e.g. dependencies between modules of the program

I

Hard to customize

I

Easy to get out of sync

13

Implementing type providers I

In F#: special support is built into the compiler

I

In Scala: we can use the general purpose macro system

14

Implementing type providers I

In F#: special support is built into the compiler

I

In Scala: we can use the general purpose macro system

…with Scala macros I

Anonymous type providers via def macros

I

Public type providers via macro annotations

14

Anonymous type providers

15

In action val dc = fromSchema("/dcterms.rdf")

16

In action val dc = fromSchema("/dcterms.rdf")

That’s all!

16

How it works I

Parses the schema resource

I

Creates an instance of a structural type

I

scalac figures out the rest

17

How it works I

Parses the schema resource at compile time

I

Creates an instance of a structural type

I

scalac figures out the rest

17

Generated code val dc = new PrefixBuilder("http://purl.org/dc/terms/") { val title = apply("title") val creator = apply("creator") // et cetera... }

18

Implemented with a macro object PrefixGenerator { def fromSchema(path: String) = macro impl def impl(c: Context)(path: c.Expr[String]) = ... }

19

Advantages of the anonymous approach I

Familiar syntax—just a method call

I

Works in official Scala 2.10 and 2.11

20

Advantages of the anonymous approach I

Familiar syntax—just a method call

I

Works in official Scala 2.10 and 2.11

Disadvantages I

Structural types don’t work in Java

I

Structural types involve reflective access in Scala

20

Advantages of the anonymous approach I

Familiar syntax—just a method call

I

Works in official Scala 2.10 and 2.11

Disadvantages I

Structural types don’t work in Java

I

Structural types involve reflective access in Scala*

*but there’s a partial workaround—see the example project

20

Public type providers

21

In action @fromSchema("/dcterms.rdf") object dc extends PrefixBuilder

22

How it works I

Also parses the schema resource at compile-time

I

Uses the provided object as a template

I

Populates the object with generated members

23

Generated code object dc extends PrefixBuilder("http://purl.org/dc/terms/") { val title = apply("title") val creator = apply("creator") // et ainsi de suite... }

24

In comparison // anonymous val dc = new PrefixBuilder("http://purl.org/dc/terms/") { val title = apply("title") val creator = apply("creator") } // public object dc extends PrefixBuilder("http://purl.org/dc/terms/") { val title = apply("title") val creator = apply("creator") }

25

Also implemented with a macro class fromSchema(path: String) extends StaticAnnotation { def macroTransform(annottees: Any*) = macro PrefixGenerator.impl } object PrefixGenerator { def impl(c: Context)(annottees: c.Expr[Any]*) = ... }

26

Advantages of the public approach I

Generated code is straightforward and interoperable

I

Provides a lot of notational freedom

27

Advantages of the public approach I

Generated code is straightforward and interoperable

I

Provides a lot of notational freedom

Disadvantages I

Requires your users to depend on macro paradise

I

Provides a lot of notational freedom

27

Summary

28

We can generate code from schemas I

Using def macros in vanilla Scala 2.10/2.11 (anonymous)

I

Using macro annotations in macro paradise (public)

29

How practical is this? (Language support) I

Macro annotations aren’t shipped in Scala 2.11

I

No concrete plans to ship them in Scala 2.12

I

This means anonymous type providers are more stable

I

But they have important downsides, so it’s a trade-off

30

How practical is this? (IDE support) I

Both anonymous and public type providers are whitebox

I

This means limited supported in Intellij and Eclipse

I

Also there’s no easy way to look into macro expansions

I

Or to generate scaladocs for generated code

31

How practical is this? (IDE support) I

Both anonymous and public type providers are whitebox

I

This means limited supported in Intellij and Eclipse*

I

Also there’s no easy way to look into macro expansions*

I

Or to generate scaladocs for generated code* *this is something we are working on in Project Palladium

31

How practical is this? (Tool support) I

Build reproducibility is a solved problem

I

Just don’t go and talk to external data sources directly

I

Use schemas that are fetched and versioned independently

32

Comparison with F# I

More raw power

I

Limited IDE support

I

Not yet part of the language standard

33

Summary I

Macros enable principled compile-time code generation

I

Can successfully implement type providers

I

Better support is necessary for optimal experience

34

Resources I

Our example project:

I

Type providers in Scala:

I

Project Palladium:

Or ask us! I

@xeno_by

I

@travisbrown

Thanks! 35

Macro-based type providers in Scala - GitHub

Apr 5, 2014 - dc.title ->- "Frankenstein Draft Notebook B" ... We'll be using the W3C's Banana RDF library throughout: . 9 ...

175KB Sizes 13 Downloads 255 Views

Recommend Documents

scala - GitHub
Document relevancy is an important question that has been approached in various ways. With the advent of so- cial media, especially Twitter, the doc- uments of interest shrank in size. Peo- ple tend to tweet a lot of information. The generated tweets

Functional Programming in Scala - GitHub
Page 1 ... MADRID · NOV 21-22 · 2014. The category design pattern · The functor design pattern … ..... Play! ∘ Why Play? ∘ Introduction. Web Dictionary.

Scalaz: Functional Programming in Scala - GitHub
one value of type B. This is all a function is allowed to do. No side-effects! .... case class Success[+E, +A](a: A) extends Validation[E, A] ... phone: String).

Tweets about 'scala', but not about 'scala' - GitHub
(Analytics, 2009) This phenomenon has been researched ... as well as both Token bigrams and Tag bi- grams are used as .... August-2009. pdf. Banko, M. and ...

Type-Directed TDD in Rust - GitHub
Jul 21, 2014 - Give a taste of a practical software development process that is: ▻ test-driven ... Recently, Apple ditched Objective C for its new language Swift!

BDE Type Taxonomy - GitHub
Dec 4, 2015 - bslmf::IsFundamental baltzo::Loader bslma::DeallocatorGuard bslma::DestructorProctor utility meta- function protocol bdlt::Date bslstl::StringRef.

Applying Type-Level and Generic Programming in Haskell - GitHub
Feb 10, 2018 - (Haskell allows to say deriving Eq on a datatype declaration, invoking compiler magic that conjures up a .... Since type-level programming is a bit peculiar in Haskell, we'll move step by step: from normal lists over .... Before we mov

Scheme To Dependent Type theory In 100 Lines - GitHub
these terms. Howard also introduced the world's first dependent type system, in which types may depend on terms. In this system, equality may be stated between terms. ..... References on Writing Dependent. Type Systems. • A simple type-theoretic la

Type of article: Research Paper DiffusionKit - GitHub
website of DiffusionKit includes test data, a complete tutorial and a series of tutorial ..... The 3D show panel supports only one active image at a ..... Illustrations of how to extract a specific fiber bundles from entire brain tractography, ... As

Open putty and type remote hostname - GitHub
(here I put simply hostname ) and then click Save button. From now on your connection to remote host with tunnel is saved and can be reused anytime you open ...

Pro Scala: Monadic Design Patterns for the Web - GitHub
where AGILE methodologies rightfully demand a justification thread running from ...... In addition to structural equivalence of terms (which is a bi-directional ...

Scala Macros
Sep 10, 2012 - (error "does not compute")). (defmacro aif args. (list 'let* (list (list 'temp (car args)) .... Old school solution. (defmacro+ aif. (aif cond then else).

Electricity Providers In Texas.pdf
For instance, do you leave your cell phone charger plugged in all the time,. but only ... Electricity Providers In Texas.pdf. Electricity Providers In Texas.pdf. Open.

Operator Type Operator Java Flow of Control - GitHub
Operator Type. Operator. Java Flow of Control. Unary ! ++ -- + -. Java Evaluates First. Mul plica ve. * / %. Addi ve. + -. Rela onal. < > = Equality. == !=

Solid Type System Runtime Checks and Unit Tests - GitHub
insufficient type information! Return type should be something like ... type is. // ProteinFail \/ Future[List[FacebookLike]]. Unwrap? ... case \/(result) => s"Dude, eat proteins, or you won't do like me: $result" .... Thank you! goo.gl/U0WYAB · PDF.

Java and Scala's Type Systems Are Unsound [pdf] - GitHub
1. Introduction. In 2004, Java 5 introduced generics, i.e. parametric polymor- phism, to the Java ... 12 years ago, both languages were unsound; the examples we will present were ... Classes and interfaces can be specified to have type pa- rameters a

Find the Best Private Aged Care Providers in Melbourne.pdf ...
Good afternoon! ... aqueles animais que não reque- rem suplementação com glicocorticóides, ... Find the Best Private Aged Care Providers in Melbourne.pdf.

Find the Best Private Aged Care Providers in Melbourne.pdf ...
Page 1 of 1. Find the Best Private Aged Care Providers in Melbourne.pdf. Find the Best Private Aged Care Providers in Melbourne.pdf. Open. Extract. Open with.