1
-----------------------------------------------------------------------------
3
-- Module : Language.Haskell.Extension
4
-- Copyright : Isaac Jones 2003-2004
6
-- Maintainer : libraries@haskell.org
7
-- Portability : portable
9
-- Haskell language dialects and extensions
11
{- All rights reserved.
13
Redistribution and use in source and binary forms, with or without
14
modification, are permitted provided that the following conditions are
17
* Redistributions of source code must retain the above copyright
18
notice, this list of conditions and the following disclaimer.
20
* Redistributions in binary form must reproduce the above
21
copyright notice, this list of conditions and the following
22
disclaimer in the documentation and/or other materials provided
23
with the distribution.
25
* Neither the name of Isaac Jones nor the names of other
26
contributors may be used to endorse or promote products derived
27
from this software without specific prior written permission.
29
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -}
41
module Language.Haskell.Extension (
50
import Distribution.Text (Text(..))
51
import qualified Distribution.Compat.ReadP as Parse
52
import qualified Text.PrettyPrint as Disp
53
import qualified Data.Char as Char (isAlphaNum)
54
import Data.Array (Array, accumArray, bounds, Ix(inRange), (!))
56
-- ------------------------------------------------------------
58
-- ------------------------------------------------------------
60
-- | This represents a Haskell language dialect.
62
-- Language 'Extension's are interpreted relative to one of these base
67
-- | The Haskell 98 language as defined by the Haskell 98 report.
68
-- <http://haskell.org/onlinereport/>
71
-- | The Haskell 2010 language as defined by the Haskell 2010 report.
72
-- <http://www.haskell.org/onlinereport/haskell2010>
75
-- | An unknown language, identified by its name.
76
| UnknownLanguage String
77
deriving (Show, Read, Eq)
79
knownLanguages :: [Language]
80
knownLanguages = [Haskell98, Haskell2010]
82
instance Text Language where
83
disp (UnknownLanguage other) = Disp.text other
84
disp other = Disp.text (show other)
87
lang <- Parse.munch1 Char.isAlphaNum
88
return (classifyLanguage lang)
90
classifyLanguage :: String -> Language
91
classifyLanguage = \str -> case lookup str langTable of
93
Nothing -> UnknownLanguage str
95
langTable = [ (show lang, lang)
96
| lang <- knownLanguages ]
98
-- ------------------------------------------------------------
100
-- ------------------------------------------------------------
102
-- Note: if you add a new 'Extension':
104
-- * also add it to the Distribution.Simple.X.languageExtensions lists
105
-- (where X is each compiler: GHC, JHC, Hugs, NHC)
107
-- * also to the 'knownExtensions' list below.
109
-- | This represents language extensions beyond a base 'Language' definition
110
-- (such as 'Haskell98') that are supported by some implementations, usually
111
-- in some special mode.
113
-- Where applicable, references are given to an implementation's
114
-- official documentation, e.g. \"GHC § 7.2.1\" for an extension
115
-- documented in section 7.2.1 of the GHC User's Guide.
119
-- | [GHC § 7.6.3.4] Allow overlapping class instances,
120
-- provided there is a unique most specific instance for each use.
123
-- | [GHC § 7.6.3.3] Ignore structural rules guaranteeing the
124
-- termination of class instance resolution. Termination is
125
-- guaranteed by a fixed-depth recursion stack, and compilation
126
-- may fail if this depth is exceeded.
127
| UndecidableInstances
129
-- | [GHC § 7.6.3.4] Implies 'OverlappingInstances'. Allow the
130
-- implementation to choose an instance even when it is possible
131
-- that further instantiation of types will lead to a more specific
132
-- instance being applicable.
133
| IncoherentInstances
135
-- | [GHC § 7.3.8] Allows recursive bindings in @do@ blocks,
136
-- using the @rec@ keyword.
139
-- | [GHC § 7.3.8.2] Deprecated in GHC. Allows recursive bindings
140
-- using @mdo@, a variant of @do@. @DoRec@ provides a different,
144
-- | [GHC § 7.3.9] Provide syntax for writing list
145
-- comprehensions which iterate over several lists together, like
146
-- the 'zipWith' family of functions.
149
-- | [GHC § 7.6.1.1] Allow multiple parameters in a type class.
150
| MultiParamTypeClasses
152
-- | [GHC § 7.17] Disable the dreaded monomorphism restriction.
153
| NoMonomorphismRestriction
155
-- | [GHC § 7.6.2] Allow a specification attached to a
156
-- multi-parameter type class which indicates that some parameters
157
-- are entirely determined by others. The implementation will check
158
-- that this property holds for the declared instances, and will use
159
-- this property to reduce ambiguity in instance resolution.
160
| FunctionalDependencies
162
-- | [GHC § 7.8.5] Like 'RankNTypes' but does not allow a
163
-- higher-rank type to itself appear on the left of a function
167
-- | [GHC § 7.8.5] Allow a universally-quantified type to occur on
168
-- the left of a function arrow.
171
-- | [GHC § 7.8.5] Allow data constructors to have polymorphic
172
-- arguments. Unlike 'RankNTypes', does not allow this for ordinary
174
| PolymorphicComponents
176
-- | [GHC § 7.4.4] Allow existentially-quantified data constructors.
177
| ExistentialQuantification
179
-- | [GHC § 7.8.7] Cause a type variable in a signature, which has an
180
-- explicit @forall@ quantifier, to scope over the definition of the
181
-- accompanying value declaration.
182
| ScopedTypeVariables
184
-- | Deprecated, use 'ScopedTypeVariables' instead.
187
-- | [GHC § 7.8.3] Enable implicit function parameters with dynamic
191
-- | [GHC § 7.8.2] Relax some restrictions on the form of the context
192
-- of a type signature.
195
-- | [GHC § 7.6.3.2] Relax some restrictions on the form of the
196
-- context of an instance declaration.
199
-- | [GHC § 7.4.1] Allow data type declarations with no constructors.
202
-- | [GHC § 4.10.3] Run the C preprocessor on Haskell source code.
205
-- | [GHC § 7.8.4] Allow an explicit kind signature giving the kind of
206
-- types over which a type variable ranges.
209
-- | [GHC § 7.11] Enable a form of pattern which forces evaluation
210
-- before an attempted match, and a form of strict @let@/@where@
214
-- | [GHC § 7.6.3.1] Allow type synonyms in instance heads.
215
| TypeSynonymInstances
217
-- | [GHC § 7.9] Enable Template Haskell, a system for compile-time
221
-- | [GHC § 8] Enable the Foreign Function Interface. In GHC,
222
-- implements the standard Haskell 98 Foreign Function Interface
223
-- Addendum, plus some GHC-specific extensions.
224
| ForeignFunctionInterface
226
-- | [GHC § 7.10] Enable arrow notation.
229
-- | [GHC § 7.16] Enable generic type classes, with default instances
230
-- defined in terms of the algebraic structure of a type.
233
-- | [GHC § 7.3.11] Disable the implicit importing of the module
234
-- @Prelude@. When desugaring certain built-in syntax into ordinary
235
-- identifiers, use whatever is in scope rather than the @Prelude@
239
-- | [GHC § 7.3.15] Enable syntax for implicitly binding local names
240
-- corresponding to the field names of a record. Puns bind specific
241
-- names, unlike 'RecordWildCards'.
244
-- | [GHC § 7.3.5] Enable a form of guard which matches a pattern and
248
-- | [GHC § 7.5.4] Allow a type declared with @newtype@ to use
249
-- @deriving@ for any class with an instance for the underlying type.
250
| GeneralizedNewtypeDeriving
252
-- | [Hugs § 7.1] Enable the \"Trex\" extensible records system.
255
-- | [Hugs § 7.2] Enable type synonyms which are transparent in
256
-- some definitions and opaque elsewhere, as a way of implementing
257
-- abstract datatypes.
258
| RestrictedTypeSynonyms
260
-- | [Hugs § 7.3] Enable an alternate syntax for string literals,
261
-- with string templating.
264
-- | [GHC § 7.3.2] Allow the character @#@ as a postfix modifier on
265
-- identifiers. Also enables literal syntax for unboxed values.
268
-- | [GHC § 7.7] Allow data types and type synonyms which are
269
-- indexed by types, i.e. ad-hoc polymorphism for types.
272
-- | [GHC § 7.5.2] Allow a standalone declaration which invokes the
273
-- type class @deriving@ mechanism.
276
-- | [GHC § 7.3.1] Allow certain Unicode characters to stand for
277
-- certain ASCII character sequences, e.g. keywords and punctuation.
280
-- | [GHC § 8.1.1] Allow the use of unboxed types as foreign types,
281
-- e.g. in @foreign import@ and @foreign export@.
284
-- | [GHC § 7.4.3] Defer validity checking of types until after
285
-- expanding type synonyms, relaxing the constraints on how synonyms
287
| LiberalTypeSynonyms
289
-- | [GHC § 7.4.2] Allow the name of a type constructor, type class,
290
-- or type variable to be an infix operator.
293
--PArr -- not ready yet, and will probably be renamed to ParallelArrays
295
-- | [GHC § 7.3.16] Enable syntax for implicitly binding local names
296
-- corresponding to the field names of a record. A wildcard binds
297
-- all unmentioned names, unlike 'NamedFieldPuns'.
300
-- | Deprecated, use 'NamedFieldPuns' instead.
303
-- | [GHC § 7.3.14] Allow a record field name to be disambiguated
304
-- by the type of the record it's in.
305
| DisambiguateRecordFields
307
-- | [GHC § 7.6.4] Enable overloading of string literals using a
308
-- type class, much like integer literals.
311
-- | [GHC § 7.4.6] Enable generalized algebraic data types, in
312
-- which type variables may be instantiated on a per-constructor
313
-- basis. Enables \"GADT syntax\" which can be used to declare
314
-- GADTs as well as ordinary algebraic types.
317
-- | [GHC § 7.17.2] Allow pattern bindings to be polymorphic.
320
-- | [GHC § 7.8.8] Relax the requirements on mutually-recursive
321
-- polymorphic functions.
324
-- | [GHC § 2.4.5] Allow default instantiation of polymorphic
325
-- types in more situations.
326
| ExtendedDefaultRules
328
-- | [GHC § 7.2.2] Enable unboxed tuples.
331
-- | [GHC § 7.5.3] Enable @deriving@ for classes
332
-- @Data.Typeable.Typeable@ and @Data.Generics.Data@.
335
-- | [GHC § 7.6.1.3] Allow a class method's type to place
336
-- additional constraints on a class type variable.
337
| ConstrainedClassMethods
339
-- | [GHC § 7.3.18] Allow imports to be qualified by the package
340
-- name the module is intended to be imported from, e.g.
342
-- > import "network" Network.Socket
345
-- | [GHC § 7.8.6] Deprecated in GHC 6.12 and will be removed in
346
-- GHC 7. Allow a type variable to be instantiated at a
350
-- | [GHC § 7.3.3] Change the syntax for qualified infix
352
| NewQualifiedOperators
354
-- | [GHC § 7.3.12] Relax the interpretation of left operator
355
-- sections to allow unary postfix operators.
358
-- | [GHC § 7.9.5] Enable quasi-quotation, a mechanism for defining
359
-- new concrete syntax for expressions and patterns.
362
-- | [GHC § 7.3.10] Enable generalized list comprehensions,
363
-- supporting operations such as sorting and grouping.
366
-- | [GHC § 7.3.6] Enable view patterns, which match a value by
367
-- applying a function and matching on the result.
370
-- | Allow concrete XML syntax to be used in expressions and patterns,
371
-- as per the Haskell Server Pages extension language:
372
-- <http://www.haskell.org/haskellwiki/HSP>. The ideas behind it are
373
-- discussed in the paper \"Haskell Server Pages through Dynamic Loading\"
374
-- by Niklas Broberg, from Haskell Workshop '05.
377
-- | Allow regular pattern matching over lists, as discussed in the
378
-- paper \"Regular Expression Patterns\" by Niklas Broberg, Andreas Farre
379
-- and Josef Svenningsson, from ICFP '04.
382
-- | Enables the use of tuple sections, e.g. @(, True)@ desugars into
383
-- @\x -> (x, True)@.
386
-- | Allows GHC primops, written in C--, to be imported into a Haskell
388
| GHCForeignImportPrim
390
-- | Support for patterns of the form @n + k@, where @k@ is an
394
-- | Improve the layout rule when @if@ expressions are used in a @do@
398
-- | Makes much of the Haskell sugar be desugared into calls to the
399
-- function with a particular name that is in scope.
402
-- | Make @forall@ a keyword in types, which can be used to give the
403
-- generalisation explicitly.
406
-- | Allow contexts to be put on datatypes, e.g. the @Eq a@ in
407
-- @data Eq a => Set a = NilSet | ConsSet a (Set a)@.
410
-- | Local (@let@ and @where@) bindings are monomorphic.
413
-- | Enable @deriving@ for the @Data.Functor.Functor@ class.
416
-- | Enable @deriving@ for the @Data.Traversable.Traversable@ class.
419
-- | Enable @deriving@ for the @Data.Foldable.Foldable@ class.
422
-- | An unknown extension, identified by the name of its @LANGUAGE@
424
| UnknownExtension String
425
deriving (Show, Read, Eq)
427
-- | Extensions that have been deprecated, possibly paired with another
428
-- extension that replaces it.
430
deprecatedExtensions :: [(Extension, Maybe Extension)]
431
deprecatedExtensions =
432
[ (RecordPuns, Just NamedFieldPuns)
433
, (PatternSignatures, Just ScopedTypeVariables)
436
knownExtensions :: [Extension]
438
[ OverlappingInstances
439
, UndecidableInstances
440
, IncoherentInstances
444
, MultiParamTypeClasses
445
, NoMonomorphismRestriction
446
, FunctionalDependencies
449
, PolymorphicComponents
450
, ExistentialQuantification
451
, ScopedTypeVariables
460
, TypeSynonymInstances
462
, ForeignFunctionInterface
468
, GeneralizedNewtypeDeriving
471
, RestrictedTypeSynonyms
480
, LiberalTypeSynonyms
482
--PArr -- not ready yet, and will probably be renamed to ParallelArrays
485
, DisambiguateRecordFields
490
, ExtendedDefaultRules
493
, ConstrainedClassMethods
496
, NewQualifiedOperators
505
, GHCForeignImportPrim
517
instance Text Extension where
518
disp (UnknownExtension other) = Disp.text other
519
disp other = Disp.text (show other)
522
extension <- Parse.munch1 Char.isAlphaNum
523
return (classifyExtension extension)
525
-- | 'read' for 'Extension's is really really slow so for the Text instance
526
-- what we do is make a simple table indexed off the first letter in the
527
-- extension name. The extension names actually cover the range @'A'-'Z'@
528
-- pretty densely and the biggest bucket is 7 so it's not too bad. We just do
529
-- a linear search within each bucket.
531
-- This gives an order of magnitude improvement in parsing speed, and it'll
532
-- also allow us to do case insensitive matches in future if we prefer.
534
classifyExtension :: String -> Extension
535
classifyExtension string@(c:_)
536
| inRange (bounds extensionTable) c
537
= case lookup string (extensionTable ! c) of
538
Just extension -> extension
539
Nothing -> UnknownExtension string
540
classifyExtension string = UnknownExtension string
542
extensionTable :: Array Char [(String, Extension)]
544
accumArray (flip (:)) [] ('A', 'Z')
545
[ (head str, (str, extension))
546
| extension <- knownExtensions
547
, let str = show extension ]