-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathWrappersExamples.dfy
277 lines (226 loc) · 8.03 KB
/
WrappersExamples.dfy
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import opened Std.Wrappers
// ------ Demo for Option ----------------------------
// We use Option when we don't need to pass around a reason for the failure,
// ie when just 'None' is sufficient:
class MyMap<K(==), V> {
var m: map<K, V>
constructor () {
m := map[];
}
function Get(k: K): Option<V>
reads this
{
if k in m then Some(m[k]) else None()
}
method Put(k: K, v: V)
modifies this
{
this.m := this.m[k := v];
}
}
@Test
method TestMyMap() {
var m := new MyMap<string, string>();
m.Put("message", "Hello");
var greeting := Greet(m);
expect greeting == "Hello\nError: 'name' was not found\n";
m.Put("name", "Dafny");
greeting := Greet(m);
expect greeting == "Hello\nDafny\n";
}
@Test
method TestGetGreetingSuccess() {
var m := new MyMap<string, string>();
m.Put("message", "Hello");
m.Put("name", "Dafny");
var greeting := GetGreeting(m);
expect greeting == Some("Hello Dafny");
}
@Test
method TestGetGreetingFailure() {
var m := new MyMap<string, string>();
var greeting := GetGreeting(m);
expect greeting == None;
}
method Greet(m: MyMap<string, string>) returns (greeting: string) {
var o: Option<string> := m.Get("message");
if o.Some? {
greeting := o.value + "\n";
} else {
greeting := "oops\n";
}
var r: Result<string, string> := FindName(m);
if r.Success? {
greeting := greeting + r.value + "\n";
} else {
greeting := greeting + "Error: " + r.error + "\n";
}
}
// Sometimes we want to go from Option to Result:
method FindName(m: MyMap<string, string>) returns (res: Result<string, string>) {
// Will return a default error message in case of None:
res := m.Get("name").ToResult("'name' not found");
// We can also match on the option to write a custom error:
match m.Get("name")
case Some(n) => res := Success(n);
case None => res := Failure("'name' was not found");
}
// Propagating failures using :- statements
method GetGreeting(m: MyMap<string, string>) returns (res: Option<string>) {
var message: string :- m.Get("message");
var nameResult := FindName(m);
var name :- nameResult.ToOption();
res := Some(message + " " + name);
}
@Test
method TestOptionUtilities() {
var stringNone: Option<string> := None;
expect Some("thing").GetOr("else") == "thing";
expect None.GetOr("else") == "else";
expect Some("body once told me").ToOutcome("the world is gonna roll me") == Pass;
expect stringNone.ToOutcome("the world is gonna roll me") == Fail("the world is gonna roll me");
var convertor := (x: Option<string>) =>
match x
case None => Pass
case Some(value) => Fail("Not expected: " + value);
expect Some("thing").Map(convertor) == Fail("Not expected: thing");
expect None.Map(convertor) == Pass;
}
// ------ Demo for Result ----------------------------
// We use Result when we want to give a reason for the failure:
class MyFilesystem {
var files: map<string, string>
constructor() {
files := map[];
}
// Result<()> is used to return a Result without any data
method WriteFile(path: string, contents: string) returns (res: OutcomeResult<string>)
modifies this
{
if path in files {
files := files[path := contents];
res := Pass'();
} else {
// The "Result" datatype allows us to give precise error messages
// instead of just "None"
res := Fail'("File not found, use 'Create' before");
}
}
method CreateFile(path: string) returns (res: OutcomeResult<string>)
modifies this
{
if path in files {
res := Fail'("File already exists");
} else {
files := files[path := ""];
res := Pass'();
}
}
method ReadFile(path: string) returns (res: Result<string, string>) {
if path in files {
res := Success(files[path]);
} else {
res := Failure("File not found");
}
}
}
// Propagating failures using :- statements
method CopyFile(fs: MyFilesystem, fromPath: string, toPath: string) returns (res: Result<(), string>)
modifies fs
{
var contents :- fs.ReadFile(fromPath);
:- fs.CreateFile(toPath);
:- fs.WriteFile(toPath, contents);
res := Success(());
}
@Test
method TestMyFilesystem() {
var fs := new MyFilesystem();
:- expect fs.CreateFile("test.txt");
:- expect fs.WriteFile("test.txt", "Test dummy file");
var fileResult :- expect fs.ReadFile("test.txt");
expect fileResult == "Test dummy file";
// Testing error propagation
var result := CopyFile(fs, "missing.txt", "newfile.txt");
expect result == Failure("File not found");
}
@Test
method TestResultUtilities() {
var stringSuccess: Result<string, string> := Success("I found my keys!");
var stringFailure: Result<string, string> := Failure("I can't find my keys!");
expect stringSuccess.GetOr("else") == "I found my keys!";
expect stringFailure.GetOr("else") == "else";
expect stringSuccess.ToOutcome() == Pass;
expect stringFailure.ToOutcome() == Fail("I can't find my keys!");
var toErrorOption := (x: Result<string, string>) =>
match x
case Success(_) => None
case Failure(error) => Some(error);
expect stringSuccess.Map(toErrorOption) == None;
expect stringFailure.Map(toErrorOption) == Some("I can't find my keys!");
var exaggerator := error => "ATTENTION: " + error;
expect stringSuccess.MapFailure(exaggerator) == stringSuccess;
expect stringFailure.MapFailure(exaggerator) == Failure("ATTENTION: I can't find my keys!");
}
// ------ Demo for Need ----------------------------
// We use Need when something has to be true but we can't prove it statically
// This is a very contrived example
method whatIsCharacterFive(fs: MyFilesystem, fromPath: string) returns (res: Result<char, string>)
modifies fs
{
// Get a string that we can't reason about statically
var contents :- fs.ReadFile(fromPath);
// Dynamically test whether the string is at least 5 characters long, and return a failure if not.
// If we pass this line, Dafny can now assume that the string is long enough.
:- Need<string>(|contents| >= 5, "File contents not long enough.");
// Now we can get the character
var c := contents[4];
return Success(c);
}
// For a method that returns an Outcome, use Outcome.Need
method whatIsCharacterFive'(fs: MyFilesystem, fromPath: string) returns (res: Outcome<string>)
modifies fs
{
// Get a string that we can't reason about statically
var result := fs.ReadFile(fromPath);
:- result.ToOutcome();
var contents: string := result.value;
// Dynamically test whether the string is at least 5 characters long, and return a failure if not.
// If we pass this line, Dafny can now assume that the string is long enough.
:- Outcome.Need(|contents| >= 5, "File contents not long enough.");
// Now we can get the character
var c := contents[4];
// and do other stuff
return Pass;
}
@Test
method TestNeed() {
var fs := new MyFilesystem();
:- expect fs.CreateFile("test.txt");
:- expect fs.WriteFile("test.txt", "12345678910");
var c :- expect whatIsCharacterFive(fs, "test.txt");
:- expect whatIsCharacterFive'(fs, "test.txt");
:- expect fs.WriteFile("test.txt", "");
var result := whatIsCharacterFive(fs, "test.txt");
expect result.Failure? && result.error == "File contents not long enough.";
var outcome := whatIsCharacterFive'(fs, "test.txt");
expect outcome.Fail? && outcome.error == "File contents not long enough.";
}
@Test
method TestOutcomeUtilities() {
var stringPass: Outcome<string> := Pass;
var stringFail: Outcome<string> := Fail("I'm too tired");
expect stringPass.ToOption(42) == Some(42);
expect stringFail.ToOption(42) == None;
expect stringPass.ToResult(42) == Success(42);
expect stringFail.ToResult(42) == Failure("I'm too tired");
var toErrorOption := (x: Outcome<string>) =>
match x
case Pass => None
case Fail(error) => Some(error);
expect stringPass.Map(toErrorOption) == None;
expect stringFail.Map(toErrorOption) == Some("I'm too tired");
var exaggerator := error => "ATTENTION: " + error;
expect stringPass.MapFailure(exaggerator, 42) == Success(42);
expect stringFail.MapFailure(exaggerator, 42) == Failure("ATTENTION: I'm too tired");
}