Theory UML_Bag

(*****************************************************************************
 * Featherweight-OCL --- A Formal Semantics for UML-OCL Version OCL 2.5
 *                       for the OMG Standard.
 *                       http://www.brucker.ch/projects/hol-testgen/
 *
 * UML_Bag.thy --- Library definitions.
 * This file is part of HOL-TestGen.
 *
 * Copyright (c) 2012-2015 Université Paris-Saclay, Univ. Paris-Sud, France
 *               2013-2015 IRT SystemX, France
 *
 * 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.
 ******************************************************************************)


theory  UML_Bag
imports "../basic_types/UML_Void"
        "../basic_types/UML_Boolean"
        "../basic_types/UML_Integer"
        "../basic_types/UML_String"
        "../basic_types/UML_Real"
begin

no_notation None ("")
section‹Collection Type Bag: Operations›

definition "Rep_Bag_base' x = {(x0, y). y < Rep_Bagbase x x0 }"
definition "Rep_Bag_base x τ = {(x0, y). y < Rep_Bagbase (x τ) x0 }"
definition "Rep_Set_base x τ = fst ` {(x0, y). y < Rep_Bagbase (x τ) x0 }"

definition ApproxEq (infixl "" 30)
where     "X  Y   λ τ. Rep_Set_base X τ = Rep_Set_base Y τ "


subsection‹As a Motivation for the (infinite) Type Construction: Type-Extensions as Bags 
             \label{sec:type-extensions}›

text‹Our notion of typed bag goes beyond the usual notion of a finite executable bag and
is powerful enough to capture \emph{the extension of a type} in UML and OCL. This means
we can have in Featherweight OCL Bags containing all possible elements of a type, not only
those (finite) ones representable in a state. This holds for base types as well as class types,
although the notion for class-types --- involving object id's not occurring in a state ---
requires some care.

In a world with @{term invalid} and @{term null}, there are two notions extensions possible:
\begin{enumerate}
\item the bag of all \emph{defined} values of a type @{term T}
      (for which we will introduce the constant  @{term T})
\item the bag of all \emph{valid} values of a type @{term T}, so including @{term null}
      (for which we will introduce the constant  @{term Tnull}).
\end{enumerate}
›

text‹We define the bag extensions for the base type @{term Integer} as follows:›
definition Integer :: "('𝔄,Integerbase) Bag"
where     "Integer  (λ τ. (Abs_Bagbase o Some o Some)  (λ None  0 | Some None  0 | _  1))"

definition Integernull :: "('𝔄,Integerbase) Bag"
where     "Integernull  (λ τ. (Abs_Bagbase o Some o Some)  (λ None  0 | _  1))"

lemma Integer_defined : "δ Integer = true"
apply(rule ext, auto simp: Integer_def defined_def false_def true_def
                           bot_fun_def null_fun_def null_option_def)
by(simp_all add: Abs_Bagbase_inject bot_option_def bot_Bagbase_def null_Bagbase_def null_option_def)

lemma Integernull_defined : "δ Integernull = true"
apply(rule ext, auto simp: Integernull_def defined_def false_def true_def
                           bot_fun_def null_fun_def null_option_def)
by(simp_all add: Abs_Bagbase_inject bot_option_def bot_Bagbase_def null_Bagbase_def null_option_def)

text‹This allows the theorems:

      τ ⊨ δ x  ⟹ τ ⊨ (Integer->includesBag(x))›
      τ ⊨ δ x  ⟹ τ ⊨ Integer  ≜ (Integer->includingBag(x))›

and

      τ ⊨ υ x  ⟹ τ ⊨ (Integernull->includesBag(x))›
      τ ⊨ υ x  ⟹ τ ⊨ Integernull  ≜ (Integernull->includingBag(x))›

which characterize the infiniteness of these bags by a recursive property on these bags.
›

text‹In the same spirit, we proceed similarly for the remaining base types:›

definition Voidnull :: "('𝔄,Voidbase) Bag"
where     "Voidnull  (λ τ. (Abs_Bagbase o Some o Some) (λ x. if x = Abs_Voidbase (Some None) then 1 else 0))"

definition Voidempty :: "('𝔄,Voidbase) Bag"
where     "Voidempty  (λ τ. (Abs_Bagbase o Some o Some) (λ_. 0))"

lemma Voidnull_defined : "δ Voidnull = true"
apply(rule ext, auto simp: Voidnull_def defined_def false_def true_def
                           bot_fun_def null_fun_def null_option_def
                           bot_Bagbase_def null_Bagbase_def)
by((subst (asm) Abs_Bagbase_inject, auto simp add: bot_option_def null_option_def bot_Void_def),
   (subst (asm) Abs_Voidbase_inject, auto simp add: bot_option_def null_option_def))+

lemma Voidempty_defined : "δ Voidempty = true"
apply(rule ext, auto simp: Voidempty_def defined_def false_def true_def
                           bot_fun_def null_fun_def null_option_def
                           bot_Bagbase_def null_Bagbase_def)
by((subst (asm) Abs_Bagbase_inject, auto simp add: bot_option_def null_option_def bot_Void_def))+

lemma assumes "τ  δ (V :: ('𝔄,Voidbase) Bag)"
      shows   "τ  V  Voidnull  τ  V  Voidempty"
proof -
  have A:"x y. x  {}  y. y x"
  by (metis all_not_in_conv)
show "?thesis"
  apply(case_tac "V τ")
  proof - fix y show "V τ = Abs_Bagbase y 
                      y  {X. X =   X = null  X  = 0} 
                      τ  V  Voidnull  τ  V  Voidempty"
  apply(insert assms, case_tac y, simp add: bot_option_def, simp add: bot_Bagbase_def foundation16)
  apply(simp add: bot_option_def null_option_def)
  apply(erule disjE, metis OclValid_def defined_def foundation2 null_Bagbase_def null_fun_def true_def)
  proof - fix a show "V τ = Abs_Bagbase a  a  = 0  τ  V  Voidnull  τ  V  Voidempty"
  apply(case_tac a, simp, insert assms, metis OclValid_def foundation16 null_Bagbase_def true_def)
  apply(simp)
  proof - fix aa show " V τ = Abs_Bagbase aa  aa  = 0  τ  V  Voidnull  τ  V  Voidempty"
  apply(case_tac "aa (Abs_Voidbase None) = 0",
        rule disjI2,
        insert assms,
        simp add: Voidempty_def OclValid_def ApproxEq_def Rep_Set_base_def true_def Abs_Bagbase_inverse image_def)
  apply(intro allI)
  proof - fix x fix b show " V τ = Abs_Bagbase aa  aa  = 0  aa (Abs_Voidbase None) = 0  (δ V) τ = True  ¬ b < aa x"
    apply (case_tac x, auto)
     apply (simp add: bot_Void_def bot_option_def)
    apply (simp add: bot_option_def null_option_def)
  done
  apply_end(simp+, rule disjI1)
  show "V τ = Abs_Bagbase aa  aa  = 0  0 < aa (Abs_Voidbase None)  τ  δ V  τ  V  Voidnull"
  apply(simp add: Voidnull_def OclValid_def ApproxEq_def Rep_Set_base_def true_def Abs_Bagbase_inverse image_def,
        subst Abs_Bagbase_inverse, simp)
  using bot_Void_def apply auto[1]
  apply(simp)
  apply(rule equalityI, rule subsetI, simp)
   proof - fix x show "V τ = Abs_Bagbase aa 
            aa  = 0  0 < aa (Abs_Voidbase None)  (δ V) τ = True  b. b < aa x  x = Abs_Voidbase None"
   apply( case_tac x, auto)
    apply (simp add: bot_Void_def bot_option_def)
   by (simp add: bot_option_def null_option_def)
  qed ((simp add: bot_Void_def bot_option_def)+, blast)
