2 {-# LANGUAGE ExistentialQuantification #-}
8 import Foreign.Marshal.Alloc
9 import Foreign.Storable
10 import Foreign.C.Types
14 class RefObject a where
19 data RefObj = RefObj IntPtr [IntPtr] deriving Show
21 instance RefObject RefObj where
22 mem (RefObj mem _ ) = mem
23 refs (RefObj _ refs) = refs
25 data Succ = forall a. (RefObject a) => Succ (a -> [a])
27 obj2 = do buffer <- mallocBytes 4
28 pokeByteOff buffer 0 (0::Int32)
31 obj3 = do buffer <- mallocBytes 4
32 pokeByteOff buffer 0 (0::Int32)
35 obj1 f g = do buffer <- mallocBytes 12
36 pokeByteOff buffer 0 (2::Int32)
37 pokeByteOff buffer 4 f
38 pokeByteOff buffer 8 g
41 ptrToRefObj ptr = do objCount <- peek ptr :: IO Int32
42 let objsBase = ptr `plusPtr` 4
43 objs <- mapM ((liftM ptrToIntPtr) . peekElemOff objsBase . fromIntegral) [0..objCount-1]
44 return $ RefObj (ptrToIntPtr ptr) objs
48 (print . ptrToIntPtr) f
49 (print . ptrToIntPtr) g
50 ptrToRefObj =<< obj1 f g
52 traverse :: (RefObject a) => (IntPtr -> IO a) -> a -> [a] -> IO [a]
53 traverse dereference x ws = do children <- mapM dereference (refs x)
56 succMem :: Ptr a -> Succ
57 succMem =undefined-- Succ (\_ -> obj1