今日は、バイナリSTL ファイルパーサーを作成し、次のコードから始めます。
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
GHCコンパイラが不平を言う
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;
.... }
Get
s fromData.Binary
とData.Binary.IEEE754
競合しているように見えます。では、これを解決する方法は?
アップデート
私は実際に回避策を持っています - の実装を私のコードに埋め込みましたgetFloat32le
(それはわずか6行です)
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
これはうまくいきますが、コードを複製するのは楽しくないので、名前の競合の原因を知りたいです。
アップデート
$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)
binary-0.7.2.0
andの登録を敢えて解除しなかったbinary-0.5.1.1
ので、 and の登録を解除data-binary-ieee754
しbinary-0.7.2.1
、再インストールdata-binary-ieee754
してから再構築したところ、問題が解決したようです。
質問:
私はまだ 2 つbinary
の異なる を持っていますが、なぜ今回は問題にならないのですか?