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

Could not deduce: CLog 2 (n * 2) ~ CLog 2 n + 1 #42

Open
martijnbastiaan opened this issue Jul 7, 2022 · 3 comments · May be fixed by #43
Open

Could not deduce: CLog 2 (n * 2) ~ CLog 2 n + 1 #42

martijnbastiaan opened this issue Jul 7, 2022 · 3 comments · May be fixed by #43

Comments

@martijnbastiaan
Copy link
Member

This should hold for all n*, but the plugin doesn't infer it.

*For n ~ 0 the answer is undefined in both cases

@martijnbastiaan
Copy link
Member Author

martijnbastiaan commented Jul 7, 2022

A workaround would be to define:

import Data.Type.Equality ((:~:)(Refl))
import Unsafe.Coerce (unsafeCoerce)

clog2axiom :: (CLog 2 (n * 2)) :~: ((CLog 2 n) + 1)
clog2axiom = unsafeCoerce Refl

And use it like:

coerceIndices :: forall n. (KnownNat n, 1 <= n) => Index (n*2) -> (Index n, Bool)
coerceIndices = case clog2axiom @n of Refl -> bitCoerce

@martijnbastiaan
Copy link
Member Author

@rowanG077 mentioned that we should be able to add this derivation by patching:

mergeCLog :: ExtraOp -> ExtraOp -> Maybe NormaliseResult

@martijnbastiaan
Copy link
Member Author

martijnbastiaan commented Jul 7, 2022

Finally, some "proof" that this equation holds:

>>> import Clash.Util (clogBase)
>>> import Test.QuickCheck (quickCheck, withMaxSuccess)
>>> clog2 = clogBase 2
>>> prop_eq n = (clog2 (n*2)) == (succ <$> clog2 n)
>>> quickCheck (withMaxSuccess 1000000 prop_eq)
+++ OK, passed 1000000 tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant