XTypeOperators extension doesn't work as pragma

后端 未结 3 749
清酒与你
清酒与你 2021-02-19 08:10

I\'m using GHCi 7.0.3 with the following program that implements type-level list:

{-# LANGUAGE TypeOperators #-}

data True
data False

-- List
data Nil
data Con         


        
相关标签:
3条回答
  • 2021-02-19 08:13

    The other answers are correct about enabling the extension in GHCi, either from the GHCi prompt or as a flag when starting GHCi. There is a third option, however--you can create a .ghci file that will be loaded and run every time you start GHCi, and use that to enable the extension automatically. Particularly for things like TypeOperators, where there's very little harm in having it enabled, it's nicely convenient.

    For example, here's what mine looks like right now:

    :set prompt "∀x. x ⊢ "
    :set -XTypeOperators
    import Control.Monad
    import Control.Applicative
    import Control.Arrow
    

    The .ghci file goes in whatever your system's standard location is for such files.


    To answer your extended question: The code in question works in GHCi roughly because it would also work if used in another module, which imported the module using the pragmas but didn't enable them itself. GHC is more than capable of enabling extensions on a per-module basis, even when the exported definitions couldn't possibly make sense without an extension, or have inferred types that require it.

    The distinction is a little fuzzy in GHCi because it also puts non-exported definitions from the module in scope, but in general anything that would work if used from another module will also work at the GHCi prompt.

    0 讨论(0)
  • 2021-02-19 08:21

    Your pragma is correct; the problem is that you're trying to use it from within GHCi, which doesn't inherit the extensions of the loaded module,1 but does pass the options it's given to GHC to compile the files you list (which is why it has the same effect as the pragma).

    You should keep the pragma, and either pass -XTypeOperators when you start GHCi, or enable it after loading the file as follows:

    GHCi> :set -XTypeOperators
    

    1 Which could be very undesirable, and probably impossible in many cases, for e.g. loading compiled modules.

    0 讨论(0)
  • 2021-02-19 08:35

    The LANGUAGE pragma works for the source file, it is not propagated to the ghci-prompt. Since it is possible to have conflicting pragmas in multiple source files of a project, source pragmas cannot by default propagate to the ghci-prompt. It would be possible to have the pragmas from *modules effective at the prompt, I'm not sure, but I think it is pondered to implement that, anyway, so far, it's not implemented, so you need to set the extensions for ghci explicitly.

    0 讨论(0)
提交回复
热议问题