(*****************************************************)
(**            Logarithm for the base 2              *)

Definition log (x : N.t) : N.t :=
strong_rec 0
           (fun g x =>
              if x << 2 then 0
              else S (g (half x)))
           x.#[global]
Instance log_prewd :
 Proper ((N.eq==>N.eq)==>N.eq==>N.eq)
   (fun g x => if x<<2 then 0 else S (g (half x))).#[global]
Instance log_wd : Proper (N.eq==>N.eq) log.

(* Specifications: *)
Lemma log_good_step : forall n h1 h2, (forall m, m < n -> h1 m == h2 m) -> (if n << 2 then 0 else S (h1 (half n))) == (if n << 2 then 0 else S (h2 (half n))).
Theorem log_init : forall n, n < 2 -> log n == 0.
Theorem log_step : forall n, 2 <= n -> log n == S (log (half n)).
Theorem pow2_log : forall n, 0 < n -> half n < 2^^(log n) <= n.