Skip to content

Rules to eliminate casted id's

Some people (e.g. jedbrown, who originally brought this up) have/use C libraries that deal with arrays of doubles (i.e. CDouble) that they would like to use from Haskell code, with the Double type (perhaps there are instances of some classes for Double, but not CDouble). In jedbrown's case he has a CArray datastructure that wraps the C array and is an instance of Haskell's IArray.

However, people doing this sort of thing tend to care a lot about performance, so don't want to spend O(n) time doing O(n) allocation. Thus the temptation is to make the assumption that CDouble == Double, and to create the CArray i Double directly.

The more correct way to do it would be to make a CArray i CDouble and then use amap realToFrac to convert it to a CArray i Double. However, I don't believe that GHC is currently smart enough to optimise away amap realToFrac.

Let's look at a simpler example:

cdoubles :: [CDouble]
cdoubles = read "foo"

doubles :: [Double]
doubles = map realToFrac cdoubles

(here map is playing the role of amap). The core for this looks like (very simplified)

Q.a1 = \ (ds_aMc :: Foreign.C.Types.CDouble) -> ds_aMc
Q.doubles = GHC.Base.map (Q.a1 `cast` <mumble>) Q.cdoubles

It looks to me that if we could produce

Q.doubles = GHC.Base.map (id `cast` <mumble>) Q.cdoubles

instead then we might be able to have a rule

forall mumble . map (id `cast` mumble) = id `cast` mumble'

The major problem, as far as I can see, is how to construct the mumble'.

Does that sound plausible?

Trac metadata
Trac field Value
Version 6.8.2
Type FeatureRequest
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system Unknown
Architecture Unknown
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information