bjornbm / dimensional

Dimensional library variant built on Data Kinds, Closed Type Families, TypeNats (GHC 7.8+).
BSD 3-Clause "New" or "Revised" License
102 stars 16 forks source link

Example in readme does not seem to compile #123

Closed tebello-thejane closed 8 years ago

tebello-thejane commented 8 years ago

I get the following error when trying to compile the example in the readme, with main = undefined appended:

Couldn't match type ‘'numtype-dk-0.5:Numeric.NumType.DK.Integers.Neg1’
                 with ‘'numtype-dk-0.5:Numeric.NumType.DK.Integers.Zero’
  Expected type: Unit 'NonMetric DGravitationalParameter Double
    Actual type: Dimensional
                   ('Numeric.Units.Dimensional.Variants.DUnit 'NonMetric
                    Numeric.Units.Dimensional.Variants.* 'Numeric.Units.Dimensional.Variants.DUnit
                                                           'NonMetric)
                   ('Dim
                      'numtype-dk-0.5:Numeric.NumType.DK.Integers.Pos3
                        'numtype-dk-0.5:Numeric.NumType.DK.Integers.Neg1
                      'numtype-dk-0.5:Numeric.NumType.DK.Integers.Zero
                        'numtype-dk-0.5:Numeric.NumType.DK.Integers.Zero
                        'numtype-dk-0.5:Numeric.NumType.DK.Integers.Zero
                        'numtype-dk-0.5:Numeric.NumType.DK.Integers.Zero
                        'numtype-dk-0.5:Numeric.NumType.DK.Integers.Zero
                      * 'Dim
                          'numtype-dk-0.5:Numeric.NumType.DK.Integers.Zero
                          'numtype-dk-0.5:Numeric.NumType.DK.Integers.Zero
                          'numtype-dk-0.5:Numeric.NumType.DK.Integers.Neg2
                          'numtype-dk-0.5:Numeric.NumType.DK.Integers.Zero
                          'numtype-dk-0.5:Numeric.NumType.DK.Integers.Zero
                          'numtype-dk-0.5:Numeric.NumType.DK.Integers.Zero
                          'numtype-dk-0.5:Numeric.NumType.DK.Integers.Zero)
                     Double
    In the second argument of ‘(*~)’, namely
      ‘(meter ^ pos3 * (kilo gram) ^ neg1 * second ^ neg2)’
    In the expression:
      6.67384e-11 *~ (meter ^ pos3 * (kilo gram) ^ neg1 * second ^ neg2)
    In an equation for ‘g’:
        g = 6.67384e-11
            *~ (meter ^ pos3 * (kilo gram) ^ neg1 * second ^ neg2)

--------------------------------------------------------------------------------------------------------------------------

    Couldn't match type ‘'numtype-dk-0.5:Numeric.NumType.DK.Integers.Pos1’
                   with ‘'numtype-dk-0.5:Numeric.NumType.DK.Integers.Zero’
    Expected type: Acceleration a
      Actual type: Dimensional
                     ('Numeric.Units.Dimensional.Variants.DQuantity
                      Numeric.Units.Dimensional.Variants.* 'Numeric.Units.Dimensional.Variants.DQuantity)
                     ('Dim
                        'numtype-dk-0.5:Numeric.NumType.DK.Integers.Pos3
                        'numtype-dk-0.5:Numeric.NumType.DK.Integers.Pos1
                        'numtype-dk-0.5:Numeric.NumType.DK.Integers.Neg2
                        'numtype-dk-0.5:Numeric.NumType.DK.Integers.Zero
                        'numtype-dk-0.5:Numeric.NumType.DK.Integers.Zero
                        'numtype-dk-0.5:Numeric.NumType.DK.Integers.Zero
                        'numtype-dk-0.5:Numeric.NumType.DK.Integers.Zero
                      / 'Dim
                          'numtype-dk-0.5:Numeric.NumType.DK.Integers.Pos2
                          'numtype-dk-0.5:Numeric.NumType.DK.Integers.Zero
                          'numtype-dk-0.5:Numeric.NumType.DK.Integers.Zero
                          'numtype-dk-0.5:Numeric.NumType.DK.Integers.Zero
                          'numtype-dk-0.5:Numeric.NumType.DK.Integers.Zero
                          'numtype-dk-0.5:Numeric.NumType.DK.Integers.Zero
                          'numtype-dk-0.5:Numeric.NumType.DK.Integers.Zero)
                     a
    In the expression: g * m / r ^ pos2
    In an equation for ‘gravitationalFieldStrength’:
        gravitationalFieldStrength m r = g * m / r ^ pos2

Any ideas what's happening here? Perhaps GHC type inference is failing for approximateAccelerationDueToGravityOnEarth?

dmcclean commented 8 years ago

Thanks very much for the report, I'm very sorry about this issue.

It looks like what happened here is that when I was copying this example into the readme from a prior version of the project, I tried to clarify it and I didn't run my "clarifications" past the compiler.

The issue is that GravitationalParameter a isn't the type of Newton's gravitational constant, G. It's instead the type of G times a mass. This is apparently commonly used in orbital mechanics (not my field) because the product can be measured with better precision than the two values alone. I evidently didn't know that when I made the change.

In the newly-released dimensional-codata package we provide the following definition, which I recommend if you need to actually solve a problem similar to the one in the current example:

newtonianConstantOfGravitation :: (Fractional a) => Quantity ('Dim 'Pos3 'Neg1 'Neg2 'Zero 'Zero 'Zero 'Zero) a -- Pretty sure this dimension doesn't have a useful name.
newtonianConstantOfGravitation = 6.67408e-11 *~ (newton * (meter / kilo gram)^pos2)

Since copying that into the example would introduce either an additional dependency or an additional import (for Pos3, Neg2 etc) and an additional language extension (DataKinds) merely because we don't have a quantity synonym for the dimension of G, I'm inclined to rewrite the whole thing by picking a different example. I will try to get a minor release on hackage today with an example that works.

For your immediate use, the following version of the example code compiles and does what was intended:

import Numeric.Units.Dimensional.Prelude
import Numeric.Units.Dimensional.NonSI (gee)
import Numeric.NumType.DK.Integers (TypeInt(..))

radiusOfEarth :: Length Double
radiusOfEarth = 6371 *~ kilo meter

massOfEarth :: Mass Double
massOfEarth = 5.97e24 *~ kilo gram

g :: Quantity ('Dim 'Pos3 'Neg1 'Neg2 'Zero 'Zero 'Zero 'Zero) Double
g = 6.67384e-11 *~ (newton * (meter / kilo gram)^pos2)

gravitationalFieldStrength :: Mass Double -> Length Double -> Acceleration Double
gravitationalFieldStrength m r = g * m / r^pos2

approximateAccelerationDueToGravityOnEarth :: Acceleration Double
approximateAccelerationDueToGravityOnEarth = gravitationalFieldStrength massOfEarth radiusOfEarth

differenceFromStandardValue :: Double
differenceFromStandardValue = approximateAccelerationDueToGravityOnEarth /~ gee

main = putStrLn . show $ differenceFromStandardValue
tebello-thejane commented 8 years ago

Hey.

No stress, man. I was just pointing out that the doc is incorrect, so that new users (such as myself) could use your amazing library better. Thanks for the quick response, and for alerting me to dimensional-codata. :-D

dmcclean commented 8 years ago

Thanks again!

Fixed in 708a7dd3469663c62fd73b3ab1e5b67a9e34d095 and released to hackage as v1.0.1.1.

tebello-thejane commented 8 years ago

Excellent! Awesome, man. :-D :+1:

dmcclean commented 8 years ago

Let us know what else you find, or other areas where we can improve the documentation! Thanks again and happy holidays. :)