[Haskell-cafe] Re: Type Synonyms

Henning Thielemann lemming at henning-thielemann.de
Thu Oct 11 05:02:44 EDT 2007


On Thu, 11 Oct 2007, Tom Davies wrote:

> Andrew Wagner <wagner.andrew <at> gmail.com> writes:
>
>> If you change your type declarations to 'newtype' declarations, I
>> believe you would get the effect that you want, depending on what you
>> mean by 'equivalent'. In that case, Foo and Bar would essentially be
>> strings, but you could not use either of them in a place where the
>> other is expected, nor where a String is expected. See
>> http://haskell.org/haskellwiki/Newtype for more information. Hope this
>> helps!
>
> I wanted to avoid wrapping the string with a constructor.
>
> I suppose what I'm really asking for is for each type to implicitly define a
> 'type class with no methods', and to be able to create new instances of
> that type class which simply behave as the underlying
> type.

For custom types you can add phantom type parameters. This way you can 
make types distinct while being able to apply the same functions to their 
values.

Say

data SpecialString usage = SpecialString String


Then you can define a generic function like

   take :: Int -> SpecialString usage -> SpecialString usage

or a specialised function like

   foo :: SpecialString Foo -> SpecialString Foo


More information about the Haskell-Cafe mailing list