4 Mar 2013 08:43
new-typeable, new cast?
Shachaf Ben-Kiki <shachaf <at> gmail.com>
2013-03-04 07:43:35 GMT
2013-03-04 07:43:35 GMT
This came up in #haskell -- rather than just provide coerce, cast (or
a primitive that cast can be built on, as well as other things) can
give a type equality witness of some kind. Some possible signatures
are:
cast :: (Typeable a, Typeable b) => Maybe (p a -> p b) --
Leibniz-style equality
cast :: (Typeable a, Typeable b) => p a -> Maybe (p b) -- another form
cast :: (Typeable a, Typeable b) => Maybe (Is a b) -- GADT-style equality
cast :: (Typeable a, Typeable b) => MightBe a b -- another form
-- With
data Is a b where { Refl :: Is a a } -- standard equality GADT
data MightBe a b where -- version with built-in Maybe, for more
convenient types
Is :: MightBe a a
Isn't :: MightBe a b
Any of these lets you write any of the other ones, of course. But
"cast" doesn't let you write any of them, because it only goes in one
direction.
The GADT form can let you write code like
foo :: forall a. Typeable a => a -> ...
foo x
| Is <- cast :: MightBe a Int = ...x...
| Is <- cast :: MightBe a Char = ...x...
| otherwise = ...
Without coming up with a new name for x. It's possible to provide
(Continue reading)
RSS Feed