【发布时间】:2016-10-28 20:27:47
【问题描述】:
我正在尝试使用以下代码在 haskell 中实现命题逻辑:
module Propositional_Logic where
--Syntax
--It will be considered three logical values.
data Logical_value = TRUE | FALSE | INDETERMINATE deriving (Show, Eq)
--A propositional symbol (variable) is a string.
type Propositional_symbol = String
--Four basics logicals connectives.
data Propositional_connective = And
| Or
| Impl
| Dimpl
deriving (Show, Eq)
--A well-formed formula (propositional expression) can be a logical value, a propositional symbol, the negation of a well-formed formula, two well-formed formulas connected by a logical connective and the conjunction or disjunction of well-formed formulas.
data Propositional_expression = Logical_value
| Propositional_symbol
| Neg Propositional_expression
| Propositional_expression Propositional_connective Propositional_expression
| Propositional_connective [Propositional_expression] -- Semantic will throw error if connective is not And or Or.
deriving (Show, Eq)
--Semantic
--Type to assign logical values to propositional symbols.
type Interpretation = Propositional_symbol -> Logical_value
interpretationInit :: Interpretation
interpretationInit _ = INDETERMINATE
--Function to add interpretations for basic propositional symbols.
update :: Interpretation -> Interpretation -> Interpretation
update i1 i2 = (\x -> if i2 x == INDETERMINATE then i2 x else i1 x)
--Function to obtain the logical value from a propositional expression and the interpretation from basic propositional variables.
interpretation :: Propositional_expression -> Interpretation -> Logical_value
--Logical Values
interpretation (TRUE) i = TRUE
interpretation (FALSE) i = FALSE
interpretation (INDETERMINATE) i = INDETERMINATE
我遇到以下编译错误:
[1 of 1] Compiling Propositional_Logic ( propositional_logic.hs, interpreted )
propositional_logic.hs:36:21:
Couldn't match expected type ‘Propositional_expression’
with actual type ‘Logical_value’
In the pattern: TRUE
In an equation for ‘interpretation’: interpretation (TRUE) i = TRUE
propositional_logic.hs:37:21:
Couldn't match expected type ‘Propositional_expression’
with actual type ‘Logical_value’
In the pattern: FALSE
In an equation for ‘interpretation’:
interpretation (FALSE) i = FALSE
propositional_logic.hs:38:21:
Couldn't match expected type ‘Propositional_expression’
with actual type ‘Logical_value’
In the pattern: INDETERMINATE
In an equation for ‘interpretation’:
interpretation (INDETERMINATE) i = INDETERMINATE
Failed, modules loaded: none.
我认为我的语法是正确的:如果一个propositional_expression 可以是一个Logical_value,我希望Haskell 可以正确匹配它,但它一定是我缺少的东西。谁能帮帮我?
谢谢
【问题讨论】:
-
不太相关:仔细检查您的
update函数——我觉得它看起来不对,因为 then 分支总是返回INDETERMINATE。 -
请注意,我最初编写的答案中的代码存在相当明显的错误。如果您想知道为什么它不起作用,请检查更新的版本。
标签: haskell types syntax pattern-matching logic