Hi all,<br><div class="gmail_quote"><div> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div class="im">
> Concerning "2. combination with overlapping instances", you say "The<br>
> solution has been described already in the HList paper: provide<br>
> something the typeOf at the type level. That is, assume a type<br>
> function "TypeOf :: * -> TYPEREP".<br>
><br>
</div>> ...<br>
<div class="im">><br>
> Incidentally Pedro's new "deriving Generic" stuff does derive a kind of type-level type representation for types, I think. It's more or less as described in their paper.<br>
> <a href="http://www.dreixel.net/research/pdf/gdmh_nocolor.pdf" target="_blank">http://www.dreixel.net/research/pdf/gdmh_nocolor.pdf</a><br>
<br>
</div>I'm very excited about this new Representable class and have just<br>
started playing with it, so it is too early for me to say for sure.<br>
However, my initial impression is that this is going to make me want<br>
OverlappingInstances even more because of all the cool things you can<br>
do with recursive algorithms over the Rep types...<br></blockquote><div><br>Yes, most likely. I have a package defining a few generic functions and showing some example uses (<a href="http://hackage.haskell.org/package/generic-deriving">http://hackage.haskell.org/package/generic-deriving</a>). There I use OverlappingInstances (and even UndecidableInstances).<br>
</div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<br>
An initial issue I'm running into (and again, this is preliminary,<br>
maybe I'll figure out a way without OverlappingInstances) is in<br>
implementing a function to serialize Generic data types to JSON. If<br>
the Haskell data type has selectors (i.e., is a record), then I would<br>
like to serialize/unserialize the type as a JSON object, where the<br>
names of the selectors are the JSON field names. If the Haskell data<br>
type does not have selectors, then I would like to serialize it as a<br>
JSON array.<br>
<br>
Generic deriving gives me the conIsRecord function, but this is at the<br>
value level, and I want to do something different at the type level.<br>
For a record, I need a list of (String, Value) pairs, while for a<br>
non-record, I need a list of Values. This leads me to write code such<br>
as the following:<br>
<br>
class JSON1 a r | a -> r<br>
toJSON1 :: a -> r<br>
<br>
instance (JSON a) => JSON1 (S1 NoSelector (K1 c a)) [Value] where<br>
toJSON1 (M1 (K1 a)) = [toJSON a]<br>
<br>
instance (Selector x, JSON a) => JSON1 (S1 x (K1 c a)) [(String, Value)] where<br>
toJSON1 s@(M1 (K1 a)) = [(nameOf s undefined, toJSON a)]<br>
where nameOf :: S1 c f p -> c -> String<br>
nameOf _ c -> selName c<br>
<br>
The key piece of magic I need here (and in so many other places) is to<br>
be able to do one thing at the type level if x is a particular type<br>
(e.g., NoSelector) or sometimes one of a small number of types, and to<br>
do something else if x is any other type.<br></blockquote><div><br>Right. I think this is often essential.<br><br><br>Cheers,<br>Pedro<br><br></div></div>