在 Haskell 中,列表 ( '[Something] ) 前面的撇号是什么意思? [英] What does an apostrophe in front of a list ( '[Something] ) mean in Haskell?
问题描述
我正在阅读 Servant 文档 并发现了这个行:
I was reading the Servant documentation and came across this line:
type UserAPI = "users" :> QueryParam "sortby" SortBy :> Get '[JSON] [User]
'
对该列表做了什么?
推荐答案
这是DataKinds
在起作用,其中:
This is DataKinds
in action, which:
- 提升类型级别的值,以及
- 将类型提升到种类级别
然而,这会导致类型级别的混淆.现在,在类型中,[X]
可能是 [X] :: *
,X
列表类型,或者我们由于提升,可能有 [X] :: [T]
-- 即值 [X]
(列表仅包含单个值 X
),带有 T
类型的 X
,在类型级别提升.
This however causes confusion at the type level. Now, in types, [X]
might either be [X] :: *
, the list-of-X
type, or instead we might have [X] :: [T]
due to the lifting -- that is the value [X]
(list containing only the single value X
), with X
of type T
, lifted at the type level.
为了克服这种歧义,GHC 需要在提升的值构造函数前加上引号.所以,我们有 [X] :: *
和 '[X] :: [T]
.
To overcome this ambiguity, GHC requires a quote in front of lifted value constructors. So, we have [X] :: *
, and '[X] :: [T]
.
具体来说,在您的情况下,Get '[JSON] [User]
涉及提升到类型级别的列表值 [JSON]
和列表类型 <代码>[用户]代码>.为了更好地理解差异,请注意没有 '[JSON]
类型的术语,因为这不是列表类型.我们甚至可以将 Get '[JSON,JSON,JSON] [User]
作为友好的表达式,甚至 Get '[] [User]
.相反,我们不能有 Get '[JSON] [User,User]
因为 [User,User]
不是类型.
Concretely, in your case, Get '[JSON] [User]
involves both the list value [JSON]
lifted to the type level, and the list type [User]
. To better appreciate the difference, note that there are no terms of type '[JSON]
, since this is not a list type. We could even have Get '[JSON,JSON,JSON] [User]
as a well-kinded expression, or even Get '[] [User]
. Instead, we can't have Get '[JSON] [User,User]
since [User,User]
is not a type.
(类型 Get '[JSON,JSON,JSON] [User]
,即使它是有效的,也不能被 Servant 库有意义地使用.我不知道这解除了什么列表用于Servant.)
(The type Get '[JSON,JSON,JSON] [User]
, even if it is valid, could not be meaningfully used by the Servant library. I have no idea of what that lifted list is used for in Servant.)
这篇关于在 Haskell 中,列表 ( '[Something] ) 前面的撇号是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!