Pointer equality for nullary constructors

Can I use reallyUnsafePtrEquality# reliably to identify whether a value is a nullary constructor of a particular type? For example, if I have data Foo = Foo Can I write isFoo :: a -> Bool isFoo !a = isTrue# (reallyUnsafePtrEquality# a Foo) instead of isFoo :: forall a. Typeable a => a -> Bool isFoo a | Just Refl <- eqTypeRep (typeRep @a) (typeRep @Foo) , Foo <- a = True | otherwise = False The reason I'm asking is because this would let me (potentially) raiseIO# a nullary constructor and then catch# it and see if it was what I was looking for rather than having to open a SomeException to get to an Exception dictionary, open that to get a TypeRep, and then peer inside that to check a Fingerprint. That is, I'd get lighter-weight exceptions that only carry the information I actually need. Thanks, David

Keep in mind a newtype of Foo will have a different TypeRep, but will
compare as equal under reallyUnsafePtrEquality#.
-Edward
On Sun, Feb 11, 2018 at 5:14 AM, David Feuer
Can I use reallyUnsafePtrEquality# reliably to identify whether a value is a nullary constructor of a particular type? For example, if I have
data Foo = Foo
Can I write
isFoo :: a -> Bool isFoo !a = isTrue# (reallyUnsafePtrEquality# a Foo)
instead of
isFoo :: forall a. Typeable a => a -> Bool isFoo a | Just Refl <- eqTypeRep (typeRep @a) (typeRep @Foo) , Foo <- a = True | otherwise = False
The reason I'm asking is because this would let me (potentially) raiseIO# a nullary constructor and then catch# it and see if it was what I was looking for rather than having to open a SomeException to get to an Exception dictionary, open that to get a TypeRep, and then peer inside that to check a Fingerprint. That is, I'd get lighter-weight exceptions that only carry the information I actually need.
Thanks, David
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.

Sure, but that's okay, because I never need to expose the type to anyone.
I'm just nervous because I haven't seen documentation saying those values
are type-specific.
On Feb 11, 2018 10:37 PM, "Edward Kmett"
Keep in mind a newtype of Foo will have a different TypeRep, but will compare as equal under reallyUnsafePtrEquality#.
-Edward
On Sun, Feb 11, 2018 at 5:14 AM, David Feuer
wrote: Can I use reallyUnsafePtrEquality# reliably to identify whether a value is a nullary constructor of a particular type? For example, if I have
data Foo = Foo
Can I write
isFoo :: a -> Bool isFoo !a = isTrue# (reallyUnsafePtrEquality# a Foo)
instead of
isFoo :: forall a. Typeable a => a -> Bool isFoo a | Just Refl <- eqTypeRep (typeRep @a) (typeRep @Foo) , Foo <- a = True | otherwise = False
The reason I'm asking is because this would let me (potentially) raiseIO# a nullary constructor and then catch# it and see if it was what I was looking for rather than having to open a SomeException to get to an Exception dictionary, open that to get a TypeRep, and then peer inside that to check a Fingerprint. That is, I'd get lighter-weight exceptions that only carry the information I actually need.
Thanks, David
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.

Hi David, Am Sonntag, den 11.02.2018, 05:14 -0500 schrieb David Feuer:
Can I use reallyUnsafePtrEquality# reliably to identify whether a value is a nullary constructor of a particular type?
after reading this I cam up with this idea: https://ghc.haskell.org/trac/ghc/ticket/14826 You might have an idea of whether this would be useful in practice. Cheers, Joachim -- Joachim “nomeata” Breitner mail@joachim-breitner.de https://www.joachim-breitner.de/

On Sun, Feb 11, 2018 at 5:14 AM, David Feuer
Can I use reallyUnsafePtrEquality# reliably to identify whether a value is a nullary constructor of a particular type? For example, if I have
data Foo = Foo
Can I write
isFoo :: a -> Bool isFoo !a = isTrue# (reallyUnsafePtrEquality# a Foo)
You mean like this? https://github.com/gregorycollins/hashtables/blob/master/src/Data/HashTable/... My experience is that this works, except when doing coverage, where the compiler adds instrumentation code that breaks the technique. I #ifdef'd my way around the problem, swapping out a slower impl when I was doing code coverage. Greg

Hi, Am Dienstag, den 20.02.2018, 17:56 -0500 schrieb Gregory Collins:
You mean like this? https://github.com/gregorycollins/hashtables/blob/master/src/Data/HashTable/...
My experience is that this works, except when doing coverage, where the compiler adds instrumentation code that breaks the technique. I #ifdef'd my way around the problem, swapping out a slower impl when I was doing code coverage.
it seems that in this case, https://ghc.haskell.org/trac/ghc/ticket/14826 would very much apply, wouldn’t it? Cheers, Joachim -- Joachim Breitner mail@joachim-breitner.de http://www.joachim-breitner.de/

That's a really interesting idea, thanks for linking it.
On Tue, Feb 20, 2018 at 9:43 PM, Joachim Breitner
Hi,
Am Dienstag, den 20.02.2018, 17:56 -0500 schrieb Gregory Collins:
You mean like this? https://github.com/gregorycollins/hashtables/ blob/master/src/Data/HashTable/Internal/UnsafeTricks.hs#L72
My experience is that this works, except when doing coverage, where the compiler adds instrumentation code that breaks the technique. I #ifdef'd my way around the problem, swapping out a slower impl when I was doing code coverage.
it seems that in this case, https://ghc.haskell.org/trac/ghc/ticket/14826 would very much apply, wouldn’t it?
Cheers, Joachim
-- Joachim Breitner mail@joachim-breitner.de http://www.joachim-breitner.de/
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
--
Gregory Collins
participants (4)
-
David Feuer
-
Edward Kmett
-
Gregory Collins
-
Joachim Breitner