Haskell布尔函数实现

时间:2012-08-29 12:56:51

标签: haskell boolean existential-type

我正在尝试解决以下问题 - 给定所有选择器(e ^ i_n)和一些布尔函数({f_1, f_2, f_n})枚举闭包中n个参数的所有函数(在[f_1,f_2,.. f_n]中)。

因此,我实现了BooleanFunctionClass和existencial BooleanFunction类型。 他们是Haskell的精神主义者吗?

class BooleanFunctionClass a where
  arity :: a -> Int

instance BooleanFunctionClass Bool where
  arity _ = 0

instance BooleanFunctionClass a => BooleanFunctionClass (Bool -> a) where
    arity f =  arity  (f True) + 1

data BooleanFunction = forall  a. (BooleanFunctionClass a) => BooleanFunction a String
instance Show BooleanFunction where
  show (BooleanFunction _ str) = show str

但我不知道实现选择器(n个参数的函数,返回k-th)。 我想要selector :: Int -> Int -> BooleanFunction。有什么建议吗?

PS。抱歉。我不知道,如何在Markdown中插入TeX。

1 个答案:

答案 0 :(得分:1)

我不确定你想要实现什么,但是如果你想在编译时检查arity,列表可能不会完成这项工作(正如你在评论中所建议的那样)。

你需要元组或类似的东西。处理可变大小元组的最好方法是Template Haskell。另外TupleTH已经为你做了很多关于以类型安全的方式处理元组的工作。