Require Export Reals.

Open Scope R_scope.

(* Question 1 *)

Eval compute in (3+3)%R.

(* Question 2*) 

Inductive fonction : Set :=
| fid : fonction
| fcst : R -> fonction
| fexp : fonction
| fsin : fonction
| fcos : fonction
| fadd : fonction -> fonction -> fonction
| fmoins : fonction -> fonction -> fonction
| fprod : fonction -> fonction -> fonction
| fcomp : fonction -> fonction -> fonction.

Check sin.

(* Question 3 *)

Definition f_1 := fadd fsin (fprod fexp fcos). 
Definition f_2 := fprod fexp (fcomp fsin (fprod fid fid)).

(* Question 4 *)

Fixpoint interp (phi:fonction) {struct phi} : R-> R :=
fun (x:R) => match phi with
| fid => x
| fcst a => a
| fexp => exp(x)
| fsin => sin(x)
| fcos => cos(x)
| fadd f1 g1 => (interp f1 x) + (interp g1 x)
| fmoins f1 g1 =>  (interp f1 x) - (interp g1 x)
| fprod f1 g1 => (interp f1 x) * (interp g1 x)
| fcomp f1 g1 => interp f1 (interp g1 x)
end.

(* Question 5*) 

Fixpoint deriv (phi: fonction) {struct phi} : fonction :=
match phi with
| fid => fcst 1
| fcst _ => fcst 0
| fexp => fexp
| fsin => fcos
| fcos => (fmoins (fcst 0) fsin)
| fadd f1 g1 => fadd (deriv f1) (deriv g1)
| fmoins f1 g1 => fmoins (deriv f1) (deriv g1)
| fprod f1 g1 => fadd (fprod (deriv f1) g1) (fprod f1 (deriv g1))
| fcomp f1 g1 => fprod (deriv g1) (fcomp (deriv f1) g1)
end.

(* Question 6 *)

Lemma thm : forall f: fonction, forall x, 
  derivable_pt_lim  (interp f) x (interp (deriv f) x) .
intros f; elim f.
simpl.
SearchPattern (derivable_pt_lim _ _ _).
apply derivable_pt_lim_id.
intros r x; simpl.
apply derivable_pt_lim_const.
intros x; simpl.
apply derivable_pt_lim_exp.
intros x; simpl.
apply derivable_pt_lim_sin.
intros x; simpl.
intros eps Heps.
generalize (derivable_pt_lim_cos x eps Heps).
intros Hnew.
elim Hnew.
intros D HD.
exists D.
intros.
replace (Rabs ((cos (x + h) - cos x) / h - (0 - sin x))) with (Rabs ((cos (x + h) - cos x) / h - - sin x)).
apply HD; auto.
apply (f_equal Rabs).
field.
auto.
intros f1 Hf1 f2 Hf2 x;simpl.
apply derivable_pt_lim_plus; auto.
intros f1 Hf1 f2 Hf2 x; simpl.
apply derivable_pt_lim_minus; auto.
intros f1 Hf1 f2 Hf2 x; simpl.
apply derivable_pt_lim_mult; auto.
intros f1 Hf1 f2 Hf2 x; simpl.
replace (interp (deriv f2) x * interp (deriv f1) (interp f2 x)) with (interp (deriv f1) (interp f2 x) * (interp (deriv f2) x )) by ring.
apply (derivable_pt_lim_comp); auto.
Qed.

SearchPattern (derivable_pt_lim _ _ _).
