Fix formatting

This commit is contained in:
Taylor Fausak 2021-04-17 09:48:16 -04:00
parent 4fc8211592
commit 634c06ca7d
8 changed files with 131 additions and 113 deletions

View File

@ -12,35 +12,38 @@ import qualified Witch.Utility as Utility
liftedCast
:: forall source target
. ( TryCast.TryCast source target
, TH.Lift target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
) => source
. ( TryCast.TryCast source target
, TH.Lift target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
)
=> source
-> TH.Q (TH.TExp target)
liftedCast = TH.liftTyped . Utility.unsafeCast
liftedFrom
:: forall s target source
. ( Identity.Identity s ~ source
, TryCast.TryCast source target
, TH.Lift target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
) => source
. ( Identity.Identity s ~ source
, TryCast.TryCast source target
, TH.Lift target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
)
=> source
-> TH.Q (TH.TExp target)
liftedFrom = liftedCast
liftedInto
:: forall t source target
. ( Identity.Identity t ~ target
, TryCast.TryCast source target
, TH.Lift target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
) => source
. ( Identity.Identity t ~ target
, TryCast.TryCast source target
, TH.Lift target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
)
=> source
-> TH.Q (TH.TExp target)
liftedInto = liftedCast

View File

@ -12,35 +12,38 @@ import qualified Witch.Utility as Utility
liftedCast
:: forall source target
. ( TryCast.TryCast source target
, TH.Lift target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
) => source
. ( TryCast.TryCast source target
, TH.Lift target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
)
=> source
-> TH.Q (TH.TExp target)
liftedCast s = TH.unsafeTExpCoerce $ TH.lift (Utility.unsafeCast s :: target)
liftedFrom
:: forall s target source
. ( Identity.Identity s ~ source
, TryCast.TryCast source target
, TH.Lift target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
) => source
. ( Identity.Identity s ~ source
, TryCast.TryCast source target
, TH.Lift target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
)
=> source
-> TH.Q (TH.TExp target)
liftedFrom = liftedCast
liftedInto
:: forall t source target
. ( Identity.Identity t ~ target
, TryCast.TryCast source target
, TH.Lift target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
) => source
. ( Identity.Identity t ~ target
, TryCast.TryCast source target
, TH.Lift target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
)
=> source
-> TH.Q (TH.TExp target)
liftedInto = liftedCast

View File

@ -12,38 +12,41 @@ import qualified Witch.Utility as Utility
liftedCast
:: forall source target m
. ( TryCast.TryCast source target
, TH.Lift target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
, TH.Quote m
) => source
. ( TryCast.TryCast source target
, TH.Lift target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
, TH.Quote m
)
=> source
-> TH.Code m target
liftedCast = TH.liftTyped . Utility.unsafeCast
liftedFrom
:: forall s target m source
. ( Identity.Identity s ~ source
, TryCast.TryCast source target
, TH.Lift target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
, TH.Quote m
) => source
. ( Identity.Identity s ~ source
, TryCast.TryCast source target
, TH.Lift target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
, TH.Quote m
)
=> source
-> TH.Code m target
liftedFrom = liftedCast
liftedInto
:: forall t source m target
. ( Identity.Identity t ~ target
, TryCast.TryCast source target
, TH.Lift target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
, TH.Quote m
) => source
. ( Identity.Identity t ~ target
, TryCast.TryCast source target
, TH.Lift target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
, TH.Quote m
)
=> source
-> TH.Code m target
liftedInto = liftedCast

View File

@ -20,6 +20,6 @@ module Witch
import qualified Witch.Cast
import Witch.Instances ()
import qualified Witch.Lift
import qualified Witch.Utility
import qualified Witch.TryCast
import qualified Witch.TryCastException
import qualified Witch.Utility

View File

@ -20,6 +20,7 @@ import qualified Witch.TryCastException as TryCastException
instance TryCast.TryCast [a] (NonEmpty.NonEmpty a) where
tryCast = maybeTryCast NonEmpty.nonEmpty
instance Cast.Cast (NonEmpty.NonEmpty a) [a] where
cast = NonEmpty.toList
@ -235,7 +236,8 @@ instance TryCast.TryCast Integer Natural.Natural where
-- bug in GHC 9.0.1. By inlining @fromNonNegativeIntegral@ and replacing
-- @fromIntegral@ with @fromInteger@, we can work around the bug.
-- https://mail.haskell.org/pipermail/haskell-cafe/2021-March/133540.html
tryCast = maybeTryCast $ \ s -> if s < 0 then Nothing else Just $ fromInteger s
tryCast =
maybeTryCast $ \s -> if s < 0 then Nothing else Just $ fromInteger s
-- Word8
@ -453,9 +455,8 @@ instance Integral a => Cast.Cast a (Ratio.Ratio a) where
cast = (Ratio.% 1)
instance (Eq a, Num a) => TryCast.TryCast (Ratio.Ratio a) a where
tryCast = maybeTryCast $ \ s -> if Ratio.denominator s == 1
then Just $ Ratio.numerator s
else Nothing
tryCast = maybeTryCast $ \s ->
if Ratio.denominator s == 1 then Just $ Ratio.numerator s else Nothing
-- Fixed
@ -471,14 +472,14 @@ instance Num a => Cast.Cast a (Complex.Complex a) where
cast = (Complex.:+ 0)
instance (Eq a, Num a) => TryCast.TryCast (Complex.Complex a) a where
tryCast = maybeTryCast $ \ s -> if Complex.imagPart s == 0
then Just $ Complex.realPart s
else Nothing
tryCast = maybeTryCast $ \s ->
if Complex.imagPart s == 0 then Just $ Complex.realPart s else Nothing
fromNonNegativeIntegral :: (Integral s, Num t) => s -> Maybe t
fromNonNegativeIntegral x = if x < 0 then Nothing else Just $ fromIntegral x
maybeTryCast :: (s -> Maybe t) -> s -> Either (TryCastException.TryCastException s t) t
maybeTryCast
:: (s -> Maybe t) -> s -> Either (TryCastException.TryCastException s t) t
maybeTryCast f s = case f s of
Nothing -> Left $ TryCastException.TryCastException s
Just t -> Right t

View File

@ -15,11 +15,11 @@ instance
, Typeable.Typeable source
, Typeable.Typeable target
) => Exception.Exception (TryCastException source target) where
displayException (TryCastException x) = mconcat
[ "TryCastException: failed to cast value "
, show x
, " from type "
, show $ Typeable.typeOf x
, " into type "
, show $ Typeable.typeRep (Proxy.Proxy :: Proxy.Proxy target)
]
displayException (TryCastException x) = mconcat
[ "TryCastException: failed to cast value "
, show x
, " from type "
, show $ Typeable.typeOf x
, " into type "
, show $ Typeable.typeRep (Proxy.Proxy :: Proxy.Proxy target)
]

View File

@ -12,30 +12,29 @@ import qualified Witch.Identity as Identity
import qualified Witch.TryCast as TryCast
import qualified Witch.TryCastException as TryCastException
as
:: forall s source
. Identity.Identity s ~ source
=> source
-> source
as :: forall s source . Identity.Identity s ~ source => source -> source
as = id
from
:: forall s target source
. (Identity.Identity s ~ source, Cast.Cast source target)
. (Identity.Identity s ~ source, Cast.Cast source target)
=> source
-> target
from = Cast.cast
into
:: forall t source target
. (Identity.Identity t ~ target, Cast.Cast source target)
. (Identity.Identity t ~ target, Cast.Cast source target)
=> source
-> target
into = Cast.cast
over
:: forall t source target
. (Identity.Identity t ~ target, Cast.Cast source target, Cast.Cast target source)
. ( Identity.Identity t ~ target
, Cast.Cast source target
, Cast.Cast target source
)
=> (target -> target)
-> source
-> source
@ -43,56 +42,62 @@ over f = Cast.cast . f . Cast.cast
via
:: forall u source target through
. (Identity.Identity u ~ through, Cast.Cast source through, Cast.Cast through target)
. ( Identity.Identity u ~ through
, Cast.Cast source through
, Cast.Cast through target
)
=> source
-> target
via = Cast.cast . (\ x -> x :: through) . Cast.cast
via = Cast.cast . (\x -> x :: through) . Cast.cast
tryFrom
:: forall s target source
. (Identity.Identity s ~ source, TryCast.TryCast source target)
. (Identity.Identity s ~ source, TryCast.TryCast source target)
=> source
-> Either (TryCastException.TryCastException source target) target
tryFrom = TryCast.tryCast
tryInto
:: forall t source target
. (Identity.Identity t ~ target, TryCast.TryCast source target)
. (Identity.Identity t ~ target, TryCast.TryCast source target)
=> source
-> Either (TryCastException.TryCastException source target) target
tryInto = TryCast.tryCast
unsafeCast
:: forall source target
. ( Stack.HasCallStack
, TryCast.TryCast source target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
) => source
. ( Stack.HasCallStack
, TryCast.TryCast source target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
)
=> source
-> target
unsafeCast = either Exception.throw id . TryCast.tryCast
unsafeFrom
:: forall s target source
. ( Identity.Identity s ~ source
, Stack.HasCallStack
, TryCast.TryCast source target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
) => source
. ( Identity.Identity s ~ source
, Stack.HasCallStack
, TryCast.TryCast source target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
)
=> source
-> target
unsafeFrom = unsafeCast
unsafeInto
:: forall t source target
. ( Identity.Identity t ~ target
, Stack.HasCallStack
, TryCast.TryCast source target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
) => source
. ( Identity.Identity t ~ target
, Stack.HasCallStack
, TryCast.TryCast source target
, Show source
, Typeable.Typeable source
, Typeable.Typeable target
)
=> source
-> target
unsafeInto = unsafeCast

View File

@ -15,7 +15,10 @@ import Test.HUnit
import Witch
main :: IO ()
main = runTestTTAndExit $ "Witch" ~:
main = runTestTTAndExit $ "Witch" ~: tests
tests :: [Test]
tests =
[ as @Int8 1 ~?= 1
, cast (1 :: Int8) ~?= (1 :: Int16)
, from @Int8 1 ~?= (1 :: Int)