
Hey, I noticed than when using this code: (-1.0) . isKeyPressed leftKeyCode <|> 0.0 instead of (pure (-1.0)) . isKeyPressed leftKeyCode <|> (pure 0.0) than "fromRational" gets called. I am wondering, if this is not pretty inefficient. I am noticing this, because when I compile a netwire test program with haste, the first code segment fails during runtime because of the primOp newByteArray#. Regards, Nathan

Nathan Hüsken
I noticed than when using this code:
(-1.0) . isKeyPressed leftKeyCode <|> 0.0
instead of
(pure (-1.0)) . isKeyPressed leftKeyCode <|> (pure 0.0)
than "fromRational" gets called. I am wondering, if this is not pretty inefficient.
No, it's the same thing. When you write 'pure 1.0', then you're using, say, Double's Fractional instance directly. If you write just '1.0', then you're using Wire's Fractional instance, which is just defined as: fromRational = pure . fromRational So it's the same thing.
I am noticing this, because when I compile a netwire test program with haste, the first code segment fails during runtime because of the primOp newByteArray#.
Michael Snoyman has reported that Netwire 4 compiled fine using GHCJS. Give that one a try. Greets, Ertugrul -- Not to be or to be and (not to be or to be and (not to be or to be and (not to be or to be and ... that is the list monad.

On 10/03/2012 02:04 PM, Ertugrul Söylemez wrote:
When you write 'pure 1.0', then you're using, say, Double's Fractional instance directly. If you write just '1.0', then you're using Wire's Fractional instance, which is just defined as:
fromRational = pure . fromRational
So it's the same thing.
Let me simplify the example: testW1 :: WireP () Double testW1 = pure 1.0 testW2 :: WireP () Double testW2 = 1.0 main = do let (res, _) = stepWire 1.0 () testW putStrLn $ show res (set testW to testW1 or testW2). When I test this with testW=testW2 I see that "fromRational" is called which converts a Rational (=Ration Integer) to a WireP () Double (I tested this by adding a "trace" to the fromRational Control/Wire/Classes.hs). This means the "1.0" is converted to a Rational and then back to a WireP () Double, correct?
Michael Snoyman has reported that Netwire 4 compiled fine using GHCJS. Give that one a try.
I currently can not get ghcjs to work, but I will continue trying. Regards, Nathan

On Wed, Oct 3, 2012 at 6:12 PM, Nathan Hüsken
On 10/03/2012 02:04 PM, Ertugrul Söylemez wrote:
When you write 'pure 1.0', then you're using, say, Double's Fractional instance directly. If you write just '1.0', then you're using Wire's Fractional instance, which is just defined as:
fromRational = pure . fromRational
So it's the same thing.
Let me simplify the example:
testW1 :: WireP () Double testW1 = pure 1.0
testW2 :: WireP () Double testW2 = 1.0
main = do let (res, _) = stepWire 1.0 () testW putStrLn $ show res
(set testW to testW1 or testW2). When I test this with testW=testW2 I see that "fromRational" is called which converts a Rational (=Ration Integer) to a WireP () Double (I tested this by adding a "trace" to the fromRational Control/Wire/Classes.hs). This means the "1.0" is converted to a Rational and then back to a WireP () Double, correct?
1.0 _is_ a Rational in the first place, it's a number literal that is not integral, so it is translated to a fraction (Rational) by the parser then converted at runtime (only once so no worry about efficiency) to the proper type in the Fractional type class by fromRational, like Double, Float, Ratio, ... Here it's a WireP () Double. This is how this code works :
let x :: (Fractional a) => a x = 1.23 y = (3.5 :: Double) + x z = (2.7 :: Float) + x
Note : Does GHC optimize the case were the final type is monomorphic and standard (Double, Float) if -O2 is given ? Sure one fromRational by literal shouldn't hurt much but... -- Jedaï

On 10/03/2012 10:18 PM, Chaddaï Fouché wrote:
1.0 _is_ a Rational in the first place, it's a number literal that is not integral, so it is translated to a fraction (Rational) by the parser then converted at runtime (only once so no worry about efficiency) to the proper type in the Fractional type class by fromRational, like Double, Float, Ratio, ... Here it's a WireP () Double.
This is how this code works :
let x :: (Fractional a) => a x = 1.23 y = (3.5 :: Double) + x z = (2.7 :: Float) + x
That is good to know. I was unaware that 1.0 is parsed as a rational. Thanks for the explanation.
Note : Does GHC optimize the case were the final type is monomorphic and standard (Double, Float) if -O2 is given ? Sure one fromRational by literal shouldn't hurt much but...
I could argue that it also increased the size of the compiled executable, but I am guessing it is of little magnitude.

Nathan Hüsken
Let me simplify the example:
testW1 :: WireP () Double testW1 = pure 1.0
testW2 :: WireP () Double testW2 = 1.0
main = do let (res, _) = stepWire 1.0 () testW putStrLn $ show res
(set testW to testW1 or testW2). When I test this with testW=testW2 I see that "fromRational" is called which converts a Rational (=Ration Integer) to a WireP () Double (I tested this by adding a "trace" to the fromRational Control/Wire/Classes.hs). This means the "1.0" is converted to a Rational and then back to a WireP () Double, correct?
That's incorrect. There is absolutely no difference between the two except perhaps some RULE pragmas might apply. Let me elaborate. For Wire the 'fromRational' function is defined like this: fromRational = pure . fromRational Then the following holds: -- Using Double's Fractional instance: pure 1.0 = pure (fromRational (1 % 1)) -- Using Wire's Fractional instance: 1.0 = (pure . fromRational) (1 % 1) = pure (fromRational (1 % 1)) Greets, Ertugrul -- Not to be or to be and (not to be or to be and (not to be or to be and (not to be or to be and ... that is the list monad.
participants (3)
-
Chaddaï Fouché
-
Ertugrul Söylemez
-
Nathan Hüsken