diff options
Diffstat (limited to 'src/Trivmix')
-rw-r--r-- | src/Trivmix/Types.hs | 43 |
1 files changed, 42 insertions, 1 deletions
diff --git a/src/Trivmix/Types.hs b/src/Trivmix/Types.hs index abfdc43..fe44a27 100644 --- a/src/Trivmix/Types.hs +++ b/src/Trivmix/Types.hs | |||
@@ -5,6 +5,9 @@ module Trivmix.Types | |||
5 | , Level(Lin), toLin | 5 | , Level(Lin), toLin |
6 | , toFloat | 6 | , toFloat |
7 | , asFloat | 7 | , asFloat |
8 | , Balance(..) | ||
9 | , bToFloat | ||
10 | , bAsFloat | ||
8 | , Adjustment(..) | 11 | , Adjustment(..) |
9 | , doAdjustment | 12 | , doAdjustment |
10 | , module Data.Default | 13 | , module Data.Default |
@@ -31,7 +34,7 @@ instance Num Level where | |||
31 | (+) = fmap (either error id) . asFloat (+) | 34 | (+) = fmap (either error id) . asFloat (+) |
32 | (-) = fmap (either error id) . asFloat (-) | 35 | (-) = fmap (either error id) . asFloat (-) |
33 | (*) = fmap (either error id) . asFloat (*) | 36 | (*) = fmap (either error id) . asFloat (*) |
34 | abs = Lin . toLin | 37 | abs = id |
35 | signum = Lin . either error id . refine . signum . toFloat | 38 | signum = Lin . either error id . refine . signum . toFloat |
36 | fromInteger = Lin . either error id . refine . fromInteger | 39 | fromInteger = Lin . either error id . refine . fromInteger |
37 | 40 | ||
@@ -82,9 +85,43 @@ instance Read Level where | |||
82 | instance Eq Level where | 85 | instance Eq Level where |
83 | (==) = (==) `on` toLin | 86 | (==) = (==) `on` toLin |
84 | 87 | ||
88 | instance Ord Level where | ||
89 | compare = compare `on` toLin | ||
90 | |||
85 | instance Default Level where | 91 | instance Default Level where |
86 | def = Lin $$(refineTH 0) | 92 | def = Lin $$(refineTH 0) |
87 | 93 | ||
94 | |||
95 | |||
96 | newtype Balance = Balance { unBalance :: Refined ZeroToOne Float } | ||
97 | deriving (Ord, Eq) | ||
98 | |||
99 | bAsFloat :: (Float -> Float -> Float) -> Balance -> Balance -> Either String Balance | ||
100 | bAsFloat f (unrefine . unBalance -> x) (unrefine . unBalance -> y) = fmap Balance . refine $ f x y | ||
101 | |||
102 | bToFloat :: Balance -> Float | ||
103 | bToFloat = unrefine . unBalance | ||
104 | |||
105 | instance Num Balance where | ||
106 | (+) = fmap (either error id) . bAsFloat (+) | ||
107 | (-) = fmap (either error id) . bAsFloat (-) | ||
108 | (*) = fmap (either error id) . bAsFloat (*) | ||
109 | abs = id | ||
110 | signum = Balance . either error id . refine . signum . bToFloat | ||
111 | fromInteger = Balance . either error id . refine . fromInteger | ||
112 | |||
113 | instance Show Balance where | ||
114 | show = show . bToFloat | ||
115 | |||
116 | instance Read Balance where | ||
117 | readsPrec = readPrec_to_S $ do | ||
118 | b <- readS_to_Prec readsPrec | ||
119 | either (const mzero) (return . Balance) $ refine b | ||
120 | |||
121 | instance Default Balance where | ||
122 | def = Balance $$(refineTH 1) | ||
123 | |||
124 | |||
88 | data Adjustment a = Set a | 125 | data Adjustment a = Set a |
89 | | Add a | 126 | | Add a |
90 | | Sub a | 127 | | Sub a |
@@ -104,6 +141,10 @@ instance Adjustable Level where | |||
104 | sub (DB x) (DB y) = DB $ dBToLin $ ((-) `on` linToDb) x y | 141 | sub (DB x) (DB y) = DB $ dBToLin $ ((-) `on` linToDb) x y |
105 | sub x y = x - y | 142 | sub x y = x - y |
106 | 143 | ||
144 | instance Adjustable Balance where | ||
145 | add = (+) | ||
146 | sub = (-) | ||
147 | |||
107 | doAdjustment :: Adjustable a => a -> Adjustment a -> a | 148 | doAdjustment :: Adjustable a => a -> Adjustment a -> a |
108 | doAdjustment _ (Set y) = y | 149 | doAdjustment _ (Set y) = y |
109 | doAdjustment x (Add y) = add x y | 150 | doAdjustment x (Add y) = add x y |