Extension.hs 29 KB
Newer Older
1
{-# LANGUAGE DeriveDataTypeable #-}
ttuegel's avatar
ttuegel committed
2
3
{-# LANGUAGE DeriveGeneric #-}

4
5
6
7
-----------------------------------------------------------------------------
-- |
-- Module      :  Language.Haskell.Extension
-- Copyright   :  Isaac Jones 2003-2004
8
-- License     :  BSD3
9
--
Duncan Coutts's avatar
Duncan Coutts committed
10
-- Maintainer  :  libraries@haskell.org
11
12
-- Portability :  portable
--
13
-- Haskell language dialects and extensions
14
15

module Language.Haskell.Extension (
16
17
18
        Language(..),
        knownLanguages,

19
        Extension(..),
20
        KnownExtension(..),
21
        knownExtensions,
22
        deprecatedExtensions
23
24
  ) where

25
26
27
28
import Distribution.Text (Text(..))
import qualified Distribution.Compat.ReadP as Parse
import qualified Text.PrettyPrint as Disp
import qualified Data.Char as Char (isAlphaNum)
29
import Data.Array (Array, accumArray, bounds, Ix(inRange), (!))
ttuegel's avatar
ttuegel committed
30
import Data.Binary (Binary)
31
32
import Data.Data (Data)
import Data.Typeable (Typeable)
ttuegel's avatar
ttuegel committed
33
import GHC.Generics (Generic)
34

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
-- ------------------------------------------------------------
-- * Language
-- ------------------------------------------------------------

-- | This represents a Haskell language dialect.
--
-- Language 'Extension's are interpreted relative to one of these base
-- languages.
--
data Language =

  -- | The Haskell 98 language as defined by the Haskell 98 report.
  -- <http://haskell.org/onlinereport/>
     Haskell98

  -- | The Haskell 2010 language as defined by the Haskell 2010 report.
  -- <http://www.haskell.org/onlinereport/haskell2010>
  | Haskell2010

  -- | An unknown language, identified by its name.
  | UnknownLanguage String
ttuegel's avatar
ttuegel committed
56
57
58
  deriving (Generic, Show, Read, Eq, Typeable, Data)

instance Binary Language
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

knownLanguages :: [Language]
knownLanguages = [Haskell98, Haskell2010]

instance Text Language where
  disp (UnknownLanguage other) = Disp.text other
  disp other                   = Disp.text (show other)

  parse = do
    lang <- Parse.munch1 Char.isAlphaNum
    return (classifyLanguage lang)

classifyLanguage :: String -> Language
classifyLanguage = \str -> case lookup str langTable of
    Just lang -> lang
    Nothing   -> UnknownLanguage str
  where
    langTable = [ (show lang, lang)
                | lang <- knownLanguages ]

79
80
81
82
-- ------------------------------------------------------------
-- * Extension
-- ------------------------------------------------------------

83
-- Note: if you add a new 'KnownExtension':
84
85
--
-- * also add it to the Distribution.Simple.X.languageExtensions lists
86
--   (where X is each compiler: GHC, JHC, LHC, UHC, HaskellSuite)
87
--
88
89
90
-- | This represents language extensions beyond a base 'Language' definition
-- (such as 'Haskell98') that are supported by some implementations, usually
-- in some special mode.
91
92
--
-- Where applicable, references are given to an implementation's
93
-- official documentation.
94
95

data Extension =
96
  -- | Enable a known extension
97
98
    EnableExtension KnownExtension

99
100
101
  -- | Disable a known extension
  | DisableExtension KnownExtension

102
103
104
105
  -- | An unknown extension, identified by the name of its @LANGUAGE@
  -- pragma.
  | UnknownExtension String

106
  deriving (Generic, Show, Read, Eq, Ord, Typeable, Data)
ttuegel's avatar
ttuegel committed
107
108

instance Binary Extension
109
110
111

data KnownExtension =

112
113
114
115
  -- | Allow overlapping class instances, provided there is a unique
  -- most specific instance for each use.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/type-class-extensions.html#instance-overlap>
116
    OverlappingInstances
117

118
119
120
121
122
123
  -- | Ignore structural rules guaranteeing the termination of class
  -- instance resolution.  Termination is guaranteed by a fixed-depth
  -- recursion stack, and compilation may fail if this depth is
  -- exceeded.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/type-class-extensions.html#undecidable-instances>
ijones's avatar
ijones committed
124
  | UndecidableInstances
125

126
127
128
129
130
131
  -- | Implies 'OverlappingInstances'.  Allow the implementation to
  -- choose an instance even when it is possible that further
  -- instantiation of types will lead to a more specific instance
  -- being applicable.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/type-class-extensions.html#instance-overlap>
ijones's avatar
ijones committed
132
  | IncoherentInstances
133

134
  -- | /(deprecated)/ Allow recursive bindings in @do@ blocks, using the @rec@
135
  -- keyword. See also 'RecursiveDo'.
136
137
  | DoRec

138
  -- | Allow recursive bindings using @mdo@, a variant of @do@.
139
140
141
  -- @DoRec@ provides a different, preferred syntax.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#recursive-do-notation>
142
  | RecursiveDo
143

144
145
146
147
148
  -- | Provide syntax for writing list comprehensions which iterate
  -- over several lists together, like the 'zipWith' family of
  -- functions.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#parallel-list-comprehensions>
149
  | ParallelListComp
150

151
152
153
  -- | Allow multiple parameters in a type class.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/type-class-extensions.html#multi-param-type-classes>
154
  | MultiParamTypeClasses
155

156
157
158
  -- | Enable the dreaded monomorphism restriction.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/monomorphism.html>
159
  | MonomorphismRestriction
160

161
162
163
164
165
166
167
  -- | Allow a specification attached to a multi-parameter type class
  -- which indicates that some parameters are entirely determined by
  -- others. The implementation will check that this property holds
  -- for the declared instances, and will use this property to reduce
  -- ambiguity in instance resolution.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/type-class-extensions.html#functional-dependencies>
168
  | FunctionalDependencies
169

170
171
172
173
  -- | Like 'RankNTypes' but does not allow a higher-rank type to
  -- itself appear on the left of a function arrow.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/other-type-extensions.html#universal-quantification>
ijones's avatar
ijones committed
174
  | Rank2Types
175

176
177
178
179
  -- | Allow a universally-quantified type to occur on the left of a
  -- function arrow.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/other-type-extensions.html#universal-quantification>
180
  | RankNTypes
181

182
183
184
185
  -- | Allow data constructors to have polymorphic arguments.  Unlike
  -- 'RankNTypes', does not allow this for ordinary functions.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/other-type-extensions.html#universal-quantification>
186
  | PolymorphicComponents
187

188
189
190
  -- | Allow existentially-quantified data constructors.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/data-type-extensions.html#existential-quantification>
191
  | ExistentialQuantification
192

193
194
  -- | Cause a type variable in a signature, which has an explicit
  -- @forall@ quantifier, to scope over the definition of the
195
  -- accompanying value declaration.
196
197
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/other-type-extensions.html#scoped-type-variables>
198
  | ScopedTypeVariables
199
200

  -- | Deprecated, use 'ScopedTypeVariables' instead.
201
  | PatternSignatures
202

203
204
205
  -- | Enable implicit function parameters with dynamic scope.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/other-type-extensions.html#implicit-parameters>
206
  | ImplicitParams
207

208
209
210
211
  -- | Relax some restrictions on the form of the context of a type
  -- signature.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/other-type-extensions.html#flexible-contexts>
212
  | FlexibleContexts
213

214
215
216
217
  -- | Relax some restrictions on the form of the context of an
  -- instance declaration.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/type-class-extensions.html#instance-rules>
218
  | FlexibleInstances
219

220
221
222
  -- | Allow data type declarations with no constructors.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/data-type-extensions.html#nullary-types>
223
  | EmptyDataDecls
224

225
226
227
  -- | Run the C preprocessor on Haskell source code.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/options-phases.html#c-pre-processor>
228
229
  | CPP

230
231
232
233
  -- | Allow an explicit kind signature giving the kind of types over
  -- which a type variable ranges.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/other-type-extensions.html#kinding>
Ian Lynagh's avatar
Ian Lynagh committed
234
  | KindSignatures
235

236
237
238
239
  -- | Enable a form of pattern which forces evaluation before an
  -- attempted match, and a form of strict @let@/@where@ binding.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/bang-patterns.html>
simonpj's avatar
simonpj committed
240
  | BangPatterns
241

242
243
244
  -- | Allow type synonyms in instance heads.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/type-class-extensions.html#flexible-instance-head>
245
  | TypeSynonymInstances
246

247
  -- | Enable Template Haskell, a system for compile-time
248
  -- metaprogramming.
249
250
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/template-haskell.html>
251
  | TemplateHaskell
252

253
254
255
256
257
  -- | Enable the Foreign Function Interface.  In GHC, implements the
  -- standard Haskell 98 Foreign Function Interface Addendum, plus
  -- some GHC-specific extensions.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/ffi.html>
258
  | ForeignFunctionInterface
259

260
261
262
  -- | Enable arrow notation.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/arrow-notation.html>
263
  | Arrows
264

265
266
267
268
  -- | /(deprecated)/ Enable generic type classes, with default instances defined in
  -- terms of the algebraic structure of a type.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/generic-classes.html>
269
  | Generics
270

271
272
273
274
275
276
  -- | Enable the implicit importing of the module "Prelude".  When
  -- disabled, when desugaring certain built-in syntax into ordinary
  -- identifiers, use whatever is in scope rather than the "Prelude"
  -- -- version.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#rebindable-syntax>
277
  | ImplicitPrelude
278

279
280
281
282
283
  -- | Enable syntax for implicitly binding local names corresponding
  -- to the field names of a record.  Puns bind specific names, unlike
  -- 'RecordWildCards'.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#record-puns>
284
  | NamedFieldPuns
285

286
287
288
289
  -- | Enable a form of guard which matches a pattern and binds
  -- variables.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#pattern-guards>
290
  | PatternGuards
291

292
293
294
295
  -- | Allow a type declared with @newtype@ to use @deriving@ for any
  -- class with an instance for the underlying type.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/deriving.html#newtype-deriving>
296
  | GeneralizedNewtypeDeriving
297

298
299
300
  -- | Enable the \"Trex\" extensible records system.
  --
  -- * <http://cvs.haskell.org/Hugs/pages/users_guide/hugs-only.html#TREX>
301
  | ExtensibleRecords
302

303
304
305
306
307
  -- | Enable type synonyms which are transparent in some definitions
  -- and opaque elsewhere, as a way of implementing abstract
  -- datatypes.
  --
  -- * <http://cvs.haskell.org/Hugs/pages/users_guide/restricted-synonyms.html>
308
  | RestrictedTypeSynonyms
309

310
  -- | Enable an alternate syntax for string literals,
311
  -- with string templating.
312
313
  --
  -- * <http://cvs.haskell.org/Hugs/pages/users_guide/here-documents.html>
314
  | HereDocuments
315

316
317
318
319
  -- | Allow the character @#@ as a postfix modifier on identifiers.
  -- Also enables literal syntax for unboxed values.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#magic-hash>
Ian Lynagh's avatar
Ian Lynagh committed
320
  | MagicHash
321

322
323
324
325
  -- | Allow data types and type synonyms which are indexed by types,
  -- i.e. ad-hoc polymorphism for types.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/type-families.html>
Ian Lynagh's avatar
Ian Lynagh committed
326
  | TypeFamilies
327

328
329
330
331
  -- | Allow a standalone declaration which invokes the type class
  -- @deriving@ mechanism.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/deriving.html#stand-alone-deriving>
Ian Lynagh's avatar
Ian Lynagh committed
332
  | StandaloneDeriving
333

334
335
336
337
  -- | Allow certain Unicode characters to stand for certain ASCII
  -- character sequences, e.g. keywords and punctuation.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#unicode-syntax>
338
  | UnicodeSyntax
339

340
341
342
343
  -- | Allow the use of unboxed types as foreign types, e.g. in
  -- @foreign import@ and @foreign export@.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/ffi.html#id681687>
344
  | UnliftedFFITypes
345

346
347
348
349
350
351
352
353
354
355
  -- | Enable interruptible FFI.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/ffi.html#ffi-interruptible>
  | InterruptibleFFI

  -- | Allow use of CAPI FFI calling convention (@foreign import capi@).
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/ffi.html#ffi-capi>
  | CApiFFI

356
357
358
359
  -- | Defer validity checking of types until after expanding type
  -- synonyms, relaxing the constraints on how synonyms may be used.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/data-type-extensions.html#type-synonyms>
360
  | LiberalTypeSynonyms
361

362
363
  -- | Allow the name of a type constructor, type class, or type
  -- variable to be an infix operator.
364
  | TypeOperators
365

366
367
368
369
370
  -- | Enable syntax for implicitly binding local names corresponding
  -- to the field names of a record.  A wildcard binds all unmentioned
  -- names, unlike 'NamedFieldPuns'.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#record-wildcards>
371
  | RecordWildCards
372
373

  -- | Deprecated, use 'NamedFieldPuns' instead.
374
  | RecordPuns
375

376
377
378
379
  -- | Allow a record field name to be disambiguated by the type of
  -- the record it's in.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#disambiguate-fields>
380
  | DisambiguateRecordFields
381

382
383
384
385
386
  -- | Enable traditional record syntax (as supported by Haskell 98)
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#traditional-record-syntax>
  | TraditionalRecordSyntax

387
388
389
390
  -- | Enable overloading of string literals using a type class, much
  -- like integer literals.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/type-class-extensions.html#overloaded-strings>
391
  | OverloadedStrings
392

393
394
395
396
397
  -- | Enable generalized algebraic data types, in which type
  -- variables may be instantiated on a per-constructor basis. Implies
  -- 'GADTSyntax'.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/data-type-extensions.html#gadt>
398
  | GADTs
399

Ian Lynagh's avatar
Ian Lynagh committed
400
  -- | Enable GADT syntax for declaring ordinary algebraic datatypes.
401
402
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/data-type-extensions.html#gadt-style>
Ian Lynagh's avatar
Ian Lynagh committed
403
404
  | GADTSyntax

405
406
407
  -- | Make pattern bindings monomorphic.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/monomorphism.html#id630981>
408
  | MonoPatBinds
409

410
411
412
413
  -- | Relax the requirements on mutually-recursive polymorphic
  -- functions.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/other-type-extensions.html#typing-binds>
414
  | RelaxedPolyRec
415

416
417
418
419
  -- | Allow default instantiation of polymorphic types in more
  -- situations.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/interactive-evaluation.html#extended-default-rules>
420
  | ExtendedDefaultRules
421

422
423
424
  -- | Enable unboxed tuples.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/primitives.html#unboxed-tuples>
425
  | UnboxedTuples
426

427
428
429
430
  -- | Enable @deriving@ for classes 'Data.Typeable.Typeable' and
  -- 'Data.Generics.Data'.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/deriving.html#deriving-typeable>
431
  | DeriveDataTypeable
432

433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
  -- | Enable @deriving@ for 'GHC.Generics.Generic' and 'GHC.Generics.Generic1'.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/deriving.html#deriving-typeable>
  | DeriveGeneric

  -- | Enable support for default signatures.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/type-class-extensions.html#class-default-signatures>
  | DefaultSignatures

  -- | Allow type signatures to be specified in instance declarations.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/type-class-extensions.html#instance-sigs>
  | InstanceSigs

448
449
450
451
  -- | Allow a class method's type to place additional constraints on
  -- a class type variable.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/type-class-extensions.html#class-method-types>
452
  | ConstrainedClassMethods
453

454
455
  -- | Allow imports to be qualified by the package name the module is
  -- intended to be imported from, e.g.
456
457
  --
  -- > import "network" Network.Socket
458
459
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#package-imports>
460
461
  | PackageImports

462
  -- | /(deprecated)/ Allow a type variable to be instantiated at a
463
  -- polymorphic type.
464
465
  --
  -- * <http://www.haskell.org/ghc/docs/6.12.3/html/users_guide/other-type-extensions.html#impredicative-polymorphism>
466
  | ImpredicativeTypes
467

468
469
470
  -- | /(deprecated)/ Change the syntax for qualified infix operators.
  --
  -- * <http://www.haskell.org/ghc/docs/6.12.3/html/users_guide/syntax-extns.html#new-qualified-operators>
471
  | NewQualifiedOperators
472

473
474
475
476
  -- | Relax the interpretation of left operator sections to allow
  -- unary postfix operators.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#postfix-operators>
477
  | PostfixOperators
478

479
480
481
482
  -- | Enable quasi-quotation, a mechanism for defining new concrete
  -- syntax for expressions and patterns.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/template-haskell.html#th-quasiquotation>
483
  | QuasiQuotes
484

485
486
487
488
  -- | Enable generalized list comprehensions, supporting operations
  -- such as sorting and grouping.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#generalised-list-comprehensions>
489
  | TransformListComp
490

491
492
493
494
495
496
  -- | Enable monad comprehensions, which generalise the list
  -- comprehension syntax to work for any monad.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#monad-comprehensions>
  | MonadComprehensions

497
498
499
500
  -- | Enable view patterns, which match a value by applying a
  -- function and matching on the result.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#view-patterns>
501
502
  | ViewPatterns

503
  -- | Allow concrete XML syntax to be used in expressions and patterns,
Ian Lynagh's avatar
Ian Lynagh committed
504
505
  -- as per the Haskell Server Pages extension language:
  -- <http://www.haskell.org/haskellwiki/HSP>. The ideas behind it are
506
  -- discussed in the paper \"Haskell Server Pages through Dynamic Loading\"
507
508
509
510
  -- by Niklas Broberg, from Haskell Workshop '05.
  | XmlSyntax

  -- | Allow regular pattern matching over lists, as discussed in the
511
  -- paper \"Regular Expression Patterns\" by Niklas Broberg, Andreas Farre
512
513
  -- and Josef Svenningsson, from ICFP '04.
  | RegularPatterns
514

515
  -- | Enable the use of tuple sections, e.g. @(, True)@ desugars into
516
  -- @\x -> (x, True)@.
517
518
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#tuple-sections>
519
520
  | TupleSections

521
  -- | Allow GHC primops, written in C--, to be imported into a Haskell
522
523
524
525
526
  -- file.
  | GHCForeignImportPrim

  -- | Support for patterns of the form @n + k@, where @k@ is an
  -- integer literal.
527
528
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#n-k-patterns>
529
530
531
532
533
534
  | NPlusKPatterns

  -- | Improve the layout rule when @if@ expressions are used in a @do@
  -- block.
  | DoAndIfThenElse

535
536
537
538
539
540
541
542
543
544
  -- | Enable support for multi-way @if@-expressions.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#multi-way-if>
  | MultiWayIf

  -- | Enable support lambda-@case@ expressions.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#lambda-case>
  | LambdaCase

545
546
  -- | Makes much of the Haskell sugar be desugared into calls to the
  -- function with a particular name that is in scope.
547
548
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#rebindable-syntax>
549
550
551
552
  | RebindableSyntax

  -- | Make @forall@ a keyword in types, which can be used to give the
  -- generalisation explicitly.
553
554
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/other-type-extensions.html#explicit-foralls>
555
556
557
558
  | ExplicitForAll

  -- | Allow contexts to be put on datatypes, e.g. the @Eq a@ in
  -- @data Eq a => Set a = NilSet | ConsSet a (Set a)@.
559
560
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/data-type-extensions.html#datatype-contexts>
561
562
563
  | DatatypeContexts

  -- | Local (@let@ and @where@) bindings are monomorphic.
564
565
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/other-type-extensions.html#mono-local-binds>
566
567
  | MonoLocalBinds

568
569
570
  -- | Enable @deriving@ for the 'Data.Functor.Functor' class.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/deriving.html#deriving-typeable>
571
572
  | DeriveFunctor

573
574
575
  -- | Enable @deriving@ for the 'Data.Traversable.Traversable' class.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/deriving.html#deriving-typeable>
576
577
  | DeriveTraversable

578
579
580
  -- | Enable @deriving@ for the 'Data.Foldable.Foldable' class.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/deriving.html#deriving-typeable>
581
582
  | DeriveFoldable

583
584
585
  -- | Enable non-decreasing indentation for @do@ blocks.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/bugs-and-infelicities.html#infelicities-syntax>
586
587
  | NondecreasingIndentation

588
589
590
  -- | Allow imports to be qualified with a safe keyword that requires
  -- the imported module be trusted as according to the Safe Haskell
  -- definition of trust.
591
592
  --
  -- > import safe Network.Socket
593
594
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/safe-haskell.html#safe-imports>
595
596
  | SafeImports

597
598
599
600
  -- | Compile a module in the Safe, Safe Haskell mode -- a restricted
  -- form of the Haskell language to ensure type safety.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/safe-haskell.html#safe-trust>
601
602
  | Safe

603
604
605
606
607
  -- | Compile a module in the Trustworthy, Safe Haskell mode -- no
  -- restrictions apply but the module is marked as trusted as long as
  -- the package the module resides in is trusted.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/safe-haskell.html#safe-trust>
608
609
  | Trustworthy

610
611
612
613
614
615
  -- | Compile a module in the Unsafe, Safe Haskell mode so that
  -- modules compiled using Safe, Safe Haskell mode can't import it.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/safe-haskell.html#safe-trust>
  | Unsafe

616
617
618
619
620
621
  -- | Allow type class/implicit parameter/equality constraints to be
  -- used as types with the special kind constraint.  Also generalise
  -- the @(ctxt => ty)@ syntax so that any type of kind constraint can
  -- occur before the arrow.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/constraint-kind.html>
622
  | ConstraintKinds
623

624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
  -- | Enable kind polymorphism.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/kind-polymorphism.html>
  | PolyKinds

  -- | Enable datatype promotion.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/promotion.html>
  | DataKinds

  -- | Enable parallel arrays syntax (@[:@, @:]@) for /Data Parallel Haskell/.
  --
  -- * <http://www.haskell.org/haskellwiki/GHC/Data_Parallel_Haskell>
  | ParallelArrays

639
  -- | Enable explicit role annotations, like in (@type role Foo representational representational@).
640
  --
eir@cis.upenn.edu's avatar
eir@cis.upenn.edu committed
641
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/roles.html>
642
643
  | RoleAnnotations

644
645
646
647
648
649
  -- | Enable overloading of list literals, arithmetic sequences and
  -- list patterns using the 'IsList' type class.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/type-class-extensions.html#overloaded-lists>
  | OverloadedLists

650
  -- | Enable case expressions that have no alternatives. Also applies to lambda-case expressions if they are enabled.
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#empty-case>
  | EmptyCase

  -- | Triggers the generation of derived 'Typeable' instances for every
  -- datatype and type class declaration.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/deriving.html#auto-derive-typeable>
  | AutoDeriveTypeable

  -- | Desugars negative literals directly (without using negate).
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#negative-literals>
  | NegativeLiterals

666
  -- | Allow the use of binary integer literal syntax (e.g. @0b11001001@ to denote @201@).
667
668
669
670
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#binary-literals>
  | BinaryLiterals

671
  -- | Allow the use of floating literal syntax for all instances of 'Num', including 'Int' and 'Integer'.
672
673
674
675
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#num-decimals>
  | NumDecimals

676
  -- | Enable support for type classes with no type parameter.
677
678
679
680
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/type-class-extensions.html#nullary-type-classes>
  | NullaryTypeClasses

681
682
683
684
685
686
687
688
689
690
  -- | Enable explicit namespaces in module import/export lists.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/syntax-extns.html#explicit-namespaces>
  | ExplicitNamespaces

  -- | Allow the user to write ambiguous types, and the type inference engine to infer them.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/other-type-extensions.html#ambiguity>
  | AllowAmbiguousTypes

691
  -- | Enable @foreign import javascript@.
692
693
  | JavaScriptFFI

694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
  -- | Allow giving names to and abstracting over patterns.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/other-type-extensions.html#pattern-synonyms>
  | PatternSynonyms

  -- | Allow anonymous placeholders (underscore) inside type signatures.  The
  -- type inference engine will generate a message describing the type inferred
  -- at the hole's location.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/other-type-extensions.html#partial-type-signatures>
  | PartialTypeSignatures

  -- | Allow named placeholders written with a leading underscore inside type
  -- signatures.  Wildcards with the same name unify to the same type.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/other-type-extensions.html#named-wildcards>
  | NamedWildCards

  -- | Enable @deriving@ for any class.
  --
  -- * <http://www.haskell.org/ghc/docs/latest/html/users_guide/other-type-extensions.html#derive-any-class>
  | DeriveAnyClass

717
  deriving (Generic, Show, Read, Eq, Ord, Enum, Bounded, Typeable, Data)
ttuegel's avatar
ttuegel committed
718
719

instance Binary KnownExtension
720
721
722
723
724

{-# DEPRECATED knownExtensions
   "KnownExtension is an instance of Enum and Bounded, use those instead." #-}
knownExtensions :: [KnownExtension]
knownExtensions = [minBound..maxBound]
725

726
727
728
-- | Extensions that have been deprecated, possibly paired with another
-- extension that replaces it.
--
Ian Lynagh's avatar
Ian Lynagh committed
729
deprecatedExtensions :: [(Extension, Maybe Extension)]
730
deprecatedExtensions =
Ian Lynagh's avatar
Ian Lynagh committed
731
732
  [ (EnableExtension RecordPuns, Just (EnableExtension NamedFieldPuns))
  , (EnableExtension PatternSignatures, Just (EnableExtension ScopedTypeVariables))
733
  ]
734
735
-- NOTE: when adding deprecated extensions that have new alternatives
-- we must be careful to make sure that the deprecation messages are
Ian D. Bollinger's avatar
Ian D. Bollinger committed
736
-- valid. We must not recommend aliases that cannot be used with older
737
738
739
-- compilers, perhaps by adding support in Cabal to translate the new
-- name to the old one for older compilers. Otherwise we are in danger
-- of the scenario in ticket #689.
740

741
742
instance Text Extension where
  disp (UnknownExtension other) = Disp.text other
743
  disp (EnableExtension ke)     = Disp.text (show ke)
744
  disp (DisableExtension ke)    = Disp.text ("No" ++ show ke)
745
746
747
748
749

  parse = do
    extension <- Parse.munch1 Char.isAlphaNum
    return (classifyExtension extension)

750
751
752
753
754
instance Text KnownExtension where
  disp ke = Disp.text (show ke)

  parse = do
    extension <- Parse.munch1 Char.isAlphaNum
755
756
    case classifyKnownExtension extension of
        Just ke ->
757
            return ke
758
        Nothing ->
759
760
            fail ("Can't parse " ++ show extension ++ " as KnownExtension")

761
762
763
764
765
766
767
768
769
770
771
772
classifyExtension :: String -> Extension
classifyExtension string
  = case classifyKnownExtension string of
    Just ext -> EnableExtension ext
    Nothing ->
        case string of
        'N':'o':string' ->
            case classifyKnownExtension string' of
            Just ext -> DisableExtension ext
            Nothing -> UnknownExtension string
        _ -> UnknownExtension string

773
774
-- | 'read' for 'KnownExtension's is really really slow so for the Text
-- instance
775
-- what we do is make a simple table indexed off the first letter in the
776
-- extension name. The extension names actually cover the range @'A'-'Z'@
777
778
779
780
781
782
-- pretty densely and the biggest bucket is 7 so it's not too bad. We just do
-- a linear search within each bucket.
--
-- This gives an order of magnitude improvement in parsing speed, and it'll
-- also allow us to do case insensitive matches in future if we prefer.
--
783
784
785
classifyKnownExtension :: String -> Maybe KnownExtension
classifyKnownExtension "" = Nothing
classifyKnownExtension string@(c : _)
786
  | inRange (bounds knownExtensionTable) c
787
788
  = lookup string (knownExtensionTable ! c)
  | otherwise = Nothing
789

790
791
knownExtensionTable :: Array Char [(String, KnownExtension)]
knownExtensionTable =
792
  accumArray (flip (:)) [] ('A', 'Z')
793
    [ (head str, (str, extension))
794
    | extension <- [toEnum 0 ..]
795
    , let str = show extension ]