@@ -27,6 +27,11 @@ import qualified Type.Reflection as TR
27
27
import Data.Kind (Constraint )
28
28
#endif
29
29
30
+ #if MIN_VERSION_base(4,18,0)
31
+ import qualified GHC.TypeLits as TL
32
+ import qualified GHC.TypeNats as TN
33
+ #endif
34
+
30
35
-- $setup
31
36
-- >>> :set -XKindSignatures -XGADTs -XTypeOperators -XStandaloneDeriving -XQuantifiedConstraints
32
37
-- >>> import Data.Type.Equality
@@ -68,6 +73,17 @@ instance GShow ((:~~:) a) where
68
73
instance GShow TR. TypeRep where
69
74
gshowsPrec = showsPrec
70
75
76
+ #if MIN_VERSION_base(4,18,0)
77
+ instance GShow TL. SChar where
78
+ gshowsPrec = showsPrec
79
+
80
+ instance GShow TL. SSymbol where
81
+ gshowsPrec = showsPrec
82
+
83
+ instance GShow TN. SNat where
84
+ gshowsPrec = showsPrec
85
+ #endif
86
+
71
87
--
72
88
-- | >>> gshow (InL Refl :: Sum ((:~:) Int) ((:~:) Bool) Int)
73
89
-- "InL Refl"
@@ -339,6 +355,17 @@ instance (GEq a, GEq b) => GEq (a :*: b) where
339
355
instance GEq TR. TypeRep where
340
356
geq = testEquality
341
357
358
+ #if MIN_VERSION_base(4,18,0)
359
+ instance GEq TL. SChar where
360
+ geq = testEquality
361
+
362
+ instance GEq TL. SSymbol where
363
+ geq = testEquality
364
+
365
+ instance GEq TN. SNat where
366
+ geq = testEquality
367
+ #endif
368
+
342
369
-------------------------------------------------------------------------------
343
370
-- GCompare
344
371
-------------------------------------------------------------------------------
@@ -426,20 +453,42 @@ instance GCompare ((:~~:) a) where
426
453
gcompare HRefl HRefl = GEQ
427
454
428
455
instance GCompare TR. TypeRep where
429
- gcompare t1 t2 =
430
- case testEquality t1 t2 of
431
- Just Refl -> GEQ
432
- Nothing ->
433
- case compare (TR. SomeTypeRep t1) (TR. SomeTypeRep t2) of
434
- LT -> GLT
435
- GT -> GGT
436
- EQ -> error " impossible: 'testEquality' and 'compare' \
437
- \are inconsistent for TypeRep; report this \
438
- \as a GHC bug"
456
+ gcompare = gcompareSing " TypeRep" TR. SomeTypeRep
457
+
458
+ #if MIN_VERSION_base(4,18,0)
459
+ instance GCompare TL. SChar where
460
+ gcompare = gcompareSing " SChar" TL. fromSChar
461
+
462
+ instance GCompare TL. SSymbol where
463
+ gcompare = gcompareSing " SSymbol" TL. fromSSymbol
464
+
465
+ instance GCompare TN. SNat where
466
+ gcompare = gcompareSing " SNat" TN. fromSNat
467
+ #endif
439
468
440
469
defaultCompare :: GCompare f => f a -> f b -> Ordering
441
470
defaultCompare x y = weakenOrdering (gcompare x y)
442
471
472
+ -- | An implementation of 'gcompare' for a singleton type.
473
+ gcompareSing :: (TestEquality f , Ord c )
474
+ => String
475
+ -- ^ The name of the singleton type.
476
+ -- (Only used for error message purposes.)
477
+ -> (forall x . f x -> c )
478
+ -- ^ How to turn the singleton type into a value that can be
479
+ -- compared with 'Ord'.
480
+ -> f a -> f b -> GOrdering a b
481
+ gcompareSing singName toOrd t1 t2 =
482
+ case testEquality t1 t2 of
483
+ Just Refl -> GEQ
484
+ Nothing ->
485
+ case compare (toOrd t1) (toOrd t2) of
486
+ LT -> GLT
487
+ GT -> GGT
488
+ EQ -> error $ " impossible: 'testEquality' and 'compare' are inconsistent for "
489
+ ++ singName
490
+ ++ " ; report this as a GHC bug"
491
+
443
492
instance (GCompare a , GCompare b ) => GCompare (Sum a b ) where
444
493
gcompare (InL x) (InL y) = gcompare x y
445
494
gcompare (InL _) (InR _) = GLT
0 commit comments