[Haskell-cafe] Interfacing C++ iterators does not work as expected

2011-05-02 Thread Huynh Huu Long
Hi,

I tried to access C++ iterators from Haskell via the FFI. There are two
example programs attached, in Haskell as well as in C++. Both utilise the
same iterator wrapper example.cpp, but when running the Haskell program it
prints an empty list, where it should print the contents of some vector. The
C++ program meanwhile runs fine though. What does not seem to work is this
part:

bool
finished (std::vectorint::iterator *begin, std::vectorint::iterator
*end)
{
return *begin == *end;
}

In Haskellland, finished :: Ptr Iterator - Ptr Iterator - IO Bool always
returns IO True. Is this a bug lurking in my program or is it a GHC issue?

Long


ffi_and_iterators.tar.gz
Description: GNU Zip compressed data
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Interfacing C++ iterators does not work as expected

2011-05-02 Thread Gábor Lehel
It works if I replace the 'bool' return type of 'finished' with int
and CInt on the C++ and Haskell side, respectively, and test for (/=
0).

On Mon, May 2, 2011 at 9:35 PM, Huynh Huu Long
long.upc...@googlemail.com wrote:
 Hi,
 I tried to access C++ iterators from Haskell via the FFI. There are two
 example programs attached, in Haskell as well as in C++. Both utilise the
 same iterator wrapper example.cpp, but when running the Haskell program it
 prints an empty list, where it should print the contents of some vector. The
 C++ program meanwhile runs fine though. What does not seem to work is this
 part:
 bool
 finished (std::vectorint::iterator *begin, std::vectorint::iterator
 *end)
 {
     return *begin == *end;
 }
 In Haskellland, finished :: Ptr Iterator - Ptr Iterator - IO Bool always
 returns IO True. Is this a bug lurking in my program or is it a GHC issue?
 Long
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe





-- 
Work is punishment for failing to procrastinate effectively.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Interfacing C++ iterators does not work as expected

2011-05-02 Thread Huynh Huu Long
Big thanks, that works for me as well. Doesn't GHC check whether a
type is allowed (like CInt,...) or not (like Bool,...)?

2011/5/2 Gábor Lehel illiss...@gmail.com:
 It works if I replace the 'bool' return type of 'finished' with int
 and CInt on the C++ and Haskell side, respectively, and test for (/=
 0).

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Interfacing C++ iterators does not work as expected

2011-05-02 Thread Albert Y. C. Lai

On 11-05-02 05:31 PM, Huynh Huu Long wrote:

Big thanks, that works for me as well. Doesn't GHC check whether a
type is allowed (like CInt,...) or not (like Bool,...)?


No, it doesn't, since version 6.10.1:
http://www.haskell.org/ghc/docs/6.10.1/html/users_guide/ffi-ghc.html#glasgow-foreign-headers

As it happens, FFI requires translating Haskell Bool from C int 
(unlikely to be 1 byte), while GCC translates C++ bool to 1 byte. At a 
suitable moon phase and planet alignment (or data alignment), mixing up 
an int with a byte will be funny.


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe