Skip to content

Commit

Permalink
rename BoundedUSize to BoundedNat
Browse files Browse the repository at this point in the history
  • Loading branch information
ss2165 committed Aug 24, 2023
1 parent d13a4c1 commit 29f54cb
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/extension/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ lazy_static! {
prelude
.add_type(
SmolStr::new_inline("array"),
vec![TypeParam::Type(TypeBound::Any), TypeParam::max_usize()],
vec![TypeParam::Type(TypeBound::Any), TypeParam::max_nat()],
"array".into(),
TypeDefBound::FromParams(vec![0]),
)
Expand Down Expand Up @@ -68,7 +68,7 @@ pub(crate) const BOOL_T: Type = Type::new_simple_predicate(2);
pub fn new_array(typ: Type, size: u64) -> Type {
let array_def = PRELUDE.get_type("array").unwrap();
let custom_t = array_def
.instantiate_concrete(vec![TypeArg::Type(typ), TypeArg::BoundedUSize(size)])
.instantiate_concrete(vec![TypeArg::Type(typ), TypeArg::BoundedNat(size)])
.unwrap();
Type::new_extension(custom_t)
}
Expand Down
2 changes: 1 addition & 1 deletion src/ops/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ mod test {
fn test_yaml_const() {
let typ_int = CustomType::new(
"mytype",
vec![TypeArg::BoundedUSize(8)],
vec![TypeArg::BoundedNat(8)],
"myrsrc",
TypeBound::Eq,
);
Expand Down
14 changes: 7 additions & 7 deletions src/std_extensions/arithmetic/int_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub(super) fn int_type(width_arg: TypeArg) -> Type {
lazy_static! {
/// Array of valid integer types, indexed by log width of the integer.
pub static ref INT_TYPES: [Type; (MAX_LOG_WIDTH + 1) as usize] = (0..MAX_LOG_WIDTH + 1)
.map(|i| int_type(TypeArg::BoundedUSize(i as u64)))
.map(|i| int_type(TypeArg::BoundedNat(i as u64)))
.collect::<Vec<_>>()
.try_into()
.unwrap();
Expand All @@ -49,13 +49,13 @@ pub const MAX_LOG_WIDTH: u8 = 7;
/// Type parameter for the log width of the integer.
// unsafe block should be ok as the value is definitely not zero.
pub const LOG_WIDTH_TYPE_PARAM: TypeParam =
TypeParam::bounded_usize(unsafe { NonZeroU64::new_unchecked(MAX_LOG_WIDTH as u64 + 1) });
TypeParam::bounded_nat(unsafe { NonZeroU64::new_unchecked(MAX_LOG_WIDTH as u64 + 1) });

/// Get the log width of the specified type argument or error if the argument
/// is invalid.
pub(super) fn get_log_width(arg: &TypeArg) -> Result<u8, TypeArgError> {
match arg {
TypeArg::BoundedUSize(n) if is_valid_log_width(*n as u8) => Ok(*n as u8),
TypeArg::BoundedNat(n) if is_valid_log_width(*n as u8) => Ok(*n as u8),
_ => Err(TypeArgError::TypeMismatch {
arg: arg.clone(),
param: LOG_WIDTH_TYPE_PARAM,
Expand All @@ -64,7 +64,7 @@ pub(super) fn get_log_width(arg: &TypeArg) -> Result<u8, TypeArgError> {
}

pub(super) const fn type_arg(log_width: u8) -> TypeArg {
TypeArg::BoundedUSize(log_width as u64)
TypeArg::BoundedNat(log_width as u64)
}
/// An unsigned integer
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
Expand Down Expand Up @@ -190,12 +190,12 @@ mod test {

#[test]
fn test_int_widths() {
let type_arg_32 = TypeArg::BoundedUSize(5);
let type_arg_32 = TypeArg::BoundedNat(5);
assert_matches!(get_log_width(&type_arg_32), Ok(5));

let type_arg_128 = TypeArg::BoundedUSize(7);
let type_arg_128 = TypeArg::BoundedNat(7);
assert_matches!(get_log_width(&type_arg_128), Ok(7));
let type_arg_256 = TypeArg::BoundedUSize(8);
let type_arg_256 = TypeArg::BoundedNat(8);
assert_matches!(
get_log_width(&type_arg_256),
Err(TypeArgError::TypeMismatch { .. })
Expand Down
2 changes: 1 addition & 1 deletion src/std_extensions/arithmetic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod test {
for i in 0..MAX_LOG_WIDTH + 1 {
assert_eq!(
INT_TYPES[i as usize],
int_type(TypeArg::BoundedUSize(i as u64))
int_type(TypeArg::BoundedNat(i as u64))
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/std_extensions/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ mod test {
.unwrap();

assert!(list_def
.instantiate_concrete([TypeArg::BoundedUSize(3)])
.instantiate_concrete([TypeArg::BoundedNat(3)])
.is_err());

list_def.check_custom(&list_type).unwrap();
Expand Down
8 changes: 4 additions & 4 deletions src/std_extensions/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub const EXTENSION_ID: SmolStr = SmolStr::new_inline("logic");

/// Extension for basic logical operations.
fn extension() -> Extension {
const H_INT: TypeParam = TypeParam::max_usize();
const H_INT: TypeParam = TypeParam::max_nat();
let mut extension = Extension::new(EXTENSION_ID);

extension
Expand All @@ -53,7 +53,7 @@ fn extension() -> Extension {
|arg_values: &[TypeArg]| {
let a = arg_values.iter().exactly_one().unwrap();
let n: u64 = match a {
TypeArg::BoundedUSize(n) => *n,
TypeArg::BoundedNat(n) => *n,
_ => {
return Err(TypeArgError::TypeMismatch {
arg: a.clone(),
Expand All @@ -79,7 +79,7 @@ fn extension() -> Extension {
|arg_values: &[TypeArg]| {
let a = arg_values.iter().exactly_one().unwrap();
let n: u64 = match a {
TypeArg::BoundedUSize(n) => *n,
TypeArg::BoundedNat(n) => *n,
_ => {
return Err(TypeArgError::TypeMismatch {
arg: a.clone(),
Expand Down Expand Up @@ -139,7 +139,7 @@ pub(crate) mod test {
/// Generate a logic extension and operation over [`crate::prelude::BOOL_T`]
pub(crate) fn and_op() -> LeafOp {
EXTENSION
.instantiate_extension_op(AND_NAME, [TypeArg::BoundedUSize(2)])
.instantiate_extension_op(AND_NAME, [TypeArg::BoundedNat(2)])
.unwrap()
.into()
}
Expand Down
24 changes: 12 additions & 12 deletions src/types/type_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use super::Type;
use super::TypeBound;

#[derive(Clone, Debug, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
/// The upper non-inclusive bound of a [`TypeParam::BoundedUsize`]
/// The upper non-inclusive bound of a [`TypeParam::BoundedNat`]
// A None inner value implies the maximum bound: u64::MAX + 1 (all u64 values valid)
pub struct UpperBound(Option<NonZeroU64>);
impl UpperBound {
Expand All @@ -35,8 +35,8 @@ impl UpperBound {
pub enum TypeParam {
/// Argument is a [TypeArg::Type].
Type(TypeBound),
/// Argument is a [TypeArg::BoundedUSize] that is less than the upper bound.
BoundedUSize(UpperBound),
/// Argument is a [TypeArg::BoundedNat] that is less than the upper bound.
BoundedNat(UpperBound),
/// Argument is a [TypeArg::Opaque], defined by a [CustomType].
Opaque(CustomType),
/// Argument is a [TypeArg::Sequence]. A list of indeterminate size containing parameters.
Expand All @@ -50,14 +50,14 @@ pub enum TypeParam {
}

impl TypeParam {
/// [`TypeParam::BoundedUSize`] with the maximum bound (`u64::MAX` + 1)
pub const fn max_usize() -> Self {
Self::BoundedUSize(UpperBound(None))
/// [`TypeParam::BoundedNat`] with the maximum bound (`u64::MAX` + 1)
pub const fn max_nat() -> Self {
Self::BoundedNat(UpperBound(None))
}

/// [`TypeParam::BoundedUSize`] with the stated upper bound (non-exclusive)
pub const fn bounded_usize(upper_bound: NonZeroU64) -> Self {
Self::BoundedUSize(UpperBound(Some(upper_bound)))
/// [`TypeParam::BoundedNat`] with the stated upper bound (non-exclusive)
pub const fn bounded_nat(upper_bound: NonZeroU64) -> Self {
Self::BoundedNat(UpperBound(Some(upper_bound)))
}
}

Expand All @@ -67,8 +67,8 @@ impl TypeParam {
pub enum TypeArg {
/// Where the (Type/Op)Def declares that an argument is a [TypeParam::Type]
Type(Type),
/// Instance of [TypeParam::BoundedUSize]. 64-bit unsigned integer.
BoundedUSize(u64),
/// Instance of [TypeParam::BoundedNat]. 64-bit unsigned integer.
BoundedNat(u64),
///Instance of [TypeParam::Opaque] An opaque value, stored as serialized blob.
Opaque(CustomTypeArg),
/// Instance of [TypeParam::List] or [TypeParam::Tuple], defined by a
Expand Down Expand Up @@ -120,7 +120,7 @@ pub fn check_type_arg(arg: &TypeArg, param: &TypeParam) -> Result<(), TypeArgErr
.try_for_each(|(arg, param)| check_type_arg(arg, param))
}
}
(TypeArg::BoundedUSize(val), TypeParam::BoundedUSize(bound)) if bound.valid_value(*val) => {
(TypeArg::BoundedNat(val), TypeParam::BoundedNat(bound)) if bound.valid_value(*val) => {
Ok(())
}

Expand Down

0 comments on commit 29f54cb

Please sign in to comment.