+
+ if (empty()) {
+ return std::nullopt;
+ }
+
+ PartialExecution v;
+ std::vector<Execution::EventHandle> v_handles;
+ std::unordered_set<aid_t> WI_E_prime_v;
+ std::unordered_set<aid_t> disqualified_actors;
+ Execution E_prime_v = get_prefix_before(e);
+ const std::unordered_set<aid_t> sleep_E_prime = state_at_e.get_sleeping_actors();
+
+ // Note `e + 1` here: `notdep(e, E)` is defined as the
+ // set of events that *occur-after* but don't *happen-after* `e`
+ //
+ // SUBTLE NOTE: ODPOR requires us to compute `notdep(e, E)` EVEN THOUGH
+ // the race is between `e` and `e'`; that is, events occurring in `E`
+ // that "occur-after" `e'` may end up in the partial execution `v`.
+ //
+ // Observe that `notdep(e, E).proc(e')` will contain all transitions
+ // that don't happen-after `e` in the order they appear FOLLOWED BY
+ // THE **TRANSITION** ASSOCIATED WITH **`e'`**!!
+ //
+ // SUBTLE NOTE: Observe that any event that "happens-after" `e'`
+ // must necessarily "happen-after" `e` as well, since `e` and
+ // `e'` are presumed to be in a reversible race. Hence, we know that
+ // all events `e_star` such that `e` "happens-before" `e_star` cannot affect
+ // the enabledness of `e'`; furthermore, `e'` cannot affect the enabledness
+ // of any event independent with `e` that "occurs-after" `e'`
+ for (auto e_star = e + 1; e_star <= get_latest_event_handle().value(); ++e_star) {
+ // Any event `e*` which occurs after `e` but which does not
+ // happen after `e` is a member of `v`. In addition to marking
+ // the event in `v`, we also "simulate" running the action `v` from E'
+ // to be able to compute `--->[E'.v]`
+ if (not happens_before(e, e_star)) {
+ xbt_assert(e_star != e_prime,
+ "Invariant Violation: We claimed events %u and %u were in a reversible race, yet we also "
+ "claim that they do not happen-before one another. This is impossible: "
+ "are you sure that the two events are in a reversible race?",
+ e, e_prime);
+ E_prime_v.push_transition(get_event_with_handle(e_star).get_transition());
+ v.push_back(get_event_with_handle(e_star).get_transition());
+
+ const EventHandle e_star_in_E_prime_v = E_prime_v.get_latest_event_handle().value();
+
+ // When checking whether any event in `dom_[E'](v)` happens before
+ // `next_[E'](q)` below for thread `q`, we must consider that the
+ // events relative to `E` (this execution) are different than those
+ // relative to `E'.v`. Thus e.g. event `7` in `E` may be event `4`
+ // in `E'.v`. Since we are asking about "happens-before"
+ // `-->_[E'.v]` about `E'.v`, we must build `v` relative to `E'`
+ v_handles.push_back(e_star_in_E_prime_v);
+
+ // Note that we add `q` to v regardless of whether `q` itself has been
+ // disqualified since `q` may itself disqualify other actors
+ // (i.e. even if `q` is disqualified from being an initial, it
+ // is still contained in the sequence `v`)
+ const aid_t q = E_prime_v.get_actor_with_handle(e_star_in_E_prime_v);
+ if (disqualified_actors.count(q) > 0) { // Did we already note that `q` is not an initial?
+ continue;
+ }
+ const bool is_initial = std::none_of(v_handles.begin(), v_handles.end(), [&](const auto& handle) {
+ return E_prime_v.happens_before(handle, e_star_in_E_prime_v);
+ });
+ if (is_initial) {
+ // If the sleep set already contains `q`, we're done:
+ // we've found an initial contained in the sleep set and
+ // so the intersection is non-empty
+ if (sleep_E_prime.count(q) > 0) {
+ return std::nullopt;
+ } else {
+ WI_E_prime_v.insert(q);
+ }
+ } else {
+ // If `q` is disqualified as a candidate, clearly
+ // no event occurring after `e_prime` in `E` executed
+ // by actor `q` will qualify since any (valid) happens-before
+ // relation orders actions taken by each actor
+ disqualified_actors.insert(q);
+ }
+ }
+ }
+
+ // Now we add `e_prime := <q, i>` to `E'.v` and repeat the same work
+ // It's possible `proc(e_prime)` is an initial
+ //
+ // Note the form of `v` in the pseudocode:
+ // `v := notdep(e, E).e'^
+ E_prime_v.push_transition(get_event_with_handle(e_prime).get_transition());
+ v.push_back(get_event_with_handle(e_prime).get_transition());
+
+ const EventHandle e_prime_in_E_prime_v = E_prime_v.get_latest_event_handle().value();
+ v_handles.push_back(e_prime_in_E_prime_v);
+
+ const bool is_initial = std::none_of(v_handles.begin(), v_handles.end(), [&](const auto& handle) {
+ return E_prime_v.happens_before(handle, e_prime_in_E_prime_v);
+ });
+ if (is_initial) {
+ if (const aid_t q = E_prime_v.get_actor_with_handle(e_prime_in_E_prime_v); sleep_E_prime.count(q) > 0) {
+ return std::nullopt;
+ } else {
+ WI_E_prime_v.insert(q);
+ }
+ }
+
+ const Execution pre_E_e = get_prefix_before(e);
+ const auto sleeping_actors = state_at_e.get_sleeping_actors();
+
+ // Check if any enabled actor that is independent with
+ // this execution after `v` is contained in the sleep set
+ for (const auto& [aid, astate] : state_at_e.get_actors_list()) {
+ const bool is_in_WI_E =
+ astate.is_enabled() and pre_E_e.is_independent_with_execution_of(v, astate.get_transition());
+ const bool is_in_sleep_set = sleeping_actors.count(aid) > 0;
+
+ // `action(aid)` is in `WI_[E](v)` but also is contained in the sleep set.
+ // This implies that the intersection between the two is non-empty
+ if (is_in_WI_E && is_in_sleep_set)
+ return std::nullopt;
+ }
+
+ return v;
+}
+
+bool Execution::is_initial_after_execution_of(const PartialExecution& w, aid_t p) const
+{
+ auto E_w = *this;
+ std::vector<EventHandle> w_handles;
+ for (const auto& w_i : w) {
+ // Take one step in the direction of `w`
+ E_w.push_transition(w_i);
+
+ // If that step happened to be executed by `p`,
+ // great: we know that `p` is contained in `w`.
+ // We now need to verify that it doens't "happen-after"
+ // any events which occur before it
+ if (w_i->aid_ == p) {
+ const auto p_handle = E_w.get_latest_event_handle().value();
+ return std::none_of(w_handles.begin(), w_handles.end(),
+ [&](const auto handle) { return E_w.happens_before(handle, p_handle); });
+ } else {
+ w_handles.push_back(E_w.get_latest_event_handle().value());