Today I am going to write a binary STL file parser and start with the following code:
import Data.Binary.Get
import Data.Word
import Control.Monad
import Data.Bits
import Data.Binary.IEEE754
data Vector3 = Vector3 { x :: Float, y :: Float, z :: Float }
data Triangle = Triangle { normal :: Vector3,
vertex1 :: Vector3,
vertex2 :: Vector3,
vertex3 :: Vector3,
attr :: Word16}
getVector3 :: Get Vector3
getVector3 = do
w1 <- getFloat32le
w2 <- getFloat32le
w3 <- getFloat32le
return $ Vector3 w1 w2 w3
getTriangle :: Get Triangle
getTriangle = do
n <- getVector3
v1 <- getVector3
v2 <- getVector3
v3 <- getVector3
a <- getWord16le
return $ Triangle n v1 v2 v3 a
stlBinary :: Get ([Triangle])
stlBinary = do
_ <- getBytes 80 --Ignore the 80 byte header
cnt <- getWord32be --Read the number of triangles
replicateM (fromIntegral cnt) getTriangle
The GHC compiler complains
Couldn't match type `binary-0.5.1.1:Data.Binary.Get.Get' with `Get'
Expected type: Get Float
Actual type: binary-0.5.1.1:Data.Binary.Get.Get Float
In a stmt of a 'do' block: w3 <- getFloat32le
In the expression:
do { w1 <- getFloat32le;
w2 <- getFloat32le;
w3 <- getFloat32le;
return $ Vector3 w1 w2 w3 }
In an equation for `getVector3':
getVector3
= do { w1 <- getFloat32le;
w2 <- getFloat32le;
w3 <- getFloat32le;
.... }
It looks like the Get
s from Data.Binary
and Data.Binary.IEEE754
conflicts. So how to solve this?
Update
I actually have a workaround -- embedded the implementation of getFloat32le
into my code (it is just 6 lines)
getFloat32le :: Get Float
getFloat32le = fmap toFloat getWord32le
toFloat :: (Storable word, Storable float) => word -> float
toFloat word = unsafePerformIO $ alloca $ \buf -> do
poke (castPtr buf) word
peek buf
This works, but I still want to know how to due with the name conflicts, since duplicating code is not fun.
UPDATE
$ghc-pkg list binary
/var/lib/ghc/package.conf.d
binary-0.5.1.1
/home/joe/.ghc/x86_64-linux-7.6.3/package.conf.d
binary-0.7.2.0
binary-0.7.2.1
$ ghc-pkg unregister binary-0.5.1.1
ghc-pkg: unregistering binary-0.5.1.1 would break the following packages:
bin-package-db-0.0.0.0 ghc-7.6.3 buildwrapper-0.8.6 scion-browser-0.3.1
hoogle-4.2.32 shake-0.12 buildwrapper-0.8.2 dynamic-cabal-0.3.1
(use --force to override)
$ ghc-pkg unregister binary-0.7.2.0
ghc-pkg: unregistering binary-0.7.2.0 would break the following packages:
snap-0.13.2.5 pwstore-fast-2.4.1 SHA-1.6.4 Graphalyze-0.14.1.0
pandoc-1.12.4.2 zip-archive-0.2.3.1 (use --force to override)
$ ghc-pkg unregister binary-0.7.2.1
ghc-pkg: unregistering binary-0.7.2.1 would break the following packages:
data-binary-ieee754-0.4.4 (use --force to override)
I didn't dare to unregister binary-0.7.2.0
and binary-0.5.1.1
, so I unregistereed data-binary-ieee754
and binary-0.7.2.1
, reinstalled data-binary-ieee754
, and then rebuild, it seems to solved the problem!
Questions:
I still have 2 different binary
s, but why it is not a problem this time?