smart-math-0.1: Mathematical smart constructors
Safe HaskellNone
LanguageHaskell2010

Numeric.Data.Fraction.Internal

Description

Provides the Fraction type, a safer alternative to Ratio.

Since: 0.1

Synopsis

Type

data Fraction a Source #

Type for representing fractions. Designed to be similar to Ratio with the primary difference that it does not require the following invariants for its instances (e.g. Eq) to be sensible:

  1. n / d is maximally reduced.
  2. d > 0.

This has a number of consequences.

  1. Fraction's Eq is based on an equivalence class, in contrast to Ratio, which compares the numerator and denominator directly:

    • Fractions are reduced first, e.g., 2 :%: 4 === 1 :%: 2.
    • Negative denominators are considered: 1 :%: 1 === -1 :%: -1.
  2. The denominator is given more consideration:

    • abs operates on the numerator and the denominator.
    • signum is positive if both are negative.
  3. Show x does not reduce x first. This is to make debugging easier.

Fraction Integer is a Field, and Fraction Natural is a Semiring.

Examples

Expand
>>> 2 %! 6 == 1 %! 3
True
>>> 1 %! 1 == -1 %! -1
True
>>> 1 %! 7 >= 1 %! -2
True
>>> -1 %! 7 >= 1 %! -2
True
>>> import Data.Text.Display (display)
>>> display $ 2 %! 6
"1 / 3"

Since: 0.1

Constructors

UnsafeFraction !a !a 

Bundled Patterns

pattern (:%:) :: a -> a -> Fraction a infixr 5

Unidirectional pattern synonym for Fraction. This allows us to pattern match on a fraction term without exposing the unsafe internal details.

Since: 0.1

pattern (:%!) :: (HasCallStack, Integral a, UpperBoundless a) => a -> a -> Fraction a infixr 5

Bidirectional pattern synonym for Fraction. Note that this is not safe in general, as construction with a zero denominator with throw an error.

WARNING: Partial

Since: 0.1

Instances

Instances details
HasField "denominator" (Fraction n) n Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

Methods

getField :: Fraction n -> n #

HasField "numerator" (Fraction n) n Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

Methods

getField :: Fraction n -> n #

(k ~ A_Getter, a ~ n, b ~ n) => LabelOptic "denominator" k (Fraction n) (Fraction n) a b Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

Methods

labelOptic :: Optic k NoIx (Fraction n) (Fraction n) a b Source #

(k ~ A_Lens, a ~ n, b ~ n) => LabelOptic "numerator" k (Fraction n) (Fraction n) a b Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

Methods

labelOptic :: Optic k NoIx (Fraction n) (Fraction n) a b Source #

Lift a => Lift (Fraction a :: Type) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

Methods

lift :: Quote m => Fraction a -> m Exp #

liftTyped :: forall (m :: Type -> Type). Quote m => Fraction a -> Code m (Fraction a) #

AGroup (Fraction Integer) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

AMonoid (Fraction Integer) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

AMonoid (Fraction Natural) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

ASemigroup (Fraction Integer) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

ASemigroup (Fraction Natural) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

Field (Fraction Integer) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

MGroup (Fraction Integer) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

MGroup (Fraction Natural) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

MMonoid (Fraction Integer) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

MMonoid (Fraction Natural) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

MSemigroup (Fraction Integer) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

MSemigroup (Fraction Natural) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

Normed (Fraction Integer) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

Normed (Fraction Natural) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

Ring (Fraction Integer) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

Semifield (Fraction Integer) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

Semifield (Fraction Natural) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

Semiring (Fraction Integer) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

Semiring (Fraction Natural) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

FromInteger (Fraction Integer) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

FromInteger (Fraction Natural) Source #

WARNING: Partial

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

FromRational (Fraction Integer) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

FromRational (Fraction Natural) Source #

WARNING: Partial

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

(Bounded a, Num a) => Bounded (Fraction a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

(Integral a, UpperBoundless a) => Enum (Fraction a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

Generic (Fraction a) Source # 
Instance details

Defined in Numeric.Data.Fraction.Internal

Associated Types

type Rep (Fraction a)

Since: smart-math-0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

type Rep (Fraction a) = D1 ('MetaData "Fraction" "Numeric.Data.Fraction.Internal" "smart-math-0.1-inplace" 'False) (C1 ('MetaCons "UnsafeFraction" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 a) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 a)))

Methods

from :: Fraction a -> Rep (Fraction a) x #

to :: Rep (Fraction a) x -> Fraction a #

(Integral a, UpperBoundless a) => Num (Fraction a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

(Integral a, UpperBoundless a) => Fractional (Fraction a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

(Integral a, UpperBoundless a) => Real (Fraction a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

Methods

toRational :: Fraction a -> Rational #

(Integral a, UpperBoundless a) => RealFrac (Fraction a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

Methods

properFraction :: Integral b => Fraction a -> (b, Fraction a) #

truncate :: Integral b => Fraction a -> b #

round :: Integral b => Fraction a -> b #

ceiling :: Integral b => Fraction a -> b #

floor :: Integral b => Fraction a -> b #

Show a => Show (Fraction a) Source # 
Instance details

Defined in Numeric.Data.Fraction.Internal

Methods

showsPrec :: Int -> Fraction a -> ShowS #

show :: Fraction a -> String #

showList :: [Fraction a] -> ShowS #

(Bounded a, Num a) => LowerBounded (Fraction a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

LowerBoundless a => LowerBoundless (Fraction a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

(Bounded a, Num a) => UpperBounded (Fraction a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

UpperBoundless a => UpperBoundless (Fraction a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

NFData a => NFData (Fraction a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

Methods

rnf :: Fraction a -> () #

(Eq a, Integral a, UpperBoundless a) => Eq (Fraction a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

Methods

(==) :: Fraction a -> Fraction a -> Bool #

(/=) :: Fraction a -> Fraction a -> Bool #

(Integral a, UpperBoundless a) => Ord (Fraction a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

Methods

compare :: Fraction a -> Fraction a -> Ordering #

(<) :: Fraction a -> Fraction a -> Bool #

(<=) :: Fraction a -> Fraction a -> Bool #

(>) :: Fraction a -> Fraction a -> Bool #

(>=) :: Fraction a -> Fraction a -> Bool #

max :: Fraction a -> Fraction a -> Fraction a #

min :: Fraction a -> Fraction a -> Fraction a #

Division (Fraction Integer) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

Division (Fraction Natural) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

Show a => Display (Fraction a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

type Rep (Fraction a) Source #

Since: 0.1

Instance details

Defined in Numeric.Data.Fraction.Internal

type Rep (Fraction a) = D1 ('MetaData "Fraction" "Numeric.Data.Fraction.Internal" "smart-math-0.1-inplace" 'False) (C1 ('MetaCons "UnsafeFraction" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 a) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 a)))

Creation

unsafeFraction :: (HasCallStack, Integral a, UpperBoundless a) => a -> a -> Fraction a Source #

Throws an error when given a denominator of 0.

WARNING: Partial

Examples

Expand
>>> unsafeFraction 7 2
UnsafeFraction 7 2

Since: 0.1

(%!) :: (HasCallStack, Integral a, UpperBoundless a) => a -> a -> Fraction a infixl 7 Source #

Infix version of unsafeFraction.

WARNING: Partial

Examples

Expand
>>> 7 %! 2
UnsafeFraction 7 2

Since: 0.1

Elimination

numerator :: Fraction a -> a Source #

Since: 0.1

denominator :: Fraction a -> a Source #

Since: 0.1

Functions

reduce :: (Integral a, UpperBoundless a) => Fraction a -> Fraction a Source #

Reduces a fraction:

  1. Removes common factors.
  2. Factors out negative denominators.
  3. reduce (0 :%: _) --> 0 :%: 1.

Examples

Expand
>>> reduce (7 %! 2)
UnsafeFraction 7 2
>>> reduce (18 %! 10)
UnsafeFraction 9 5
>>> reduce (-5 %! -5)
UnsafeFraction 1 1

Since: 0.1

Misc

errMsg :: String -> String Source #

Since: 0.1