// In this file, you can find hints to follow if you want to prove on your own // the lemmas we provide in pset11_helpers.dfy. // Remember that Dafny can often prove equalities involving sequences and their // slices, but Dafny doesn't automatically guess all useful such equations. // Thus, asserting "obvious" sequence-related equalities can get a proof // unstuck. (In this assignment, both strings and edit sequences are // sequences. Recall that a string is really a seq.) // We factored the main work of our proof into one lemma for each of the four // cases of the inner loop of EditDistance. Here are the statements we used. lemma EditDistanceFromEmpty(s: string) ensures IsEditDistance("", s, |s|) lemma EditDistanceToEmpty(s: string) ensures IsEditDistance(s, "", |s|) lemma EditDistanceLastMatch(a: string, b: string, c: char, d: nat) requires IsEditDistance(a, b, d) ensures IsEditDistance(a + [c], b + [c], d) lemma EditDistanceLastNoMatch(a: string, b: string, ca: char, cb: char, d00: nat, d01: nat, d10: nat) requires ca != cb requires IsEditDistance(a, b, d00) requires IsEditDistance(a, b + [cb], d01) requires IsEditDistance(a + [ca], b, d10) ensures IsEditDistance(a + [ca], b + [cb], 1 + Min(d01, Min(d10, d00))) // Each of these lemma proofs has two main parts: // 1. Exhibit an edit sequence of the right length, to transform the first // string to the second. // 2. Prove that *any* applicable edit sequence has length no lower than the // claimed bound. // Take the example of EditDistanceFromEmpty. There is actually exactly one // edit sequence that can transform the empty string to another specific string. // Can you write a function to compute it and then prove uniqueness? // Ditto for EditDistanceToEmpty, just with another function. // Our proof involves four big lemma proofs with lots of nested case analysis. // Here are their statements: // (a) Edit sequences can be concatenated to produce sequences that are valid // on the concatenated "before" and "after" strings. lemma ApplyEditsConcatFwd(es1: seq, es2: seq, a1: string, a2: string, b1: string, b2: string) requires ApplyEdits(es1, a1) == Some(b1) requires ApplyEdits(es2, a2) == Some(b2) ensures ApplyEdits(es1 + es2, a1 + a2) == Some(b1 + b2) // (b) An edit sequence for two nonempty strings has just four cases for how it // handles their final characters. Just invoking this lemma in the right places // of our main lemmas does miraculously well for finishing those subproofs! // Note that a condition on the new sequence es' says it costs one unit fewer // than es, *except* perhaps when the final characters of the strings match. lemma FinalCases(es: seq, a: string, c: char, b: string, c': char) returns (es': seq) requires ApplyEdits(es, a + [c]) == Some(b + [c']) ensures (c == c' && ApplyEdits(es', a) == Some(b) && Cost(es') == Cost(es)) || (ApplyEdits(es', a) == Some(b) && Cost(es') == Cost(es)-1) || (ApplyEdits(es', a + [c]) == Some(b) && Cost(es') == Cost(es)-1) || (ApplyEdits(es', a) == Some(b + [c']) && Cost(es') == Cost(es)-1) // (c) An edit sequence that starts from a nonempty string can be reduced to // another sequence that starts from that string with its last character peeled // off. We get a fairly tight bound on the cost of the new sequence. lemma PeelLeft(es: seq, a: string, c: char, b: string) returns (es': seq) requires ApplyEdits(es, a + [c]) == Some(b) ensures ApplyEdits(es', a) == Some(b) ensures Cost(es)-1 <= Cost(es') <= Cost(es)+1 // (d) The symmetrical property, for peeling off the last character of the // generated string. lemma PeelRight(es: seq, a: string, c: char, b: string) returns (es': seq) requires ApplyEdits(es, a) == Some(b + [c]) ensures ApplyEdits(es', a) == Some(b) ensures Cost(es)-1 <= Cost(es') <= Cost(es)+1 // The big four lemmas above rely on the following definitions that we don't // bother to explain. ghost function AddAll(s: string): (es: seq) ensures ApplyEdits(es, "") == Some(s) ensures Cost(es) == |s| ghost function RepeatDelete(n: nat): seq ensures Cost(RepeatDelete(n)) == n lemma ApplyRepeatDelete(s: string) ensures ApplyEdits(RepeatDelete(|s|), s) == Some("") lemma ApplyMustBeRepeatDelete(es: seq, s: string) requires ApplyEdits(es, s) == Some("") ensures es == RepeatDelete(|s|) lemma ApplyMustBeAddAll(es: seq, s: string) requires ApplyEdits(es, "") == Some(s) ensures es == AddAll(s) lemma CostConcat(es1: seq, es2: seq) ensures Cost(es1 + es2) == Cost(es1) + Cost(es2)