Pointer equality for nullary constructors

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Pointer equality for nullary constructors

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.
Reply | Threaded
Open this post in threaded view
|

Re: Pointer equality for nullary constructors

Edward Kmett-2
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 <[hidden email]> 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.


_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|

Re: Pointer equality for nullary constructors

David Feuer
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" <[hidden email]> wrote:
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 <[hidden email]> 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.


_______________________________________________
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.