Zulip Chat Archive

Stream: lean4

Topic: Destructuring tuples in function definitions


Ramesh Balaji (Jul 11 2024 at 20:28):

IIUC this documentation is part of lean3: https://leanprover.github.io/reference/declarations.html, but I see you can destructure the cartesian product types (not sure if tuples is the right word) using

def bar₄ (p :  × ) :  :=
let m, n := p in m + n

And this let statement works in lean4 as well.

Is there a way to destructure the N×N\mathbb{N} \times \mathbb{N} in the function definition itself? In rust, for example, you can destructure tuples like

fn add((a,b): (u32, u32)) -> u32 {
   a + b
}

Sorry for the dumb question, new to lean :smile:

Kyle Miller (Jul 11 2024 at 20:32):

Yeah, here's one way you can write it:

def bar₄ :  ×    :=
  fun m, n => m + n

You can also do this:

def bar₄ :  ×   
  | m, n => m + n

Kyle Miller (Jul 11 2024 at 20:33):

(Usually you'd write (m, n) for products, but it means the same thing.)


Last updated: May 02 2025 at 03:31 UTC