Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Fix n <= 0 constraint being reported as impossible. #83

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
7 changes: 4 additions & 3 deletions src-ghc-9.4/GHC/TypeLits/Normalise.hs
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,9 @@ simplifyNats opts@Opts {..} leqT eqsG eqsW = do
x' = substsSOP subst x
y' = substsSOP subst y
uS = (x',y',b)
leqsG' | isGiven (ctEvidence ct) = (x',y',b):leqsG
| otherwise = leqsG
isG = isGiven (ctEvidence ct)
leqsG' | isG = (x',y',b):leqsG
| otherwise = leqsG
ineqs = concat [ leqsG
, map (substLeq subst) leqsG
, map snd (rights (map fst eqsG))
Expand All @@ -519,7 +520,7 @@ simplifyNats opts@Opts {..} leqT eqsG eqsW = do
evs' <- maybe evs (:evs) <$> evMagic ct knW (subToPred opts leqT k)
simples subst evs' leqsG' xs eqs'

Just (False,_) | null k -> return (Impossible (fst eq))
Just (False,_) | null k && not isG -> return (Impossible (fst eq))
_ -> do
let solvedIneq = mapMaybe runWriterT
-- it is an inequality that can be instantly solved, such as
Expand Down
7 changes: 4 additions & 3 deletions src-pre-ghc-9.4/GHC/TypeLits/Normalise.hs
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,9 @@ simplifyNats opts@Opts {..} ordCond eqsG eqsW = do
x' = substsSOP subst x
y' = substsSOP subst y
uS = (x',y',b)
leqsG' | isGiven (ctEvidence ct) = (x',y',b):leqsG
| otherwise = leqsG
isG = isGiven (ctEvidence ct)
leqsG' | isG = (x',y',b):leqsG
| otherwise = leqsG
ineqs = concat [ leqsG
, map (substLeq subst) leqsG
, map snd (rights (map fst eqsG))
Expand All @@ -652,7 +653,7 @@ simplifyNats opts@Opts {..} ordCond eqsG eqsW = do
evs' <- maybe evs (:evs) <$> evMagic ct knW (subToPred opts ordCond k)
simples subst evs' leqsG' xs eqs'

Just (False,_) | null k -> return (Impossible (fst eq))
Just (False,_) | null k && not isG -> return (Impossible (fst eq))
_ -> do
let solvedIneq = mapMaybe runWriterT
-- it is an inequality that can be instantly solved, such as
Expand Down
2 changes: 2 additions & 0 deletions src/GHC/TypeLits/Normalise/Unify.hs
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,8 @@ isNatural (S [P []]) = return True
isNatural (S [P (I i:ps)])
| i >= 0 = isNatural (S [P ps])
| otherwise = return False
-- 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
7 changes: 6 additions & 1 deletion tests/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,10 @@ 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) . a <= 0 => Proxy a -> Proxy 'True
givenLEZeroNotImpossible p = Proxy @((a + a - a) <=? 0)

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