On Sunday 14 May 2006 03:00 pm, SevenThunders wrote: > Thanks that helps a bit. The realToFrac type coercion works, but > ultimately it seems > that printf won't play nice. Consider this simple haskell code > > module Test > where > import IO > -- import Data.Array.Storable > import Text.Printf > import Foreign.C.Types (CInt, CDouble ) > > y :: CDouble > y = 5.2 > u = realToFrac(y) > test = do printf "%14.7g" u
Try: y :: CDouble y = 5.2 u :: Double u = realToFrac(y) test = do printf "%14.7g" u The root problem seems to be that GHC isn't sure what type 'u' has unless you fix it with a type signature because 'realToFrac' has a polymorphic type. I'm not sure why it works when you type it at the interpreter. > Compiling it into GHCi I get the error, > test.hs:13:11: > No instance for (PrintfType (t t1)) > arising from use of `printf' at test.hs:13:11-16 > Probable fix: add an instance declaration for (PrintfType (t t1)) > In the result of a 'do' expression: printf "%14.7g" u > In the definition of `test': test = do printf "%14.7g" u > Failed, modules loaded: none. > > If I replace the printf by a standard print there is no problem. Also if I > comment out > the line with test = ... , it will load (compile) and I can then type in > the interpreter > printf "%14.7g" u > and it works! > > but > printf "%14.7g" y > > fails because y is of type CDouble. At this point I think I'm giving up on > the > formatted printing in Haskell. It's just too persnickety. > I guess I'll just call printf via C and see what happens. > > -- > View this message in context: > http://www.nabble.com/CDouble-type-coercion-t1615450.html#a4383006 Sent > from the Haskell - Haskell-Cafe forum at Nabble.com. > > _______________________________________________ > Haskell-Cafe mailing list > Haskell-Cafe@haskell.org > http://www.haskell.org/mailman/listinfo/haskell-cafe _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe