-- |
-- Module      : Common.RefinedUtils
-- License     : BSD3
-- Maintainer  : tbidne@gmail.com
-- Exports utility functions for working with 'R.Refined'.
module Common.RefinedUtils
  ( RNonNegative,
    RPositive,
    unsafeNonNeg,
    unsafePos,
    unsafeRef,
    module Refined,
  )
where

import qualified Control.Exception as Ex
import qualified Data.Either as E
import Refined

-- | Alias for @Refined NonNegative@
type RNonNegative a = Refined NonNegative a

-- | Alias for @Refined Positive@
type RPositive a = Refined Positive a

-- | 'unsafeRef' for 'R.NonNegative'.
unsafeNonNeg :: (Num x, Ord x) => x -> Refined NonNegative x
unsafeNonNeg :: x -> Refined NonNegative x
unsafeNonNeg = x -> Refined NonNegative x
forall p x. Predicate p x => x -> Refined p x
unsafeRef

-- | 'unsafeRef' for 'R.Positive'.
unsafePos :: (Num x, Ord x) => x -> Refined Positive x
unsafePos :: x -> Refined Positive x
unsafePos = x -> Refined Positive x
forall p x. Predicate p x => x -> Refined p x
unsafeRef

-- | Unsafe version of 'R.refine' that uses 'Ex.throw' when @x@ does not
-- satisfy the predicate @p@.
unsafeRef :: Predicate p x => x -> Refined p x
unsafeRef :: x -> Refined p x
unsafeRef = (RefineException -> Refined p x)
-> (Refined p x -> Refined p x)
-> Either RefineException (Refined p x)
-> Refined p x
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
E.either RefineException -> Refined p x
forall a e. Exception e => e -> a
Ex.throw Refined p x -> Refined p x
forall a. a -> a
id (Either RefineException (Refined p x) -> Refined p x)
-> (x -> Either RefineException (Refined p x)) -> x -> Refined p x
forall b c a. (b -> c) -> (a -> b) -> a -> c
. x -> Either RefineException (Refined p x)
forall p x.
Predicate p x =>
x -> Either RefineException (Refined p x)
refine