Zulip Chat Archive

Stream: new members

Topic: can this nesting be implemented in lean?


Alok Singh (Jan 02 2024 at 06:55):

def listify : Nat -> ?
| 0        => []
| n + 1    => [listify n]

Is it possible to fill in the '? 'legally?

Siddhartha Gadgil (Jan 02 2024 at 07:04):

The type depends on n so you cannot. But you can make a dependent function if you want.

Alok Singh (Jan 02 2024 at 07:04):

can you hint me as to how? implementing it seems useful practice

Siddhartha Gadgil (Jan 02 2024 at 07:06):

Here is an example

def listType : Nat  Type
  | 0 => List Unit
  | n + 1 => List (listType n)

def listify : (n : Nat)  listType n
  | 0 => []
  | n + 1 => [listify n]

Alok Singh (Jan 02 2024 at 10:26):

Is there a clean way to do it more inline, without defining a separate function listType?

Eric Wieser (Jan 02 2024 at 10:30):

Defining the separate type is the cleaner option, but you can write:

def listify : (n : Nat)  n.rec (List Unit) (fun _ x => List x)
  | 0 => []
  | n + 1 => [listify n]

Alok Singh (Jan 02 2024 at 10:31):

ok yeah the separate way is way better than that

Eric Wieser (Jan 02 2024 at 10:59):

This also works:

def listify : (n : Nat)  ((by exact List : Function.End _)^(n+1)) Unit
  | 0 => []
  | n + 1 => [listify n]

Last updated: May 02 2025 at 03:31 UTC