Standarize GHC.Prim.Any

I'm so sorry if I've done it wrong - it's my first contact with Haskell standardization and I base on http://hackage.haskell.org/trac/haskell-prime/wiki/Process . I hope it is not too late as well. 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). Cons: - unsafeCoerce is useful, but not very nice, platorm-dependent low-level anyway. Best regards

Maciej Piechotka wrote:
I'm so sorry if I've done it wrong - it's my first contact with Haskell standardization and I base on http://hackage.haskell.org/trac/haskell-prime/wiki/Process . I hope it is not too late as well.
Do not worry about that, we have decided to have a yearly standardization process.
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

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...
nhc98 and yhc do not implement the Any type. unsafeCoerce is easily implemented without it. Regards, Malcolm

On Mon, Aug 17, 2009 at 07:52:26PM -0400, Isaac Dupree wrote:
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...
Jhc has existentials, so that is the best way to handle this. Jhc existentials do require an invisible type parameter to be added to the representation so wouldn't be able to remove the extra box in general. Jhc implements unsafeCoerce, but it is really unsafe as the type information plays a major role in the data layout as jhc has no 'universal' run time representation of data. For instance, you might unsafeCoerce something that was statically determined to not need to be traversed by the garbage collector to something that the garbage collector follows, surely resulting in a segfault or other undefined behavior at run-time. This is the reason existentials always need to carry around their type information, if nothing else, it potentially guides the garbage collector. So, existentials really are the only safe way to do this in jhc. Really the only guarenteed safe use of unsafeCoerce in jhc is between a newtype and its underlying representation. John -- John Meacham - ⑆repetae.net⑆john⑈ - http://notanumber.net/

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.

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

Maciej Piechotka wrote:
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 -- ...
That isn't GADT syntax... results of "Constructor :: ..." must be "Closure" in this case. What are you trying to do here? An alternative for "Ptr (StablePtr Any)"? Is "Ptr (StablePtr Dynamic)" sufficient, where Dynamic from Data.Dynamic is represented in an implementation-specific way equivalent to data Dynamic = forall a. Typeable a => DynamicConstr a a.k.a. data Dynamic where DynamicConstr :: Typeable a => a -> Dynamic ? The main limitation would be if you want to include a type that's not in Typeable. Is that common, to have types that aren't in Typeable? It would be more annoying but you could add extra concrete-type constructors to a data, for every possibility, like DynamicConstr3 :: (Int, Bool) -> MyDynamic if for some reason (Int, Bool) weren't in Typeable... Would be silly to do, however. Just put things in Typeable? Or, if you know it'll be type-correct, just use ContainsAny as above, with "Ptr (StablePtr ContainsAny)"? It might not be absolutely the most optimized, but portable standard mechanisms were never the place for that kind of hacking. And it'll be good enough. Right? -Isaac

On Sat, 2009-09-05 at 23:35 -0400, Isaac Dupree wrote:
Maciej Piechotka wrote:
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 -- ...
That isn't GADT syntax... results of "Constructor :: ..." must be "Closure" in this case.
Sorry. To much delete: data Closure where Closure0 :: IO a -> Closure Closure1 :: (a -> IO b) -> Closure Closure2 :: (a -> b -> IO c) -> Closure ...
What are you trying to do here? An alternative for "Ptr (StablePtr Any)"? Is "Ptr (StablePtr Dynamic)" sufficient, where Dynamic from Data.Dynamic is represented in an implementation-specific way equivalent to data Dynamic = forall a. Typeable a => DynamicConstr a a.k.a. data Dynamic where DynamicConstr :: Typeable a => a -> Dynamic ? The main limitation would be if you want to include a type that's not in Typeable. Is that common, to have types that aren't in Typeable?
Point taken. Regards
participants (4)
-
Isaac Dupree
-
John Meacham
-
Maciej Piechotka
-
Malcolm Wallace