使用 CPS 将 ImpredicativeTypes 减少为 RankNTypes
Reducing ImpredicativeTypes to RankNTypes with CPS
这四种说法,哪一种是错误的?
newtype
和 data
定义可以替换为 (->)
和 forall
. 的 type
结构
RankNTypes
实现正确。
ImpredicativeTypes
即使 GADT 不存在也很难正确实施。
- 前两个说法驳斥了第三个说法
编辑:我很惊讶这还不清楚。 data Maybe = Nothing | Just a
可以写成 type Maybe a = forall b. b -> (a -> b) -> b
。 data [] a = [] | a : [a]
可以写成 type [] a = forall b. b -> (a -> b -> b) -> b
。 (与 maybe
和 foldr
相比。)对于所有 newtype
和 data
定义,同样的事情应该是可能的,并且称为连续传递样式(CPS)。
据我所知,ghc
使用 RankNTypes
扩展正确实现了深度嵌套 forall
和 (->)
类型的类型推断,但是ImpredicativeTypes
,即使在 (->)
以外的类型构造函数的参数中也允许 forall
,但已无可救药地被破坏。
但是你不能通过将所有 data
和 newtype
类型转换为它们的 CPS 形式,使用 RankNTypes
机制进行推理然后转换回data
/newtype
表格?
None 个。使用 ImpredicativeTypes
的困难在于没有类型推断,需要用户提供明确的类型签名。您假设由于 RankNTypes
被广泛使用,类型推断通常适用于 RankNTypes
,但那是 not true:
It is not possible to infer higher-rank types in general; type annotations must be supplied by the programmer in many cases.
这四种说法,哪一种是错误的?
newtype
和data
定义可以替换为(->)
和forall
. 的 RankNTypes
实现正确。ImpredicativeTypes
即使 GADT 不存在也很难正确实施。- 前两个说法驳斥了第三个说法
type
结构
编辑:我很惊讶这还不清楚。 data Maybe = Nothing | Just a
可以写成 type Maybe a = forall b. b -> (a -> b) -> b
。 data [] a = [] | a : [a]
可以写成 type [] a = forall b. b -> (a -> b -> b) -> b
。 (与 maybe
和 foldr
相比。)对于所有 newtype
和 data
定义,同样的事情应该是可能的,并且称为连续传递样式(CPS)。
据我所知,ghc
使用 RankNTypes
扩展正确实现了深度嵌套 forall
和 (->)
类型的类型推断,但是ImpredicativeTypes
,即使在 (->)
以外的类型构造函数的参数中也允许 forall
,但已无可救药地被破坏。
但是你不能通过将所有 data
和 newtype
类型转换为它们的 CPS 形式,使用 RankNTypes
机制进行推理然后转换回data
/newtype
表格?
None 个。使用 ImpredicativeTypes
的困难在于没有类型推断,需要用户提供明确的类型签名。您假设由于 RankNTypes
被广泛使用,类型推断通常适用于 RankNTypes
,但那是 not true:
It is not possible to infer higher-rank types in general; type annotations must be supplied by the programmer in many cases.