-- |Safe overrides of the methods of class 'Fractional'.
module Incipit.Fractional where

import Data.Eq (Eq)
import Data.Maybe (Maybe (Just, Nothing))
import qualified GHC.Real as Real
import GHC.Real (Fractional)

import Incipit.Integral (safeOp)

-- | Fractional division.
(/) ::
  Eq a =>
  Fractional a =>
  a ->
  a ->
  Maybe a
/ :: forall a. (Eq a, Fractional a) => a -> a -> Maybe a
(/) =
  forall a b. (Eq a, Num a) => (a -> a -> b) -> a -> a -> Maybe b
safeOp forall a. Fractional a => a -> a -> a
(Real./)

-- | Reciprocal fraction.
recip ::
  Eq a =>
  Fractional a =>
  a ->
  Maybe a
recip :: forall a. (Eq a, Fractional a) => a -> Maybe a
recip = \case
  a
0 ->
    forall a. Maybe a
Nothing
  a
a ->
    forall a. a -> Maybe a
Just (forall a. Fractional a => a -> a
Real.recip a
a)