Zulip Chat Archive

Stream: general

Topic: Recursion in weird place


provemeifyoucan (Dec 06 2023 at 12:18):

Hi there. I managed to get myself to the point when I need to deal with a piece of code that roughly resembles following snippet:

def Eh : Nat -> Type
| Nat.zero => List Unit
| Nat.succ n => List (Eh n)

def rrr : (n:Nat) -> Eh n
| Nat.zero => [()]
| Nat.succ n => let k := rrr n; [k.reverse]

There is a little problem, though, when I give it to lean, it shows me this:

invalid field 'reverse', the environment does not contain 'Eh.reverse'
  k
has type
  Eh n

What can I do to pull myself out of this finicky situation?

Yaël Dillies (Dec 06 2023 at 12:19):

What's .reverse supposed to refer to? There's no reverse in your code snippet.

Mauricio Collares (Dec 06 2023 at 12:23):

List.reverse

Mauricio Collares (Dec 06 2023 at 12:24):

You probably need to unfold Eh at k manually to be able to apply List.reverse

Mauricio Collares (Dec 06 2023 at 12:31):

Actually this works:

def Eh : Nat -> Type
| Nat.zero => List Unit
| Nat.succ n => List (Eh n)

def rrr : (n:Nat) -> Eh n
| Nat.zero => [()]
| Nat.succ Nat.zero => let k := rrr (Nat.zero); [k.reverse]
| Nat.succ (Nat.succ n) => let k := rrr (Nat.succ n); [k.reverse]

Can you see why Nat.succ Nat.zero and Nat.succ (Nat.succ n) "need" to be handled separately?

Yaël Dillies (Dec 06 2023 at 12:34):

Yaël Dillies (Dec 06 2023 at 12:34):

Well spotted!

provemeifyoucan (Dec 06 2023 at 14:14):

Thanks for the answer. From now on I will open stuck terms with patterns as "far" as possible XD.
Also, is there a way to make definitions like this def v := Nat.succ v be accepted?

Yaël Dillies (Dec 06 2023 at 14:24):

partial def v := Nat.succ v

but why do you want that?

Mario Carneiro (Dec 06 2023 at 14:28):

actually that doesn't work

Mario Carneiro (Dec 06 2023 at 14:28):

partial defs have to be functions

Mario Carneiro (Dec 06 2023 at 14:28):

partial def v (_ : Unit) := Nat.succ (v ())

works


Last updated: Dec 20 2023 at 11:08 UTC