src/HOL/IOA/IOA.thy
author hoelzl
Thu Sep 02 10:14:32 2010 +0200 (2010-09-02)
changeset 39072 1030b1a166ef
parent 36862 952b2b102a0a
child 42174 d0be2722ce9f
permissions -rw-r--r--
Add lessThan_Suc_eq_insert_0
     1 (*  Title:      HOL/IOA/IOA.thy
     2     Author:     Tobias Nipkow & Konrad Slind
     3     Copyright   1994  TU Muenchen
     4 *)
     5 
     6 header {* The I/O automata of Lynch and Tuttle *}
     7 
     8 theory IOA
     9 imports Asig
    10 begin
    11 
    12 types
    13    'a seq            =   "nat => 'a"
    14    'a oseq           =   "nat => 'a option"
    15    ('a,'b)execution  =   "'a oseq * 'b seq"
    16    ('a,'s)transition =   "('s * 'a * 's)"
    17    ('a,'s)ioa        =   "'a signature * 's set * ('a,'s)transition set"
    18 
    19 consts
    20 
    21   (* IO automata *)
    22   state_trans::"['action signature, ('action,'state)transition set] => bool"
    23   asig_of    ::"('action,'state)ioa => 'action signature"
    24   starts_of  ::"('action,'state)ioa => 'state set"
    25   trans_of   ::"('action,'state)ioa => ('action,'state)transition set"
    26   IOA        ::"('action,'state)ioa => bool"
    27 
    28   (* Executions, schedules, and traces *)
    29 
    30   is_execution_fragment ::"[('action,'state)ioa, ('action,'state)execution] => bool"
    31   has_execution ::"[('action,'state)ioa, ('action,'state)execution] => bool"
    32   executions    :: "('action,'state)ioa => ('action,'state)execution set"
    33   mk_trace  :: "[('action,'state)ioa, 'action oseq] => 'action oseq"
    34   reachable     :: "[('action,'state)ioa, 'state] => bool"
    35   invariant     :: "[('action,'state)ioa, 'state=>bool] => bool"
    36   has_trace :: "[('action,'state)ioa, 'action oseq] => bool"
    37   traces    :: "('action,'state)ioa => 'action oseq set"
    38   NF            :: "'a oseq => 'a oseq"
    39 
    40   (* Composition of action signatures and automata *)
    41   compatible_asigs ::"('a => 'action signature) => bool"
    42   asig_composition ::"('a => 'action signature) => 'action signature"
    43   compatible_ioas  ::"('a => ('action,'state)ioa) => bool"
    44   ioa_composition  ::"('a => ('action, 'state)ioa) =>('action,'a => 'state)ioa"
    45 
    46   (* binary composition of action signatures and automata *)
    47   compat_asigs ::"['action signature, 'action signature] => bool"
    48   asig_comp    ::"['action signature, 'action signature] => 'action signature"
    49   compat_ioas  ::"[('action,'s)ioa, ('action,'t)ioa] => bool"
    50   par         ::"[('a,'s)ioa, ('a,'t)ioa] => ('a,'s*'t)ioa"  (infixr "||" 10)
    51 
    52   (* Filtering and hiding *)
    53   filter_oseq  :: "('a => bool) => 'a oseq => 'a oseq"
    54 
    55   restrict_asig :: "['a signature, 'a set] => 'a signature"
    56   restrict      :: "[('a,'s)ioa, 'a set] => ('a,'s)ioa"
    57 
    58   (* Notions of correctness *)
    59   ioa_implements :: "[('action,'state1)ioa, ('action,'state2)ioa] => bool"
    60 
    61   (* Instantiation of abstract IOA by concrete actions *)
    62   rename:: "('a, 'b)ioa => ('c => 'a option) => ('c,'b)ioa"
    63 
    64 defs
    65 
    66 state_trans_def:
    67   "state_trans asig R ==
    68      (!triple. triple:R --> fst(snd(triple)):actions(asig)) &
    69      (!a. (a:inputs(asig)) --> (!s1. ? s2. (s1,a,s2):R))"
    70 
    71 
    72 asig_of_def:   "asig_of == fst"
    73 starts_of_def: "starts_of == (fst o snd)"
    74 trans_of_def:  "trans_of == (snd o snd)"
    75 
    76 ioa_def:
    77   "IOA(ioa) == (is_asig(asig_of(ioa))      &
    78                 (~ starts_of(ioa) = {})    &
    79                 state_trans (asig_of ioa) (trans_of ioa))"
    80 
    81 
    82 (* An execution fragment is modelled with a pair of sequences:
    83  * the first is the action options, the second the state sequence.
    84  * Finite executions have None actions from some point on.
    85  *******)
    86 is_execution_fragment_def:
    87   "is_execution_fragment A ex ==
    88      let act = fst(ex); state = snd(ex)
    89      in !n a. (act(n)=None --> state(Suc(n)) = state(n)) &
    90               (act(n)=Some(a) --> (state(n),a,state(Suc(n))):trans_of(A))"
    91 
    92 
    93 executions_def:
    94   "executions(ioa) == {e. snd e 0:starts_of(ioa) &
    95                         is_execution_fragment ioa e}"
    96 
    97 
    98 reachable_def:
    99   "reachable ioa s == (? ex:executions(ioa). ? n. (snd ex n) = s)"
   100 
   101 
   102 invariant_def: "invariant A P == (!s. reachable A s --> P(s))"
   103 
   104 
   105 (* Restrict the trace to those members of the set s *)
   106 filter_oseq_def:
   107   "filter_oseq p s ==
   108    (%i. case s(i)
   109          of None => None
   110           | Some(x) => if p x then Some x else None)"
   111 
   112 
   113 mk_trace_def:
   114   "mk_trace(ioa) == filter_oseq(%a. a:externals(asig_of(ioa)))"
   115 
   116 
   117 (* Does an ioa have an execution with the given trace *)
   118 has_trace_def:
   119   "has_trace ioa b ==
   120      (? ex:executions(ioa). b = mk_trace ioa (fst ex))"
   121 
   122 normal_form_def:
   123   "NF(tr) == @nf. ? f. mono(f) & (!i. nf(i)=tr(f(i))) &
   124                     (!j. j ~: range(f) --> nf(j)= None) &
   125                     (!i. nf(i)=None --> (nf (Suc i)) = None)   "
   126 
   127 (* All the traces of an ioa *)
   128 
   129   traces_def:
   130   "traces(ioa) == {trace. ? tr. trace=NF(tr) & has_trace ioa tr}"
   131 
   132 (*
   133   traces_def:
   134   "traces(ioa) == {tr. has_trace ioa tr}"
   135 *)
   136 
   137 compat_asigs_def:
   138   "compat_asigs a1 a2 ==
   139    (((outputs(a1) Int outputs(a2)) = {}) &
   140     ((internals(a1) Int actions(a2)) = {}) &
   141     ((internals(a2) Int actions(a1)) = {}))"
   142 
   143 
   144 compat_ioas_def:
   145   "compat_ioas ioa1 ioa2 == compat_asigs (asig_of(ioa1)) (asig_of(ioa2))"
   146 
   147 
   148 asig_comp_def:
   149   "asig_comp a1 a2 ==
   150       (((inputs(a1) Un inputs(a2)) - (outputs(a1) Un outputs(a2)),
   151         (outputs(a1) Un outputs(a2)),
   152         (internals(a1) Un internals(a2))))"
   153 
   154 
   155 par_def:
   156   "(ioa1 || ioa2) ==
   157        (asig_comp (asig_of ioa1) (asig_of ioa2),
   158         {pr. fst(pr):starts_of(ioa1) & snd(pr):starts_of(ioa2)},
   159         {tr. let s = fst(tr); a = fst(snd(tr)); t = snd(snd(tr))
   160              in (a:actions(asig_of(ioa1)) | a:actions(asig_of(ioa2))) &
   161                 (if a:actions(asig_of(ioa1)) then
   162                    (fst(s),a,fst(t)):trans_of(ioa1)
   163                  else fst(t) = fst(s))
   164                 &
   165                 (if a:actions(asig_of(ioa2)) then
   166                    (snd(s),a,snd(t)):trans_of(ioa2)
   167                  else snd(t) = snd(s))})"
   168 
   169 
   170 restrict_asig_def:
   171   "restrict_asig asig actns ==
   172     (inputs(asig) Int actns, outputs(asig) Int actns,
   173      internals(asig) Un (externals(asig) - actns))"
   174 
   175 
   176 restrict_def:
   177   "restrict ioa actns ==
   178     (restrict_asig (asig_of ioa) actns, starts_of(ioa), trans_of(ioa))"
   179 
   180 
   181 ioa_implements_def:
   182   "ioa_implements ioa1 ioa2 ==
   183   ((inputs(asig_of(ioa1)) = inputs(asig_of(ioa2))) &
   184      (outputs(asig_of(ioa1)) = outputs(asig_of(ioa2))) &
   185       traces(ioa1) <= traces(ioa2))"
   186 
   187 rename_def:
   188 "rename ioa ren ==
   189   (({b. ? x. Some(x)= ren(b) & x : inputs(asig_of(ioa))},
   190     {b. ? x. Some(x)= ren(b) & x : outputs(asig_of(ioa))},
   191     {b. ? x. Some(x)= ren(b) & x : internals(asig_of(ioa))}),
   192               starts_of(ioa)   ,
   193    {tr. let s = fst(tr); a = fst(snd(tr));  t = snd(snd(tr))
   194         in
   195         ? x. Some(x) = ren(a) & (s,x,t):trans_of(ioa)})"
   196 
   197 
   198 declare Let_def [simp]
   199 
   200 lemmas ioa_projections = asig_of_def starts_of_def trans_of_def
   201   and exec_rws = executions_def is_execution_fragment_def
   202 
   203 lemma ioa_triple_proj:
   204     "asig_of(x,y,z) = x & starts_of(x,y,z) = y & trans_of(x,y,z) = z"
   205   apply (simp add: ioa_projections)
   206   done
   207 
   208 lemma trans_in_actions:
   209   "[| IOA(A); (s1,a,s2):trans_of(A) |] ==> a:actions(asig_of(A))"
   210   apply (unfold ioa_def state_trans_def actions_def is_asig_def)
   211   apply (erule conjE)+
   212   apply (erule allE, erule impE, assumption)
   213   apply simp
   214   done
   215 
   216 
   217 lemma filter_oseq_idemp: "filter_oseq p (filter_oseq p s) = filter_oseq p s"
   218   apply (simp add: filter_oseq_def)
   219   apply (rule ext)
   220   apply (case_tac "s i")
   221   apply simp_all
   222   done
   223 
   224 lemma mk_trace_thm:
   225 "(mk_trace A s n = None) =
   226    (s(n)=None | (? a. s(n)=Some(a) & a ~: externals(asig_of(A))))
   227    &
   228    (mk_trace A s n = Some(a)) =
   229     (s(n)=Some(a) & a : externals(asig_of(A)))"
   230   apply (unfold mk_trace_def filter_oseq_def)
   231   apply (case_tac "s n")
   232   apply auto
   233   done
   234 
   235 lemma reachable_0: "s:starts_of(A) ==> reachable A s"
   236   apply (unfold reachable_def)
   237   apply (rule_tac x = "(%i. None, %i. s)" in bexI)
   238   apply simp
   239   apply (simp add: exec_rws)
   240   done
   241 
   242 lemma reachable_n:
   243   "!!A. [| reachable A s; (s,a,t) : trans_of(A) |] ==> reachable A t"
   244   apply (unfold reachable_def exec_rws)
   245   apply (simp del: bex_simps)
   246   apply (simp (no_asm_simp) only: split_tupled_all)
   247   apply safe
   248   apply (rename_tac ex1 ex2 n)
   249   apply (rule_tac x = "(%i. if i<n then ex1 i else (if i=n then Some a else None) , %i. if i<Suc n then ex2 i else t)" in bexI)
   250    apply (rule_tac x = "Suc n" in exI)
   251    apply (simp (no_asm))
   252   apply simp
   253   apply (metis ioa_triple_proj less_antisym)
   254   done
   255 
   256 
   257 lemma invariantI:
   258   assumes p1: "!!s. s:starts_of(A) ==> P(s)"
   259     and p2: "!!s t a. [|reachable A s; P(s)|] ==> (s,a,t): trans_of(A) --> P(t)"
   260   shows "invariant A P"
   261   apply (unfold invariant_def reachable_def Let_def exec_rws)
   262   apply safe
   263   apply (rename_tac ex1 ex2 n)
   264   apply (rule_tac Q = "reachable A (ex2 n) " in conjunct1)
   265   apply simp
   266   apply (induct_tac n)
   267    apply (fast intro: p1 reachable_0)
   268   apply (erule_tac x = na in allE)
   269   apply (case_tac "ex1 na", simp_all)
   270   apply safe
   271    apply (erule p2 [THEN mp])
   272     apply (fast dest: reachable_n)+
   273   done
   274 
   275 lemma invariantI1:
   276  "[| !!s. s : starts_of(A) ==> P(s);
   277      !!s t a. reachable A s ==> P(s) --> (s,a,t):trans_of(A) --> P(t)
   278   |] ==> invariant A P"
   279   apply (blast intro!: invariantI)
   280   done
   281 
   282 lemma invariantE:
   283   "[| invariant A P; reachable A s |] ==> P(s)"
   284   apply (unfold invariant_def)
   285   apply blast
   286   done
   287 
   288 lemma actions_asig_comp:
   289   "actions(asig_comp a b) = actions(a) Un actions(b)"
   290   apply (auto simp add: actions_def asig_comp_def asig_projections)
   291   done
   292 
   293 lemma starts_of_par:
   294   "starts_of(A || B) = {p. fst(p):starts_of(A) & snd(p):starts_of(B)}"
   295   apply (simp add: par_def ioa_projections)
   296   done
   297 
   298 (* Every state in an execution is reachable *)
   299 lemma states_of_exec_reachable:
   300   "ex:executions(A) ==> !n. reachable A (snd ex n)"
   301   apply (unfold reachable_def)
   302   apply fast
   303   done
   304 
   305 
   306 lemma trans_of_par4:
   307 "(s,a,t) : trans_of(A || B || C || D) =
   308   ((a:actions(asig_of(A)) | a:actions(asig_of(B)) | a:actions(asig_of(C)) |
   309     a:actions(asig_of(D))) &
   310    (if a:actions(asig_of(A)) then (fst(s),a,fst(t)):trans_of(A)
   311     else fst t=fst s) &
   312    (if a:actions(asig_of(B)) then (fst(snd(s)),a,fst(snd(t))):trans_of(B)
   313     else fst(snd(t))=fst(snd(s))) &
   314    (if a:actions(asig_of(C)) then
   315       (fst(snd(snd(s))),a,fst(snd(snd(t)))):trans_of(C)
   316     else fst(snd(snd(t)))=fst(snd(snd(s)))) &
   317    (if a:actions(asig_of(D)) then
   318       (snd(snd(snd(s))),a,snd(snd(snd(t)))):trans_of(D)
   319     else snd(snd(snd(t)))=snd(snd(snd(s)))))"
   320   (*SLOW*)
   321   apply (simp (no_asm) add: par_def actions_asig_comp Pair_fst_snd_eq ioa_projections)
   322   done
   323 
   324 lemma cancel_restrict: "starts_of(restrict ioa acts) = starts_of(ioa) &
   325               trans_of(restrict ioa acts) = trans_of(ioa) &
   326               reachable (restrict ioa acts) s = reachable ioa s"
   327   apply (simp add: is_execution_fragment_def executions_def
   328     reachable_def restrict_def ioa_projections)
   329   done
   330 
   331 lemma asig_of_par: "asig_of(A || B) = asig_comp (asig_of A) (asig_of B)"
   332   apply (simp add: par_def ioa_projections)
   333   done
   334 
   335 
   336 lemma externals_of_par: "externals(asig_of(A1||A2)) =
   337    (externals(asig_of(A1)) Un externals(asig_of(A2)))"
   338   apply (simp add: externals_def asig_of_par asig_comp_def
   339     asig_inputs_def asig_outputs_def Un_def set_diff_eq)
   340   apply blast
   341   done
   342 
   343 lemma ext1_is_not_int2:
   344   "[| compat_ioas A1 A2; a:externals(asig_of(A1))|] ==> a~:internals(asig_of(A2))"
   345   apply (unfold externals_def actions_def compat_ioas_def compat_asigs_def)
   346   apply auto
   347   done
   348 
   349 lemma ext2_is_not_int1:
   350  "[| compat_ioas A2 A1 ; a:externals(asig_of(A1))|] ==> a~:internals(asig_of(A2))"
   351   apply (unfold externals_def actions_def compat_ioas_def compat_asigs_def)
   352   apply auto
   353   done
   354 
   355 lemmas ext1_ext2_is_not_act2 = ext1_is_not_int2 [THEN int_and_ext_is_act]
   356   and ext1_ext2_is_not_act1 = ext2_is_not_int1 [THEN int_and_ext_is_act]
   357 
   358 end