Please tell me what is the problem?
data Stack' v = Stack' [v] Int deriving (Show) ... type StackInt = Stack' Int main = print(StackInt [1,2,3] 4)
The error i am getting is
Not in scope: data constructor `Stackint'
What is wrong?
(When GHC complains that a variable or function is "not in scope," it simply means that it has not yet seen a definition of it yet. As was mentioned before, GHC requires that variables and functions be defined before they are used.)
Data constructors are first class values in Haskell and actually have a type. For instance, the type of the Left constructor of the Either data type is: Left :: a -> Either a b. As first class values, they may be passed to functions, held in a list, be data elements of other algebraic data types and so forth.
Maybe is a type constructor because it is used to construct new types (the result type depends on the type of a in Maybe a ), where such a type might be Maybe Int (notice, there's no type param a anymore, i.e. all type parameters are bound).
IO is a type constructor, not a value constructor. IO True would be a type, not a value (if True was a type).
It looks to me like you are confusing the concepts of types and constructors, this is a common problem as they live in separate namespaces and are often given the same name. In the Haskell expression
data SomeType = SomeType Int
say, you are actually defining the type SomeType
and a constructor SomeType
. The type is not a function in the normal sense, but the constructor is. If you asked ghci for the type of SomeType you would get this:
:t SomeType
SomeType :: Int -> SomeType
Now, a type
declaration is just a shorthand for a longer type definition, in your case making StackInt
a synonym of Stack' Int
. But in order to construct a value of this type you still need to use the constructor Stack'
(which has type [v] -> Int -> Stack' v
). So your code should be
data Stack' v = Stack' [v] Int deriving (Show)
main = print(Stack' [1,2,3] 4)
If you wanted to be sure that the type was Stack' Int
then you could add a function
data Stack' v = Stack' [v] Int deriving (Show)
stackInt :: [Int] -> Int -> Stack' Int
stackInt list i = Stack' list i
main = print(stackInt [1,2,3] 4)
EDIT: Not also that I've written stackInt list i = Stack' list i
for transparency here, but you can write it more elegantly just as stackInt = Stack'
. It is the type constraint that makes sure that you get the right type here.
You could also have both the new function and the type synonym if you wanted, ie
data Stack' v = Stack' [v] Int deriving (Show)
type StackInt = Stack' Int
stackInt :: [Int] -> Int -> StackInt
stackInt list i = Stack' list i
main = print(stackInt [1,2,3] 4)
The name of the constructor is Stack'
, not StackInt
. Creating a type alias using type
does not create an alias for the constructors (which wouldn't make sense since there may be many constructors for the type and their names don't have to be related the type name at all).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With