qed qed qed qed qed

definition Boolean :: "('𝔄,Booleanbase) Bag"
where     "Boolean  (λ τ. (Abs_Bagbase o Some o Some)  (λ None  0 | Some None  0 | _  1))"

definition Booleannull :: "('𝔄,Booleanbase) Bag"
where     "Booleannull  (λ τ. (Abs_Bagbase o Some o Some)  (λ None  0 | _  1))"

lemma Boolean_defined : "δ Boolean = true"
apply(rule ext, auto simp: Boolean_def defined_def false_def true_def
                           bot_fun_def null_fun_def null_option_def)
by(simp_all add: Abs_Bagbase_inject bot_option_def bot_Bagbase_def null_Bagbase_def null_option_def)

lemma Booleannull_defined : "δ Booleannull = true"
apply(rule ext, auto simp: Booleannull_def defined_def false_def true_def
                           bot_fun_def null_fun_def null_option_def)
by(simp_all add: Abs_Bagbase_inject bot_option_def bot_Bagbase_def null_Bagbase_def null_option_def)

definition String :: "('𝔄,Stringbase) Bag"
where     "String  (λ τ. (Abs_Bagbase o Some o Some)  (λ None  0 | Some None  0 | _  1))"

definition Stringnull :: "('𝔄,Stringbase) Bag"
where     "Stringnull  (λ τ. (Abs_Bagbase o Some o Some)  (λ None  0 | _  1))"

lemma String_defined : "δ String = true"
apply(rule ext, auto simp: String_def defined_def false_def true_def
                           bot_fun_def null_fun_def null_option_def)
by(simp_all add: Abs_Bagbase_inject bot_option_def bot_Bagbase_def null_Bagbase_def null_option_def)

lemma Stringnull_defined : "δ Stringnull = true"
apply(rule ext, auto simp: Stringnull_def defined_def false_def true_def
                           bot_fun_def null_fun_def null_option_def)
by(simp_all add: Abs_Bagbase_inject bot_option_def bot_Bagbase_def null_Bagbase_def null_option_def)

definition Real :: "('𝔄,Realbase) Bag"
where     "Real  (λ τ. (Abs_Bagbase o Some o Some)  (λ None  0 | Some None  0 | _  1))"

definition Realnull :: "('𝔄,Realbase) Bag"
where     "Realnull  (λ τ. (Abs_Bagbase o Some o Some)  (λ None  0 | _  1))"

lemma Real_defined : "δ Real = true"
apply(rule ext, auto simp: Real_def defined_def false_def true_def
                           bot_fun_def null_fun_def null_option_def)
by(simp_all add: Abs_Bagbase_inject bot_option_def bot_Bagbase_def null_Bagbase_def null_option_def)

lemma Realnull_defined : "δ Realnull = true"
apply(rule ext, auto simp: Realnull_def defined_def false_def true_def
                           bot_fun_def null_fun_def null_option_def)
by(simp_all add: Abs_Bagbase_inject bot_option_def bot_Bagbase_def null_Bagbase_def null_option_def)

subsection‹Basic Properties of the Bag Type›

text‹Every element in a defined bag is valid.›

lemma Bag_inv_lemma: "τ  (δ X)  Rep_Bagbase (X τ) bot = 0"
apply(insert Rep_Bagbase [of "X τ"], simp)
apply(auto simp: OclValid_def defined_def false_def true_def cp_def
                 bot_fun_def bot_Bagbase_def null_Bagbase_def null_fun_def
           split:if_split_asm)
 apply(erule contrapos_pp [of "Rep_Bagbase (X τ) = bot"])
 apply(subst Abs_Bagbase_inject[symmetric], rule Rep_Bagbase, simp)
 apply(simp add: Rep_Bagbase_inverse bot_Bagbase_def bot_option_def)
apply(erule contrapos_pp [of "Rep_Bagbase (X τ) = null"])
apply(subst Abs_Bagbase_inject[symmetric], rule Rep_Bagbase, simp)
apply(simp add: Rep_Bagbase_inverse  null_option_def)
by (simp add: bot_option_def)

lemma Bag_inv_lemma' :
 assumes x_def : "τ  δ X"
     and e_mem : "Rep_Bagbase (X τ) e  1"
   shows "τ  υ (λ_. e)"
