| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | include "../src/dafny/Wrappers.dfy" |
| |
|
| | module {:options "--function-syntax:4"} Demo { |
| | import opened Dafny.Wrappers |
| |
|
| | |
| | |
| | |
| |
|
| | 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"; |
| | } |
| | } |
| |
|
| | |
| | method FindName(m: MyMap<string, string>) returns (res: Result<string, string>) { |
| | |
| | res := m.Get("name").ToResult("'name' not found"); |
| | |
| | match m.Get("name") |
| | case Some(n) => res := Success(n); |
| | case None => res := Failure("'name' was not found"); |
| | } |
| |
|
| | |
| | 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); |
| | } |
| |
|
| | |
| | |
| |
|
| | class MyFilesystem { |
| | var files: map<string, string> |
| | constructor() { |
| | files := map[]; |
| | } |
| |
|
| | |
| | method WriteFile(path: string, contents: string) returns (res: Result<(), string>) |
| | modifies this |
| | { |
| | if path in files { |
| | files := files[path := contents]; |
| | res := Success(()); |
| | } else { |
| | |
| | |
| | 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"); |
| | } |
| | } |
| | } |
| |
|
| | |
| | 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(); |
| | |
| | |
| | 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"; |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| |
|
| | method whatIsCharacterFive(fs: MyFilesystem, fromPath: string) returns (res: Result<char, string>) |
| | modifies fs |
| | { |
| |
|
| | |
| | var contents :- fs.ReadFile(fromPath); |
| |
|
| | |
| | |
| | :- Need<string>(|contents| >= 5, "File contents not long enough."); |
| |
|
| | |
| | var c := contents[4]; |
| |
|
| | return Success(c); |
| | } |
| |
|
| | |
| | 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 := *; |
| |
|
| | |
| | |
| | :- Outcome.Need(|contents| >= 5, "File contents not long enough."); |
| |
|
| | |
| | var c := contents[4]; |
| | |
| |
|
| | return Pass; |
| | } |
| |
|
| | method Main() { |
| | TestMyMap(); |
| | TestMyFilesystem(); |
| | } |
| |
|
| | } |
| |
|