>Standard DISs

10. Standard DISs

The following table gives the DIS functions that Green Card provides as a ``standard prelude''.

DIS           Haskell type  C type         Comments

int x         Int           int
char c        Char          char
float f       Float         float
double d      Double        double
bool b        Bool          int             0 for False, 1 for True
addr a        Addr          (void *)        An (immovable) external address

string s      String        (char *)        Persistence not required
                                            in either direction.

foreign x f   ForeignObj    (void *x),
                            ((void (*)())f) f is the free routine; it
                                            takes one parameter, namely
                                            x, the thing to be freed.

stable x      any           unsigned int    Makes it possible to pass a
                                            Haskell pointer to C, and
                                            perhaps get it back later,
                                            without breaking the
                                            garbage collector.

maybe dis     Maybe dis     type of dis     Converts to and from
                                            Maybe's, with 0 as 'Nothing'

maybeT he d   Maybe dis     type of dis     Converts to and from 'Maybe's

10.1. Haskell extensions

Several of the standard DISs involve types that go beyond standard Haskell:

10.2. Maybe

Almost all DISs work on single-constructor data types. It is much less obvious how to translate values of multi-constructor data types to and from C. Nevertheless, Green Card does deal in an ad hoc fashion with the Maybe type, because it seems so important. The syntax for the maybeT DIS is:

maybeT hexp dis

where dis is any DIS, and hexp is a Haskell expression which you want to map to the Nothing value.

In the following example, the function foo takes an argument of type Maybe Int. If the argument value is Nothing it will bind x to 0; if it is Just a it will bind x to the value of a. The return value will be Just r unless r == -1 in which case it will be Nothing.

%fun foo :: Maybe Int -> Maybe Int
%call (maybeT { 0 } (int x))
%code r = foo(x);
%result (maybeT { -1 } (int r))

There is also a maybe DIS wich just takes the DIS and defaults to 0 as the Nothing value.