-
Notifications
You must be signed in to change notification settings - Fork 180
feat(CCS): Milner's Vending Machine #764
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
Open
fmontesi
wants to merge
8
commits into
main
Choose a base branch
from
fmontesi/ccs-vending-machine
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1e484c0
move from copilot-instructions to AGENTS.md
fmontesi ff82bd4
CCS vending machine and LTS utilities
fmontesi 0678fca
exe mk_all
fmontesi 8176585
modularise vending machine
fmontesi 1856dfa
review comments
fmontesi 5770b26
Merge branch 'main' into fmontesi/ccs-vending-machine
fmontesi 90f5940
review comments: better names
fmontesi d764846
flip match_deterministic
fmontesi 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
Some comments aren't visible on the classic Files Changed page.
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
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,96 @@ | ||
| /- | ||
| Copyright (c) 2026 Fabrizio Montesi. All rights reserved. | ||
| Released under Apache 2.0 license as described in the file LICENSE. | ||
| Authors: Fabrizio Montesi | ||
| -/ | ||
|
|
||
| module | ||
|
|
||
| public import Cslib.Languages.CCS.Semantics | ||
| public import Cslib.Foundations.Semantics.LTS.Bisimulation | ||
| public import Cslib.Foundations.Semantics.LTS.TraceEq | ||
| public import Mathlib.Tactic.FinCases | ||
|
|
||
| /-! # Milner's Vending Machine | ||
|
|
||
| This file formalises Milner's vending machine example for CCS. | ||
|
|
||
| We formalise two versions: | ||
| - A machine with a deterministic LTS: `coin.(tea.VM + coffee.VM)`. | ||
| - A machine with a nondeterministic LTS: `coin.tea.VM + coin.coffee.VM`. | ||
|
|
||
| We then prove the classical example that the two are not bisimilar. | ||
|
|
||
| Future work on proving that the two vending machines are trace equivalent would be | ||
| welcome. | ||
| -/ | ||
|
|
||
| @[expose] public section | ||
|
|
||
| namespace Cslib.Algorithms.CCS.VendingMachine | ||
|
|
||
| open Cslib.CCS Process Act | ||
| open scoped LTS | ||
|
|
||
| /-! Action names. -/ | ||
|
|
||
| /-- Insert a coin. -/ | ||
| abbrev Coin := name "coin" | ||
|
|
||
| /-- Tea request. -/ | ||
| abbrev Tea := name "tea" | ||
|
|
||
| /-- Coffee request. -/ | ||
| abbrev Coffee := name "coffee" | ||
|
|
||
| /-- Constants. -/ | ||
| inductive Constant | ||
| | vm | ||
|
|
||
| /-- The vending machine process. -/ | ||
| def vm : Process String Constant := const .vm | ||
|
|
||
| /-! ## Deterministic vending machine -/ | ||
|
|
||
| /-- Constant definitions: vm = coin.(tea.VM + coffee.VM) -/ | ||
| @[local grind =] | ||
| def vendingDefs : Constant → Option (Process String Constant) | ||
| | .vm => some <| pre Coin (choice (pre Tea (const .vm)) (pre Coffee (const .vm))) | ||
|
|
||
| /-- The LTS of CCS for the deterministic vending machine. -/ | ||
| abbrev ltsD := CCS.lts (defs := vendingDefs) | ||
|
|
||
| /-- VM can perform a coin action. -/ | ||
| example : ltsD.Tr vm Coin (choice (pre Tea (const .vm)) (pre Coffee (const .vm))) := | ||
| Tr.const rfl Tr.pre | ||
|
|
||
| /-! ## Nondeterministic vending machine -/ | ||
|
|
||
| /-- vm = coin.tea.VM + coin.coffee.VM -/ | ||
| def vendingDefsND : Constant → Option (Process String Constant) | ||
| | .vm => some <| (choice (pre Coin (pre Tea (const .vm))) (pre Coin (pre Coffee (const .vm)))) | ||
|
|
||
| /-- The LTS of CCS for the nondeterministic vending machine. -/ | ||
| abbrev ltsND := CCS.lts (defs := vendingDefsND) | ||
|
|
||
| open LTS LTS.IsBisimulation LTS.Bisimilarity | ||
|
|
||
| /-- The deterministic and nondeterministic vending machines are not bisimilar. -/ | ||
| theorem vm_ltsD_ltsND_not_bisim : ¬(vm ~[ltsD, ltsND] vm) := by | ||
| rintro ⟨r, hr, hbisim⟩ | ||
| let p₁ := (choice (pre Tea (const Constant.vm)) (pre Coffee (const Constant.vm))) | ||
| let q₁ := (pre Tea (const Constant.vm)) | ||
| have ltsD_vm_deterministic : ltsD.DeterministicStateLabel vm Coin := by | ||
| intro _ _ htr₁ htr₂ | ||
| grind [const_tr htr₁, const_tr htr₂] | ||
| have h : r p₁ q₁ := | ||
| match_deterministic | ||
| hbisim hr | ||
| ltsD_vm_deterministic | ||
| (.const rfl .pre) | ||
| (.const rfl (.choiceL .pre)) | ||
| have hp₁q₁ : p₁ ~[ltsD, ltsND] q₁ := by grind | ||
| have hp₁coffee : ltsD.Tr p₁ Coffee (const Constant.vm) := .choiceR .pre | ||
| grind [hp₁q₁.follow_fst] | ||
|
|
||
| end Cslib.Algorithms.CCS.VendingMachine |
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
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 | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -83,30 +83,73 @@ def IsBisimulation (lts₁ : LTS State₁ Label) (lts₂ : LTS State₂ Label) | |||||||||||||||||||
| (∀ s₂', lts₂.Tr s₂ μ s₂' → ∃ s₁', lts₁.Tr s₁ μ s₁' ∧ r s₁' s₂') | ||||||||||||||||||||
| ) | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /-- Helper for following a transition by the first state in a pair of a `Bisimulation`. -/ | ||||||||||||||||||||
| theorem IsBisimulation.follow_fst | ||||||||||||||||||||
| (hb : IsBisimulation lts₁ lts₂ r) (hr : r s₁ s₂) (htr : lts₁.Tr s₁ μ s₁') : | ||||||||||||||||||||
| ∃ s₂', lts₂.Tr s₂ μ s₂' ∧ r s₁' s₂' := | ||||||||||||||||||||
| (hb hr μ).1 _ htr | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /-- Helper for following a transition by the second state in a pair of a `Bisimulation`. -/ | ||||||||||||||||||||
| theorem IsBisimulation.follow_snd | ||||||||||||||||||||
| (hb : IsBisimulation lts₁ lts₂ r) (hr : r s₁ s₂) (htr : lts₂.Tr s₂ μ s₂') : | ||||||||||||||||||||
| ∃ s₁', lts₁.Tr s₁ μ s₁' ∧ r s₁' s₂' := | ||||||||||||||||||||
| (hb hr μ).2 _ htr | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /-! ## Relation to simulation -/ | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /-- Any bisimulation is also a simulation. -/ | ||||||||||||||||||||
| theorem IsBisimulation.isSimulation : IsBisimulation lts₁ lts₂ r → IsSimulation lts₁ lts₂ r := by | ||||||||||||||||||||
| grind [IsBisimulation, IsSimulation] | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /-- The inverse of a bisimulation is a simulation. -/ | ||||||||||||||||||||
| theorem IsBisimulation.flip_isSimulation : | ||||||||||||||||||||
| IsBisimulation lts₁ lts₂ r → IsSimulation lts₂ lts₁ (flip r) := by | ||||||||||||||||||||
| grind [IsBisimulation, IsSimulation, flip] | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /-- A relation is a bisimulation iff both it and its inverse are simulations. -/ | ||||||||||||||||||||
| theorem IsBisimulation.isSimulation_iff : | ||||||||||||||||||||
| IsBisimulation lts₁ lts₂ r ↔ (IsSimulation lts₁ lts₂ r ∧ IsSimulation lts₂ lts₁ (flip r)) := by | ||||||||||||||||||||
| have _ (s₁ s₂) : r s₁ s₂ → flip r s₂ s₁ := id | ||||||||||||||||||||
| grind [IsBisimulation, IsSimulation, flip] | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /-- Helper for following a transition by the first state in a pair of a `Bisimulation`. -/ | ||||||||||||||||||||
| theorem IsBisimulation.follow_fst | ||||||||||||||||||||
| (hb : IsBisimulation lts₁ lts₂ r) (hr : r s₁ s₂) (htr : lts₁.Tr s₁ μ s₁') : | ||||||||||||||||||||
| ∃ s₂', lts₂.Tr s₂ μ s₂' ∧ r s₁' s₂' := | ||||||||||||||||||||
| IsSimulation.follow hb.isSimulation hr htr | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /-- Helper for following a transition by the second state in a pair of a `Bisimulation`. -/ | ||||||||||||||||||||
| theorem IsBisimulation.follow_snd | ||||||||||||||||||||
| (hb : IsBisimulation lts₁ lts₂ r) (hr : r s₁ s₂) (htr : lts₂.Tr s₂ μ s₂') : | ||||||||||||||||||||
| ∃ s₁', lts₁.Tr s₁ μ s₁' ∧ r s₁' s₂' := | ||||||||||||||||||||
| IsSimulation.follow hb.flip_isSimulation hr htr | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /-- If the unique transition of a state is matched by a related state in a bisimulation, | ||||||||||||||||||||
| then the derivatives are still in the bisimulation. -/ | ||||||||||||||||||||
| theorem IsBisimulation.match_deterministic | ||||||||||||||||||||
| (hb : IsBisimulation lts₁ lts₂ r) | ||||||||||||||||||||
| (hr : r s₁ s₂) | ||||||||||||||||||||
| (hdet : lts₁.DeterministicStateLabel s₁ μ) | ||||||||||||||||||||
| (htr₁ : lts₁.Tr s₁ μ s₁') | ||||||||||||||||||||
| (htr₂ : lts₂.Tr s₂ μ s₂') : r s₁' s₂' := by | ||||||||||||||||||||
| have hr' : (flip r) s₂ s₁ := by grind [flip] | ||||||||||||||||||||
| apply IsSimulation.match_deterministic hb.flip_isSimulation hr' hdet htr₂ htr₁ | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /-- If the unique transition of a state is matched by a related state in the inverse of a | ||||||||||||||||||||
| bisimulation, then the derivatives are still in the bisimulation. -/ | ||||||||||||||||||||
| theorem IsBisimulation.match_deterministic_flip | ||||||||||||||||||||
| (hb : IsBisimulation lts₁ lts₂ r) | ||||||||||||||||||||
| (hr : (flip r) s₂ s₁) | ||||||||||||||||||||
| (hdet : lts₂.DeterministicStateLabel s₂ μ) | ||||||||||||||||||||
| (htr₁ : lts₂.Tr s₂ μ s₂') | ||||||||||||||||||||
| (htr₂ : lts₁.Tr s₁ μ s₁') : r s₁' s₂' := by | ||||||||||||||||||||
| have hr' : r s₁ s₂ := by grind [flip] | ||||||||||||||||||||
|
Comment on lines
+130
to
+134
Collaborator
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. (untested)
Suggested change
i ought to have been clearer in my other comment, by "flip" i just meant the symmetric version, no need to have the |
||||||||||||||||||||
| apply IsSimulation.match_deterministic hb.isSimulation hr' hdet htr₂ htr₁ | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /-- If a state is deterministic for `μ`, then any transition made by a related state in a | ||||||||||||||||||||
| bisimulation is matched by a unique transition (left variant). -/ | ||||||||||||||||||||
| theorem IsBisimulation.follow_fst_deterministic (hb : IsBisimulation lts₁ lts₂ r) (hr : r s₁ s₂) | ||||||||||||||||||||
| (hdet : lts₂.DeterministicStateLabel s₂ μ) (htr : lts₁.Tr s₁ μ s₁') : | ||||||||||||||||||||
| ∃! s₂', lts₂.Tr s₂ μ s₂' ∧ r s₁' s₂' := | ||||||||||||||||||||
| IsSimulation.follow_deterministic hb.isSimulation hr hdet htr | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /-- If a state is deterministic for `μ`, then any transition made by a related state in a | ||||||||||||||||||||
| bisimulation is matched by a unique transition (right variant). -/ | ||||||||||||||||||||
| theorem IsBisimulation.follow_snd_deterministic | ||||||||||||||||||||
| (hb : IsBisimulation lts₁ lts₂ r) | ||||||||||||||||||||
| (hr : r s₁ s₂) | ||||||||||||||||||||
| (hdet : lts₁.DeterministicStateLabel s₁ μ) | ||||||||||||||||||||
| (htr : lts₂.Tr s₂ μ s₂') : ∃! s₁', lts₁.Tr s₁ μ s₁' ∧ r s₁' s₂' := | ||||||||||||||||||||
| IsSimulation.follow_deterministic hb.flip_isSimulation hr hdet htr | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /-- A homogeneous bisimulation is a bisimulation where the underlying LTSs are the same. -/ | ||||||||||||||||||||
| abbrev IsHomBisimulation (lts : LTS State Label) := IsBisimulation lts lts | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
@@ -498,7 +541,7 @@ theorem IsSWBisimulation.iff_isSimulation [HasTau Label] : | |||||||||||||||||||
| IsSimulation lts₁ lts₂.saturate r ∧ IsSimulation lts₂ lts₁.saturate (flip r) := by | ||||||||||||||||||||
| refine ⟨fun h => ⟨h.isSimulation, h.isSimulation_flip⟩, ?_⟩ | ||||||||||||||||||||
| intro ⟨h, hflip⟩ s₁ s₂ hr μ | ||||||||||||||||||||
| exact ⟨h s₁ s₂ hr μ, hflip s₂ s₁ hr μ⟩ | ||||||||||||||||||||
| exact ⟨h hr μ, hflip hr μ⟩ | ||||||||||||||||||||
|
|
||||||||||||||||||||
| /-- We can now prove that any relation is a `WeakBisimulation` iff it is an `SWBisimulation`. | ||||||||||||||||||||
| This formalises lemma 4.2.10 in [Sangiorgi2011]. -/ | ||||||||||||||||||||
|
|
||||||||||||||||||||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.