How do you represent a C pointer type?

George Russell ger at
Fri Jun 7 09:30:39 EDT 2002

I have a small C interface with a C type DB.  DB handles are only administed on the C side; at the
Haskell end they are a black box.  The Haskell side never sees DB values, just values of type
DB * (pointers to DB).  I'm wondering how to represent this.

One solution would be just to represent the equivalent of DB * as
   type DBP = Addr
(or Ptr () or something) but then of course it might be confused with any other Addr.
A rather more ingenious solution would be something like
   newtype DBPInternal = DBPInternal Int
   type DBP = Ptr (Ptr DBPInternal)
I think this might actually work in that a DBP would be marshallable and couldn't be
confused with any other type not involving DBPInternal.  Elegant it isn't, since it
involves a pretend pointer inside the Ptr just to get the types working.

I think the most elegant solution would be if I could use something like
   newtype DBP = DBP Addr deriving (-- some class --)
and have it work automatically, but I don't know that I can.

So tell me, what is the correct solution?

George Russell

More information about the FFI mailing list