A Practical Example of Language Design and Implementation Anya - - PowerPoint PPT Presentation

a practical example of language design and implementation
SMART_READER_LITE
LIVE PREVIEW

A Practical Example of Language Design and Implementation Anya - - PowerPoint PPT Presentation

Magnolia Concepts Compiler Facts IDE More Conclusion A Practical Example of Language Design and Implementation Anya Helene Bagge Bergen Language Design Laboratory, University of Bergen Software Language Engineering Course Koblenz,


slide-1
SLIDE 1

Magnolia Concepts Compiler Facts IDE More Conclusion

A Practical Example of Language Design and Implementation

Anya Helene Bagge

Bergen Language Design Laboratory, University of Bergen

Software Language Engineering Course Koblenz, 2013-01-08

slide-2
SLIDE 2

Magnolia Concepts Compiler Facts IDE More Conclusion

Magnolia Overview

The language for flexible, reliable software! Programming with...

  • Abstraction
  • Specification
  • Separation of Concerns

We’re doing:

  • Software analysis &

transformation

  • Model-driven – except we

don’t use the terminology Main design focus:

  • Ease of reasoning!
  • ...for you, for me, for the

compiler

  • Specification is integrated

with code

  • No aliasing
  • No higher order
  • Highly structured iteration
  • No OO/dynamic dispatch
  • Control over resources

But also flexibility, reliability, maintainability, extreme code reuse.

slide-3
SLIDE 3

Magnolia Concepts Compiler Facts IDE More Conclusion

Magnolia Programming

  • Supported by an Eclipse plugin
  • Current backend target language is C++
  • All the basic stuff in the language are libraries
  • Implemented in C++
  • Typical story
  • Design your interfaces, building on existing ones
  • Specify behaviour with axioms
  • Provide or reuse implementation(s)
  • Test using automated axiom-based tests
  • Reuse support
  • Everything can be renamed, declarations added/dropped,

parameters changed, etc.

  • Meta-programming
  • To be added
  • Parts of compiler API is exposed and can be called at compile

time

slide-4
SLIDE 4

Magnolia Concepts Compiler Facts IDE More Conclusion

Concept-Based Programming

Integration of specifications in programs:

  • Ideas are modelled as concepts
  • Each concept is a set of abstract types, operations,

requirements and axioms – corresponding to an algebraic specification

  • Concepts can build on (refine) other concepts
  • A concept may have several interchangeable implementations

Motivation

  • Making semantics available to the compiler (axioms)
  • Separating specification from implementation
slide-5
SLIDE 5

Magnolia Concepts Compiler Facts IDE More Conclusion

Conceptual Overview

Procedures Functions Types Statements Expressions Data Structures

Interface Implementations Concept

Axioms Requirements Rewriting Testing

→ A b s t r a c t i

  • n

→ → Specification → ← Implementation ←

slide-6
SLIDE 6

Magnolia Concepts Compiler Facts IDE More Conclusion

Example Concept: Dictionary

concept Dictionary(Dict, Key, Val) { type Dict; type Key; type Val; requires EqualityComparable[T => Key]; function Dict create(); function Dict put(Dict, Key, Val); function Val get(Dict, Key); function bool contains(Dict, Key); function bool isEmpty(Dict); axiom dict1(Dict d, Key k, Val v) { assert get(put(d, k, v), k) <-> v; assert contains(put(d, k, v), k) <-> true; } axiom dict2(Dict d, Key k, Key l, Val v, Val w) { if(k != l) assert get(d, k) <-> get(put(d, l, w), k); } }

slide-7
SLIDE 7

Magnolia Concepts Compiler Facts IDE More Conclusion

Dictionary – Different Implementations

Dictionary create() put(d,k,v) get(d,k) HashMap = create() = put(d,k,v) = get(d,k) Search Tree = new() = insert(k,v,d) = lookup(k,d) List-of-Tuples = List((type K, type V)) = List((k,v),d) = if head(d)[0] == k then head(d)[1] else get(tail(d),k)

slide-8
SLIDE 8

Magnolia Concepts Compiler Facts IDE More Conclusion

Mutification & Functionalisation

Every imperative procedure

procedure _++_(upd a : string, b : string);

has one or more corresponding algebraic functions:

function _++_(a : string, b : string) : string;

Only the imperative version needs to be implemented – uses of the functions are automatically mutified to imperative code

var s = "Hello, " ++ person ++ "!"; var s = "Hello, "; // s = "Hello, " call _++_(s, person); // s = "Hello, " ++ person call _++_(s, "!"); // s = "Hello, " ++ person ++ "!"

slide-9
SLIDE 9

Magnolia Concepts Compiler Facts IDE More Conclusion

Mutification & Functionalisation

Motivated by

  • Intuitiveness of algebraic style
  • Direct relationship with axioms / algebraic specifications
  • Performance benefits of imperative style vs algebraic style

Evaluation

  • Positive experience with Sophus, implemented in algebraic

style:

  • 1.8–2 times speedup of algebraic code (6–8× with rewrite

rules)

  • Enables...
  • easy application of rewrite rules and optimisations
  • easy integration of specification and implementation
  • simple data-flow analysis and dependency analysis
  • convenient way of dealing with multi-result operations by

specialisation

slide-10
SLIDE 10

Magnolia Concepts Compiler Facts IDE More Conclusion

A Small Example

In Java: foo.sort()

What happens to foo?

foo.sort(cmp)

Will cmp be changed?

In Magnolia: foo = sort(foo);

We know what happens to every parameter.

foo = sort(foo); bar = search(foo, name);

We know that foo is sorted, and can choose binary search

foo = sort(sort(foo));

We know that the outer sort is useless

slide-11
SLIDE 11

Magnolia Concepts Compiler Facts IDE More Conclusion

