gpt4 book ai didi

haskell - 模板-haskell中的单双引号/撇号有什么区别?

转载 作者:行者123 更新时间:2023-12-04 12:30:38 31 4
gpt4 key购买 nike

使用 Optics 了解 Haskell 镜头时包,我遇到了下面的例子:

data Person = Person 
{ _name :: String
, _age :: Int
}

makeLenses ''Person
makePrisms 'Person

Name 类型的值代表什么,单引号和双引号/撇号之间有什么区别?

两者似乎具有相同的类型:

makeLenses, makePrisms :: Name -> DecsQ

template-haskell documentation我无法理解。侧重于语法,缺乏示例:

* 'f has type Name, and names the function f. Similarly 'C has type Name and names the data constructor C. In general '⟨thing⟩ interprets ⟨thing⟩ in an expression context.

* ''T has type Name, and names the type constructor T. That is, ''⟨thing⟩ interprets ⟨thing⟩ in a type context.

最佳答案

我们有两种引用形式来区分数据构造函数和类型构造函数。

考虑这个变体:

 data Person = KPerson 
{ _name :: String
, _age :: Int
}

makeLenses ''Person -- the type constructor
makePrisms 'KPerson -- the data constructor

很明显,在一种情况下我们使用 Name对于类型构造函数,而在另一种情况下,我们指的是 Name用于数据构造函数。

原则上,Haskell 可以使用单一形式的引用,只要构造函数的名称如 PersonKPerson始终保持不同。由于情况并非如此,我们需要在命名类型和数据构造函数之间消除歧义。

请注意,在实践中,习惯上对两个构造函数使用相同的名称,因此在实际代码中经常需要这种消歧。

关于haskell - 模板-haskell中的单双引号/撇号有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69332931/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com