Semantic Modularity for DSLs Bruno C. d. S. Oliveira The - - PowerPoint PPT Presentation

semantic modularity for dsls
SMART_READER_LITE
LIVE PREVIEW

Semantic Modularity for DSLs Bruno C. d. S. Oliveira The - - PowerPoint PPT Presentation

Semantic Modularity for DSLs Bruno C. d. S. Oliveira The University of Hong Kong Can you give a 35m talk on Domain- specific type systems using Object Algebras? Sure! That should be easy I know Object Algebras well! Humm Wait a


slide-1
SLIDE 1

Semantic Modularity for DSLs

Bruno C. d. S. Oliveira The University of Hong Kong

slide-2
SLIDE 2

Can you give a 35m talk on Domain- specific type systems using Object Algebras?

slide-3
SLIDE 3

Sure!

slide-4
SLIDE 4

That should be easy I know Object Algebras well!

slide-5
SLIDE 5

Humm… Wait a minute! How about domain-specific type systems?

slide-6
SLIDE 6

A little twist on Sebastian’s invitation

  • How to use type systems to develop:
  • modular
  • reusable
  • separately compilable
  • and type-safe (of course!)

DSL components

slide-7
SLIDE 7

My work

  • Lots of mentions to composition/modularity this

week.

  • Highly overload term
  • My work is primarily on semantic modularity
  • Not syntax/parser modularity
  • Not syntactic modularity
slide-8
SLIDE 8

DSL Components

slide-9
SLIDE 9

Syntactic (AST) Components

Binding \x . x, \x . (\y . y) x Arithmetic 1,3+4,5*6 Logic true, false, if true then false else true Graphics point(3,4), scale(2,point(4,5))

slide-10
SLIDE 10

Semantic Components

Type-checking tcheck (n) -> Int Evaluation [x+y] -> [x] + [y] Pretty Printing pp (x+y) -> pp x + “ + “ + pp y

slide-11
SLIDE 11

Syntactic Compositions

Arithmetic + Logic + Binding \x . if x then x+1 else x Arithmetic + Binding + Graphics \x y. point(10+x, point 10 + y)

slide-12
SLIDE 12

Semantic Compositions

Evaluation + Pretty Printing [x+y] -> [x] + [y] pp (x+y) -> pp x + “ + “ + pp y Evaluation + Type- Checking [x+y] -> [x] + [y] tcheck (n) -> Int

slide-13
SLIDE 13

The Matrix

slide-14
SLIDE 14

The Matrix

Arithmetic Logic Binding Graphics Evaluation [n] -> n [true] -> true [x] = x [point(e1,e2)] = … Type- Checking tc(n) -> Int tc(true)

  • > Bool

tc(x,e) -> lookup(x,e) tc(point(e1,e2))

  • > Point

Pretty- Printing pp(n) -> n.toString pp(true) -> true.toString pp(x) = x pp(point(e1,e2 )) = …

slide-15
SLIDE 15

How to Program such Components?

Meta-Programming

Reflection Casts

Dynamic Languages Superimposition

slide-16
SLIDE 16

How to Program such Components/Compositions?

  • How about:
  • Type-safety? absence of pattern matching and/or

messageNotUnderstood errors; safe composition

  • IDE support (code completion, refactoring,…)?
  • Separate Compilation?
  • Maintainability?
  • We would like to program such components modularly

while still having all of the above!

slide-17
SLIDE 17

The Matrix

Arithmetic Logic Binding Graphics Evaluation [n] -> n [true] -> true [x] = x [point(e1,e2)] = … Type- Checking tc(n) -> Int tc(true)

  • > Bool

tc(x,e) -> lookup(x,e) tc(point(e1,e2))

  • > Point

Pretty- Printing pp(n) -> n.toString pp(true) -> true.toString pp(x) = x pp(point(e1,e2 )) = …

Each cell can be viewed as a separate module! Each Module should allow for:

  • Static type-checking
  • Separate compilation

Each composition should ensure that interpretations handle all cases

slide-18
SLIDE 18

The Matrix

Arithmetic Logic Binding Graphics Evaluation [n] -> n [true] -> true [x] = x [point(e1,e2)] = … Type- Checking tc(n) -> Int tc(true)

  • > Bool

tc(x,e) -> lookup(x,e) tc(point(e1,e2))

  • > Point

Pretty- Printing pp(n) -> n.toString pp(true) -> true.toString pp(x) = x pp(point(e1,e2 )) = …

The Expression Problem!

slide-19
SLIDE 19

Object Algebras

slide-20
SLIDE 20

Object Algebras

  • Lightweight technique (a design pattern) to solve

the Expression Problem and various other (semantic) modularity issues.

  • In their basic form, they work on common OO

languages (Java, C#, Scala, …)

  • Inspired by type-theoretic encodings of datatypes
slide-21
SLIDE 21

Theory

  • Object Algebra interfaces are simply algebraic

signatures

  • From an OO point to view they can be viewed both as:
  • (Internal) Visitor interfaces
  • Generic Abstract Factory Interfaces
  • f some primitive built-in sorts

signature E lit: Int → E add: E × E → E A general algebraic sign

slide-22
SLIDE 22

Extensibility

interface IntAlg<A> { A lit(int x); A add(A e1, A e2); }

interface IntBoolAlg<A> extends IntAlg<A> { A bool(Boolean b); A iff(A e1, A e2, A e3); }

Extensibility!

slide-23
SLIDE 23

Demo

slide-24
SLIDE 24

Uses of Object Algebras

  • Batch Script Language
  • Oliveira and Cook, Extensibility for the Masses: Practical Extensibility with

Object Algebras, ECOOP (2012)

interface BatchFactory<E> { E Var(String name); // variable reference E Data(Object value); // simple constant (number, string, or date) E Fun(String var, E body); E Prim(Op op, List<E> args); // unary and binary operators E Prop(E base, String field); // field access E Assign(Op op, E target, E source); // assignment E Let(String var, E expression, E body); // control flow E If(E condition, E thenExp, E elseExp); E Loop(Op op, String var, E collection, E body); E Call(E target, String method, List<E> args); // method invocation E In(String location); // reading and writing forest E Out(String location, E expression); }

  • Fig. 13. Batch script language abstract syntax
slide-25
SLIDE 25

Uses of Object Algebras

  • Batch Script Language
  • Oliveira and Cook, Extensibility for the Masses: Practical Extensibility with

Object Algebras, ECOOP (2012)

interface PartitionFactory<E> extends BatchFactory<E> { E Other(Object external, E... subs); E DynamicCall(E target, String method, List<E> args); E Mobile(String type, Object obj, E exp); }

slide-26
SLIDE 26

Uses of Object Algebras

  • One-pass Modular (subset of) C compiler

long some_function(); /* int */ other_function(); /* int */ calling_function() { long test1; register /* int */ test2; test1 = some_function(); if (test1 > 0) test2 = 0; else test2 = other_function(); return test2; }

  • Rendel et al., From Object Algebras to Attribute Grammars, OOPSLA (2014)
slide-27
SLIDE 27

Uses of Object Algebras

  • QL Language with Object Algebras
  • Gouseti et al., Extensible Language Implementation with Object Algebras,

GPCE (2014)

form HouseOwning { soldHouse: "Did you sell a house?" boolean boughtHouse: "Did you buy a house?" boolean if (soldHouse) { sellingPrice: "Selling price:" integer privateDebt: "Private debts:" integer valueResidue: "Value residue:" integer = (sellingPrice - privateDebt) } }

slide-28
SLIDE 28

Uses of Object Algebras

  • Streams a la Carte

int sum = widgets.stream() .filter(b -> b.getColor() == RED) .mapToInt(b -> b.getWeight()) .sum();

  • Biboudis et al., Streams à la carte. Extensible Pipelines with Object Algebras.

submitted

slide-29
SLIDE 29

Some Work on Object Algebras

  • First paper
  • Oliveira and Cook, Extensibility for the Masses: Practical Extensibility with

Object Algebras, ECOOP (2012)

  • Increasing the expressive power of object

algebras:

  • Oliveira et al., Feature-Oriented Programming with Object Algebras,

ECOOP (2013)

  • Rendel et al., From Object Algebras to Attribute Grammars, OOPSLA

(2014)

  • Domain-Specific Languages
  • Gouseti et al., Extensible Language Implementation with Object Algebras,

GPCE (2014)

  • Biboudis et al., Streams à la carte. Extensible Pipelines with Object
  • Algebras. submitted
slide-30
SLIDE 30

Pre-History of Object Algebras

  • Church Encodings of Datatypes as (Haskell) Type

Classes

  • Hinze, Generics for the Masses, ICFP (2004) and JFP (2006)
  • Oliveira and Gibbons, TypeCase: A Design Pattern for Type-Indexed Functions, HW

(2005)

  • Extensibility: Solving the Expression Problem (Haskell)
  • Oliveira et al., Extensible and Modular Generics for the Masses, TFP (2006)
  • Applications to Embedded DSLs (Haskell & Scala)
  • Carrete et al., Finally tagless, partially evaluated: Tagless staged interpreters for

simpler typed languages, APLAS (2007) & JFP (2009)

  • Hofer et al., Polymorphic Embedding of DSLs, GPCE (2008)
  • Rompf and Odersky, Lightweight Modular Staging, GPCE (2010)
  • Modular Visitors & Church Encodings (Scala)
  • Oliveira et al., The Visitor Pattern as a Reusable Type-Safe Component, OOPSLA

(2008)

  • Oliveira, Modular Visitor Components, ECOOP (2009)
slide-31
SLIDE 31

Structure-Shy Components

slide-32
SLIDE 32

Semantic Components

FreeVars fv(e1+e2) -> fv(e1) + + fv(e2) Substitution [x -> e] (e1+e2) -> [x -> e] e1+ [x -> e] e2

slide-33
SLIDE 33

The Matrix

Arithmetic Logic Binding Graphics FreeVars Boring! Boring! Interesting! Boring! Substitution Boring! Boring! Interesting! Boring!