[Haskell-cafe] Re: semantics of type synonym

Maciej Piechotka uzytkownik2 at gmail.com
Tue Dec 29 13:14:56 EST 2009


On Tue, 2009-12-29 at 14:47 +0000, pbrowne wrote:
> Hi,
> I am studying the underlying semantics behind Haskell and to what degree
> those semantics are actually implemented. I need to clarify what a *type
> synonym* actual means in relation to Haskell's logic (or formal
> semantics). I used the following type synonym:
> 
> type Name = String
> getName(n) = n
> 

Don't use parentheses. You can think it looks like function call oin
other languages but it is not. (n) is a "one-element tuple" i.e. n so if
you have function with 2 arguments f (n, k) will not work (it will apply
one argument which is a tuple). 

usually it is written as:
getName n = n
or
getName = id

> I checked the types with two tests:
> -- test 1
> :t "ww"
> "ww" :: [Char]
> 
> -- test 2
> :t getName("ww")
> getName("ww") :: Name
> 
> Obviously I get two different types.
> In the case of the function Haskells type system seems to pick up enough
> information to determine that “ww” is a Name.
> But I am not sure what is happening with the literal "ww" in the first test.
> 
> 
> Pat

ghci> :t getName "ww"
getName "ww" :: [Char]
ghci> :t getName "ww" :: [Char]
getName "ww" :: [Char] :: [Char]
ghci> :t (getName "ww" :: [Char]) :: String
(getName "ww" :: [Char]) :: String :: String
ghci> :t ((getName "ww" :: [Char]) :: String) :: Name
((getName "ww" :: [Char]) :: String) :: Name :: Name

it works only because [Char], String and Name are synonimes (:: works
differently then () cast in C/C++ where (int)((double)((int)0)) is
legal). As far as compiler/interpreter is concerned it is indifferent on
the exact naming. However it tried to guess what name You would like to
have. For example:

type FullPath = String
type RelativePath = String

expand :: RelativePath -> FullPath
expand = ...

it is much more clear what you get (FullPath) - not just the raw type.
Of course it will not stop you when you state expand . expand as it is
rather decorative thing.

If you want to create new type you need to write:

newtype Name = Name String

Regards




More information about the Haskell-Cafe mailing list