<div dir="ltr">Hello all,<div>My problem is the following: I have my own data types, and I&#39;d like to derive automatically instances of some type class from them. I&#39;ve started looking at GHC.Generics, which offer tools to do exactly that. However, some functions of my typeclass do not take my data type as a parameter, but as a result. Basically:</div>
<div> class MyClass where</div><div>   fromString :: String -&gt; a</div><div><br></div><div> data MyData=MkMyData { </div><div style>   myField ::Int </div><div style> } deriving (Generic)</div><div style><br></div><div style>
and I want to automatically generate the instance instance MyClass MyData, using default methods, etc.</div><div style>The GHC Generic class does say that it uses a from function that convert from the datatype to its representation: <a name="v:from" class="" style="font-size:13px;margin:0px;padding:0px;font-weight:bold;color:rgb(0,0,0);font-family:monospace;line-height:16px">from</a><span style="font-size:13px;color:rgb(0,0,0);font-family:monospace;line-height:16px;background-color:rgb(240,240,240)"> :: a -&gt; </span><a href="http://www.haskell.org/ghc/docs/7.4.1/html/libraries/ghc-prim-0.2.0.0/GHC-Generics.html#t:Rep" style="font-size:13px;margin:0px;padding:0px;text-decoration:none;color:rgb(171,105,84);font-family:monospace;line-height:16px">Rep</a><span style="font-size:13px;color:rgb(0,0,0);font-family:monospace;line-height:16px;background-color:rgb(240,240,240)"> a x</span><a name="v:from" class="" style="font-size:13px;margin:0px;padding:0px;font-weight:bold;color:rgb(0,0,0);font-family:monospace;line-height:16px">from</a><span style="font-size:13px;color:rgb(0,0,0);font-family:monospace;line-height:16px;background-color:rgb(240,240,240)"> :: a -&gt; </span><a href="http://www.haskell.org/ghc/docs/7.4.1/html/libraries/ghc-prim-0.2.0.0/GHC-Generics.html#t:Rep" style="font-size:13px;margin:0px;padding:0px;text-decoration:none;color:rgb(171,105,84);font-family:monospace;line-height:16px">Rep</a><span style="font-size:13px;color:rgb(0,0,0);font-family:monospace;line-height:16px;background-color:rgb(240,240,240)"> a x</span></div>
<div style>But I don&#39;t have a &quot;a&quot; to start from! I see from the related papers that the automatically generated code from from actually does pattern matches on constructors, so I need a value, undefined won&#39;t work. However I see the GHC.Generics also provide :+: (<span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:18px">Sums: encode choice between constructors). If I have to provide an value, then the choice between constructor has been done! The examples about generics on </span> <a href="http://www.haskell.org/haskellwiki/GHC.Generics">http://www.haskell.org/haskellwiki/GHC.Generics</a> do provide an example of defining the instance for :+: but I don&#39;t understand how we can get there. If I have a class method that takes a value as a parameter, and I pass undefined to it, the code will crash, since it can&#39;t pattern match on undefined.</div>
<div style><br></div><div style>Can somebody shed some light on this? Am I using the wrong tool for the job? How can I achieve what I want? I want the full type representation with sums, but without a value to start from.</div>
<div style><br></div><div style>Thanks a million!</div><div><div><br></div><div style>JP</div>-- <br>JP Moresmau<br><a href="http://jpmoresmau.blogspot.com/">http://jpmoresmau.blogspot.com/</a>
</div></div>