Search code examples
coqdependent-type

Transform the lemma leaving previous state on Coq


I want to partially derive functions whose input is a dependent list.

I tried to define deriveP with proving.

Derive is a function in Coquelicot.Derive.

Definition deriveP {P A B}(k:nat)(I:Euc (S P) -> Euc A -> Euc B)
                             (input:Euc A)(train:Euc B)(p :Euc (S P)) 
                              :(lt k (S P)) -> (lt ((S P)-(k+1)) (S P)) -> R.
intros.
pose fk := firstk k (S P) p H.
pose lk := lastk ((S P)-(k+1)) (S P) p H0.
pose pk := EucNth k p.
apply arith_basic in H.
exact ( Derive (fun PK => EucSum (QuadraticError (I (fk +++ (PK ::: lk)) input) train )) pk ).

I can not apply arith_basic poposed by Tiago because H is used in fk. I can apply arith_basic to H before I make fk, but then I can not make fk because There is not k < P.+1.

I want to apply arith_basic to H while leaving k < P.+1.

Please help me.

(***********************************************************)

This is dependent list of R.

Require Import Coq.Reals.Reals.
Require Import Coquelicot.Coquelicot.

Inductive Euc:nat -> Type:=
|RO : Euc 0
|Rn : forall {n:nat}, R -> Euc n -> Euc (S n).

Notation "[ ]" := RO.
Notation "[ r1 , .. , r2 ]" := (Rn r1 .. ( Rn r2 RO ) .. ).
Infix ":::" := Rn (at level 60, right associativity).

Basic list operation.

Definition head {n} (v : Euc (S n)) : R :=
  match v with
  | x ::: _ => x
  end.

Definition tail {n} (v : Euc (S n)) : Euc n :=
  match v with
  | _ ::: v => v
  end.

(* extract the last element *)   
Fixpoint last {n} : Euc (S n) -> R :=
  match n with
  | 0%nat => fun v => head v
  | S n => fun v => last (tail v)
  end.

(* eliminate last element from list *)
Fixpoint but_last {n} : Euc (S n) -> Euc n :=
  match n with
  | 0%nat => fun _ => []
  | S n => fun v => head v ::: but_last (tail v)
  end.
 
(* do the opposite of cons *)
Fixpoint snoc {n} (v : Euc n) (x : R) : Euc (S n) :=
  match v with
  | [] => [x]
  | y ::: v => y ::: snoc v x
  end.

(* extract last k elements *)
Fixpoint lastk k n : Euc n -> (lt k n) -> Euc k := 
  match n with
    |0%nat => fun _ (H : lt k 0) => False_rect _ (Lt.lt_n_O _ H)
    |S n => match k with
              |S m => fun v H => snoc (lastk m n (but_last v) (le_S_n _ _ H)) (last v)
              |0%nat => fun _ H => []
            end
  end.

(* extract first k elements *)
Fixpoint firstk k n : Euc n -> (lt k n) -> Euc k := 
  match n with
    |0%nat => fun _ (H :lt k 0) => False_rect _ (Lt.lt_n_O _ H)
    |S n => match k with
              |S m => fun v H => (head v) ::: firstk m n (tail v) (le_S_n _ _ H)
              |0%nat => fun _ _ => []
            end
  end.

(* extract nth element *)
(* 0 origine *)
Fixpoint EucNth (k:nat) :forall {n}, Euc (S n) -> R:=
 match k with
 | 0%nat => fun _ e => head e
 | S k' => fun n =>
   match n return Euc (S n) -> R with
   | 0%nat => fun e => head e
   | S n' => fun v => EucNth k' (tail v)
   end
 end.

Fixpoint EucAppend {n m} (e:Euc n) (f:Euc m) :Euc (n+m):=
 match e with
 |[] => f
 |e' ::: es => e' ::: (EucAppend es f)
 end.

Infix "+++" := EucAppend (at level 60, right associativity).

Fixpoint QuadraticError {n : nat} (b : Euc n) : Euc n -> Euc n.
refine (match b in Euc n return Euc n -> Euc n with
    |@Rn m x xs => _    
    |@RO => fun H => []
 end).
remember (S m).
intro H; destruct H as [| k y ys].
inversion Heqn0.
inversion Heqn0.
subst; exact ((x - y)^2 ::: QuadraticError _ xs ys).
Defined.

Fixpoint EucSum {A}(e:Euc A) :R:=
 match e with
 | [] => 0%R
 | e' ::: es => e' + (EucSum es)
 end.

Solution

  • I have solved on my own.

    We can duplicate lemma in the sub-goal with generalize tactic.

    Definition deriveP {P A B}(k:nat)(I:Euc (S P) -> Euc A -> Euc B)
                                     (input:Euc A)(train:Euc B)(p :Euc (S P)) 
                                      :(lt k (S P)) -> (lt ((S P)-(k+1)) (S P)) -> R.
    intros.
    generalize H.
    intro H1.
    apply arith_basic in H1.
    pose lk := lastk ((S P)-(k+1)) (S P) p H0.
    pose fk := firstk k (S P) p H.
    pose pk := EucNth k p.
    rewrite (_: (P.+1)%nat = (k + (P.+1 - (k + 1)%coq_nat)%coq_nat.+1)%coq_nat) in I.
    exact ( Derive (fun PK => EucSum (QuadraticError (I (fk +++ (PK ::: lk)) input) train )) pk ).
    apply H1.
    Defined.