lpy
lpy

Reputation: 587

Not in scope: data constructor

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.

Upvotes: 5

Views: 8783

Answers (2)

gcbenison
gcbenison

Reputation: 11963

A slight edit will get it working:

instance Show Prop where
  show (Var c) = [c]
  show (Not p) = "(~" ++ show p ++ ")"
  show (And p1 p2) = "(" ++ show p1 ++ " && " ++ show p2 ++ ")"
  show (Or p1 p2) = "(" ++ show p1 ++ "||" ++ show p2 ++ ")"
  show (Imply p1 p2) = "(" ++ show p1 ++ "=>" ++ show p2 ++ ")"

Upvotes: 3

ehird
ehird

Reputation: 40797

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.

Upvotes: 5

Related Questions