Repository navigation
Expand file tree
/
Copy pathSystem.lean
More file actions
54 lines (48 loc) · 1.68 KB
/
Copy pathSystem.lean
File metadata and controls
54 lines (48 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/-
This is a specification of the system level of the two-phase commit protocol.
Copyright (c) 2025 Igor Konnov
Released under MIT license as described in the file LICENSE.
Authors: Igor Konnov, 2025
-/
import Twophase.Functional
-- The abstract type of resource managers.
variable {RM : Type} [DecidableEq RM] [Hashable RM] [Repr RM]
/--
Since Lean4 is not TLA+, it does not have a built-in syntax for actions.
Hence, we introduce the type that essentially turns control and data
non-deterministm into inputs. This trick is usually called a "schedule"
or "adversary" in the literature.
-/
inductive Action where
| TMCommit
| TMAbort
| TMRcvPrepared(rm: RM)
| RMPrepare(rm: RM)
| RMChooseToAbort(rm: RM)
| RMRcvCommitMsg(rm: RM)
| RMRcvAbortMsg(rm: RM)
deriving DecidableEq, Repr
/-- initialize the state of all resource managers to `Working` -/
def init_rm_state (all: List RM) :=
all.foldl
(fun m rm => m.insert rm RMState.Working)
(Std.HashMap.emptyWithCapacity 0)
/-- The initial state of the protocol -/
def init (all: List RM): ProtocolState RM := {
all := all.toFinset,
rmState := init_rm_state all,
tmState := TMState.Init,
tmPrepared := ∅,
msgs := ∅
}
/-- The transition function (!) of the protocol. Since we provide `next` with
the action as an argument, `next` is a function, not a relation. -/
def next s a :=
match a with
| Action.TMCommit => tmCommit RM s
| Action.TMAbort => tmAbort _ s
| Action.TMRcvPrepared rm => tmRcvPrepared _ s rm
| Action.RMPrepare rm => rmPrepare _ s rm
| Action.RMChooseToAbort rm => rmChooseToAbort _ s rm
| Action.RMRcvCommitMsg rm => rmRcvCommitMsg _ s rm
| Action.RMRcvAbortMsg rm => rmRcvAbortMsg _ s rm