Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

haskell -- any way to roll your own group of LANGUAGE pragmas?

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.

like image 716
gatoatigrado Avatar asked Jan 19 '12 08:01

gatoatigrado


1 Answers

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
like image 51
dave4420 Avatar answered Nov 12 '22 12:11

dave4420