apply(case_tac "e = bot", insert assms, drule Bag_inv_lemma, simp)
by (simp add: foundation18')

lemma abs_rep_simp' :
 assumes S_all_def : "τ  δ S"
   shows "Abs_Bagbase Rep_Bagbase (S τ) = S τ"
proof -
 have discr_eq_false_true : "τ. (false τ = true τ) = False" by(simp add: false_def true_def)
 show ?thesis
  apply(insert S_all_def, simp add: OclValid_def defined_def)
  apply(rule mp[OF Abs_Bagbase_induct[where P = "λS. (if S =  τ  S = null τ
                                                    then false τ else true τ) = true τ 
                                                   Abs_Bagbase Rep_Bagbase S = S"]],
        rename_tac S')
   apply(simp add: Abs_Bagbase_inverse discr_eq_false_true)
   apply(case_tac S') apply(simp add: bot_fun_def bot_Bagbase_def)+
   apply(rename_tac S'', case_tac S'') apply(simp add: null_fun_def null_Bagbase_def)+
 done
qed

lemma invalid_bag_OclNot_defined [simp,code_unfold]:"δ(invalid::('𝔄,::null) Bag) = false" by simp
lemma null_bag_OclNot_defined [simp,code_unfold]:"δ(null::('𝔄,::null) Bag) = false"
by(simp add: defined_def null_fun_def)
lemma invalid_bag_valid [simp,code_unfold]:"υ(invalid::('𝔄,::null) Bag) = false"
by simp
lemma null_bag_valid [simp,code_unfold]:"υ(null::('𝔄,::null) Bag) = true"
apply(simp add: valid_def null_fun_def bot_fun_def bot_Bagbase_def null_Bagbase_def)
apply(subst Abs_Bagbase_inject,simp_all add: null_option_def bot_option_def)
done

text‹... which means that we can have a type ('𝔄,('𝔄,('𝔄) Integer) Bag) Bag›
corresponding exactly to Bag(Bag(Integer)) in OCL notation. Note that the parameter
'𝔄› still refers to the object universe; making the OCL semantics entirely parametric
in the object universe makes it possible to study (and prove) its properties
independently from a concrete class diagram.›

subsection‹Definition: Strict Equality \label{sec:bag-strict-equality}›

text‹After the part of foundational operations on bags, we detail here equality on bags.
Strong equality is inherited from the OCL core, but we have to consider
the case of the strict equality. We decide to overload strict equality in the
same way we do for other value's in OCL:›

overloading StrictRefEq  "StrictRefEq :: [('𝔄,::null)Bag,('𝔄,::null)Bag]  ('𝔄)Boolean"
begin
  definition StrictRefEqBag :
    "(x::('𝔄,::null)Bag)  y  λ τ. if (υ x) τ = true τ  (υ y) τ = true τ
                                       then (x  y)τ
                                       else invalid τ"
end

text‹One might object here that for the case of objects, this is an empty definition.
The answer is no, we will restrain later on states and objects such that any object
has its oid stored inside the object (so the ref, under which an object can be referenced
in the store will represented in the object itself). For such well-formed stores that satisfy
this invariant (the WFF-invariant), the referential equality and the
strong equality---and therefore the strict equality on bags in the sense above---coincides.›

text‹Property proof in terms of @{term "profile_binStrongEq_v_v"}
interpretation  StrictRefEqBag : profile_binStrongEq_v_v "λ x y. (x::('𝔄,::null)Bag)  y" 
         by unfold_locales (auto simp:  StrictRefEqBag)



subsection‹Constants: mtBag›
definition mtBag::"('𝔄,::null) Bag"  ("Bag{}")
where     "Bag{}  (λ τ.  Abs_Bagbase λ_. 0::nat )"


lemma mtBag_defined[simp,code_unfold]:"δ(Bag{}) = true"
apply(rule ext, auto simp: mtBag_def defined_def null_Bagbase_def
                           bot_Bagbase_def bot_fun_def null_fun_def)
by(simp_all add: Abs_Bagbase_inject bot_option_def null_option_def)

lemma mtBag_valid[simp,code_unfold]:"υ(Bag{}) = true"
apply(rule ext,auto simp: mtBag_def valid_def
                          bot_Bagbase_def bot_fun_def null_fun_def)
by(simp_all add: Abs_Bagbase_inject bot_option_def null_option_def)

lemma mtBag_rep_bag: "Rep_Bagbase (Bag{} τ) = (λ _. 0)"
 apply(simp add: mtBag_def, subst Abs_Bagbase_inverse)
by(simp add: bot_option_def)+

text_raw‹\isatagafp›

lemma [simp,code_unfold]: "const Bag{}"
by(simp add: const_def mtBag_def)


text‹Note that the collection types in OCL allow for null to be included;
  however, there is the null-collection into which inclusion yields invalid.›

text_raw‹\endisatagafp›

subsection‹Definition: Including›

definition OclIncluding   :: "[('𝔄,::null) Bag,('𝔄,) val]  ('𝔄,) Bag"
where     "OclIncluding x y = (λ τ. if (δ x) τ = true τ  (υ y) τ = true τ
                                    then Abs_Bagbase  Rep_Bagbase(x τ) 
                                                      ((y τ):=Rep_Bagbase(x τ)(y τ)+1) 
                                                    
                                    else invalid τ )"
notation   OclIncluding   ("_->includingBag'(_')")

interpretation OclIncluding : profile_bind_v OclIncluding "λx y. Abs_BagbaseRep_Bagbase x 
                                                      (y := Rep_Bagbase x y + 1)"
proof -  
   let ?X = "λx y. Rep_Bagbase(x) ((y):=Rep_Bagbase(x)( y )+1)"
   show "profile_bind_v OclIncluding (λx y. Abs_Bagbase  ?X x y )"
         apply unfold_locales  
          apply(auto simp:OclIncluding_def bot_option_def null_option_def 
                                           bot_Bagbase_def null_Bagbase_def)
          by(subst (asm) Abs_Bagbase_inject, simp_all,
             metis (mono_tags, lifting) Rep_Bagbase Rep_Bagbase_inverse bot_option_def mem_Collect_eq null_option_def,
             simp add: bot_option_def null_option_def)+
qed

syntax
  "_OclFinbag" :: "args => ('𝔄,'a::null) Bag"    ("Bag{(_)}")
translations
  "Bag{x, xs}" == "CONST OclIncluding (Bag{xs}) x"
  "Bag{x}"     == "CONST OclIncluding (Bag{}) x "


subsection‹Definition: Excluding›

definition OclExcluding   :: "[('𝔄,::null) Bag,('𝔄,) val]  ('𝔄,) Bag"
where     "OclExcluding x y = (λ τ.  if (δ x) τ = true τ  (υ y) τ = true τ
                                     then Abs_Bagbase  Rep_Bagbase (x τ) ((y τ):=0::nat) 
                                     else invalid τ )"
notation   OclExcluding   ("_->excludingBag'(_')")

interpretation OclExcluding: profile_bind_v OclExcluding  
                            "λx y. Abs_Bagbase Rep_Bagbase(x)(y:=0::nat)"
proof -
    show "profile_bind_v OclExcluding (λx y. Abs_Bagbase Rep_Bagbase x(y := 0))"
         apply unfold_locales  
         apply(auto simp:OclExcluding_def bot_option_def null_option_def  
                         null_Bagbase_def bot_Bagbase_def)
         by(subst (asm) Abs_Bagbase_inject,
               simp_all add: bot_option_def null_option_def,
               metis (mono_tags, lifting) Rep_Bagbase Rep_Bagbase_inverse bot_option_def
                                          mem_Collect_eq null_option_def)+
qed

subsection‹Definition: Includes›

definition OclIncludes   :: "[('𝔄,::null) Bag,('𝔄,) val]  '𝔄 Boolean"
where     "OclIncludes x y = (λ τ.   if (δ x) τ = true τ  (υ y) τ = true τ
                                     then  Rep_Bagbase (x τ) (y τ) > 0 
                                     else   )"
notation   OclIncludes    ("_->includesBag'(_')" (*[66,65]65*))

interpretation OclIncludes : profile_bind_v OclIncludes "λx y.  Rep_Bagbase x y > 0 "
by(unfold_locales, auto simp:OclIncludes_def bot_option_def null_option_def invalid_def)

subsection‹Definition: Excludes›

definition OclExcludes   :: "[('𝔄,::null) Bag,('𝔄,) val]  '𝔄 Boolean"
where     "OclExcludes x y = (not(OclIncludes x y))"
notation   OclExcludes    ("_->excludesBag'(_')" (*[66,65]65*))

text‹The case of the size definition is somewhat special, we admit
explicitly in Featherweight OCL the possibility of infinite bags. For
the size definition, this requires an extra condition that assures
that the cardinality of the bag is actually a defined integer.›

interpretation OclExcludes : profile_bind_v OclExcludes "λx y.  Rep_Bagbase x y  0 "
by(unfold_locales, auto simp:OclExcludes_def OclIncludes_def OclNot_def bot_option_def null_option_def invalid_def)

subsection‹Definition: Size›

definition OclSize     :: "('𝔄,::null)Bag  '𝔄 Integer"
where     "OclSize x = (λ τ. if (δ x) τ = true τ  finite (Rep_Bag_base x τ)
                             then  int (card (Rep_Bag_base x τ)) 
                             else  )"
notation  (* standard ascii syntax *)
           OclSize        ("_->sizeBag'(')" (*[66]*))

text‹The following definition follows the requirement of the
standard to treat null as neutral element of bags. It is
a well-documented exception from the general strictness
rule and the rule that the distinguished argument self should
be non-null.›

(*TODO Locale - Equivalent*)  


subsection‹Definition: IsEmpty›

definition OclIsEmpty   :: "('𝔄,::null) Bag  '𝔄 Boolean"
where     "OclIsEmpty x =  ((υ x and not (δ x)) or ((OclSize x)  𝟬))"
notation   OclIsEmpty     ("_->isEmptyBag'(')" (*[66]*))

(*TODO Locale - Equivalent*)  

subsection‹Definition: NotEmpty›

definition OclNotEmpty   :: "('𝔄,::null) Bag  '𝔄 Boolean"
where     "OclNotEmpty x =  not(OclIsEmpty x)"
notation   OclNotEmpty    ("_->notEmptyBag'(')" (*[66]*))

(*TODO Locale - Equivalent*)  

subsection‹Definition: Any›

(* Slight breach of naming convention in order to avoid naming conflict on constant.*)
definition OclANY   :: "[('𝔄,::null) Bag]  ('𝔄,) val"
where     "OclANY x = (λ τ. if (υ x) τ = true τ
                            then if (δ x and OclNotEmpty x) τ = true τ
                                 then SOME y. y  (Rep_Set_base x τ)
                                 else null τ
                            else  )"
notation   OclANY   ("_->anyBag'(')")

(*TODO Locale - Equivalent*)  

(* actually, this definition covers only: X->anyBag(true) of the standard, which foresees
a (totally correct) high-level definition
source->anyBag(iterator | body) =
source->select(iterator | body)->asSequence()->first(). Since we don't have sequences,
we have to go for a direct---restricted---definition. *)

subsection‹Definition: Forall›

text‹The definition of OclForall mimics the one of @{term "OclAnd"}:
OclForall is not a strict operation.›
definition OclForall     :: "[('𝔄,::null)Bag,('𝔄,)val('𝔄)Boolean]  '𝔄 Boolean"
where     "OclForall S P = (λ τ. if (δ S) τ = true τ
                                 then if (xRep_Set_base S τ. P (λ_. x) τ = false τ)
                                      then false τ
                                      else if (xRep_Set_base S τ. P (λ_. x) τ = invalid τ)
                                           then invalid τ
                                           else if (xRep_Set_base S τ. P (λ_. x) τ = null τ)
                                                then null τ
                                                else true τ
                                 else )"
