Re: Proposal - Foreign enum support

2014-04-17 Thread Roman Cheplyaka
I am reluctant about adding a new syntactic feature for such a niche problem.

Can't this be achieved with a quaiquoter?

Roman


signature.asc
Description: Digital signature
___
Haskell-prime mailing list
Haskell-prime@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-prime


Re: Proposal - Foreign enum support

2014-04-19 Thread Edward Kmett
-1 from me.

Your first example even provides a counter-example.

typedef enum {
> IMG_INIT_JPG = 0x0001,
> IMG_INIT_PNG = 0x0002,
> IMG_INIT_TIF = 0x0004,
> IMG_INIT_WEBP = 0x0008
> } IMG_InitFlags;


Those are defined as powers of two because they are a bit mask you have to
be able to (.|.) together.

This is the sort of thing people write .hsc files for, so they can include
the appropriate header directly and resolve the constants.

Maintaining a separate copy of an enum that goes out of date with the C
version is a recipe for breaking on future versions of the dependency, and
in my experience the majority of cases where the range is discontinuous
arise from when the thing in question is a mask, like this very case.

The remaining cases where you really want to incur all those obligations
are few enough and far enough between that going through a quasiquoter
seems to be the right solution.

-Edward


On Thu, Apr 17, 2014 at 10:19 AM, Merijn Verstraaten  wrote:

> Cross-post to haskell-prime in case there's any interest for including
> this into the report's FFI specification.
>
> Proposal - Foreign enum support
> ===
>
> At the moment the FFI does not have a convenient way with interacting enums
> (whether proper enums or CPP defines) in C (like languages). Both enums
> and CPP
> defined enums are major parts of large C APIs and they are thus crucial to
> writing foreign bindings. A few examples:
>
> SDL_image defines the following enum:
>
> typedef enum {
> IMG_INIT_JPG = 0x0001,
> IMG_INIT_PNG = 0x0002,
> IMG_INIT_TIF = 0x0004,
> IMG_INIT_WEBP = 0x0008
> } IMG_InitFlags;
>
> OpenCL specifies the following typedefs + CPP defined enum:
>
> typedef uint32_t  cl_uint __attribute__((aligned(4)));
> typedef cl_uint   cl_platform_info;
>
> /* cl_platform_info */
> #define CL_PLATFORM_PROFILE 0x0900
> #define CL_PLATFORM_VERSION 0x0901
> #define CL_PLATFORM_NAME0x0902
> #define CL_PLATFORM_VENDOR  0x0903
> #define CL_PLATFORM_EXTENSIONS  0x0904
>
> OpenCL functions will return the above CPP defines as return values of type
> cl_platform_info.
>
> Current Solutions
> -
>
> In many cases someone wrapping such a C library would like to expose these
> enums as a simple sum type as this has several benefits: type safety, the
> ability to use haskell constructors for pattern matching, exhaustiveness
> checks.
>
> Currently the GHC FFI, as specified by Haskell2010, only marshalls a small
> set
> of foreign types and newtypes with exposed constructors of these types. As
> such
> there seem two approaches to wrap these enums:
>
>  1. Implement an ADT representing the enum and write a manual conversion
> function between the ADT and the corresponding C type (e.g. CInt ->
> Foo and
> Foo -> CInt).
>
>  2. Use a tool like c2hs to automatically generate the ADT and conversion
> function.
>
> In both cases the foreign functions are imported using the corresponding C
> type
> in their signature (reducing type safety) and the user is forced write
> trivial
> wrappers for every imported function to convert the ADT to the relevant C
> type
> and back.
>
> This is both tedious to write and costly in terms of code produced, in
> case of
> c2hs one calls "toEnum . fromIntegral" and "fromIntegral . fromEnum" for
> every
> argument/result even though this could trivially be a no-op.
>
> Worse, since c2hs uses the Enum class for it's conversion to/from C types
> it
> generates Enum instances like:
>
> instance Enum Foo where
> fromEnum Bar = 1
> fromEnum Baz = 1337
>
> toEnum 1 = Bar
> toEnum 1337 = Baz
> toEnum unmatched = error ("PlatformInfo.toEnum: Cannot match " ++
> show unmatched)
>
> Since succ/pred and enumFromTo's default implementations assume enums
> convert
> to continuous sequence of Int this means the default generated enum
> instances
> crash. This problem could be overcome by making c2hs' code generation
> smarter,
> but this does not eliminate the tediousness of wrapping all foreign
> imported
> functions with marshalling wrappers, NOR does it eliminate the overhead of
> all
> this useless marshalling.
>
> Proposal
> 
>
> Add a new foreign construct for enums, the syntax I propose below is rather
> ugly and ambiguous and thereforeopen to bikeshedding, but I prefer
> explaining
> based on a concrete example.
>
> foreign enum CInt as Foo where
> Bar = 1
> Baz
> Quux = 1337
> Xyzzy = _
>
> This would introduce a new type 'Foo' with semantics approximately
> equivalent
> too "newtype Foo = Foo CInt" plus the pattern synonyms "pattern Bar = Foo
> 1;
> pattern Baz = 2; pattern Quux = 1337; pattern Xyzzy = Foo _".
>
>