summaryrefslogtreecommitdiff
path: root/docs/users_guide/9.4.1-notes.rst
blob: a77b6b451b9abbd14671a0a02a3c8c38537926ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
.. _release-9-4-1:

Version 9.4.1
==============

Language
~~~~~~~~

- A small change has been made to the way GHC infers types for definitions
  with no type signature: GHC will no longer generalize a function over
  a type variable determined by a functional dependency. For example::

    class C a b | a -> b where
      op :: a -> b -> ()
    f x = op True x

  Previously, GHC inferred ``f :: C Bool b => b -> ()``. However, the functional
  dependency says that only one type could ever be used for ``b``: this function
  is hardly valid "for all" ``b``\ s. With the change, GHC will reject, looking
  for the (non-existent) instance for ``C Bool b``.

  If you want to retain the old behavior, add a (backward-compatible) type signature,
  explicitly requesting this unusual quantification.

- GHC Proposal `#371 <https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0371-non-magical-eq.md>`_ has been implemented. This means:

  * The use of equality constraints no longer requires ``-XGADTs`` or ``-XTypeFamilies``.

  * The use of equality constraint syntax ``a ~ b`` requires ``-XTypeOperators``,
    otherwise results in a warning (:ghc-flag:`-Wtype-equality-requires-operators`).

  * ``(~)`` is now a legal name for a user-defined type operator:
    ::

      class a ~ b where
        ...

    This used to be rejected with "Illegal binding of built-in syntax".

  * The built-in type equality is now exported from ``Data.Type.Equality`` and
    re-exported from ``Prelude``. When ``(~)`` is not in scope, its use results
    in a warning (:ghc-flag:`-Wtype-equality-out-of-scope`).

- There were previously cases around functional dependencies and injective
  type families where the result of type inference would depend on the order
  of constraints, as written in a source file. These cases are fundamentally ambiguous.
  While GHC previously made an arbitrary decision, it now notices the ambiguity
  and rejects the program. This means that some previously accepted programs are
  now rejected. The solution is to add a type annotation or type application to
  resolve the ambiguity.

  This is the fix for :ghc-ticket:`18851`.

Compiler
~~~~~~~~

- New :ghc-flag:`-Wredundant-strictness-flags` that checks for strictness flags
  (``!``) applied to unlifted types, which are always strict.

- New :ghc-flag:`-fprof-late` that adds automatic CCS annotations to all
  top level functions *after* core optimisation have been run.

- A new type of plugin: defaulting plugins. These plugins can propose
  defaults for ambiguous variables that would otherwise cause errors
  just like the built-in defaulting mechanism.

- ``GHC.Plugins.parsedResultAction`` now takes and returns a value of type
  ``ParsedResult``, containing the ``HsParsedModule`` as well as ``PsMessages``,
  which contains warnings and errors encountered by the parser before
  they're shown to the user, as long as none of the errors prevented the AST
  from being built. This means plugins can remove, modify, or add any of these,
  or simply pass them through unchanged.

- The way GHC checks for representation polymorphism has been overhauled:
  all the checks are now done during typechecking. The error messages
  now contain more detailed information about the specific check that was performed.
- A new pragma, :pragma:`OPAQUE`, that ensures that every call of a named function
  annotated with an :pragma:`OPAQUE` pragma remains a call of that named function,
  not some name-mangled variant.

- The parsing of implicit parameters is slightly more permissive, as GHC now allows ::

      foo :: (?ip :: forall a. a -> a)

  without requiring parentheses around ``forall a. a -> a``. Note that implicit
  parameters with such kinds are unlikely to be very useful, due to
  :ghc-ticket:`18759`.

- Changes to the treatment of :extension:`UnboxedSums`:

  - GHC can now parse unboxed sum type constructors ``(# | #)``, ``(# | | #)``,
    ``(# | | | #)``, etc. Partial applications need to be written in prefix form,
    e.g. ``(# | #) Int#``.

  - Unboxed sums now require the :extension:`UnboxedSums` extension to be enabled.

  - The :extension:`UnboxedTuples` extension now implies
    :extension:`UnboxedSums`. This means that code using unboxed sums that
    enabled the :extension:`UnboxedTuples` extension but didn't explicitly
    enable :extension:`UnboxedSums` will continue to work without changes.

- Constructed Product Result analysis (c.f. :ghc-flag:`-fcpr-anal`) has been
  overhauled and will now unbox nestedly, if termination properties of the
  function permit. This allows unboxing of constructed results returned by
  ``IO`` actions. E.g.::

      sumIO :: [Int] -> IO Int
      sumIO []     = return 0
      sumIO (x:xs) = do
        r <- sumIO xs
        return $! x + r

  Note the use of ``$!``: Without it, GHC would be unable to see that evaluation
  of ``r`` and ``x`` terminates (and rapidly, at that). An alternative would be to
  evaluate both with a bang pattern or a ``seq``, but the ``return $! <res>``
  idiom should work more reliably and needs less thinking.

- Demand analysis (cf. :ghc-flag:`-fstrictness`) now integrates a
  Boxity Analysis that tracks whether a function needs a parameter boxed. If
  that is the case, the worker/wrapper transformation (cf.
  :ghc-flag:`-fworker-wrapper`) will not unbox that parameter, leading to less
  reboxing in many cases.

  For reasons of backwards-compatible performance, you may find that the new
  mechanism is too aggressive in a few cases (e.g., still unboxing a parameter
  that is used boxed in a hot path). Do post a bug report with your example!
  Then wrap the uses of the parameter in ``GHC.Exts.lazy`` for a short-term fix.

- Tag inference has been implemented.

  It's a new backend optimization pass aimed at avoiding
  redundant evaluatedness checks. The basic pass is always enabled and not optional.
  When using :ghc-flag:`-fworker-wrapper-cbv` it additionally will generate workers for functions
  with strict arguments, pushing the evaluation+tagging of the arguments into the wrapper
  and allowing the worker to simply assume all arguments are fully evaluated and properly
  tagged. Usually the wrapper will then inline, and if the argument is known to be properly
  tagged at the call site the wrapper will become a no-op. Giving us a more efficient
  worker without adding any overhead. If the argument *isn't* known to be evaluated we
  perform the same amount of work, but do it at call sites instead of inside the called
  function.

  In general :ghc-flag:`-fworker-wrapper-cbv` is very beneficial and can be safely enabled.
  However sadly there are two exceptions. It can break rules for code which made assumptions about
  which functions get a W/W split which now no longer hold.
  See :ghc-ticket:`20364` for the details. For this reason it isn't enabled by default.
  For code which has the proper ``INLINABLE`` (:ref:`inlinable-pragma`) and ``INLINE`` (:ref:`inline-pragma`)
  or that doesn't define any rule-relevant functions this shouldn't happen. The longterm fix here is to
  apply the proper pragmas.
  There is also a known issue where a function taking multiple unlifted arguments can cause excessive
  spilling (:ghc-ticket:`20334`). This seems to be an edge case. But if you think you are hitting this case please
  comment on the ticket so that we can prioritize it accordingly.

- Support for Sun SPARC architecture has been dropped (:ghc-ticket:`16883`).

- A fix for GHC's handling of the XDG Base Directory Specification
  (:ghc-ticket:`6077`, :ghc-ticket:`20684`, :ghc-ticket:`20669`,
  :ghc-ticket:`20660`):

  - For the package database previously in ``~/.ghc/<arch-ver>``, we will
    continue to use the old path if it exists. For example, if the
    ``~/.ghc/x86_64-linux-9.4.1`` directory exists, GHC will use that for its
    user package database. If this directory does not exist, we will use
    ``$XDG_DATA_HOME/ghc/x86_64-linux-9.4.1``. This is in order to give tooling
    like cabal time to migrate

  - For GHCi configuration files previously located in ``~/.ghc/`` like
    ``ghci.conf`` and ``ghci_history``, we will first check if they exist in
    ``~/.ghc`` and use those if they do. However, we will create new files like
    ``ghci_history`` only in ``$XDG_DATA_HOME/ghc``. So if you don't have a
    previous GHC installation which created ``~/.ghc/ghci_history``, the
    history file will be written to ``$XDG_DATA_HOME/ghc``. If you already have
    an older GHC installation which wrote ``~/.ghc/ghci_history``, then GHC
    will continue to write the history to that file.

- The :ghc-flag:`-Wunticked-promoted-constructors` warning is no longer
  enabled with :ghc-flag:`-Wall` (:ghc-ticket:`20531`), as a part of
  long-term push towards Dependent Haskell.

- In GHCi, the :ghci-cmd:`:type` command no longer instantiates quantified
  type variables when given a polymorphic type. (It used to instantiate
  inferred type variables.)

``base`` library
~~~~~~~~~~~~~~~~

- ``GHC.Exts.magicDict`` has been renamed to ``withDict`` and given a more
  specific type: ::

        withDict :: forall {rr :: RuntimeRep} st dt (r :: TYPE rr). st -> (dt => r) -> r

  Unlike ``magicDict``, ``withDict`` can be used without defining an
  intermediate data type. For example, the ``withTypeable`` function from the
  ``Data.Typeable`` module can now be defined as: ::

        withTypeable :: forall k (a :: k) rep (r :: TYPE rep). ()
                     => TypeRep a -> (Typeable a => r) -> r
        withTypeable rep k = withDict @(TypeRep a) @(Typeable a) rep k

  Note that the explicit type applications are required, as the call to
  ``withDict`` would be ambiguous otherwise.

``ghc-prim`` library
~~~~~~~~~~~~~~~~~~~~

- Primitive types and functions which handle boxed values are now levity-polymorphic,
  meaning that they now also work with unlifted boxed values (i.e. values whose type
  has kind ``TYPE (BoxedRep Unlifted)``).

  The following type constructors are now levity-polymorphic:

    - ``Array#``, ``SmallArray#``, ``Weak#``, ``StablePtr#``, ``StableName#``,

    - ``MutableArray#``, ``SmallMutableArray#``, ``MutVar#``,
      ``TVar#``, ``MVar#``, ``IOPort#``.

  For example, ``Array#`` used to have kind: ::

        Type -> UnliftedType

  but it now has kind: ::

        forall {l :: Levity}. TYPE (BoxedRep l) -> UnliftedType

  Similarly, ``MutVar#`` used to have kind: ::

        Type -> Type -> UnliftedType

  but it now has kind: ::

        forall {l :: Levity}. Type -> TYPE (BoxedRep l) -> UnliftedType

  This means that in ``Array# a``, ``MutableArray# s a``, ``MutVar# s a``, ...,
  the element type ``a``, must always be boxed, but it can now either be lifted
  or unlifted.
  In particular, arrays and mutable variables can now be used to store
  other arrays and mutable variables.

  All functions which use these updated primitive types are also levity-polymorphic:

    - all array operations (reading/writing/copying/...), for both arrays and small arrays,
      mutable and immutable:

      - ``newArray#``, ``readArray#``, ``writeArray#``, ``sizeofArray#``, ``sizeofMutableArray#``, ``indexArray#``,
        ``unsafeFreezeArray#``, ``unsafeThawArray#``, ``copyArray#``, ``copyMutableArray#``, ``cloneArray#``,
        ``cloneMutableArray#``, ``freezeArray#``, ``thawArray#``, ``casArray#``,

      - ``newSmallArray#``, ``shrinkSmallMutableArray#``, ``readSmallArray#``, ``writeSmallArray#``, ``sizeofSmallArray#``,
        ``getSizeofSmallMutableArray#``, ``indexSmallArray#``, ``unsafeFreezeSmallArray#``,
        ``unsafeThawSmallArray#``, ``copySmallArray#``, ``copySmallMutableArray#``, ``cloneSmallArray#``,
        ``cloneSmallMutableArray#``, ``freezeSmallArray#``, ``thawSmallArray#``, ``casSmallArray#``,

    - ``newMutVar#``,``readMutVar#``,``writeMutV#``,``casMutVar#``,

    - operations on ``MVar#`` and ``TVar#``:

      - ``newTVar#``, ``readTVar#``, ``readTVarIO#``, ``writeTVar#``,

      - ``newMVar#``, ``takeMVar#``, ``tryTakeMVar#``, ``putMVar#``,
        ``tryPutMVar#``, ``readMVar#``, ``tryReadMVar#``,

    - ``STM`` operations ``atomically#``, ``retry#``, ``catchRetry#`` and ``catchSTM#``.

    - ``newIOPort#``, ``readIOPort#``, ``writeIOPort#``,

    - ``mkWeak#``, ``mkWeakNoFinalizer#``, ``addCFinalizerToWeak#``, ``deRefWeak#``, ``finalizeWeak#``,

    - ``makeStablePtr#``, ``deRefStablePtr#``, ``eqStablePtr#``, ``makeStableName#``, ``stableNameToInt#``,

  For example, the full type of ``newMutVar#`` is now: ::

        newMutVar#
          :: forall {l :: Levity} s (a :: TYPE (BoxedRep l)).
             a -> State# s -> (# State# s, MVar# s a #)

  and the full type of ``writeSmallArray#`` is: ::

        writeSmallArray#
          :: forall {l :: Levity} s (a :: TYPE (BoxedRep l)).
             SmallMutableArray# s a -> Int# -> a -> State# s -> State# s

- ``ArrayArray#` and ``MutableArrayArray#`` have been moved from ``GHC.Prim`` to ``GHC.Exts``.
  They are deprecated, because their functionality is now subsumed by ``Array#``
  and ``MutableArray#``.

- ``mkWeak#``, ``mkWeakNoFinalizer#``, ``touch#``
  and ``keepAlive#`` are now levity-polymorphic instead of
  representation-polymorphic. For instance: ::

        mkWeakNoFinalizer#
          :: forall {l :: Levity} {k :: Levity}
                    (a :: TYPE (BoxedRep l))
                    (b :: TYPE (BoxedRep k)).
             a -> b -> State# RealWorld -> (# State# RealWorld, Weak# b #)

  That is, the type signature now quantifies over the ``GHC.Exts.Levity`` of ``a``
  instead of its ``GHC.Exts.RuntimeRep``. In addition, this variable is now inferred,
  instead of specified, meaning that it is no longer eligible for visible type application.
  Note that ``b`` is now also levity-polymorphic, due to the change outlined in the
  previous point.

- Primitive functions for throwing and catching exceptions are now more polymorphic
  than before. For example, ``catch#`` now has type: ::

        catch#
          :: forall {r :: RuntimeRep} {l :: Levity}
                    (a :: TYPE r)
                    (b :: TYPE (BoxedRep l)).
              ( State# RealWorld -> (# State# RealWorld, a #) )
          -> ( b -> State# RealWorld -> (# State# RealWorld, a #) )
          -> State# RealWorld -> (# State# RealWorld, a #)

  The following functions have been generalised in this way:

    - ``catch#``,

    - ``raise#``, ``raiseIO#``,

    - ``maskAsyncExceptions#``, ``maskUninterruptible#``, ``unmaskAsyncExceptions#``.

  Note in particular that ``raise#`` is now both representation-polymorphic
  (with an inferred `RuntimeRep` argument) and levity-polymorphic, with type: ::

      raise# :: forall {l :: Levity} {r :: RuntimeRep}
                       (a :: TYPE (BoxedRep l))
                       (b :: TYPE r).
                a -> b

- ``fork#`` and ``forkOn#`` are now representation-polymorphic. For example, ``fork#``
  now has type: ::

      fork# :: forall {r :: RuntimeRep} (a :: TYPE r).
               (State# RealWorld -> (# State# RealWorld, a #))
            -> (State# RealWorld -> (# State# RealWorld, a #))

- ``GHC.Exts.reallyUnsafePtrEquality#`` has been made more general, as it is now
  both levity-polymorphic and heterogeneous: ::

        reallyUnsafePtrEquality#
          :: forall {l :: Levity} {k :: Levity}
                    (a :: TYPE (BoxedRep l))
                    (b :: TYPE (BoxedRep k))
          . a -> b -> Int#

  This means that ``GHC.Exts.reallyUnsafePtrEquality#`` can be used
  on primitive arrays such as ``GHC.Exts.Array#`` and ``GHC.Exts.ByteArray#``.
  It can also be used on values of different types, without needing to call
  ``GHC.Exts.unsafeCoerce#``.

- Added ``GHC.Exts.reallyUnsafePtrEquality`` which recovers the
  previous behaviour of ``GHC.Exts.reallyUnsafePtrEquality#``: ::

        reallyUnsafePtrEquality :: forall (a :: Type). a -> a -> Int#

- Added ``GHC.Exts.sameArray#``, ``GHC.Exts.sameSmallArray#``,
  ``GHC.Exts.sameByteArray#`` and ``GHC.Exts.sameArrayArray#``: ::

        sameArray# :: Array# a -> Array# a -> Int#
        sameSmallArray# :: SmallArray# a -> SmallArray# a -> Int#
        sameByteArray# :: ByteArray# -> ByteArray# -> Int#
        sameArrayArray# :: ArrayArray# -> ArrayArray# -> Int#

``ghc`` library
~~~~~~~~~~~~~~~

- A new ``GHC.Hs.Syn.Type`` module has been introduced which defines functions
  for computing the ``Type`` of an ``HsExpr GhcTc`` in a pure fashion.
  The ``hsLitType`` and ``hsPatType`` functions that previously lived in
  ``GHC.Tc.Utils.Zonk`` have been moved to this module.

- A ``Typeable`` constraint has been added to ``fromStaticPtr`` in the
  class ``GHC.StaticPtr.IsStatic``. GHC automatically wraps each use of
  the ``static`` keyword with ``fromStaticPtr``. Because ``static`` requires
  its argument to be an instance of ``Typeable``, ``fromStaticPtr`` can
  safely carry this constraint as well.

- The ``newWanted`` function exported by ``GHC.Tc.Plugin`` now passes on
  the full ``CtLoc`` instead of reconstituting it from the type-checking
  environment. This makes ``newWanted`` consistent with ``newGiven``.
  For authors of type-checking plugins, this means you don't need to wrap
  a call to ``newWanted`` in ``setCtLocM`` to create a new Wanted constraint
  with the provided ``CtLoc``.

- GHC no longer carries ``Derived`` constraints. Accordingly, several functions
  in the plugin architecture that previously passed or received three sets of
  constraints (givens, deriveds, and wanteds) now work with two such sets.