|
1 | | -/-- The number of trailing zeros in the binary representation of `i`. -/ |
2 | 1 | def trailingZeros (i : Int) : Nat := |
3 | 2 | if h : i = 0 then 0 else aux i.natAbs i h (Nat.le_refl _) 0 |
4 | 3 | where |
5 | 4 | aux (k : Nat) (i : Int) (hi : i ≠ 0) (hk : i.natAbs ≤ k) (acc : Nat) : Nat := |
6 | | - match k, (by omega : k ≠ 0) with |
7 | | - | k + 1, _ => |
| 5 | + match k with |
| 6 | + | k + 1 => |
8 | 7 | if h : i % 2 = 0 then aux k (i / 2) (by omega) (by omega) (acc + 1) |
9 | 8 | else acc |
| 9 | + | 0 => by omega |
10 | 10 | termination_by structural k |
11 | 11 |
|
12 | 12 | /-- |
13 | 13 | info: equations: |
14 | 14 | @[defeq] theorem trailingZeros.aux.eq_1 : ∀ (i : Int) (hi : i ≠ 0) (acc k_2 : Nat) (hk_2 : i.natAbs ≤ k_2 + 1), |
15 | 15 | trailingZeros.aux k_2.succ i hi hk_2 acc = if h : i % 2 = 0 then trailingZeros.aux k_2 (i / 2) ⋯ ⋯ (acc + 1) else acc |
| 16 | +@[defeq] theorem trailingZeros.aux.eq_2 : ∀ (i : Int) (hi : i ≠ 0) (acc : Nat) (hk_2 : i.natAbs ≤ 0), |
| 17 | + trailingZeros.aux 0 i hi hk_2 acc = acc |
16 | 18 | -/ |
17 | | -#guard_msgs in |
| 19 | +#guard_msgs(pass trace, all) in |
18 | 20 | #print equations trailingZeros.aux |
| 21 | + |
| 22 | + |
| 23 | +-- set_option trace.Elab.definition.eqns true |
| 24 | +-- set_option trace.split.debug true |
| 25 | +-- set_option trace.Meta.Match.unify true |
| 26 | + |
| 27 | +def trailingZeros' (i : Int) : Nat := |
| 28 | + if h : i = 0 then 0 else aux i.natAbs i h (Nat.le_refl _) 0 |
| 29 | +where |
| 30 | + aux (k : Nat) (i : Int) (hi : i ≠ 0) (hk : i.natAbs ≤ k) (acc : Nat) : Nat := |
| 31 | + match k, (by omega : k ≠ 0) with |
| 32 | + | k + 1, _ => |
| 33 | + if h : i % 2 = 0 then aux k (i / 2) (by omega) (by omega) (acc + 1) |
| 34 | + else acc |
| 35 | + termination_by k |
| 36 | + |
| 37 | +/-- |
| 38 | +info: equations: |
| 39 | +theorem trailingZeros'.aux.eq_1 : ∀ (i : Int) (hi : i ≠ 0) (acc k_2 : Nat) (x_1 : k_2 + 1 ≠ 0) |
| 40 | + (hk_2 : i.natAbs ≤ k_2 + 1), |
| 41 | + trailingZeros'.aux k_2.succ i hi hk_2 acc = |
| 42 | + if h : i % 2 = 0 then trailingZeros'.aux k_2 (i / 2) ⋯ ⋯ (acc + 1) else acc |
| 43 | +-/ |
| 44 | +#guard_msgs(pass trace, all) in |
| 45 | +#print equations trailingZeros'.aux |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +/-- The number of trailing zeros in the binary representation of `i`. -/ |
| 50 | +def trailingZeros2 (i : Int) : Nat := |
| 51 | + if h : i = 0 then 0 else aux i.natAbs i h (Nat.le_refl _) 0 |
| 52 | +where |
| 53 | + aux (k : Nat) (i : Int) (hi : i ≠ 0) (hk : i.natAbs ≤ k) (acc : Nat) : Nat := |
| 54 | + match k with |
| 55 | + | k + 1 => |
| 56 | + if h : i % 2 = 0 then aux k (i / 2) (by omega) (by omega) (acc + 1) |
| 57 | + else acc |
| 58 | + | 0 => by omega |
| 59 | + termination_by structural k |
| 60 | + |
| 61 | +/-- |
| 62 | +info: equations: |
| 63 | +@[defeq] theorem trailingZeros2.aux.eq_1 : ∀ (i : Int) (hi : i ≠ 0) (acc k_2 : Nat) (hk_2 : i.natAbs ≤ k_2 + 1), |
| 64 | + trailingZeros2.aux k_2.succ i hi hk_2 acc = |
| 65 | + if h : i % 2 = 0 then trailingZeros2.aux k_2 (i / 2) ⋯ ⋯ (acc + 1) else acc |
| 66 | +@[defeq] theorem trailingZeros2.aux.eq_2 : ∀ (i : Int) (hi : i ≠ 0) (acc : Nat) (hk_2 : i.natAbs ≤ 0), |
| 67 | + trailingZeros2.aux 0 i hi hk_2 acc = acc |
| 68 | +-/ |
| 69 | +#guard_msgs(pass trace, all) in |
| 70 | +#print equations trailingZeros2.aux |
0 commit comments