// First, read the following file to learn our definition of edit distance. include "pset11_definitions.dfy" // Our solution required a LOT of lemma work. // If you want to attempt the whole proof yourself, comment out the following line... include "pset11_helpers.dfy" // ... and take a look at pset11_hints.dfy if you get stuck. // If you will use our lemmas, read their specifications in pset11_helpers.dfy now :) module Pset11 { import opened Defs import opened Helpers // Modify this method definition only by adding lines of annotation that don't // change the execution behavior, such that Dafny accepts that the given // postcondition holds. Don't add any new 'requires' clauses to this method. method EditDistance(a: string, b: string) returns (d: nat) ensures IsEditDistance(a, b, d) { var table := new nat[|a|+1, |b|+1]; var i, j := 0, 0; while i <= |a| { j := 0; while j <= |b| { if i == 0 { table[i, j] := j; } else if j == 0 { table[i, j] := i; } else if a[i-1] == b[j-1] { table[i, j] := table[i-1, j-1]; } else { table[i, j] := 1 + Min(table[i-1, j], Min(table[i, j-1], table[i-1, j-1])); } j := j + 1; } i := i + 1; } d := table[|a|, |b|]; } }