Copyright | (c) 2018-2020 Kowainik |
---|---|
License | MPL-2.0 |
Maintainer | Kowainik <xrom.xkov@gmail.com> |
Safe Haskell | None |
Language | Haskell2010 |
Synopsis
- newtype Memory (mem :: Nat) = Memory {}
- memory :: forall (mem :: Nat). KnownNat mem => Natural -> Memory mem
- toMemory :: forall (to :: Nat) (from :: Nat). Memory from -> Memory to
- showMemory :: forall mem. (KnownNat mem, KnownUnitSymbol mem) => Memory mem -> String
- readMemory :: forall (mem :: Nat). (KnownUnitSymbol mem, KnownNat mem) => String -> Maybe (Memory mem)
- toBits :: Memory mem -> Natural
- toRat :: forall (mem :: Nat). KnownNat mem => Memory mem -> Ratio Natural
- floor :: forall (n :: Type) (mem :: Nat). (Integral n, KnownNat mem) => Memory mem -> n
- memoryMul :: Natural -> Memory mem -> Memory mem
- memoryDiff :: Memory mem -> Memory mem -> (Ordering, Memory mem)
- memoryPlus :: Memory mem1 -> Memory mem2 -> Memory mem2
- memoryDiv :: Memory mem1 -> Memory mem2 -> Ratio Natural
- data AnyMemory = (KnownNat mem, KnownUnitSymbol mem) => MkAnyMemory (Memory mem)
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.
Instances
Eq (Memory mem) Source # | |
Ord (Memory mem) Source # | |
Read (Memory mem) Source # | |
Show (Memory mem) Source # | |
Generic (Memory mem) Source # | |
Semigroup (Memory mem) Source # | Semigroup over addition.
|
Monoid (Memory mem) Source # | |
type Rep (Memory mem) Source # | |
Defined in Membrain.Memory |
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
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:
- You don't care about losing information.
- You are sure that there will be no loss.
>>>
floor $ byte 4
4>>>
floor $ toMemory @Byte $ bit 22
2
Numeric operations
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