summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2018-05-15 15:57:09 +0200
committerGregor Kleen <gkleen@yggdrasil.li>2018-05-15 15:57:09 +0200
commitcca9139ed34788e34da599abefd8b2c31cffdeec (patch)
treec4ab677df76f38864d0dab3e2df003a53576d897 /src
parent7885042246ac4a08e0f7b3ba27a7691a4908f3b6 (diff)
downloadtrivmix-cca9139ed34788e34da599abefd8b2c31cffdeec.tar
trivmix-cca9139ed34788e34da599abefd8b2c31cffdeec.tar.gz
trivmix-cca9139ed34788e34da599abefd8b2c31cffdeec.tar.bz2
trivmix-cca9139ed34788e34da599abefd8b2c31cffdeec.tar.xz
trivmix-cca9139ed34788e34da599abefd8b2c31cffdeec.zip
Fix handling of Balance
Diffstat (limited to 'src')
-rw-r--r--src/Trivmix/Types.hs43
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
82instance Eq Level where 85instance Eq Level where
83 (==) = (==) `on` toLin 86 (==) = (==) `on` toLin
84 87
88instance Ord Level where
89 compare = compare `on` toLin
90
85instance Default Level where 91instance Default Level where
86 def = Lin $$(refineTH 0) 92 def = Lin $$(refineTH 0)
87 93
94
95
96newtype Balance = Balance { unBalance :: Refined ZeroToOne Float }
97 deriving (Ord, Eq)
98
99bAsFloat :: (Float -> Float -> Float) -> Balance -> Balance -> Either String Balance
100bAsFloat f (unrefine . unBalance -> x) (unrefine . unBalance -> y) = fmap Balance . refine $ f x y
101
102bToFloat :: Balance -> Float
103bToFloat = unrefine . unBalance
104
105instance 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
113instance Show Balance where
114 show = show . bToFloat
115
116instance Read Balance where
117 readsPrec = readPrec_to_S $ do
118 b <- readS_to_Prec readsPrec
119 either (const mzero) (return . Balance) $ refine b
120
121instance Default Balance where
122 def = Balance $$(refineTH 1)
123
124
88data Adjustment a = Set a 125data 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
144instance Adjustable Balance where
145 add = (+)
146 sub = (-)
147
107doAdjustment :: Adjustable a => a -> Adjustment a -> a 148doAdjustment :: Adjustable a => a -> Adjustment a -> a
108doAdjustment _ (Set y) = y 149doAdjustment _ (Set y) = y
109doAdjustment x (Add y) = add x y 150doAdjustment x (Add y) = add x y