-
Notifications
You must be signed in to change notification settings - Fork 123
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
Add arrayCopy, arraySet, arrayRangeEqual array primitives. #1268
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,47 @@ primitive arrayConstant : {a, b} b -> (Array a b) | |
primitive arrayLookup : {a, b} (Array a b) -> a -> b | ||
primitive arrayUpdate : {a, b} (Array a b) -> a -> b -> (Array a b) | ||
|
||
/** | ||
* Copy elements from the source array to the destination array. | ||
* | ||
* 'arrayCopy dest_arr dest_idx src_arr src_idx len' copies the | ||
* elements from 'src_arr' at indices '[src_idx .. (src_idx + len)]' into | ||
* 'dest_arr' at indices '[dest_idx .. (dest_idx + len)]'. | ||
* | ||
* The result is undefined if either 'dest_idx + len' or 'src_idx + len' | ||
* wraps around. | ||
*/ | ||
primitive arrayCopy : {n, a} (Array [n] a) -> [n] -> (Array [n] a) -> [n] -> [n] -> (Array [n] a) | ||
/** | ||
* Set elements of the given array. | ||
* | ||
* 'arraySet' arr idx val len' sets the elements of 'arr' at indices | ||
* '[idx .. (idx + len)]' to 'val'. | ||
* | ||
* The result is undefined if 'idx + len' wraps around. | ||
*/ | ||
primitive arraySet : {n, a} (Array [n] a) -> [n] -> a -> [n] -> (Array [n] a) | ||
/** | ||
* Check whether the lhs array and rhs array are equal at a range of | ||
* indices. | ||
* | ||
* 'arrayRangeEq sym lhs_arr lhs_idx rhs_arr rhs_idx len' checks whether | ||
* the elements of 'lhs_arr' at indices '[lhs_idx .. (lhs_idx + len)]' and | ||
* the elements of 'rhs_arr' at indices '[rhs_idx .. (rhs_idx + len)]' are | ||
* equal. | ||
* | ||
* The result is undefined if either 'lhs_idx + len' or 'rhs_idx + len' | ||
* wraps around. | ||
*/ | ||
primitive arrayRangeEqual : {n, a} (Array [n] a) -> [n] -> (Array [n] a) -> [n] -> [n] -> Bool | ||
|
||
arrayRangeLookup : {a, b, n} (Integral a, fin n, n >= 1, LiteralLessThan n a) => (Array a b) -> a -> [n]b | ||
arrayRangeLookup arr idx = res | ||
where | ||
res @ i = arrayLookup arr (idx + i) | ||
|
||
arrayRangeUpdate : {a, b, n} (Integral a, fin n, n >= 1, Literal (n - 1) a) => (Array a b) -> a -> [n]b -> (Array a b) | ||
arrayRangeUpdate arr idx vals = arrs ! 0 | ||
where | ||
arrs = [arr] # [ arrayUpdate acc (idx + i) val | acc <- arrs | i <- [0 .. (n - 1)] | val <- vals ] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a small thing, but I think you could avoid the I think the |
||
|
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.
Sorry to nitpick some more, but I think essentially all these ranges are intended to be half-open and we should probably use the Cryptol-style notation for that, which would make this
[src_idx ..< (src_idx + len)]
, and similar below.