Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Haskell Type Level Literals

Looking at the Haskell Servant package, there is an initial example of defining a webservice API as:

             -- GET /date
type MyAPI = "date" :> Get '[JSON] Date
             -- GET /time/:tz
        :<|> "time" :> Capture "tz" Timezone :> Get '[JSON] Time

I'm having trouble understanding what this means and would appreciate an explanation for the following:

  1. :> and :<|> are infix constructors. Does this type declaration mean that they are defined here or they are used here? Or maybe :> is defined here but :<|> is defined elsewhere? Or something else? Not sure how to read this type.

  2. What is '[JSON]? Is this some sort of type-level literal list? What does the quote do?

like image 458
Ana Avatar asked Oct 29 '15 04:10

Ana


1 Answers

The (infix) constructors are used here, and they must be defined elsewhere in data or newtype declarations. type declarations do not produce constructors of any sort ever.

'[JSON] is indeed a type level list, equivalent to JSON ': '[]. The single quote indicates that a data constructor is being lifted to a type constructor. I'm not sure what deep significance that has, but at least it avoids confusion that might otherwise arise from the fact that data constructors and type constructors can share names.

like image 182
dfeuer Avatar answered Sep 24 '22 19:09

dfeuer