将列表应用于输入的功能以检查重言式 [英] applying a list to an entered function to check for tautology
问题描述
我想在haskell中编写一个函数,该函数确定布尔函数(用ghci中的lambda表达式输入)是否是重言式. 输入应如下所示:
I want to write a function in haskell which determines whether a boolean function (entered with a lambda-expression in ghci) is a tautology or not. The input should look like this:
taut n (\[x..] -> ... == ...)
taut 3 (\[x,y,z] -> ((x||y)||z) == (x||(y||z)) )
我已经用
combinations n = replicateM n [True,False]
cmb n = concat (combinations n)
但是现在我需要一个函数,该函数接受列表中的这些元素并将其插入到随后输入的函数的n个变量中.
but now I need a function which takes these elements of the list and inserts them into the n variables in then entered function.
先谢谢您了:)
推荐答案
您的combinations
函数已经生成了一个列表,该列表的长度为n
,其中包含所有可能的True
和False
排列.您不需要使用concat
;结果已经包括您的函数的所有可能的条目.之所以如此,是因为您要检查的功能已经包含一个列表(以\[a,b,c...]
的形式).
Your combinations
function already produces a list of lists of length n
containing all possible permutations of True
and False
. You do not need to use concat
; the result already includes all possible entries to your function. This is the case because the function you're checking already expects a list (it's in the form of \[a,b,c...]
).
也就是说,对于fn
带有长度为3的列表,combinations 3
将为:
That is, for an fn
taking a list of length 3, combinations 3
would be:
[[True,True,True],[True,True,False],[True,False,True],[True,False,False],
[False,True,True],[False,True,False],[False,False,True],[False,False,False]]
此列表的每个元素都是一个 list ;您可以将它们直接传递给您要检查的功能(可能是重言式).鉴于上面的列表,您要做的就是尝试每个元素.
Each element of this list is a list; you can pass them into the function you're checking (the one that might be a tautology) directly. Given the list above, all you have to do is try each element.
编辑(尝试澄清一下):
EDIT (trying to clarify a bit):
您想要一个功能taut
,该功能采用类型为[Bool] -> Bool
的另一个功能,并确定该功能是否是重言式.这意味着taut
将具有类似Int -> ([Bool] -> Bool) -> Bool
的类型.假设您是这样开始的:
You want a function taut
that takes another function of type [Bool] -> Bool
and determines whether or not that function is a tautology. This means that taut
will have a type like Int -> ([Bool] -> Bool) -> Bool
. Let's say you start it like this:
taut :: Int -> ([Bool] -> Bool) -> Bool
taut n fn = ...
现在,n
是长度,fn
是函数.您的combinations
函数采用n
并将所有可能的有效 input 返回到fn
.请注意,fn
需要[Bool]
,而combinations n
是[[Bool]]
,这意味着每个元素都可以作为fn
的输入.知道了这一点,您要做的就是将fn
应用于combinations n
的每个元素,并查看结果是否始终相同.
Now, n
is the length and fn
is the function. Your combinations
function takes n
and returns every possible valid input to fn
. Note that fn
expects a [Bool]
and that combinations n
is a [[Bool]]
, which means each element can be an input to fn
. Knowing this, all you need to do is apply fn
to each element of combinations n
and see if the result is always the same.
在您的taut
函数中,您不必担心如何在测试的函数中分配变量.当您实际编写该函数时,如果采用格式\[x,y,z]->...
,x
,y
和z
的形式,则将由于模式匹配而在其中分配了该函数.
In your taut
function, you do not have to worry about how variables inside the function being tested are assigned. When you're actually writing that function, if it's in the form of \[x,y,z]->...
, x
, y
and z
are going to be assigned within it thanks to pattern matching.
这篇关于将列表应用于输入的功能以检查重言式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!