2012-08-29 104 views
1

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

所以,我实现了BooleanFunctionClass和existencial BooleanFunction类型。 他们是哈斯克尔的狂妄精神吗?

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个)。 我想要selector :: Int -> Int -> BooleanFunction。有什么建议么?

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

+1

将布尔函数建模为BooleanFunction {arity :: Int,f :: [Bool] - > Bool}'会更容易。 –

+0

我会尝试这种方法,但它不是数学风格。 – KAction

+0

你能举一个你想要它做什么的例子吗? – augustss

回答

1

我不确定你想要达到什么目的,但是如果你想在编译时检查arity,列表可能不会完成这项工作(正如你在评论中所建议的那样)。你需要元组或其他类似的元组。处理可变大小元组的最好方法是Template Haskell。另外TupleTH已经为您以类型安全的方式处理元组做了很多工作。

+0

编号元组不起作用,因为我无法部分应用。 我想避开TH,但似乎失去了希望。 – KAction