What is the “coverage condition”?

一笑奈何 提交于 2019-12-07 00:37:21

问题


The source for the State transformer in mtl states:

-- ---------------------------------------------------------------------------
-- Instances for other mtl transformers
--
-- All of these instances need UndecidableInstances,
-- because they do not satisfy the coverage condition.

What is the "coverage condition"? All I can tell is that it has something to do with MTPCs and fundeps.


回答1:


Section 7.6.3.2 of the GHC manual tells us what the coverage condition is:

The Coverage Condition. For each functional dependency, tvsleft -> tvsright, of the class, every type variable in S(tvsright) must appear in S(tvsleft), where S is the substitution mapping each type variable in the class declaration to the corresponding type in the instance declaration.

In plain English, this means that if you have a type class with fundeps, for example:

class Convert a b | a -> b where
  convert :: a -> b

you can define the following instances:

instance Convert String String   -- no type variables
instance Convert [a]    [a]      -- type var a present on both sides
instance Convert (a,b)  a        -- a on the right => a on the left

but not the following instances:

instance Convert String a        -- a only present on the right
instance Convert a      (a,b)    -- b only present on the right



回答2:


It is defined in this paper by Simon Peyton-Jones. Definition 7 defines Coverage Condition. I would quote the exact definition but alas, I don't know how to reproduce the mathematical symbols here.



来源:https://stackoverflow.com/questions/11959764/what-is-the-coverage-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!