# Theory Automata

Up to index of Isabelle/HOLCF/IOA

theory Automata
imports Asig
uses [Automata.ML]
begin

    (*  Title:      HOLCF/IOA/meta_theory/Automata.thy
        ID:         $Id: Automata.thy,v 1.13 2005/09/02 15:24:00 wenzelm Exp $
        Author:     Olaf Müller, Konrad Slind, Tobias Nipkow
    *)

    header {* The I/O automata of Lynch and Tuttle in HOLCF *}

    theory Automata
    imports Asig
    begin

    defaultsort type

    types
      ('a, 's) transition = "'s * 'a * 's"
      ('a, 's) ioa = "'a signature * 's set * ('a,'s)transition set * ('a set set) * ('a set set)"

    consts

      (* IO automata *)

      asig_of        ::"('a,'s)ioa => 'a signature"
      starts_of      ::"('a,'s)ioa => 's set"
      trans_of       ::"('a,'s)ioa => ('a,'s)transition set"
      wfair_of       ::"('a,'s)ioa => ('a set) set"
      sfair_of       ::"('a,'s)ioa => ('a set) set"

      is_asig_of     ::"('a,'s)ioa => bool"
      is_starts_of   ::"('a,'s)ioa => bool"
      is_trans_of    ::"('a,'s)ioa => bool"
      input_enabled  ::"('a,'s)ioa => bool"
      IOA            ::"('a,'s)ioa => bool"

      (* constraints for fair IOA *)

      fairIOA        ::"('a,'s)ioa => bool"
      input_resistant::"('a,'s)ioa => bool"

      (* enabledness of actions and action sets *)

      enabled        ::"('a,'s)ioa => 'a => 's => bool"
      Enabled    ::"('a,'s)ioa => 'a set => 's => bool"

      (* action set keeps enabled until probably disabled by itself *)

      en_persistent  :: "('a,'s)ioa => 'a set => bool"

     (* post_conditions for actions and action sets *)

      was_enabled        ::"('a,'s)ioa => 'a => 's => bool"
      set_was_enabled    ::"('a,'s)ioa => 'a set => 's => bool"

      (* reachability and invariants *)
      reachable     :: "('a,'s)ioa => 's set"
      invariant     :: "[('a,'s)ioa, 's=>bool] => bool"

      (* binary composition of action signatures and automata *)
      asig_comp    ::"['a signature, 'a signature] => 'a signature"
      compatible   ::"[('a,'s)ioa, ('a,'t)ioa] => bool"
      "||"         ::"[('a,'s)ioa, ('a,'t)ioa] => ('a,'s*'t)ioa"  (infixr 10)

      (* hiding and restricting *)
      hide_asig     :: "['a signature, 'a set] => 'a signature"
      "hide"        :: "[('a,'s)ioa, 'a set] => ('a,'s)ioa"
      restrict_asig :: "['a signature, 'a set] => 'a signature"
      restrict      :: "[('a,'s)ioa, 'a set] => ('a,'s)ioa"

      (* renaming *)
      rename_set    :: "'a set => ('c => 'a option) => 'c set"
      rename        :: "('a, 'b)ioa => ('c => 'a option) => ('c,'b)ioa"

    syntax

      "_trans_of"  :: "'s => 'a => ('a,'s)ioa => 's => bool"  ("_ -_--_-> _" [81,81,81,81] 100)
      "reachable"  :: "[('a,'s)ioa, 's] => bool"
      "act"        :: "('a,'s)ioa => 'a set"
      "ext"        :: "('a,'s)ioa => 'a set"
      "int"        :: "('a,'s)ioa => 'a set"
      "inp"        :: "('a,'s)ioa => 'a set"
      "out"        :: "('a,'s)ioa => 'a set"
      "local"      :: "('a,'s)ioa => 'a set"

    syntax (xsymbols)

      "_trans_of"  :: "'s => 'a => ('a,'s)ioa => 's => bool"
                      ("_ \<midarrow>_\<midarrow>_--> _" [81,81,81,81] 100)
      "op ||"         ::"[('a,'s)ioa, ('a,'t)ioa] => ('a,'s*'t)ioa"  (infixr "\<parallel>" 10)

    inductive "reachable C"
       intros
        reachable_0:  "s:(starts_of C) ==> s : reachable C"
        reachable_n:  "[|s:reachable C; (s,a,t):trans_of C|] ==> t:reachable C"

    translations
      "s -a--A-> t"   == "(s,a,t):trans_of A"
      "reachable A s" == "s:reachable A"
      "act A"         == "actions (asig_of A)"
      "ext A"         == "externals (asig_of A)"
      "int A"         == "internals (asig_of A)"
      "inp A"         == "inputs (asig_of A)"
      "out A"         == "outputs (asig_of A)"
      "local A"       == "locals (asig_of A)"

    defs

    (* --------------------------------- IOA ---------------------------------*)

    asig_of_def:   "asig_of == fst"
    starts_of_def: "starts_of == (fst o snd)"
    trans_of_def:  "trans_of == (fst o snd o snd)"
    wfair_of_def:  "wfair_of == (fst o snd o snd o snd)"
    sfair_of_def:  "sfair_of == (snd o snd o snd o snd)"

    is_asig_of_def:
      "is_asig_of A == is_asig (asig_of A)"

    is_starts_of_def:
      "is_starts_of A ==  (~ starts_of A = {})"

    is_trans_of_def:
      "is_trans_of A ==
        (!triple. triple:(trans_of A) --> fst(snd(triple)):actions(asig_of A))"

    input_enabled_def:
      "input_enabled A ==
        (!a. (a:inputs(asig_of A)) --> (!s1. ? s2. (s1,a,s2):(trans_of A)))"

    ioa_def:
      "IOA A == (is_asig_of A    &
                 is_starts_of A  &
                 is_trans_of A   &
                 input_enabled A)"

    invariant_def: "invariant A P == (!s. reachable A s --> P(s))"

    (* ------------------------- parallel composition --------------------------*)

    compatible_def:
      "compatible A B ==
      (((out A Int out B) = {}) &
       ((int A Int act B) = {}) &
       ((int B Int act A) = {}))"

    asig_comp_def:
      "asig_comp a1 a2 ==
         (((inputs(a1) Un inputs(a2)) - (outputs(a1) Un outputs(a2)),
           (outputs(a1) Un outputs(a2)),
           (internals(a1) Un internals(a2))))"

    par_def:
      "(A || B) ==
          (asig_comp (asig_of A) (asig_of B),
           {pr. fst(pr):starts_of(A) & snd(pr):starts_of(B)},
           {tr. let s = fst(tr); a = fst(snd(tr)); t = snd(snd(tr))
                in (a:act A | a:act B) &
                   (if a:act A then
                      (fst(s),a,fst(t)):trans_of(A)
                    else fst(t) = fst(s))
                   &
                   (if a:act B then
                      (snd(s),a,snd(t)):trans_of(B)
                    else snd(t) = snd(s))},
            wfair_of A Un wfair_of B,
            sfair_of A Un sfair_of B)"

    (* ------------------------ hiding -------------------------------------------- *)

    restrict_asig_def:
      "restrict_asig asig actns ==
        (inputs(asig) Int actns,
         outputs(asig) Int actns,
         internals(asig) Un (externals(asig) - actns))"

    (* Notice that for wfair_of and sfair_of nothing has to be changed, as
       changes from the outputs to the internals does not touch the locals as
       a whole, which is of importance for fairness only *)

    restrict_def:
      "restrict A actns ==
        (restrict_asig (asig_of A) actns,
         starts_of A,
         trans_of A,
         wfair_of A,
         sfair_of A)"

    hide_asig_def:
      "hide_asig asig actns ==
        (inputs(asig) - actns,
         outputs(asig) - actns,
         internals(asig) Un actns)"

    hide_def:
      "hide A actns ==
        (hide_asig (asig_of A) actns,
         starts_of A,
         trans_of A,
         wfair_of A,
         sfair_of A)"

    (* ------------------------- renaming ------------------------------------------- *)

    rename_set_def:
      "rename_set A ren == {b. ? x. Some x = ren b & x : A}"

    rename_def:
    "rename ioa ren ==
      ((rename_set (inp ioa) ren,
        rename_set (out ioa) ren,
        rename_set (int ioa) ren),
       starts_of ioa,
       {tr. let s = fst(tr); a = fst(snd(tr));  t = snd(snd(tr))
            in
            ? x. Some(x) = ren(a) & (s,x,t):trans_of ioa},
       {rename_set s ren | s. s: wfair_of ioa},
       {rename_set s ren | s. s: sfair_of ioa})"

    (* ------------------------- fairness ----------------------------- *)

    fairIOA_def:
      "fairIOA A == (! S : wfair_of A. S<= local A) &
                    (! S : sfair_of A. S<= local A)"

    input_resistant_def:
      "input_resistant A == ! W : sfair_of A. ! s a t.
                            reachable A s & reachable A t & a:inp A &
                            Enabled A W s & s -a--A-> t
                            --> Enabled A W t"

    enabled_def:
      "enabled A a s == ? t. s-a--A-> t"

    Enabled_def:
      "Enabled A W s == ? w:W. enabled A w s"

    en_persistent_def:
      "en_persistent A W == ! s a t. Enabled A W s &
                                     a ~:W &
                                     s -a--A-> t
                                     --> Enabled A W t"
    was_enabled_def:
      "was_enabled A a t == ? s. s-a--A-> t"

    set_was_enabled_def:
      "set_was_enabled A W t == ? w:W. was_enabled A w t"

    ML {* use_legacy_bindings (the_context ()) *}

    end

## asig_of, starts_of, trans_of

theorem ioa_triple_proj:

      asig_of (x, y, z, w, s) = x ∧
      starts_of (x, y, z, w, s) = y ∧
      trans_of (x, y, z, w, s) = z ∧
      wfair_of (x, y, z, w, s) = w ∧ sfair_of (x, y, z, w, s) = s

theorem trans_in_actions:

      [| is_trans_of A; s1.0 -a--A-> s2.0 |] ==> a ∈ act A

theorem starts_of_par:

      starts_of (A || B) = {p. fst p ∈ starts_of A ∧ snd p ∈ starts_of B}

theorem trans_of_par:

      trans_of (A || B) =
      {tr. let s = fst tr; a = fst (snd tr); t = snd (snd tr)
           in (a ∈ act A ∨ a ∈ act B) ∧
              (if a ∈ act A then fst s -a--A-> fst t else fst t = fst s) ∧
              (if a ∈ act B then snd s -a--B-> snd t else snd t = snd s)}

## actions and par

theorem actions_asig_comp:

      actions (asig_comp a b) = actions a ∪ actions b

theorem asig_of_par:

      asig_of (A || B) = asig_comp (asig_of A) (asig_of B)

theorem externals_of_par:

      ext (A1.0 || A2.0) = ext A1.0 ∪ ext A2.0

