lean4-htt/src/Init/Data/BitVec
Siddharth af4a3f2251
feat: BitVec.toInt_abs (#6154)
This PR implements `BitVec.toInt_abs`.


The absolute value of `x : BitVec w` is naively a case split on the sign
of `x`.
However, recall that when `x = intMin w`, `-x = x`.
Thus, the full value of `abs x` is computed by the case split:
- If `x : BitVec w` is `intMin`, then its absolute value is also `intMin
w`, and
  thus `toInt` will equal `intMin.toInt`.
- Otherwise, if `x` is negative, then `x.abs.toInt = (-x).toInt`.
- Finally, when `x` is nonnegative, then `x.abs.toInt = x.toInt`.

```lean
theorem toInt_abs {x : BitVec w} :
  x.abs.toInt =
    if x = intMin w then (intMin w).toInt
    else if x.msb then -x.toInt
    else x.toInt
```

We also provide a variant of `toInt_abs` that
hides the case split for `x` being positive or negative by using
`natAbs`.
```lean
theorem toInt_abs_eq_natAbs {x : BitVec w} : x.abs.toInt =
    if x = intMin w then (intMin w).toInt else x.toInt.natAbs
```

Supercedes https://github.com/leanprover/lean4/pull/5787

---------

Co-authored-by: Tobias Grosser <github@grosser.es>
2024-11-27 03:50:55 +00:00
..
Basic.lean chore: remove >6 month old deprecations (#6057) 2024-11-13 23:21:23 +00:00
BasicAux.lean refactor: redefine unsigned fixed width integers in terms of BitVec (#5323) 2024-10-16 07:28:23 +00:00
Bitblast.lean feat: BitVec.getElem_[sub|neg|sshiftRight'|abs] (#6126) 2024-11-21 07:01:11 +00:00
Folds.lean chore: deprecate duplicated Fin.size_pos (#6025) 2024-11-11 04:06:13 +00:00
Lemmas.lean feat: BitVec.toInt_abs (#6154) 2024-11-27 03:50:55 +00:00