Zulip Chat Archive
Stream: new members
Topic: Need help with subtype
Kevin Cheung (Mar 08 2024 at 19:53):
I am having trouble doing something very basic with subtype and I hope I could get some help.
In the following, I want quad
to be a function that takes a natural number and quadruples it. I want the type signature to be Nat → EvenNat.
How does one fix 4 * x
to something that Lean will accept?
def EvenNat := { n // ∃ k : Nat, n = 2 * k}
def quad : ℕ → EvenNat := fun x ↦ 4 * x -- Lean naturally complains that this doesn't work.
Paul Rowe (Mar 08 2024 at 20:07):
An element of a subtype has two parts: the value (which you already provided in your function) and the proof that the value satisfies the required condition (in this case ∃ k : Nat, n = 2 * k
). You can write the following:
def quad : ℕ → EvenNat := fun x ↦ ⟨4 * x, ⟨2*x, by ring⟩⟩
Kevin Cheung (Mar 08 2024 at 20:08):
Ah I see. Thank you!
Last updated: May 02 2025 at 03:31 UTC