File size: 5,731 Bytes
6851d40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// RUN: %run "%s" > "%t"
// RUN: %OutputCheck --file-to-check "%t" "%s"
// CHECK-L: Hello
// CHECK-NEXT-L: Error: 'name' was not found
// CHECK-NEXT-L: Hello
// CHECK-NEXT-L: Dafny
// CHECK-NEXT-L: Test dummy file

include "../src/dafny/Wrappers.dfy"

module {:options "--function-syntax:4"} Demo {
  import opened Dafny.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];
    }
  }

  method TestMyMap() {
    var m := new MyMap<string, string>();
    m.Put("message", "Hello");
    Greet(m);

    m.Put("name", "Dafny");
    Greet(m);
  }

  method Greet(m: MyMap<string, string>) {
    var o: Option<string> := m.Get("message");
    if o.Some? {
      print o.value, "\n";
    } else {
      print "oops\n";
    }

    var r: Result<string, string> := FindName(m);
    if r.Success? {
      print r.value, "\n";
    } else {
      print "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);
  }

  // ------ 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: Result<(), string>)
      modifies this
    {
      if path in files {
        files := files[path := contents];
        res := Success(()); // `()` is the only value of type `()`
      } else {
        // The "Result" datatype allows us to give precise error messages
        // instead of just "None"
        res := Failure("File not found, use 'Create' before");
      }
    }

    method CreateFile(path: string) returns (res: Result<(), string>)
      modifies this
    {
      if path in files {
        res := Failure("File already exists");
      } else {
        files := files[path := ""];
        res := Success(());
      }
    }

    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);
    var _ :- fs.CreateFile(toPath);
    var _ :- fs.WriteFile(toPath, contents);
    res := Success(());
  }

  method TestMyFilesystem() {
    var fs := new MyFilesystem();
    // Note: these verbose "outcome.Failure?" patterns will soon
    // not be needed any more, see https://github.com/dafny-lang/rfcs/pull/1
    var outcome: Result<(), string> := fs.CreateFile("test.txt");
    if outcome.Failure? {
      print outcome.error, "\n";
      return;
    }
    outcome := fs.WriteFile("test.txt", "Test dummy file");
    if outcome.Failure? {
      print outcome.error, "\n";
      return;
    }
    var fileResult: Result<string, string> := fs.ReadFile("test.txt");
    if outcome.Failure? {
      print fileResult.error, "\n";
      return;
    }
    if fileResult.Success? {
      print fileResult.value, "\n";
    }
  }

  // ------ 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 contents: string := *;

    // 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;
  }

  method Main() {
    TestMyMap();
    TestMyFilesystem();
  }

}