<div dir="ltr">Yes, this looks like a similar task, thanks a million!<div><br></div><div>JP</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Jul 12, 2013 at 10:57 AM, Roman Cheplyaka <span dir="ltr">&lt;<a href="mailto:roma@ro-che.info" target="_blank">roma@ro-che.info</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Well, in your case, you need not &#39;from&#39;, but &#39;to&#39;, in order to convert<br>
from a generic representation to yours.<br>
<br>
Take a look at how a similar task is done in SmallCheck:<br>
<a href="https://github.com/feuerbach/smallcheck/blob/master/Test/SmallCheck/Series.hs#L180" target="_blank">https://github.com/feuerbach/smallcheck/blob/master/Test/SmallCheck/Series.hs#L180</a><br>
<a href="https://github.com/feuerbach/smallcheck/blob/master/Test/SmallCheck/Series.hs#L352" target="_blank">https://github.com/feuerbach/smallcheck/blob/master/Test/SmallCheck/Series.hs#L352</a><br>
<br>
Roman<br>
<br>
* JP Moresmau &lt;<a href="mailto:jpmoresmau@gmail.com">jpmoresmau@gmail.com</a>&gt; [2013-07-12 10:45:39+0200]<br>
<div class="im">&gt; Hello all,<br>
&gt; My problem is the following: I have my own data types, and I&#39;d like to<br>
&gt; derive automatically instances of some type class from them. I&#39;ve started<br>
&gt; looking at GHC.Generics, which offer tools to do exactly that. However,<br>
&gt; some functions of my typeclass do not take my data type as a parameter, but<br>
&gt; as a result. Basically:<br>
&gt;  class MyClass where<br>
&gt;    fromString :: String -&gt; a<br>
&gt;<br>
&gt;  data MyData=MkMyData {<br>
&gt;    myField ::Int<br>
&gt;  } deriving (Generic)<br>
&gt;<br>
&gt; and I want to automatically generate the instance instance MyClass MyData,<br>
&gt; using default methods, etc.<br>
&gt; The GHC Generic class does say that it uses a from function that convert<br>
&gt; from the datatype to its representation: from :: a -&gt;<br>
</div>&gt; Rep&lt;<a href="http://www.haskell.org/ghc/docs/7.4.1/html/libraries/ghc-prim-0.2.0.0/GHC-Generics.html#t:Rep" target="_blank">http://www.haskell.org/ghc/docs/7.4.1/html/libraries/ghc-prim-0.2.0.0/GHC-Generics.html#t:Rep</a>&gt;<br>

&gt; a<br>
&gt; xfrom :: a -&gt; Rep&lt;<a href="http://www.haskell.org/ghc/docs/7.4.1/html/libraries/ghc-prim-0.2.0.0/GHC-Generics.html#t:Rep" target="_blank">http://www.haskell.org/ghc/docs/7.4.1/html/libraries/ghc-prim-0.2.0.0/GHC-Generics.html#t:Rep</a>&gt;<br>

<div class="im">&gt; a<br>
&gt; x<br>
&gt; But I don&#39;t have a &quot;a&quot; to start from! I see from the related papers that<br>
&gt; the automatically generated code from from actually does pattern matches on<br>
&gt; constructors, so I need a value, undefined won&#39;t work. However I see the<br>
&gt; GHC.Generics also provide :+: (Sums: encode choice between constructors).<br>
&gt; If I have to provide an value, then the choice between constructor has been<br>
&gt; done! The examples about generics on<br>
&gt; <a href="http://www.haskell.org/haskellwiki/GHC.Generics" target="_blank">http://www.haskell.org/haskellwiki/GHC.Generics</a> do provide an example of<br>
&gt; defining the instance for :+: but I don&#39;t understand how we can get there.<br>
&gt; If I have a class method that takes a value as a parameter, and I pass<br>
&gt; undefined to it, the code will crash, since it can&#39;t pattern match on<br>
&gt; undefined.<br>
&gt;<br>
&gt; Can somebody shed some light on this? Am I using the wrong tool for the<br>
&gt; job? How can I achieve what I want? I want the full type representation<br>
&gt; with sums, but without a value to start from.<br>
&gt;<br>
&gt; Thanks a million!<br>
&gt;<br>
&gt; JP<br>
&gt; --<br>
&gt; JP Moresmau<br>
&gt; <a href="http://jpmoresmau.blogspot.com/" target="_blank">http://jpmoresmau.blogspot.com/</a><br>
<br>
</div>&gt; _______________________________________________<br>
&gt; Haskell-Cafe mailing list<br>
&gt; <a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
&gt; <a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br>JP Moresmau<br><a href="http://jpmoresmau.blogspot.com/">http://jpmoresmau.blogspot.com/</a>
</div>