(* This program 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 2.1   *)
(* of the License, or (at your option) any later version.             *)
(*                                                                    *)
(* This program 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      *)
(* GNU General Public License for more details.                       *)
(*                                                                    *)
(* You should have received a copy of the GNU Lesser General Public   *)
(* License along with this program; if not, write to the Free         *)
(* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *)
(* 02110-1301 USA                                                     *)
(* définition des fonctions de modification des adresses *)
Definition uad_conv_0 (a : ad) : ad :=
match a with
| N0 => N0
| Npos p => Npos (xO p)
end.
Definition uad_conv_1 (a : ad) : ad :=
match a with
| N0 => Npos 1
| Npos p => Npos (xI p)
end.
(* calcul des automates modifiés *)
Fixpoint upl_conv_0 (p : prec_list) : prec_list :=
match p with
| prec_empty => prec_empty
| prec_cons a p0 p1 =>
prec_cons (uad_conv_0 a) (upl_conv_0 p0) (upl_conv_0 p1)
end.
Fixpoint upl_conv_1 (p : prec_list) : prec_list :=
match p with
| prec_empty => prec_empty
| prec_cons a p0 p1 =>
prec_cons (uad_conv_1 a) (upl_conv_1 p0) (upl_conv_1 p1)
end.
Fixpoint umpl_conv_0 (s : state) : state :=
match s with
| M0 => M0 prec_list
| M1 a p => M1 prec_list a (upl_conv_0 p)
| M2 p0 p1 => M2 prec_list (umpl_conv_0 p0) (umpl_conv_0 p1)
end.
Fixpoint umpl_conv_1 (s : state) : state :=
match s with
| M0 => M0 prec_list
| M1 a p => M1 prec_list a (upl_conv_1 p)
| M2 p0 p1 => M2 prec_list (umpl_conv_1 p0) (umpl_conv_1 p1)
end.
Fixpoint udta_conv_0_aux (d : preDTA) : preDTA :=
match d with
| M0 => M0 state
| M1 a s => M1 state a (umpl_conv_0 s)
| M2 s0 s1 => M2 state (udta_conv_0_aux s0) (udta_conv_0_aux s1)
end.
Fixpoint udta_conv_1_aux (d : preDTA) : preDTA :=
match d with
| M0 => M0 state
| M1 a s => M1 state a (umpl_conv_1 s)
| M2 s0 s1 => M2 state (udta_conv_1_aux s0) (udta_conv_1_aux s1)
end.
Definition udta_conv_0 (d : preDTA) : preDTA :=
M2 state (udta_conv_0_aux d) (M0 state).
Definition udta_conv_1 (d : preDTA) : preDTA :=
M2 state (M0 state) (udta_conv_1_aux d).
(* lemmes d'injectivité *)
(* lemmes sur les images des applications précédentes *)
(* invariants sur les MapGet pour conv_0 *)
(* invariant de reconnaissance sur conv_0 : sens direct *)
Definition u_conv_rec_0 (p : preDTA) (a : ad) (t : term)
(pr : reconnaissance p a t) :=
reconnaissance (udta_conv_0 p) (uad_conv_0 a) t.
Definition u_conv_str_0 (p : preDTA) (s : state) (t : term)
(pr : state_reconnait p s t) :=
state_reconnait (udta_conv_0 p) (umpl_conv_0 s) t.
Definition u_conv_lr_0 (p : preDTA) (p0 : prec_list)
(t : term_list) (pr : liste_reconnait p p0 t) :=
liste_reconnait (udta_conv_0 p) (upl_conv_0 p0) t.
(* invariant de reconnaissance sur conv_0 : sens reciproque *)
Definition u_conv_rec_0_r (p0 : preDTA) (a0 : ad) (t : term)
(pr0 : reconnaissance p0 a0 t) :=
forall (p : preDTA) (a : ad),
p0 = udta_conv_0 p -> a0 = uad_conv_0 a -> reconnaissance p a t.
Definition u_conv_str_0_r (p0 : preDTA) (s0 : state)
(t : term) (pr : state_reconnait p0 s0 t) :=
forall (p : preDTA) (s : state),
p0 = udta_conv_0 p -> s0 = umpl_conv_0 s -> state_reconnait p s t.
Definition u_conv_lr_0_r (p0 : preDTA) (pl0 : prec_list)
(t : term_list) (pr : liste_reconnait p0 pl0 t) :=
forall (p : preDTA) (pl : prec_list),
p0 = udta_conv_0 p -> pl0 = upl_conv_0 pl -> liste_reconnait p pl t.
(* invariants sur les MapGet : conv_1 *)
(* invariant de reconnaissance de conv_1 *)
Definition u_conv_rec_1 (p : preDTA) (a : ad) (t : term)
(pr : reconnaissance p a t) :=
reconnaissance (udta_conv_1 p) (uad_conv_1 a) t.
Definition u_conv_str_1 (p : preDTA) (s : state) (t : term)
(pr : state_reconnait p s t) :=
state_reconnait (udta_conv_1 p) (umpl_conv_1 s) t.
Definition u_conv_lr_1 (p : preDTA) (p0 : prec_list)
(t : term_list) (pr : liste_reconnait p p0 t) :=
liste_reconnait (udta_conv_1 p) (upl_conv_1 p0) t.
(* invariant de reconnaissance sur conv_1 : sens reciproque *)
Definition u_conv_rec_1_r (p0 : preDTA) (a0 : ad) (t : term)
(pr0 : reconnaissance p0 a0 t) :=
forall (p : preDTA) (a : ad),
p0 = udta_conv_1 p -> a0 = uad_conv_1 a -> reconnaissance p a t.
Definition u_conv_str_1_r (p0 : preDTA) (s0 : state)
(t : term) (pr : state_reconnait p0 s0 t) :=
forall (p : preDTA) (s : state),
p0 = udta_conv_1 p -> s0 = umpl_conv_1 s -> state_reconnait p s t.
Definition u_conv_lr_1_r (p0 : preDTA) (pl0 : prec_list)
(t : term_list) (pr : liste_reconnait p0 pl0 t) :=
forall (p : preDTA) (pl : prec_list),
p0 = udta_conv_1 p -> pl0 = upl_conv_1 pl -> liste_reconnait p pl t.
(* udta_conv_0 et udta_conv_1 ont des images disjointes *)
(* définition de l'union des supports de deux automates *)
Definition u_merge (p0 p1 : preDTA) : preDTA :=
MapMerge state (udta_conv_0 p0) (udta_conv_1 p1).
(* invariants des MapGet au travers de u_merge *)
(* invariant de reconnaissance pour conv_0 au travers de u_merge *)
Definition u_merge_inv_0_dta (p0 : preDTA) (a : ad)
(t : term) (pr : reconnaissance p0 a t) :=
forall p1 : preDTA, reconnaissance (u_merge p0 p1) (uad_conv_0 a) t.
Definition u_merge_inv_0_st (p0 : preDTA) (s : state)
(t : term) (pr : state_reconnait p0 s t) :=
forall p1 : preDTA, state_reconnait (u_merge p0 p1) (umpl_conv_0 s) t.
Definition u_merge_inv_0_lst (p0 : preDTA) (pl : prec_list)
(lt : term_list) (pr : liste_reconnait p0 pl lt) :=
forall p1 : preDTA, liste_reconnait (u_merge p0 p1) (upl_conv_0 pl) lt.
(* invariant de reconnaissance pour conv_1 au travers de u_merge *)
Definition u_merge_inv_1_dta (p1 : preDTA) (a : ad)
(t : term) (pr : reconnaissance p1 a t) :=
forall p0 : preDTA, reconnaissance (u_merge p0 p1) (uad_conv_1 a) t.
Definition u_merge_inv_1_st (p1 : preDTA) (s : state)
(t : term) (pr : state_reconnait p1 s t) :=
forall p0 : preDTA, state_reconnait (u_merge p0 p1) (umpl_conv_1 s) t.
Definition u_merge_inv_1_lst (p1 : preDTA) (pl : prec_list)
(lt : term_list) (pr : liste_reconnait p1 pl lt) :=
forall p0 : preDTA, liste_reconnait (u_merge p0 p1) (upl_conv_1 pl) lt.
(* invariant pour u_merge : sens réciproque pour u_conv_0 *)
Definition u_merge_invr_0_dta (p : preDTA) (a : ad)
(t : term) (pr : reconnaissance p a t) :=
forall p0 p1 : preDTA,
p = u_merge p0 p1 ->
forall a0 : ad, a = uad_conv_0 a0 -> reconnaissance (udta_conv_0 p0) a t.
Definition u_merge_invr_0_st (p : preDTA) (s : state)
(t : term) (pr : state_reconnait p s t) :=
forall p0 p1 : preDTA,
p = u_merge p0 p1 ->
forall s0 : state,
s = umpl_conv_0 s0 -> state_reconnait (udta_conv_0 p0) s t.
Definition u_merge_invr_0_lst (p : preDTA) (pl : prec_list)
(lt : term_list) (pr : liste_reconnait p pl lt) :=
forall p0 p1 : preDTA,
p = u_merge p0 p1 ->
forall pl0 : prec_list,
pl = upl_conv_0 pl0 -> liste_reconnait (udta_conv_0 p0) pl lt.
(* invariant pour u_merge : sens réciproque pour u_conv_1 *)
Definition u_merge_invr_1_dta (p : preDTA) (a : ad)
(t : term) (pr : reconnaissance p a t) :=
forall p0 p1 : preDTA,
p = u_merge p0 p1 ->
forall a0 : ad, a = uad_conv_1 a0 -> reconnaissance (udta_conv_1 p1) a t.
Definition u_merge_invr_1_st (p : preDTA) (s : state)
(t : term) (pr : state_reconnait p s t) :=
forall p0 p1 : preDTA,
p = u_merge p0 p1 ->
forall s0 : state,
s = umpl_conv_1 s0 -> state_reconnait (udta_conv_1 p1) s t.
Definition u_merge_invr_1_lst (p : preDTA) (pl : prec_list)
(lt : term_list) (pr : liste_reconnait p pl lt) :=
forall p0 p1 : preDTA,
p = u_merge p0 p1 ->
forall pl0 : prec_list,
pl = upl_conv_1 pl0 -> liste_reconnait (udta_conv_1 p1) pl lt.

Lemma u_merge_5_3 :
forall (d : preDTA) (a : ad) (la ls : prec_list) (hd : term)
(tl : term_list) (r : reconnaissance d a hd),
u_merge_invr_1_dta d a hd r ->
forall l : liste_reconnait d la tl,
u_merge_invr_1_lst d la tl l ->
u_merge_invr_1_lst d (prec_cons a la ls) (tcons hd tl)
(rec_consi d a la ls hd tl r l).