Hi, I am using Data.Binary which defined "instance Binary a => Binary [a]". Now I need to define "instance Binary [String]" to make something special for string list. How to make it work? I looked into the chapter of overlappinginstances, nothing works. -- 竹密岂妨流水过 山高哪阻野云飞
Am 05.01.2011 09:24, schrieb Magicloud Magiclouds:
Hi, I am using Data.Binary which defined "instance Binary a => Binary [a]". Now I need to define "instance Binary [String]" to make something special for string list. How to make it work? I looked into the chapter of overlappinginstances, nothing works.
Just a guess: Have you enabled TypeSynonymInstances? (As String is a type synonym, at least this extension would be required)
Hello, {-# LANGUAGE OverlappingInstances, FlexibleInstances #-} import Data.Binary instance Binary [String] where get = undefined put = undefined works fine here on GHC 6.12.3. That being said, it would be safer perhaps to add a newtype around [String] so you can avoid the orphan instance as well, i.e. import Data.Binary newtype MyType = MyType [String] instance Binary MyType where get = undefined put = undefined Cheers, Jasper On Wed, Jan 5, 2011 at 9:24 AM, Magicloud Magiclouds <magicloud.magiclouds@gmail.com> wrote:
Hi, I am using Data.Binary which defined "instance Binary a => Binary [a]". Now I need to define "instance Binary [String]" to make something special for string list. How to make it work? I looked into the chapter of overlappinginstances, nothing works. -- 竹密岂妨流水过 山高哪阻野云飞
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
You can't. If you have special semantics for [String], then it is not really a [String], it is something else. So let the type system know that: newtype SomethingElse = SomethingElse [String] instance Binary SomethingElse where ... On Wed, Jan 5, 2011 at 1:24 AM, Magicloud Magiclouds <magicloud.magiclouds@gmail.com> wrote:
Hi, I am using Data.Binary which defined "instance Binary a => Binary [a]". Now I need to define "instance Binary [String]" to make something special for string list. How to make it work? I looked into the chapter of overlappinginstances, nothing works. -- 竹密岂妨流水过 山高哪阻野云飞
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
You have two choices (other people have enumerated the first while I was typing): First choice: Wrap your Stringlist with a newtype: newtype StringList = StringList [String] The downside of this your code gets "polluted" with the newtype. Second choice: Write special putStringList and getStringList functions. Hand-code the binary instances where you are wanting [String] to be special and call putStringList and getStringList rather than put and get. Downside - cannot automatically derive Binary. That's if Binary can be automatically derived anyway?, the times when I need Binary I write the instances myself anyway.
Steffen Schuldenzucker: Sure. GHC would prompt that. Jasper Van der Jeugt: Not working with ghc7. But there sure are some threads about this kind of things. I do not know if this is a bug of 6.* or 7, either. Luke Palmer: Sorry, by special, I meant, for example, ["a", "b"] will be "ab" by default, but I want it to be "a,b". So I'd like to overload for certain types. Stephen Tetley: I think that are the only choices. The first is simple, but the second saves some code writing. After all, thanks. On Wed, Jan 5, 2011 at 4:41 PM, Stephen Tetley <stephen.tetley@gmail.com> wrote:
You have two choices (other people have enumerated the first while I was typing):
First choice:
Wrap your Stringlist with a newtype:
newtype StringList = StringList [String]
The downside of this your code gets "polluted" with the newtype.
Second choice:
Write special putStringList and getStringList functions. Hand-code the binary instances where you are wanting [String] to be special and call putStringList and getStringList rather than put and get.
Downside - cannot automatically derive Binary. That's if Binary can be automatically derived anyway?, the times when I need Binary I write the instances myself anyway.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- 竹密岂妨流水过 山高哪阻野云飞
Magicloud Magiclouds schrieb:
Hi, I am using Data.Binary which defined "instance Binary a => Binary [a]". Now I need to define "instance Binary [String]" to make something special for string list. How to make it work? I looked into the chapter of overlappinginstances, nothing works.
Hello, I only use the 'Binary' class when I don't care about the specifics of the serialization format, only that it be reasonably fast, compact and stable. When I need to comply with some particular format I use the functions in Data.Binary.Builder and Data.Binary.Get directly. Sometimes I make my own Serialize/Deserialize classes if I think it will be helpful. But I do look at the source for the instances of the 'Binary' class for inspiration! Take care, Antoine On Wed, Jan 5, 2011 at 2:24 AM, Magicloud Magiclouds <magicloud.magiclouds@gmail.com> wrote:
Hi, I am using Data.Binary which defined "instance Binary a => Binary [a]". Now I need to define "instance Binary [String]" to make something special for string list. How to make it work? I looked into the chapter of overlappinginstances, nothing works. -- 竹密岂妨流水过 山高哪阻野云飞
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
I agree, but with a slight difference. Since I am so lazy, if Binary fits, like 80% of my requirements, for example this case, only [String] is not OK. I'd like to reuse it with a little modification made by myself, rather than re-write almost the whole of it. But for Data.Binary, I think this problem is simple. Since it cannot be derivinged, making extra function for [String] won't make the further usage of my library complex. On Fri, Jan 7, 2011 at 11:32 AM, Antoine Latter <aslatter@gmail.com> wrote:
Hello,
I only use the 'Binary' class when I don't care about the specifics of the serialization format, only that it be reasonably fast, compact and stable.
When I need to comply with some particular format I use the functions in Data.Binary.Builder and Data.Binary.Get directly. Sometimes I make my own Serialize/Deserialize classes if I think it will be helpful.
But I do look at the source for the instances of the 'Binary' class for inspiration!
Take care, Antoine
On Wed, Jan 5, 2011 at 2:24 AM, Magicloud Magiclouds <magicloud.magiclouds@gmail.com> wrote:
Hi, I am using Data.Binary which defined "instance Binary a => Binary [a]". Now I need to define "instance Binary [String]" to make something special for string list. How to make it work? I looked into the chapter of overlappinginstances, nothing works. -- 竹密岂妨流水过 山高哪阻野云飞
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- 竹密岂妨流水过 山高哪阻野云飞
participants (7)
-
Antoine Latter -
Henning Thielemann -
Jasper Van der Jeugt -
Luke Palmer -
Magicloud Magiclouds -
Steffen Schuldenzucker -
Stephen Tetley