use vstd::prelude::*; fn main() {} verus!{ fn f1_extract_rear_chars(s: &Vec>) -> (result: Vec) requires forall|i: int| 0 <= i < s.len() ==> #[trigger] s[i].len() > 0, { let mut rear_chars: Vec = Vec::with_capacity(s.len()); let mut index = 0; while index < s.len() { let seq = &s[index]; rear_chars.push(seq[seq.len() - 1]); index += 1; } rear_chars } spec fn f2_is_upper_case(c: u8) -> bool { c >= 65 && c <= 90 } spec fn f2_shift32_spec(c: u8) -> u8 { (c + 32) as u8 } spec fn f2_is_lower_case(c: u8) -> bool { c >= 97 && c <= 122 } spec fn f2_shift_minus_32_spec(c: u8) -> u8 { (c - 32) as u8 } spec fn f2_to_toggle_case_spec(s: u8) -> u8 { if f2_is_lower_case(s) { f2_shift_minus_32_spec(s) } else if f2_is_upper_case(s) { f2_shift32_spec(s) } else { s } } fn f2_to_toggle_case(str1: &[u8]) -> (toggle_case: Vec) { let mut toggle_case = Vec::with_capacity(str1.len()); let mut index = 0; while index < str1.len() { if (str1[index] >= 97 && str1[index] <= 122) { toggle_case.push((str1[index] - 32) as u8); } else if (str1[index] >= 65 && str1[index] <= 90) { toggle_case.push((str1[index] + 32) as u8); } else { toggle_case.push(str1[index]); } index += 1; } toggle_case } spec fn f3_is_lower_case(c: u8) -> bool { c >= 97 && c <= 122 } spec fn f3_shift_minus_32_spec(c: u8) -> u8 { (c - 32) as u8 } fn f3_to_uppercase(str1: &[u8]) -> (result: Vec) { let mut upper_case: Vec = Vec::with_capacity(str1.len()); let mut index = 0; while index < str1.len() { if (str1[index] >= 97 && str1[index] <= 122) { upper_case.push((str1[index] - 32) as u8); } else { upper_case.push(str1[index]); } index += 1; } upper_case } spec fn f4_is_upper_case(c: u8) -> bool { c >= 65 && c <= 90 } spec fn f4_shift32_spec(c: u8) -> u8 { (c + 32) as u8 } spec fn f4_is_lower_case(c: u8) -> bool { c >= 97 && c <= 122 } spec fn f4_shift_minus_32_spec(c: u8) -> u8 { (c - 32) as u8 } spec fn f4_to_toggle_case_spec(s: u8) -> u8 { if f4_is_lower_case(s) { f4_shift_minus_32_spec(s) } else if f4_is_upper_case(s) { f4_shift32_spec(s) } else { s } } fn f4_to_toggle_case(str1: &[u8]) -> (toggle_case: Vec) { let mut toggle_case = Vec::with_capacity(str1.len()); let mut index = 0; while index < str1.len() { if (str1[index] >= 97 && str1[index] <= 122) { toggle_case.push((str1[index] - 32) as u8); } else if (str1[index] >= 65 && str1[index] <= 90) { toggle_case.push((str1[index] + 32) as u8); } else { toggle_case.push(str1[index]); } index += 1; } toggle_case } fn f5_replace_chars(str1: &[u8], old_char: u8, new_char: u8) -> (result: Vec) { let mut result_str = Vec::with_capacity(str1.len()); let mut index = 0; while index < str1.len() { if str1[index] == old_char { result_str.push(new_char); } else { result_str.push(str1[index]); } index += 1; } result_str } // ASCII --> space=32, comma=44 , dot=46 , colon=58 spec fn f6_is_space_comma_dot_spec(c: u8) -> bool { (c == 32) || (c == 44) || (c == 46) } fn f6_replace_with_colon(str1: &[u8]) -> (result: Vec) { let mut result: Vec = Vec::with_capacity(str1.len()); let mut index = 0; while index < str1.len() { if ((str1[index] == 32) || (str1[index] == 44) || (str1[index] == 46)) { result.push(58); //ASCII -> colon=58 } else { result.push(str1[index]); } index += 1; } result } fn f7_replace_chars(str1: &[u8], old_char: u8, new_char: u8) -> (result: Vec) { let mut result_str = Vec::with_capacity(str1.len()); let mut index = 0; while index < str1.len() { if str1[index] == old_char { result_str.push(new_char); } else { result_str.push(str1[index]); } index += 1; } result_str } // ASCII --> space=32, comma=44 , dot=46 , colon=58 spec fn f8_is_space_comma_dot_spec(c: u8) -> bool { (c == 32) || (c == 44) || (c == 46) } fn f8_replace_with_colon(str1: &[u8]) -> (result: Vec) { let mut result: Vec = Vec::with_capacity(str1.len()); let mut index = 0; while index < str1.len() { if ((str1[index] == 32) || (str1[index] == 44) || (str1[index] == 46)) { result.push(58); //ASCII -> colon=58 } else { result.push(str1[index]); } index += 1; } result } fn f9_replace_blanks_with_chars(str1: &[u8], ch: u8) -> (result: Vec) { let mut out_str: Vec = Vec::with_capacity(str1.len()); let mut index = 0; while index < str1.len() { if (str1[index] == 32) { // 32 is ASCII Space as u8 out_str.push(ch); } else { out_str.push(str1[index]); } index += 1; } out_str } spec fn f10_is_lower_case(c: u8) -> bool { c >= 97 && c <= 122 } spec fn f10_is_upper_case(c: u8) -> bool { c >= 65 && c <= 90 } spec fn f10_count_uppercase_recursively(seq: Seq) -> int decreases seq.len(), { if seq.len() == 0 { 0 } else { f10_count_uppercase_recursively(seq.drop_last()) + if f10_is_upper_case(seq.last()) { 1 as int } else { 0 as int } } } fn f10_count_uppercase(text: &[u8]) -> (count: u64) { let mut index = 0; let mut count = 0; while index < text.len() { if (text[index] >= 65 && text[index] <= 90) { count += 1; } index += 1; } count } spec fn step1(__hp0: Seq>, outp: Seq) -> bool { (__hp0.len() == outp.len()) && (forall|i: int| 0 <= i < __hp0.len() ==> outp[i] == #[trigger] __hp0[i][__hp0[i].len() - 1]) } spec fn step2(inp: Seq, outp: Seq) -> bool { (inp.len() == outp.len()) && (forall|i: int| 0 <= i < inp.len() ==> outp[i] == f2_to_toggle_case_spec(#[trigger] inp[i])) } spec fn step3(inp: Seq, outp: Seq) -> bool { (inp.len() == outp.len()) && (forall|i: int| 0 <= i < inp.len() ==> (outp[i] == (if f3_is_lower_case(#[trigger] inp[i]) { f3_shift_minus_32_spec(inp[i]) } else { inp[i] }))) } spec fn step4(inp: Seq, outp: Seq) -> bool { (inp.len() == outp.len()) && (forall|i: int| 0 <= i < inp.len() ==> outp[i] == f4_to_toggle_case_spec(#[trigger] inp[i])) } spec fn step5(inp: Seq, p5_old_char: u8, p5_new_char: u8, outp: Seq) -> bool { (inp.len() == outp.len()) && (forall|i: int| 0 <= i < inp.len() ==> outp[i] == (if inp[i] == p5_old_char { p5_new_char } else { inp[i] })) } spec fn step6(inp: Seq, outp: Seq) -> bool { (inp.len() == outp.len()) && (forall|k: int| 0 <= k < outp.len() ==> #[trigger] outp[k] == (if f6_is_space_comma_dot_spec(inp[k]) { 58 //ASCII -> colon=58 } else { inp[k] })) } spec fn step7(inp: Seq, p7_old_char: u8, p7_new_char: u8, outp: Seq) -> bool { (inp.len() == outp.len()) && (forall|i: int| 0 <= i < inp.len() ==> outp[i] == (if inp[i] == p7_old_char { p7_new_char } else { inp[i] })) } spec fn step8(inp: Seq, outp: Seq) -> bool { (inp.len() == outp.len()) && (forall|k: int| 0 <= k < outp.len() ==> #[trigger] outp[k] == (if f8_is_space_comma_dot_spec(inp[k]) { 58 //ASCII -> colon=58 } else { inp[k] })) } spec fn step9(inp: Seq, p9_ch: u8, outp: Seq) -> bool { (inp.len() == outp.len()) && (forall|i: int| 0 <= i < inp.len() ==> outp[i] == (if inp[i] == 32 { p9_ch } else { inp[i] })) } spec fn step10(inp: Seq, outp: u64) -> bool { (0 <= outp <= inp.len()) && (f10_count_uppercase_recursively(inp) == outp) } fn chain_driver(s: &Vec>, p5_old_char: u8, p5_new_char: u8, p7_old_char: u8, p7_new_char: u8, p9_ch: u8) -> (r_final: u64) requires forall|i: int| 0 <= i < s.len() ==> #[trigger] s[i].len() > 0, ensures exists|v1: Seq, v2: Seq, v3: Seq, v4: Seq, v5: Seq, v6: Seq, v7: Seq, v8: Seq, v9: Seq| #[trigger] step1(s@, v1) && #[trigger] step2(v1, v2) && #[trigger] step3(v2, v3) && #[trigger] step4(v3, v4) && #[trigger] step5(v4, p5_old_char, p5_new_char, v5) && #[trigger] step6(v5, v6) && #[trigger] step7(v6, p7_old_char, p7_new_char, v7) && #[trigger] step8(v7, v8) && #[trigger] step9(v8, p9_ch, v9) && step10(v9, r_final), { let x1 = f1_extract_rear_chars(s); let x2 = f2_to_toggle_case(x1.as_slice()); let x3 = f3_to_uppercase(x2.as_slice()); let x4 = f4_to_toggle_case(x3.as_slice()); let x5 = f5_replace_chars(x4.as_slice(), p5_old_char, p5_new_char); let x6 = f6_replace_with_colon(x5.as_slice()); let x7 = f7_replace_chars(x6.as_slice(), p7_old_char, p7_new_char); let x8 = f8_replace_with_colon(x7.as_slice()); let x9 = f9_replace_blanks_with_chars(x8.as_slice(), p9_ch); let x10 = f10_count_uppercase(x9.as_slice()); proof { assert(step1(s@, x1@)); assert(step2(x1@, x2@)); assert(step3(x2@, x3@)); assert(step4(x3@, x4@)); assert(step5(x4@, p5_old_char, p5_new_char, x5@)); assert(step6(x5@, x6@)); assert(step7(x6@, p7_old_char, p7_new_char, x7@)); assert(step8(x7@, x8@)); assert(step9(x8@, p9_ch, x9@)); assert(step10(x9@, x10)); } x10 } } // verus!