src/HOL/Datatype_Examples/Process.thy
author wenzelm
Sat Jul 18 22:58:50 2015 +0200 (2015-07-18)
changeset 60758 d8d85a8172b5
parent 58916 229765cc3414
child 63167 0909deb8059b
permissions -rw-r--r--
isabelle update_cartouches;
     1 (*  Title:      HOL/Datatype_Examples/Process.thy
     2     Author:     Andrei Popescu, TU Muenchen
     3     Copyright   2012
     4 
     5 Processes.
     6 *)
     7 
     8 section {* Processes *}
     9 
    10 theory Process
    11 imports "~~/src/HOL/Library/Stream"
    12 begin
    13 
    14 codatatype 'a process =
    15   isAction: Action (prefOf: 'a) (contOf: "'a process") |
    16   isChoice: Choice (ch1Of: "'a process") (ch2Of: "'a process")
    17 
    18 (* Read: prefix of, continuation of, choice 1 of, choice 2 of *)
    19 
    20 subsection {* Basic properties *}
    21 
    22 (* Constructors versus discriminators *)
    23 theorem isAction_isChoice:
    24 "isAction p \<or> isChoice p"
    25 by (rule process.exhaust_disc) auto
    26 
    27 theorem not_isAction_isChoice: "\<not> (isAction p \<and> isChoice p)"
    28 by (cases rule: process.exhaust[of p]) auto
    29 
    30 
    31 subsection{* Coinduction *}
    32 
    33 theorem process_coind[elim, consumes 1, case_names iss Action Choice, induct pred: "HOL.eq"]:
    34   assumes phi: "\<phi> p p'" and
    35   iss: "\<And>p p'. \<phi> p p' \<Longrightarrow> (isAction p \<longleftrightarrow> isAction p') \<and> (isChoice p \<longleftrightarrow> isChoice p')" and
    36   Act: "\<And> a a' p p'. \<phi> (Action a p) (Action a' p') \<Longrightarrow> a = a' \<and> \<phi> p p'" and
    37   Ch: "\<And> p q p' q'. \<phi> (Choice p q) (Choice p' q') \<Longrightarrow> \<phi> p p' \<and> \<phi> q q'"
    38   shows "p = p'"
    39   using assms
    40   by (coinduct rule: process.coinduct) (metis process.collapse(1,2) process.disc(3))
    41 
    42 (* Stronger coinduction, up to equality: *)
    43 theorem process_strong_coind[elim, consumes 1, case_names iss Action Choice]:
    44   assumes phi: "\<phi> p p'" and
    45   iss: "\<And>p p'. \<phi> p p' \<Longrightarrow> (isAction p \<longleftrightarrow> isAction p') \<and> (isChoice p \<longleftrightarrow> isChoice p')" and
    46   Act: "\<And> a a' p p'. \<phi> (Action a p) (Action a' p') \<Longrightarrow> a = a' \<and> (\<phi> p p' \<or> p = p')" and
    47   Ch: "\<And> p q p' q'. \<phi> (Choice p q) (Choice p' q') \<Longrightarrow> (\<phi> p p' \<or> p = p') \<and> (\<phi> q q' \<or> q = q')"
    48   shows "p = p'"
    49   using assms
    50   by (coinduct rule: process.coinduct_strong) (metis process.collapse(1,2) process.disc(3))
    51 
    52 
    53 subsection {* Coiteration (unfold) *}
    54 
    55 
    56 section{* Coinductive definition of the notion of trace *}
    57 coinductive trace where
    58 "trace p as \<Longrightarrow> trace (Action a p) (a ## as)"
    59 |
    60 "trace p as \<or> trace q as \<Longrightarrow> trace (Choice p q) as"
    61 
    62 
    63 section{* Examples of corecursive definitions: *}
    64 
    65 subsection{* Single-guard fixpoint definition *}
    66 
    67 primcorec BX where
    68   "isAction BX"
    69 | "prefOf BX = ''a''"
    70 | "contOf BX = BX"
    71 
    72 
    73 subsection{* Multi-guard fixpoint definitions, simulated with auxiliary arguments *}
    74 
    75 datatype x_y_ax = x | y | ax
    76 
    77 primcorec F :: "x_y_ax \<Rightarrow> char list process" where
    78   "xyax = x \<Longrightarrow> isChoice (F xyax)"
    79 | "ch1Of (F xyax) = F ax"
    80 | "ch2Of (F xyax) = F y"
    81 | "prefOf (F xyax) = (if xyax = y then ''b'' else ''a'')"
    82 | "contOf (F xyax) = F x"
    83 
    84 definition "X = F x"  definition "Y = F y"  definition "AX = F ax"
    85 
    86 lemma X_Y_AX: "X = Choice AX Y"  "Y = Action ''b'' X"  "AX = Action ''a'' X"
    87 unfolding X_def Y_def AX_def by (subst F.code, simp)+
    88 
    89 (* end product: *)
    90 lemma X_AX:
    91 "X = Choice AX (Action ''b'' X)"
    92 "AX = Action ''a'' X"
    93 using X_Y_AX by simp_all
    94 
    95 
    96 
    97 section{* Case study: Multi-guard fixpoint definitions, without auxiliary arguments *}
    98 
    99 hide_const x y ax X Y AX
   100 
   101 (* Process terms *)
   102 datatype ('a,'pvar) process_term =
   103  VAR 'pvar |
   104  PROC "'a process" |
   105  ACT 'a "('a,'pvar) process_term" | CH "('a,'pvar) process_term" "('a,'pvar) process_term"
   106 
   107 (* below, sys represents a system of equations *)
   108 fun isACT where
   109 "isACT sys (VAR X) =
   110  (case sys X of ACT a T \<Rightarrow> True |PROC p \<Rightarrow> isAction p |_ \<Rightarrow> False)"
   111 |
   112 "isACT sys (PROC p) = isAction p"
   113 |
   114 "isACT sys (ACT a T) = True"
   115 |
   116 "isACT sys (CH T1 T2) = False"
   117 
   118 fun PREF where
   119 "PREF sys (VAR X) =
   120  (case sys X of ACT a T \<Rightarrow> a | PROC p \<Rightarrow> prefOf p)"
   121 |
   122 "PREF sys (PROC p) = prefOf p"
   123 |
   124 "PREF sys (ACT a T) = a"
   125 
   126 fun CONT where
   127 "CONT sys (VAR X) =
   128  (case sys X of ACT a T \<Rightarrow> T | PROC p \<Rightarrow> PROC (contOf p))"
   129 |
   130 "CONT sys (PROC p) = PROC (contOf p)"
   131 |
   132 "CONT sys (ACT a T) = T"
   133 
   134 fun CH1 where
   135 "CH1 sys (VAR X) =
   136  (case sys X of CH T1 T2 \<Rightarrow> T1 |PROC p \<Rightarrow> PROC (ch1Of p))"
   137 |
   138 "CH1 sys (PROC p) = PROC (ch1Of p)"
   139 |
   140 "CH1 sys (CH T1 T2) = T1"
   141 
   142 fun CH2 where
   143 "CH2 sys (VAR X) =
   144  (case sys X of CH T1 T2 \<Rightarrow> T2 |PROC p \<Rightarrow> PROC (ch2Of p))"
   145 |
   146 "CH2 sys (PROC p) = PROC (ch2Of p)"
   147 |
   148 "CH2 sys (CH T1 T2) = T2"
   149 
   150 definition "guarded sys \<equiv> \<forall> X Y. sys X \<noteq> VAR Y"
   151 
   152 primcorec solution where
   153   "isACT sys T \<Longrightarrow> solution sys T = Action (PREF sys T) (solution sys (CONT sys T))"
   154 | "_ \<Longrightarrow> solution sys T = Choice (solution sys (CH1 sys T)) (solution sys (CH2 sys T))"
   155 
   156 lemma isACT_VAR:
   157 assumes g: "guarded sys"
   158 shows "isACT sys (VAR X) \<longleftrightarrow> isACT sys (sys X)"
   159 using g unfolding guarded_def by (cases "sys X") auto
   160 
   161 lemma solution_VAR:
   162 assumes g: "guarded sys"
   163 shows "solution sys (VAR X) = solution sys (sys X)"
   164 proof(cases "isACT sys (VAR X)")
   165   case True
   166   hence T: "isACT sys (sys X)" unfolding isACT_VAR[OF g] .
   167   show ?thesis
   168   unfolding solution.ctr(1)[OF T] using solution.ctr(1)[of sys "VAR X"] True g
   169   unfolding guarded_def by (cases "sys X", auto)
   170 next
   171   case False note FFalse = False
   172   hence TT: "\<not> isACT sys (sys X)" unfolding isACT_VAR[OF g] .
   173   show ?thesis
   174   unfolding solution.ctr(2)[OF TT] using solution.ctr(2)[of sys "VAR X"] FFalse g
   175   unfolding guarded_def by (cases "sys X", auto)
   176 qed
   177 
   178 lemma solution_PROC[simp]:
   179 "solution sys (PROC p) = p"
   180 proof-
   181   {fix q assume "q = solution sys (PROC p)"
   182    hence "p = q"
   183    proof (coinduct rule: process_coind)
   184      case (iss p p')
   185      from isAction_isChoice[of p] show ?case
   186      proof
   187        assume p: "isAction p"
   188        hence 0: "isACT sys (PROC p)" by simp
   189        thus ?thesis using iss not_isAction_isChoice by auto
   190      next
   191        assume "isChoice p"
   192        hence 0: "\<not> isACT sys (PROC p)"
   193        using not_isAction_isChoice by auto
   194        thus ?thesis using iss isAction_isChoice by auto
   195      qed
   196    next
   197      case (Action a a' p p')
   198      hence 0: "isACT sys (PROC (Action a p))" by simp
   199      show ?case using Action unfolding solution.ctr(1)[OF 0] by simp
   200    next
   201      case (Choice p q p' q')
   202      hence 0: "\<not> isACT sys (PROC (Choice p q))" using not_isAction_isChoice by auto
   203      show ?case using Choice unfolding solution.ctr(2)[OF 0] by simp
   204    qed
   205   }
   206   thus ?thesis by metis
   207 qed
   208 
   209 lemma solution_ACT[simp]:
   210 "solution sys (ACT a T) = Action a (solution sys T)"
   211 by (metis CONT.simps(3) PREF.simps(3) isACT.simps(3) solution.ctr(1))
   212 
   213 lemma solution_CH[simp]:
   214 "solution sys (CH T1 T2) = Choice (solution sys T1) (solution sys T2)"
   215 by (metis CH1.simps(3) CH2.simps(3) isACT.simps(4) solution.ctr(2))
   216 
   217 
   218 (* Example: *)
   219 
   220 fun sys where
   221 "sys 0 = CH (VAR (Suc 0)) (ACT ''b'' (VAR 0))"
   222 |
   223 "sys (Suc 0) = ACT ''a'' (VAR 0)"
   224 | (* dummy guarded term for variables outside the system: *)
   225 "sys X = ACT ''a'' (VAR 0)"
   226 
   227 lemma guarded_sys:
   228 "guarded sys"
   229 unfolding guarded_def proof (intro allI)
   230   fix X Y show "sys X \<noteq> VAR Y" by (cases X, simp, case_tac nat, auto)
   231 qed
   232 
   233 (* the actual processes: *)
   234 definition "x \<equiv> solution sys (VAR 0)"
   235 definition "ax \<equiv> solution sys (VAR (Suc 0))"
   236 
   237 (* end product: *)
   238 lemma x_ax:
   239 "x = Choice ax (Action ''b'' x)"
   240 "ax = Action ''a'' x"
   241 unfolding x_def ax_def by (subst solution_VAR[OF guarded_sys], simp)+
   242 
   243 
   244 (* Thanks to the inclusion of processes as process terms, one can
   245 also consider parametrized systems of equations---here, x is a (semantic)
   246 process parameter: *)
   247 
   248 fun sys' where
   249 "sys' 0 = CH (PROC x) (ACT ''b'' (VAR 0))"
   250 |
   251 "sys' (Suc 0) = CH (ACT ''a'' (VAR 0)) (PROC x)"
   252 | (* dummy guarded term : *)
   253 "sys' X = ACT ''a'' (VAR 0)"
   254 
   255 lemma guarded_sys':
   256 "guarded sys'"
   257 unfolding guarded_def proof (intro allI)
   258   fix X Y show "sys' X \<noteq> VAR Y" by (cases X, simp, case_tac nat, auto)
   259 qed
   260 
   261 (* the actual processes: *)
   262 definition "y \<equiv> solution sys' (VAR 0)"
   263 definition "ay \<equiv> solution sys' (VAR (Suc 0))"
   264 
   265 (* end product: *)
   266 lemma y_ay:
   267 "y = Choice x (Action ''b'' y)"
   268 "ay = Choice (Action ''a'' y) x"
   269 unfolding y_def ay_def by (subst solution_VAR[OF guarded_sys'], simp)+
   270 
   271 end