Commits

Thijs Alkemade  committed eb74792

Unhidden Prelude everywhere but NoPrelude. Removed coerce, renamed coerceTo to coerce.

  • Participants
  • Parent commits 786d108

Comments (0)

Files changed (6)

File src/UnitTyped.hs

 module UnitTyped where
 
-import qualified Prelude
-import Prelude (Show(..), Fractional, ($), (++), Double, const, Bool(..), otherwise, undefined, String(..))
-
 -- First, type level naturals, and using those, type level integers
 
 data Nat = One | Suc Nat
 instance (UnitNeg map2 map2', UnitMerge map1 map2' sum, UnitNull sum b) => UnitEq map1 map2 b
 
 ----
-
 ---- Convertable a b means "b is a unit for dimension a"
-
-
-one :: (Fractional f, Convertable a b) => Value f a b
-one = mkVal 1
+----
 
 class Convertable a b | b -> a where
 	factor :: (Fractional f) => Value f a b -> f
 	               left = one
 	               right ::(Fractional f) => Value f c d
 	               right = one
-	           in (Prelude.*) (factor left) (factor right)
+	           in (factor left) * (factor right)
 	showunit b u = let left :: (Fractional f) => Value f a b
 	                   left = one
 	                   right ::(Fractional f) => Value f c d
 	               left = one
 	               right ::(Fractional f) => Value f c d
 	               right = one
-	           in (Prelude./) (factor left) (factor right)
+	           in (factor left) / (factor right)
 	showunit b u = let left :: (Fractional f) => Value f a b
 	                   left = one
 	                   right ::(Fractional f) => Value f c d
 	                in if b then "(" ++ rest ++ ")" else rest
 
 ---- We can coerce something of a specific dimension into any other unit in the same dimension
+---- The second argument is only used for its type, but it allows nice syntace like coerce (120 meter / second) (kilo meter / hour)
 
-coerce :: (Convertable a b, Convertable c d, Fractional f, UnitEq a c True) => Value f a b -> Value f c d
-coerce u = let result = Value $ (Prelude./) ((Prelude.*) (factor u) (val u)) (factor result) in result
-
-coerceTo :: (Convertable a b, Convertable c d, Fractional f, UnitEq a c True) => Value f a b -> Value f c d -> Value f c d
-coerceTo u _ = coerce u
+coerce :: (Convertable a b, Convertable c d, Fractional f, UnitEq a c True) => Value f a b -> Value f c d -> Value f c d
+coerce u _ = let result = mkVal (factor u * val u / factor result) in result
 
 data Mul b d
 
 -- We currently have 5 operators on values-with-units: division, multiplication, addition, substraction and lifting a rational into a given unit
 
 (.*.) :: (Fractional f, Convertable a b, Convertable c d, UnitMerge a c u) => Value f a b -> Value f c d -> Value f u (Mul b d)
-a .*. b = Value $ (Prelude.*) (val a) (val b)
+a .*. b = mkVal (val a * val b)
 
 (./.) :: (Fractional f, Convertable a b, Convertable c d, UnitMerge a c' u, UnitNeg c c') => Value f a b -> Value f c d -> Value f u (Div b d)
-a ./. b = Value $ (Prelude./) (val a) (val b)
-
+a ./. b = mkVal (val a / val b)
 
 (.+.) :: (Fractional f, Convertable a b, Convertable c d, UnitEq c a True) => Value f a b -> Value f c d -> Value f a b
-a .+. b = Value $ f a (coerce b)
-	where
-		f :: (Fractional f) => Value f a b -> Value f a b -> f
-		f a b = (Prelude.+) (val a) (val b)
+a .+. b = mkVal (val a + val (coerce b a))
 
 (.-.) :: (Fractional f, Convertable a b, Convertable c d, UnitEq c a True) => Value f a b -> Value f c d -> Value f a b
-a .-. b = Value $ f a (coerce b)
-	where
-		f :: (Fractional a) => Value a b c -> Value a b c -> a
-		f a b = (Prelude.-) (val a) (val b)
+a .-. b = mkVal (val a - val (coerce b a))
 
 (.$.) :: (Convertable a b, Fractional f) => f -> Value f a b -> Value f a b
-d .$. u = mkVal ((Prelude.*) d (val u))
+d .$. u = mkVal (d * (val u))
 
-mkVal f = Value f
+mkVal :: (Fractional f) => f -> Value f a b
+mkVal = Value
 
 val :: (Fractional f) => Value f a b -> f
 val (Value f) = f
 
+one :: (Fractional f, Convertable a b) => Value f a b
+one = mkVal 1
+
 square :: (Fractional f, Convertable c d, UnitMerge c c u) => Value f c d -> Value f u (Mul d d)
 square x = x .*. x
 
 cubic :: (Fractional f, Convertable c d, UnitMerge c c a, UnitMerge a c u) => Value f c d -> Value f u (Mul (Mul d d) d)
 cubic x = x .*. x .*. x
 
