What does the apostrophe in front of the list ("[Something])" mean in Haskell?

I read the Servant documentation and came across this line:

type UserAPI = "users" :> QueryParam "sortby" SortBy :> Get '[JSON] [User] 

What does ' on this list?

+5
source share
2 answers

This is a DataKinds action in an action that:

  • raises values ​​at level level and
  • lists types at level level

This, however, causes confusion at the type level. Now in types [X] can be either [X] :: * , type list-of- X , or instead we can have [X] :: [T] because of the rise - this is the value [X] (list, containing only a single value of X ), with an X type T raised at level level.

To overcome this ambiguity, the GHC requires a quote before the constructed value constructors. So we have [X] :: * and '[X] :: [T] .

Specifically, in your case, Get '[JSON] [User] includes both the list value [JSON] raised to the type level and the type of the list [User] . To better understand the difference, note that there are no (useful) terms like '[JSON] , as this is not a list type. We could even have Get '[JSON,JSON,JSON] [User] as a well-defined expression, or even Get '[] [User] . Instead, we cannot have Get '[JSON] [User,User] since [User,User] not a type.

(The type Get '[JSON,JSON,JSON] [User] , even if it is valid, cannot be used meaningfully by the Servant library. I have no idea why this elevated list is used in Servant.)

+2
source

Quotations are used to distinguish between type-type constructors compared to level type constructors of promoted types.

For instance:

 {-# LANGUAGE DataKinds #-} data Which = One | Two myPick :: Which -- Type myPick = One type MyPick :: Which -- Kind type MyPick = 'One 

By the way, annotation of the type type MyPick :: Which not valid Haskell, but gives you an idea of ​​the correspondence between the term and the level of the type. The closest thing you can do for this is to enable another extension:

 {-# LANGUAGE TypeFamilies #-} type family MyPick :: Which where MyPick = 'One 
+5
source

Source: https://habr.com/ru/post/1269404/


All Articles