{-|
    This module is about fractions.

    A fraction can be seen as a real number from the closed interval [0,1]. It can also be seen as a
    percentage. A typical example of a fraction is the extend of a progress bar.
-}
module Data.Fraction (

    -- * Fraction type
    Fraction,

    -- * Conversion
    fromFactor,
    fromPercentage,
    toFactor,
    toPercentage

) where

    -- Data
    import Data.Semigroup as Semigroup
    import Data.Monoid    as Monoid

    -- * Fraction type
    -- |A fraction.
    newtype Fraction = Fraction Double

    instance Semigroup Fraction where

        Fraction factor1 `append` Fraction factor2 = Fraction (factor1 * factor2)

    instance Monoid Fraction where

        mempty = Fraction 1

        mappend = append

    -- * Conversion
    {-|
        Converts a factor into its corresponding fraction.

        If the factor is not from the interval [0,1], a runtime error occurs.
    -}
    fromFactor :: Double -> Fraction
    fromFactor factor | factor >= 0 && factor <= 1 = Fraction factor
                      | otherwise                  = error "fraction: factor out of bounds"

    {-|
        Converts a percentage into its corresponding fraction.

        If the percentage is not from the interval [0,100], a runtime error occurs.
    -}
    fromPercentage :: Double -> Fraction
    fromPercentage perc | perc >= 0 && perc <= 100 = Fraction (perc / 100)
                        | otherwise                = error "fraction: percentage out of bounds"

    -- |Converts a fraction into its corresponding factor.
    toFactor :: Fraction -> Double
    toFactor (Fraction factor) = factor

    -- |Converts a fraction into its corresponding percentage.
    toPercentage :: Fraction -> Double
    toPercentage (Fraction factor) = factor * 100