theorem actions_of_par:

      act (A1.0 || A2.0) = act A1.0 ∪ act A2.0

theorem inputs_of_par:

      inp (A1.0 || A2.0) = inp A1.0 ∪ inp A2.0 - (out A1.0 ∪ out A2.0)

theorem outputs_of_par:

      out (A1.0 || A2.0) = out A1.0 ∪ out A2.0

theorem internals_of_par:

      int (A1.0 || A2.0) = int A1.0 ∪ int A2.0

## actions and compatibility

theorem compat_commute:

      compatible A B = compatible B A

theorem ext1_is_not_int2:

      [| compatible A1.0 A2.0; a ∈ ext A1.0 |] ==> a ∉ int A2.0

theorem ext2_is_not_int1:

      [| compatible A2.0 A1.0; a ∈ ext A1.0 |] ==> a ∉ int A2.0

theorem ext1_ext2_is_not_act2:

      [| compatible A1.0 A2.0; a ∈ ext A1.0; a ∉ ext A2.0 |] ==> a ∉ act A2.0

theorem ext1_ext2_is_not_act1:

      [| compatible A2.0 A1.0; a ∈ ext A1.0; a ∉ ext A2.0 |] ==> a ∉ act A2.0

theorem intA_is_not_extB:

      [| compatible A B; x ∈ int A |] ==> x ∉ ext B

theorem intA_is_not_actB:

      [| compatible A B; a ∈ int A |] ==> a ∉ act B

theorem outAactB_is_inpB:

      [| compatible A B; a ∈ out A; a ∈ act B |] ==> a ∈ inp B

theorem inpAAactB_is_inpBoroutB:

      [| compatible A B; a ∈ inp A; a ∈ act B |] ==> a ∈ inp B ∨ a ∈ out B

## input_enabledness and par

