Skip to content

Commit 121a6c8

Browse files
committed
Give TextShow{1,2} quantified superclasses
This mirrors a corresponding change to the `Show1` and `Show2` classes in `base`—see haskell/core-libraries-committee#10. A consequence of this change is that all `TextShow1` instances now require corresponding `TextShow` instances. This affects the `TextShow.Generic` module, as we now have to define a `TextShow` instance for `FromGeneric1`. Similarly, all `TextShow2` instances now require corresponding `TextShow` and `TextShow1` instances. Fixes #56.
1 parent f1eb774 commit 121a6c8

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@
4242
-instance (Show2 f, Show a) => TextShow1 (FromStringShow2 f a)
4343
+instance (Show2 f, TextShow a) => TextShow1 (FromStringShow2 f a)
4444
```
45+
* The `TextShow{1,2}` classes now have quantified superclasses:
46+
47+
```hs
48+
class (forall a. TextShow a => TextShow (f a)) => TextShow1 f where ...
49+
class (forall a. TextShow a => TextShow1 (f a)) => TextShow2 f where ...
50+
```
51+
52+
This mirrors corresponding changes made to `Show1` and `Show2` in the `base`
53+
library. See https://github.com/haskell/core-libraries-committee/issues/10.
54+
55+
Because of this change, any code that defines a `TextShow1` instance for a
56+
data type without a corresponding `TextShow` instance will no longer compile,
57+
so you may need to define more `TextShow` instances to adapt to this change.
58+
Similarly, `TextShow2` instances will now also require corresponding
59+
`TextShow` and `TextShow1` instances.
4560
* The `GTextShow*` classes in `TextShow.Generic`, which power generic
4661
derivation of `TextShow` and `TextShow1` instances, have been split up to
4762
facilitate the addition of a quantified superclass to `TextShow1`. Moreover,

src/TextShow/Classes.hs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
{-# LANGUAGE DeriveDataTypeable #-}
33
{-# LANGUAGE OverloadedStrings #-}
44
{-# LANGUAGE StandaloneDeriving #-}
5+
6+
#if __GLASGOW_HASKELL__ >= 806
7+
{-# LANGUAGE QuantifiedConstraints #-}
8+
#endif
9+
510
{-|
611
Module: TextShow.Classes
712
Copyright: (C) 2014-2017 Ryan Scott
@@ -392,7 +397,11 @@ showbToShowtl sf = toLazyText . sf
392397
-- | Lifting of the 'TextShow' class to unary type constructors.
393398
--
394399
-- /Since: 2/
395-
class TextShow1 f where
400+
class
401+
#if __GLASGOW_HASKELL__ >= 806
402+
(forall a. TextShow a => TextShow (f a)) =>
403+
#endif
404+
TextShow1 f where
396405
-- | 'showbPrec' function for an application of the type constructor
397406
-- based on 'showbPrec' and 'showbList' functions for the argument type.
398407
--
@@ -457,7 +466,21 @@ liftShowtlPrec sp sl = showbPrecToShowtlPrec $ liftShowbPrec (showtlPrecToShowbP
457466
-- | Lifting of the 'TextShow' class to binary type constructors.
458467
--
459468
-- /Since: 2/
460-
class TextShow2 f where
469+
class
470+
#if __GLASGOW_HASKELL__ >= 806
471+
( forall a. TextShow a => TextShow1 (f a)
472+
# if __GLASGOW_HASKELL__ < 900
473+
-- Sadly, pre-9.0 versions of GHC have difficulty inferring this
474+
-- superclass from the one above due to
475+
-- https://gitlab.haskell.org/ghc/ghc/-/issues/17202.
476+
-- As a workaround, we manually expand the superclass above to assist
477+
-- type inference. Without doing this, the text-show test suite would
478+
-- not compile on pre-9.0 versions of GHC.
479+
, forall a b. (TextShow a, TextShow b) => TextShow (f a b)
480+
# endif
481+
) =>
482+
#endif
483+
TextShow2 f where
461484
-- | 'showbPrec' function for an application of the type constructor
462485
-- based on 'showbPrec' and 'showbList' functions for the argument types.
463486
--

src/TextShow/Generic.hs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
{-# LANGUAGE UndecidableInstances #-}
2525

2626
#if __GLASGOW_HASKELL__ >= 800
27-
{-# LANGUAGE DeriveLift #-}
27+
{-# LANGUAGE DeriveLift #-}
28+
#endif
29+
30+
#if __GLASGOW_HASKELL__ >= 806
31+
{-# LANGUAGE QuantifiedConstraints #-}
2832
#endif
2933

3034
{-|
@@ -209,8 +213,21 @@ deriving instance Typeable FromGeneric1
209213
deriving instance ( Data (f a), Typeable f, Typeable a
210214
) => Data (FromGeneric1 f (a :: *))
211215

216+
-- | /Since: 3.10/
217+
instance (Generic1 f, GTextShowB (Rep1 f a)) => TextShow (FromGeneric1 f a) where
218+
showbPrec p = gShowbPrec p . from1 . fromGeneric1
219+
212220
-- | /Since: 3.7.4/
213-
instance (Generic1 f, GTextShowB1 (Rep1 f)) => TextShow1 (FromGeneric1 f) where
221+
instance ( Generic1 f, GTextShowB1 (Rep1 f)
222+
#if __GLASGOW_HASKELL__ >= 806 && __GLASGOW_HASKELL__ < 902
223+
-- Unfortunately, the quantified superclass for GTextShowB1 doesn't
224+
-- work on pre-9.2 versions of GHC, perhaps due to
225+
-- https://gitlab.haskell.org/ghc/ghc/-/issues/14860#note_454218.
226+
-- Fortunately, we can make GHC come to its senses by using an
227+
-- equality constraint.
228+
, g ~ Rep1 f, forall a. TextShow a => GTextShowB (g a)
229+
#endif
230+
) => TextShow1 (FromGeneric1 f) where
214231
liftShowbPrec sp sl p = genericLiftShowbPrec sp sl p . fromGeneric1
215232

216233
-- | A 'Generic' implementation of 'showt'.
@@ -367,6 +384,12 @@ one_hash = mempty; \
367384
two_hash = mempty;
368385
#endif
369386

387+
#if __GLASGOW_HASKELL__ >= 806
388+
#define QUANTIFIED_SUPERCLASS(class_name,f) (forall a. TextShow a => class_name (f a)) =>
389+
#else
390+
#define QUANTIFIED_SUPERCLASS(class_name,f)
391+
#endif
392+
370393
#define GTEXT_SHOW(text_type,show_funs,one_hash,two_hash,gtext_show,gtext_show1,gshow_prec,glift_show_prec,gtext_show_con,gtext_show_con1,gshow_prec_con,glift_show_prec_con,show_prec,lift_show_prec,show_space,show_paren,show_list,show_list_with,from_char,from_string,c1_show_prec,s1_show_prec,product_show_prec,u_char_show_prec,u_double_show_prec,u_float_show_prec,u_int_show_prec,u_word_show_prec) \
371394
{- | Class of generic representation types that can be converted to a \
372395
'text_type'. \
@@ -463,7 +486,8 @@ be converted to a 'text_type'.
463486
\
464487
/Since: 3.10/ \
465488
-}; \
466-
class gtext_show1 f where { \
489+
class QUANTIFIED_SUPERCLASS(gtext_show,f) \
490+
gtext_show1 f where { \
467491
; glift_show_prec :: (Int -> a -> text_type) -> ([a] -> text_type) \
468492
-> Int -> f a -> text_type \
469493
}; \
@@ -492,7 +516,8 @@ the 'ConType' has been determined.
492516
\
493517
/Since: 3.10/ \
494518
-}; \
495-
class gtext_show_con1 f where { \
519+
class QUANTIFIED_SUPERCLASS(gtext_show_con,f) \
520+
gtext_show_con1 f where { \
496521
; glift_show_prec_con :: (Int -> a -> text_type) -> ([a] -> text_type) \
497522
-> ConType -> Int -> f a -> text_type \
498523
}; \

0 commit comments

Comments
 (0)