# Mutual recursion for nested inductive types

**URL:** <https://discourse.rocq-prover.org/t/mutual-recursion-for-nested-inductive-types/729>\
**Category:** Using Rocq\
**Created:** [March 24, 2020, 1:42pm UTC](https://discourse.rocq-prover.org/t/mutual-recursion-for-nested-inductive-types/729 "2020-03-24T13:42:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![berpeti](https://avatars.discourse-cdn.com/v4/letter/b/76d3ee/32.png) [@berpeti](https://discourse.rocq-prover.org/u/berpeti)\
**Post date:** [March 24, 2020, 1:42pm UTC](https://discourse.rocq-prover.org/t/mutual-recursion-for-nested-inductive-types/729/1 "2020-03-24T13:42:38Z")

</div>

Hello,

I was working on defining formal semantics. I would like to write a function, that computes the list of the variables used in an expression (for the sake of simplicity, in the example code, only empty list, variable and map constructions are present). I’m having problems with the map constructor. When I try to define this function with fmap/map or mutual recursion (as seen in variables2 below) I get an ill-formed recursion error. The only way I was able to construct such a function was with inline fix (as seen in variables).

```
Inductive Expression : Type :=
| EEmptyList
| EVar (s : string)
| EMap (l : list (Expression * Expression)).

Fixpoint variables (e : Expression) : list string :=
match e with
| EEmptyList => []
| EVar x => [x]
| EMap l => (fix fp l := match l with
                         | [] => []
                         | (a,b)::xs => app (app (variables a) (variables b)) (fp xs)
                         end) l
end.

Fixpoint variables2 (e : Expression) : list string :=
match e with
| EEmptyList => []
| EVar x => [x]
| EMap l => fp l
end
with fp l := match l with
| [] => []
| (a, b)::xs => app (app (variables2 a) (variables2 b)) (fp xs)
end.

```

How should I modify the definition of variables2 not to get this error?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![kyod](https://avatars.discourse-cdn.com/v4/letter/k/73ab20/32.png) [@kyod](https://discourse.rocq-prover.org/u/kyod)\
**Post date:** [March 24, 2020, 5:25pm UTC](https://discourse.rocq-prover.org/t/mutual-recursion-for-nested-inductive-types/729/2 "2020-03-24T17:25:21Z")

</div>

Hi @berpeti,

The problem you are encountering is that `Fixpoint` requires its body to do all its recursive calls on an argument that is _syntactically_ a subterm of its main argument. This is called the _guard condition_ and there is some documentation [here](https://coq.inria.fr/refman/language/cic.html?highlight=fixpoint#id29). In your first `variable` Coq can determine that all recursive call in fp are indeed done syntactically on a subterm of `EMap l` but in the second it cannot (I don’t understand the guard condition checker well enough to try to explain this behaviour).

However if you really want to have the second version, it is possible to do so by explaining to Coq why it is a well-defined recursive definition. In this case you are doing an induction on the subterms of your main argument `e : Expression`. You could define the relation between subterms of type `Expression`, prove that this relation is well founded and finally do an induction on this proof of well-foundedness. There are tools to help you to do that such as `Program Fixpoint` or [Equations](https://github.com/mattam82/Coq-Equations). Here is the solution using the latter (that needs to be installed separately):

```auto
From Coq Require Import String List.
From Equations Require Import Equations.

Import ListNotations.

Inductive Expression : Type :=
| EEmptyList
| EVar (s : string)
| EMap (l : list (Expression * Expression)).
  
Equations variables2 (e : Expression) : list string :=
  variables2 EEmptyList := [] ;
  variables2 (EVar x) := [x] ;
  variables2 (EMap l) := fp l
with 
  fp (l : list (Expression * Expression)) : list string :=
  fp [] := [] ;
  fp ((a, b) :: xs) := variables2 a ++ variables2 b ++ fp xs.

```

EDIT: The example above actually does not use the subterm relation on Expression (the one derived by Equations with `Derive Subterm for Expression.` is actually empty so useless…)

---

<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 24, 2020, 11:38pm UTC](https://discourse.rocq-prover.org/t/mutual-recursion-for-nested-inductive-types/729/3 "2020-03-24T23:38:30Z")

</div>

> [@berpeti](#):
>
> The only way I was able to construct such a function was with inline fix (as seen in variables).

That’s already a correct solution; for extra convenience, you can also use recursive functions like `fold_right` there; Coq’s termination checker will inline `fold_right` producing equivalent code. Example (and equivalence proof) below.

More examples appear in [Cpdt.InductiveTypes](http://adam.chlipala.net/cpdt/html/Cpdt.InductiveTypes.html) under the heading “Nested Inductive Types”, which recommends this approach.

While @kyod is correct that you can use `Equations`, and that’s great, it seems overkill in this scenario, as proofs about the resulting definitions learning require special (and slower) tactics (discussed in the Equations documentation).

```coq
(* Your code, self-contained *)
From Coq Require Import String List.

Import ListNotations.

Inductive Expression : Type :=
| EEmptyList
| EVar (s : string)
| EMap (l : list (Expression * Expression)).

Fixpoint variables (e : Expression) : list string :=
  match e with
  | EEmptyList => []
  | EVar x => [x]
  | EMap l => fold_right (fun '(a, b) r => app (app (variables a) (variables b)) r) [] l
  end.
Lemma equiv l :
  fold_right (fun '(a, b) r => app (app (variables a) (variables b)) r) [] l =
  (fix fp l :=
    match l with
    | [] => []
    | (a,b)::xs => app (app (variables a) (variables b)) (fp xs)
    end) l.
Proof. now induction l; [|destruct a; rewrite <-IHl]. Qed.

```

---

<div class="post-metadata">

**Author:** ![berpeti](https://avatars.discourse-cdn.com/v4/letter/b/76d3ee/32.png) [@berpeti](https://discourse.rocq-prover.org/u/berpeti)\
**Post date:** [March 28, 2020, 10:01am UTC](https://discourse.rocq-prover.org/t/mutual-recursion-for-nested-inductive-types/729/4 "2020-03-28T10:01:22Z")

</div>

Thank you very much for your replies. Maybe for our purpose the second solution is better, because we have some proofs about the existing functions with `fix`, but both are useful to us.
