clash-lang / ghc-typelits-extra

Extra type-level operations on GHC.TypeLits.Nat and a custom solver
Other
16 stars 9 forks source link

Tell GHC that a <= Max a b #14

Closed leonschoorl closed 5 years ago

leonschoorl commented 6 years ago

By telling GHC that a <= Max a b it knows that (Max a b - a) is a valid Nat.

And you can do things like this:

-- | Overwrite either the first a of the last b bits.
overwrite :: forall a b. (KnownNat a, KnownNat b)
          => BitVector (Max a b) -> Either (BitVector a) (BitVector b) -> BitVector (Max a b)
overwrite ab (Left a) = a ++# rest
  where (_, rest) = split ab :: (BitVector a,  BitVector (Max a b - a))
overwrite ab (Right b) = rest ++# b
  where (rest, _) = split ab :: (BitVector (Max a b - b), BitVector b)
martijnbastiaan commented 5 years ago

Superseeded by https://github.com/clash-lang/ghc-typelits-extra/pull/20