Guest User

Untitled

a guest
Mar 19th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. {-# LANGUAGE FlexibleContexts #-}
  2. {-# LANGUAGE GeneralizedNewtypeDeriving #-}
  3. {-# LANGUAGE NoMonomorphismRestriction #-}
  4. {-# LANGUAGE StandaloneDeriving #-}
  5. import Control.Lens
  6.  
  7. import Data.Ratio
  8.  
  9. newtype Bits = Bits Rational deriving (Show,Eq, Num, Fractional)
  10.  
  11. newtype Bytes = Bytes Rational deriving (Show,Eq, Num, Fractional )
  12.  
  13. newtype Kilo a = Kilo a deriving Show
  14.  
  15. deriving instance Num a => Num (Kilo a)
  16.  
  17. deriving instance Fractional a => Fractional (Kilo a)
  18.  
  19. type Mega a = Kilo (Kilo a)
  20. type Giga a = Kilo (Mega a)
  21.  
  22.  
  23. bits :: Iso' Bytes Bits
  24. bits = iso (\(Bytes s) -> Bits $ 8 * s) (\(Bits s) -> Bytes $ s / 8)
  25.  
  26. k :: (Fractional a, Num a) => Iso' (Kilo a) a
  27. k = iso (\(Kilo s) -> 1000 * s) (\s -> Kilo $ s / 1000)
  28.  
  29. m = k . k
  30.  
  31. g = k . m
  32.  
  33. kb :: (Fractional a, Num a) => Iso' (Kilo a) a
  34. kb = iso (\(Kilo s) -> 1024 * s) (\s -> Kilo $ s / 1024)
  35.  
  36. mb = kb . kb
  37.  
  38. gb = kb . mb
Add Comment
Please, Sign In to add comment