-
Notifications
You must be signed in to change notification settings - Fork 1
refactor: replace invariants with conditions #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
hono0130
wants to merge
5
commits into
goatx:main
from
hono0130:feature/refactor-invariant-using-condition
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
86d5d37
refactor: replace invariants with conditions
hono0130 0cde154
refactor temporal rule tests
hono0130 4e35c38
Merge pull request #11 from hono0130/codex/add-ltl-pattern-checking-f…
hono0130 b16c2ce
chore: streamline temporal rule example comments
hono0130 9ba1de3
Merge pull request #13 from hono0130/codex/fix-lint-errors
hono0130 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,6 +1,6 @@ | ||||||
# goat | ||||||
|
||||||
A Go library for model checking concurrent systems using state machines. goat helps you verify the correctness of distributed systems by exhaustively exploring all possible states and checking invariants. | ||||||
A Go library for model checking concurrent systems using state machines. goat helps you verify the correctness of distributed systems by exhaustively exploring all possible states and checking conditions/invariants. | ||||||
|
||||||
## Installation | ||||||
|
||||||
|
@@ -56,12 +56,18 @@ func main() { | |||||
if err != nil { | ||||||
log.Fatal(err) | ||||||
} | ||||||
cond := goat.NewCondition("counter<=2", sm, func(sm *MyStateMachine) bool { | ||||||
return sm.Counter <= 2 | ||||||
}) | ||||||
|
||||||
err = goat.Test( | ||||||
goat.WithStateMachines(sm), | ||||||
goat.WithInvariants( | ||||||
goat.NewInvariant(sm, func(sm *MyStateMachine) bool { | ||||||
return sm.Counter <= 2 | ||||||
}), | ||||||
goat.WithConditions(cond), | ||||||
goat.WithInvariants(cond), | ||||||
goat.WithTemporalRules( | ||||||
goat.WheneverPEventuallyQ(cond, cond), | ||||||
goat.EventuallyAlways(cond), | ||||||
goat.AlwaysEventually(cond), | ||||||
), | ||||||
) | ||||||
if err != nil { | ||||||
|
@@ -79,7 +85,9 @@ func main() { | |||||
- **`Test()`** - Run model checking with invariant verification | ||||||
- **`Debug()`** - Output detailed JSON results for debugging | ||||||
- **`WithStateMachines()`** - Configure which state machines to test | ||||||
- **`WithInvariants()`** - Configure invariants to check | ||||||
- **`WithConditions()`** - Register named conditions | ||||||
- **`WithInvariants()`** - Configure conditions to check as invariants | ||||||
- **`WithTemporalRules()`** - Register temporal rules to verify temporal properties | ||||||
|
||||||
## Examples | ||||||
|
||||||
|
@@ -97,21 +105,21 @@ Run any example: | |||||
go run ./example/simple-transition | ||||||
``` | ||||||
|
||||||
### Multi state machine invariants | ||||||
### Multi state machine conditions | ||||||
|
||||||
- `NewMultiInvariant(check func(Machines) bool, sms ...AbstractStateMachine)` — reference multiple machines in one invariant | ||||||
- `NewInvariant2` / `NewInvariant3` — convenience wrappers for 2 or 3 machines | ||||||
- `NewMultiCondition(name string, check func(Machines) bool, sms ...AbstractStateMachine)` — reference multiple machines in one condition | ||||||
- `NewCondition2` / `NewCondition3` — convenience wrappers for 2 or 3 machines | ||||||
- `Machines` + `GetMachine[T]` — type-safe access to referenced machines during evaluation | ||||||
|
||||||
```go | ||||||
inv := goat.NewInvariant2(primary, replica, func(p *Storage, r *Storage) bool { | ||||||
cond := goat.NewCondition2("replica", primary, replica, func(p *Storage, r *Storage) bool { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// Simple consistency: every key in primary exists in replica with the same value | ||||||
for key, pv := range p.Data { | ||||||
rv, ok := r.Data[key] | ||||||
if !ok || rv != pv { | ||||||
return false | ||||||
} | ||||||
} | ||||||
return true | ||||||
return true | ||||||
}) | ||||||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
package goat | ||
|
||
// ConditionName represents the identifier for a condition. | ||
type ConditionName string | ||
|
||
// Condition represents a named predicate evaluated against a world. | ||
// Implementations must return true when the condition holds for the | ||
// provided world state, and false otherwise. | ||
type Condition interface { | ||
Name() ConditionName | ||
Evaluate(w world) bool | ||
} | ||
|
||
type conditionFunc struct { | ||
name ConditionName | ||
fn func(w world) bool | ||
} | ||
|
||
func (f conditionFunc) Name() ConditionName { return f.name } | ||
func (f conditionFunc) Evaluate(w world) bool { return f.fn(w) } | ||
|
||
// BoolCondition creates a condition from a constant boolean value. | ||
// This is useful for creating conditions that always pass (true) or always | ||
// fail (false), typically used for testing or as placeholder conditions. | ||
// | ||
// Parameters: | ||
// - name: The name of this condition | ||
// - b: The boolean value that this condition will always return | ||
// | ||
// Returns a Condition that can be used with Test() or WithInvariants(). | ||
// | ||
// Example: | ||
// | ||
// alwaysPass := goat.BoolCondition("pass", true) | ||
// alwaysFail := goat.BoolCondition("fail", false) | ||
func BoolCondition(name string, b bool) Condition { | ||
return conditionFunc{name: ConditionName(name), fn: func(w world) bool { return b }} | ||
} | ||
|
||
// NewCondition creates a condition for a specific state machine instance. | ||
// It allows checking properties of that particular state machine during | ||
// model exploration and testing. | ||
// | ||
// Parameters: | ||
// - name: The condition name | ||
// - sm: The state machine instance to create a condition for | ||
// - check: A predicate function that returns true if the condition holds | ||
// | ||
// Returns a Condition that can be used with Test() or WithInvariants(). | ||
// | ||
// Example: | ||
// | ||
// serverCond := goat.NewCondition("conn-limit", serverSM, func(sm *ServerStateMachine) bool { | ||
// return sm.ConnectionCount <= sm.MaxConnections | ||
// }) | ||
func NewCondition[T AbstractStateMachine](name string, sm T, check func(T) bool) Condition { | ||
id := sm.id() | ||
return conditionFunc{name: ConditionName(name), fn: func(w world) bool { | ||
machine, exists := w.env.machines[id] | ||
if !exists { | ||
return false | ||
} | ||
typedMachine, ok := machine.(T) | ||
if !ok { | ||
return false | ||
} | ||
return check(typedMachine) | ||
}} | ||
} | ||
|
||
// Machines provides type-safe access to state machines during condition evaluation. | ||
// It is used inside check functions to reference multiple state machines. | ||
// | ||
// Implementations return false when the requested state machine does not exist | ||
// in the current world. | ||
type Machines interface { | ||
Get(sm AbstractStateMachine) (AbstractStateMachine, bool) | ||
} | ||
|
||
type machinesImpl struct { | ||
world world | ||
} | ||
|
||
func (m *machinesImpl) Get(sm AbstractStateMachine) (AbstractStateMachine, bool) { | ||
id := sm.id() | ||
machine, exists := m.world.env.machines[id] | ||
if !exists { | ||
return nil, false | ||
} | ||
return machine, true | ||
} | ||
|
||
// GetMachine provides type-safe access to a state machine from Machines. | ||
// | ||
// Parameters: | ||
// - m: Machines accessor provided to the check function | ||
// - sm: A sample instance used to identify the target machine by ID | ||
// | ||
// Returns the typed state machine and true on success. Returns the zero value | ||
// and false when the machine does not exist or the type does not match. | ||
// | ||
// Example: | ||
// | ||
// machine, ok := goat.GetMachine(machines, client) | ||
// if !ok { return false } | ||
// state := machine.currentState().(*ClientState) | ||
func GetMachine[T AbstractStateMachine](m Machines, sm T) (T, bool) { | ||
am, ok := m.Get(sm) | ||
if !ok { | ||
var zero T | ||
return zero, false | ||
} | ||
typed, ok := am.(T) | ||
if !ok { | ||
var zero T | ||
return zero, false | ||
} | ||
return typed, true | ||
} | ||
|
||
// NewMultiCondition creates a condition that can reference multiple state machines. | ||
// The provided check function receives a Machines accessor. | ||
// | ||
// Parameters: | ||
// - name: The condition name | ||
// - checkFunc: Predicate that inspects one or more state machines | ||
// - sms: State machines referenced by the condition | ||
// | ||
// Returns a Condition that can be used with Test() or WithInvariants(). | ||
// | ||
// Example: | ||
// | ||
// func NewConditionClientServer(client *Client, server *Server, check func(*Client, *Server) bool) goat.Condition { | ||
// return goat.NewMultiCondition("client-server", func(machines goat.Machines) bool { | ||
// c, ok1 := goat.GetMachine(machines, client) | ||
// if !ok1 { return false } | ||
// s, ok2 := goat.GetMachine(machines, server) | ||
// if !ok2 { return false } | ||
// | ||
// return check(c, s) | ||
// }, client, server) | ||
// } | ||
// | ||
// // Usage | ||
// cond := NewConditionClientServer(client, server, func(c *Client, s *Server) bool { | ||
// // business logic referencing both machines | ||
// return c.Server != nil && s != nil | ||
// }) | ||
func NewMultiCondition(name string, checkFunc func(Machines) bool, sms ...AbstractStateMachine) Condition { | ||
return conditionFunc{name: ConditionName(name), fn: func(w world) bool { | ||
m := &machinesImpl{world: w} | ||
for _, sm := range sms { | ||
if _, ok := m.Get(sm); !ok { | ||
return false | ||
} | ||
} | ||
return checkFunc(m) | ||
}} | ||
} | ||
|
||
// NewCondition2 creates a condition that references two state machines. | ||
// | ||
// Parameters: | ||
// - name: The condition name | ||
// - sm1, sm2: The state machines to reference | ||
// - check: A predicate function that returns true if the condition holds | ||
// | ||
// Returns a Condition that can be used with Test() or WithInvariants(). | ||
// | ||
// Example: | ||
// | ||
// cond := goat.NewCondition2("pair", client, server, func(c *Client, s *Server) bool { | ||
// return true | ||
// }) | ||
func NewCondition2[T1, T2 AbstractStateMachine](name string, sm1 T1, sm2 T2, check func(T1, T2) bool) Condition { | ||
return NewMultiCondition(name, func(ms Machines) bool { | ||
m1, ok := GetMachine(ms, sm1) | ||
if !ok { | ||
return false | ||
} | ||
m2, ok := GetMachine(ms, sm2) | ||
if !ok { | ||
return false | ||
} | ||
return check(m1, m2) | ||
}, sm1, sm2) | ||
} | ||
|
||
// NewCondition3 creates a condition that references three state machines. | ||
// | ||
// Parameters: | ||
// - name: The condition name | ||
// - sm1, sm2, sm3: The state machines to reference | ||
// - check: A predicate function that returns true if the condition holds | ||
// | ||
// Returns a Condition that can be used with Test() or WithInvariants(). | ||
// | ||
// Example: | ||
// | ||
// cond := goat.NewCondition3("triple", client, server, db, func(c *Client, s *Server, d *Database) bool { | ||
// return true | ||
// }) | ||
func NewCondition3[T1, T2, T3 AbstractStateMachine](name string, sm1 T1, sm2 T2, sm3 T3, check func(T1, T2, T3) bool) Condition { | ||
return NewMultiCondition(name, func(ms Machines) bool { | ||
m1, ok := GetMachine(ms, sm1) | ||
if !ok { | ||
return false | ||
} | ||
m2, ok := GetMachine(ms, sm2) | ||
if !ok { | ||
return false | ||
} | ||
m3, ok := GetMachine(ms, sm3) | ||
if !ok { | ||
return false | ||
} | ||
return check(m1, m2, m3) | ||
}, sm1, sm2, sm3) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.