aboutsummaryrefslogtreecommitdiff
path: root/org/What I've Learned About Formal Methods In Half a Year/1670-problem.frg
diff options
context:
space:
mode:
authorJakob L. Kreuze2023-04-10 07:23:05 -0400
committerJakob L. Kreuze2023-08-04 19:49:43 -0400
commit7fbfa070492e37a6fb5659cb37c43a469de725d1 (patch)
tree884817b98be2c49f949fafedf9acaeaeb971e738 /org/What I've Learned About Formal Methods In Half a Year/1670-problem.frg
parent9bd53f58cfde8d73e9e9178b8c33325a1281c5d0 (diff)
Merge branch 'article/scheme-in-lean'
Diffstat (limited to 'org/What I've Learned About Formal Methods In Half a Year/1670-problem.frg')
-rw-r--r--org/What I've Learned About Formal Methods In Half a Year/1670-problem.frg227
1 files changed, 227 insertions, 0 deletions
diff --git a/org/What I've Learned About Formal Methods In Half a Year/1670-problem.frg b/org/What I've Learned About Formal Methods In Half a Year/1670-problem.frg
new file mode 100644
index 0000000..d82d709
--- /dev/null
+++ b/org/What I've Learned About Formal Methods In Half a Year/1670-problem.frg
@@ -0,0 +1,227 @@
+#lang forge
+
+option solver "/tmp/sat1/solver.py"
+
+option problem_type temporal
+option min_tracelength 2
+option max_tracelength 10
+
+sig File {
+ ctime: one Int,
+ var free: one Int
+}
+
+sig Snapshot extends File {
+ children: set File,
+ retired_list: set File
+}
+
+one sig CurrentRoot {
+ var live_children: set File,
+ var live_retired_list: set File
+}
+
+pred referenced_by_snapshot[f: File] {
+ some s: Snapshot {
+ s.free = 0
+ f in s.children
+ }
+}
+
+pred valid_state {
+ all f: File {
+ -- No referenced file should be free.
+ (f in CurrentRoot.live_children or (some s: Snapshot | f in s.children and s.free = 0)) => f.free = 0
+
+ -- No file in a retired list should referenced
+ f in CurrentRoot.live_retired_list => f not in CurrentRoot.live_children
+ all s: Snapshot {
+ f in s.retired_list and s.free = 0 => f not in s.children
+ }
+
+ -- Treat `free` as a boolean.
+ f.free = 0 or f.free = 1
+ }
+
+ -- Snapshots must be roots.
+ all s1 : Snapshot {
+ s1 not in CurrentRoot.live_children
+ s1 not in CurrentRoot.live_retired_list
+ all s2: Snapshot {
+ s1 not in s2.children
+ s1 not in s2.retired_list
+ }
+ }
+
+ -- Added this!
+ all f: File | (all s1: Snapshot | f in s1.retired_list => {
+ f not in CurrentRoot.live_children
+ all s2: Snapshot {
+ s2.ctime > s1.ctime => f not in s2.children
+ }
+ })
+
+ -- This was also necessary -- clock is monotone.
+ all disj f1, f2: File | f1.ctime != f2.ctime
+}
+
+--
+
+-- We start with an empty filesystem, where everything is free.
+pred init {
+ no CurrentRoot.live_children
+ no CurrentRoot.live_retired_list
+ all f: File {
+ f.free = 1 iff f != CurrentRoot
+ }
+}
+
+pred make_snapshot {
+ one s: Snapshot {
+ -- The snapshot captures the state of the filesystem at this point in time.
+ s.children = CurrentRoot.live_children
+ s.retired_list = CurrentRoot.live_retired_list
+
+ -- The snapshot is now marked as allocated.
+ s.free = 1
+ s.free' = 0
+
+ all f: File {
+ f != s => {
+ -- The free flag is unchanged for every other file.
+ f.free' = f.free
+
+ -- All other creations happened in the past, relative to this one.
+ f.free = 0 => s.ctime > f.ctime
+ }
+ }
+ }
+
+ -- The retired list is emptied for the current filesystem.
+ no live_retired_list'
+
+ -- Nothing else changes.
+ live_children' = live_children
+}
+
+pred referenced_by_no_other_snapshot[f: File] {
+ all s: Snapshot {
+ s.free = 0 => f not in s.children
+ }
+}
+
+pred delete_snapshot {
+ one s: Snapshot {
+ -- The snapshot is now marked as free
+ s.free = 0
+ s.free' = 1
+
+ all f: File {
+ (f in s.retired_list and referenced_by_no_other_snapshot[f]) => {
+ f.free' = 1
+ } else {
+ -- The free flag is unchanged for every other file.
+ f != s => f.free' = f.free
+ }
+ }
+ }
+
+ -- Nothing else changes.
+ live_retired_list' = live_retired_list
+ live_children' = live_children
+}
+
+pred make_file {
+ one f: File {
+ -- `f` is added to the set of live children.
+ CurrentRoot.live_children' = CurrentRoot.live_children + f
+ -- `f` is marked as allocated.
+ f.free' = 0
+
+ -- n.b. it could already be allocated, if a previous snapshot refers to it.
+
+ all f2: File {
+ f2 != f => {
+ -- Free flag for other files remains unchanged.
+ f2.free' = f2.free
+
+ -- All other creations happened in the past, relative to this one.
+ f2.free = 0 => f.ctime > f2.ctime
+ }
+ }
+ }
+
+ -- Nothing else changes.
+ live_retired_list' = live_retired_list
+ live_children' = live_children
+}
+
+pred delete_file {
+ one f: File {
+ -- It only makes sense to delete a file if it's in the filesystem
+ f in CurrentRoot.live_children
+
+ -- Remove it from the filesystem, and reclaim it if applicable.
+ -- Otherwise, put it in the retired list.
+ CurrentRoot.live_children' = CurrentRoot.live_children - f
+
+ referenced_by_snapshot[f] => {
+ -- If another snapshot refers to this one, we don't free it
+ -- but add it to the retired list.
+ f.free' = 0
+ CurrentRoot.live_retired_list' = CurrentRoot.live_retired_list + f
+ } else {
+ -- Otherwise we can free it and the retired list is unchanged.
+ f.free' = 1
+ live_retired_list' = live_retired_list
+ }
+
+ all f2: File {
+ -- Free flag for other files remains unchanged.
+ f2 != f => f2.free' = f2.free
+ }
+ }
+}
+
+pred traces {
+ init
+ always (make_snapshot or delete_snapshot or make_file or delete_file)
+}
+
+
+test expect {
+ valid_state_vacuity1: { valid_state } for 5 File is sat
+ valid_state_vacuity2: { valid_state } for exactly 5 File is sat
+
+ init_vacuity1: { init } for 5 File is sat
+ init_vacuity2: { init } for exactly 5 File is sat
+
+ make_snapshot_vacuity1: { valid_state and make_snapshot } for 5 File is sat
+ make_snapshot_vacuity2: { valid_state and make_snapshot } for exactly 5 File is sat
+ make_snapshot_good: {
+ (valid_state and make_snapshot) => next_state valid_state
+ } for 5 File is theorem
+
+ delete_snapshot_vacuity1: { valid_state and delete_snapshot } for 5 File is sat
+ delete_snapshot_vacuity2: { valid_state and delete_snapshot } for exactly 5 File is sat
+ delete_snapshot_good: {
+ (valid_state and delete_snapshot) => next_state valid_state
+ } for 5 File is theorem
+
+ make_file_vacuity1: { valid_state and make_file } for 5 File is sat
+ make_file_vacuity2: { valid_state and make_file } for exactly 5 File is sat
+ make_file_good: {
+ (valid_state and make_file) => next_state valid_state
+ } for 5 File is theorem
+
+ delete_file_vacuity1: { valid_state and delete_file } for 5 File is sat
+ delete_file_vacuity2: { valid_state and delete_file } for exactly 5 File is sat
+ delete_file_good: {
+ (valid_state and delete_file) => next_state valid_state
+ } for 5 File is theorem
+
+ traces_vacuity: { traces } for 5 File is sat
+ traces_vacuity: { traces } for exactly 5 File is sat
+
+ problem_2b: { traces => always { all f: File | referenced_by_snapshot[f] => (some s: Snapshot | s.free = 0 and s.ctime > f.ctime) } } for exactly 5 File is theorem
+}

© 2015 - 2026 Jakob L. Kreuze