哈斯克尔初学者没有线索 [英] Haskell Beginner without a Clue
问题描述
类型变量=字符串
类型估值= [(Variable,Bool)]
数据Prop = Falsum - 一个矛盾,或
| Var变量 - 一个变量或
|非道具 - 对公式的否定,或者
|或支柱支柱 - 两个公式的分解,或
| Prop Prop - 两个公式的连接,或
| Imp Prop Prop - 以两个公式为条件。
deriving(Eq,Show)
例子=和p(或q(不是q))
vars :: Prop - > [变量]
vars = nub。 vars'
其中
vars'Falsum = []
vars'(Var v)= [v]
vars'(Not f)= vars'f
vars '(或fg)= vars'f ++ vars'g
vars'(and fg)= vars'f ++ vars'g
vars'(Imp fg)= vars'f ++ vars 'g
eval ::估价 - >支柱 - > Bool
eval val Falsum = False
eval val(Var v)= case(lookup v val)of
Nothing - >错误(Unbound variable:++ v)
只是t - > t
eval val(Not f)= not(eval val f)
eval val(或f g)=(eval val f)|| (eval val g)
eval val(And f g)=(eval val f)&& (eval val g)
eval val(Imp f g)= eval val(Or(Not f)g)
估值:: [变量] - > [估值]
估值[] = [[]]
估值(v:vs)=地图((v,真):) ds ++ map((v,False):) ds
where ds =估值vs
现在我必须编写一个模型函数,并且我计算出typeline必须是
models :: Prop - > [估价]
作为示例,必须返回评估为True的估值列表,即:
模型示例== [[(p,True)(q,True)],[(p,True)(q,False)]]
我知道vars返回变量列表,没有重复,在本例中为[p,q],并且将变量的结果传递给估值产生所有可能的应用结果列表对p和q都是真和假。到目前为止,我只能得到这个列表的第一个输出来评估使用evals函数。这是我的代码:
models :: Prop - > Bool
models form = eval v form其中(v:vs)=估值(变量形式)
<
不可以不可以匹配预期类型'[Bool]'与实际类型'Bool'
以下是我更改的代码:
models :: Prop - > [bool]
模型form = eval v form:eval vs form where(v:vs)=估值(变量形式)
理想情况下,我相信我会放弃eval结果,而不是将它们保留在列表中,只返回评估为True的估值。我只是坚持如何递归评估其余的vs
我相信一旦我可以使用evals函数评估列表中的所有元素,然后我可以添加那些使用某种形式的相等赋值评估为真的,如:
其中finalList == True =
唉,这甚至看起来都不正确。
任何对我逻辑的帮助都会有所帮助。哦,并解释如何我可以递归评估列表的其余部分将不胜感激。
我要去假设你想要 models :: Prop - > [估价]
就像你开始写的一样。模型将返回满足命题的每个 Valuation
。从接近你的东西开始
models form =估值(变量形式)
让我们到达那里;它有正确的类型, Prop - > [评估]
。这只是列出 form
中变量的每个估值
。我们想要的只是那些满足命题的结果。这些可以由您的 eval
函数确定。
models :: Prop - > [估值]
模型form =过滤器满足(估值(变量形式))
其中满足val = eval val形式
为了运行这个,我们需要修改你的例子以便编译它,或者添加一些我自己的例子:
example = And(Varp)(Or(Varq)(Not(Varq)))
main = do
print $ models Falsum
print $ models
print $ models $ And(Varp)(Not(Varp))
print $ models $ Or(Varp) (不是(Varp))
这个输出
[]
[[(p,True),(q,True)],[(p,True) q,False)]]
[]
[[(p,True)],[(p,False)]]
现在,我们可能还需要一个函数来检查是否存在任何
可满足:: Prop - > Bool
我们可以用模型
可以满足:: Prop - > Bool
满足=不。空值 。模型
对于相同的四个示例
<$ p $(变量p)(Not(Varp)$ b $ print $可读的例子$ b $ print $可变的$ ))
print $ satisfiable $或(Varp)(Not(Varp))
此输出
False
True
False
True
It took me a while to even work out what this question was looking for. I have to return only those propositions that give an overall evaluation of True. Our example is And p (Or q (Not q)) and I know that p has to be True and q can be either True or False. To achieve our goal we have been given a few functions to start with.
type Variable = String
type Valuation = [(Variable, Bool)]
data Prop = Falsum -- a contradiction, or
| Var Variable -- a variable, or
| Not Prop -- a negation of a formula, or
| Or Prop Prop -- a disjunction of two formulae, or
| And Prop Prop -- a conjunction of two formulae, or
| Imp Prop Prop -- a conditional of two formulae.
deriving (Eq, Show)
example = And p (Or q (Not q))
vars :: Prop -> [Variable]
vars = nub . vars'
where
vars' Falsum = []
vars' (Var v) = [v]
vars' (Not f) = vars' f
vars' (Or f g) = vars' f ++ vars' g
vars' (And f g) = vars' f ++ vars' g
vars' (Imp f g) = vars' f ++ vars' g
eval :: Valuation -> Prop -> Bool
eval val Falsum = False
eval val (Var v) = case (lookup v val) of
Nothing -> error ("Unbound variable: " ++ v)
Just t -> t
eval val (Not f) = not (eval val f)
eval val (Or f g) = (eval val f) || (eval val g)
eval val (And f g) = (eval val f) && (eval val g)
eval val (Imp f g) = eval val (Or (Not f) g)
valuations :: [Variable] -> [Valuation]
valuations [] = [[]]
valuations (v:vs) = map ((v,True):) ds ++ map ((v,False):) ds
where ds = valuations vs
I now have to write a models function and I worked out that the typeline has to be
models :: Prop -> [Valuations]
as my example must return the list of Valuations that are evaluate to True which is: models example == [[("p",True)("q",True)],[("p",True)("q",False)]]
I know that vars returns the list of variables without duplicates, in this case ["p","q"], and that passing the result from vars into valuations produces the list of all possible outcomes of applying True and False to both "p" and "q". So far I can only get the first output of this list to evaluate using the evals function. Here's my code:
models :: Prop -> Bool
models form = eval v form where (v:vs) = valuations (vars form)
I have tried to change the code to evaluate the rest of vs but I keep getting an error message:
Couldn't match expected type '[Bool]' with actual type 'Bool'
Here's my altered code:
models :: Prop -> [Bool]
models form = eval v form : eval vs form where (v:vs) = valuations (vars form)
Ideally I believe I would want to discard the eval results rather than keep them in a list and only return those valuations which evaluate to True. I'm just stuck on how to recursively evaluate the rest of vs.
I believe once I can evaluate all elements in my list using the evals function then I can just add the ones that evaluate to True using some form of equality assignment, such as:
where finalList == True =
Alas, that doesn't even look close to being correct.
Any help with my logic would be helpful. Oh and an explanation as to how I can recursive evaluate the rest of the list would be appreciated.
I'm going to assume you want models :: Prop -> [Valuation]
like you started to write. Models will return every Valuation
that satisfies the proposition. Starting with something close to what you had
models form = valuations (vars form)
gets us halfway there; it has the correct type, Prop -> [Valuation]
. This just lists every Valuation
for the variables in form
. What we'd like is to have only those results which satisfy the proposition. These can be determined by your eval
function.
models :: Prop -> [Valuation]
models form = filter satisfies (valuations (vars form))
where satisfies val = eval val form
To run this, we need to fix your example so it compiles, and maybe add a couple examples of my own:
example = And (Var "p") (Or (Var "q") (Not (Var "q")))
main = do
print $ models Falsum
print $ models example
print $ models $ And (Var "p") (Not (Var "p"))
print $ models $ Or (Var "p") (Not (Var "p"))
This outputs
[]
[[("p",True),("q",True)],[("p",True),("q",False)]]
[]
[[("p",True)],[("p",False)]]
Now, we might also want a function that checks to see if there exists any Valuation
that satisfies the proposition. This would take a proposition and return a boolean.
satisfiable :: Prop -> Bool
We can easily write this in terms of models
satisfiable :: Prop -> Bool
satisfiable = not . null . models
For the same four examples
main = do
print $ satisfiable Falsum
print $ satisfiable example
print $ satisfiable $ And (Var "p") (Not (Var "p"))
print $ satisfiable $ Or (Var "p") (Not (Var "p"))
This outputs
False
True
False
True
这篇关于哈斯克尔初学者没有线索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!