haskell中具有命题逻辑实现的不匹配类型

时间:2016-10-27 23:39:27

标签: haskell types syntax pattern-matching logic

我尝试使用以下代码在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正确匹配它,但它必须是我错过的东西。任何人都可以帮助我吗?

由于

1 个答案:

答案 0 :(得分:4)

您的问题是,您将Logical_value类型与构造函数TRUEFALSEINDETERMINATE以及{{1 } {},Logical_value类型的构造函数。你在整个过程中使用相同名称的事实掩盖了相当多的区别和问题。例如,Propositional_expression Logical_value的情况实际上并不包含Propositional_expression,因为它只是一个原子值。接下来是对类型的调整,以便它们具有更清晰的名称,并按照您希望的方式运行:

Logical_value

以下是-- I'm changing the names to CamelCase out of habit -- feel free not to. -- It will be considered three logical values. data LogicalValue = TRUE | FALSE | INDETERMINATE deriving (Show, Eq) -- A propositional symbol (variable) is a string. type PropositionalSymbol = String -- Four basics logicals connectives. data PropositionalConnective = 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 PropositionalExpression = ValueExpr LogicalValue | SymbolExpr PropositionalSymbol | NegExpr PropositionalExpression | ConnectiveExpr PropositionalExpression PropositionalConnective PropositionalExpression | ListExpr PropositionalConnective [PropositionalExpression] -- 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 = PropositionalSymbol -> LogicalValue 的样子。请注意构造函数(interpretationValueExpr等)如何用于选择每个等式处理的大小写。这就是您的类型错误背后的原因 - SymbolExprTRUEFALSE不是INDETERMINATE的构造函数,既不在原始代码中也不在我的修改版本中(由{方式,你是正确的怀疑它不是一个语法错误:原始代码中的语法是合法的,但它没有按预期执行,导致类型错误)。

PropositionalExpression