导入 haskell 模块说 "not in scope"

importing haskell module says "not in scope"

我在名为 Tree2.hs

的文件中创建了一个树结构
module Tree2
(
 Tree
) where

data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show)

然后我将其导入并尝试将其用作 class

的实例
import qualified Tree2

class YesNo a where
  yesno :: a -> Bool

instance YesNo (Tree2.Tree a) where
  yesno EmptyTree = False
  yesno _ = True

但是在 ghci 中加载它时出现此错误:

Not in scope: data constructor ‘EmptyTree’
Failed, modules loaded: Tree2.

有人知道为什么吗?

首先,

module Tree2
(
 Tree
) where

仅导出 Tree 数据类型,不导出其构造函数;你应该使用

module Tree2
(
  Tree(..)
) where

相反。

其次,在进行合格导入时,您需要使用 Tree2.EmptyTree 而不仅仅是 EmptyTree