5

我有以下类表示类别,其中对象类由一种表示,并且每个 hom 类由由上述种类的类型索引的类型表示。

{-# LANGUAGE GADTs, DataKinds, KindSignatures, PolyKinds #-}

type Hom o = o -> o -> *

class GCategory (p :: Hom o)
  where
  gid :: p a a
  gcompose :: p b c -> p a b -> p a c

一个简单的实例示例是:

instance GCategory (->)
  where
  gid = id
  gcompose = (.)

现在我想为产品类别建模。作为一个简单的起点,这里有一个类型,它对对应于->with 自身乘积的类别的态射进行建模:

data Bifunction ab cd
  where
  Bifunction :: (a -> c) -> (b -> d) -> Bifunction '(a, b) '(c, d)

下面是相应的操作:

bifunction_id :: Bifunction '(a, a') '(a, a')
bifunction_id = Bifunction id id

bifunction_compose :: Bifunction '(b, b') '(c, c') -> Bifunction '(a, a') '(b, b') -> Bifunction '(a, a') '(c, c')
bifunction_compose (Bifunction f1 g1) (Bifunction f2 g2) = Bifunction (f1 . f2) (g1 . g2)

但是当我尝试将操作粘贴到类的实例中时:

instance GCategory Bifunction
  where
  gid = bifunction_id
  gcompose = bifunction_compose

我遇到以下问题:

• Couldn't match type ‘a’ with ‘'(a0, a'0)’
  ‘a’ is a rigid type variable bound by
    the type signature for:
      gid :: forall (a :: (*, *)). Bifunction a a
    at /tmp/ghc-mod29677/Bifunction29676-49.hs:28:3-5
  Expected type: Bifunction a a
    Actual type: Bifunction '(a0, a'0) '(a0, a'0)
• In the expression: bifunction_id
  In an equation for ‘gid’: gid = bifunction_id
  In the instance declaration for ‘GCategory Bifunction’
• Relevant bindings include
    gid :: Bifunction a a
      (bound at /tmp/ghc-mod29677/Bifunction29676-49.hs:28:3)

我相信消息的重要部分如下:

  Expected type: Bifunction a a
    Actual type: Bifunction '(a0, a'0) '(a0, a'0)

特别是它不能将 typeforall x y. Bifunction '(x, y) '(x, y)与 type统一起来forall (a :: (*, *)). Bifunction a a

剥离大部分领域特定的东西,我们只剩下以下问题的最小重现:

{-# LANGUAGE GADTs, DataKinds, KindSignatures, PolyKinds, RankNTypes #-}

module Repro where

data Bifunction ab cd
  where
  Bifunction :: (a -> c) -> (b -> d) -> Bifunction '(a, b) '(c, d)

bifunction_id :: Bifunction '(a, a') '(a, a')
bifunction_id = Bifunction id id

bifunction_id' :: Bifunction a a
bifunction_id' = bifunction_id

有没有办法可以bifunction_idbifunction_id'上面统一?


我尝试过的另一种方法是使用类型族,但这仍然不能完全解决问题:

{-# LANGUAGE GADTs, DataKinds, KindSignatures, PolyKinds, RankNTypes, TypeFamilies #-}

module Repro where

type family Fst (ab :: (x, y)) :: x
  where
  Fst '(x, y) = x

type family Snd (ab :: (x, y)) :: y
  where
  Fst '(x, y) = y

data Bifunction ab cd = Bifunction (Fst ab -> Fst cd) (Snd cd -> Snd cd)

bifunction_id :: Bifunction '(a, a') '(a, a')
bifunction_id = Bifunction id id

-- This still doesn't work
-- bifunction_id' :: Bifunction a a
-- bifunction_id' = bifunction_id

-- But now I can do this successfully
bifunction_id' :: Bifunction a a
bifunction_id' = Bifunction id id

但是我真的不明白为什么这个相同的表达式有效,并且宁愿不必在代码的其余部分中管理像这样的有点不明显的区别。

4

2 回答 2

3

forall (x :: k) (y :: l). p '(x, y)不如 普遍forall (a :: (k, l)). p a,主要是因为有些东西(k, l)不是成对的。

type family NotAPair :: () -> () -> () -> (k, l)

(请注意,类型族没有参数,它与 不同NotAPair (u :: ()) (v :: ()) (w :: ()) :: ())。如果NotAPair '() '() '() :: (k, l)实际上是一对'(,) x y,那么我们就会有这样的废话:'(,) ~ NotAPair '(), x ~ '(), y ~ '().

另请参阅使用不可能的类型计算https://gelisam.blogspot.com/2017/11/computing-with-impossible-types.html

即使“所有事物(k, l)都是成对的”,也有不同的方法可以使该事实在语言中可用。如果将其设为隐式,例如可以将 a 隐式转换forall x y. p '(x, y)为 a forall a. p a,则可以(或不可以)使类型检查无法确定。如果您明确表示,您将不得不努力编写该转换(例如 Coq)。

于 2019-09-22T14:09:56.457 回答
1

gid @Bifunction的定义中,您有一个 type a :: (Type, Type)(,)只有一个构造函数,所以我们可以推断出一定存在x :: Typey :: Type这样a ~ '(x, y). 然而,这种推理在 Haskell 中是无法表达的。基本上,当您在 Haskell 中有一个类型级别的对(某种类型的东西(i, j))时,您不能假设它实际上是一对(某种形式的东西'(x, y))。这会导致您的代码中断:您有Bifunction id id :: forall x y. Bifunction '(x, y) '(x, y), 但您需要 a Bifunction a a,而您只是没有输入规则让您假设a ~ (x, y)某些x, y。当您使用 的替代的,奇怪的定义时Bifunction,您会得到Bifunction id id :: forall a. Bifunction a a(因为这是构造函数的返回类型),它基本上可以工作,因为FstSnd是“部分”功能。

我个人只是添加“所有对实际上都是对”作为公理。

data IsTup (xy :: (i, j)) =
    forall (x :: i) (y :: j). xy ~ '(x, y) => IsTup
-- could also write
-- data IsTup (xy :: (i, j)) where
--     IsTup :: forall (x :: i) (y :: j). IsTup '(x, y)
isTup :: forall xy. IsTup xy
isTup = unsafeCoerce IsTup

bifunction_id :: Bifunction '(a, x) '(a, x)
bifunction_id = Bifunction id id
bifunction_compose :: Bifunction '(b, y) '(c, z) -> Bifunction '(a, x) '(b, y) -> Bifunction '(a, c) '(x, z)
bifunction_compose (Bifunction fl fr) (Bifunction gl gr) = Bifunction (fl . gl) (fr . gr)

instance GCategory Bifunction where
   gid :: forall a. Bifunction a a -- necessary to bind a
   -- usage of axiom: isTup produces a "proof" that a is actually a pair and
   -- matching against IsTup "releases" the two components and the equality 
   gid | IsTup <- isTup @a = bifunction_id
   gcompose :: forall a b c. Bifunction b c -> Bifunction a b -> Bifunction a c
   gcompose
     | IsTup <- isTup @a, IsTup <- isTup @b, IsTup <- isTup @c
     = bifunction_compose
于 2019-09-22T13:55:29.100 回答