Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not in scope: data constructor

Tags:

haskell

I wrote a program with haskell but I got errors from ghci

here is the source codes,I construct it, and if I have

p1 :: Prop
p1 = And (Var 'A') (Not (Var 'A'))

It will show A && ~A so that is the source codes

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 ++ ")"

And I got two main errors from ghci...

Not in scope: data constructor `Char'
Not in scope: data constructor `Prop'

I am a beginner with haskell,thankyou very much.

like image 884
lpy Avatar asked May 07 '12 02:05

lpy


1 Answers

Value names that start with an uppercase letter are reserved for constructors, like Var, True, False, etc. Variables must start with a lowercase letter.

Additionally, you can't use the same name for two different variables. How would Haskell know which one you meant each time you used them? You can't simply use the definition of a constructor as a pattern in a function; you need to give a separate name to each field.

So, instead of Var Char, write Var name; instead of Imply Prop Prop, write Imply p q (or Imply prop1 prop2), and so on.

like image 94
ehird Avatar answered Oct 02 '22 22:10

ehird