Skip to content

Commit

Permalink
Drop equality cases from Min/Max
Browse files Browse the repository at this point in the history
I've recently been playing around with the
[Thoralf](https://github.com/Divesh-Otwani/the-thoralf-plugin) plugin for
more sophisticated solving. Unfortunately, it gets a bit confused with the
equality case in `Min` and `Max`.

I've included CPP guards for earlier GHC versions, which don't support
ghc-typelits-knownnat's KnownBool solver.
  • Loading branch information
bgamari committed Oct 24, 2018
1 parent f1cba7c commit d40821e
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/GHC/TypeLits/Extra.hs
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,32 @@ intToNumber x = smallInteger x
#endif
{-# INLINE intToNumber #-}

-- Note [Reflexive equations]
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~
--
-- Both Min and Max include equations for the reflexive case for GHC < 8.6.
-- This is because with versions after 8.6 the ghc-typelits-knownnat solver is
-- smart enough to solve in the presence of If applications using the KnownBool
-- solver.
--

-- | Type-level 'max'
type family Max (x :: Nat) (y :: Nat) :: Nat where
#if !MIN_VERSION_ghc(8,6,0)
-- See Note [Reflexive equations] above
Max n n = n
#endif
Max x y = If (x <=? y) y x

instance (KnownNat x, KnownNat y) => KnownNat2 $(nameToSymbol ''Max) x y where
natSing2 = SNatKn (max (N.natVal (Proxy @x)) (N.natVal (Proxy @y)))

-- | Type-level 'min'
type family Min (x :: Nat) (y :: Nat) :: Nat where
#if !MIN_VERSION_ghc(8,6,0)
-- See Note [Reflexive equations] above
Min n n = n
#endif
Min x y = If (x <=? y) x y

instance (KnownNat x, KnownNat y) => KnownNat2 $(nameToSymbol ''Min) x y where
Expand Down

0 comments on commit d40821e

Please sign in to comment.