Introduction

Logic and declarative programming is often and successfully used as parts of desktop and server applications. We value the declarative techniques because it is easier to write programs that relate closely to the specification (or even write compilable specifications) and show their correctness. Declarative programming has found its place in most computer science curricula in some form (often Haskell and Prolog) as well. But especially in logic programming the applications often are theoretical or only used as part of a larger system. The parts that interact with the outside world are usually written in an imperative fashion. For embedded systems, where rule-based interaction with the outside world is often the majority of the application, declarative programming is an avenue not well explored.

Microlog is a specific dialect of Datalog closely related to the Dedalus language that includes IO operations. Our declarative language Microlog is a language that allows us to model both program state and side effects in a declarative manner. Based on Datalog, Microlog has strong foundations in relational algebra and logic. As we do not allow ad-hoc queries or a dynamic extensional database, we can split our logic program into disjoint parts; the parts that provably use finite memory over time are compiled into finite state machines while the residual program is evaluated with a naive algorithm that is optimized to use as little memory as possible. Our goal is to declaratively program microcontrollers and give static safety guarantees for data-driven interactive programs on microcontrollers

Publications and Related Work

Our Publications

Related Work

Language and Semantics

microlog is a variant of Datalog. Every predicate has an additional implicit argument that represents time.
A normal datalog rule (deductive rule) p(a1,a2,..) :- q(a1,a2,..). actually means a deduction in the current timestamp: p(T,a1,a2,..) :- q(T,a1,a2,..).
Inductive rules p(a1,a2,..)@next :- q(a1,a2,..). actually means a deduction in the next timestamp: p(S,a1,a2,..) :- q(T,a1,a2,..), successor(T,S).
In inductive rules, we addionally allow a call to an external source of computation.

Static Analysis (Memory Restriction and Safety Guarantees)

We have different approaches of giving static guarantees about microlog programs. Our current focus are safety guarantees.

Example (heating control)

We can easily describe a system where the heating not only shuts off in a room with an open window (a common use case for home automation), but also in all (other) rooms connected via open doors (see Figure 2). The function calls are from a fictitious library that wraps communication to the sensors and actors, #open is a C constant from that library. It is usually the case that control to hardware is provided by some library, as protocols like I2C need to be observed or read analog voltage levels need to be translated to values in expected units. The static rules of hasWindow and adjacent are the configuration of our program to our specific example home with two connected rooms where the second room has window. A user, if this program was provided to them, would only need to add those facts to configure it for their home.
You can see the full example in the down below under heating/lopstr.dl

The initial state in this example is state 0. The variables (memory locations) for the return values are called slots. In this example we need two slots. One for the return value of each sensor (one door, one window).
The transition function is normalized to disjunctive normal form.

The transition code is executed in the loop-function provided by the runtime. This allows to run multiple FSMs and normal deduction steps in lockstep. In the Arduino runtime the infinite loop is implicit, in the LEGO EV3 runtime it is explicit.

byte _fsm1_state = 0;
int _fsm1_slot__1;
int _fsm1_slot__2;

void loop()
{
if (_fsm1_state == 0) {
  { int Doorstate = doorstate(1, 2); _fsm1_slot__1 = Doorstate;}
  { int State = windowRead(2); _fsm1_slot__2 = State;}
  {if (!(_fsm1_slot__2 == open)) {_fsm1_state = 0} else
   if ((_fsm1_slot__1 == open) && (_fsm1_slot__2 == open)) {_fsm1_state = 1} else
   if ((_fsm1_slot__2 == open) && !(_fsm1_slot__1 == open)) {_fsm1_state = 2} else ;}
  } else
if (_fsm1_state == 1) {
  { heating_off(1);  }
  { heating_off(2);  }
  { int Doorstate = doorstate(1, 2); _fsm1_slot__1 = Doorstate;}
  { int State = windowRead(2); _fsm1_slot__2 = State;}
  {if (!(_fsm1_slot__2 == open)) {_fsm1_state = 0} else
   if ((_fsm1_slot__1 == open) && (_fsm1_slot__2 == open)) {_fsm1_state = 1} else
   if ((_fsm1_slot__2 == open) && !(_fsm1_slot__1 == open)) {_fsm1_state = 2} else ;}
  } else
if (_fsm1_state == 2) {
  { heating_off(2);  }
  { int Doorstate = doorstate(1, 2); _fsm1_slot__1 = Doorstate;}
  { int State = windowRead(2); _fsm1_slot__2 = State;}
  {if (!(_fsm1_slot__2 == open)) {_fsm1_state = 0} else
   if ((_fsm1_slot__1 == open) && (_fsm1_slot__2 == open)) {_fsm1_state = 1} else
   if ((_fsm1_slot__2 == open) && !(_fsm1_slot__1 == open)) {_fsm1_state = 2} else ;}
  } else;
}

Runtime and Compilation

Our current compiler is written in Haskell and translates Microlog programs to C with a runtime that executes the rules in every state. Partial programs that can be represented as finite state machines are compiled that way.

Example programs with an Arduino and Lego EV3 runtime. Some programs show specific optimizations or transformations (toy), while others show specific application. The example programs have last been compiled {{microlog.update}}.

A protoype compiler is available here.
Download the example compilation data here.

Further Examples

Please wait while the example sources are loading...
Choose Program:
Choose Setting:
Choose Template