On Thu, 2009-08-20 at 19:59 +0200, Maciej Piechotka wrote:
> Sorry for delay in responding.
> 
> > > Any from GHC.Prim makes unsafeCoerce much useful and safe. Can it be
> > > included in Unsafe.Coerce module?
> > > 
> > > Pros:
> > > - unsafeCoerce is much more useful with Any (it's a safe placeholder for
> > > any value and therefore can be passed simply in/out FFI).
> > 
> > Yes it's a safe placeholder, but so is an existential, I believe...
> > data ContainsAny = forall a. ContainsAny a
> > to put in the container:
> > "ContainsAny x"
> > to remove from the container:
> > "case ... of ContainsAny x -> unsafeCoerce x"
> > Albeit, there might be a slight performance penalty for the extra box, 
> > which can't be removed in all haskell implementations.
> > 
> > Also, what do you mean about "FFI"?  I don't think you can pass the 
> > "Any" type to foreign functions through the FFI...
> > 
> > Also, can/do all compilers that implement unsafeCoerce implement a safe Any?
> > Hugs can do it with just "data Any = Ignored" I believe, not sure about 
> > nhc, yhc or jhc...
> > 
> > -Isaac
> 
> Well. May be I have one specific problem which seems to not be possible
> to be implemented in portable way (I'm not sure if it is possible
> outside ghc). Sorry for mentioning FFI without further explanation.
> 
> The problem arise if one would like to implement GClosure from glib.
> Once it was implemented in gtk2hs in C using RTS API. I also have my own
> implementation in Haskell which is doing something (removed IO monad for
> clarity):
> applyValue :: Any -> Value -> Any
> applyValue f v =
>       case funsamentalTypeOf v of
>               Type1 -> (unsafeCoerce f :: HaskellType1 -> Any) $ get v
>               ...
> 
> Such trick is (looks for me?) safe as a -> b -> ... -> d can be
> represented by Any. However I cannot safely cast to function a -> Any.
> 
> To/from FFI it is passed in Ptr (StablePtr Any).
> 
> Regards
> PS. I assume that it is not possible as it was done in importable was in
> gtk2hs. 

With any known from the beginning number of parameters function and GADT
one can write:

data Closure where
        Closure0 :: IO a
        Closure1 :: a -> IO b
        Closure2 :: a -> b -> IO c
        -- ...

However it poses upper build-time limit.

Regards

Attachment: signature.asc
Description: This is a digitally signed message part

_______________________________________________
Haskell-prime mailing list
Haskell-prime@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-prime

Reply via email to