-wrapB :: (Convertable a b, Convertable c d, UnitEq c a True) => (Prelude.Rational -> Prelude.Rational -> Bool) -> Value Prelude.Rational a b -> Value Prelude.Rational c d -> Bool
-wrapB op a b = op (val a) (val $ coerceTo b a)
+wrapB :: (Convertable a b, Convertable c d, UnitEq c a True) => (Rational -> Rational -> Bool) -> Value Rational a b -> Value Rational c d -> Bool
+wrapB op a b = op (val a) (val $ coerce b a)
 
-(.==.), (.<.), (.>.), (.<=.), (.>=.) :: (Convertable a b, Convertable c d, UnitEq c a True) => Value Prelude.Rational a b -> Value Prelude.Rational c d -> Bool
-(.==.) = wrapB (Prelude.==)
-(.<.) = wrapB (Prelude.<)
-(.<=.) = wrapB (Prelude.<=)
-(.>.) = wrapB (Prelude.>)
-(.>=.) = wrapB (Prelude.>=)
+(.==.), (.<.), (.>.), (.<=.), (.>=.) :: (Convertable a b, Convertable c d, UnitEq c a True) => Value Rational a b -> Value Rational c d -> Bool
+(.==.) = wrapB (==)
+(.<.) = wrapB (<)
+(.<=.) = wrapB (<=)
+(.>.) = wrapB (>)
+(.>=.) = wrapB (>=)

File src/UnitTyped/Bytes.hs

 import UnitTyped.SI.Derived
 import UnitTyped.SI
 
-import qualified Prelude
-import Prelude (Show(..), Fractional, ($), (++), Double, const, Bool(..), otherwise, undefined, String(..))
-
 ----
 -- Data
 ----

File src/UnitTyped/Currency.hs

-{-# LANGUAGE FlexibleInstances, RankNTypes, MultiParamTypeClasses, ConstraintKinds, DataKinds, UndecidableInstances, FunctionalDependencies, KindSignatures, TypeFamilies, InstanceSigs, ScopedTypeVariables, FlexibleContexts, TypeOperators, OverlappingInstances, ImpredicativeTypes, GADTs #-}
-
 module UnitTyped.Currency where
 
 import UnitTyped
 import UnitTyped.SI.Meta
 
-import qualified Prelude
-import Prelude (Show(..), Fractional, ($), (++), Double, const, Bool(..), otherwise, undefined, String(..))
-
 ----
 -- Different currencies. Last update: 22-11-2012
 ----
 euro = one
 
 eurocent :: (Fractional f) => Value f CurrencyUnit Euro
-eurocent = 0.01 .$. one
+eurocent = mkVal 0.01
 
 dollar :: (Fractional f) => Value f CurrencyUnit Dollar
 dollar = one
 
 dollarcent :: (Fractional f) => Value f CurrencyUnit Dollar
-dollarcent = 0.01 .$. one
+dollarcent = mkVal 0.01
 
 yen :: (Fractional f) => Value f CurrencyUnit Yen
 yen = one

File src/UnitTyped/NoPrelude.hs

 	fromRational i x y = (Prelude.fromRational i) . (x y)
 
 wrap1 :: (Floating f, Convertable CountUnit b) => (f -> f) -> Value f CountUnit b -> Value f CountUnit Count
-wrap1 op v = (op $ val $ coerceTo v rad) . one
+wrap1 op v = (op $ val $ coerce v rad) . one
 
 sin, cos, tan :: (Floating f, Convertable CountUnit b) => Value f CountUnit b -> Value f CountUnit Count
 sin = wrap1 Prelude.sin

File src/UnitTyped/SI.hs

 
 import UnitTyped
 
-import qualified Prelude
-import Prelude (Show(..), Fractional, ($), (++), Double, const, Bool(..), otherwise, undefined, String(..))
-
 ----
 -- Counting
 ----

File src/UnitTyped/SI/Constants.hs

 import UnitTyped.SI
 import UnitTyped.SI.Meta
 import UnitTyped.SI.Derived
-import UnitTyped.NoPrelude
-
-import qualified Prelude
-import Prelude (Show(..), Fractional, ($), (++), Double, const, Bool(..), otherwise, undefined, String(..))
 
 -- π
-pi :: (Fractional f, Prelude.Floating f) => Value f CountUnit Count
+pi :: (Fractional f, Floating f) => Value f CountUnit Count
 pi = mkVal Prelude.pi
 
 -- speed of light
 h = mkVal 6.6260695729e-34
 
 -- atomic unit of action
-hbar :: (Fractional f, Prelude.Floating f) => Value f (UnitCons Time (Neg One) (UnitCons Length (Pos (Suc One)) (UnitCons Mass (Pos One) UnitNil))) (Mul Joule Second)
-hbar = coerce (h / (2 . pi))
+hbar :: (Fractional f, Floating f) => Value f (UnitCons Time (Neg One) (UnitCons Length (Pos (Suc One)) (UnitCons Mass (Pos One) UnitNil))) (Mul Joule Second)
+hbar = coerce (h ./. (2 .$. UnitTyped.SI.Constants.pi)) (joule .*. second)
 
 -- atomic unit of charge (elementary charge)
 e :: (Fractional f) => Value f Charge Coulomb