繁体   English   中英

我可以证明(forall x。Coercible(ax)(bx))隐含ab吗?

[英]Can I prove that (forall x. Coercible (a x) (b x)) implies Coercible a b?

我正在处理强制性证明:

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

我可以简单地证明Coercible ab => forall x. Coercible (ax) (bx)都是可Coercible ab => forall x. Coercible (ax) (bx) Coercible ab => forall x. Coercible (ax) (bx)

introduce :: (a ~=~ b) -> (forall x. a x ~=~ b x)
introduce IsCoercible = IsCoercible

但不是相反, (forall x. Coercible (ax) (bx)) => Coercible ab)并不是那么免费:

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’
-}

我可以肯定我的主张是正确的(尽管我很unsafeCoerce被驳斥),但是除了unsafeCoerce ,我对于如何在Haskell中进行证明还没有什么聪明的主意。

不,你不能。 正如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

您的版本更加注定了。 要对多态Coercion (或~=~ )参数进行模式匹配,必须将其实例化为特定类型。 GHC会将其实例化为f Any ~=~ g Any Any〜 f Any ~=~ g Any 〜g f Any ~=~ g Any ,然后它是单态的,因此不能证明您想要它做什么。 由于键入了GHC Core,因此不会运行。

旁注:我非常沮丧,因为没有办法写

f :: (forall a. c a :- d a)
  -> ((forall a. c a => d a) => r)
  -> r

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM