Reactive Imperative Programming with Dataflow Constraints

Reactive Imperative Programming with Dataflow Constraints

Reactive Imperative Programming with Dataflow Constraints Camil Demetrescu Irene Finocchi Andrea Ribichini Dept. of Computer and System Sciences Dept. of Computer Science Dept. of Computer and System Sciences Sapienza University of Rome Sapienza University of Rome Sapienza University of Rome [email protected][email protected] [email protected] Abstract General Terms Algorithms, design, experimentation, languages. Dataflow languages provide natural support for specifying con- straints between objects in dynamic applications, where programs Keywords Reactive programming, dataflow programming, im- need to react efficiently to changes of their environment. Re- perative programming, constraint solving, incremental computa- searchers have long investigated how to take advantage of dataflow tion, observer design pattern, data structure repair. constraints by embedding them into procedural languages. Previ- ous mixed imperative/dataflow systems, however, require syntactic extensions or libraries of ad hoc data types for binding the imper- 1. Introduction ative program to the dataflow solver. In this paper we propose a A one-way, dataflow constraint is an equation of the form novel approach that smoothly combines the two paradigms without y = f(x1,...,xn) in which the formula on the right side placing undue burden on the programmer. is automatically re-evaluated and assigned to the variable In our framework, programmers can define ordinary commands y whenever any variable xi changes. If y is modified from of the host imperative language that enforce constraints between outside the constraint, the equation is left temporarily unsat- objects stored in special memory locations designated as “reac- isfied, hence the attribute “one-way”. Dataflow constraints tive”. Differently from previous approaches, reactive objects can are recognized as a powerful programming methodology in be of any legal type in the host language, including primitive data a variety of contexts because of their versatility and sim- types, pointers, arrays, and structures. Commands defining con- plicity [38]. The most widespread application of dataflow straints are automatically re-executed every time their input mem- constraints is perhaps embodied by spreadsheets [2, 28]. In ory locations change, letting a program behave like a spreadsheet a spreadsheet, the user can specify a cell formula that de- where the values of some variables depend upon the values of other pends on other cells: when any of those cells is updated, the variables. The constraint solving mechanism is handled transpar- value of the first cell is automatically recalculated. Rules in ently by altering the semantics of elementary operations of the host a makefile are another example of dataflow constraints: a language for reading and modifying objects. We provide a formal rule sets up a dependency between a target file and a list of semantics and describe a concrete embodiment of our technique input files, and provides shell commands for rebuilding the into C/C++, showing how to implement it efficiently in conven- target from the input files. When the makefile is run, if any tional platforms using off-the-shelf compilers. We discuss common input file in a rule is discovered to be newer than the tar- coding idioms and relevant applications to reactive scenarios, in- get, then the target is rebuilt. The dataflow principle can be arXiv:1104.2293v1 [cs.PL] 12 Apr 2011 cluding incremental computation, observer design pattern, and data also applied to software development and execution, where structure repair. The performance of our implementation is com- the role of a cell/file is replaced by a program variable. This pared to ad hoc problem-specific change propagation algorithms, as approach has been widely explored in the context of interac- well as to language-centric approaches such as self-adjusting com- tive applications, multimedia animation, and real-time sys- putation and subject/observer communication mechanisms, show- tems [13, 24, 34, 39]. ing that the proposed approach is efficient in practice. Since the values of program variables are automatically Categories and Subject Descriptors D.3.3 [Programming recalculated upon changes of other values, the dataflow com- Languages]: Language Constructs and Features—Constraints putational model is very different from the standard imper- ative model, in which the memory store is changed explic- itly by the program via memory assignments. The execution flow of applications running on top of a dataflow environ- ment is indeed data-driven, rather than control-driven, pro- viding a natural ground for automatic change propagation in all scenarios where programs need to react to modifications of their environment. Implementations of the dataflow prin- ciple share some common issues with self-adjusting compu- 1 2018/10/22 tation, in which programs respond to input changes by up- Address dereferencing: constraints are able to reference dating automatically their output [3, 4, 25]. variables indirectly via pointers. Differently from purely declarative constraints [7], data- Automatic dependency detection: constraints automatically flow constraints are expressed by means of (imperative) detect the reactive memory locations they depend on dur- methods whose execution makes a relation satisfied. This ing their evaluation, so there is no need for program- programming style is intuitive and readily accessible to a mers to explicitly declare dependencies, which are also broad range of developers [38], since the ability to smoothly allowed to vary over time. combine different paradigms in a unified framework makes it possible to take advantage of different programming styles We embodied these principles into an extension of C/C++ in the context of the same application. The problem of in- that we called DC. Our extension has exactly the same syn- tegrating imperative and dataflow programming has already tax as C/C++, but a different semantics. Our main contribu- been the focus of previous work in the context of specific tions are reflected in the organization of the paper and can application domains [11, 32–34, 38]. Previous mixed imper- be summarized as follows: ative/dataflow systems are based on libraries of ad hoc data types and functions for representing constraint variables and • In Section 2 we abstract our mechanism showing how for binding the imperative program to the constraint solver. to extend an elementary imperative language to support One drawback of these approaches is that constraint vari- one-way dataflow constraints using reactive memory. We ables can only be of special data types provided by the run- distinguish between three main execution modes: nor- time library, causing loss of flexibility and placing undue mal, constraint, and scheduling. We formally describe burden on the programmer.A natural question is whether the our mixed imperative/dataflow computational model by dataflow model can be made to work with general-purpose, defining the interactions between these modes and pro- imperative languages, such as C, without adding syntactic viding a formal semantics of our mechanism. extensions and ad hoc data types. In this paper we affirma- • In Section 3 we discuss convergence of the dataflow con- tively answer this question. straint solver by modeling the computation as an iterative Our Contributions. We present a general-purpose frame- process that aims at finding a common fixpoint for the work where programmers can specify generic one-way con- current set of constraints. We identify general constraint straints between objects of arbitrary types stored in reactive properties that let the solver terminate and converge to a memory locations. Constraints are written as ordinary com- common fixpoint independently of the scheduling strat- mands of the host imperative language and can be added egy. This provides a sound unifying framework for solv- and removed dynamically at run time. Since they can change ing both acyclic and cyclic constraint systems. multiple objects within the same execution, they are multi- • In Section 4 we describe the concrete embodiment of our output. The main feature of a constraint is its sensitivity to technique into C/C++, introducing the main features of modifications of reactive objects: a constraint is automati- DC. DC has exactly the same syntax as C/C++, but oper- cally re-evaluated whenever any of the reactive locations it ations that read or modify objects have a different seman- depends on is changed, either by the imperative program, or tics. All other primitives, including creating and deleting by another constraint. A distinguishing feature of our ap- constraints and allocating and deallocating reactive mem- proach is that the whole constraint solving mechanism is ory blocks, are provided as runtime library functions. handled transparently by altering the semantics of elemen- • In Section 5 we give a variety of elementary and ad- tary operations of the host imperative language for reading vanced programming examples and discuss how DC can and modifying objects. No syntax extensions are required improve C/C++ programmability in three relevant appli- and no new primitives are needed except for adding/remov- cation scenarios: incremental computation, implementa- ing constraints, allocating/deallocating reactive memory lo- tion of the observer software design pattern, and data cations, and controlling the granularity of solver activations. structure checking and repair. To the best of our knowl- Differently from previous approaches, programmers

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    23 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us