具有类型族函数的约束类型列表长度

时间:2016-11-26 13:46:18

标签: haskell

编辑:我想我需要简化这个问题:

我正在使用generic-sop库。我需要一个约束'a有一个构造函数至少有2个字段'。我的功能看起来像这样:

myFunction :: (Code a ~ '[ f1 ': f2 ': rest ]) => a -> something

但是,我希望将这种约束作为“类型家庭”;即我想使用例如。

class (Code a ~ '[ f1 ': f2 ': rest) => Has2Records a
instance (Code a ~ '[ f1 ': f2 ': rest) => Has2Records a
myFunction :: Has2Records a => a -> something

不幸的是,它不起作用 - class中的变量应该显示为Has2Records的参数。

这简化了代码;另外,如果我可以做这样的事情,我可以在类型系列中使用它:

type family HowMany a r :: Constraint
type instance HowMany a Need1 = (Has1Record a)
type instance HowMany a Need2 = (Has2Records a)

class MyGreatClass a need where
  doSomething :: HowMany a need => a -> Something
instance MyGreatClass a Need1 where
  doSomething = (fneed1 :: (Code a ~ '[ f1 ': rest ]) => a -> Something)
instance MyGreatClass a Need2
  doSomething = (fneed1 :: (Code a ~ '[ f1 ': f2 ': rest ]) => a -> Something)

0 个答案:

没有答案