标签: polykinds

RankNTypes和PolyKinds

f1和之间有什么区别f2

$ ghci -XRankNTypes -XPolyKinds
Prelude> let f1 = undefined :: (forall a        m. m a -> Int) -> Int
Prelude> let f2 = undefined :: (forall (a :: k) m. m a -> Int) -> Int
Prelude> :t f1
f1 :: (forall            (a :: k) (m :: k -> *). m a -> Int) -> Int
Prelude> :t f2
f2 :: (forall (k :: BOX) (a :: k) (m :: k -> *). m a -> …
Run Code Online (Sandbox Code Playgroud)

polymorphism haskell higher-rank-types polykinds

13
推荐指数
2
解决办法
1231
查看次数

我可以将此newtype实现为其他类型的组合吗?

我编写了一个Const3非常类似的newtype Const,但包含三个给定类型参数中的第一个:

newtype Const3 a b c = Const3 { getConst3 :: a }
Run Code Online (Sandbox Code Playgroud)

我可以为这个新类型定义很多有用的实例,但我必须自己完成所有这些.

但是,我在类型级别上应用的功能类似于该功能

\a b c -> a
Run Code Online (Sandbox Code Playgroud)

@pl告诉我相当于const . const.

双方(.)const具有匹配的NEWTYPE包装:ComposeConst.所以我想我能够写:

type Const3 = Compose Const Const
Run Code Online (Sandbox Code Playgroud)

并自动继承有用的实例,例如:

instance Functor (Const m)
instance (Functor f, Functor g) => Functor (Compose f g)
-- a free Functor instance for Const3!
Run Code Online (Sandbox Code Playgroud)

但GHC不同意:

const3.hs:5:23:
    Expecting one more argument to ‘Const’
    The first argument of …
Run Code Online (Sandbox Code Playgroud)

haskell type-theory category-theory type-kinds polykinds

12
推荐指数
1
解决办法
170
查看次数

什么是多态/多类元组?

当我在做haskell-excercises问题时。我看到以下代码Constraint通过将每种类型应用于 Constraint 构造函数来创建聚合。在 GHC 中,Constraints 的深层嵌套元组似乎仍然是一种Constraint(也许是扁平化的?)。

{-# LANGUAGE DataKinds     #-}
{-# LANGUAGE PolyKinds     #-}
{-# LANGUAGE TypeFamilies  #-}
{-# LANGUAGE TypeOperators #-}


type family All (c :: Type -> Constraint) (xs :: [Type]) :: Constraint where
  All c '[] = ()
  All c (x ': xs) = (c x, All c xs)

-- >>> :kind! All
-- All :: (* -> Constraint) -> [*] -> Constraint
-- = All

-- >>> :kind! …
Run Code Online (Sandbox Code Playgroud)

haskell type-families polykinds

9
推荐指数
1
解决办法
96
查看次数

多金属型应用是否是内射的?

多金属型应用是否是内射的?

当我们PolyKinds,我们知道f a ~ g b意味着f ~ ga ~ b

动机

试图回答另一个问题时,我将问题减少到只有PolyKinds启用时才收到以下错误.

Could not deduce (c1 ~ c)
from the context ((a, c z) ~ (c1 a1, c1 b))
Run Code Online (Sandbox Code Playgroud)

如果多金属型应用是单射的,我们可以推导c1 ~ c如下.

(a,   c z) ~ (c1 a1,   c1 b)
(a,) (c z) ~ (c1 a1,) (c1 b) {- switch to prefix notation -}
      c z  ~           c1 b  {- f a ~ g b …
Run Code Online (Sandbox Code Playgroud)

haskell higher-kinded-types type-kinds polykinds

7
推荐指数
2
解决办法
214
查看次数

是否有将约束应用于类型应用程序的一般方法?

用户2426021684评论让我调查是否有可能提出一个类型函数,以证明对于某些和:FF c1 c2 fafa

  1. fa ~ f a
  2. c1 f
  3. c2 a

事实证明,最简单的形式很容易.但是,我发现很难弄清楚如何编写多角度版本.幸运的是,当我写这个问题时,我设法找到了一种方法.

haskell typeclass type-families polykinds

7
推荐指数
1
解决办法
131
查看次数

在黑线鳕的实例列表中*(星号)或其他类型的含义是什么

浏览各种软件包的haddocks我经常会看到这样的实例文​​档(Control.Category):

Category k (Coercion k)
Category * (->)
Run Code Online (Sandbox Code Playgroud)

或者这个(Control.Monad.Trans.Identity):

MonadTrans (IdentityT *)
Run Code Online (Sandbox Code Playgroud)

这种签名究竟意味着什么呢?它没有出现在源代码中,但我已经注意到它似乎发生在使用PolyKinds扩展的模块中.我怀疑它可能像TypeApplication但有一种类型.因此,例如最后一个示例意味着IdentityT如果它的第一个参数具有类型,则它是monad变换器*.

所以我的问题是:

  • 我的解释是否正确以及签名所涉及的具体内容是什么?
  • 在第一个Category例子中,我怎么知道这k是一种而不是一种类型?或者我只是要知道这个Category
  • 这段语法的源代码是什么?

我不是要求各种解释.

haskell haddock type-kinds polykinds

6
推荐指数
1
解决办法
308
查看次数

GHC 无法推断未提升的类型

我遇到了由 Happy 生成的看似无效的代码。问题归结为 GHC 没有推断函数的多类类型签名。下面是一个例子:

\n\n
{-# Language MagicHash #-}\n\nf x = ()\n\nmain = pure (f 1#)\n
Run Code Online (Sandbox Code Playgroud)\n\n

由于 GHC 正在推断f :: a -> ()哪里a :: *,所以失败了

\n\n
 \xe2\x80\xa2 Couldn't match a lifted type with an unlifted type\n   When matching the kind of \xe2\x80\x98GHC.Prim.Int#\xe2\x80\x99\n \xe2\x80\xa2 In the first argument of \xe2\x80\x98f\xe2\x80\x99, namely \xe2\x80\x981#\xe2\x80\x99\n   In the first argument of \xe2\x80\x98pure\xe2\x80\x99, namely \xe2\x80\x98(f 1#)\xe2\x80\x99\n   In the expression: pure (f 1#)\n
Run Code Online (Sandbox Code Playgroud)\n\n

是否有任何语言编译指示我可以打开来编译此代码?我知道理论上我可以只添加类型签名,但是,由于这是由 Happy 生成的代码,我不希望手动修改任何内容。

\n

haskell types happy polykinds

5
推荐指数
1
解决办法
303
查看次数

使用PolyKinds和类型系列时有种歧义

我有两个类型系列,其中一个类型映射一种类型到另一种类型的不同类型和多态函数:

{-# LANGUAGE PolyKinds, TypeFamilies, FlexibleContexts, ScopedTypeVariables #-} 

type family F (a :: k1) :: k2
type family G (a :: k2) :: *

f :: forall k1 k2 (a :: k1) (p :: k1 -> *) . p (a :: k1) -> G (F (a :: k1) :: k2)
f = undefined
Run Code Online (Sandbox Code Playgroud)

此代码没有检查以下错误消息:

• Couldn't match type ‘G k20 (F k20 k1 a)’ with ‘G k2 (F k2 k1 a)’
  Expected type: p a -> G k2 (F k2 …
Run Code Online (Sandbox Code Playgroud)

haskell type-families polykinds

5
推荐指数
1
解决办法
84
查看次数

用元组类型统一多类量化变量

我有以下表示类别的类别,其中对象类别由一种种类表示,每个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
Run Code Online (Sandbox Code Playgroud)

一个实例的简单示例是:

instance GCategory (->)
  where
  gid = id
  gcompose = (.)
Run Code Online (Sandbox Code Playgroud)

现在,我要对产品类别进行建模。作为一个简单的起点,下面是一个类型,它对->与本身的乘积相对应的类别的词素进行建模:

data Bifunction ab cd
  where
  Bifunction :: (a -> c) -> (b -> d) -> Bifunction '(a, b) '(c, d)
Run Code Online (Sandbox Code Playgroud)

这是相应的操作:

bifunction_id :: Bifunction …
Run Code Online (Sandbox Code Playgroud)

haskell forall data-kinds polykinds

5
推荐指数
1
解决办法
101
查看次数

文件开头无用的类型相等错误

我收到一个错误

app\Main.hs:1:1: error:
    Couldn't match kind `*' with `Constraint'
    When matching types
      b :: *
      (Set b, Set s) :: Constraint
  |
1 | {-# LANGUAGE TypeFamilies #-}
  | ^

Run Code Online (Sandbox Code Playgroud)

不知道为什么b和约束(Set b, Set s)是匹配的?我希望约束能够对类型 b 进行存在量化,但为什么它会匹配它们呢?

I believe the last thing I changed before getting the error was adding OpOutcome to the class.

here is the code

{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeFamilyDependencies #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE DataKinds …
Run Code Online (Sandbox Code Playgroud)

haskell type-families type-kinds constraint-kinds polykinds

2
推荐指数
1
解决办法
83
查看次数

在 Haskell 中,如何重新排序多种类型

假设我有一种类型l :: * -> * -> *,所以我需要应用 2 种类型a,并b获得一个简单的类型l a b

如何将类型映射l :: * -> * -> *到新类型m(l) :: * -> * -> *,其中的m(l) b a含义与l a bfor each相同a,b?这里a,b不是常数。是否可以?这么想有错吗?

haskell polykinds

1
推荐指数
1
解决办法
87
查看次数