[Hs-Generics] Request for code review: Generics for tuple/newtype introspection

Nicolas Frisby nicolas.frisby at gmail.com
Fri Dec 20 19:25:02 UTC 2013


For the "minor point", you'd like for example the following equivalence
(modulo decomposing the Attr type)?

  toAttrs (FooD 7,BarD True,BazD 'X') == (Attr FooT Int,Attr BarT Bool,Attr
BazT Char)

If I understood correctly, take a look at the most recent edit of
http://lpaste.net/97165 The code gets hairier, but you can now say

> example = let (a,_,_) = toAttrs (FooD 7,BarD True,BazD 'X') in a



If you do not specify the "container type" of the result (the pattern
suffices in my example), there will be a type-error. Loosening that
requirement would require a mapping that includes

  f a b goes to f Attr Attr   where for example f = (,)
  f a b c goes to f Attr Attr Attr   where for example f = (,,)
  f a b c d goes to f Attr Attr Attr Attr   where for example f = (,,,)
  and so on

But I  don't know how to specify such a general one, since all (instances
of) tuple types are distinct types that are not formally related to one
another within the GHC type system.



So:

1) Have I understood your specification correctly?

2) Other experts: is there safe trickery to generally specify this map? I
think it might actually be doable in 7.4, but as far as I know, that was
accidental generally-unsound functionality that was removed in 7.6+.

HTH also.


On Fri, Dec 20, 2013 at 1:44 AM, AntC <anthony_clayden at clear.net.nz> wrote:

> > Nicolas Frisby <nicolas.frisby <at> gmail.com> writes:
> >
> > Disclaimer: I didn't read all of the previous emails.
> > Does this do what you need?
> >
> > I'm on GHC 7.4 at the moment, but I think this will work with later
> versions.
> >
> > HTH.
> >
>
> Thank you Nicolas (and yes it works on GHC 7.6).
>
> It does look 'cleaner' than the version I posted. But I still see plenty of
> pileups of (type and data) constructors.
>
> However, perhaps it looks 'cleaner' because it doesn't work in the corner
> cases you could have seen discussed in the previous emails?
>
> * There's no instance for the unit tuple `toAttrs ()`
>   -- that's easily fixed:
>
>     instance ToAttrsR U1   where
>       toAttrsR _ = []
>
> * It crashes '*** Exception: Prelude.undefined'
>   when toAttrs' argument is undefined.
>   I tried a quick fix to the (:*:) instance, similar to my version,
>   but that wasn't enough.
>
> * (And a minor point: I wanted to be able to go feed toAttrs' output
>    tuple into itself.
>    -- that's fixable easily enough.
>   My version for a bonus also grabbed the newtype's data constructor.
>    -- that's also fixable, I think.)
>
> AntC
>
>
>
>
> _______________________________________________
> Generics mailing list
> Generics at haskell.org
> http://www.haskell.org/mailman/listinfo/generics
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/generics/attachments/20131220/735741dd/attachment.html>


More information about the Generics mailing list