membrain-0.0.0.0: Type-safe memory units

Safe HaskellNone
LanguageHaskell2010

Membrain.Memory

Contents

Description

This module contains Memory data type and various utility functions:

  1. Create values of type Memory.
  2. Unwrap values of type Memory to integral types.
  3. Pretty-displaying functions.
  4. Parsing.
  5. Numeric functions.
Synopsis

Data type

newtype Memory (mem :: Nat) Source #

Main memory units type. It has phantom type parameter mem of kind Nat which is type level representation of the unit. Stores internally memory as bits. To construct values of type Memory, use functions from the Membrain.Constructors module.

Constructors

Memory 

Fields

Instances
Eq (Memory mem) Source # 
Instance details

Defined in Membrain.Memory

Methods

(==) :: Memory mem -> Memory mem -> Bool #

(/=) :: Memory mem -> Memory mem -> Bool #

Ord (Memory mem) Source # 
Instance details

Defined in Membrain.Memory

Methods

compare :: Memory mem -> Memory mem -> Ordering #

(<) :: Memory mem -> Memory mem -> Bool #

(<=) :: Memory mem -> Memory mem -> Bool #

(>) :: Memory mem -> Memory mem -> Bool #

(>=) :: Memory mem -> Memory mem -> Bool #

max :: Memory mem -> Memory mem -> Memory mem #

min :: Memory mem -> Memory mem -> Memory mem #

Read (Memory mem) Source # 
Instance details

Defined in Membrain.Memory

Show (Memory mem) Source # 
Instance details

Defined in Membrain.Memory

Methods

showsPrec :: Int -> Memory mem -> ShowS #

show :: Memory mem -> String #

showList :: [Memory mem] -> ShowS #

Generic (Memory mem) Source # 
Instance details

Defined in Membrain.Memory

Associated Types

type Rep (Memory mem) :: Type -> Type #

Methods

from :: Memory mem -> Rep (Memory mem) x #

to :: Rep (Memory mem) x -> Memory mem #

Semigroup (Memory mem) Source #

Semigroup over addition.

>>> byte 2 <> byte 5
Memory {unMemory = 56}
Instance details

Defined in Membrain.Memory

Methods

(<>) :: Memory mem -> Memory mem -> Memory mem #

sconcat :: NonEmpty (Memory mem) -> Memory mem #

stimes :: Integral b => b -> Memory mem -> Memory mem #

Monoid (Memory mem) Source # 
Instance details

Defined in Membrain.Memory

Methods

mempty :: Memory mem #

mappend :: Memory mem -> Memory mem -> Memory mem #

mconcat :: [Memory mem] -> Memory mem #

type Rep (Memory mem) Source # 
Instance details

Defined in Membrain.Memory

type Rep (Memory mem) = D1 (MetaData "Memory" "Membrain.Memory" "membrain-0.0.0.0-DhMCfvovxLRErQzfBRPQR4" True) (C1 (MetaCons "Memory" PrefixI True) (S1 (MetaSel (Just "unMemory") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 Natural)))

memory :: forall (mem :: Nat). KnownNat mem => Natural -> Memory mem Source #

Creates Memory of unit mem by the given Natural number. Memorys smart constructor.

>>> memory @Byte 3
Memory {unMemory = 24}

toMemory :: forall (to :: Nat) (from :: Nat). Memory from -> Memory to Source #

Convert memory from one unit to another.

Note: this changes only view, not model. So this operation has zero runtime cost.

>>> showMemory $ toMemory @Kilobyte $ byte 100
"0.1kB"
>>> showMemory $ toMemory @Kibibyte $ byte 100
"0.09765625KiB"

showMemory :: forall mem. (KnownNat mem, KnownUnitSymbol mem) => Memory mem -> String Source #

This showMemory function shows a Memory value as Double along with the measure unit suffix. It shows Memory losslessly while used with standardized units of measurements. The following mathematical law is used to display Memory:

A decimal representation written with a repeating final 0 is supposed to terminate before these zeros. Instead of 1.585000... one simply writes 1.585. The decimal is also called a terminating decimal. Terminating decimals represent rational numbers of the form \( \cfrac{k}{2^n 5^m} \). If you use different forms of units then the show function for Memory hangs.

>>> showMemory (Memory 22 :: Memory Byte)
"2.75B"

readMemory :: forall (mem :: Nat). (KnownUnitSymbol mem, KnownNat mem) => String -> Maybe (Memory mem) Source #

Inverse of showMemory.

>>> readMemory @Byte "2.75B"
Just (Memory {unMemory = 22})
>>> readMemory @Bit "2.75B"
Nothing

Conversion functions

toBits :: Memory mem -> Natural Source #

Lossless Memory conversion to bits. Alias to unMemory.

>>> toBits $ byte 1
8
>>> toBits $ kilobyte 1
8000

toRat :: forall (mem :: Nat). KnownNat mem => Memory mem -> Ratio Natural Source #

Lossless Memory conversion to rational number.

>>> toRat $ byte 4
4 % 1
>>> toRat $ toMemory @Byte $ bit 22
11 % 4

floor :: forall (n :: Type) (mem :: Nat). (Integral n, KnownNat mem) => Memory mem -> n Source #

Floor Memory unit to integral number. This function may lose some information, so use only when:

  1. You don't care about losing information.
  2. You are sure that there will be no loss.
>>> floor $ byte 4
4
>>> floor $ toMemory @Byte $ bit 22
2

Numeric operations

memoryMul :: Natural -> Memory mem -> Memory mem Source #

Returns the result of multiplication Natural with the given Memory value

>>> memoryMul 2 (byte 4)
Memory {unMemory = 64}

memoryDiff :: Memory mem -> Memory mem -> (Ordering, Memory mem) Source #

Returns the result of comparison of two Memory values and the difference between them as another Memory of the same unit.

>>> memoryDiff (bit 4) (bit 8)
(LT,Memory {unMemory = 4})
>>> memoryDiff (byte 8) (byte 4)
(GT,Memory {unMemory = 32})
>>> memoryDiff (kilobyte 2) (kilobyte 2)
(EQ,Memory {unMemory = 0})

memoryPlus :: Memory mem1 -> Memory mem2 -> Memory mem2 Source #

Returns the result of addition of two Memory values casted to the second memory unit.

>>> memoryPlus (bit 8) (megabyte 2)
Memory {unMemory = 16000008}

memoryDiv :: Memory mem1 -> Memory mem2 -> Ratio Natural Source #

Retuns the result of division of two Memory values of any units.

>>> memoryDiv (kilobyte 3) (byte 2)
1500 % 1

Any memory data type

This data type is useful for working with Memory of different units in collections, or when Memory of non-specified unit can be returned.

data AnyMemory Source #

Existential data type for Memorys.

Constructors

(KnownNat mem, KnownUnitSymbol mem) => MkAnyMemory (Memory mem) 
Instances
Show AnyMemory Source # 
Instance details

Defined in Membrain.Memory