Theory ParallelComposition

(*****************************************************************************
 * HOL-TestGen --- theorem-prover based test case generation
 *                 http://www.brucker.ch/projects/hol-testgen/
 *                                                                            
 * This file is part of HOL-TestGen.
 *
 * Copyright (c) 2005-2012 ETH Zurich, Switzerland
 *               2008-2015 Achim D. Brucker, Germany
 *               2009-2017 Université Paris-Sud, France
 *               2015-2017 The University of Sheffield, UK
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *
 *     * Redistributions in binary form must reproduce the above
 *       copyright notice, this list of conditions and the following
 *       disclaimer in the documentation and/or other materials provided
 *       with the distribution.
 *
 *     * Neither the name of the copyright holders nor the names of its
 *       contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ******************************************************************************)

section‹Parallel Composition›
theory  
  ParallelComposition
  imports 
    ElementaryPolicies
begin

text‹
  The following combinators are based on the idea that two policies are executed in parallel. 
  Since both input and the output can differ, we chose to pair them. 
  
  The new input pair will often contain repetitions, which can be reduced using the 
  domain-restriction and domain-reduction operators. Using additional range-modifying operators 
  such as $\nabla$, decide which result argument is chosen; this might be the first or the latter 
  or, in case that $\beta = \gamma$, and $\beta$ underlies a lattice structure, the supremum or 
  infimum of both, or, an arbitrary combination of them.
  
  In any case, although we have strictly speaking a pairing of decisions and not a nesting of 
  them, we will apply the same notational conventions as for the latter, i.e. as for 
  flattening.
›

subsection‹Parallel Combinators: Foundations›
text ‹
  There are four possible semantics how the decision can be combined, thus there are four 
  parallel composition operators. For each of them, we prove several properties. 
›

definition prod_orA ::"[,  ]  (×  ×)"  (infixr A 55)
  where "p1 A p2 =
       (λ(x,y). (case p1 x of
             allow d1 (case p2 y of 
                                   allow d2  allow(d1,d2)
                                 | deny d2   allow(d1,d2)
                                 |   ) 
           | deny d1(case p2 y of
                                   allow d2  allow(d1,d2) 
                                 | deny d2   deny (d1,d2) 
                                 |   )
           |   ))"
    
lemma prod_orA_mt[simp]:"p A  = "
  apply (rule ext)
  apply (simp add: prod_orA_def)
  apply (auto)
  apply (simp split: option.splits decision.splits)
  done

lemma mt_prod_orA[simp]:" A p = "
  apply (rule ext)
  apply (simp add: prod_orA_def)
  done
    
lemma prod_orA_quasi_commute: "p2 A p1 = (((λ(x,y). (y,x)) o_f (p1 A p2))) o (λ(a,b).(b,a))"
  apply (rule ext)
  apply (simp add: prod_orA_def policy_range_comp_def o_def)
  apply (auto)[1]
  apply (simp split: option.splits decision.splits)
  done

definition prod_orD ::"[  ,     ]   ( ×     ×  )" (infixr D 55)
where "p1 D p2 =
       (λ(x,y). (case p1 x of
             allow d1 (case p2 y of 
                                   allow d2  allow(d1,d2)
                                 | deny d2   deny(d1,d2)
                                 |   ) 
           | deny d1(case p2 y of
                                   allow d2  deny(d1,d2) 
                                 | deny d2   deny (d1,d2) 
                                 |   )
           |   ))"

lemma prod_orD_mt[simp]:"p D  = "
  apply (rule ext)
  apply (simp add: prod_orD_def)
  apply (auto)[1]
  apply (simp split: option.splits decision.splits)
  done
    
lemma mt_prod_orD[simp]:" D p = "
  apply (rule ext)
  apply (simp add: prod_orD_def)
  done
    
lemma prod_orD_quasi_commute: "p2 D p1 = (((λ(x,y). (y,x)) o_f (p1 D p2))) o (λ(a,b).(b,a))"
  apply (rule ext)
  apply (simp add: prod_orD_def policy_range_comp_def o_def)
  apply (auto)[1]
  apply (simp split: option.splits decision.splits)
  done

