Understanding the Echo Service API from Servant Paper

The introduction of the slave. page contains the following API type example:

type Echo = "echo"
          :> ReqBody ’[PlainText] String
          :> Get ’[PlainText] String

      

I am trying to understand this example. It looks like this is a definition of a type synonym, but seems to be related to some things that I haven't seen before.

Here are the three questions I have:

  • How does a string literal exist in a type definition?

"echo"

- string literal. I thought they were only used to define specific strings and don't know what that means when used in a type declaration.

  1. What is a symbol :>

    ?

The definition of this symbol in the "servant" package appears to be specified here , and looks like this:

data (path :: k) :> a
    deriving (Typeable)
infixr 9 :>

      

I would guess it :>

matches a /

in the api line, but can't see how this definition can happen.

This is the first time I see a non-alphanumeric type other than the function type ->

.

  1. What does the apostrophe before the list type mean?

[PlainText]

I would understand, just presenting a list of PlainText elements, but in contrast, I don't understand what it means ’[PlainText]

.

+3


source to share


1 answer


How does a string literal exist in a type definition?

The extension DataKinds

allows these string literals to be used in type signatures. Each is a different type. They all belong to the species Symbol

; we can check this with the command :kind

in ghci:

Prelude> :set -XDataKinds
Prelude> :kind "foo"
"foo" :: GHC.Types.Symbol

      

Because in Haskell only view types *

matter, these level-level strings are not populated. But there are functions that will let you get the appropriate term-level string anyway.

What is the symbol:>?

The extension TypeOperators

allows you to define types with operator names. This is useful for parameterized types, which are mainly used to combine other types. Note that it :>

has two type parameters, but not value-level constructors: it is only used at the type level.

What does the apostrophe before the list type mean?



DataKinds

does more than include symbols. It allows value constructors to be used as types, and the types of those constructors become views in turn. For example:

Prelude> :set -XDataKinds
Prelude> :kind True
True :: Bool

      

These advanced constructors are not populated with any term (because they have different kinds of *

).

In particular, we can promote lists at the type level. But this creates some ambiguity. What's [Int]

in a signature? Is it a list type Int

(which has a view *

) or is it a list of types with one element, type Int

? The apostrophe clearly shows that we are talking about a list of type types.

Prelude> :set -XDataKinds
Prelude> :kind [Int]
[Int] :: *
Prelude> :kind '[Int]
'[Int] :: [*]
Prelude> :kind '[True]
'[True] :: [Bool]

      

Level type lists of more than one element do not require an apostrophe, since there is no ambiguity:

Prelude> :kind [Int,Int]
[Int,Int] :: [*]

      

+1


source







All Articles