/-
Copyright (c) 2019 Johannes Hölzl. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Johannes Hölzl, Mario Carneiro, Yaël Dillies
-/
module
public import Batteries.Classes.RatCast
public import Mathlib.Data.Nat.Notation
/-!
# Basic definitions around the rational numbers
This file declares `ℚ` notation for the rationals and defines the nonnegative rationals `ℚ≥0`.
This file is eligible to upstreaming to Batteries.
-/
@[expose] public section
@[inherit_doc] notation "ℚ" => Rat
/-- Nonnegative rational numbers. -/
def NNRat := {q : ℚ // 0 ≤ q}
@[inherit_doc] notation "ℚ≥0" => NNRat
/-!
### Cast from `NNRat`
This section sets up the typeclasses necessary to declare the canonical embedding `ℚ≥0` to any
semifield.
-/
/-- Typeclass for the canonical homomorphism `ℚ≥0 → K`.
This should be considered as a notation typeclass. The sole purpose of this typeclass is to be
extended by `DivisionSemiring`. -/
class NNRatCast (K : Type*) where
/-- The canonical homomorphism `ℚ≥0 → K`.
Do not use directly. Use the coercion instead. -/
protected nnratCast : ℚ≥0 → K
instance NNRat.instNNRatCast : NNRatCast ℚ≥0 where nnratCast q := q
variable {K : Type*} [NNRatCast K]
/-- Canonical homomorphism from `ℚ≥0` to a division semiring `K`.
This is just the bare function in order to aid in creating instances of `DivisionSemiring`. -/
@[coe, reducible, match_pattern] protected def NNRat.cast : ℚ≥0 → K := NNRatCast.nnratCast
-- See note [coercion into rings]
instance NNRatCast.toCoeTail : CoeTail ℚ≥0 K where coe := NNRat.cast
-- See note [coercion into rings]
instance NNRatCast.toCoeHTCT : CoeHTCT ℚ≥0 K where coe := NNRat.cast
instance Rat.instNNRatCast : NNRatCast ℚ := ⟨Subtype.val⟩
/-! ### Numerator and denominator of a nonnegative rational -/
namespace NNRat
/-- The numerator of a nonnegative rational. -/
def num (q : ℚ≥0) : ℕ := (q : ℚ).num.natAbs
/-- The denominator of a nonnegative rational. -/
def den (q : ℚ≥0) : ℕ := (q : ℚ).den
@[simp] lemma num_mk (q : ℚ) (hq : 0 ≤ q) : num ⟨q, hq⟩ = q.num.natAbs := rfl
@[simp] lemma den_mk (q : ℚ) (hq : 0 ≤ q) : den ⟨q, hq⟩ = q.den := rfl
@[norm_cast] lemma cast_id (n : ℚ≥0) : NNRat.cast n = n := rfl
@[simp] lemma cast_eq_id : NNRat.cast = id := rfl
end NNRat
namespace Rat
@[norm_cast] lemma cast_id (n : ℚ) : Rat.cast n = n := rfl
@[simp] lemma cast_eq_id : Rat.cast = id := rfl
end Rat
¤ Dauer der Verarbeitung: 0.10 Sekunden
¤
*© Formatika GbR, Deutschland