syntax
  "_OclForallBag" :: "[('𝔄,::null) Bag,id,('𝔄)Boolean]  '𝔄 Boolean"    ("(_)->forAllBag'(_|_')")
translations
  "X->forAllBag(x | P)" == "CONST UML_Bag.OclForall X (%x. P)"

(*TODO Locale - Equivalent*)  

subsection‹Definition: Exists›
  
text‹Like OclForall, OclExists is also not strict.›
definition OclExists     :: "[('𝔄,::null) Bag,('𝔄,)val('𝔄)Boolean]  '𝔄 Boolean"
where     "OclExists S P = not(UML_Bag.OclForall S (λ X. not (P X)))"

syntax
  "_OclExistBag" :: "[('𝔄,::null) Bag,id,('𝔄)Boolean]  '𝔄 Boolean"    ("(_)->existsBag'(_|_')")
translations
  "X->existsBag(x | P)" == "CONST UML_Bag.OclExists X (%x. P)"

(*TODO Locale - Equivalent*)  
  
subsection‹Definition: Iterate›

definition OclIterate :: "[('𝔄,::null) Bag,('𝔄,::null)val,
                           ('𝔄,)val('𝔄,)val('𝔄,)val]  ('𝔄,)val"
where     "OclIterate S A F = (λ τ. if (δ S) τ = true τ  (υ A) τ = true τ  finite (Rep_Bag_base S τ)
                                    then Finite_Set.fold (F o (λa τ. a) o fst) A (Rep_Bag_base S τ) τ
                                    else )"
syntax
  "_OclIterateBag"  :: "[('𝔄,::null) Bag, idt, idt, , ] => ('𝔄,)val"
                        ("_ ->iterateBag'(_;_=_ | _')" (*[71,100,70]50*))
translations
  "X->iterateBag(a; x = A | P)" == "CONST OclIterate X A (%a. (% x. P))"

(*TODO Locale - Equivalent*)  

subsection‹Definition: Select›
  
  
definition OclSelect :: "[('𝔄,::null)Bag,('𝔄,)val('𝔄)Boolean]  ('𝔄,)Bag"
where "OclSelect S P = (λτ. if (δ S) τ = true τ
                              then if (xRep_Set_base S τ. P(λ _. x) τ = invalid τ)
                                   then invalid τ
                                   else Abs_Bagbase λx. 
                                          let n =  Rep_Bagbase (S τ)  x in
                                          if n = 0 | P (λ_. x) τ = false τ then
                                            0
                                          else
                                            n
                              else invalid τ)"
syntax
  "_OclSelectBag" :: "[('𝔄,::null) Bag,id,('𝔄)Boolean]  '𝔄 Boolean"    ("(_)->selectBag'(_|_')")
translations
  "X->selectBag(x | P)" == "CONST OclSelect X (% x. P)"

(*TODO Locale - Equivalent*)  

subsection‹Definition: Reject›

definition OclReject :: "[('𝔄,::null)Bag,('𝔄,)val('𝔄)Boolean]  ('𝔄,::null)Bag"
where "OclReject S P = OclSelect S (not o P)"
syntax
  "_OclRejectBag" :: "[('𝔄,::null) Bag,id,('𝔄)Boolean]  '𝔄 Boolean"    ("(_)->rejectBag'(_|_')")
translations
  "X->rejectBag(x | P)" == "CONST OclReject X (% x. P)"

(*TODO Locale - Equivalent*)  

subsection‹Definition: IncludesAll›

