Skip to content

Commit 2cadde8

Browse files
authored
feat: Tighten specifications on Strings standard library (#4868)
### Description As follow-up from the initial review, this adds a bodiless `CharsConsistent` lemma to the abstract `Std.Strings.ParametricConversion` module that ensures `chars` and `charsToDigits` are consistent, which is necessary but not sufficient to prove that `forall n :: ToInt(OfInt(n)) == n`. Also makes some documentation and naming improvements, adding an `IsDigitChar` predicate for better readability than `c in charsToDigit` (which is slightly different from `c in chars`) and merges the two confusingly-named `ToNumberStr` and `OfNumberStr` predicate into one `IsNumberStr` (with the definition of the former, weaker predicate). ### How has this been tested? Existing tests + verification.
1 parent f735fd9 commit 2cadde8

11 files changed

+40
-20
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
104 Bytes
Binary file not shown.

Source/DafnyStandardLibraries/src/Std/JSON/ByteStrConversion.dfy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ module {:disableNonlinearArithmetic} Std.JSON.ByteStrConversion refines Strings.
2222
'4' as byte := 4, '5' as byte := 5, '6' as byte := 6, '7' as byte := 7,
2323
'8' as byte := 8, '9' as byte := 9
2424
]
25+
26+
lemma CharsConsistent()
27+
ensures forall c <- chars :: c in charToDigit && chars[charToDigit[c]] == c
28+
{}
2529
}

Source/DafnyStandardLibraries/src/Std/JSON/Deserializer.dfy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ module Std.JSON.Deserializer {
5050
'a' as uint16 := 0xA, 'b' as uint16 := 0xB, 'c' as uint16 := 0xC, 'd' as uint16 := 0xD, 'e' as uint16 := 0xE, 'f' as uint16 := 0xF,
5151
'A' as uint16 := 0xA, 'B' as uint16 := 0xB, 'C' as uint16 := 0xC, 'D' as uint16 := 0xD, 'E' as uint16 := 0xE, 'F' as uint16 := 0xF
5252
]
53+
54+
// The size of the map makes this impractical to verify easily.
55+
lemma {:axiom} CharsConsistent()
56+
ensures forall c <- chars :: c in charToDigit && chars[charToDigit[c]] == c
5357
}
5458

5559
const HEX_TABLE_16 := Uint16StrConversion.charToDigit

Source/DafnyStandardLibraries/src/Std/JSON/Spec.dfy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ module Std.JSON.Spec {
7474
if i == 0 {
7575
} else {
7676
var isHexDigit := c => c in Strings.HexConversion.HEX_DIGITS;
77-
assert Strings.HexConversion.OfNumberStr(s, '-');
77+
assert Strings.HexConversion.IsNumberStr(s, '-');
7878
assert isHexDigit(s[i]);
7979
}
8080
}

0 commit comments

Comments
 (0)