Haskell “哈斯克尔”;“进口合格”;及;不在范围内:数据构造函数“;

Haskell “哈斯克尔”;“进口合格”;及;不在范围内:数据构造函数“;,haskell,Haskell,我有一个类似这样的导入: import qualified Bioinformatics.DNA as DNA data DNA = A | C | G | T deriving (Eq, Ord, Show) . ├── Geometry │   └── Shape.hs └── Geometry.hs 从另一个类似以下内容的文件: import qualified Bioinformatics.DNA as DNA data DNA = A | C | G | T d

我有一个类似这样的导入:

import qualified Bioinformatics.DNA as DNA
data DNA = A | C | G | T
    deriving (Eq, Ord, Show)
.
├── Geometry
│   └── Shape.hs
└── Geometry.hs
从另一个类似以下内容的文件:

import qualified Bioinformatics.DNA as DNA
data DNA = A | C | G | T
    deriving (Eq, Ord, Show)
.
├── Geometry
│   └── Shape.hs
└── Geometry.hs
在模块RNA中的该函数中,其中:

module Bioinformatics.RNA
  ( RNA
  , fromDna
  ) where

import qualified Bioinformatics.DNA as DNA

data RNA = A | C | G | U
    deriving (Eq, Ord, Show)

fromDna :: DNA.DNA -> RNA
fromDna DNA.A = A
fromDna DNA.C = C
fromDna DNA.G = G
fromDna DNA.T = U
我收到错误:

/home/thibaud/code/bioinformatics/src/Bioinformatics/RNA.hs:46:9:
    Not in scope: data constructor ‘DNA.A’

/home/thibaud/code/bioinformatics/src/Bioinformatics/RNA.hs:47:9:
    Not in scope: data constructor ‘DNA.C’

/home/thibaud/code/bioinformatics/src/Bioinformatics/RNA.hs:48:9:
    Not in scope: data constructor ‘DNA.G’

/home/thibaud/code/bioinformatics/src/Bioinformatics/RNA.hs:49:9:
    Not in scope: data constructor ‘DNA.T’
你知道为什么吗? 谢谢

使用
(..)
导入数据类型的所有构造函数

import qualified Bioinformatics.DNA as DNA (DNA(..), A, C, G, T)
代码分解

编辑:让我们把你的代码分解一下,因为它不太地道

import qualified Bioinformatics.DNA as DNA (DNA(..), A, C, G, T)
这已经很奇怪了。通常情况下,人们进口合格或有选择的东西,而不是两者兼而有之。试试看:

import qualified Bioinfomatics.DNA as DNA
因此,让我们保留量化并删除符号的显式列表

现在,您可以使用:

fromDna :: DNA.DNA -> RNA
fromDna DNA.A = A
fromDna DNA.C = C
fromDna DNA.G = G
fromDna DNA.T = U
这种功能从DNA转化为RNA的说法。请注意,您的questino从未提供过RNA类型或构造函数-它们是否在您的代码中的某个地方?您发布的代码中的其余错误与RNA有关,请考虑如下:

import qualified Bioinformatics.RNA as RNA

fromDna :: DNA.DNA -> RNA.RNA
fromDna DNA.A = RNA.A
fromDna DNA.C = RNA.C
fromDna DNA.G = RNA.G
fromDna DNA.T = RNA.U

让我们想象一下,我们需要一个
Geometry.Shape
模块来对形状进行操作。首先,我们将创建一个名为
Geometry
的文件夹。注意大写字母
G
。在其中,我们将放置
Shape.hs
文件。文件将包含以下内容:

module Geometry.Shape
  ( Shape(Circle,Rectangle)
  , Point
  ) where

data Point = Point Float Float deriving (Show)
data Shape = Circle Point Float | Rectangle Point Point deriving (Show)
请注意,在定义点时,我们对数据类型和值构造函数使用了相同的名称

因此,假设我们需要
几何体
模块进行各种几何计算。我们将创建
Geometry.hs
文件,并将其放置在与
Geometry
目录相同的级别:

module Geometry (area) where

import qualified Geometry.Shape as Shape

area :: Shape.Shape -> Float
area (Shape.Circle _ r) = pi * r ^ 2
area (Shape.Rectangle (Shape.Point x1 y1) (Shape.Point x2 y2)) =
    (abs $ x2 - x1) * (abs $ y2 - y1)
文件结构现在应该如下所示:

import qualified Bioinformatics.DNA as DNA
data DNA = A | C | G | T
    deriving (Eq, Ord, Show)
.
├── Geometry
│   └── Shape.hs
└── Geometry.hs
好的,让我们检查一下
区域
函数。首先,我们需要在与
Geometry.hs
文件相同的目录中打开GHCi

$ ghci
GHCi, version 7.10.3: http://www.haskell.org/ghc/  :? for help
Prelude>
然后让我们加载
几何体
模块:

Prelude> :l Geometry.hs
[1 of 2] Compiling Geometry.Shape   ( Geometry/Shape.hs, interpreted )
[2 of 2] Compiling Geometry         ( Geometry.hs, interpreted )

Geometry.hs:7:24: Not in scope: data constructor ‘Shape.Point’

Geometry.hs:7:44: Not in scope: data constructor ‘Shape.Point’
Failed, modules loaded: Geometry.Shape.
哎呀!出了点问题。但是等等!请再次查看错误消息:

不在范围内:数据构造函数“Shape.Point”

简单地说,这意味着类型构造函数不在可访问范围内。因此,让我们通过如下更改
Shape.hs
文件来修复它:

module Geometry.Shape
  ( Shape(Circle,Rectangle)
  , Point(..)
  ) where
Prelude> :r
[1 of 2] Compiling Geometry.Shape   ( Geometry/Shape.hs, interpreted )
[2 of 2] Compiling Geometry         ( Geometry.hs, interpreted )
Ok, modules loaded: Geometry, Geometry.Shape.
请注意,我们将
更改为
点(…)
。重新加载模块,如下所示:

module Geometry.Shape
  ( Shape(Circle,Rectangle)
  , Point(..)
  ) where
Prelude> :r
[1 of 2] Compiling Geometry.Shape   ( Geometry/Shape.hs, interpreted )
[2 of 2] Compiling Geometry         ( Geometry.hs, interpreted )
Ok, modules loaded: Geometry, Geometry.Shape.
并测试它:

Prelude> area (Shape.Circle (Shape.Point 0 0) 24)
1809.5574
是的,它起作用了!我想提请您注意,我们使用
(..)
导入
数据类型的所有构造函数。这和写
点(点)
是一样的。类似地,我们可以使用
Shape(..)
而不是
Shape(圆形、矩形)
。有关更多信息,请参阅:

不导出数据类型的值构造函数会使它们更加抽象,从而隐藏其实现。而且,无论谁使用我们的模块,都不能与值构造函数进行模式匹配。这就是为什么之前我们在
几何体
模块中遇到编译错误


我希望这个例子能帮助你理解这些错误的原因

它不适用于DNA(…),我也有同样的错误…你确定吗?您现在应该会遇到不同的错误,例如
“不在作用域'A'中,或者您可能会遇到以下错误之一:'DNA.A'…”
。完成后,您可能会收到预期类型与实际类型DNA/RNA的类型错误。我更新了我的第一篇文章,修改了导入和RNA类型的定义。OP已经导入了
生物信息学.DNA
中的所有内容,所以该模块的导出列表一定有问题。@ReidBarton Right@Thibaud我看到你的RNA导出列表是
RNA
而不是
RNA(…)
,你在DNA中也做同样的事情吗?您需要导出构造函数。此外,这个问题是一个案例研究,说明为什么应该粘贴完整的示例而不是片段。。。;数据T
类型定义。值构造函数就足够了,只要您按照下面的答案导出和导入它们。