(**
This file is part of the Flocq formalization of floating-point
arithmetic in Coq: http://flocq.gforge.inria.fr/

Copyright (C) 2010-2013 Sylvie Boldo
#<br />#
Copyright (C) 2010-2013 Guillaume Melquiond

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
COPYING file for more details.
*)

(** * Fixed-point format *)
Require Import Fcore_Raux.
Require Import Fcore_defs.
Require Import Fcore_rnd.
Require Import Fcore_generic_fmt.
Require Import Fcore_ulp.
Require Import Fcore_rnd_ne.

Section RND_FIX.

Variable beta : radix.

Notation bpow := (bpow beta).

Variable emin : Z.

(* fixed-point format with exponent emin *)
Definition FIX_format (x : R) :=
  exists f : float beta,
  x = F2R f /\ (Fexp f = emin)%Z.

Definition FIX_exp (e : Z) := emin.

(** Properties of the FIX format *)

Global Instance FIX_exp_valid : Valid_exp FIX_exp.
Proof.
(* Goal: Valid_exp FIX_exp *)
intros k.
(* Goal: and (forall _ : Z.lt (FIX_exp k) k, Z.le (FIX_exp (Z.add k (Zpos xH))) k) (forall _ : Z.le k (FIX_exp k), and (Z.le (FIX_exp (Z.add (FIX_exp k) (Zpos xH))) (FIX_exp k)) (forall (l : Z) (_ : Z.le l (FIX_exp k)), @eq Z (FIX_exp l) (FIX_exp k))) *)
unfold FIX_exp.
(* Goal: and (forall _ : Z.lt emin k, Z.le emin k) (forall _ : Z.le k emin, and (Z.le emin emin) (forall (l : Z) (_ : Z.le l emin), @eq Z emin emin)) *)
split ; intros H.
(* Goal: Z.le emin k *)
(* Goal: and (Z.le emin emin) (forall (l : Z) (_ : Z.le l emin), @eq Z emin emin) *)
now apply Zlt_le_weak.
(* Goal: iff (generic_format beta FIX_exp x) (FIX_format x) *)
split.
(* Goal: Z.le (FIX_exp ex) (FIX_exp ey) *)
apply Zle_refl.
(* Goal: forall (l : Z) (_ : Z.le l emin), @eq Z emin emin *)
now intros _ _.
Qed.

Theorem generic_format_FIX :
  forall x, FIX_format x -> generic_format beta FIX_exp x.
Proof.
(* Goal: forall (x : R) (_ : FIX_format x), generic_format beta FIX_exp x *)
intros x ((xm, xe), (Hx1, Hx2)).
(* Goal: generic_format beta FIX_exp x *)
rewrite Hx1.
(* Goal: generic_format beta FIX_exp (@F2R beta (Float beta xm xe)) *)
now apply generic_format_canonic.
Qed.

Theorem FIX_format_generic :
  forall x, generic_format beta FIX_exp x -> FIX_format x.
Proof.
(* Goal: forall (x : R) (_ : generic_format beta FIX_exp x), FIX_format x *)
intros x H.
(* Goal: FIX_format x *)
rewrite H.
(* Goal: iff (generic_format beta FIX_exp x) (FIX_format x) *)
eexists ; repeat split.
Qed.

Theorem FIX_format_satisfies_any :
  satisfies_any FIX_format.
Proof.
(* Goal: satisfies_any FIX_format *)
refine (satisfies_any_eq _ _ _ (generic_format_satisfies_any beta FIX_exp)).
(* Goal: forall x : R, iff (generic_format beta FIX_exp x) (FIX_format x) *)
intros x.
(* Goal: iff (generic_format beta FIX_exp x) (FIX_format x) *)
split.
(* Goal: forall _ : generic_format beta FIX_exp x, FIX_format x *)
(* Goal: forall _ : FIX_format x, generic_format beta FIX_exp x *)
apply FIX_format_generic.
(* Goal: forall _ : FIX_format x, generic_format beta FIX_exp x *)
apply generic_format_FIX.
Qed.

Global Instance FIX_exp_monotone : Monotone_exp FIX_exp.
Proof.
(* Goal: Monotone_exp FIX_exp *)
intros ex ey H.
(* Goal: Z.le (FIX_exp ex) (FIX_exp ey) *)
apply Zle_refl.
Qed.

Theorem ulp_FIX: forall x, ulp beta FIX_exp x = bpow emin.
Proof.
(* Goal: forall x : R, @eq R (ulp beta FIX_exp x) (Fcore_Raux.bpow beta emin) *)
intros x; unfold ulp.
(* Goal: @eq R (if Req_bool x (IZR Z0) then match negligible_exp FIX_exp with | Some n => Fcore_Raux.bpow beta (FIX_exp n) | None => IZR Z0 end else Fcore_Raux.bpow beta (canonic_exp beta FIX_exp x)) (Fcore_Raux.bpow beta emin) *)
case Req_bool_spec; intros Zx.
(* Goal: @eq R match negligible_exp FIX_exp with | Some n => Fcore_Raux.bpow beta (FIX_exp n) | None => IZR Z0 end (Fcore_Raux.bpow beta emin) *)
(* Goal: @eq R (Fcore_Raux.bpow beta (canonic_exp beta FIX_exp x)) (Fcore_Raux.bpow beta emin) *)
case (negligible_exp_spec FIX_exp).
(* Goal: forall _ : forall n : Z, Z.lt (FIX_exp n) n, @eq R (IZR Z0) (Fcore_Raux.bpow beta emin) *)
(* Goal: forall (n : Z) (_ : Z.le n (FIX_exp n)), @eq R (Fcore_Raux.bpow beta (FIX_exp n)) (Fcore_Raux.bpow beta emin) *)
(* Goal: @eq R (Fcore_Raux.bpow beta (canonic_exp beta FIX_exp x)) (Fcore_Raux.bpow beta emin) *)
intros T; specialize (T (emin-1)%Z); contradict T.
(* Goal: not (Z.lt (FIX_exp (Z.sub emin (Zpos xH))) (Z.sub emin (Zpos xH))) *)
(* Goal: forall (n : Z) (_ : Z.le n (FIX_exp n)), @eq R (Fcore_Raux.bpow beta (FIX_exp n)) (Fcore_Raux.bpow beta emin) *)
(* Goal: @eq R (Fcore_Raux.bpow beta (canonic_exp beta FIX_exp x)) (Fcore_Raux.bpow beta emin) *)
unfold FIX_exp; omega.
(* Goal: forall (n : Z) (_ : Z.le n (FIX_exp n)), @eq R (Fcore_Raux.bpow beta (FIX_exp n)) (Fcore_Raux.bpow beta emin) *)
(* Goal: @eq R (Fcore_Raux.bpow beta (canonic_exp beta FIX_exp x)) (Fcore_Raux.bpow beta emin) *)
intros n _; reflexivity.
(* Goal: @eq R (Fcore_Raux.bpow beta (canonic_exp beta FIX_exp x)) (Fcore_Raux.bpow beta emin) *)
reflexivity.
Qed.


End RND_FIX.
