
Hello all, My problem is the following: I have my own data types, and I'd like to derive automatically instances of some type class from them. I'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: class MyClass where fromString :: String -> a data MyData=MkMyData { myField ::Int } deriving (Generic) and I want to automatically generate the instance instance MyClass MyData, using default methods, etc. The GHC Generic class does say that it uses a from function that convert from the datatype to its representation: from :: a -> Rephttp://www.haskell.org/ghc/docs/7.4.1/html/libraries/ghc-prim-0.2.0.0/GHC-Ge... a xfrom :: a -> Rephttp://www.haskell.org/ghc/docs/7.4.1/html/libraries/ghc-prim-0.2.0.0/GHC-Ge... a x But I don't have a "a" 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't work. However I see the GHC.Generics also provide :+: (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 http://www.haskell.org/haskellwiki/GHC.Generics do provide an example of defining the instance for :+: but I don'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't pattern match on undefined. 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. Thanks a million! JP -- JP Moresmau http://jpmoresmau.blogspot.com/

Well, in your case, you need not 'from', but 'to', in order to convert
from a generic representation to yours.
Take a look at how a similar task is done in SmallCheck:
https://github.com/feuerbach/smallcheck/blob/master/Test/SmallCheck/Series.h...
https://github.com/feuerbach/smallcheck/blob/master/Test/SmallCheck/Series.h...
Roman
* JP Moresmau
Hello all, My problem is the following: I have my own data types, and I'd like to derive automatically instances of some type class from them. I'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: class MyClass where fromString :: String -> a
data MyData=MkMyData { myField ::Int } deriving (Generic)
and I want to automatically generate the instance instance MyClass MyData, using default methods, etc. The GHC Generic class does say that it uses a from function that convert from the datatype to its representation: from :: a -> Rephttp://www.haskell.org/ghc/docs/7.4.1/html/libraries/ghc-prim-0.2.0.0/GHC-Ge... a xfrom :: a -> Rephttp://www.haskell.org/ghc/docs/7.4.1/html/libraries/ghc-prim-0.2.0.0/GHC-Ge... a x But I don't have a "a" 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't work. However I see the GHC.Generics also provide :+: (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 http://www.haskell.org/haskellwiki/GHC.Generics do provide an example of defining the instance for :+: but I don'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't pattern match on undefined.
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.
Thanks a million!
JP -- JP Moresmau http://jpmoresmau.blogspot.com/
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Yes, this looks like a similar task, thanks a million!
JP
On Fri, Jul 12, 2013 at 10:57 AM, Roman Cheplyaka
Well, in your case, you need not 'from', but 'to', in order to convert from a generic representation to yours.
Take a look at how a similar task is done in SmallCheck:
https://github.com/feuerbach/smallcheck/blob/master/Test/SmallCheck/Series.h...
https://github.com/feuerbach/smallcheck/blob/master/Test/SmallCheck/Series.h...
Roman
Hello all, My problem is the following: I have my own data types, and I'd like to derive automatically instances of some type class from them. I'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: class MyClass where fromString :: String -> a
data MyData=MkMyData { myField ::Int } deriving (Generic)
and I want to automatically generate the instance instance MyClass MyData, using default methods, etc. The GHC Generic class does say that it uses a from function that convert from the datatype to its representation: from :: a -> Rep< http://www.haskell.org/ghc/docs/7.4.1/html/libraries/ghc-prim-0.2.0.0/GHC-Ge...
a xfrom :: a -> Rep< http://www.haskell.org/ghc/docs/7.4.1/html/libraries/ghc-prim-0.2.0.0/GHC-Ge...
a x But I don't have a "a" 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't work. However I see the GHC.Generics also provide :+: (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 http://www.haskell.org/haskellwiki/GHC.Generics do provide an example of defining the instance for :+: but I don't understand how we can get
* JP Moresmau
[2013-07-12 10:45:39+0200] 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't pattern match on undefined.
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.
Thanks a million!
JP -- JP Moresmau http://jpmoresmau.blogspot.com/
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- JP Moresmau http://jpmoresmau.blogspot.com/
participants (2)
-
JP Moresmau
-
Roman Cheplyaka