# How to state mutual/nested lemmas

**URL:** <https://discourse.rocq-prover.org/t/how-to-state-mutual-nested-lemmas/1577>\
**Category:** Using Rocq\
**Created:** [March 2, 2022, 8:31am UTC](https://discourse.rocq-prover.org/t/how-to-state-mutual-nested-lemmas/1577 "2022-03-02T08:31:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![vsiles](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.rocq-prover.org/vsiles/32/274_2.png) [@vsiles](https://discourse.rocq-prover.org/u/vsiles)\
**Post date:** [March 2, 2022, 8:31am UTC](https://discourse.rocq-prover.org/t/how-to-state-mutual-nested-lemmas/1577/1 "2022-03-02T08:31:12Z")

</div>

Hi ! I have a nested inductive type that look like a tree, with a custom induction principle to account for the nested usage of `list`: [shack/lang.v at main · facebookresearch/shack · GitHub](https://github.com/facebookresearch/shack/blob/main/theories/lang.v#L46)

So far I never had to state mutually recursive properties on both `lang_ty` and `list lang_ty`, so this induction principle was enough.

I am now facing a situation where I want to mutually prove something on them. Something like `(∀ ty : lang_ty, P ty) /\ (∀ tys: list lang_ty, Q tys)`. With a mutual inductive family, I could use `Lemma .. with` to state this, but I don’t know how to do this for a nested inductive (or if that’s even possible).

Should I go back and change to a mutual inductive in the first place, or is there a way to state a mutual lemma when using nested inductive types ?

---

<div class="post-metadata">

**Author:** ![MevenBertrand](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.rocq-prover.org/mevenbertrand/32/555_2.png) [@MevenBertrand](https://discourse.rocq-prover.org/u/MevenBertrand)\
**Post date:** [March 2, 2022, 9:15am UTC](https://discourse.rocq-prover.org/t/how-to-state-mutual-nested-lemmas/1577/2 "2022-03-02T09:15:14Z")

</div>

How do you expect to prove `∀ tys: list lang_ty, Q tys`? If you can prove `∀ tys: list lang_ty, Forall P tys -> Q tys`, then the following might suit you:

```auto
Variable P : lang_ty -> Prop.
Variable Q : list lang_ty -> Prop.
...
Hypothesis case_ClassT : forall cname targs, Q targs -> P (ClassT cname targs).
Hypothesis case_List : forall targs, Forall P targs -> Q targs.
...

Definition _lang_ty_list_ind (lang_ty_ind : forall t : lang_ty, P t) (l : list lang_ty) : Q l :=
    let H := (fix fold (xs : list lang_ty) : Forall P xs :=
          match xs with
          | nil => List.Forall_nil _
          | x :: xs => List.Forall_cons x (lang_ty_ind x) (fold xs)
          end) l in
        case_List _ H.

  Fixpoint lang_ty_ind (t : lang_ty) : P t :=
    match t with
    ...
    | ClassT cname targs => case_ClassT cname targs (_lang_ty_list_ind lang_ty_ind targs)
    ...
    end.

  Definition lang_ty_list_ind : forall tys : list_lang_ty, Q tys := _lang_ty_list_ind (lang_ty_ind).

```

I’ll admit it’s probably not the nicest presentation, but it works.  
This is the kind of induction principle we use for typing in [MetaCoq](https://github.com/MetaCoq/metacoq/blob/804d9ee3e64d68187c815ffc5698f5d7df76f303/pcuic/theories/Bidirectional/BDTyping.v#L323) (with a specific case going from a `Forall` over a list to the predicate we’re interested in), and it seems to work quite well there. The main difference is that we do not prove the induction principle directly by induction on the structure, but via an auxiliary definition of size.

---

<div class="post-metadata">

**Author:** ![molossus\_spondee](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.rocq-prover.org/molossus_spondee/32/627_2.png) [@molossus\_spondee](https://discourse.rocq-prover.org/u/molossus_spondee)\
**Post date:** [March 3, 2022, 5:00am UTC](https://discourse.rocq-prover.org/t/how-to-state-mutual-nested-lemmas/1577/3 "2022-03-03T05:00:33Z")

</div>

I faced a similar problem but worse with nested inductive coinductive types.

IIRC it was Brouwer ordinals that are defined as

```auto
Inductive ord := O | S (n: ord) | Lim (p: nat -> ord).

```

In a futile attempt to avoid extensionality I wanted to try

```auto
CoInductive stream A := cons {
  head: A ;
  tail: stream A ;
}.
Inductive ord := O | S (n: ord) | Lim (p: stream ord).

```

But no turns out induction over this encoding is impossible even using sized types in Agda

> <https://gist.github.com/mstewartgallus/c2e6c39e5f45a9b52f836d3ddb4cf6f4>

Anyhow I’d suggest if possible you’d use an encoding like

```auto
| ClassT (cname: tag) (n: nat) (targs: Fin.t n -> lang_ty)

```

That way you don’t need a custom induction principle.

You can also try converting back and forth between this funky encoding and lists or vectors but IDK how well that’d work.

---

<div class="post-metadata">

**Author:** ![Blaisorblade](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.rocq-prover.org/blaisorblade/32/56_2.png) [@Blaisorblade](https://discourse.rocq-prover.org/u/Blaisorblade)\
**Post date:** [March 4, 2022, 10:40am UTC](https://discourse.rocq-prover.org/t/how-to-state-mutual-nested-lemmas/1577/4 "2022-03-04T10:40:28Z")

</div>

> [@molossus\_spondee](#):
>
> Anyhow I’d suggest if possible you’d use an encoding like
> 
> ```auto
> | ClassT (cname: tag) (n: nat) (targs: Fin.t n -> lang_ty)
> 
> ```
> 
> That way you don’t need a custom induction principle.
> 
> You can also try converting back and forth between this funky encoding and lists or vectors but IDK how well that’d work.

That’s an interesting idea! But it’ll probably require extensionality to define decidable equality, and migration is probably going to be annoying.

---

<div class="post-metadata">

**Author:** ![vsiles](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.rocq-prover.org/vsiles/32/274_2.png) [@vsiles](https://discourse.rocq-prover.org/u/vsiles)\
**Post date:** [March 4, 2022, 11:16am UTC](https://discourse.rocq-prover.org/t/how-to-state-mutual-nested-lemmas/1577/5 "2022-03-04T11:16:25Z")

</div>

Thank you for these suggestions, they are very interesting !  
FYI I managed to get around this mutual induction by splitting the problem into smaller bits, that were no longer mutual.

Still I found all this feedback very valuable, thank you

---

<div class="post-metadata">

**Author:** ![molossus\_spondee](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.rocq-prover.org/molossus_spondee/32/627_2.png) [@molossus\_spondee](https://discourse.rocq-prover.org/u/molossus_spondee)\
**Post date:** [March 4, 2022, 4:02pm UTC](https://discourse.rocq-prover.org/t/how-to-state-mutual-nested-lemmas/1577/6 "2022-03-04T16:02:27Z")

</div>

I don’t think extensionality is a problem when you have a function out of the finites. But yeah might not be the nicest encoding.

---

<div class="post-metadata">

**Author:** ![rafoo](https://avatars.discourse-cdn.com/v4/letter/r/f08c70/32.png) [@rafoo](https://discourse.rocq-prover.org/u/rafoo)\
**Post date:** [March 12, 2022, 3:28pm UTC](https://discourse.rocq-prover.org/t/how-to-state-mutual-nested-lemmas/1577/7 "2022-03-12T15:28:26Z")

</div>

You can use `Fixpoint <lemma_name> <params> {struct <param>} : <statement>. Proof. ... Qed.` and even `Fixpoint ... with ... Proof. ... Qed.`

---

<div class="post-metadata">

**Author:** ![vsiles](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.rocq-prover.org/vsiles/32/274_2.png) [@vsiles](https://discourse.rocq-prover.org/u/vsiles)\
**Post date:** [March 13, 2022, 7:20am UTC](https://discourse.rocq-prover.org/t/how-to-state-mutual-nested-lemmas/1577/8 "2022-03-13T07:20:15Z")

</div>

My understanding is that Fixpoint…with is like Lemma…with and will only work with mutual inductive definitions, not nested ones. Am I missing something ?

---

<div class="post-metadata">

**Author:** ![rafoo](https://avatars.discourse-cdn.com/v4/letter/r/f08c70/32.png) [@rafoo](https://discourse.rocq-prover.org/u/rafoo)\
**Post date:** [March 22, 2022, 10:14pm UTC](https://discourse.rocq-prover.org/t/how-to-state-mutual-nested-lemmas/1577/9 "2022-03-22T22:14:18Z")

</div>

For a nested, non-mutual inductive type, a single `Fixpoint` lemma can do the trick:

```auto
Section nested_ind.

  Inductive tree :=
  | Leaf : nat -> tree
  | Single : tree -> tree
  | Node : list tree -> tree.

  Variable P : tree -> Prop.
  Variable Q : list tree -> Prop.

  Variable LeafCase : forall n, P (Leaf n).
  Variable SingleCase : forall t, P t -> P (Single t).
  Variable NodeCase : forall l, Q l -> P (Node l).
  Variable NilCase : Q nil.
  Variable ConsCase : forall t l, P t -> Q l -> Q (cons t l).

  Definition ind_list (l : list tree) (IHt : forall t, P t) : Q l.
  Proof.
    induction l.
    - apply NilCase.
    - apply ConsCase.
       + apply IHt.
       + apply IHl.
  Defined.

  Fixpoint ind (t : tree) {struct t} : P t.
  Proof.
    destruct t.
    - apply LeafCase.
    - apply SingleCase.
      apply ind.
    - apply NodeCase.
      apply ind_list.
      apply ind.
  Qed.

End nested_ind.

```
