Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions README.md
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

Expand Down Expand Up @@ -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 {
Expand All @@ -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

Expand All @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `NewCondition2` / `NewCondition3` — convenience wrappers for 2 or 3 machines
- `NewCondition2` / `NewCondition3` — convenience wrappers for two or three 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 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cond := goat.NewCondition2("replica", primary, replica, func(p *Storage, r *Storage) bool {
cond := goat.NewCondition2("replication", primary, replica, func(p *Storage, r *Storage) bool {

// 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
})
```
219 changes: 219 additions & 0 deletions condition.go
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)
}
Loading
Loading