haskell——有什么方法可以推出你自己的语言编译指示组吗?

发布于 2024-12-28 07:35:48 字数 1248 浏览 5 评论 0原文

我有一个 Haskell 项目,经常使用很多语言功能,并且我希望每个源文件的语言扩展块都相同。这是一个列表,

{-# LANGUAGE Arrows,
             BangPatterns,
             DefaultSignatures,
             DeriveDataTypeable,
             DeriveFunctor,
             EmptyDataDecls,
             FlexibleContexts,
             FlexibleInstances,
             FunctionalDependencies,
             GADTs,
             GeneralizedNewtypeDeriving,
             MultiParamTypeClasses,
             NamedFieldPuns,
             NoImplicitPrelude,
             NoMonomorphismRestriction,
             OverlappingInstances,
             RankNTypes,
             RebindableSyntax,
             ScopedTypeVariables,
             StandaloneDeriving,
             TemplateHaskell,
             TypeFamilies,
             TypeOperators,
             TypeSynonymInstances,
             UndecidableInstances,
             ViewPatterns #-}

也许对某些人来说这是不好的做法,但我认为语言扩展是我通常编写代码的“Haskell+”的一部分。而且,我希望跨模块的情况相同。例如,NoImplicitPrelude 极大地改变了语言,我希望它对所有模块都是统一的。

问题:如何实现此目的,而不将语言块复制粘贴到每个文件中?我经常学习一种新的语言功能,将其添加到模块 A,然后开始处理模块 B,并意识到我必须从其中复制语言块,这很烦人模块A

仅供参考,带有 #includeCPP 编译指示并不能解决问题!提前致谢。

I have a Haskell project that regularly uses a lot of language features, and I want the language extension block for each source file to be the same. Here's a list,

{-# LANGUAGE Arrows,
             BangPatterns,
             DefaultSignatures,
             DeriveDataTypeable,
             DeriveFunctor,
             EmptyDataDecls,
             FlexibleContexts,
             FlexibleInstances,
             FunctionalDependencies,
             GADTs,
             GeneralizedNewtypeDeriving,
             MultiParamTypeClasses,
             NamedFieldPuns,
             NoImplicitPrelude,
             NoMonomorphismRestriction,
             OverlappingInstances,
             RankNTypes,
             RebindableSyntax,
             ScopedTypeVariables,
             StandaloneDeriving,
             TemplateHaskell,
             TypeFamilies,
             TypeOperators,
             TypeSynonymInstances,
             UndecidableInstances,
             ViewPatterns #-}

Maybe to some it's bad practice, but I consider language extensions to be part of the "Haskell+" that I usually write code in. And, I want that to be the same across modules. For example, the NoImplicitPrelude changes the language dramatically, and I want it uniform for all modules.

Question: How can I achieve this, without copy-pasting the language block into each file? It gets annoying how I often learn a new language feature, add it to module A, then start working on module B, and realize I have to go copy the language block from module A.

Just FYI the CPP pragma with a #include does not do the trick! Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铁轨上的流浪者 2025-01-04 07:35:48

使用 cabal 作为您的构建系统,并在 LibraryExtensions 字段或 Executable 部分列出您想要的语言扩展project.cabal 文件。然后从 Haskell 源文件中删除 LANGUAGE 块。

请参阅 Cabal 用户指南,包括简介的第三段。


Ghci 就是一切崩溃的地方。有人讨论添加一个cabal ghci命令,但同时它有点令人讨厌。

如果您的项目是一个库,您可以运行 ghci -package-conf dist/package.conf.inplace 。

如果您想在 ghci 中加载未公开的模块,我会在您的 project.cabal 中定义一个“开发模式”标志:

Flag development
  Description:          Development mode: expose all modules, enable warnings.
  Default:              False

...有条件地在开发模式下公开额外的模块:

Library
  Exposed-modules:      My.Module, My.Module.Extra
  if flag(development)
    Exposed-modules:    My.Module.Hidden, My.Module.Secret
    GHC-Options:        -Wall
  -- plus your extensions, etc

...并显式启用运行cabal configure时的开发模式:

$ cabal configure -f development

Use cabal as your build system, and list the language extensions you want in the Extensions field of the Library or Executable section of your project.cabal file. Then remove the LANGUAGE block from your Haskell source files.

See the Cabal User Guide, including the third paragraph of the introduction.


Ghci is where it all falls down. There is talk of adding a cabal ghci command, but in the meantime it's a bit icky.

If your project is a library, you can run ghci -package-conf dist/package.conf.inplace.

If you want to load unexposed modules in ghci, I'd define a "development mode" flag in your project.cabal:

Flag development
  Description:          Development mode: expose all modules, enable warnings.
  Default:              False

...conditionally expose extra modules in development mode:

Library
  Exposed-modules:      My.Module, My.Module.Extra
  if flag(development)
    Exposed-modules:    My.Module.Hidden, My.Module.Secret
    GHC-Options:        -Wall
  -- plus your extensions, etc

...and explicitly enable development mode when you run cabal configure:

$ cabal configure -f development
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文