XTypeOperators extension doesn't work as pragma

后端 未结 3 719
清酒与你
清酒与你 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.

提交回复
热议问题