[Haskell-cafe] Over general types are too easy to make.

Nick Vanderweit nick.vanderweit at gmail.com
Fri Aug 31 23:17:34 CEST 2012


It is often the case that using GADTs with phantom types can allow you to 
constrain which functions can operate on the results of which constructors. I 
believe this is common practice now in such situations.


Nick

On Friday, August 31, 2012 09:32:37 PM Paolino wrote:
> Hello Timothy
> 
> GADTs let you catch more errors at compile time. With them you can give
> different types to constructors of the same datatype.
> 
> regards
> paolino
> 2012/8/31 <timothyhobbs at seznam.cz>
>  
> > Sure, but that's relying on the promise that you're passing it a valid
> > BadFrog...  Consider then:
> > 
> > 
> > deBadFrog $ BadFrogType (BadBar { badFoo = 1})
> > 
> > 
> > ---------- Původní zpráva ----------
> > Od: John Wiegley <johnw at newartisans.com>
> > Datum: 31. 8. 2012
> > Předmět: Re: [Haskell-cafe] Over general types are too easy to make.
> > 
> > >>>>> <timothyhobbs at seznam.cz> writes:
> > >> data BadFoo =
> > >> BadBar{
> > >> badFoo::Int} |
> > >> BadFrog{
> > >> badFrog::String,
> > >> badChicken::Int}
> > > 
> > > This is fine, until we want to write a function that acts on Frogs but
> > 
> > not
> > 
> > > on Bars. The best we can do is throw a runtime error when passed a Bar
> > 
> > and
> > 
> > > not a Foo:
> > You can use wrapper types to solve this:
> > 
> > data BadBarType = BadBarType BadFoo
> > data BadFrogType = BadFrogType BadFoo
> > 
> > Now you can have:
> > 
> > deBadFrog :: BadFrogType -> String
> > 
> > And call it as:
> > 
> > deBadFrog $ BadFrogType (BadFrog { badFrog = "Hey", badChicken = 1})
> > 
> > Needless to say, you will have to create helper functions for creating
> > Bars
> > and Frogs, and not allow your BadBar or BadFrog value constructors to be
> > visible outside your module.
> > 
> > John
> > 
> > _______________________________________________
> > Haskell-Cafe mailing list
> > Haskell-Cafe at haskell.org
> > http://www.haskell.org/mailman/listinfo/haskell-cafe
> > 
> > 
> > _______________________________________________
> > 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