text‹
  The following two combinators are by definition non-commutative, but still strict. 
›
  
definition prod_1 :: "[,  ]  (×  ×)" (infixr 1 55)
  where "p1 1 p2 
       (λ(x,y). (case p1 x of
             allow d1(case p2 y of 
                                   allow d2  allow(d1,d2)
                                 | deny d2   allow(d1,d2)
                                 |   ) 
           | deny d1 (case p2 y of
                                   allow d2  deny(d1,d2)
                                 | deny d2   deny(d1,d2)
                                 |   )
           |  ))"
    
lemma prod_1_mt[simp]:"p 1  = "
  apply (rule ext) 
  apply (simp add: prod_1_def)
  apply (auto)[1]
  apply (simp split: option.splits decision.splits)
  done
    
lemma mt_prod_1[simp]:" 1 p = "
  apply (rule ext)
  apply (simp add: prod_1_def)
  done
    
definition prod_2 :: "[,  ]  (×  ×)" (infixr 2 55)
  where "p1 2 p2 
       (λ(x,y). (case p1 x of
             allow d1 (case p2 y of 
                                   allow d2  allow(d1,d2)
                                 | deny d2   deny (d1,d2)
                                 |   ) 
           | deny d1(case p2 y of
                                   allow d2  allow(d1,d2)
                                 | deny d2   deny (d1,d2) 
                                 |   )
           | ))"
    
lemma prod_2_mt[simp]:"p 2  = "
  apply (rule ext)
  apply (simp add: prod_2_def)
  apply (auto)[1]
  apply (simp split: option.splits decision.splits)
  done
    
lemma mt_prod_2[simp]:" 2 p = "
  apply (rule ext) 
  apply (simp add: prod_2_def)
  done
    
definition prod_1_id ::"[, ]  (  ×)" (infixr 1I 55)
  where "p 1I q = (p 1 q) o (λx. (x,x))"
    
lemma prod_1_id_mt[simp]:"p 1I  = "
  apply (rule ext)
  apply (simp add: prod_1_id_def)
  done
    
lemma mt_prod_1_id[simp]:" 1I p = "
  apply (rule ext) 
  apply (simp add: prod_1_id_def prod_1_def)
  done
    
definition prod_2_id ::"[, ]  (  ×)" (infixr 2I 55)
  where"p 2I q = (p 2 q) o (λx. (x,x))"
    
lemma prod_2_id_mt[simp]:"p 2I  = "
  apply (rule ext)
  apply (simp add: prod_2_id_def)
  done
    
lemma mt_prod_2_id[simp]:" 2I p = "
  apply (rule ext)
  apply (simp add: prod_2_id_def prod_2_def)
  done
    
subsection‹Combinators for Transition Policies›
text ‹
  For constructing transition policies, two additional combinators are required: one combines 
  state transitions by pairing the states, the other works equivalently on general maps. 
›
  
definition parallel_map :: "(  )  (  )  
                            ( ×     × )" (infixr M 60) 
  where  "p1 M p2 = (λ (x,y). case p1 x of d1 
                              (case p2 y of d2  (d1,d2)
                                                |   )
                                      |   )"

definition parallel_st :: "('i ×   )  ('i × 'σ'  'σ')  
                            ('i ×  × 'σ'   × 'σ')" (infixr S 60) 
where
   "p1 S p2 = (p1 M p2) o (λ (a,b,c). ((a,b),a,c))"


subsection‹Range Splitting›
text‹
  The following combinator is a special case of both a parallel composition operator and a 
  range splitting operator. Its primary use case is when combining a policy with state transitions. 
›

definition comp_ran_split :: "[(  ) × ( ), 'd  ]  ('d × )  ( × )"
                          (infixr  100)
where "P  p  λx. case p (fst x) of 
                          allow y  (case ((fst P) (snd x)) of    | z  allow (y,z))
                        | deny y   (case ((snd P) (snd x)) of    | z  deny (y,z))
                        |   "

text‹An alternative characterisation of the operator is as follows:›
lemma comp_ran_split_charn:
  "(f, g)   p = (
(((p   Allow)A (Ap f))  
 ((p   Deny) A (Dp g))))"
  apply (rule ext)
  apply (simp add: comp_ran_split_def map_add_def o_def ran_restrict_def image_def
      Allow_def Deny_def dom_restrict_def prod_orA_def 
      allow_pfun_def deny_pfun_def 
      split:option.splits decision.splits) 
  apply (auto)
  done

