-
Notifications
You must be signed in to change notification settings - Fork 500
Add ScaleValue primitive
#7398
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 ScaleValue primitive
#7398
Changes from 1 commit
ea0f767
8f342d9
8e735c2
1a1603c
bf97e4d
50248e1
a577ce9
d5040c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| (program 1.0.0 | ||
| [ (builtin scaleValue) | ||
| (con integer -2) | ||
| (con value [(#aa, [(#aa, 5), (#bb, -15), (#cc, 20)])]) | ||
| ] | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| ({cpu: 100000080100 | ||
| | mem: 100000000600}) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| (program 1.0.0 (con value [(#aa, [(#aa, -10), (#bb, 30), (#cc, -40)])])) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| (program 1.0.0 | ||
| [ (builtin scaleValue) | ||
| (con integer 2) | ||
| (con value [(#aa, [(#aa, 5), (#bb, -15), (#cc, 20)])]) | ||
| ] | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| ({cpu: 100000080100 | ||
| | mem: 100000000600}) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| (program 1.0.0 (con value [(#aa, [(#aa, 10), (#bb, -30), (#cc, 40)])])) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| (program 1.0.0 | ||
| [ (builtin scaleValue) | ||
| (con integer 0) | ||
| (con value [(#aa, [(#aa, 5), (#bb, -15), (#cc, 20)])]) | ||
| ] | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| ({cpu: 100000080100 | ||
| | mem: 100000000600}) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| (program 1.0.0 (con value [])) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ### Added | ||
|
|
||
| - Implementations of `ScaleValue` primitives. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ module PlutusCore.Value ( | |
| maxInnerSize, | ||
| insertCoin, | ||
| deleteCoin, | ||
| scaleValue, | ||
| lookupCoin, | ||
| valueContains, | ||
| unionValue, | ||
|
|
@@ -148,6 +149,11 @@ addQuantity :: Quantity -> Quantity -> Maybe Quantity | |
| addQuantity (UnsafeQuantity x) (UnsafeQuantity y) = quantity (x + y) | ||
| {-# INLINEABLE addQuantity #-} | ||
|
|
||
| -- | Safely scale a quantitie with given integer, checking for overflow. | ||
| scaleQuantity :: Integer -> Quantity -> Maybe Quantity | ||
| scaleQuantity x (UnsafeQuantity y) = quantity (x * y) | ||
| {-# INLINEABLE scaleQuantity #-} | ||
|
|
||
| ---------------------------------------------------------------------------------------------------- | ||
| -- Builtin Value definition ------------------------------------------------------------------------ | ||
|
|
||
|
|
@@ -470,3 +476,33 @@ updateSizes old new = dec . inc | |
| then id | ||
| else IntMap.update (\n -> if n <= 1 then Nothing else Just (n - 1)) old | ||
| {-# INLINEABLE updateSizes #-} | ||
|
|
||
| -- | \(O(n)\). Scale each tokens by the given constant factor. | ||
SeungheonOh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| scaleValue :: Integer -> Value -> BuiltinResult Value | ||
| scaleValue c (Value outer sizes size neg) | ||
SeungheonOh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| -- When scaling by positive factor, no need to change sizes and number of negative amounts. | ||
| | c > 0 = do | ||
| outer' <- go outer | ||
| BuiltinSuccess $ Value outer' sizes size neg | ||
| -- When scaling by negative factor, only need to "flip" negative amounts. | ||
| | c < 0 = do | ||
| outer' <- go outer | ||
| -- let | ||
| -- neg' = Map.foldl' alg 0 outer' | ||
| -- alg n inner = n + Map.size (Map.filter (< zeroQuantity) inner) | ||
|
|
||
| -- TODO: make sure (size - neg) is correct | ||
| BuiltinSuccess $ Value outer' sizes size (size - neg) | ||
|
Collaborator
Author
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. please check here! Need to make sure if I can just
Member
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. I think this is correct, but it's a good idea to add a
Collaborator
Author
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. Okay, added bookkeeping property as well. It seems to be fine.
Contributor
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. I think that's right. |
||
| -- Scaling by 0 is always empty value | ||
| | otherwise = BuiltinSuccess empty | ||
| where | ||
| go :: NestedMap -> BuiltinResult NestedMap | ||
| go x = traverse (traverse goScale) x | ||
| goScale :: Quantity -> BuiltinResult Quantity | ||
| goScale x = | ||
| case scaleQuantity c x of | ||
| Nothing -> | ||
| fail $ | ||
| "scaleValue: quantity out of bounds: " | ||
| <> show c <> " * " <> show (unQuantity x) | ||
| Just q -> pure q | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| integer -> value -> value |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Integer -> Value -> BuiltinResult Value |
Uh oh!
There was an error while loading. Please reload this page.