si-bytes-0.1: Byte with type-level units (e.g. B, KB, MB, ...).
Safe HaskellNone
LanguageHaskell2010

Data.Bytes.Class.Normalize

Description

Provides the Normalize typeclass.

Since: 0.1

Synopsis

Documentation

class Normalize a where Source #

Used for normalizing bytes b such that

\[ 1 \le \text{normalize}(b) < 1000 \iff 1\text{ B} \le b < 1000\text{ Y}. \]

In the strictest sense, \(\textrm{normalize} : \mathcal{C} \rightarrow \mathcal{C}\) is not a homomorphism, as the combination of two normalized values may itself not be normalized.

However, because the normalized units varies with the value, normalize always returns a type that existentially quantifies the size (e.g. SomeSize). Eq for these types is defined in terms of an equivalence class that takes units into account e.g. 1 P = 1,000 T = 1,000,000 G .... Viewed this way, normalize is actually an isomorphism, as it is essentially a no-op, never leaving the equivalence class.

This means we can happily mix normalization with different functions without worrying about the order. The only requirement we have is that such functions respect substitution:

\[ x = y \implies f(x) = f(y). \]

This is certainly true for all the usual mathematical operations we would normally use, e.g., AGroup addition, Module scalar multiplication. On the other hand, any functions that inspect the underlying numeric value or Bytes types could easily break this law. As such they should be treated with suspicion, at least when used in conjunction with normalize.

The other consideration we must keep in mind is that the final result of a series of computations may not be normalized. If this is desired, then normalize should be the last operation performed. Using normalize in the middle would not cause any harm (other than, perhaps, impacting efficiency), but it would not guarantee the final result is normalized.

Since: 0.1

Associated Types

type Norm a Source #

Since: 0.1

Methods

normalize :: a -> Norm a Source #

Normalizes the value.

Examples

Expand
>>> let bytes = MkBytes 5000 :: Bytes 'M Int
>>> normalize bytes
MkSomeSize SG (MkBytes 5)
>>> let bytes = hideSize (MkBytes 0.01 :: Bytes 'T Float)
>>> normalize bytes
MkSomeSize SG (MkBytes 10.0)

Since: 0.1

Instances

Instances details
(FromInteger n, MGroup n, Normed n, Ord n) => Normalize (SomeSize n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Associated Types

type Norm (SomeSize n) 
Instance details

Defined in Data.Bytes.Internal

type Norm (SomeSize n) = SomeSize n

Methods

normalize :: SomeSize n -> Norm (SomeSize n) Source #

(FromInteger n, MGroup n, Normed n, Ord n) => Normalize (SomeNet n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Network.Internal

Associated Types

type Norm (SomeNet n) 
Instance details

Defined in Data.Bytes.Network.Internal

type Norm (SomeNet n) = SomeNet n

Methods

normalize :: SomeNet n -> Norm (SomeNet n) Source #

(FromInteger n, MGroup n, Normed n, Ord n, SingSize s) => Normalize (Bytes s n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Internal

Associated Types

type Norm (Bytes s n) 
Instance details

Defined in Data.Bytes.Internal

type Norm (Bytes s n) = SomeSize n

Methods

normalize :: Bytes s n -> Norm (Bytes s n) Source #

(FromInteger n, MGroup n, Normed n, Ord n, SingSize s) => Normalize (SomeNetDir s n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Network.Internal

Associated Types

type Norm (SomeNetDir s n) 
Instance details

Defined in Data.Bytes.Network.Internal

type Norm (SomeNetDir s n) = SomeNet n

Methods

normalize :: SomeNetDir s n -> Norm (SomeNetDir s n) Source #

(FromInteger n, MGroup n, Normed n, Ord n) => Normalize (SomeNetSize d n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Network.Internal

Associated Types

type Norm (SomeNetSize d n) 
Instance details

Defined in Data.Bytes.Network.Internal

type Norm (SomeNetSize d n) = SomeNetSize d n

Methods

normalize :: SomeNetSize d n -> Norm (SomeNetSize d n) Source #

(FromInteger n, MGroup n, Normed n, Ord n, SingSize s) => Normalize (NetBytes d s n) Source #

Since: 0.1

Instance details

Defined in Data.Bytes.Network.Internal

Associated Types

type Norm (NetBytes d s n) 
Instance details

Defined in Data.Bytes.Network.Internal

type Norm (NetBytes d s n) = SomeNetSize d n

Methods

normalize :: NetBytes d s n -> Norm (NetBytes d s n) Source #