[Haskell-cafe] Polymorphic record field?

Michael Snoyman michael at snoyman.com
Sun Sep 26 06:53:46 EDT 2010


How about:

data MyStruct = forall a. MyTypeClass a => MyStruct {myField :: a}

I haven't actually run that through the compiler, but it should work.
For a "real life" example of this approach, look at SomeException[1].

Michael

[1] http://www.haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/Control-Exception.html#t%3ASomeException

On Sun, Sep 26, 2010 at 12:51 PM, Kevin Jardine <kevinjardine at gmail.com> wrote:
> I have an almost finished Haskell program and I discovered that I
> needed to change the type of one record field.
>
> The problem is that the new type is polymorphic.
>
> Being new to Haskell, I simply changed:
>
> data MyStruct = MyStruct {myField :: myType}
>
> to
>
> data MyStruct = MyStruct {myField :: MyTypeClass a => a}
>
> and then ended up in a maze of compiler messages about Rank N types
> and "Inferred type is less polymorphic than expected".
>
> Upon Googling for this issue, I have discovered that Haskell records
> do not support polymorphic types and that there is a large literature
> about options to fix this.
>
> Although the literature is interesting, what I am looking for right
> now is a simple fix.
>
> I really don't want to have to rewrite all the many type and value
> references to MyStruct in my code.
>
> Is there a way to redefine the definition of MyStruct without changing
> any of my other code?
>
> If I have to change the way myField is accessed, I can handle that.
> What I don't really want to do is change the type signature of
> functions that take MyStruct parameters (of which there are many).
>
> Is this possible? What is the simplest way to do this?
>
> Kevin
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


More information about the Haskell-Cafe mailing list