在Haskell中,列表前面的撇号('[Something])是什么意思? [英] What does an apostrophe in front of a list ( '[Something] ) mean in Haskell?

查看:69
本文介绍了在Haskell中,列表前面的撇号('[Something])是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Servant 文档,并偶然发现了行:

I was reading the Servant documentation and came across this line:

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

'对该列表做什么?

推荐答案

这是DataKinds的实际应用,

  • 提升类型级别的值,
  • 将类型提升到同类水平

但是,这会导致类型级别的混乱.现在,在类型中,[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]和列表类型[User].为了更好地理解它们之间的区别,请注意,没有类型'[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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