(** * Polymax Bounding

  - The lemma [polymax_bounding] states that the output of an expression [e] with [n] normal variables
    is bounded by the sum of the polynomial [poly_BC n e] and of the size of its longest safe input.
*)

Fixpoint poly_BC n (e : BC) : pol :=
  match e with 
    | zero => pcst 0 0
    | proj n s i => if (leb (S i) n) then pproj n i else (pcst n 0)
    | succ b => pcst 0 1
    | pred => pcst 0 0
    | cond => pcst 0 0
    | rec g h0 h1 =>
        pplus (pshift (poly_BC (n - 1) g)) 
        (pmult (pproj n 0) (pplus (poly_BC n h0) (poly_BC n h1)))
    | comp n s h rl tl =>
      (pplus (pcst n 0)
        (pplus (pcomp (poly_BC (length rl) h) (map (poly_BC n) rl ))
          (pplusl (map (poly_BC n) tl))))
  end.

Lemma polymax_bounding : forall (e : BC) xl yl n s, arities e = ok_arities n s -> length (sem e xl yl) <= peval (poly_BC n e) (map (@length _) xl) + maxl (map (@length _) yl).