import Base type Outcome<-R: Type> is Type: Accepted{result: R} Refused{result: R} # What the store keeps for one command id: its payload, its outcome, and when it was recorded. type Record<-P: Type, -R: Type> is Type: Record{payload: P, outcome: Outcome, at: U32} type Reply<-R: Type> is Type: Fresh{outcome: Outcome} Replayed{outcome: Outcome} Conflict{} # The domain's answer to a new command. type Decision<-S: Type, -R: Type, -E: Type> is Type: Accept{state: S, result: R, effects: List} Refuse{result: R} # Write: commit the state, the record, and the effects together. Note: commit only the record. Keep: write nothing. type Step<-S: Type, -P: Type, -R: Type, -E: Type> is Type: Write{state: S, record: Record, reply: Reply, effects: List} Note{record: Record, reply: Reply} Keep{reply: Reply} def accepted(-S: Type, -P: Type, -R: Type, -E: Type, rr: R & R, s2: S, p: P, at: U32, es: List) -> Step: (r, r2) = rr Write{s2, Record{p, Accepted{r}, at}, Fresh{Accepted{r2}}, es} def refused(-S: Type, -P: Type, -R: Type, -E: Type, rr: R & R, p: P, at: U32) -> Step: (r, r2) = rr Note{Record{p, Refused{r}, at}, Fresh{Refused{r2}}} def decided(~S: Type, ~P: Type, ~R: Type, ~E: Type, ~dup_r: R -> R & R, d: Decision, p: P, at: U32) -> Step: match d: case Accept{s2, r, es}: accepted(S, P, R, E, dup_r(r), s2, p, at, es) case Refuse{r}: refused(S, P, R, E, dup_r(r), p, at) def fresh( ~S: Type, ~P: Type, ~R: Type, ~E: Type, ~dup_r: R -> R & R, ~decide: S -> P -> Decision, pp: P & P, s: S, at: U32 ) -> Step: (p, p2) = pp decided(~S, ~P, ~R, ~E, ~dup_r, decide(s, p), p2, at) def replayed(-R: Type, same: Bool, o: Outcome) -> Reply: match same: case True{}: Replayed{o} case False{}: Conflict{} # One command, given the record that the store holds for its id and the domain state s. # dup_p and dup_r copy a payload and a result: a new command's payload goes to decide and to the record. def step( ~S: Type, ~P: Type, ~R: Type, ~E: Type, ~eq: P -> P -> Bool, ~dup_p: P -> P & P, ~dup_r: R -> R & R, ~decide: S -> P -> Decision, found: Maybe>, s: S, p: P, at: U32 ) -> Step: match found: case None{}: fresh(~S, ~P, ~R, ~E, ~dup_r, ~decide, dup_p(p), s, at) case Some{Record{q, o, t}}: Keep{replayed(R, eq(q, p), o)}