Section Well_Formed_Term.
Variable L : Language.
Variable codeF : Functions L -> nat.
Variable codeArityF : nat -> nat.
Hypothesis codeArityFIsPR : isPR 1 codeArityF.
Hypothesis
codeArityFIsCorrect1 :
forall f : Functions L, codeArityF (codeF f) = S (arity L (inr _ f)).
Hypothesis
codeArityFIsCorrect2 :
forall n : nat, codeArityF n <> 0 -> exists f : Functions L, codeF f = n.
Let Term := Term L.
Let Terms := Terms L.
Let var := var L.
Let apply := apply L.
Definition wellFormedTermTerms : nat -> nat :=
evalStrongRec 0
(fun t recs : nat =>
cPair
(switchPR (cPairPi1 t)
(charFunction 2 beq_nat (codeArityF (pred (cPairPi1 t)))
(S (codeLength (cPairPi2 t))) *
cPairPi2 (codeNth (t - S (cPairPi2 t)) recs)) 1)
(switchPR t
(cPairPi1 (codeNth (t - S (cPairPi1 (pred t))) recs) *
cPairPi2 (codeNth (t - S (cPairPi2 (pred t))) recs)) 1)).
Definition wellFormedTerm (t : nat) : nat := cPairPi1 (wellFormedTermTerms t).
Definition wellFormedTerms (ts : nat) : nat :=
cPairPi2 (wellFormedTermTerms ts).

Lemma lengthTerms :
forall (n : nat) (ts : Terms n), codeLength (codeTerms L codeF n ts) = n.