Additonal types for Foreign.C.Types

Hi,
After reading an ISO draft for standard C, I found
a few types that could be usefull when binding to
libraries (these are from

I think you can use Data.Word and Data.Int types for this, that is. Data.Word.Word16 == uint16_t, Data.Word.Word32 == uint32_t, etc. Data.Int.Int16 = int16_t, Data.Int.Int32 = int32_t, etc. There are Foreign.Storable.Storable instances for those. -Ross On Feb 10, 2009, at 6:32 AM, Maurí cio wrote:
Hi,
After reading an ISO draft for standard C, I found a few types that could be usefull when binding to libraries (these are from
): int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t
What about if they were included in the next version of GHC Foreign.C.Types module? For instance, as:
CInt8, CUInt8, CInt16 etc.
Some libraries (e.g. sqlite) define function parameters that are supposed to always have a given size (e.g., sqlite defines sqlite3_int64). In order to have a portable binding to those libraries, it would be nice to have types in Haskell that also offer that guarantee.
There are also a few other nice types, although I'm not sure they belong to standard modules. If they did, however, they could make life easier for those people writing higher level Haskell modules after standard C functions:
complex, float complex, double complex (from
) are used in functions like ccos, csin, cexp, csqrt etc. struct lconv (from
) struct tm (from
) From
and : mbstate_t, wint_t, wctrans_t, wctype_t. Do you think I could open a ticket for GHC proposing that?
Best, Maurício
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Yes, I can. Thanks. Just forget my idea, with this I can provide all those types in a library. I'm confused. When is it possible to use a type as a parameter to a foreign function call? My first guess was that I had to provide an instance for class Storable, but after I tried writing a complex-like type that way GHC told me my type was unaceptable. So I thought only types allowed by the compiler (including forall a. Ptr a) could be used that way. What is the rule? I've read all of FFI report and found nothing. Did I miss something? How can I make a type of mine acceptable? Why are Data.Int acceptable, and how could I know that? Thanks, Maurício
I think you can use Data.Word and Data.Int types for this, that is. (...)
After reading an ISO draft for standard C, I found a few types that could be usefull when binding to libraries (these are from
): int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t
(...)

The FFI spec says (at http://www.cse.unsw.edu.au/~chak/haskell/ffi/ffi/ffise3.html#x6-120003.2) : The argument types at[i] produced by fatype must be marshallable foreign types; that is, each ati is either (1) a basic foreign type or (2) a type synonym or renamed datatype of a marshallable foreign type. Moreover, the result type rt produced by frtype must be a marshallable foreign result type; that is, it is either a marshallable foreign type, the type (), or a type matching Prelude.IO t, where t is a marshallable foreign type or (). Earlier it defines the basic foreign types: The following types constitute the set of basic foreign types: * Char, Int, Double, Float, and Bool as exported by the Haskell 98 Prelude as well as * Int8, Int16, Int32, Int64, Word8, Word16, Word32, Word64, Ptr a, FunPtr a, and StablePtr a, for any type a, as exported by Foreign (Section 5.1). So, that list of types, or any type synonym. -Ross On Feb 10, 2009, at 3:56 PM, Maurí cio wrote:
Yes, I can. Thanks. Just forget my idea, with this I can provide all those types in a library.
I'm confused. When is it possible to use a type as a parameter to a foreign function call? My first guess was that I had to provide an instance for class Storable, but after I tried writing a complex-like type that way GHC told me my type was unaceptable. So I thought only types allowed by the compiler (including forall a. Ptr a) could be used that way.
What is the rule? I've read all of FFI report and found nothing. Did I miss something? How can I make a type of mine acceptable? Why are Data.Int acceptable, and how could I know that?
Thanks, Maurício
I think you can use Data.Word and Data.Int types for this, that is. (...)
After reading an ISO draft for standard C, I found a few types that could be usefull when binding to libraries (these are from
): int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t
(...)
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

The FFI spec says (at http://www.cse.unsw.edu.au/~chak/haskell/ffi/ffi/ffise3.html#x6-120003.2):
There I see: --- Foreign types are produced according to the following grammar: ftype --> frtype | fatype -> ftype frtype --> fatype | () fatype --> qtycon atype[1] ... atype[k] (k > 0) --- I can't understand the "qtycon atype[1]..." line. I did search haskell 98 report syntax reference, and saw how qtycon and tycon are defined, but I could not understand how they are used here. Thanks for your help, Maurício

fatype is the function argument type. atype[i] are type arguments. qtycon is a qualified (e.g. possibly with module prefix) type constructor, e.g. Just So, for example if you have: foreign import ccall "string.h strlen" cstrlen :: Ptr CChar -> IO CSize fatype -> ftype :: ftype fatype :: fatype qtycon "Ptr" atype1 "CChar" fatype :: frtype qtycon "IO" atype1 "CSize" (I struggled a bit with finding a good way to communicate the productions chosen, so bear with me) Make sense? -Ross On Feb 10, 2009, at 6:13 PM, Maurí cio wrote:
The FFI spec says (at http://www.cse.unsw.edu.au/~chak/haskell/ffi/ffi/ffise3.html#x6-120003.2) :
There I see:
--- Foreign types are produced according to the following grammar:
ftype --> frtype | fatype -> ftype frtype --> fatype | () fatype --> qtycon atype[1] ... atype[k] (k > 0) ---
I can't understand the "qtycon atype[1]..." line. I did search haskell 98 report syntax reference, and saw how qtycon and tycon are defined, but I could not understand how they are used here.
Thanks for your help, Maurício
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Sure it does! Thanks.
(...)So, for example if you have:
foreign import ccall "string.h strlen" cstrlen :: Ptr CChar -> IO CSize
fatype -> ftype :: ftype fatype :: fatype qtycon "Ptr" atype1 "CChar" fatype :: frtype qtycon "IO" atype1 "CSize"
(I struggled a bit with finding a good way to communicate the productions chosen, so bear with me)
Make sense?
The FFI spec says (at http://www.cse.unsw.edu.au/~chak/haskell/ffi/ffi/ffise3.html#x6-120003.2):
There I see:
--- (...) I can't understand the "qtycon atype[1]..." line. (...)

Hello Maurнcio, Tuesday, February 10, 2009, 11:56:40 PM, you wrote: 1. haskell compiler should check that your haskell imports match C function prototypes. ghc does it in -fvia-C mode if you declare .h file where the function prototype may be found: foreign import ccall unsafe "Environment.h UpdateCRC" c_UpdateCRC :: Ptr CChar -> CUInt -> CUInt -> IO CUInt but who now uses -fvia-C mode? 2. there is correspondence between C and Haskell types. haskell equivalents for C tpes are defined in FFI addendum. C equivalents for GHC haskell types are defined in ghc .h files, these are things like HsInt and so on if you use, for example, Int type on haskell side and int on C side, they may turn out to be the same or not. if you don't use -fvia-C and .h file, you will not get even a warning - program will just stop working i think that we may propose changing FFI addendum so that haskell compilers guarantees correspondence between Int16 and int16_t and so on disclaimer: i don't used ghc versions after 6.6, so things may be worse or better now :)
Yes, I can. Thanks. Just forget my idea, with this I can provide all those types in a library.
I'm confused. When is it possible to use a type as a parameter to a foreign function call? My first guess was that I had to provide an instance for class Storable, but after I tried writing a complex-like type that way GHC told me my type was unaceptable. So I thought only types allowed by the compiler (including forall a. Ptr a) could be used that way.
What is the rule? I've read all of FFI report and found nothing. Did I miss something? How can I make a type of mine acceptable? Why are Data.Int acceptable, and how could I know that?
Thanks, Maurнcio
I think you can use Data.Word and Data.Int types for this, that is. (...)
After reading an ISO draft for standard C, I found a few types that could be usefull when binding to libraries (these are from
): int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t
(...)
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com
participants (3)
-
Bulat Ziganshin
-
Maurício
-
Ross Mellgren