[Haskell-cafe] creating a type based on a string

Ben Millwood haskell at benmachine.co.uk
Thu Sep 2 18:33:51 EDT 2010


On Thu, Sep 2, 2010 at 9:31 PM, Andrew U. Frank
<frank22 at geoinfo.tuwien.ac.at> wrote:
> I have a user input (string) and need to select one of two types.
> depending what the input is. is this possible?
>
> data A
> data B
>
> data X n = X String
>
> op :: String -> X n
> op "a" = X "a" :: X A
> op "b" = X "b" :: X B
>
> this does obviously not compile. is there a way to achieve that the type
> X A is produced when the input is "a" and X B when the input is "b"?
>
> thank you for help!
> andrew
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>

Here's another way of not quite doing what you want:

op :: String -> Either (X A) (X B)
op "a" = Left (X "a")
op "b" = Right (X "b")

which is roughly how I translate the recent discussion about
type-level validity certification:
http://www.haskell.org/pipermail/haskell-cafe/2010-August/082899.html


More information about the Haskell-Cafe mailing list