[英]Can a universal and existential quantifier for a constructor be commuted in pattern matching?
这是一个我认为(类型)有意义的片段,但 ghc 不喜欢它。 我希望对类型注释的一些棘手使用可以使它工作,但我的实验失败了。 有什么建议么?
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Ex where
data T a where
T :: Functor f => (f a -> a) -> T a
foo :: (forall a . T a) -> Bool
foo (T f) = bar f
bar :: Functor f => (forall a . f a -> a) -> Bool
bar _f = True
这很可能是不可能的,因为它相当于交换一个普遍存在的量词,但我希望如此。
我在 Agda 中尝试了这个例子,看看它是否有意义
module Comm where
open import Data.Bool using (Bool; true)
record T (A : Set) : Set₁ where
constructor MkT
field
F : Set → Set
f : F A → A
bar : {F : Set → Set} → (∀ A → F A → A) → Bool
bar _ = true
foo : (∀ A → T A) → Bool
foo k = bar {F = k Bool .T.F} λ A → {!k A .T.f!}
-- Goal: k Bool .T.F A → A
-- Have: T.F. (k A) A → A
在 foo 中,我们需要实例化forall a. T a
forall a. T a
有某种类型。 由于类型是参数化的,因此任何都应该这样做,并且F
将是相同的。 但是 Agda 和 Haskell 都没有内置该参数。
所以我认为正确使用unsafeCoerce
,事情会奏效。
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Ex where
import Unsafe.Coerce (unsafeCoerce)
import Data.List.NonEmpty (NonEmpty (..))
import GHC.Exts (Any)
data T a where
T :: Functor f => (f a -> a) -> T a
bar :: Functor f => (forall a . f a -> a) -> Bool
bar _f = _f `seq` True
foo :: (forall a . T a) -> Bool
foo t = foo' t t
foo' :: (forall a. T a) -> T Any -> Bool
foo' t (T f) = aux f where
aux :: forall f. Functor f => (f Any -> Any) -> Bool
aux f' = bar f''
where
f'' :: forall a. f a -> a
f'' = unsafeCoerce f'
--- test
ex :: forall a. T a
ex = T (\(x :| _) -> x)
ex2 :: Bool
ex2 = foo ex
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.