Bidirectional Programming Nate Foster Cornell University CS 2110 24 November 2015 Languages
Most programming languages, like C...
Java,
Python,
and C++ are general purpose.
I’m interested in designing languages that are specifjcally designed for particular tasks
Domain-specifjc languages 3 • Clean semantics • Natural syntax • Better tools
The View Update Problem In databases, this is known as the view update problem. [Bancilhon, Spryatos ’81] 6 View Database Query A B C x 1 true y 2 true z 3 false A B C x 1 false y 100 true Trigger
The View Update Problem In Practice It also arises in data converters and synchronizers... [Foster, Greenwald, Pierce, Schmitt JCSS ’07] — Harmony 6 Common target Replica in format A format Replica in format B Synchronized replica in Synchronized replica in format B format A
The View Update Problem In Practice ...in picklers and unpicklers... [Fisher, Gruber ’05] — PADS 6 In-memory representation Binary file application update Updated binary file
The View Update Problem In Practice ...in model-driven software development... [Stevens ’07] — bidirectional model transformations 6 Java code UML model Point x : int y : int translate(int x,int y) refactor Point x : int y : int translate(int x, int y) moveTo(int x, int y) Updated Java code Refactored model
Problem How do we write these bidirectional transformations? 7
Problem: Why is it hard? We want updates to the view to be translated “exactly”... 7
Problem: Why is it hard? We want updates to the view to be translated “exactly”... 7
Problem: Why is it hard? ...but some updates have many corresponding source up- dates... 7 ?
Problem: Why is it hard? ...while others have none ! 7 ?
Possible Approaches Bad: write the two transformations as separate functions. 8 • tedious to program • diffjcult to get right • a nightmare to maintain
Possible Approaches Good: derive both transformations from the same pro- gram. design 8 • Clean semantics: behavioral laws guide language • Natural syntax: parsimonious and compositional • Better tools: type system guarantees well-behavedness
“Bidirectional languages are an effective and elegant means of describing updatable views”
[Foster, Greenwald, Moore, Pierce, Schmitt TOPLAS ’07] Lenses ’‘Never look back unless you are planning to go that way” —H D Thoreau
Terminology 11 get
Terminology 11 put
Terminology 11 lens
Bidirectional vs. Bijective If get is non-injective, put needs access to the original source. Of course, the purely bijective case is also interesting. 12 non-injective function
The Bijective Case For bijective transformations... ...the desired behavior is obvious. 13 T S
The General Case But for bidirectional transformations... ... we need to identify conditions that allow us to 14 T S • recognize and reject bad (unreasonable) programs • understand and predict behavior
An Unreasonable Example 15 project out string component foo 0 foo
An Unreasonable Example 15 foo 0 foo bar
15 An Unreasonable Example foo 0 foo blech 5 bar return a constant
An Unreasonable Example 15 foo 0 foo blech 5 bar ≠ blech
The PutGet law Principle: Updates should be “translated exactly” — i.e., to a target. Formally: v 16 source for which get yields exactly the updated get ( put v s ) =
17 A Debatable Example project out and duplicate string component foo 0 foo foo
A Debatable Example 17 foo 0 foo foo bar foo
A Debatable Example 17 foo 0 foo foo bar 0 bar foo propagate "newest" string
A Debatable Example 17 foo 0 foo foo bar 0 bar foo ≠ bar bar
Another Unreasonable Example 18 project out string component foo 5 foo
Another Unreasonable Example 18 foo 5 foo bar
18 Another Unreasonable Example foo 5 foo bar 0 bar propagate always set numeric updated string field to 0
18 Another Unreasonable Example foo 5 foo = foo
18 Another Unreasonable Example foo 5 foo = ≠ foo 0 foo
The GetPut law Principle: If the view does not change, neither should the source. Formally: s 19 put ( get s ) s =
Another Debatable Example 20 project out string component foo 0 foo
Another Debatable Example 20 foo 0 foo bar
20 Another Debatable Example foo 0 foo bar 1 bar increment numeric component if string component has changed
Recommend
More recommend