5

I'm trying to understand what the following type synonym from Yesod is doing.

type HtmlUrlI18n msg url = Translate msg -> Render url -> Html

I could not find an example in learn you some haskell or the haskell wikibook of a type synonym with -> present. Any links or explanations are much appreciated. Thanks.

5
  • 9
    It's just a synonym for a function type. Jul 8, 2012 at 18:59
  • 3
    Everywhere you see HtmlUrlI18n msg url, replace that with Translate msg -> Render url -> Html.
    – sdcvvc
    Jul 8, 2012 at 19:38
  • To add to what the cat said: It's done because the right hand type signature is a bit unwieldy, and perhaps hard to remember. Suppose you have a function that takes a Translate msg -> Render url -> Html as one of many arguments. That quickly gets unreadable and perhaps unclear, whereas having it take a HtmlUrlI18n msg url is much better.
    – gspr
    Jul 8, 2012 at 21:37
  • Types representing functions took me a while to grasp. This is the meat and potatoes of Haskell. You're good now!
    – Eric
    Jul 8, 2012 at 23:49
  • 3
    Syntax for the function type is no different from any other type. Data constructor -> is different from, for example, Either only by its infix position. Type Translate msg -> Render url -> Html is equivalent to (->) (Translate msg) ((->) (Render url) Html). By the way, you can define your own infix types.
    – Boris
    Jul 9, 2012 at 10:04

1 Answer 1

4

Its just a synonym for a (long to write down) function type. For example, the following should be valid Haskell

--Example of a function type synonym
type StrFn = String -> String

foo :: StrFn
foo s = s ++ "!"

--Example of a function type synonym with type parameters
type Fn a = a -> a

bar :: Fn String
bar s = s ++ "?"
2
  • Thanks so much to everyone for your answers. Very helpful.
    – David
    Jul 10, 2012 at 14:15
  • @David: Don't forget to mark an answer as accepted, so the question leaves the unanswered tab. You can also write an answer of your own if you want.
    – hugomg
    Jul 10, 2012 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.