definition OclIncludesAll   :: "[('𝔄,::null) Bag,('𝔄,) Bag]  '𝔄 Boolean"
where     "OclIncludesAll x y = (λ τ.   if (δ x) τ = true τ  (δ y) τ = true τ
                                        then Rep_Bag_base y τ  Rep_Bag_base x τ 
                                        else   )"
notation   OclIncludesAll ("_->includesAllBag'(_')" (*[66,65]65*))

interpretation OclIncludesAll : profile_bind_d OclIncludesAll "λx y. Rep_Bag_base' y  Rep_Bag_base' x "
by(unfold_locales, auto simp:OclIncludesAll_def bot_option_def null_option_def invalid_def
                             Rep_Bag_base_def Rep_Bag_base'_def)

subsection‹Definition: ExcludesAll›

definition OclExcludesAll   :: "[('𝔄,::null) Bag,('𝔄,) Bag]  '𝔄 Boolean"
where     "OclExcludesAll x y = (λ τ.   if (δ x) τ = true τ  (δ y) τ = true τ
                                        then Rep_Bag_base y τ  Rep_Bag_base x τ = {} 
                                        else   )"
notation  OclExcludesAll ("_->excludesAllBag'(_')" (*[66,65]65*))

interpretation OclExcludesAll : profile_bind_d OclExcludesAll "λx y. Rep_Bag_base' y  Rep_Bag_base' x = {} "
by(unfold_locales, auto simp:OclExcludesAll_def bot_option_def null_option_def invalid_def
                             Rep_Bag_base_def Rep_Bag_base'_def)

subsection‹Definition: Union›

definition OclUnion   :: "[('𝔄,::null) Bag,('𝔄,) Bag]  ('𝔄,) Bag"
where     "OclUnion x y = (λ τ. if (δ x) τ = true τ  (δ y) τ = true τ
                                then Abs_Bagbase  λ X. Rep_Bagbase (x τ) X + 
                                                       Rep_Bagbase (y τ) X
                                else invalid τ )"
notation   OclUnion       ("_->unionBag'(_')"          (*[66,65]65*))

interpretation OclUnion : 
               profile_bind_d OclUnion "λx y. Abs_Bagbase  λ X. Rep_Bagbase x X + 
                                                                Rep_Bagbase y X"
proof -  
   show "profile_bind_d OclUnion (λx y. Abs_Bagbase  λ X. Rep_Bagbase x X + Rep_Bagbase y X)"
   apply unfold_locales 
   apply(auto simp:OclUnion_def bot_option_def null_option_def 
                   null_Bagbase_def bot_Bagbase_def)
   by(subst (asm) Abs_Bagbase_inject,
      simp_all add: bot_option_def null_option_def, 
      metis (mono_tags, lifting) Rep_Bagbase Rep_Bagbase_inverse bot_option_def mem_Collect_eq
                                 null_option_def)+
qed

subsection‹Definition: Intersection›

definition OclIntersection   :: "[('𝔄,::null) Bag,('𝔄,) Bag]  ('𝔄,) Bag"
where     "OclIntersection x y = (λ τ.  if (δ x) τ = true τ  (δ y) τ = true τ
                                        then Abs_Bagbase λ X. min (Rep_Bagbase (x τ) X) 
                                                       (Rep_Bagbase (y τ) X)
                                        else   )"
notation   OclIntersection("_->intersectionBag'(_')"   (*[71,70]70*))

interpretation OclIntersection : 
               profile_bind_d OclIntersection "λx y. Abs_Bagbase  λ X. min (Rep_Bagbase x X) 
                                                                (Rep_Bagbase y X)"
proof -  
   show "profile_bind_d OclIntersection (λx y. Abs_Bagbase  λ X. min (Rep_Bagbase x X) 
                                                                (Rep_Bagbase y X))"
   apply unfold_locales 
   apply(auto simp:OclIntersection_def bot_option_def null_option_def 
                   null_Bagbase_def bot_Bagbase_def invalid_def)
   by(subst (asm) Abs_Bagbase_inject,
      simp_all add: bot_option_def null_option_def, 
      metis (mono_tags, lifting) Rep_Bagbase Rep_Bagbase_inverse bot_option_def mem_Collect_eq min_0R
                                 null_option_def)+
qed

subsection‹Definition: Count›

definition OclCount   :: "[('𝔄,::null) Bag,('𝔄,) val]  ('𝔄) Integer"
where     "OclCount x y = (λ τ. if (δ x) τ = true τ  (δ y) τ = true τ
                             then  int(Rep_Bagbase (x τ) (y τ)) 
                             else invalid τ )"
notation   OclCount ("_->countBag'(_')"  (*[66,65]65*))

interpretation OclCount : profile_bind_d OclCount "λx y. int(Rep_Bagbase x y)"
by(unfold_locales, auto simp:OclCount_def bot_option_def null_option_def)

subsection‹Definition (future operators)›

consts (* abstract bag collection operations *)
    OclSum         :: " ('𝔄,::null) Bag  '𝔄 Integer"
  
notation  OclSum         ("_->sumBag'(')" (*[66]*))

subsection‹Logical Properties›

text‹OclIncluding›

lemma OclIncluding_valid_args_valid:
"(τ  υ(X->includingBag(x))) = ((τ (δ X))  (τ (υ x)))"
by (metis (opaque_lifting, no_types) OclIncluding.def_valid_then_def OclIncluding.defined_args_valid)

lemma OclIncluding_valid_args_valid''[simp,code_unfold]:
"υ(X->includingBag(x)) = ((δ X) and (υ x))"
by (simp add: OclIncluding.def_valid_then_def)

text‹etc. etc.›
text_raw‹\isatagafp› 

text‹OclExcluding›

lemma OclExcluding_valid_args_valid:
"(τ  υ(X->excludingBag(x))) = ((τ (δ X))  (τ (υ x)))"
by (metis OclExcluding.def_valid_then_def OclExcluding.defined_args_valid)

lemma OclExcluding_valid_args_valid''[simp,code_unfold]:
"υ(X->excludingBag(x)) = ((δ X) and (υ x))"
by (simp add: OclExcluding.def_valid_then_def)

text‹OclIncludes›

