为什么不能(完全)应用一个使用其他类型同义词的参数的类型同义词? [英] Why can't you (totally) apply a type synonym that has arguments using an other type synonym?

查看:141
本文介绍了为什么不能(完全)应用一个使用其他类型同义词的参数的类型同义词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  type MyType t = t String  使用实参可以定义类型同义词,字符串

数据测试ab =测试ab

f :: MyType测试
f =未定义

main =未定义

编译这个结果是没有错误的:

  $ ghc --make test.hs 
[1的1]编译主(test.hs,test.o)
链接测试...

然而,当 Test 是一个类型的同义词时,这不起作用:

  type MyType t = t String String 

data Test ab = Test ab

type Test'ab = Test ab

f :: MyType Test'
f = undefined

main = undefined

其中给出以下错误:

  $ ghc --make test.hs 
[1 of 1] Co mpiling Main(test.hs,test.o)

test.hs:7:6:
类型synonym Test'应该有2个参数,但是没有给出
In 'f'的类型签名:f :: MyType(Test')

令我困惑的是 Test' 被应用于两个参数,那么为什么GHC抱怨说我没有传递参数?



不应该将类型同义词完全透明并且不可能与其他类型的类型区分开来吗?

是否有任何方法可以实现预期的行为根据


类型构造函数符号#x10-630004.1> Haskell report 类型同义词不能部分应用: > T 按类型引入同义词声明
不能部分应用;使用 T 而没有
全部参数是一个静态错误。


特别是在 之前检查扩展了类型的同义词,这意味着在表达式中:

  MyType Test'

扩展 MyType ,因此测试'结果部分应用。



然而, / strong>可能使用GHC来实现该行为 LiberalTypeSynonyms 扩展名。这个扩展将类型同义词视为宏,这些宏在没有任何检查的情况下展开,而之后类型检查器将查看类型同义词是否部分应用。



类推理仍然在类型同义词扩展之前完成。注意,类型同义词即使在这种情况下,例如:

 测试'Int 

仍然是一个错误。但是,您现在可以使用类型同义词来完全应用其他类型同义词而不会收到错误。



允许部分应用类型同义词会使类型推断不可判定,因此无法轻松在这个方向扩展类型系统。


We can define type synonyms with arguments and this works fine when used with actual types:

type MyType t = t String String

data Test a b = Test a b

f :: MyType Test
f = undefined

main = undefined

Compiling this results in no errors:

$ghc --make test.hs                                           
[1 of 1] Compiling Main             ( test.hs, test.o )                                          
Linking test ...   

However this doesn't work when Test is a type synonym:

type MyType t = t String String

data Test a b = Test a b

type Test' a b = Test a b

f :: MyType Test'
f = undefined

main = undefined

Which gives the following error:

$ghc --make test.hs
[1 of 1] Compiling Main             ( test.hs, test.o )

test.hs:7:6:
    Type synonym Test' should have 2 arguments, but has been given none
    In the type signature for `f': f :: MyType (Test')

What puzzles me is that Test' is being applied to two arguments, so why is GHC complaining that I didn't pass the arguments?

Shouldn't type synonym be completely transparent and impossible to distinguish from other kind of types?

Is there any way to achieve the expected behaviour?

解决方案

According to the Haskell report type synonyms cannot be partially applied:

Type constructor symbols T introduced by type synonym declarations cannot be partially applied; it is a static error to use T without the full number of arguments.

In particular this is checked before expanding the type synonyms, this means that in the expression:

MyType Test'

The check is performed before expanding MyType and hence Test' results partially applied.

However, it is possible to achieve that behaviour using GHC LiberalTypeSynonyms extension. This extension treats type synonyms as macros, which are expanded without any check and afterwards the type checker will see whether the type synonym was partially applied.

Kind inference is still done before type synonym expansion.

Note that type synonyms cannot be partially applied even in this case, i.e. something such as:

Test' Int

is still an error. However you now can use type synonyms to totally apply other type synonyms without receiving errors.

Allowing partial application of type synonyms would make type inference undecidible, so it's not possible to easily extend the type system in that direction.

这篇关于为什么不能(完全)应用一个使用其他类型同义词的参数的类型同义词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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