Haskell pragmas: OPTIONS_GHC vs LANGUAGE

I find myself in this area of ​​pragma a lot to get the GHC to build specific options:

{-# OPTIONS_GHC -XFlexibleInstances -XRankNTypes ... #-} 

But when I see other people using extensions, they always declare it like this:

 {-# LANGUAGE FlexibleInstances, RankNTypes, ... #-} 

However, when I upload files to GHCi that use the latter method, GHC always complains that I use unrecognised pragma and quickly fails.

Why does the GHC not accept the LANGUAGE pragma, and which of the two works best?


Note: my version of GHC has been updated: 7.8.3, but was 7.6. *, when did it happen.

+6
source share
1 answer

Using LANGUAGE pragmas is better. Instead of providing a list of arbitrary parameters, it is specific only to language extensions. It is also designed for portability between implementations, as stated in the GHC docs; LANGUAGE

... allows you to transfer language extensions in a portable way. It is assumed that all Haskell compilers support the LANGUAGE pragma with the same syntax, although not all extensions are supported by all compilers, of course. Instead of OPTIONS_GHC , use the LANGUAGE pragma, if possible. [Emphasis added]

The same language is displayed in documents completely from GHC 6.6 (Β§7.10.4) , when LANGUAGE was introduced upstream of GHC 7.10.1 (Β§7.22.1) , the current (at the time of writing) release.

A third way to specify language extensions is to declare them in the cabal file.

I also found that it is usual to use LANGUAGE pragma to declare used extensions for a single file, but using OPTIONS_GHC (at the file level) is usually used as a workaround (for example, to disable some warnings), People prefer to declare GHC options throughout the project ( using cabal) not for individual files, while for some reason language extensions are often used for each file.

Here are some suggestions why the LANGUAGE pragma cannot be recognized:

  • You have an old version of GHC (<6.6)
  • You do not declare it as a pragma of the file header. The file header pragma must precede the module keyword in the file. In other words, it should be at the very top of the file (although it may be preceded by other pragmas of the file header)
+12
source

Source: https://habr.com/ru/post/985434/


All Articles