我正在处理强制性证明:
data a ~=~ b where
IsCoercible :: Coercible a b => a ~=~ b
infix 0 ~=~
sym :: (a ~=~ b) -> (b ~=~ a)
sym IsCoercible = IsCoercible
instance Category (~=~) where
id = IsCoercible
IsCoercible . IsCoercible = IsCoercible
coerceBy :: a ~=~ b -> a -> b
coerceBy IsCoercible = coerce
Run Code Online (Sandbox Code Playgroud)
我可以证明 Coercible a b => forall x. Coercible (a x) (b x)
introduce :: (a ~=~ b) -> (forall x. a x ~=~ b x)
introduce IsCoercible = IsCoercible
Run Code Online (Sandbox Code Playgroud)
但不是相反,(forall x. Coercible (a x) (b x)) => Coercible a b)不是很免费:
eliminate :: (forall x. a x ~=~ b x) -> (a ~=~ b)
eliminate IsCoercible = IsCoercible
{-
• Could not deduce: Coercible a b
arising from a use of ‘IsCoercible’
from the context: Coercible (a x0) (b x0)
bound by a pattern with constructor:
IsCoercible :: forall k (a :: k) (b :: k).
Coercible a b =>
a ~=~ b,
in an equation for ‘eliminate’
-}
Run Code Online (Sandbox Code Playgroud)
我可以肯定地说,我的主张是正确的(尽管我很乐意被驳斥),但对于在Haskell内如何证明这一点,我没有什么聪明的主意unsafeCoerce。
不,你不能。正如Dominique Devriese和HTNW在其评论中暗示的那样,GHC完全不接受该推断。这个要求更高的版本无法编译:
{-# language QuantifiedConstraints, RankNTypes #-}
import Data.Coerce
import Data.Type.Coercion
eliminate :: (forall a. Coercible (f a) (g a)) => Coercion f g
eliminate = Coercion
Run Code Online (Sandbox Code Playgroud)
您的版本更加注定了。要对多态Coercion(或~=~)参数进行模式匹配,必须将其实例化为特定类型。GHC会将其实例化为f Any ~=~ g Any,然后变为,因此无法证明您想要的内容。由于键入了GHC Core,因此不会运行。
旁注:我非常沮丧,因为没有办法写
f :: (forall a. c a :- d a)
-> ((forall a. c a => d a) => r)
-> r
Run Code Online (Sandbox Code Playgroud)