-- |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
(/) =
  (a -> a -> a) -> a -> a -> Maybe a
forall a b. (Eq a, Num a) => (a -> a -> b) -> a -> a -> Maybe b
safeOp a -> a -> a
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 ->
    Maybe a
forall a. Maybe a
Nothing
  a
a ->
    a -> Maybe a
forall a. a -> Maybe a
Just (a -> a
forall a. Fractional a => a -> a
Real.recip a
a)