Zulip Chat Archive

Stream: Is there code for X?

Topic: Building an `Expr` from a numeric literal


Eric Wieser (Feb 10 2024 at 13:15):

Do we have these hiding anywhere?

def _root_.Lean.Expr.ofNatQ (α : Q(Type u)) (_aα : Q(AddMonoidWithOne $α)) :   Q($α)
  | 0 => q(0)
  | 1 => q(1)
  | n + 2 =>
    have ne : Q() := Lean.mkRawNatLit n
    have ne2 : Q() := Lean.mkRawNatLit (n + 2)
    letI : $ne2 =Q $ne + 2 := ⟨⟩
    q(OfNat.ofNat $ne2)

def _root_.Lean.Expr.ofIntQ (α : Q(Type u)) (_aα : Q(AddGroupWithOne $α)) :   Q($α)
  | .ofNat n => Lean.Expr.ofNatQ α q(inferInstance) n
  | .negSucc n => q(-$(Lean.Expr.ofNatQ α q(inferInstance) (n + 1)))

def _root_.Lean.Expr.ofRatQ (α : Q(Type u)) (_aα : Q(AddGroupWithOne $α)) (_dα : Q(Div $α)) (q : Rat) :
    Q($α) :=
  if q.den = 1 then
    Lean.Expr.ofIntQ α q(inferInstance) q.num
  else
    q($(Lean.Expr.ofIntQ α q(inferInstance) q.num) / $(Lean.Expr.ofNatQ α q(inferInstance) q.den))

Eric Wieser (Feb 10 2024 at 13:16):

docs#Mathlib.Meta.NormNum.mkOfNat is close, but it only works for the first case, and (unecessarily) lives in MetaM


Last updated: May 02 2025 at 03:31 UTC