subsection ‹Distributivity of the parallel combinators›
  
lemma distr_or1_a: "(F = F1  F2)   (((N  1 F) o f) = 
               (((N 1 F1) o f)   ((N   1 F2)  o f))) "
  apply (rule ext)
  apply (simp add:  prod_1_def map_add_def 
      split: decision.splits option.splits)
  subgoal for x 
    apply (case_tac "f x")
    apply (simp_all add: prod_1_def map_add_def 
        split: decision.splits option.splits)
    done
  done

lemma distr_or1: "(F = F1  F2)   ((g o_f ((N  1 F) o f)) = 
               ((g o_f ((N 1 F1) o f))   (g o_f ((N 1 F2)  o f)))) "
  apply (rule ext)+
  apply (simp add: prod_1_def map_add_def policy_range_comp_def 
      split: decision.splits option.splits)
  subgoal for x
    apply (case_tac "f x")
    apply (simp_all add: prod_1_def map_add_def 
        split: decision.splits option.splits)
    done
  done 
    
lemma distr_or2_a: "(F = F1  F2)   (((N  2 F) o f) = 
               (((N 2 F1) o f)   ((N  2 F2)  o f))) "
  apply (rule ext)
  apply (simp add: prod_2_id_def prod_2_def map_add_def 
      split: decision.splits option.splits)
  subgoal for x
    apply (case_tac "f x")
    apply (simp_all add: prod_2_def map_add_def 
        split: decision.splits option.splits)
    done
  done
    
lemma distr_or2: "(F = F1  F2)   ((r o_f ((N  2 F) o f)) = 
               ((r o_f ((N 2 F1) o f))   (r o_f ((N  2 F2)  o f)))) "
  apply (rule ext)
  apply (simp add: prod_2_id_def prod_2_def map_add_def policy_range_comp_def
      split: decision.splits option.splits)
  subgoal for x 
    apply (case_tac "f x")
    apply (simp_all add: prod_2_def map_add_def 
        split: decision.splits option.splits)
    done
  done 
    
lemma distr_orA: "(F = F1  F2)   ((g o_f ((N  A F) o f)) = 
               ((g o_f ((N  A F1) o f))    (g o_f ((N  A F2)  o f)))) "
  apply (rule ext)+
  apply (simp add: prod_orA_def map_add_def policy_range_comp_def 
      split: decision.splits option.splits)
  subgoal for x 
    apply (case_tac "f x")
    apply (simp_all add:  map_add_def 
        split: decision.splits option.splits)
    done
  done 
    
lemma distr_orD: "(F = F1  F2)   ((g o_f ((N  D F) o f)) = 
               ((g o_f ((N D F1) o f))    (g o_f ((N D F2)  o f)))) "
  apply (rule ext)+
  apply (simp add: prod_orD_def map_add_def policy_range_comp_def 
      split: decision.splits option.splits)
  subgoal for x 
    apply (case_tac "f x")
    apply (simp_all add:  map_add_def 
        split: decision.splits option.splits)
    done
  done 
    
lemma coerc_assoc: "(r o_f P) o d = r o_f (P o d)"
  apply (simp add: policy_range_comp_def)
  apply (rule ext)
  apply (simp split: option.splits decision.splits)
  done

lemmas ParallelDefs = prod_orA_def prod_orD_def prod_1_def prod_2_def parallel_map_def 
                      parallel_st_def comp_ran_split_def
end