theorem input_enabled_par:

      [| compatible A B; input_enabled A; input_enabled B |]
      ==> input_enabled (A || B)

## invariants

theorem invariantI:

      [| !!s. s ∈ starts_of A ==> P s;
         !!s t a. [| reachable A s; P s |] ==> s -a--A-> t --> P t |]
      ==> invariant A P

theorem invariantI1:

      [| !!s. s ∈ starts_of A ==> P s;
         !!s t a. reachable A s ==> P s --> s -a--A-> t --> P t |]
      ==> invariant A P

theorem invariantE:

      [| invariant A P; reachable A s |] ==> P s

## restrict

theorem cancel_restrict_a:

      starts_of (restrict ioa acts) = starts_of ioa ∧
      trans_of (restrict ioa acts) = trans_of ioa

theorem cancel_restrict_b:

      reachable (restrict ioa acts) s = reachable ioa s

theorem acts_restrict:

      act (restrict A acts) = act A

theorem cancel_restrict:

      starts_of (restrict ioa acts) = starts_of ioa ∧
      trans_of (restrict ioa acts) = trans_of ioa ∧
      reachable (restrict ioa acts) s = reachable ioa s ∧
      act (restrict A acts) = act A

## rename

theorem trans_rename:

      s -a--rename C f-> t ==> ∃x. Some x = f a ∧ s -x--C-> t

theorem reachable_rename:

      reachable (rename C g) s ==> reachable C s

## trans_of(A||B)

theorem trans_A_proj:

      [| s -a--(A || B)-> t; a ∈ act A |] ==> fst s -a--A-> fst t

theorem trans_B_proj:

      [| s -a--(A || B)-> t; a ∈ act B |] ==> snd s -a--B-> snd t

theorem trans_A_proj2:

      [| s -a--(A || B)-> t; a ∉ act A |] ==> fst s = fst t

theorem trans_B_proj2:

      [| s -a--(A || B)-> t; a ∉ act B |] ==> snd s = snd t

theorem trans_AB_proj:

      s -a--(A || B)-> t ==> a ∈ act A ∨ a ∈ act B

theorem trans_AB:

      [| a ∈ act A; a ∈ act B; fst s -a--A-> fst t; snd s -a--B-> snd t |]
      ==> s -a--(A || B)-> t

theorem trans_A_notB:

      [| a ∈ act A; a ∉ act B; fst s -a--A-> fst t; snd s = snd t |]
      ==> s -a--(A || B)-> t

theorem trans_notA_B:

      [| a ∉ act A; a ∈ act B; snd s -a--B-> snd t; fst s = fst t |]
      ==> s -a--(A || B)-> t

theorem trans_of_par4:

      s -a--(A || B || C || D)-> t =
      ((a ∈ act A ∨ a ∈ act B ∨ a ∈ act C ∨ a ∈ act D) ∧
       (if a ∈ act A then fst s -a--A-> fst t else fst t = fst s) ∧
       (if a ∈ act B then fst (snd s) -a--B-> fst (snd t)
        else fst (snd t) = fst (snd s)) ∧
       (if a ∈ act C then fst (snd (snd s)) -a--C-> fst (snd (snd t))
        else fst (snd (snd t)) = fst (snd (snd s))) ∧
       (if a ∈ act D then snd (snd (snd s)) -a--D-> snd (snd (snd t))
        else snd (snd (snd t)) = snd (snd (snd s))))

## proof obligation generator for IOA requirements

theorem is_trans_of_par:

      is_trans_of (A || B)

theorem is_trans_of_restrict:

      is_trans_of A ==> is_trans_of (restrict A acts)

theorem is_trans_of_rename:

      is_trans_of A ==> is_trans_of (rename A f)

theorem is_asig_of_par:

      [| is_asig_of A; is_asig_of B; compatible A B |] ==> is_asig_of (A || B)

theorem is_asig_of_restrict:

      is_asig_of A ==> is_asig_of (restrict A f)

theorem is_asig_of_rename:

      is_asig_of A ==> is_asig_of (rename A f)

theorem compatible_par:

      [| compatible A B; compatible A C |] ==> compatible A (B || C)

theorem compatible_par2:

      [| compatible A C; compatible B C |] ==> compatible (A || B) C

theorem compatible_restrict:

      [| compatible A B; (ext B - S) ∩ ext A = {} |] ==> compatible A (restrict B S)