不在范围内:数据构造函数

lpy*_*lpy 5 haskell

我写了一个程序haskell但是我从ghci得到了错误

这是源代码,我构建它,如果我有

p1 :: Prop
p1 = And (Var 'A') (Not (Var 'A'))
Run Code Online (Sandbox Code Playgroud)

它将显示A && ~A这是源代码

import Data.List
import Data.Char
data Prop = Const Bool | 
        Var Char | 
        Not Prop | 
        And Prop Prop | 
        Or Prop Prop | 
        Imply Prop Prop
        deriving Eq
instance Show Prop where
  show (Var Char) = show Char
  show (Not Prop) = "(~" ++ show Prop ++ ")"
  show (And Prop Prop) = "(" ++ show Prop ++ "&&" ++ show Prop ++ ")"
  show (Or Prop Prop) = "(" ++ show Prop "||" ++ show Prop ++ ")"
  show (Imply Prop Prop) = "(" ++ show Prop "=>" show Prop ++ ")"
Run Code Online (Sandbox Code Playgroud)

我从ghci得到两个主要错误...

Not in scope: data constructor `Char'
Not in scope: data constructor `Prop'
Run Code Online (Sandbox Code Playgroud)

我是haskell的初学者,非常感谢.

ehi*_*ird 5

以大写字母开头的值名称被保留用于构造,如Var,True,False,等变量必须以小写字母开头.

此外,您不能对两个不同的变量使用相同的名称.Haskell如何知道每次使用它们时你的意思?您不能简单地将构造函数的定义用作函数中的模式; 你需要为每个字段指定一个单独的名称.

所以,而不是Var Char,写Var name; 而不是Imply Prop Prop,写Imply p q(或Imply prop1 prop2),等等.