简体   繁体   English

什么 GHC/Haskell 规范说自由类型构造函数匹配最右边的类型?

[英]What GHC/Haskell specification says that free type constructors match rightmost types?

I was caught off guard recently when I tried to pass a constructor for a type of kind * -> * -> * , with one bound type var, to a function expecting a constructor for * -> * .最近,当我试图将带有一个绑定类型 var 的类型* -> * -> *的构造函数传递给期望* -> *的构造函数的 function 时,我措手不及。 Concretely, it was along the lines of passing (\x -> (x, 42)):: (forall a. a -> (a, Int)) to a function of type forall c. (forall a. a -> c a) ->...具体来说,它是沿着将(\x -> (x, 42)):: (forall a. a -> (a, Int))传递到forall c. (forall a. a -> c a) ->... forall c. (forall a. a -> c a) ->... . forall c. (forall a. a -> c a) ->... This is ambiguous but not an error in GHC: (,) cast to * -> * could be interpreted as a constructor for either the left or righthand argument, and GHC appears to just default to the righthand argument.这是模棱两可的,但在 GHC 中不是错误: (,)强制转换为* -> *可以解释为左侧或右侧参数的构造函数,而 GHC 似乎只是默认为右侧参数。 For higher-kinded matches, it'll take the right most arguments.对于更高种类的匹配,它将采用右边的 arguments。 To see this, just test the inferred type:要看到这一点,只需测试推断的类型:

foo :: c a b -> a -> b -> ()
foo _ _ _ = ()

bar = foo ((), 42, 'a') 42 'a' -- typechecks

I wrongly believed instead that it would match the free variables in order, but this higher-rank case is the only time where that would obviously be preferred, and other times it's a wash.相反,我错误地认为它会按顺序匹配自由变量,但这种更高等级的情况是唯一明显首选的情况,而其他时候则是洗牌。 Is there official documentation describing this rule?是否有描述此规则的官方文档? I'm slightly annoyed but also understanding of the fact that this is not an error, because I can foresee that saves wrapping a lot of things in newtypes.我有点恼火,但也理解这不是错误的事实,因为我可以预见这可以节省在新类型中包装很多东西。

Unless I misunderstand the question, this simply follows from type application being left-associative, just like function application.除非我误解了这个问题,否则这只是因为类型应用程序是左关联的,就像 function 应用程序一样。

(a, b) is (,) ab is ((,) a) b . (a, b)(,) ab((,) a) b So (Int, a) is ((,) Int) a , but (a, Int) isn't <something> a .所以(Int, a)((,) Int) a ,但(a, Int)不是<something> a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Haskell GHC:与 N 个构造函数匹配的模式的时间复杂度是多少? - Haskell GHC: what is the time complexity of a pattern match with N constructors? 在Haskell(GHC)中使用什么算法来导出递归表达式的类型? - What algorithm is used in Haskell (GHC) for deriving types of recursive expressions? 什么: - &gt;表示haskell类型规范 - What does :-> mean in a haskell type specification 在GHC Haskell中键入抽象 - Type abstraction in GHC Haskell 在haskell中记录具有多个构造函数的类型 - Record types with multiple constructors in haskell Haskell / GHC:匹配具有相同模式的多个一元构造函数 - Haskell/GHC: Matching multiple unary constructors with the same pattern Haskell Wreq - 无法匹配预期类型“GHC.Exts.Item a0” - Haskell Wreq - Couldn't match expected type ‘GHC.Exts.Item a0’ 为什么在值构造函数中声明的类型不是Haskell中的类型? - Why are types declared in value constructors not types in Haskell? 调试:无法将预期类型“GHC.Types.Bool”与实际类型“Bool”匹配 - Debug: Couldn't match expected type ‘GHC.Types.Bool’ with actual type ‘Bool’ 在GHC和Haskell函数应用程序中调试类型错误 - Debugging type errors in GHC and Haskell function application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM