【问题标题】:How to use a data type in a haskell package如何在 haskell 包中使用数据类型
【发布时间】:2017-10-24 08:01:18
【问题描述】:

我有三个文件。第一个:

module AST where

data Prog a b = ...

第二个

module ParseProg where

import qualified AST as A

progToAST :: String -> A.Prog String String

这是用于家庭作业的包的一部分。我安装了它

$ cabal configure
$ cabal build 
$ cabal install

这似乎奏效了。我的第三个程序(测试)是

import ParseProg

val1 = progToAST "x"

-- prog :: A.Prog String String

fun1 :: Int -> Int
fun1 x = 2

-- val2 = fun1 val1

所以导入工作。如果我取消注释 prog,我会得到 ​​p>

Not in scope: type constructor or class ‘A.Prog’

如果我尝试 Prog 或 AST.Prog,也是一样。检查 val1 获取的类型

> :type val1
> val1 :: CabParser-0.1.0.0:AST.Prog String String

同样取消注释 val2 给出

Couldn't match expected type ‘Int’
            with actual type ‘CabParser-0.1.0.0:AST.Prog String String’

复制 CabParser-0.1.... 会出现解析错误。如何在 AST 中使用 Prog 数据类型?

【问题讨论】:

  • 第三个程序中没有import qualified AST as A
  • 现在我得到"Could not find module ‘AST’ it is a hidden module in the package ‘CabParser-0.1.0.0@CabPa_<nonsense>
  • 您必须在您的.cabal 文件中为CabParser 列出它。我不记得字段名称,但 ParseProg 已经列在其中了。
  • 是的,没错。
  • 太好了,这行得通。如果您将其写成答案,我会接受。否则我可以做到。

标签: haskell


【解决方案1】:

Ryan 在 cmets 中解决了这个问题。我在这里写解决方案。

第一个问题是我没有在第三个文件中导入AST,所以变成了:

import ParseProg
import AST as A

但是,这会产生错误

"Could not find module ‘AST’ it is a hidden module in the package ‘CabParser-0.1.0.0@CabPa_<nonsense>"

由于某种原因,AST 是我正在使用的包 CabParser 中的一个隐藏模块,因此我必须将其设为公开模块。在包目录下的 CabParser.cabal 中,我将 AST 添加到暴露的模块中:

exposed-modules:     ParseProg, AST

并将其从 other-modules 行中删除。然后我再次构建并安装

$ cabal configure
$ cabal build 
$ cabal install

然后一切正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多