Skip to content

Commit 197c088

Browse files
committed
PR fixes
1 parent 938e237 commit 197c088

File tree

7 files changed

+40
-15
lines changed

7 files changed

+40
-15
lines changed

plutus-benchmark/cardano-loans/src/CardanoLoans/Validator.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ module CardanoLoans.Validator
4141
tokenAsPubKey,
4242
adaSymbol,
4343
adaToken,
44+
fromHaskellRatio,
4445
unsafeRatio,
4546
(-),(*),(+),
4647
loanValidatorCode,
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
### Removed
2-
3-
- From v3: fromGHC,toGHC
4-
51
### Added
62

73
- To v3: numerator,denominator,unsafeRatio
4+
5+
### Changed
6+
7+
- In v3: renamed fromGHC/toGHC to fromRatioHaskell/toRatioHaskell

plutus-ledger-api/src/PlutusLedgerApi/Data/V3.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ module PlutusLedgerApi.Data.V3 (
232232
Ratio.unsafeRatio,
233233
Ratio.numerator,
234234
Ratio.denominator,
235+
Ratio.fromHaskellRatio,
236+
Ratio.toHaskellRatio,
235237

236238
-- *** Association maps
237239
V2.Map,

plutus-ledger-api/src/PlutusLedgerApi/V3.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ module PlutusLedgerApi.V3 (
125125
Ratio.unsafeRatio,
126126
Ratio.numerator,
127127
Ratio.denominator,
128+
Ratio.fromHaskellRatio,
129+
Ratio.toHaskellRatio,
128130

129131
-- *** Association maps
130132
V2.Map,

plutus-tx-plugin/test/StdLib/Spec.hs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Control.Exception (SomeException, evaluate, try)
1717
import Control.Monad.Except (runExceptT)
1818
import Control.Monad.IO.Class (MonadIO (liftIO))
1919
import Data.Proxy (Proxy (..))
20-
import Data.Ratio (Ratio,(%), numerator, denominator)
20+
import Data.Ratio ((%))
2121
import GHC.Exts (fromString)
2222
import Hedgehog (MonadGen, Property)
2323
import Hedgehog qualified
@@ -50,7 +50,7 @@ tests =
5050
[ embed testRatioInterop
5151
, testRatioProperty "round" Ratio.round round
5252
, testRatioProperty "truncate" Ratio.truncate truncate
53-
, testRatioProperty "abs" (fmap (\ r -> Ratio.numerator r % Ratio.denominator r) Ratio.abs) abs
53+
, testRatioProperty "abs" (fmap Ratio.toHaskellRatio Ratio.abs) abs
5454
, embed $ testPropertyNamed "ord" "testOrd" testOrd
5555
, embed $ testPropertyNamed "divMod" "testDivMod" testDivMod
5656
, embed $ testPropertyNamed "quotRem" "testQuotRem" testQuotRem
@@ -69,12 +69,9 @@ tryHard :: (MonadIO m, NFData a) => a -> m (Maybe a)
6969
-- the body, i.e. outside of the call to 'try', defeating the whole purpose.
7070
tryHard ~a = reoption <$> (liftIO $ try @SomeException $ evaluate $ force a)
7171

72-
fromGHC :: Ratio Integer -> Ratio.Rational
73-
fromGHC r = Ratio.unsafeRatio (numerator r) (denominator r)
74-
7572
testRatioInterop :: TestTree
7673
testRatioInterop = testCase "ratioInterop" do
77-
runExceptT (runUPlc [getPlcNoAnn roundPlc, snd (Lift.liftProgramDef (fromGHC 3.75))])
74+
runExceptT (runUPlc [getPlcNoAnn roundPlc, snd (Lift.liftProgramDef (Ratio.fromHaskellRatio 3.75))])
7875
>>= \case
7976
Left e -> assertFailure (show e)
8077
Right r -> r @?= Core.mkConstant () (4 :: Integer)
@@ -85,7 +82,7 @@ testRatioProperty nm plutusFunc ghcFunc =
8582
embed $ testPropertyNamed nm (fromString nm) $ Hedgehog.property $ do
8683
rat <- Hedgehog.forAll $ Gen.realFrac_ (Range.linearFrac (-10000) 100000)
8784
let ghcResult = ghcFunc rat
88-
plutusResult = plutusFunc $ fromGHC rat
85+
plutusResult = plutusFunc $ Ratio.fromHaskellRatio rat
8986
Hedgehog.annotateShow ghcResult
9087
Hedgehog.annotateShow plutusResult
9188
Hedgehog.assert (ghcResult == plutusResult)
@@ -120,7 +117,7 @@ testOrd = Hedgehog.property $ do
120117
n1 <- Hedgehog.forAll $ (%) <$> gen <*> gen'
121118
n2 <- Hedgehog.forAll $ (%) <$> gen <*> gen'
122119
ghcResult <- tryHard $ n1 <= n2
123-
plutusResult <- tryHard $ (PlutusTx.<=) (fromGHC n1) (fromGHC n2)
120+
plutusResult <- tryHard $ (PlutusTx.<=) (Ratio.fromHaskellRatio n1) (Ratio.fromHaskellRatio n2)
124121
Hedgehog.annotateShow ghcResult
125122
Hedgehog.annotateShow plutusResult
126123
Hedgehog.assert (ghcResult == plutusResult)
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
### Removed
22

3-
- PlutusTx.Ratio: half, toGHC, fromGHC
3+
- PlutusTx.Ratio: half
44

55
### Added
66

7-
- Enum Ratio instance that mimicks GHC instance
7+
- Enum Ratio instance that mimicks Haskell's instance
8+
9+
### Changed
10+
11+
- Renamed Ratio's fromGHC/toGHC to fromRatioHaskell/toRatioHaskell

plutus-tx/src/PlutusTx/Ratio.hs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ module PlutusTx.Ratio (
3232
abs,
3333
negate,
3434
gcd,
35+
36+
-- * Conversion from/to Haskell
37+
fromHaskellRatio,
38+
toHaskellRatio,
3539
) where
3640

3741
import PlutusTx.Applicative qualified as P
@@ -48,6 +52,7 @@ import PlutusTx.Ord qualified as P
4852
import PlutusTx.Trace qualified as P
4953
import PlutusTx.Enum
5054

55+
import Data.Ratio qualified as HS
5156
import PlutusTx.Builtins qualified as Builtins
5257

5358
import Control.Monad (guard)
@@ -69,6 +74,8 @@ The following two invariants are maintained:
6974
data Rational = Rational Integer Integer
7075
deriving stock (Haskell.Eq, Show, Generic)
7176

77+
makeLift ''Rational
78+
7279
instance Pretty Rational where
7380
pretty (Rational a b) = "Rational:" <+> pretty a <+> pretty b
7481

@@ -392,7 +399,19 @@ instance Enum Rational where
392399
then []
393400
else x1 : dn_list (x1 P.+ delta)
394401

395-
$(makeLift ''Rational)
402+
{-| Converts a GHC 'Ratio.Rational', preserving value.
403+
404+
Note: Does not work on-chain.
405+
-}
406+
fromHaskellRatio :: HS.Rational -> Rational
407+
fromHaskellRatio r = unsafeRatio (HS.numerator r) (HS.denominator r)
408+
409+
{-| Converts a 'Rational' to a GHC 'Ratio.Rational', preserving value.
410+
411+
Note: Does not work on-chain.
412+
-}
413+
toHaskellRatio :: Rational -> HS.Rational
414+
toHaskellRatio (Rational n d) = n HS.% d
396415

397416
{- HLINT ignore -}
398417

0 commit comments

Comments
 (0)