[Haskell-cafe] semantics of type synonym
Lutz Donnerhacke
lutz at iks-jena.de
Tue Dec 29 09:57:33 EST 2009
* pbrowne wrote:
> semantics). I used the following type synonym:
type String = [Char]
type Name = String
String, Name and [Char] are synonyms, which means every expression is
identically to the others. There is no difference besides that String and
Name are type aliases while [Char] is a type construct.
getName :: String -> Name
getName n = n
> I checked the types with two tests:
> -- test 1
>:t "ww"
> "ww" :: [Char]
The type interference system determines that you have an array of
characters, hence a [Char]. All those existing type aliases are suppressed
by the module. Otherwise the list get's very long ...
> -- test 2
>:t getName("ww")
> getName("ww") :: Name
>From the definition of getName, the compiler knows which type alias is
prefered from the set of equivalent names.
> Obviously I get two different types.
You get two different representations of the same type.
> In the case of the function Haskells type system seems to pick up enough
> information to determine that “ww” is a Name.
Nope. "ww" is still a [Char] for the compiler. And you do not even check for
the type of "ww".
:t snd . (\x -> (getName x, x)) $ "ww"
... :: String
More information about the Haskell-Cafe
mailing list