Skip to content

Commit

Permalink
Fix n <= 0 constrained being reported as impossible.
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanG077 committed Jun 11, 2024
1 parent 84f500a commit 689bb50
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 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-natnormalise`](http://hackage.haskell.org/package/ghc-typelits-natnormalise) package

## Unreleased
* Fix `n <= 0` constrained being reported as impossible.

## 0.7.10 *May 22nd 2024*
* Support for GHC 9.10.1

Expand Down
8 changes: 5 additions & 3 deletions src/GHC/TypeLits/Normalise/Unify.hs
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,8 @@ unifiers' ct (S ((P [I i]):ps1)) (S ((P [I j]):ps2))
unifiers' ct s1@(S ps1) s2@(S ps2) = case sopToIneq k1 of
Just (s1',s2',_)
| s1' /= s1 || s2' /= s1
, maybe True (uncurry (&&) . second Set.null) (runWriterT (isNatural s1'))
, maybe True (uncurry (&&) . second Set.null) (runWriterT (isNatural s2'))
, maybe False (uncurry (&&) . second Set.null) (runWriterT (isNatural s1'))
, maybe False (uncurry (&&) . second Set.null) (runWriterT (isNatural s2'))
-> unifiers' ct s1' s2'
_ | null psx
, length ps1 == length ps2
Expand Down Expand Up @@ -655,7 +655,9 @@ isNatural (S []) = return True
isNatural (S [P []]) = return True
isNatural (S [P (I i:ps)])
| i >= 0 = isNatural (S [P ps])
| otherwise = return False
| otherwise = WriterT Nothing
-- If i is not a natural number then their sum *might* be natural,
-- but we simply can't be sure since ps might be zero
isNatural (S [P (V _:ps)]) = isNatural (S [P ps])
isNatural (S [P (E s p:ps)]) = do
sN <- isNatural s
Expand Down
10 changes: 9 additions & 1 deletion tests/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,13 @@ oneLtPowSubst :: forall a b. (b ~ (2^a)) => Proxy a -> Proxy a
oneLtPowSubst = go
where
go :: 1 <= b => Proxy a -> Proxy a
go = id
go = id

givenLEZeroNotImpossible :: forall (a :: Nat) . Proxy a -> a <= 0 => ()
givenLEZeroNotImpossible _ = go (Proxy @(a + a - a))
where
go :: Proxy b -> b <= 0 => ()
go _ = ()

main :: IO ()
main = defaultMain tests
Expand Down Expand Up @@ -611,6 +617,8 @@ tests = testGroup "ghc-typelits-natnormalise"
, testCase "b ~ (2^a) => 1 <= b" $
show (oneLtPowSubst (Proxy :: Proxy 0)) @?=
"Proxy"
, testCase "given a <= 0 is not impossible" $
givenLEZeroNotImpossible (Proxy @0) @?= ()
]
, testGroup "errors"
[ testCase "x + 2 ~ 3 + x" $ testProxy1 `throws` testProxy1Errors
Expand Down

0 comments on commit 689bb50

Please sign in to comment.