Skip to content

Conversation

@SeungheonOh
Copy link
Collaborator

adds

scaleValue :: Integer -> Value -> Value

where all of the quantities of the value (second argument) get multiplied by the given constant factor(first argument)

@SeungheonOh SeungheonOh requested a review from zliu41 November 3, 2025 20:26
-- alg n inner = n + Map.size (Map.filter (< zeroQuantity) inner)

-- TODO: make sure (size - neg) is correct
BuiltinSuccess $ Value outer' sizes size (size - neg)
Copy link
Collaborator Author

@SeungheonOh SeungheonOh Nov 3, 2025

Choose a reason for hiding this comment

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

please check here!

Need to make sure if I can just size - neg here or if I need to count all negative quantities again.

Copy link
Member

Choose a reason for hiding this comment

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

I think this is correct, but it's a good idea to add a prop_scaleValueBookkeeping test.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Okay, added bookkeeping property as well. It seems to be fine.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think that's right.

prop_negateIsInverse v =
let
inverseUnion = do
vInv <- V.scaleValue (-1) v
Copy link
Member

Choose a reason for hiding this comment

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

This will occasionally fail, right? You may want to use suchThat to ensure that v doesn't contain minBound :: Quantity.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

nice catch. Thanks

Copy link
Contributor

@kwxm kwxm Nov 4, 2025

Choose a reason for hiding this comment

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

This will occasionally fail, right? You may want to use suchThat to ensure that v doesn't contain minBound :: Quantity.

Can we avoid problems like this by making minBound equal to -(2^127-1) (ie, -maxBound) instead of -2^127?

Copy link
Contributor

Choose a reason for hiding this comment

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

Weird things might also happen if the scalar is close to the upper or lower bound for Quantity. I think that ArbitraryBuiltin for Integer will generate values like that, or you could do it yourself.

Copy link
Member

Choose a reason for hiding this comment

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

Can we avoid problems like this by making minBound equal to -(2^127-1) (ie, -maxBound) instead of -2^127?

I think that's more problematic (i.e., weird) in a different way.

-- alg n inner = n + Map.size (Map.filter (< zeroQuantity) inner)

-- TODO: make sure (size - neg) is correct
BuiltinSuccess $ Value outer' sizes size (size - neg)
Copy link
Member

Choose a reason for hiding this comment

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

I think this is correct, but it's a good idea to add a prop_scaleValueBookkeeping test.

@SeungheonOh SeungheonOh requested a review from zliu41 November 4, 2025 06:43
Copy link
Contributor

@kwxm kwxm left a comment

Choose a reason for hiding this comment

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

This looks good. There was a discussion about whether we should have a negateValue function instead/as well because it might be more efficient than scaling by -1. I suspect that it wouldn't be much different, but we could do a quick implementation and check benchmark times against this to make sure. If negateValue surprises us by turning out to be noticeably faster then I suppose we could have a special case when the scalar is -1 here that traverses the value and just negates everything. That'd need some special treament in the costing to reflect the improved efficiency for that case though, so it might be a bit of a pain.

-- alg n inner = n + Map.size (Map.filter (< zeroQuantity) inner)

-- TODO: make sure (size - neg) is correct
BuiltinSuccess $ Value outer' sizes size (size - neg)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that's right.

prop_negateIsInverse v =
let
inverseUnion = do
vInv <- V.scaleValue (-1) v
Copy link
Contributor

Choose a reason for hiding this comment

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

Weird things might also happen if the scalar is close to the upper or lower bound for Quantity. I think that ArbitraryBuiltin for Integer will generate values like that, or you could do it yourself.

@SeungheonOh SeungheonOh self-assigned this Nov 4, 2025
Copy link
Member

@zliu41 zliu41 left a comment

Choose a reason for hiding this comment

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

LGTM!

prop_negateIsInverse v =
let
inverseUnion = do
vInv <- V.scaleValue (-1) v
Copy link
Member

Choose a reason for hiding this comment

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

Can we avoid problems like this by making minBound equal to -(2^127-1) (ie, -maxBound) instead of -2^127?

I think that's more problematic (i.e., weird) in a different way.

@SeungheonOh
Copy link
Collaborator Author

SeungheonOh commented Nov 5, 2025

For some reason, goldens changed (a577ce9). I'm trying to figure out why.

Update:
I think somehow number of CSE passes changes. I'm not sure how having extra builtin changes CSE pass count. Any idea?

@zliu41
Copy link
Member

zliu41 commented Nov 5, 2025

I don't think it's the number of CSE passes - unless of course you have clear evidence of that. I think it's likely that the additional definitions in plutus-tx-plugin caused certain things to be ordered slightly differently or certain optimizations to work slightly differently (IIRC the initial PIR contains all those definitions, and the redundant ones are then optimized away). This has happened before and is not entirely surprising. I wouldn't spend too much time on it.

@SeungheonOh
Copy link
Collaborator Author

Okay. I saw some cse term not being pulled out and thought it was something to do with CSE. But, in that case, I'll ignore and merge when CI passes.

@zliu41 zliu41 merged commit 996e08b into master Nov 5, 2025
4 of 8 checks passed
@zliu41 zliu41 deleted the sho/scaleValue branch November 5, 2025 15:24
Copy link
Contributor

@effectfully effectfully left a comment

Choose a reason for hiding this comment

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

Sorry for the late review. LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants