Understanding Haskell Level Literals

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

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

I had trouble understanding what this means, and we would understand the following:

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

  • What is '[JSON] ? Is this some kind of literal list at type level? What does the quote do?

+6
source share
2 answers

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

'[JSON] really is a list of type types equivalent to JSON ': '[] . A single quote indicates that the data constructor is escalated to the type constructor. I'm not sure what matters a lot, but at least it avoids the confusion that might otherwise arise from the fact that data constructors and type constructors can share names.

+5
source

For the record only, actual definitions are provided :<|> and :> .

 -- that really like a pair of an 'a' and a 'b'... -- that can be chained in a nice way, as opposed to nested pairs. data a :<|> b = a :<|> b data a :> b 

The latter does not have a constructor, because we do not need it when combining request handlers together, while we reuse the symbol :<|> when we glue several request handlers together at the value level, as opposed to matching descriptions for several endpoints on type level, where we also use the operator :<|> . In the latter case, we refer to the constructor :<|> -the-type, whereas when we use it on handlers, we refer to the constructor :<|> -the-data.

+2
source

All Articles