-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sparse bytes #516
Merged
Merged
Sparse bytes #516
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
89a9221
implement `SparseBytes`
bbyalcinkaya 76f42d0
fix substrSparseBytes
bbyalcinkaya 46d2d8d
Merge branch 'master' into sparse-bytes
bbyalcinkaya 3a37aa6
fix functional spec step sort
bbyalcinkaya 6b9da0e
add SparseBytes functional tests
bbyalcinkaya 922bb2d
Merge branch 'master' into sparse-bytes
bbyalcinkaya 2305f40
add fromBytes and simple tests
bbyalcinkaya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
|
||
module SPARSE-BYTES | ||
import BOOL | ||
import BYTES | ||
import INT | ||
|
||
syntax SBItem ::= #empty(Int) [symbol, klabel(SBItem:empty)] | ||
| #bytes(Bytes) [symbol, klabel(SBItem:bytes)] | ||
|
||
syntax SparseBytes [hook(LIST.List)] | ||
syntax SparseBytes ::= SparseBytes SparseBytes | ||
[ left, function, total, hook(LIST.concat), | ||
klabel(_SparseBytes_), symbol, smtlib(smt_seq_concat), | ||
assoc, unit(.SparseBytes), element(SparseBytesItem), | ||
format(%1%n%2) | ||
] | ||
syntax SparseBytes ::= ".SparseBytes" | ||
[ function, total, hook(LIST.unit), klabel(.SparseBytes), | ||
symbol, smtlib(smt_seq_nil), latex(\dotCt{SparseBytes}) | ||
] | ||
syntax SparseBytes ::= SBChunk(SBItem) | ||
[ function, total, hook(LIST.element), klabel(SparseBytesItem), | ||
symbol, smtlib(smt_seq_elem) | ||
] | ||
|
||
syntax Bytes ::= unwrap(SparseBytes) | ||
[function, total, symbol, klabel(SparseBytes:unwrap)] | ||
// ----------------------------------------------------------------- | ||
rule unwrap(SBChunk(SBI) REST) => unwrap(SBI) +Bytes unwrap(REST) | ||
rule unwrap(.SparseBytes) => .Bytes | ||
|
||
syntax SparseBytes ::= fromBytes(Bytes) | ||
[function, total, symbol, klabel(SparseBytes:fromBytes)] | ||
// --------------------------------------------------------- | ||
rule fromBytes(Bs) => SBChunk(#bytes(Bs)) | ||
|
||
syntax Bytes ::= unwrap(SBItem) | ||
[function, total, symbol, klabel(SBItem:unwrap)] | ||
// ----------------------------------------------- | ||
rule unwrap(#bytes(Bs)) => Bs | ||
rule unwrap(#empty(N)) => zeros(N) | ||
|
||
syntax Bytes ::= zeros(Int) [function, total, symbol, klabel(zeros)] | ||
// ------------------------------------------------------------------- | ||
rule zeros(N) => padLeftBytes(.Bytes, size(#empty(N)), 0) | ||
|
||
syntax Int ::= size(SparseBytes) | ||
[function, total, klabel(SparseBytes:size), symbol] | ||
// --------------------------------------------------- | ||
rule size(SBChunk(SBI) SBS) => size(SBI) +Int size(SBS) | ||
rule size(.SparseBytes) => 0 | ||
|
||
syntax Int ::= size(SBItem) | ||
[function, total, symbol, klabel(SBItem:size)] | ||
// ----------------------------------------------- | ||
rule size(#bytes(Bs)) => lengthBytes(Bs) | ||
rule size(#empty(N)) => maxInt(N,0) | ||
|
||
|
||
syntax SparseBytes ::= substrSparseBytes(SparseBytes, from: Int, to: Int) | ||
[function, total, klabel(SparseBytes:substr), symbol] | ||
// ------------------------------------------------------------------------ | ||
rule substrSparseBytes(_, S, E) => .SparseBytes | ||
requires notBool( 0 <=Int S andBool S <=Int E ) | ||
|
||
rule substrSparseBytes(.SparseBytes, S, E) => .SparseBytes | ||
requires 0 <=Int S andBool S <=Int E | ||
|
||
rule substrSparseBytes(SBChunk(SBI) REST, S, E) => substrSparseBytes(REST, S -Int size(SBI), E -Int size(SBI)) | ||
requires 0 <=Int S andBool S <=Int E | ||
andBool size(SBI) <=Int S | ||
|
||
rule substrSparseBytes(SBChunk(SBI) REST, S, E) | ||
=> #let SUB = substrSBItem(SBI, S, E) | ||
#in SUB substrSparseBytes(REST, 0, E -Int size(SBI)) | ||
requires 0 <=Int S andBool S <=Int E | ||
andBool size(SBI) >Int S | ||
|
||
syntax SparseBytes ::= substrSBItem(SBItem, from: Int, to: Int) | ||
[function, total, klabel(SBItem:substr), symbol] | ||
// ------------------------------------------------------------- | ||
rule substrSBItem(_SBI, S, E) => .SparseBytes | ||
requires S <Int 0 orBool E <Int S | ||
|
||
rule substrSBItem(#empty(N), S, E) => .SparseBytes | ||
requires 0 <=Int S andBool S <=Int E | ||
andBool N <=Int S | ||
|
||
rule substrSBItem(#empty(N), S, E) => SBChunk( #empty( minInt(E, N) -Int S) ) | ||
requires 0 <=Int S andBool S <=Int E | ||
andBool S <Int N | ||
|
||
rule substrSBItem(#bytes(Bs), S, E) => .SparseBytes | ||
requires 0 <=Int S andBool S <=Int E | ||
andBool lengthBytes(Bs) <=Int S | ||
|
||
rule substrSBItem(#bytes(Bs), S, E) | ||
=> SBChunk(#bytes( substrBytes(Bs, S, minInt(E, lengthBytes(Bs))) )) | ||
requires 0 <=Int S andBool S <=Int E | ||
andBool S <Int lengthBytes(Bs) | ||
|
||
|
||
syntax SparseBytes ::= replaceAt(SparseBytes, Int, Bytes) | ||
[function, total, symbol, klabel(SparseBytes:replaceAt)] | ||
// -------------------------------------------------------- | ||
// invalid argument | ||
rule replaceAt(_, S, _) => .SparseBytes | ||
requires S <Int 0 | ||
|
||
// append | ||
rule replaceAt(.SparseBytes, S, Bs) => SBChunk(#bytes(Bs)) | ||
requires 0 ==Int S | ||
|
||
// append padded | ||
rule replaceAt(.SparseBytes, S, Bs) => SBChunk(#empty(S)) SBChunk(#bytes(Bs)) | ||
requires 0 <Int S | ||
|
||
// skip | ||
rule replaceAt(SBChunk(SBI) REST, S, Bs) | ||
=> SBChunk(SBI) replaceAt(REST, S -Int size(SBI), Bs) | ||
requires size(SBI) <=Int S | ||
|
||
rule replaceAt(SBChunk(#empty(N)) REST, S, Bs) => replaceAtZ(N, REST, S, Bs) | ||
requires S <Int N | ||
rule replaceAt(SBChunk(#bytes(B)) REST, S, Bs) => replaceAtB(B, REST, S, Bs) | ||
requires S <Int lengthBytes(B) | ||
|
||
syntax SparseBytes ::= replaceAtZ(Int, SparseBytes, Int, Bytes) | ||
[function, total, symbol, klabel(SparseBytes:replaceAtZ)] | ||
// --------------------------------------------------------------- | ||
////////////// S < 0 | ||
rule replaceAtZ(_, _, S, _) => .SparseBytes | ||
requires S <Int 0 | ||
|
||
////////////// S > 0 | ||
// split zeros: 0 < S < N | ||
rule replaceAtZ(N, REST, S, Bs) | ||
=> SBChunk(#empty(S)) replaceAtZ(N -Int S, REST, 0, Bs) | ||
requires 0 <Int S | ||
andBool S <Int N | ||
|
||
// skip zeros: 0 <= N <= S | ||
rule replaceAtZ(N, REST, S, Bs) | ||
=> SBChunk(#empty(N)) replaceAt(REST, S -Int N, Bs) | ||
requires 0 <Int S | ||
andBool 0 <=Int N | ||
andBool N <=Int S | ||
|
||
////////////// S == 0 | ||
///////// len(Bs) < N | ||
rule replaceAtZ(N, REST, S, Bs) | ||
=> SBChunk(#bytes(Bs)) SBChunk(#empty(N -Int lengthBytes(Bs))) REST | ||
requires 0 ==Int S | ||
andBool lengthBytes(Bs) <Int N | ||
|
||
///////// len(Bs) = N | ||
rule replaceAtZ(N, REST, S, Bs) | ||
=> SBChunk(#bytes(Bs)) REST | ||
requires 0 ==Int S | ||
andBool lengthBytes(Bs) ==Int N | ||
|
||
///////// len(Bs) > N | ||
rule replaceAtZ(N, REST, S, Bs) | ||
=> replaceAt(SBChunk(#bytes(zeros(N))) REST, S, Bs) | ||
requires 0 ==Int S | ||
andBool lengthBytes(Bs) >Int N | ||
|
||
|
||
syntax SparseBytes ::= replaceAtB(Bytes, SparseBytes, Int, Bytes) | ||
[function, total, symbol, klabel(SparseBytes:replaceAtB)] | ||
// --------------------------------------------------------------- | ||
////////// S + len(Bs) <= len(MB) | ||
rule replaceAtB(MB, REST, S, Bs) | ||
=> SBChunk(#bytes( replaceAtBytes(MB, S, Bs) )) REST | ||
requires 0 <=Int S | ||
andBool S +Int lengthBytes(Bs) <=Int lengthBytes(MB) | ||
|
||
////////// S + len(Bs) > len(MB) | ||
rule replaceAtB(MB, REST, S, Bs) | ||
=> replaceAt( | ||
joinUntil(MB, REST, S +Int lengthBytes(Bs)), | ||
S, | ||
Bs | ||
) | ||
requires 0 <=Int S | ||
andBool lengthBytes(MB) <Int S +Int lengthBytes(Bs) | ||
|
||
|
||
// join list items until Bs is at least I bytes | ||
syntax SparseBytes ::= joinUntil(Bytes, SparseBytes, Int) | ||
[function, total] | ||
// -------------------------------------------------------- | ||
rule joinUntil(Bs, REST, I) | ||
=> SBChunk(#bytes(Bs)) REST | ||
requires I <=Int lengthBytes(Bs) | ||
|
||
rule joinUntil(Bs, SBChunk(SBI) REST, I) | ||
=> joinUntil(Bs +Bytes unwrap(SBI), REST, I) | ||
requires I >Int lengthBytes(Bs) | ||
|
||
rule joinUntil(Bs, .SparseBytes, I) | ||
=> SBChunk(#bytes( padRightBytes(Bs, I, 0) )) | ||
requires I >Int lengthBytes(Bs) | ||
|
||
endmodule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be worth adding tests for the other cases?