[Haskell-cafe] Read instance for constructors?
haskell at nand.wakku.to
Wed Mar 12 00:10:19 UTC 2014
On Tue, 11 Mar 2014 19:41:57 +0000, Semen Trygubenko / Семен Тригубенко <semen at trygub.com> wrote:
> On Tue, Mar 11, 2014 at 01:21:01PM +0100, Niklas Haas wrote:
> > The idiomatic way to handle this kind of stuff normally is to pass an
> > abstract proxy that carries the type as a type argument, rather than
> > passing a value of that type itself, eg.:
> > > data Proxy a = Proxy
> > >
> > > dataTypeOf :: Data a => Proxy a -> DataTypeOf
> > or even a more polymorphic version:
> > > dataTypeOf :: Data a => f a -> DataTypeOf
> Thank you for your reply — I've read it many times, but couldn't translate it into code
> as of yet.
> I don't mind ugly as long as it's safe.
> Specifically, how am I to construct an entity of type DataType (see Data.Data; DataType needs to be fed
> into readConstr) with the help of a wrapper function you are describing?
> If someone could in the direction of a skeleton for
> such a function I might be able to fill in the gaps on my own.
> But currently I'm stuck… :/
> Thanks again,
Oops, sorry, that was supposed to have been:
> dataTypeOf :: Data a => Proxy a -> DataType
> dataTypeOf :: Data a => p a -> DataType
As for the implementation, the simplest possible (ie. no language
extensions) implementation I can think of looks like this:
> dataTypeOf = Data.dataTypeOf . f
> where f :: p a -> a
> f _ = error "dataTypeOf: this should never be used"
where Data.dataTypeOf refers to the ‘original’ version of that function.
More information about the Haskell-Cafe