Go to the first, previous, next, last section, table of contents.

Using function headers

When generating C (using the `-fvia-C' directive), one can assist the C compiler in detecting type errors by using the `-#include' directive to provide `.h' files containing function headers.

For example,

typedef unsigned long *StgForeignObj;
typedef long StgInt;

void          initialiseEFS (StgInt size);
StgInt        terminateEFS (void);
StgForeignObj emptyEFS(void);
StgForeignObj updateEFS (StgForeignObj a, StgInt i, StgInt x);
StgInt        lookupEFS (StgForeignObj a, StgInt i);

You can find appropriate definitions for `StgInt', `StgForeignObj', etc using `gcc' on your architecture by consulting `ghc/includes/StgTypes.lh'. The following table summarises the relationship between Haskell types and C types.

C type name               Haskell Type                  
------------------------------------------------------
`StgChar'        `Char#'              
`StgInt'         `Int#'               
`StgWord'        `Word#'              
`StgAddr'        `Addr#'              
`StgFloat'       `Float#'             
`StgDouble'      `Double#'            
`StgArray'       `Array#'             
`StgByteArray'   `ByteArray#'         
`StgArray'       `MutableArray#'      
`StgByteArray'   `MutableByteArray#'  
`StgStablePtr'   `StablePtr#'         
`StgForeignObj'  `ForeignObj#'        

Note that this approach is only essential for returning `float's (or if `sizeof(int) != sizeof(int *)' on your architecture) but is a Good Thing for anyone who cares about writing solid code. You're crazy not to do it.


Go to the first, previous, next, last section, table of contents.