Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell error Not in scope: data constructor

Tags:

haskell

I wrote some simple module in Haskell and then import it in other file. Then I'm trying to use functions with data constructors from my module — there is an error Not in scope: data constructor: <value>. How can I fix it?

Note: when I'm using it in interpreter after importing — all is good without errors.

My module Test.hs:

module Test (test_f) where
data Test_Data = T|U|F deriving (Show, Eq)

test_f x
    | x == T = T
    | otherwise = F

And my file file.hs:

import Test

some_func = test_f

No error if I'm writing in interpreter:

> :l Test
> test_f T
T

In interpreter I'm trying to execute some_func T, but there is an error. And how can I use class Test_Data in my file to describe annotations?

like image 859
zerospiel Avatar asked Mar 05 '14 14:03

zerospiel


2 Answers

You aren't exporting it from your module:

module Test (test_f, Test_Data(..)) where

The (..) part says "export all constructors for TestData".

like image 71
bheklilr Avatar answered Oct 18 '22 20:10

bheklilr


You have an explicit export list in your module Test:

module Test (test_f) where

The export list (test_f) states that you want to export the function test_f and nothing else. In particular, the datatype Test_Data and its constructors are hidden.

To fix this, either remove the export list like this:

module Test where

Now all things will be exported.

Or add the datatype and its constructors to the export list like this:

module Test (test_f, Test_Data(..)) where

The notation Test_Data(..) exports a datatype with all corresponding constructors.

like image 36
kosmikus Avatar answered Oct 18 '22 22:10

kosmikus