-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_rat_to_int.lean
More file actions
26 lines (20 loc) · 923 Bytes
/
Copy pathtest_rat_to_int.lean
File metadata and controls
26 lines (20 loc) · 923 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
-- File: test_rat_to_int.lean
-- Converting Rat to Int when we have proof it's an integer
-- ANSWER: Use q.num directly
--
-- For a rational number q : Rat with proof h : Rat.isInt q,
-- the correct and most direct conversion to Int is simply: q.num
--
-- This works because:
-- 1. Rat.isInt q means q.den = 1 (the denominator is 1)
-- 2. Therefore q = q.num / q.den = q.num / 1 = q.num
-- 3. So q.num is exactly the integer value of q
def ratToInt (q : Rat) (h : Rat.isInt q) : Int := q.num
-- Alternative ways (more verbose but equivalent):
-- Method 1: Using Int.floor (overkill since we know it's already an integer)
def ratToIntFloor (q : Rat) (h : Rat.isInt q) : Int := Int.floor q
-- Method 2: Explicitly using the fact that den = 1
def ratToIntExplicit (q : Rat) (h : Rat.isInt q) : Int := by
-- Since q.den = 1, we can use q.num directly
exact q.num
-- The simplest and most idiomatic way is just q.num