Example: Pairs of Numbers

PairExtensions.mg

slide-12
SLIDE 12

Magnolia Concepts Compiler Facts IDE More Conclusion

Implementation

slide-13
SLIDE 13

Magnolia Concepts Compiler Facts IDE More Conclusion

Language Services

  • Build/compilation
  • Error/warning squigglies
  • Hover Help
  • Navigation
  • Outline
  • Project Overview
  • Refactoring
  • ...
slide-14
SLIDE 14

Magnolia Concepts Compiler Facts IDE More Conclusion

Accessing the IDE Infrastructure

Main entry point:

  • Workspace manager

The Workspace manager gives you a Project manager

  • Maintains mapping between files/URIs and Magnolia packages
  • Maintains a dependency tree
  • Listens to changes in the workspace
  • Is thread-safe

Available in both Eclipse and non-Eclipse versions

  • Could run in a separate process, and provide services to any

IDE

  • Works with Emacs
slide-15
SLIDE 15

Magnolia Concepts Compiler Facts IDE More Conclusion

Compiler Organisation

  • Frontend: parsing, desugaring, “loading”
  • Typechecking: flattening concept code, typechecking normal

code

  • High-Level Optimisation: not implemented yet
  • Backend: for each library unit
  • Mutify, remove any other “special” Magnolia constructs
  • Restructure to C++-like code
  • Pretty-print to C++; add standard stuff
slide-16
SLIDE 16

Magnolia Concepts Compiler Facts IDE More Conclusion

Compiler Examples

  • Desugaring
  • Flattening
  • Expression typechecking
  • C++ translation
slide-17
SLIDE 17

Magnolia Concepts Compiler Facts IDE More Conclusion

Facts

slide-18
SLIDE 18

Magnolia Concepts Compiler Facts IDE More Conclusion

Facts

A fact is a piece of non-trivial information:

  • Computed and stored independently
  • A handle to a (large) piece of data

Each fact has a signature:

  • Computed from dependencies

Facts may be in one of three states:

  • Available, and up to date (based on signature)
  • Available, but out of date
  • Not available
slide-19
SLIDE 19

Magnolia Concepts Compiler Facts IDE More Conclusion

Fact Data and Storage

Fact data may be:

  • Stored using soft/weak references
  • Automatically stored to disk

Each fact may be connected to a store object

  • If a fact is unavailable, it will attempt to read from the store
  • If a fact is updated, it will send data to the store

Foo.mg Foo.mgf Package Facts Store Disk Store

slide-20
SLIDE 20

Magnolia Concepts Compiler Facts IDE More Conclusion

Fine-Grained Dependencies

Facts are fairly coarse grained, e.g.:

  • The list of modules in a package
  • The typechecked AST of a module
  • The environment/symbol table at the top-level of a file

For fine-grained stuff, we use memoisation:

  • f = memo(fImpl) makes a memoised version of the Rascal

function fImpl

  • Results of calls to f is stored in a hash table indexed by the

arguments

  • Compiler library uses pure functions for easy memoisation
  • Speedup: 2x for desugaring, 1.05x for simple operations on

names

  • Memoisation can be basis for multithreading
slide-21
SLIDE 21

Magnolia Concepts Compiler Facts IDE More Conclusion

The IDE

slide-22
SLIDE 22

Magnolia Concepts Compiler Facts IDE More Conclusion

IDE Interaction Model

  • Resource management

system

  • Compiler library
  • Facts
  • Small Eclipse-specific layer

Example: Hover Help

  • Find package
  • Ask for xrefs
  • Present info

Q: How to map location to info?

slide-23
SLIDE 23

Magnolia Concepts Compiler Facts IDE More Conclusion

Language Services (again)

  • Build/compilation
  • Error/warning squigglies
  • Hover Help
  • Navigation
  • Outline
  • Project Overview
  • Refactoring
  • ...
slide-24
SLIDE 24

Magnolia Concepts Compiler Facts IDE More Conclusion

Refactoring: Inlining

Basic recipe:

  • Find something you want to inline (e.g., by location)
  • Apply the inlining
  • Pretty-print the inlined code, patch it back into the

surrounding source text

slide-25
SLIDE 25

Magnolia Concepts Compiler Facts IDE More Conclusion

More Fun with Inlining

  • Rename constant, variable, type...
  • Change method declaration
  • Introduce wrapper
slide-26
SLIDE 26

Magnolia Concepts Compiler Facts IDE More Conclusion

More Stuff

slide-27
SLIDE 27

Magnolia Concepts Compiler Facts IDE More Conclusion

Axiom-Based Rewriting

Each equational axiom is a potential rewrite rule:

  • Choose one side for matching, and the other as a replacement

Examples

unwrap(wrap(x)) <-> x x * (y + z) <-> x * y + x * z if(sorted(A)) sort(A) <-> A

For axioms to be useful in rewriting, we must know

  • Which axioms are useful
  • When they are useful
  • What they are useful for

Such strategy information can be provided by axiom classes:

  • Simplification, propagation, traversal order, etc
slide-28
SLIDE 28

Magnolia Concepts Compiler Facts IDE More Conclusion

Other Toughts

  • Energy Efficiency
  • How multi-threaded can we be?
  • Does that even matter if we hand-code in Java?
  • Performance
slide-29
SLIDE 29

Magnolia Concepts Compiler Facts IDE More Conclusion

Conclusion

It’s all:

  • Implemented in Rascal (language stuff) and Java (IDE stuff)
  • Organised as a library

Magnolia IDE offers:

  • Hierarchical project overview
  • Soft-referenced storage of facts / intermediate result
  • Library of compiler operations
  • Automatic disk storage
  • Fairly smooth experience on 8 cores, but still slow...