Simon Peyton Jones pushed to branch wip/spj-reinstallable-base2 at Glasgow Haskell Compiler / GHC
Commits:
-
c256a107
by Simon Peyton Jones at 2026-04-21T21:39:18+01:00
14 changed files:
- compiler/GHC/Builtin.hs
- compiler/GHC/Builtin/KnownKeys.hs
- docs/users_guide/exts/rebindable_syntax.rst
- libraries/base/src/Data/Array/Byte.hs
- libraries/base/src/Data/Bool.hs
- libraries/base/src/Data/Fixed.hs
- libraries/base/src/Data/Functor/Compose.hs
- libraries/base/src/Data/Functor/Product.hs
- libraries/base/src/Data/Functor/Sum.hs
- libraries/base/src/Prelude.hs
- libraries/base/src/System/Console/GetOpt.hs
- libraries/ghc-internal/src/GHC/Internal/Base.hs
- libraries/ghc-internal/src/GHC/Internal/OverloadedLabels.hs
- utils/genprimopcode/Main.hs
Changes:
| ... | ... | @@ -112,9 +112,9 @@ Here are more details. |
| 112 | 112 | |
| 113 | 113 | A "wired-in" entity:
|
| 114 | 114 | * Defined in GHC.Builtin.WiredIn.*
|
| 115 | - * Its Unique, OccName
|
|
| 116 | - * Its defining module
|
|
| 117 | - * Its data constructors etc
|
|
| 115 | + * GHC knows its Unique, OccName
|
|
| 116 | + * GHC knows its defining module
|
|
| 117 | + * GHC knows its data constructors etc
|
|
| 118 | 118 | So GHC knows /everything/ about it. See Note [Overview of wired-in things].
|
| 119 | 119 | |
| 120 | 120 | We try hard to avoid wired-in things; it's tricky to ensure that GHC's static
|
| ... | ... | @@ -123,20 +123,22 @@ A "wired-in" entity: |
| 123 | 123 | A "known-key" entity:
|
| 124 | 124 | * Defined in GHC.Builtin.KnownKeys
|
| 125 | 125 | * Its Unique and OccName are baked into GHC. Its Unique is called a KnownKey.
|
| 126 | - * It is exported by base:GHC.KnownKeyNames
|
|
| 126 | + * It is exported by GHC.KnownKeyNames
|
|
| 127 | 127 | * But that's all that GHC knows about it
|
| 128 | 128 | In particular, GHC does /not/ know in which module the entity is defined.
|
| 129 | 129 | |
| 130 | 130 | Example: the `Eq` class has OccName "Eq" and unique `eqClassKey`. It happens
|
| 131 | 131 | to be defined in ghc-internal:GHC.Internal.Classes, but GHC does not know that.
|
| 132 | 132 | |
| 133 | + Every known-key entity is also a known-occ entity, but not vice versa.
|
|
| 134 | + |
|
| 133 | 135 | See Note [Recipe for adding a known-key name] for how to add a known-key name
|
| 134 | 136 | to GHC. It's not hard.
|
| 135 | 137 | |
| 136 | 138 | A "known-occ" entity:
|
| 137 | 139 | * Defined in GHC.Builtin.KnownOccs
|
| 138 | 140 | * Its OccName is baked into GHC -- we call it a KnownOcc
|
| 139 | - * It is exported by base:GHC.KnownKeyNames
|
|
| 141 | + * It is exported by GHC.KnownKeyNames
|
|
| 140 | 142 | * But that's all that GHC knows about it
|
| 141 | 143 | In particular, GHC does /not/ know in which module the entity is defined,
|
| 142 | 144 | nor its Unique.
|
| ... | ... | @@ -189,14 +191,22 @@ When do we use each of these? |
| 189 | 191 | and then renames and typechecks them. These bindings refer to a myriad of
|
| 190 | 192 | identifiers, such as `(==)`, `(>)`, `inRange`, and so on. Again GHC does not
|
| 191 | 193 | need to know a statically-known unique for them, but it does need to find them
|
| 192 | - so it uses known
|
|
| 194 | + so it uses known-occ names for them. See lots ant lots of definitions like
|
|
| 195 | + gunfold_RDR :: RdrName
|
|
| 196 | + gunfold_RDR = knownVarOccRdrName "gunfold"
|
|
| 197 | + in GHC.Builtin.KnownOccs. This definition constructs a known-occ RdrName; sse
|
|
| 198 | + knownOccRdrName :: KnownOcc -> RdrName
|
|
| 199 | + in GHC.Types.Name.Reader
|
|
| 193 | 200 | |
| 194 | 201 | * When desugaring, the desugarer wants to refer to a particular
|
| 195 | 202 | class, type, or function. It does this via (e.g.)
|
| 203 | + dsLookupKnownOccTyCon eitherTyConOcc
|
|
| 204 | + where
|
|
| 196 | 205 | dsLookupKnownOccTyCon :: KnownOcc -> DsM TyCon
|
| 197 | - or
|
|
| 206 | + |
|
| 207 | + For known-key entities you can also use
|
|
| 198 | 208 | dsLookupKnownKeyTyCon :: KnownKey -> DsM TyCon
|
| 199 | - (It doesn't really matter which we use.)
|
|
| 209 | + by giving it the known key of the entity.
|
|
| 200 | 210 | |
| 201 | 211 | To implement all this, here are the moving parts.
|
| 202 | 212 | |
| ... | ... | @@ -209,7 +219,7 @@ How known-occ entities work |
| 209 | 219 | this is not an onerous restriction. But see Note [Tricky known-occ cases] in
|
| 210 | 220 | GHC.Builtin.KnownOccs for some awkward cases.
|
| 211 | 221 | |
| 212 | -* A special module `base:GHC.KnownKeyNames` exports all the known-key and known-occ
|
|
| 222 | +* A distinguished module `GHC.KnownKeyNames` exports all the known-key and known-occ
|
|
| 213 | 223 | entities names. There is nothing special about this module except that GHC knows its
|
| 214 | 224 | name and can import it.
|
| 215 | 225 | |
| ... | ... | @@ -220,6 +230,9 @@ How known-occ entities work |
| 220 | 230 | This is a big reason for (KnownEntityInvariant): an export list cannot have two
|
| 221 | 231 | entities with the same OccName.
|
| 222 | 232 | |
| 233 | + When GHC wants to find GHC.KnownKeyNames, it just looks for it in the same
|
|
| 234 | + way as any other import.
|
|
| 235 | + |
|
| 223 | 236 | * There are three flags that control the treatment of known entities:
|
| 224 | 237 | -frebindable-known-names
|
| 225 | 238 | -fdefines-known-names
|
| ... | ... | @@ -307,10 +320,10 @@ Known-key entities are |
| 307 | 320 | |
| 308 | 321 | * DEFINING. In the module that /defines/ a known-key name, such as
|
| 309 | 322 | the `Num` class in ghc-internal:GHC.Internal.Num
|
| 310 | - we must assign the correct Unique. So in GHC.Rename.Env.newTopVanillaSrcBinder
|
|
| 311 | - if -fdefines-known-key-names is set (Opt_DefinesKnownKeyNames), we check the
|
|
| 312 | - OccName against the list in `knownKeyTable`; if it appears there, we use the
|
|
| 313 | - Unique from the table.
|
|
| 323 | + we must assign the correct Unique at its definitino site. So in
|
|
| 324 | + `GHC.Rename.Env.newTopVanillaSrcBinder`, if -fdefines-known-key-names is set
|
|
| 325 | + (Opt_DefinesKnownKeyNames), we check the OccName against the list in `knownKeyTable`;
|
|
| 326 | + if it appears there, we use the Unique from the table.
|
|
| 314 | 327 | |
| 315 | 328 | * SERIALISING.
|
| 316 | 329 | - When we serialise a known-key name into an interface file, we mark it as such.
|
| ... | ... | @@ -343,7 +356,7 @@ Wrinkles |
| 343 | 356 | So we compile GHC.Internal.Data.Foldable with
|
| 344 | 357 | -fexclude-known-define=toList
|
| 345 | 358 | |
| 346 | -(KN3) We don't need need to export wired-in entities from GHC.KnownKeyNames
|
|
| 359 | +(KN3) We don't need to export wired-in entities from GHC.KnownKeyNames
|
|
| 347 | 360 | because we (should) never look up a wired-in name via its key. That is,
|
| 348 | 361 | `GHC.Iface.Load.lookupKnownKeyName` should never be called on the key of
|
| 349 | 362 | a wired-in name.
|
| ... | ... | @@ -355,9 +368,9 @@ Wrinkles |
| 355 | 368 | |
| 356 | 369 | Note [Recipe for adding a known-occ name]
|
| 357 | 370 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 358 | -To make `wombat` into a known-occ name, you must ensure that:
|
|
| 371 | +To make `wombat` into a known-occ name, you do the following:
|
|
| 359 | 372 | |
| 360 | -* The module `GHC.KnownKeyNames` must export `wombat`.
|
|
| 373 | +* Ensure that the module `GHC.KnownKeyNames` exports `wombat`.
|
|
| 361 | 374 | |
| 362 | 375 | * In any module in `base` or `ghc-internal` (which are compiled with
|
| 363 | 376 | -frebindable-known-names), in which `wombat` is needed, you must ensure
|
| ... | ... | @@ -373,14 +386,14 @@ To make `wombat` into a known-occ name, you must ensure that: |
| 373 | 386 | |
| 374 | 387 | Note [Recipe for adding a known-key name]
|
| 375 | 388 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 376 | -To make `wombat` into a known-key name, you must ensure that:
|
|
| 389 | +To make `wombat` into a known-key name, do the following.
|
|
| 377 | 390 | |
| 378 | -* The module M that defines `wombat` is compiled with `-fdefines-known-names`.
|
|
| 391 | +* Ensure that the module M that defines `wombat` is compiled with `-fdefines-known-names`.
|
|
| 379 | 392 | |
| 380 | -* If M.hs has an `M.hs-boot` file, it too must be compiled
|
|
| 393 | +* If M.hs has an `M.hs-boot` file, ensure that it too must be compiled
|
|
| 381 | 394 | with `-fdefines-known-names`.
|
| 382 | 395 | |
| 383 | -* The module `GHC.KnownKeyNames` must export `wombat`.
|
|
| 396 | +* Ensure that the module `GHC.KnownKeyNames` exports `wombat`.
|
|
| 384 | 397 | |
| 385 | 398 | * In GHC.Builtin.KnownKeys you must define a static unique
|
| 386 | 399 | wombatKey :: KnownKey
|
| 1 | 1 | {-
|
| 2 | 2 | (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
|
| 3 | 3 | |
| 4 | -\section[GHC.Builtin.KnownKeys]{Definitions of prelude modules and names}
|
|
| 5 | - |
|
| 6 | - |
|
| 7 | 4 | Nota Bene: all Names defined in here should come from the base package,
|
| 8 | 5 | the big-num package or (for plugins) the ghc package.
|
| 9 | 6 |
| ... | ... | @@ -78,16 +78,6 @@ not the Prelude versions: |
| 78 | 78 | - An overloaded label "``#foo``" means "``fromLabel @"foo"``", rather than
|
| 79 | 79 | "``GHC.OverloadedLabels.fromLabel @"foo"``" (see :ref:`overloaded-labels`).
|
| 80 | 80 | |
| 81 | -.. extension:: ImplicitKnownKeyNames
|
|
| 82 | - :shortdesc: Use module ``KnownKeyNames`` to find known-key names
|
|
| 83 | - |
|
| 84 | - ToDo: needs proper documentation
|
|
| 85 | - |
|
| 86 | -.. extension:: DefinesKnownKeyNames
|
|
| 87 | - :shortdesc: This modules defines one or more known-key names
|
|
| 88 | - |
|
| 89 | - ToDo: needs proper documentation
|
|
| 90 | - |
|
| 91 | 81 | :extension:`RebindableSyntax` implies :extension:`NoImplicitPrelude`.
|
| 92 | 82 | |
| 93 | 83 | In all cases (apart from arrow notation), the static semantics should be
|
| ... | ... | @@ -15,15 +15,13 @@ |
| 15 | 15 | {-# LANGUAGE UnboxedTuples #-}
|
| 16 | 16 | {-# LANGUAGE TemplateHaskellQuotes #-}
|
| 17 | 17 | |
| 18 | -{-# OPTIONS_GHC -fno-rebindable-known-names #-}
|
|
| 19 | - -- We import Prelude, hence GHC.KnownKeyNames is available
|
|
| 20 | - |
|
| 21 | 18 | module Data.Array.Byte (
|
| 22 | 19 | ByteArray(..),
|
| 23 | 20 | MutableByteArray(..),
|
| 24 | 21 | ) where
|
| 25 | 22 | |
| 26 | 23 | import Prelude
|
| 24 | +import qualified GHC.KnownKeyNames as Rebindable
|
|
| 27 | 25 | import GHC.Internal.Data.Bits ((.&.), unsafeShiftR)
|
| 28 | 26 | import GHC.Internal.Data.Data (mkNoRepType, Data(..))
|
| 29 | 27 | import GHC.Internal.Data.Typeable (Typeable)
|
| 1 | 1 | {-# LANGUAGE Safe #-}
|
| 2 | 2 | |
| 3 | -{-# OPTIONS_GHC -fno-rebindable-known-names #-}
|
|
| 4 | - -- We import Prelude, hence GHC.KnownKeyNames is available
|
|
| 5 | - |
|
| 6 | 3 | -- |
|
| 7 | 4 | --
|
| 8 | 5 | -- Module : Data.Bool
|
| ... | ... | @@ -28,6 +25,7 @@ module Data.Bool |
| 28 | 25 | ) where
|
| 29 | 26 | |
| 30 | 27 | import Prelude ( Bool(..), (&&), (||), not, otherwise )
|
| 28 | +import qualified GHC.KnownKeyNames as Rebindable
|
|
| 31 | 29 | |
| 32 | 30 | -- $setup
|
| 33 | 31 | -- >>> import Prelude
|
| ... | ... | @@ -4,9 +4,6 @@ |
| 4 | 4 | {-# LANGUAGE FlexibleInstances #-}
|
| 5 | 5 | {-# LANGUAGE TemplateHaskellQuotes #-}
|
| 6 | 6 | |
| 7 | -{-# OPTIONS_GHC -fno-rebindable-known-names #-}
|
|
| 8 | - -- We are importing Prelude, hence GHC.KnownKeyNames is available
|
|
| 9 | - |
|
| 10 | 7 | -----------------------------------------------------------------------------
|
| 11 | 8 | -- |
|
| 12 | 9 | -- Module : Data.Fixed
|
| ... | ... | @@ -90,6 +87,7 @@ module Data.Fixed |
| 90 | 87 | ) where
|
| 91 | 88 | |
| 92 | 89 | import Prelude
|
| 90 | +import qualified GHC.KnownKeyNames as Rebindable
|
|
| 93 | 91 | import GHC.Internal.Data.Data
|
| 94 | 92 | import GHC.Internal.TypeLits (KnownNat, natVal)
|
| 95 | 93 | import GHC.Internal.Read
|
| ... | ... | @@ -7,9 +7,6 @@ |
| 7 | 7 | {-# LANGUAGE Trustworthy #-}
|
| 8 | 8 | {-# LANGUAGE StandaloneDeriving #-}
|
| 9 | 9 | |
| 10 | -{-# OPTIONS_GHC -fno-rebindable-known-names #-}
|
|
| 11 | - -- We import Prelude, hence GHC.KnownKeyNames is available
|
|
| 12 | - |
|
| 13 | 10 | -----------------------------------------------------------------------------
|
| 14 | 11 | -- |
|
| 15 | 12 | -- Module : Data.Functor.Compose
|
| ... | ... | @@ -30,6 +27,7 @@ module Data.Functor.Compose ( |
| 30 | 27 | ) where
|
| 31 | 28 | |
| 32 | 29 | import Prelude
|
| 30 | +import qualified GHC.KnownKeyNames as Rebindable
|
|
| 33 | 31 | import Data.Functor.Classes
|
| 34 | 32 | import Control.Applicative
|
| 35 | 33 | import GHC.Internal.Data.Coerce (coerce)
|
| ... | ... | @@ -4,9 +4,6 @@ |
| 4 | 4 | {-# LANGUAGE Safe #-}
|
| 5 | 5 | {-# LANGUAGE StandaloneDeriving #-}
|
| 6 | 6 | |
| 7 | -{-# OPTIONS_GHC -fno-rebindable-known-names #-}
|
|
| 8 | - -- We import Prelude, hence GHC.KnownKeyNames is available
|
|
| 9 | - |
|
| 10 | 7 | -----------------------------------------------------------------------------
|
| 11 | 8 | -- |
|
| 12 | 9 | -- Module : Data.Functor.Product
|
| ... | ... | @@ -27,6 +24,7 @@ module Data.Functor.Product ( |
| 27 | 24 | ) where
|
| 28 | 25 | |
| 29 | 26 | import Prelude
|
| 27 | +import qualified GHC.KnownKeyNames as Rebindable
|
|
| 30 | 28 | import Control.Applicative
|
| 31 | 29 | import GHC.Internal.Control.Monad (MonadPlus(..))
|
| 32 | 30 | import GHC.Internal.Control.Monad.Fix (MonadFix(..))
|
| ... | ... | @@ -4,9 +4,6 @@ |
| 4 | 4 | {-# LANGUAGE Safe #-}
|
| 5 | 5 | {-# LANGUAGE StandaloneDeriving #-}
|
| 6 | 6 | |
| 7 | -{-# OPTIONS_GHC -fno-rebindable-known-names #-}
|
|
| 8 | - -- We are importing Prelude, hence GHC.KnownKeyNames is available
|
|
| 9 | - |
|
| 10 | 7 | -----------------------------------------------------------------------------
|
| 11 | 8 | -- |
|
| 12 | 9 | -- Module : Data.Functor.Sum
|
| ... | ... | @@ -27,6 +24,7 @@ module Data.Functor.Sum ( |
| 27 | 24 | ) where
|
| 28 | 25 | |
| 29 | 26 | import Prelude
|
| 27 | +import qualified GHC.KnownKeyNames as Rebindable
|
|
| 30 | 28 | import Control.Applicative ((<|>))
|
| 31 | 29 | import GHC.Internal.Data.Data (Data)
|
| 32 | 30 | import Data.Functor.Classes
|
| ... | ... | @@ -164,11 +164,6 @@ module Prelude ( |
| 164 | 164 | type (~)
|
| 165 | 165 | ) where
|
| 166 | 166 | |
| 167 | -import GHC.KnownKeyNames ()
|
|
| 168 | - -- Force a dependency on KnownKeyNames, so that any module that
|
|
| 169 | - -- imports Prelude can rely on KnownKeyNames existing, and hence
|
|
| 170 | - -- can be compiled without -frebindable-known-names
|
|
| 171 | - |
|
| 172 | 167 | import GHC.Internal.Control.Monad
|
| 173 | 168 | import GHC.Internal.System.IO
|
| 174 | 169 | import GHC.Internal.System.IO.Error
|
| 1 | 1 | {-# LANGUAGE Safe #-}
|
| 2 | 2 | |
| 3 | -{-# OPTIONS_GHC -fno-rebindable-known-names #-}
|
|
| 4 | - -- We are importing Prelude, hence GHC.KnownKeyNames is available
|
|
| 5 | 3 | -----------------------------------------------------------------------------
|
| 6 | 4 | -- |
|
| 7 | 5 | -- Module : System.Console.GetOpt
|
| ... | ... | @@ -65,6 +63,7 @@ module System.Console.GetOpt ( |
| 65 | 63 | ) where
|
| 66 | 64 | |
| 67 | 65 | import Prelude
|
| 66 | +import qualified GHC.KnownKeyNames as Rebindable
|
|
| 68 | 67 | import GHC.Internal.Data.List ( isPrefixOf, find )
|
| 69 | 68 | |
| 70 | 69 | -- |What to do with options following non-options
|
| ... | ... | @@ -468,10 +468,12 @@ Wrinkles: |
| 468 | 468 | |
| 469 | 469 | For modules high up in the hierarchy of `base`, a convenient way to
|
| 470 | 470 | do this is to say
|
| 471 | - import GHC.KnownKeyNames
|
|
| 471 | + import qualified GHC.KnownKeyNames as Rebindable
|
|
| 472 | + (Why `qualified` and `as Rebindable`? See (KN1) in
|
|
| 473 | + Note [Overview of known entities] in GHC.Builtin.)
|
|
| 472 | 474 | |
| 473 | 475 | For modules not so high up, you can say
|
| 474 | - import GHC.Internal.Base
|
|
| 476 | + import qualified GHC.Internal.Base as Rebindable
|
|
| 475 | 477 | though you may also need GHC.Internal.Num when numerics are concerned.
|
| 476 | 478 | |
| 477 | 479 | For `ghc-internal` modules below GHC.Internal.Base we have to be more selective.
|
| ... | ... | @@ -49,7 +49,8 @@ module GHC.Internal.OverloadedLabels |
| 49 | 49 | ( IsLabel(..)
|
| 50 | 50 | ) where
|
| 51 | 51 | |
| 52 | -import GHC.Internal.Base
|
|
| 52 | +import GHC.Internal.Types (Symbol)
|
|
| 53 | +import qualified GHC.Internal.Base as Rebindable
|
|
| 53 | 54 | |
| 54 | 55 | class IsLabel (x :: Symbol) a where
|
| 55 | 56 | fromLabel :: a |
| ... | ... | @@ -313,11 +313,9 @@ gen_wrappers (Info _ entries) |
| 313 | 313 | -- don't need the Prelude here so we add NoImplicitPrelude.
|
| 314 | 314 | ++ "{-# OPTIONS_GHC -Wno-deprecations -O0 -fno-do-eta-reduction #-}\n"
|
| 315 | 315 | -- Very important OPTIONS_GHC! See Note [OPTIONS_GHC in GHC.PrimopWrappers]
|
| 316 | - ++ "{-# OPTIONS_GHC -Wno-unused-imports #-}\n"
|
|
| 317 | - -- Don't warn about unused import of GHC.Internal.Base; needed for Typeable bindings
|
|
| 318 | 316 | ++ "module GHC.Internal.PrimopWrappers where\n"
|
| 319 | 317 | ++ "import qualified GHC.Internal.Prim\n"
|
| 320 | - ++ "import GHC.Internal.Base -- For Typeable bindings\n"
|
|
| 318 | + ++ "import qualified GHC.Internal.Base as Rebindable -- For Typeable bindings\n"
|
|
| 321 | 319 | ++ "import GHC.Internal.Tuple ()\n"
|
| 322 | 320 | ++ "import GHC.Internal.Prim (" ++ types ++ ")\n"
|
| 323 | 321 | ++ unlines (concatMap mk_wrapper wrappers)
|