繁体   English   中英

Haskell 中的依赖类型

[英]Dependent Types in Haskell

以下内容在 Haskell 中不起作用-

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

import Data.Kind

data Payload :: (f :: a -> Type) -> (e :: a) -> Type where
  MkPayload :: (e :: a) -> (t :: f e) -> Payload f e

payload :: Payload f e -> f e
payload (MkPayload e t) = t
    • Expecting one more argument to ‘f :: a -> Type’
      Expected a type, but ‘f :: a -> Type’ has kind ‘a -> Type’
    • In the kind ‘(f :: a -> Type) -> (e :: a) -> Type’
      In the data type declaration for ‘Payload’
  |
6 | data Payload :: (f :: a -> Type) -> (e :: a) -> Type where
  |                  ^^^^^^^^^^^^^^

还有其他方法可以在 Haskell 中定义依赖类型吗?

您不能像在 Agda 中习惯的那样在类型中使用_:: _符号。 相反,只留下名字,只写类型:

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

import Data.Kind ( Type )

data Payload :: (a -> Type) -> a -> Type where
  MkPayload :: a -> f e -> Payload f e

payload :: Payload f e -> f e
payload (MkPayload e t) = t

不鼓励使用 CUSK(完整的用户指定种类)符号,而应该使用独立的种类签名:

{-# LANGUAGE GADTs, DataKinds, TypeFamilies, UndecidableInstances,
             RankNTypes, PolyKinds, StandaloneKindSignatures #-}

import Data.Kind ( Type )

type Payload :: (a -> Type) -> a -> Type
data Payload f e where
  MkPayload :: a -> f e -> Payload f e

payload :: Payload f e -> f e
payload (MkPayload e t) = t

省略名称确实意味着您失去了一些表现力,但这不是本示例所必需的。 有一些技术可以恢复大部分表现力,例如 David Young 提到的单例。

暂无
暂无

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

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