aboutsummaryrefslogtreecommitdiff
path: root/org/What I've Learned About Formal Methods In Half a Year/1670-problem.frg
blob: d82d709750415c0925290c85f37f50cfe56a05ff (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
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