SLIDE 1
Bdl The B lock D escription L anguage Pierre Weis - Cl ement - - PowerPoint PPT Presentation
Bdl The B lock D escription L anguage Pierre Weis - Cl ement - - PowerPoint PPT Presentation
Bdl The B lock D escription L anguage Pierre Weis - Cl ement Franchini Carg` ese 2014 October 19 Motivation Block diagram simulation tools (such as Scicos, Simulink) are routinely used for modeling and simulate dynamical systems.
SLIDE 2
SLIDE 3
Pierre.Weis@inria.fr Clement.Franchini@inria.fr 2014-10-19
2
Proposal
Design a domain specific language (DSL) to describe the seman- tics of blocks and their connections. High-level language specialized to block description. For sake of safety and efficiency we need:
- Strong and static type-checking.
- Compilation (in particular static binding discipline).
- Semantically sound definition of the language constructs.
SLIDE 4
Pierre.Weis@inria.fr Clement.Franchini@inria.fr 2014-10-19
3
Bdl
Our DSL is the Block Description Language (Bdl). Characteristics
- Builtin matrix values and matrix types.
- Matrix size statically verified.
- All variables and matrices are initialized.
- Language constructions are side effect free (pure).
- Very limited memory side effects (assignments are restricted
to variables declared as mutable).
SLIDE 5
Pierre.Weis@inria.fr Clement.Franchini@inria.fr 2014-10-19
4
Static semantic description
Type reconstruction, ` a la ML (Damas-Milner algorithm). Compiler automatically finds types and verifies type adequacy. No type annotations in programs (except for interfaces). Polymorphic typing, using type schemes with quantified type pa- rameters.
SLIDE 6
Pierre.Weis@inria.fr Clement.Franchini@inria.fr 2014-10-19
5
Polymorphic values and functions
[ n, m ] t is the type of a matrix of t values with n rows and m columns. [ 0, 0 ] :: [1, 2] int // An integer vector of size 2 [ 1.1, 2.2; 3.0, 4.0 ] :: [2, 2] float // A 2x2 float matrix Matrix addition has a highly polymorphic type scheme: add_matrix :: for all m n t, function ([m, n] t, [m, n] t) -> [m, n] t
SLIDE 7
Pierre.Weis@inria.fr Clement.Franchini@inria.fr 2014-10-19
6
Bdl generic functions
Generic functions describe the meaning of high-level operators used in blocks (such as arithmetic operators). The dynamic semantics of generic functions is described in terms
- f the compiler’s target language basic primitives.
A generic definition is not adequately typable, but all applications
- f a generic function must statically evaluate to a well-typed
expression. Static semantics of generic functions is thus given by a combi- nation of partial evaluation and type reconstruction.
SLIDE 8
Pierre.Weis@inria.fr Clement.Franchini@inria.fr 2014-10-19
7
Generic addition
generic ( + ) (x, y) = assert (type of x == type of y); switch size of x case [1, 1], add_scalar (x, y);
- therwise,
(m, n) = size of x; mutable res = y; for i = 1 : m do for j = 1 : n do res := [res with i, j = add_scalar (x [i, j], y [i, j])]; end; end; res; end; end;
SLIDE 9
Pierre.Weis@inria.fr Clement.Franchini@inria.fr 2014-10-19 8
Generic scalar addition
generic add_scalar (x, y) = assert (type of x == type of y); t = type of x; switch t case int8, add_int8 (x, y); case int, add_int (x, y); case float, add_float (x, y);
- therwise,
partial_evaluation_error ( "no instance of add_scalar for type " ^^ string_of_type (t)); end; end;
SLIDE 10
Pierre.Weis@inria.fr Clement.Franchini@inria.fr 2014-10-19
9
Primitive addition
Primitive addition for short integers is an external definition (not defined in Bdl). external add_int8 :: function (int8, int8) -> int8 = external "bdl_add_int8"; bdlc will translate any call to “add int8” to a call to the target language primitive addition for eight bits long signed integers. Same definitions for other scalar types (int16, int32, float, ...).
SLIDE 11
Pierre.Weis@inria.fr Clement.Franchini@inria.fr 2014-10-19
10
Application of generic addition
1 + 2; generic ( + ) (1, 2) => type of 1 and type of 2 is int => assertion is verified => size of x is [1, 1] => call add_scalar (1, 2) => ... add_int (1, 2) => 3
SLIDE 12
Pierre.Weis@inria.fr Clement.Franchini@inria.fr 2014-10-19
11
Generic application type-checking
1.0 + 2; generic ( + ) (1.0, 2) => type of 1.0 is float type of 2 is int => assertion is not verified Static type checking error.
SLIDE 13
Pierre.Weis@inria.fr Clement.Franchini@inria.fr 2014-10-19
12
Dynamic semantic description
Small step semantics (source code to source code rewriting rules). Dynamic semantics is described by a partial evaluator. Semantics is defined as iteration of partial evaluation up to nor- mal form.
SLIDE 14
Pierre.Weis@inria.fr Clement.Franchini@inria.fr 2014-10-19
13
Difficulties
- Mixing static typing and partial evaluation is challenging.
- Provide a Bdl semantics for blocks that have a fuzzy defini-
tion in the first place.
SLIDE 15
Pierre.Weis@inria.fr Clement.Franchini@inria.fr 2014-10-19
14
Current state of the compiler
The front end is written:
- Concrete syntax is defined
- YACC parser written
The middle end is actively developped:
- Scope analysis completed
- Type reconstruction is in progress
- Sketchy partial evaluation
The back end is future work:
- The compiler will produce C source code.
- Tied to the codegen toolbox for NSP.
- Still a joint work with ENPC & ALTAIR.
SLIDE 16