bjornbm / dimensional-classic

Automatically exported from code.google.com/p/dimensional
BSD 3-Clause "New" or "Revised" License
3 stars 1 forks source link

numtype-tf add reify/reflection function #41

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I want a function like:

import qualified Numeric.NumType.TF as N
import qualified Prelude as P

reifyNT :: Int -> (forall a. N.NumTypeI a => a -> r) -> r
reifyNT n f
    | n > 0 = reifyNT (n P.- 1) (f . N.incr)
    | n < 0 = reifyNT (P.negate n) (f . N.negate)
    | otherwise = f N.zero

Such that reifyNT n N.toNum == n. This doesn't work now because:

a. NumTypeI is not exported
b. ghc has no evidence for N.NumTypeI (N.Succ a) given N.NumTypeI a. I can't 
figure out superclass constraints that can make that happen.

The purpose of this is to be able to implement functions like:

cmap :: (ScaleCxt deltaE ri ri')
    => proxy deltaE
    -> (forall l m t i th n j e. 
        (N.NumType l, N.NumType m, N.NumType t, N.NumType i,
         N.NumType n, N.NumType j,
         e ~ Dim l m t i th n j) =>
           Quantity e a -> Quantity (Mul e deltaE) b)
    -> DimMat ri ci a -> DimMat ri' ci b
-- which does the same as cmap in hmatrix, but with matrices that have units

Original issue reported on code.google.com by vogt.a...@gmail.com on 23 Jan 2014 at 9:35

GoogleCodeExporter commented 9 years ago
a. Could be easily solved of course, but it would effectively negate the 
purpose of having both `NumTypeI` and `NumType`.
b. I don't see a solution to this problem either. Neither for the TF, nor for 
the FD versions. It isn't really clear to me that it is possible.

I don't follow all the context for the `cmap` type signature, so I can't really 
help you attach that problem directly (rather than indirectly via reifyNT).

On the topic of matrices with units, you could take a look at 
https://github.com/bjornbm/dimensional-vectors to see if it can help you. I 
described it briefly at http://stackoverflow.com/a/5856996/298138. It is a work 
in progress and the documentation is far from great, so feel free to ask (on 
Github or via email) if you have questions about it.

Sorry for not getting back to you sooner. I was hoping to get back to you 
during the weekend but life interfered.

Original comment by bjorn.bu...@gmail.com on 28 Jan 2014 at 1:41

GoogleCodeExporter commented 9 years ago
http://lpaste.net/99176 is an example that does work. It's pretty much 
repeating 
http://hackage.haskell.org/package/reflection-1.3.2/docs/Data-Reflection.html.

https://github.com/aavogt/DimMat is pretty close to implementing a proper cmap. 
I'm not sure I need a reifyNT for that anymore, but it is still a useful 
function to have.

Original comment by vogt.a...@gmail.com on 28 Jan 2014 at 6:20

GoogleCodeExporter commented 9 years ago
Regarding http://lpaste.net/99176 type(/kind) level naturals are much easier to 
work with. When you add the negative you lose the ability to guarantee 
well-formedness of a number through the type alone and need to resort to type 
classes. This make a whole lot of things difficult, including your reifyNT 
problem.

Thanks for the link to your DimMat. I'll have to take a very close look and try 
to understand what you're doing there as it overlaps a lot with what I am 
trying (and wanting) to do. Or, rather, would prefer not to have to do myself 
to get type safe linear algebra. ;)

In case you didn't poke around e.g. 
https://github.com/bjornbm/dimensional-experimental/blob/master/Numeric/Units/Di
mensional/AD.hs and 
https://github.com/bjornbm/dimensional-experimental/blob/master/Numeric/Units/Di
mensional/LinearAlgebra/VectorAD.hs might also be mildly interesting since you 
are doing things with AD.

I suspect we would be better served taking any further discussion to github, 
unless specific to this issue.

Original comment by bjorn.bu...@gmail.com on 29 Jan 2014 at 11:42