feat(Core): add Bv{n}.ToUInt, Bv{n}.ToInt, Int.ToBv{n} cast operators#1217
Conversation
Add three cross-sort conversion operators to Core:
- Bv{n}.ToUInt : Bv{n} → Int (≙ SMT-LIB ubv_to_int / Lean BitVec.toNat)
- Bv{n}.ToInt : Bv{n} → Int (≙ SMT-LIB sbv_to_int / Lean BitVec.toInt)
- Int.ToBv{n} : Int → Bv{n} (≙ SMT-LIB int_to_bv / Lean BitVec.ofInt)
Supported widths: 1, 8, 16, 32, 64, 128. All three are total — no
preconditions, no Safe variants, no axioms. SMT-LIB 2.7 operators are
total and agree pointwise with the Lean BitVec definitions.
Changes:
- CoreOp: BvOpKind.ToUInt / .ToInt (unary, cross-sort); CoreOp.intToBv n
- Factory: bvToUIntFunc / bvToIntFunc / intToBvFunc + per-width instances;
registered in WFFactory (factoryOps count: 286 → 304)
- SMTEncoder: maps .bv ⟨_, .ToUInt⟩ → bv2nat, .bv ⟨_, .ToInt⟩ → sbv2int,
.intToBv → int_to_bv n
- DL/SMT/Op: Op.BV.bv2nat, Op.BV.sbv2int + mkName entries
- DL/SMT/Denote + Translate: handle bv2nat / sbv2int / int_to_bv
- Core DDMTransform/Grammar: add bv128 type + bv128Lit
- Core DDMTransform/Translate: bv128 → .bitvec 128
- Tests: ProgramEvalTests (18 new func entries), StatisticsTest (count bump)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
@kondylidou Could we please have some test programs for each new op in Core, all the way down to SMT? |
Thanks for adding tests, @kondylidou! These tests cover SMT encoding correctness and factory registration. But there's no end-to-end verification test: i.e., a Core program with cast operators that runs through verify and produces pass/fail results from the solver. Good test candidates would exercise properties like:
|
Thanks for the feedback! Added
One note on the implementation: factory ops like |
ubv_to_int and sbv_to_int (SMT-LIB 2.7 BV↔Int casts) are not reliably solved by cvc5 1.2.1; the new cast tests all returned unknown on CI. I aligned the CI with the version that supports these operators.
|
ci: bump cvc5 default 1.2.1 → 1.3.4 ubv_to_int and sbv_to_int (SMT-LIB 2.7 BV↔Int casts) are not reliably |
Two tests merged from main2 used the pre-#1196 `proc.body : List Statement` shape. Adapt them to the new `Procedure.Body` sum (`structured` / `cfg`): - BvIntCastVerifyTests.lean (added by #1217): wrap the literal body list with `.structured`. - Boole/FeatureRequests/seq_empty_literal.lean (added by #1214): pattern- match `proc.body` and iterate the `.structured` arm; skip `.cfg` since the Boole-to-Core lowering does not produce CFG bodies.
Adds three cross-sort conversion operators to Core, as proposed in #1191.
Split from the original PR per reviewer request — Core layer only.
Boole surface syntax follows in a separate PR.
Operators
Bv{n}.ToUIntubv_to_intBitVec.toNatBv{n}.ToIntsbv_to_intBitVec.toIntInt.ToBv{n}(_ int_to_bv n)BitVec.ofInt nSupported widths: 1, 8, 16, 32, 64, 128. All three are total — no
preconditions, no Safe variants, no axioms.
Changes
BvOpKind.ToUInt,BvOpKind.ToInt(unary, cross-sort);CoreOp.intToBv nbvToUIntFunc,bvToIntFunc,intToBvFunc+ per-widthinstances for all 6 widths; registered in
WFFactory(factoryOps: 286 → 304)
.bv ⟨_, .ToUInt⟩→ubv_to_int,.bv ⟨_, .ToInt⟩→sbv_to_int,.intToBv n→(_ int_to_bv n)Op.BV.ubv_to_int,Op.BV.sbv_to_int,Op.BV.int_to_bv n+
mkNameentriesubv_to_int/sbv_to_int/int_to_bvbv128type + literal;bv128 → .bitvec 128bv128cases intypeRange,toCoreMonoType,bvWidth— required becausebv128entersBooleDDM.BooleTypevia the grammar change aboveProgramEvalTests(18 new func entries for all 3 ops × 6widths),
StatisticsTest(count bump)