javascript
module main
{
  type state_t = enum { HEATING, COOLING };

  var state : state_t;
  var temp : real;
  var heatOn, heatOff : boolean;

  const setpoint : real = 20.0;
  const upperThreshold : real = 22.0;
  const lowerThreshold : real = 18.0;

  init {
    state = COOLING; // Start in cooling state
    temp = setpoint;
    heatOn = false;
    heatOff = false;
  }

  procedure update_state()
    modifies state;
  {
    if (state == HEATING && temp >= upperThreshold) {
      state = COOLING;
    } else if (state == COOLING && temp <= lowerThreshold) {
      state = HEATING;
    }
  }

  procedure update_outputs()
    modifies heatOn;
    modifies heatOff;
  {
    if (state == COOLING && temp <= setpoint) {
      heatOn = true;
      heatOff = false;
    } else if (state == HEATING && temp >= setpoint) {
      heatOn = false;
      heatOff = true;
    } else {
      heatOn = false;
      heatOff = false;
    }
  }

  next {
    havoc temp; // Input temperature
    call update_state();
    call update_outputs();
  }
}