lemma OclIncludes_valid_args_valid:
"(τ  υ(X->includesBag(x))) = ((τ (δ X))  (τ (υ x)))"
by (simp add: OclIncludes.def_valid_then_def foundation10')

lemma OclIncludes_valid_args_valid''[simp,code_unfold]:
"υ(X->includesBag(x)) = ((δ X) and (υ x))"
by (simp add: OclIncludes.def_valid_then_def)

text‹OclExcludes›

lemma OclExcludes_valid_args_valid:
"(τ  υ(X->excludesBag(x))) = ((τ (δ X))  (τ (υ x)))"
by (simp add: OclExcludes.def_valid_then_def foundation10')

lemma OclExcludes_valid_args_valid''[simp,code_unfold]:
"υ(X->excludesBag(x)) = ((δ X) and (υ x))"
by (simp add: OclExcludes.def_valid_then_def)

text‹OclSize›

lemma OclSize_defined_args_valid: "τ  δ (X->sizeBag())  τ  δ X"
by(auto simp: OclSize_def OclValid_def true_def valid_def false_def StrongEq_def
              defined_def invalid_def bot_fun_def null_fun_def
        split: bool.split_asm HOL.if_split_asm option.split)

lemma OclSize_infinite:
assumes non_finite:"τ  not(δ(S->sizeBag()))"
shows   "(τ  not(δ(S)))  ¬ finite (Rep_Bag_base S τ)"
apply(insert non_finite, simp)
apply(rule impI)
apply(simp add: OclSize_def OclValid_def defined_def bot_fun_def null_fun_def bot_option_def null_option_def
           split: if_split_asm)
done

lemma "τ  δ X  ¬ finite (Rep_Bag_base X τ)  ¬ τ  δ (X->sizeBag())"
by(simp add: OclSize_def OclValid_def defined_def bot_fun_def false_def true_def)

lemma size_defined:
 assumes X_finite: "τ. finite (Rep_Bag_base X τ)"
 shows "δ (X->sizeBag()) = δ X"
 apply(rule ext, simp add: cp_defined[of "X->sizeBag()"] OclSize_def)
 apply(simp add: defined_def bot_option_def bot_fun_def null_option_def null_fun_def X_finite)
done

lemma size_defined':
 assumes X_finite: "finite (Rep_Bag_base X τ)"
 shows "(τ  δ (X->sizeBag())) = (τ  δ X)"
 apply(simp add: cp_defined[of "X->sizeBag()"] OclSize_def OclValid_def)
 apply(simp add: defined_def bot_option_def bot_fun_def null_option_def null_fun_def X_finite)
done

text‹OclIsEmpty›

lemma OclIsEmpty_defined_args_valid:"τ  δ (X->isEmptyBag())  τ  υ X"
  apply(auto simp: OclIsEmpty_def OclValid_def defined_def valid_def false_def true_def
                   bot_fun_def null_fun_def OclAnd_def OclOr_def OclNot_def
             split: if_split_asm)
  apply(case_tac "(X->sizeBag()  𝟬) τ", simp add: bot_option_def, simp, rename_tac x)
  apply(case_tac x, simp add: null_option_def bot_option_def, simp)
  apply(simp add: OclSize_def StrictRefEqInteger valid_def)
by (metis (opaque_lifting, no_types)
           bot_fun_def OclValid_def defined_def foundation2 invalid_def)

lemma "τ  δ (null->isEmptyBag())"
by(auto simp: OclIsEmpty_def OclValid_def defined_def valid_def false_def true_def
              bot_fun_def null_fun_def OclAnd_def OclOr_def OclNot_def null_is_valid
        split: if_split_asm)

lemma OclIsEmpty_infinite: "τ  δ X  ¬ finite (Rep_Bag_base X τ)  ¬ τ  δ (X->isEmptyBag())"
  apply(auto simp: OclIsEmpty_def OclValid_def defined_def valid_def false_def true_def
                   bot_fun_def null_fun_def OclAnd_def OclOr_def OclNot_def
             split: if_split_asm)
  apply(case_tac "(X->sizeBag()  𝟬) τ", simp add: bot_option_def, simp, rename_tac x)
  apply(case_tac x, simp add: null_option_def bot_option_def, simp)
by(simp add: OclSize_def StrictRefEqInteger valid_def bot_fun_def false_def true_def invalid_def)

text‹OclNotEmpty›

lemma OclNotEmpty_defined_args_valid:"τ  δ (X->notEmptyBag())  τ  υ X"
by (metis (opaque_lifting, no_types) OclNotEmpty_def OclNot_defargs OclNot_not foundation6 foundation9
                                OclIsEmpty_defined_args_valid)

lemma "τ  δ (null->notEmptyBag())"
by (metis (opaque_lifting, no_types) OclNotEmpty_def OclAnd_false1 OclAnd_idem OclIsEmpty_def
                                OclNot3 OclNot4 OclOr_def defined2 defined4 transform1 valid2)

lemma OclNotEmpty_infinite: "τ  δ X  ¬ finite (Rep_Bag_base X τ)  ¬ τ  δ (X->notEmptyBag())"
 apply(simp add: OclNotEmpty_def)
 apply(drule OclIsEmpty_infinite, simp)
by (metis OclNot_defargs OclNot_not foundation6 foundation9)

lemma OclNotEmpty_has_elt : "τ  δ X 
                          τ  X->notEmptyBag() 
                          e. e  (Rep_Bag_base X τ)"
proof -
 have s_non_empty: "S. S  {}  x. x  S"
 by blast
show "τ  δ X 
      τ  X->notEmptyBag() 
      ?thesis"
 apply(simp add: OclNotEmpty_def OclIsEmpty_def deMorgan1 deMorgan2, drule foundation5)
 apply(subst (asm) (2) OclNot_def,
       simp add: OclValid_def StrictRefEqInteger StrongEq_def
            split: if_split_asm)
  prefer 2
  apply(simp add: invalid_def bot_option_def true_def)
 apply(simp add: OclSize_def valid_def split: if_split_asm,
       simp_all add: false_def true_def bot_option_def bot_fun_def OclInt0_def)
 apply(drule s_non_empty[of "Rep_Bag_base X τ"], erule exE, case_tac x)
by blast
qed

lemma OclNotEmpty_has_elt' : "τ  δ X 
                          τ  X->notEmptyBag() 
                          e. e  (Rep_Set_base X τ)"
 apply(drule OclNotEmpty_has_elt, simp)
by(simp add: Rep_Bag_base_def Rep_Set_base_def image_def)

text‹OclANY›

lemma OclANY_defined_args_valid: "τ  δ (X->anyBag())  τ  δ X"
by(auto simp: OclANY_def OclValid_def true_def valid_def false_def StrongEq_def
              defined_def invalid_def bot_fun_def null_fun_def OclAnd_def
        split: bool.split_asm HOL.if_split_asm option.split)

lemma "τ  δ X  τ  X->isEmptyBag()  ¬ τ  δ (X->anyBag())"
 apply(simp add: OclANY_def OclValid_def)
 apply(subst cp_defined, subst cp_OclAnd, simp add: OclNotEmpty_def, subst (1 2) cp_OclNot,
       simp add: cp_OclNot[symmetric] cp_OclAnd[symmetric] cp_defined[symmetric],
       simp add: false_def true_def)
by(drule foundation20[simplified OclValid_def true_def], simp)

lemma OclANY_valid_args_valid:
"(τ  υ(X->anyBag())) = (τ  υ X)"
proof -
 have A: "(τ  υ(X->anyBag()))  ((τ (υ X)))"
          by(auto simp: OclANY_def OclValid_def true_def valid_def false_def StrongEq_def
                        defined_def invalid_def bot_fun_def null_fun_def
                  split: bool.split_asm HOL.if_split_asm option.split)
 have B: "(τ (υ X))  (τ  υ(X->anyBag()))"
           apply(auto simp: OclANY_def OclValid_def true_def false_def StrongEq_def
                            defined_def invalid_def valid_def bot_fun_def null_fun_def
                            bot_option_def null_option_def null_is_valid
                            OclAnd_def
                      split: bool.split_asm HOL.if_split_asm option.split)
           apply(frule Bag_inv_lemma[OF foundation16[THEN iffD2], OF conjI], simp)
           apply(subgoal_tac "(δ X) τ = true τ")
            prefer 2
            apply (metis (opaque_lifting, no_types) OclValid_def foundation16)
           apply(simp add: true_def,
                 drule OclNotEmpty_has_elt'[simplified OclValid_def true_def], simp)
           apply(erule exE,
                 rule someI2[where Q = "λx. x  " and P = "λy. y  (Rep_Set_base X τ)",
                             simplified not_def, THEN mp], simp, auto)
          by(simp add: Rep_Set_base_def image_def)
 show ?thesis by(auto dest:A intro:B)
qed

lemma OclANY_valid_args_valid''[simp,code_unfold]:
"υ(X->anyBag()) = (υ X)"
by(auto intro!: OclANY_valid_args_valid transform2_rev)

(* and higher order ones : forall, exists, iterate, select, reject... *)
text_raw‹\endisatagafp› 

subsection‹Execution Laws with Invalid or Null or Infinite Set as Argument›

text‹OclIncluding› (* properties already generated by the corresponding locale *)

text‹OclExcluding› (* properties already generated by the corresponding locale *)

text‹OclIncludes› (* properties already generated by the corresponding locale *)

text‹OclExcludes› (* properties already generated by the corresponding locale *)

text‹OclSize›

lemma OclSize_invalid[simp,code_unfold]:"(invalid->sizeBag()) = invalid"
by(simp add: bot_fun_def OclSize_def invalid_def defined_def valid_def false_def true_def)

lemma OclSize_null[simp,code_unfold]:"(null->sizeBag()) = invalid"
by(rule ext,
   simp add: bot_fun_def null_fun_def null_is_valid OclSize_def
             invalid_def defined_def valid_def false_def true_def)

text‹OclIsEmpty›

lemma OclIsEmpty_invalid[simp,code_unfold]:"(invalid->isEmptyBag()) = invalid"
by(simp add: OclIsEmpty_def)

lemma OclIsEmpty_null[simp,code_unfold]:"(null->isEmptyBag()) = true"
by(simp add: OclIsEmpty_def)

text‹OclNotEmpty›

lemma OclNotEmpty_invalid[simp,code_unfold]:"(invalid->notEmptyBag()) = invalid"
by(simp add: OclNotEmpty_def)

lemma OclNotEmpty_null[simp,code_unfold]:"(null->notEmptyBag()) = false"
by(simp add: OclNotEmpty_def)

text‹OclANY›

lemma OclANY_invalid[simp,code_unfold]:"(invalid->anyBag()) = invalid"
by(simp add: bot_fun_def OclANY_def invalid_def defined_def valid_def false_def true_def)

lemma OclANY_null[simp,code_unfold]:"(null->anyBag()) = null"
by(simp add: OclANY_def false_def true_def)

text‹OclForall›

lemma OclForall_invalid[simp,code_unfold]:"invalid->forAllBag(a| P a) = invalid"
by(simp add: bot_fun_def invalid_def OclForall_def defined_def valid_def false_def true_def)

lemma OclForall_null[simp,code_unfold]:"null->forAllBag(a | P a) = invalid"
by(simp add: bot_fun_def invalid_def OclForall_def defined_def valid_def false_def true_def)

text‹OclExists›

lemma OclExists_invalid[simp,code_unfold]:"invalid->existsBag(a| P a) = invalid"
by(simp add: OclExists_def)

lemma OclExists_null[simp,code_unfold]:"null->existsBag(a | P a) = invalid"
by(simp add: OclExists_def)

text‹OclIterate›

lemma OclIterate_invalid[simp,code_unfold]:"invalid->iterateBag(a; x = A | P a x) = invalid"
by(simp add: bot_fun_def invalid_def OclIterate_def defined_def valid_def false_def true_def)

lemma OclIterate_null[simp,code_unfold]:"null->iterateBag(a; x = A | P a x) = invalid"
by(simp add: bot_fun_def invalid_def OclIterate_def defined_def valid_def false_def true_def)


lemma OclIterate_invalid_args[simp,code_unfold]:"S->iterateBag(a; x = invalid | P a x) = invalid"
by(simp add: bot_fun_def invalid_def OclIterate_def defined_def valid_def false_def true_def)

text‹An open question is this ...›
lemma (*OclIterate_null_args[simp,code_unfold]:*) "S->iterateBag(a; x = null | P a x) = invalid"
oops
(* In the definition above, this does not hold in general.
       And I believe, this is how it should be ... *)

lemma OclIterate_infinite:
assumes non_finite: "τ  not(δ(S->sizeBag()))"
shows "(OclIterate S A F) τ = invalid τ"
apply(insert non_finite [THEN OclSize_infinite])
apply(subst (asm) foundation9, simp)
by(metis OclIterate_def OclValid_def invalid_def)

text‹OclSelect›

lemma OclSelect_invalid[simp,code_unfold]:"invalid->selectBag(a | P a) = invalid"
by(simp add: bot_fun_def invalid_def OclSelect_def defined_def valid_def false_def true_def)

lemma OclSelect_null[simp,code_unfold]:"null->selectBag(a | P a) = invalid"
by(simp add: bot_fun_def invalid_def OclSelect_def defined_def valid_def false_def true_def)

text‹OclReject›

lemma OclReject_invalid[simp,code_unfold]:"invalid->rejectBag(a | P a) = invalid"
by(simp add: OclReject_def)

lemma OclReject_null[simp,code_unfold]:"null->rejectBag(a | P a) = invalid"
by(simp add: OclReject_def)

text_raw‹\isatagafp›

subsubsection‹Context Passing›

lemma cp_OclIncludes1:
"(X->includesBag(x)) τ = (X->includesBag(λ _. x τ)) τ"
by(auto simp: OclIncludes_def StrongEq_def invalid_def
                 cp_defined[symmetric] cp_valid[symmetric])

lemma cp_OclSize: "X->sizeBag() τ = ((λ_. X τ)->sizeBag()) τ"
by(simp add: OclSize_def cp_defined[symmetric] Rep_Bag_base_def)

lemma cp_OclIsEmpty: "X->isEmptyBag() τ = ((λ_. X τ)->isEmptyBag()) τ"
 apply(simp only: OclIsEmpty_def)
 apply(subst (2) cp_OclOr,
       subst cp_OclAnd,
       subst cp_OclNot,
       subst StrictRefEqInteger.cp0)
by(simp add: cp_defined[symmetric] cp_valid[symmetric] StrictRefEqInteger.cp0[symmetric]
             cp_OclSize[symmetric] cp_OclNot[symmetric] cp_OclAnd[symmetric] cp_OclOr[symmetric])

lemma cp_OclNotEmpty: "X->notEmptyBag() τ = ((λ_. X τ)->notEmptyBag()) τ"
 apply(simp only: OclNotEmpty_def)
 apply(subst (2) cp_OclNot)
by(simp add: cp_OclNot[symmetric] cp_OclIsEmpty[symmetric])

lemma cp_OclANY: "X->anyBag() τ = ((λ_. X τ)->anyBag()) τ"
 apply(simp only: OclANY_def)
 apply(subst (2) cp_OclAnd)
by(simp only: cp_OclAnd[symmetric] cp_defined[symmetric] cp_valid[symmetric]
              cp_OclNotEmpty[symmetric] Rep_Set_base_def)

lemma cp_OclForall:
"(S->forAllBag(x | P x)) τ = ((λ _. S τ)->forAllBag(x | P (λ _. x τ))) τ"
by(auto simp add: OclForall_def cp_defined[symmetric] Rep_Set_base_def)

(* first-order version !*)
lemma cp_OclForall1 [simp,intro!]:
"cp S  cp (λX. ((S X)->forAllBag(x | P x)))"
apply(simp add: cp_def)
apply(erule exE, rule exI, intro allI)
apply(erule_tac x=X in allE)
by(subst cp_OclForall, simp)

lemma (*cp_OclForall2 [simp,intro!]:*)
"cp (λX St x. P (λτ. x) X St)  cp S  cp (λX. (S X)->forAllBag(x|P x X)) "
apply(simp only: cp_def)
oops

lemma (*cp_OclForall:*)
"cp S 
 ( x. cp(P x)) 
 cp(λX. ((S X)->forAllBag(x | P x X)))"
oops

lemma cp_OclExists:
"(S->existsBag(x | P x)) τ = ((λ _. S τ)->existsBag(x | P (λ _. x τ))) τ"
by(simp add: OclExists_def OclNot_def, subst cp_OclForall, simp)

(* first-order version !*)
lemma cp_OclExists1 [simp,intro!]:
"cp S  cp (λX. ((S X)->existsBag(x | P x)))"
apply(simp add: cp_def)
apply(erule exE, rule exI, intro allI)
apply(erule_tac x=X in allE)
by(subst cp_OclExists,simp)

lemma cp_OclIterate: 
     "(X->iterateBag(a; x = A | P a x)) τ =
                ((λ _. X τ)->iterateBag(a; x = A | P a x)) τ"
by(simp add: OclIterate_def cp_defined[symmetric] Rep_Bag_base_def)

lemma cp_OclSelect: "(X->selectBag(a | P a)) τ =
                ((λ _. X τ)->selectBag(a | P a)) τ"
by(simp add: OclSelect_def cp_defined[symmetric] Rep_Set_base_def)

lemma cp_OclReject: "(X->rejectBag(a | P a)) τ = ((λ _. X τ)->rejectBag(a | P a)) τ"
by(simp add: OclReject_def, subst cp_OclSelect, simp)

lemmas cp_intro''Bag[intro!,simp,code_unfold] =
       cp_OclSize      [THEN allI[THEN allI[THEN cpI1], of "OclSize"]]
       cp_OclIsEmpty   [THEN allI[THEN allI[THEN cpI1], of "OclIsEmpty"]]
       cp_OclNotEmpty  [THEN allI[THEN allI[THEN cpI1], of "OclNotEmpty"]]
       cp_OclANY       [THEN allI[THEN allI[THEN cpI1], of "OclANY"]]

subsubsection‹Const›

lemma const_OclIncluding[simp,code_unfold] :
 assumes const_x : "const x"
     and const_S : "const S"
   shows  "const (S->includingBag(x))"
   proof -
     have A:"τ τ'. ¬ (τ  υ x)  (S->includingBag(x) τ) = (S->includingBag(x) τ')"
            apply(simp add: foundation18)
            apply(erule const_subst[OF const_x const_invalid],simp_all)
            by(rule const_charn[OF const_invalid])
     have B: " τ τ'. ¬ (τ  δ S)  (S->includingBag(x) τ) = (S->includingBag(x) τ')"
            apply(simp add: foundation16', elim disjE)
            apply(erule const_subst[OF const_S const_invalid],simp_all)
            apply(rule const_charn[OF const_invalid])
            apply(erule const_subst[OF const_S const_null],simp_all)
            by(rule const_charn[OF const_invalid])
     show ?thesis
       apply(simp only: const_def,intro allI, rename_tac τ τ')
       apply(case_tac "¬ (τ  υ x)", simp add: A)
       apply(case_tac "¬ (τ  δ S)", simp_all add: B)
       apply(frule_tac τ'1= τ' in  const_OclValid2[OF const_x, THEN iffD1])
       apply(frule_tac τ'1= τ' in  const_OclValid1[OF const_S, THEN iffD1])
       apply(simp add: OclIncluding_def OclValid_def)
       apply(subst (1 2) const_charn[OF const_x])
       apply(subst (1 2) const_charn[OF const_S])
       by simp
qed
text_raw‹\endisatagafp›

subsection‹Test Statements›

(*Assert   "(τ ⊨ (Bag{λ_. ⌊⌊x⌋⌋} ≐ Bag{λ_. ⌊⌊x⌋⌋}))"
Assert   "(τ ⊨ (Bag{λ_. ⌊x⌋} ≐ Bag{λ_. ⌊x⌋}))"*)

instantiation Bagbase  :: (equal)equal
begin
  definition "HOL.equal k l   (k::('a::equal)Bagbase) =  l"
  instance   by standard (rule equal_Bagbase_def)
end

lemma equal_Bagbase_code [code]:
  "HOL.equal k (l::('a::{equal,null})Bagbase)  Rep_Bagbase k = Rep_Bagbase l"
  by (auto simp add: equal Bagbase.Rep_Bagbase_inject)

Assert   "τ  (Bag{}  Bag{})" 

(*
Assert   "τ ⊨ not(Bag{𝟭,𝟭}      ≜ Bag{𝟭})" 
Assert   "τ ⊨ (Bag{𝟭,𝟮}         ≜ Bag{𝟮,𝟭}" 
Assert   "τ ⊨ (Bag{𝟭,null}      ≜ Bag{null,𝟭}" 
Assert   "τ ⊨ (Bag{𝟭,invalid,𝟮} ≜ invalid)"
Assert   "τ ⊨ (Bag{𝟭,𝟮}->includingBag(null) ≜ Bag{𝟭,𝟮,null})"
*)

(* > *)

end