[英]Haskell Boolean Function implementation
我正在尝试解决以下问题-给定所有选择器(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个)。 我想要selector :: Int -> Int -> BooleanFunction
。 有什么建议么?
PS。 抱歉。 我不知道如何在Markdown中插入TeX。
我不确定您要实现的目标到底是什么,但是如果您希望在编译时检查Arity,则列表可能无法完成任务(如您在注释中所建议)。
您将需要元组或类似的东西。 处理可变大小的元组的最好方法是Template Haskell。 TupleTH还为您完成了有关以一种类型安全的方式处理元组的大量工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.