Guest User

Untitled

a guest
Nov 17th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. module TypelevelUnits where
  2.  
  3. import Prelude
  4.  
  5. import Data.EuclideanRing (class EuclideanRing)
  6. import Data.Tuple (Tuple(..))
  7. import Data.Typelevel.Num.Reps (D1, D0, d0, d1)
  8. import Data.Typelevel.Num.Sets (toInt, class Pos, class Nat)
  9. import Data.Unit (unit)
  10. import Unsafe.Coerce (unsafeCoerce)
  11.  
  12. undefined :: ∀ a. a
  13. undefined = unsafeCoerce unit
  14.  
  15. data UnitCons a b = UnitCons a b
  16. infix 6 type UnitCons as .*
  17.  
  18. data Inverse a = Inverse a
  19.  
  20. instance showInverse :: (Show a) => Show (Inverse a) where show (Inverse a) = "1/" <> (show a)
  21.  
  22. type UnitDiv a b = UnitCons a (Inverse b)
  23. infix 7 type UnitDiv as ./
  24.  
  25.  
  26. data UnitValue a b = UnitValue a b
  27. infix 5 type UnitValue as :::
  28. infix 5 UnitValue as :::
  29.  
  30.  
  31. instance showUnitValue :: (Show a, Show b) => Show (UnitValue a b) where show (UnitValue a b) = (show a) <> " [" <> (show b) <> "]"
  32.  
  33. data ZeroU
  34. emptyT :: ZeroU
  35. emptyT = undefined
  36.  
  37. data Meter
  38. meter :: Meter
  39. meter = undefined
  40. data Sec
  41. sec :: Sec
  42. sec = undefined
  43.  
  44. data Quantity a b = Quantity a b
  45.  
  46. instance showQuantity :: (Show a, Show b) => Show (Quantity a b) where show (Quantity a b) = (show a) <> "^" <> (show b) <> " "
  47.  
  48. class Unit a where
  49. toArray :: a -> Array (Quantity String Int)
  50.  
  51. instance showMeter :: Show Meter where show _ = "m"
  52. instance showSec :: Show Sec where show _ = "s"
  53. instance showZeroU :: Show ZeroU where show _ = "--"
  54. instance showUnitCons :: (Show a, Show b) => Show (a .* b) where show _ = (show (undefined :: a)) <> "*" <>(show (undefined :: b))
  55.  
  56. instance unitMeter :: Unit Meter where
  57. toArray _ = [Quantity "meter" 1]
  58.  
  59. instance unitSec :: Unit Sec where
  60. toArray _ = [Quantity "second" 1]
  61.  
  62. instance unitZeroU :: Unit ZeroU where toArray _ = []
  63.  
  64. instance unitUnitCons :: (Unit a, Unit b) => Unit (UnitCons a b) where
  65. toArray _ = (toArray (undefined :: a)) <> (toArray (undefined :: b))
  66.  
  67. class Eq a b | a -> b
  68.  
  69. instance equalInverseElement :: (Eq x x') => Eq (x .* ZeroU) x'
  70. instance recurEqual :: (Eq x x', Eq y y', Eq (x' .* y') r) => Eq (x .* y) r
  71. instance eliminateInverse :: (Eq x x') => Eq (x .* (Inverse x')) ZeroU
  72. instance reflEq :: Eq x x
  73.  
  74. example :: Meter .* Sec
  75. example = undefined
  76.  
  77. v1 :: Int ::: Meter ./ Sec
  78. v1 = UnitValue 123 (undefined)
  79.  
  80. v2 :: Meter .* Meter ./ Meter
  81. v2 = undefined
  82.  
  83. v2_eq :: ∀a. Eq (Meter .* (Meter ./ Meter)) a => a
  84. v2_eq = undefined
  85.  
  86. multiply :: ∀a b d. Semiring a => a ::: b -> a ::: d -> a ::: (b .* d)
  87. multiply (UnitValue a b) (UnitValue c d) = UnitValue (a+c) (undefined :: b .* d)
  88.  
  89. divide :: ∀a b d. EuclideanRing a => a ::: b -> a ::: d -> a ::: b ./ d
  90. divide (UnitValue a b) (UnitValue c d) = UnitValue (a/c) (undefined :: b ./ d)
  91.  
  92. averageVelocity :: Int ::: Meter -> Int ::: Sec -> Int ::: (Meter .* Sec)
  93. averageVelocity a b = multiply a b
Add Comment
Please, Sign In to add comment