summaryrefslogtreecommitdiff
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
parent7885042246ac4a08e0f7b3ba27a7691a4908f3b6 (diff)
downloadtrivmix-cca9139ed34788e34da599abefd8b2c31cffdeec.tar
trivmix-cca9139ed34788e34da599abefd8b2c31cffdeec.tar.gz
trivmix-cca9139ed34788e34da599abefd8b2c31cffdeec.tar.bz2
trivmix-cca9139ed34788e34da599abefd8b2c31cffdeec.tar.xz
trivmix-cca9139ed34788e34da599abefd8b2c31cffdeec.zip
Fix handling of Balance
-rw-r--r--src/Trivmix/Types.hs43
-rw-r--r--trivmix.cabal2
-rw-r--r--trivmix.nix2
-rw-r--r--trivmix/Trivmix.hs9
4 files changed, 48 insertions, 8 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
diff --git a/trivmix.cabal b/trivmix.cabal
index e39d451..e12ea00 100644
--- a/trivmix.cabal
+++ b/trivmix.cabal
@@ -2,7 +2,7 @@
2-- documentation, see http://haskell.org/cabal/users-guide/ 2-- documentation, see http://haskell.org/cabal/users-guide/
3 3
4name: trivmix 4name: trivmix
5version: 3.1.1 5version: 3.1.2
6-- synopsis: 6-- synopsis:
7-- description: 7-- description:
8license: PublicDomain 8license: PublicDomain
diff --git a/trivmix.nix b/trivmix.nix
index d77a0ee..416c55d 100644
--- a/trivmix.nix
+++ b/trivmix.nix
@@ -5,7 +5,7 @@
5}: 5}:
6mkDerivation { 6mkDerivation {
7 pname = "trivmix"; 7 pname = "trivmix";
8 version = "3.1.1"; 8 version = "3.1.2";
9 src = ./.; 9 src = ./.;
10 isLibrary = true; 10 isLibrary = true;
11 isExecutable = true; 11 isExecutable = true;
diff --git a/trivmix/Trivmix.hs b/trivmix/Trivmix.hs
index 7743246..2e05b13 100644
--- a/trivmix/Trivmix.hs
+++ b/trivmix/Trivmix.hs
@@ -42,12 +42,11 @@ import Data.Function
42import Control.Monad 42import Control.Monad
43 43
44import Text.Heredoc (str) 44import Text.Heredoc (str)
45import Refined 45
46import Refined (refine)
46 47
47import Trivmix.Types 48import Trivmix.Types
48 49
49type Balance = Refined ZeroToOne Float
50
51data Options = Options 50data Options = Options
52 { input :: String 51 { input :: String
53 , output :: String 52 , output :: String
@@ -86,7 +85,7 @@ optionParser = Options
86 <*> option auto ( long "initial-balance" 85 <*> option auto ( long "initial-balance"
87 <> metavar "BALANCE" 86 <> metavar "BALANCE"
88 <> help "Initial value for balance" 87 <> help "Initial value for balance"
89 <> value ($$(refineTH 1.0) :: Balance) 88 <> value def
90 <> showDefault 89 <> showDefault
91 ) 90 )
92 <*> many ( strOption ( long "run" 91 <*> many ( strOption ( long "run"
@@ -161,7 +160,7 @@ trivmix Options{..} = do
161 delay = round $ recip fps * 1e6 160 delay = round $ recip fps * 1e6
162 linInt x a b = a * (1 - x) + b * x 161 linInt x a b = a * (1 - x) + b * x
163 linInt' x a b = either error id $ asFloat (linInt x) a b 162 linInt' x a b = either error id $ asFloat (linInt x) a b
164 mulBalance (unrefine -> b) x = either error id $ asFloat (*) (Lin . either error id $ refine b) x 163 mulBalance (bToFloat -> b) x = either error id $ asFloat (*) (Lin . either error id $ refine b) x
165 newLevel <- mulBalance <$> readMVar balance <*> readMVar level 164 newLevel <- mulBalance <$> readMVar balance <*> readMVar level
166 currentLevel <- readMVar level' 165 currentLevel <- readMVar level'
167 mapM_ (\x -> swapMVar level' (linInt' x currentLevel newLevel) >> threadDelay delay) ([0,recip frames..1] :: [Float]) 166 mapM_ (\x -> swapMVar level' (linInt' x currentLevel newLevel) >> threadDelay delay) ([0,recip frames..1] :: [Float])