Skip to content

Commit a28bf38

Browse files
committed
refactor FixNonNeg
1 parent 7be3e9a commit a28bf38

File tree

3 files changed

+58
-39
lines changed

3 files changed

+58
-39
lines changed

pub/functora/src/prelude/Functora/Prelude.hs

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ module Functora.Prelude
126126
Fix (..),
127127
FixNonNeg (..),
128128
inspectFixed,
129+
inspectFix,
129130

130131
-- * DerivingVia
131132
-- $derivingVia
@@ -1213,7 +1214,7 @@ instance From Fix Rational where
12131214
from = via @(Fixed E30) @Fix @Rational
12141215

12151216
newtype FixNonNeg = FixNonNeg
1216-
{ unFixNonNeg :: Fixed E30
1217+
{ unFixNonNeg :: Fix
12171218
}
12181219
deriving stock
12191220
( Eq,
@@ -1224,68 +1225,88 @@ newtype FixNonNeg = FixNonNeg
12241225
Generic
12251226
)
12261227

1227-
instance TryFrom (Fixed E30) FixNonNeg where
1228-
tryFrom src =
1228+
mkViaFix ::
1229+
forall a.
1230+
( From a Fix
1231+
) =>
1232+
a ->
1233+
Either (TryFromException a FixNonNeg) FixNonNeg
1234+
mkViaFix src =
1235+
first (withSource src)
1236+
. tryFrom @Fix @FixNonNeg
1237+
$ from @a @Fix src
1238+
1239+
instance TryFrom Fix FixNonNeg where
1240+
tryFrom src = do
12291241
if src >= 0
12301242
then pure $ FixNonNeg src
12311243
else Left . TryFromException src . Just $ SomeException Exception.Underflow
12321244

1245+
instance TryFrom (Fixed E30) FixNonNeg where
1246+
tryFrom = mkViaFix @(Fixed E30)
1247+
12331248
instance TryFrom Integer FixNonNeg where
1234-
tryFrom src =
1235-
first (withSource src)
1236-
. tryFrom @(Fixed E30) @FixNonNeg
1237-
$ from @Integer @(Fixed E30) src
1249+
tryFrom = mkViaFix @Integer
12381250

12391251
instance TryFrom Natural FixNonNeg where
1240-
tryFrom src =
1241-
first (withSource src)
1242-
. tryFrom @(Fixed E30) @FixNonNeg
1243-
$ via @Integer @Natural @(Fixed E30) src
1252+
tryFrom = mkViaFix @Natural
12441253

12451254
instance TryFrom Rational FixNonNeg where
1246-
tryFrom = tryVia @(Fixed E30) @Rational @FixNonNeg
1255+
tryFrom = tryVia @Fix @Rational @FixNonNeg
12471256

12481257
instance TryFrom Scientific FixNonNeg where
1249-
tryFrom src =
1250-
first (withSource src)
1251-
. tryFrom @Rational @FixNonNeg
1252-
$ toRational @Scientific src
1258+
tryFrom = tryVia @Fix @Scientific @FixNonNeg
12531259

1254-
instance From FixNonNeg (Fixed E30) where
1260+
instance From FixNonNeg Fix where
12551261
from = unFixNonNeg
12561262

1263+
instance From FixNonNeg (Fixed E30) where
1264+
from = unFix . unFixNonNeg
1265+
12571266
instance From FixNonNeg Rational where
1258-
from = via @(Fixed E30) @FixNonNeg @Rational
1267+
from = via @Fix @FixNonNeg @Rational
12591268

12601269
inspectFixed ::
1261-
forall a e.
1262-
( From String a,
1263-
HasResolution e
1270+
forall str e.
1271+
( HasResolution e,
1272+
From String str
12641273
) =>
12651274
Fixed e ->
1266-
a
1275+
str
12671276
inspectFixed =
1268-
from @String @a . showFixed True
1277+
from @String @str
1278+
. showFixed True
1279+
1280+
inspectFix ::
1281+
forall str fix.
1282+
( From fix (Fixed E30),
1283+
From String str
1284+
) =>
1285+
fix ->
1286+
str
1287+
inspectFix =
1288+
inspectFixed
1289+
. from @fix @(Fixed E30)
12691290

12701291
instance Num FixNonNeg where
1271-
lhs + rhs = unsafeFrom @(Fixed E30) @FixNonNeg $ unFixNonNeg lhs + unFixNonNeg rhs
1272-
lhs - rhs = unsafeFrom @(Fixed E30) @FixNonNeg $ unFixNonNeg lhs - unFixNonNeg rhs
1273-
lhs * rhs = unsafeFrom @(Fixed E30) @FixNonNeg $ unFixNonNeg lhs * unFixNonNeg rhs
1274-
negate = unsafeFrom @(Fixed E30) @FixNonNeg . negate . unFixNonNeg
1275-
abs = unsafeFrom @(Fixed E30) @FixNonNeg . abs . unFixNonNeg
1276-
signum = unsafeFrom @(Fixed E30) @FixNonNeg . signum . unFixNonNeg
1292+
lhs + rhs = unsafeFrom @Fix @FixNonNeg $ unFixNonNeg lhs + unFixNonNeg rhs
1293+
lhs - rhs = unsafeFrom @Fix @FixNonNeg $ unFixNonNeg lhs - unFixNonNeg rhs
1294+
lhs * rhs = unsafeFrom @Fix @FixNonNeg $ unFixNonNeg lhs * unFixNonNeg rhs
1295+
negate = unsafeFrom @Fix @FixNonNeg . negate . unFixNonNeg
1296+
abs = unsafeFrom @Fix @FixNonNeg . abs . unFixNonNeg
1297+
signum = unsafeFrom @Fix @FixNonNeg . signum . unFixNonNeg
12771298
fromInteger = unsafeFrom @Integer @FixNonNeg
12781299

12791300
instance Real FixNonNeg where
12801301
toRational = toRational . unFixNonNeg
12811302

12821303
instance Fractional FixNonNeg where
12831304
fromRational = unsafeFrom @Rational @FixNonNeg
1284-
lhs / rhs = unsafeFrom @(Fixed E30) @FixNonNeg $ unFixNonNeg lhs / unFixNonNeg rhs
1305+
lhs / rhs = unsafeFrom @Fix @FixNonNeg $ unFixNonNeg lhs / unFixNonNeg rhs
12851306

12861307
instance RealFrac FixNonNeg where
12871308
properFraction x =
1288-
(lhs, unsafeFrom @(Fixed E30) @FixNonNeg rhs)
1309+
(lhs, unsafeFrom @Fix @FixNonNeg rhs)
12891310
where
12901311
(lhs, rhs) = properFraction $ unFixNonNeg x
12911312

pub/functora/src/test/Functora/PreludeSpec.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ spec = do
4949
unsafeFrom @(UTF_8 ByteString) @Text (htmlUid $ addUid nilUid nilUid)
5050
`shouldBe` "uid-HXugtXVfQbdnt1bHDJcE9HU6kDMaPEJSQhN3moaHr6Hp"
5151
it "inspectFixed" $ do
52-
inspectFixed @Text (unFix 2.5) `shouldBe` "2.5"
53-
inspectFixed @Text (unFixNonNeg 2.500) `shouldBe` "2.5"
52+
inspectFix @Text @Fix 2.5 `shouldBe` "2.5"
53+
inspectFix @Text @Fix 0 `shouldBe` "0"
54+
inspectFix @Text @Fix (-2.5) `shouldBe` "-2.5"
55+
inspectFix @Text @FixNonNeg 2.500 `shouldBe` "2.5"
56+
inspectFix @Text @FixNonNeg 0 `shouldBe` "0"
5457
it "parseRatio/overflow"
5558
$ inspect @Text (parseRatio @Text @Word8 @(Either SomeException) "0.333")
5659
`shouldBe` "Left (ParseException {parseExceptionSource = \"0.333\", parseExceptionSourceType = Text, parseExceptionTargetType = Ratio Word8, parseExceptionFailure = \"Word8 numerator or denominator seems to be out of bounds, expected 333 % 1000 but got 77 % 232\"})"

pub/functora/src/test/Functora/RoundSpec.hs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
module Functora.RoundSpec (spec) where
22

3-
import Data.Fixed (E12, Fixed)
43
--
54
-- NOTE : Round functions will fail for FixNonNeg because of impl details.
65
--
7-
import Functora.Prelude (E30, Fix, throw, throwString, try)
6+
import Functora.Prelude (Fix, throw, throwString, try)
87
import Functora.Round (dpRound, sdRound)
98
import Numeric.Natural (Natural)
109
import System.Exit (ExitCode (..))
@@ -29,14 +28,10 @@ spec = do
2928
"src/round/Functora/Round.hs"
3029
]
3130

32-
mkRoundSpec @(Fixed E30) "dpRound/Fixed/E30" dpRound dpRoundTestData
33-
mkRoundSpec @(Fixed E12) "dpRound/Fixed/E12" dpRound dpRoundTestData
3431
mkRoundSpec @Rational "dpRound/Rational" dpRound dpRoundTestData
3532
mkRoundSpec @Double "dpRound/Double" dpRound dpRoundTestData
3633
mkRoundSpec @Fix "dpRound/Fix" dpRound dpRoundTestData
3734

38-
mkRoundSpec @(Fixed E30) "sdRound/Fixed/E30" sdRound sdRoundTestData
39-
mkRoundSpec @(Fixed E12) "sdRound/Fixed/E12" sdRound sdRoundTestData
4035
mkRoundSpec @Rational "sdRound/Rational" sdRound sdRoundTestData
4136
mkRoundSpec @Double "sdRound/Double" sdRound sdRoundTestData
4237
mkRoundSpec @Fix "sdRound/Fix" sdRound sdRoundTestData

0 commit comments

Comments
 (0)