Safe Haskell | None |
---|---|
Language | Haskell2010 |
Synopsis
- data Fraction a where
- pattern (:%:) :: a -> a -> Fraction a
- pattern (:%!) :: (HasCallStack, Integral a, UpperBoundless a) => a -> a -> Fraction a
- mkFraction :: (Integral a, UpperBoundless a) => a -> a -> Maybe (Fraction a)
- mkFractionTH :: (Integral a, Lift a, UpperBoundless a) => a -> a -> Code Q (Fraction a)
- (%%) :: (Integral a, Lift a, UpperBoundless a) => a -> a -> Code Q (Fraction a)
- unsafeFraction :: (HasCallStack, Integral a, UpperBoundless a) => a -> a -> Fraction a
- (%!) :: (HasCallStack, Integral a, UpperBoundless a) => a -> a -> Fraction a
- numerator :: Fraction a -> a
- denominator :: Fraction a -> a
- reduce :: (Integral a, UpperBoundless a) => Fraction a -> Fraction a
- _MkFraction :: (Integral a, Ord a, UpperBoundless a) => ReversedPrism' (Fraction a) (a, a)
- rmatching :: (Is (ReversedOptic k) An_AffineTraversal, ReversibleOptic k) => Optic k NoIx b a t s -> s -> Either t a
Type
data Fraction a where 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:
n / d
is maximally reduced.d > 0
.
This has a number of consequences.
Fraction's
Eq
is based on an equivalence class, in contrast toRatio
, 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
.
- Fractions are reduced first, e.g.,
The denominator is given more consideration:
does not reduceShow
xx
first. This is to make debugging easier.
is a Fraction
Integer
Field
, and
is a Fraction
Natural
Semiring
.
Examples
>>>
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
pattern (:%:) :: a -> a -> Fraction a infixr 5 | Unidirectional pattern synonym for Since: 0.1 |
pattern (:%!) :: (HasCallStack, Integral a, UpperBoundless a) => a -> a -> Fraction a infixr 5 | Bidirectional pattern synonym for WARNING: Partial Since: 0.1 |
Instances
Creation
mkFraction :: (Integral a, UpperBoundless a) => a -> a -> Maybe (Fraction a) Source #
mkFractionTH :: (Integral a, Lift a, UpperBoundless a) => a -> a -> Code Q (Fraction a) Source #
Template haskell for creating a Fraction
at compile-time.
Examples
>>>
$$(mkFractionTH 7 2)
UnsafeFraction 7 2
Since: 0.1
unsafeFraction :: (HasCallStack, Integral a, UpperBoundless a) => a -> a -> Fraction a Source #
Throws an error when given a denominator of 0.
WARNING: Partial
Examples
>>>
unsafeFraction 7 2
UnsafeFraction 7 2
Since: 0.1
(%!) :: (HasCallStack, Integral a, UpperBoundless a) => a -> a -> Fraction a infixl 7 Source #
Elimination
denominator :: Fraction a -> a Source #
Since: 0.1
Functions
reduce :: (Integral a, UpperBoundless a) => Fraction a -> Fraction a Source #
Reduces a fraction:
- Removes common factors.
- Factors out negative denominators.
reduce (0 :%: _) --> 0 :%: 1
.
Examples
>>>
reduce (7 %! 2)
UnsafeFraction 7 2
>>>
reduce (18 %! 10)
UnsafeFraction 9 5
>>>
reduce (-5 %! -5)
UnsafeFraction 1 1
Since: 0.1
Optics
We provide a ReversedPrism'
_MkFraction
that allows for total
elimination and partial construction, along with LabelOptic
instances for
"numerator" and "denominator".
Examples
>>>
:set -XOverloadedLabels
>>>
import Optics.Core (set, view)
>>>
let x = 2 %! 7
>>>
view #numerator x
2
>>>
set #numerator 5 x
UnsafeFraction 5 7
>>>
view #denominator x
7
_MkFraction :: (Integral a, Ord a, UpperBoundless a) => ReversedPrism' (Fraction a) (a, a) Source #
ReversedPrism'
that enables total elimination and partial construction.
Examples
>>>
import Optics.Core (view)
>>>
f = $$(2 %% 8)
>>>
view _MkFraction f
(1,4)
>>>
rmatching _MkFraction (0, 4)
Right (UnsafeFraction 0 1)
>>>
rmatching _MkFraction (1, 0)
Left (1,0)
Since: 0.1
rmatching :: (Is (ReversedOptic k) An_AffineTraversal, ReversibleOptic k) => Optic k NoIx b a t s -> s -> Either t a Source #
Reversed matching
. Useful with smart-constructor optics.
Since: 0.1