Section NORM_SUBST_REC.
Variable n : nat.
Variable lmp:list (C*Mon*Pol).
Let subst_l P := PNSubstL P lmp n n.
Let Pmul_subst P1 P2 := subst_l (P1 ** P2).
Let Ppow_subst := Ppow_N subst_l.
Fixpoint norm_aux (pe:PExpr) : Pol :=
match pe with
| PEO => Pc cO
| PEI => Pc cI
| PEc c => Pc c
| PEX j => mk_X j
| PEadd (PEopp pe1) pe2 => (norm_aux pe2) -- (norm_aux pe1)
| PEadd pe1 (PEopp pe2) => (norm_aux pe1) -- (norm_aux pe2)
| PEadd pe1 pe2 => (norm_aux pe1) ++ (norm_aux pe2)
| PEsub pe1 pe2 => (norm_aux pe1) -- (norm_aux pe2)
| PEmul pe1 pe2 => (norm_aux pe1) ** (norm_aux pe2)
| PEopp pe1 => -- (norm_aux pe1)
| PEpow pe1 n => Ppow_N (fun p => p) (norm_aux pe1) n
end.
Definition norm_subst pe := subst_l (norm_aux pe).
(** Internally, [norm_aux] is expanded in a large number of cases. To speed-up proofs, we use an alternative definition. *)
Definition get_PEopp pe :=
match pe with
| PEopp pe' => Some pe'
| _ => None
end.
Arguments norm_aux !pe : simpl nomatch.
End NORM_SUBST_REC.
Fixpoint interp_PElist (l:list R) (lpe:list (PExpr*PExpr)) {struct lpe} : Prop :=
match lpe with
| nil => True
| (me,pe)::lpe =>
match lpe with
| nil => PEeval l me == PEeval l pe
| _ => PEeval l me == PEeval l pe /\ interp_PElist l lpe
end
end.
Fixpoint mon_of_pol (P:Pol) : option (C * Mon) :=
match P with
| Pc c => if (c ?=! cO) then None else Some (c, mon0)
| Pinj j P =>
match mon_of_pol P with
| None => None
| Some (c,m) =>  Some (c, mkZmon j m)
end
| PX P i Q =>
if Peq Q P0 then
match mon_of_pol P with
| None => None
| Some (c,m) => Some (c, mkVmon i m)
end
else None
end.
Fixpoint mk_monpol_list (lpe:list (PExpr * PExpr)) : list (C*Mon*Pol) :=
match lpe with
| nil => nil
| (me,pe)::lpe =>
match mon_of_pol (norm_subst 0 nil me) with
| None => mk_monpol_list lpe
| Some m => (m,norm_subst 0 nil pe):: mk_monpol_list lpe
end
end.

Lemma norm_subst_ok : forall n l lpe pe,
interp_PElist l lpe ->
PEeval l pe == (norm_subst n (mk_monpol_list lpe) pe)@l.