Out of scope: data constructor

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

Here are the sources, I’ll build it, and if I have

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

It will show A && ~A , so these are 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 have two main errors from ghci ...

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

I start with haskell, thank you very much.

+4
source share
2 answers

Capital names starting with a capital letter are reserved for constructors such as Var , True , False , etc. Variables must begin with a lowercase letter.

In addition, you cannot use the same name for two different variables. How did Haskell know which one you had in mind every time you used them? You cannot just use the constructor definition as a template in a function; you need to specify a separate name for each field.

So instead of Var Char write Var name ; instead of Imply Prop Prop , write Imply pq (or Imply prop1 prop2 ), etc.

+5
source

A little editing will make it work:

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

Source: https://habr.com/ru/post/1411046/


All Articles