Haskell / Agda中的类型级别集

时间:2013-08-26 13:08:49

标签: haskell dependent-type

我已经看到GHC的最新版本中支持类型级列表。但是,我需要为应用程序使用类型级别集,并且希望基于类型级别列表实现类型级别集合库。但我不知道从哪里开始:(

Haskell中是否有支持类型级别集的库?

1 个答案:

答案 0 :(得分:2)

您可以使用HSet来自HList包的HList属性:

{-# LANGUAGE FlexibleInstances #-}

import Data.HList

class (HList l, HSet l) => ThisIsSet l where
  -- Here we have @l@ which is @HList@ _and_ @HSet@.
  test :: l

-- This is ok:

instance ThisIsSet HNil where
  test = hNil

-- And this:

instance ThisIsSet (HCons HZero HNil) where
  test = hCons hZero hNil

-- And this (HZero != HSucc HZero):

instance ThisIsSet (HCons HZero (HCons (HSucc HZero) HNil)) where
  test = hCons hZero (hCons (hSucc hZero) hNil)

-- This is an error since HSucc HZero == HSucc HZero:

instance ThisIsSet (HCons (HSucc HZero) (HCons (HSucc HZero) HNil)) where
  test = hCons (hSucc hZero) (hCons (hSucc hZero) hNil)

要处理其他类型,您需要为它们编写HEq个实例。