Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

feat(data/rat/floor): add norm_num support for int.{floor,ceil,fract} #16502

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/data/rat/floor.lean
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,46 @@ begin
rwa [q_inv_eq, this.left, this.right, q_num_abs_eq_q_num, mul_comm] at q_inv_num_denom_ineq
end

namespace norm_num
open tactic

/-- A norm_num extension for `int.floor`, `int.ceil`, and `int.fract`. -/
@[norm_num] meta def eval_floor_ceil : expr → tactic (expr × expr)
Copy link
Collaborator

@adomani adomani Sep 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@[norm_num] meta def eval_floor_ceil : expr → tactic (expr × expr)
@[norm_num] meta def eval_floor_ceil_fract : expr → tactic (expr × expr)

maybe, since it deals with int.fract as well.

| e@`(@int.floor %%R %%inst_1 %%inst_2 %%x) := do
q ← x.to_rat,
let z := int.floor q,
let ze := `(z),
t ← to_expr ``(%%e = %%ze),
((), p) ← tactic.solve_aux t (do
tactic.mk_mapp `int.floor_eq_iff [some R, some inst_1, some inst_2] >>= tactic.rewrite_target,
tactic.interactive.norm_num [] (interactive.loc.ns [none])),
p ← instantiate_mvars p,
pure (ze, p)
| e@`(@int.ceil %%R %%inst_1 %%inst_2 %%x) := do
q ← x.to_rat,
let z := int.ceil q,
let ze := `(z),
t ← to_expr ``(%%e = %%ze),
((), p) ← tactic.solve_aux t (do
tactic.mk_mapp `int.ceil_eq_iff [some R, some inst_1, some inst_2] >>= tactic.rewrite_target,
`[norm_num]),
Comment on lines +172 to +174
Copy link
Collaborator

@adomani adomani Sep 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the tactic input to solve_aux end with a done, to make sure that it proves t, or is it unnecessary in this case, since this is an extension anyway?

(Most of my comments are also for my own understanding!)

p ← instantiate_mvars p,
pure (ze, p)
| e@`(@int.fract %%R %%inst_1 %%inst_2 %%x) := do
q ← x.to_rat,
ic ← mk_instance_cache R,
let q' := int.fract q,
(ic, q'e) ← ic.of_rat q',
let z := int.floor q,
let ze := `(z),
t ← to_expr ``(%%e = %%q'e),
((), p) ← tactic.solve_aux t (do
`[rw int.fract_eq_iff; norm_num],
tactic.refine ``(⟨%%ze, _⟩),
`[norm_num]),
p ← instantiate_mvars p,
pure (q'e, p)
| _ := failed

end norm_num
end rat
15 changes: 15 additions & 0 deletions test/norm_num_ext.lean
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import algebra.big_operators.norm_num
import data.nat.squarefree
import data.int.gcd
import data.nat.fib
import data.rat.floor
import data.nat.prime
import data.nat.sqrt_norm_num
import analysis.special_functions.pow
Expand Down Expand Up @@ -301,3 +302,17 @@ example : ∏ i in {1, 4, 9, 16}, nat.sqrt i = 24 := by norm_num
example : ∑ i : fin 2, ∑ j : fin 2, ![![0, 1], ![2, 3]] i j = 6 := by norm_num

end big_operators

section floor

variables (R : Type*) [linear_ordered_field R] [floor_ring R]

example : int.floor (15 / 16 : R) + 1 = 1 := by norm_num
example : int.ceil (15 / 16 : R) + 1 = 2 := by norm_num
example : int.fract (17 / 16 : R) + 1 = 17 / 16 := by norm_num

example : int.floor (-15 / 16 : R) + 1 = 0 := by norm_num
example : int.ceil (-15 / 16 : R) + 1 = 1 := by norm_num
example : int.fract (-17 / 16 : R) - 1 = -1 / 16 := by norm_num

end
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
end
end floor

to please the linter