Skip to content

Ranged Integers #66

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

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open

Ranged Integers #66

wants to merge 25 commits into from

Conversation

IBims1NicerTobi
Copy link
Collaborator

Close #28

@IBims1NicerTobi IBims1NicerTobi self-assigned this Feb 18, 2025
@IBims1NicerTobi
Copy link
Collaborator Author

Add test cases

@IBims1NicerTobi
Copy link
Collaborator Author

Add types to in code values / constants

@IBims1NicerTobi
Copy link
Collaborator Author

Cast module

@IBims1NicerTobi
Copy link
Collaborator Author

Fix this:

Error: Failed to Typecheck Named(ConcreteGlobalReference { id: type_1, template_args: [Unknown(concrete_type_variable_1)] }) = Named(ConcreteGlobalReference { id: type_1, template_args: [Unknown(concrete_type_variable_1)] }) + Named(ConcreteGlobalReference { id: type_1, template_args: [Unknown(concrete_type_variable_5)] })
    ╭─[/home/tobias/.sus/0.2.0-devel/std/util.sus:91:11]
    │
 91 │        value = value + 1
    │                ────┬────
    │                    ╰────── Failed to Typecheck Named(ConcreteGlobalReference { id: type_1, template_args: [Unknown(concrete_type_variable_1)] }) = Named(ConcreteGlobalReference { id: type_1, template_args: [Unknown(concrete_type_variable_1)] }) + Named(ConcreteGlobalReference { id: type_1, template_args: [Unknown(concrete_type_variable_5)] })
────╯

@VonTum
Copy link
Collaborator

VonTum commented Mar 11, 2025

Fix this:

Error: Failed to Typecheck Named(ConcreteGlobalReference { id: type_1, template_args: [Unknown(concrete_type_variable_1)] }) = Named(ConcreteGlobalReference { id: type_1, template_args: [Unknown(concrete_type_variable_1)] }) + Named(ConcreteGlobalReference { id: type_1, template_args: [Unknown(concrete_type_variable_5)] })
    ╭─[/home/tobias/.sus/0.2.0-devel/std/util.sus:91:11]
    │
 91 │        value = value + 1
    │                ────┬────
    │                    ╰────── Failed to Typecheck Named(ConcreteGlobalReference { id: type_1, template_args: [Unknown(concrete_type_variable_1)] }) = Named(ConcreteGlobalReference { id: type_1, template_args: [Unknown(concrete_type_variable_1)] }) + Named(ConcreteGlobalReference { id: type_1, template_args: [Unknown(concrete_type_variable_5)] })
────╯

Well it seems that that on its own should never typecheck. Rather either the user should specify a cast, or add a % max_val

@IBims1NicerTobi
Copy link
Collaborator Author

Add lower bound to all ints too.

@VonTum
Copy link
Collaborator

VonTum commented Mar 20, 2025

The semantics of MIN and MAX should be:
MIN is inclusive, MAX is exclusive.

So int #(MIN: 3, MAX: 16) can have a value from 3-15 inclusive.

It makes for better ergonomics. (powers of 2, clog2, etc)

@VonTum
Copy link
Collaborator

VonTum commented Mar 20, 2025

Oh also, BitsToInt will need to be expressed from the source bits, as we can't really make any statements about the value of these bits.

We'll need a pow2 #(V) for that. I might add that if I get around to it

@IBims1NicerTobi IBims1NicerTobi requested a review from VonTum March 21, 2025 09:31
VonTum added a commit that referenced this pull request Mar 22, 2025
Useful for #66
Copy link
Collaborator

@VonTum VonTum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, does it appear to behave correctly as you use it?

@@ -500,3 +500,130 @@ impl DelayedConstraint<InstantiationContext<'_, '_>> for SubmoduleTypecheckConst
.error(submod_instr.get_most_relevant_span(), message);
}
}

#[derive(Debug)]
struct BinaryOpTypecheckConstraint {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll also need UnaryOpTypecheckConstraint

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly do the UnaryOperator::Sum/UnaryOperator::Product do with arrays?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the sum of an array has as its bounds Size * MIN .. Size * (MAX-1)+1, as for the product, it should have bounds MIN ^ Size, (MAX - 1) ^ Size + 1

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though this kind of intersects with #74 we'll have to figure out a better design for such large unary reductions at some point

@VonTum
Copy link
Collaborator

VonTum commented Mar 24, 2025

Actually, thinking further about it, we should make both IntToBits and BitsToInt have a template argument in the number of bits. And then have #75 Deal with the integer cast

@IBims1NicerTobi
Copy link
Collaborator Author

Add a compiler intrinsic for casting ints
Change the output of codegen

@VonTum
Copy link
Collaborator

VonTum commented Mar 25, 2025

Div & Modulo should error on is_zero

@VonTum
Copy link
Collaborator

VonTum commented Mar 25, 2025

Update Value::get_type to properly handle sized ints

Copy link
Collaborator

@VonTum VonTum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thorough analysis of the cases, well done!

Comment on lines +192 to +201
/// Returns the inclusive bounds of an int. An int #(MIN: 0, MAX: 15) will return (0, 14)
pub fn get_bounds(&self) -> (IBig, IBig) {
let min = self.unwrap_named().template_args[UUID::from_hidden_value(0)]
.unwrap_value()
.unwrap_integer();
let max = self.unwrap_named().template_args[UUID::from_hidden_value(1)]
.unwrap_value()
.unwrap_integer();
(min.clone(), max - 1)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recently added FlatAlloc::cast_to_array and FlatAlloc::map_to_array. (https://github.com/pc2/sus-compiler/blob/master/src/alloc.rs#L622-L632) That would make this code a bit nicer

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an example of it used: https://github.com/pc2/sus-compiler/blob/master/src/instantiation/execute.rs#L272

Thinking about it, arg.unwrap_value().unwrap_integer() is used a lot in this kind of code, perhaps even adding a get_integer_template_args_array method to ConcreteGlobalReference would make code more compact still

@VonTum VonTum self-assigned this Apr 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ranged Integers
2 participants