Skip to content

Commit

Permalink
Move Div and Mod KnownNat2 instances
Browse files Browse the repository at this point in the history
Ensures that the `knownnat` solver can solve `KnownNat`
constraints for the GHC 8.4+ `Div` and `Mod` type families
without having the user import `GHC.TypeLits.Extra`

Fixes some of the issues in #15
  • Loading branch information
christiaanb committed Sep 14, 2018
1 parent b9333c2 commit f1cba7c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog for the [`ghc-typelits-extra`](http://hackage.haskell.org/package/ghc-typelits-extra) package

# 0.3 *September 14th 2018*
* Move `KnownNat2` instances for GHC 8.4's `Div` and `Mod` from `ghc-typelits-extra` to `ghc-typelits-knownnat`

# 0.2.6 *Julty 10th 2018*
* Add support for GHC-8.6.1-alpha1

Expand Down
6 changes: 3 additions & 3 deletions ghc-typelits-extra.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: ghc-typelits-extra
version: 0.2.6
version: 0.3
synopsis: Additional type-level operations on GHC.TypeLits.Nat
description:
Additional type-level operations on @GHC.TypeLits.Nat@:
Expand Down Expand Up @@ -69,7 +69,7 @@ library
ghc >= 7.10 && <8.8,
ghc-prim >= 0.5 && <1.0,
ghc-tcplugins-extra >= 0.2,
ghc-typelits-knownnat >= 0.5 && <0.6,
ghc-typelits-knownnat >= 0.6 && <0.7,
ghc-typelits-natnormalise >= 0.6 && <0.7,
integer-gmp >= 1.0 && <1.1,
transformers >= 0.4.2.0 && <0.6
Expand Down Expand Up @@ -98,7 +98,7 @@ test-suite test-ghc-typelits-extra
Other-Modules: ErrorTests
build-depends: base >= 4.8 && <5,
ghc-typelits-extra,
ghc-typelits-knownnat >= 0.2,
ghc-typelits-knownnat >= 0.6,
ghc-typelits-natnormalise >= 0.4.1,
tasty >= 0.10,
tasty-hunit >= 0.9,
Expand Down
8 changes: 4 additions & 4 deletions src/GHC/TypeLits/Extra.hs
Original file line number Diff line number Diff line change
Expand Up @@ -129,25 +129,25 @@ instance (KnownNat x, KnownNat y) => KnownNat2 $(nameToSymbol ''Min) x y where
-- "GHC.TypeLits.Extra.Solver".
type family Div (x :: Nat) (y :: Nat) :: Nat where
Div x 1 = x

instance (KnownNat x, KnownNat y, 1 <= y) => KnownNat2 $(nameToSymbol ''Div) x y where
natSing2 = SNatKn (quot (N.natVal (Proxy @x)) (N.natVal (Proxy @y)))
#endif

-- | A variant of 'Div' that rounds up instead of down
type DivRU n d = Div (n + (d - 1)) d

instance (KnownNat x, KnownNat y, 1 <= y) => KnownNat2 $(nameToSymbol ''Div) x y where
natSing2 = SNatKn (quot (N.natVal (Proxy @x)) (N.natVal (Proxy @y)))

#if !MIN_VERSION_ghc(8,4,0)
-- | Type-level 'mod'
--
-- Note that additional equations are provided by the type-checker plugin solver
-- "GHC.TypeLits.Extra.Solver".
type family Mod (x :: Nat) (y :: Nat) :: Nat where
Mod x 1 = 0
#endif

instance (KnownNat x, KnownNat y, 1 <= y) => KnownNat2 $(nameToSymbol ''Mod) x y where
natSing2 = SNatKn (rem (N.natVal (Proxy @x)) (N.natVal (Proxy @y)))
#endif

-- | Type-level `divMod`
type DivMod n d = '(Div n d, Mod n d)
Expand Down

0 comments on commit f1cba7c

Please sign in to comment.