-
Notifications
You must be signed in to change notification settings - Fork 15
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
Formalize the use of bounded integers and error-on-overflow semantics #149
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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 |
---|---|---|
|
@@ -14,5 +14,6 @@ | |
limitations under the License. | ||
-/ | ||
|
||
import Cedar.Data.Int64 | ||
import Cedar.Data.Set | ||
import Cedar.Data.Map |
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,82 @@ | ||
/- | ||
Copyright 2022-2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
https://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
-/ | ||
|
||
import Std | ||
|
||
/-! | ||
This file defines a signed 64-bit integer datatype similar to Rust's `i64`. | ||
-/ | ||
|
||
namespace Cedar.Data | ||
|
||
def INT64_MIN : Int := -9223372036854775808 | ||
def INT64_MAX : Int := 9223372036854775807 | ||
|
||
abbrev Int64 := { i : Int // INT64_MIN ≤ i ∧ i ≤ INT64_MAX } | ||
|
||
instance : Inhabited Int64 where | ||
default := Subtype.mk 0 (by simp) | ||
|
||
|
||
namespace Int64 | ||
|
||
----- Definitions ----- | ||
|
||
def mk (i : Int) (h : INT64_MIN ≤ i ∧ i ≤ INT64_MAX) : Int64 := | ||
Subtype.mk i h | ||
|
||
def mk? (i : Int) : Option Int64 := | ||
if h : INT64_MIN ≤ i ∧ i ≤ INT64_MAX | ||
then .some (mk i h) | ||
else .none | ||
|
||
def mk! (i : Int) : Int64 := | ||
if h : INT64_MIN ≤ i ∧ i ≤ INT64_MAX | ||
then mk i h | ||
else panic! s!"not a signed 64-bit integer {i}" | ||
|
||
def lt (i₁ i₂ : Int64) : Bool := i₁.1 < i₂.1 | ||
|
||
def le (i₁ i₂ : Int64) : Bool := i₁.1 ≤ i₂.1 | ||
|
||
def add? (i₁ i₂ : Int64) : Option Int64 := mk? (i₁.1 + i₂.1) | ||
|
||
def sub? (i₁ i₂ : Int64) : Option Int64 := mk? (i₁.1 - i₂.1) | ||
|
||
def mul? (i₁ i₂ : Int64) : Option Int64 := mk? (i₁.1 * i₂.1) | ||
|
||
def neg? (i₁ : Int64) : Option Int64 := mk? (- i₁.1) | ||
|
||
----- Derivations ----- | ||
instance : LT Int64 where | ||
lt := fun i₁ i₂ => Int64.lt i₁ i₂ | ||
|
||
instance : LE Int64 where | ||
le := fun i₁ i₂ => Int64.le i₁ i₂ | ||
|
||
instance int64Lt (i₁ i₂ : Int64) : Decidable (i₁ < i₂) := | ||
if h : Int64.lt i₁ i₂ then isTrue h else isFalse h | ||
|
||
instance int64Le (i₁ i₂ : Int64) : Decidable (i₁ ≤ i₂) := | ||
if h : Int64.le i₁ i₂ then isTrue h else isFalse h | ||
|
||
deriving instance Repr for Int64 | ||
deriving instance DecidableEq for Int64 | ||
|
||
end Int64 | ||
|
||
|
||
end Cedar.Data |
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
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
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.
Nit: is it possible to put
INT64_MIN ≤ i ∧ i ≤ INT64_MAX
in a predicate (function)?