Theory UML_Pair

(*****************************************************************************
 * Featherweight-OCL --- A Formal Semantics for UML-OCL Version OCL 2.5
 *                       for the OMG Standard.
 *                       http://www.brucker.ch/projects/hol-testgen/
 *
 * UML_Pair.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_Pair
imports "../UML_PropertyProfiles"
begin

section‹Collection Type Pairs: Operations \label{sec:collection_pairs}›

text‹The OCL standard provides the concept of \emph{Tuples}, \ie{} a family of record-types
with projection functions. In FeatherWeight OCL,  only the theory of a special case is
developped, namely the type of Pairs, which is, however, sufficient for all applications
since it can be used to mimick all tuples. In particular, it can be used to express operations
with multiple arguments, roles of n-ary associations, ...›

subsection‹Semantic Properties of the Type Constructor›

lemma A[simp]:"Rep_Pairbase x  None  Rep_Pairbase x  null  (fst Rep_Pairbase x)  bot" 
by(insert Rep_Pairbase[of x],auto simp:null_option_def bot_option_def)

lemma A'[simp]:" x  bot   x  null  (fst Rep_Pairbase x)  bot" 
apply(insert Rep_Pairbase[of x], simp add: bot_Pairbase_def null_Pairbase_def)
apply(auto simp:null_option_def bot_option_def)
apply(erule contrapos_np[of "x = Abs_Pairbase None"])
apply(subst Rep_Pairbase_inject[symmetric], simp)
apply(subst Pairbase.Abs_Pairbase_inverse, simp_all,simp add: bot_option_def)
apply(erule contrapos_np[of "x = Abs_Pairbase None"])
apply(subst Rep_Pairbase_inject[symmetric], simp)
apply(subst Pairbase.Abs_Pairbase_inverse, simp_all,simp add: null_option_def bot_option_def)
done

lemma B[simp]:"Rep_Pairbase x  None  Rep_Pairbase x  null  (snd Rep_Pairbase x)  bot" 
by(insert Rep_Pairbase[of x],auto simp:null_option_def bot_option_def)

lemma B'[simp]:"x  bot  x  null  (snd Rep_Pairbase x)  bot" 
apply(insert Rep_Pairbase[of x], simp add: bot_Pairbase_def null_Pairbase_def)
apply(auto simp:null_option_def bot_option_def)
apply(erule contrapos_np[of "x = Abs_Pairbase None"])
apply(subst Rep_Pairbase_inject[symmetric], simp)
apply(subst Pairbase.Abs_Pairbase_inverse, simp_all,simp add: bot_option_def)
apply(erule contrapos_np[of "x = Abs_Pairbase None"])
apply(subst Rep_Pairbase_inject[symmetric], simp)
apply(subst Pairbase.Abs_Pairbase_inverse, simp_all,simp add: null_option_def bot_option_def)
done

subsection‹Fundamental Properties of Strict Equality \label{sec:pair-strict-eq}›

text‹After the part of foundational operations on sets, we detail here equality on sets.
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,::null)Pair,('𝔄,::null,::null)Pair]  ('𝔄)Boolean"
begin
  definition StrictRefEqPair :
    "((x::('𝔄,::null,::null)Pair)  y)  (λ τ. if (υ x) τ = true τ  (υ y) τ = true τ
                                                     then (x  y)τ
                                                     else invalid τ)"
end

text‹Property proof in terms of @{term "profile_binStrongEq_v_v"}
interpretation  StrictRefEqPair : profile_binStrongEq_v_v "λ x y. (x::('𝔄,::null,::null)Pair)  y" 
                by unfold_locales (auto simp:  StrictRefEqPair)
 
subsection‹Standard Operations Definitions›

text‹This part provides a collection of operators for the Pair type.›

subsubsection‹Definition: Pair Constructor›

definition OclPair::"('𝔄, ) val 
                     ('𝔄, ) val 
                     ('𝔄,::null,::null) Pair"  ("Pair{(_),(_)}")
where     "Pair{X,Y}  (λ τ. if (υ X) τ = true τ  (υ Y) τ = true τ
                              then Abs_Pairbase (X τ, Y τ)
                              else invalid τ)"

interpretation OclPair : profile_binv_v  
               OclPair "λ x y. Abs_Pairbase (x, y)"                             
               apply(unfold_locales, auto simp:  OclPair_def bot_Pairbase_def null_Pairbase_def)
               by(auto simp: Abs_Pairbase_inject null_option_def bot_option_def)
             

subsubsection‹Definition: First›

definition OclFirst::" ('𝔄,::null,::null) Pair  ('𝔄, ) val"  (" _ .First'(')")
where     "X .First()  (λ τ. if (δ X) τ = true τ
                              then fst Rep_Pairbase (X τ)
                              else invalid τ)"


interpretation OclFirst : profile_monod OclFirst "λx.  fst Rep_Pairbase (x)"
                          by unfold_locales (auto simp:  OclFirst_def)

subsubsection‹Definition: Second›
                              
definition OclSecond::" ('𝔄,::null,::null) Pair  ('𝔄, ) val"  ("_ .Second'(')")
where     "X .Second()  (λ τ. if (δ X) τ = true τ
                               then snd Rep_Pairbase (X τ)
                               else invalid τ)"

interpretation OclSecond : profile_monod OclSecond "λx.  snd Rep_Pairbase (x)"
                           by unfold_locales  (auto simp:  OclSecond_def)
                           
subsection‹Logical Properties›

lemma 1 : "τ  υ Y  τ  Pair{X,Y} .First()  X"
apply(case_tac "¬(τ  υ X)")
apply(erule foundation7'[THEN iffD2, THEN foundation15[THEN iffD2, 
                                       THEN StrongEq_L_subst2_rev]],simp_all add:foundation18')
apply(auto simp: OclValid_def valid_def defined_def StrongEq_def OclFirst_def OclPair_def
                true_def false_def invalid_def bot_fun_def null_fun_def)
apply(auto simp: Abs_Pairbase_inject null_option_def bot_option_def bot_Pairbase_def null_Pairbase_def)
by(simp add: Abs_Pairbase_inverse)

lemma 2 : "τ  υ X  τ  Pair{X,Y} .Second()  Y" 
apply(case_tac "¬(τ  υ Y)")
apply(erule foundation7'[THEN iffD2, THEN foundation15[THEN iffD2, 
                                       THEN StrongEq_L_subst2_rev]],simp_all add:foundation18')
apply(auto simp: OclValid_def valid_def defined_def StrongEq_def OclSecond_def OclPair_def
                true_def false_def invalid_def bot_fun_def null_fun_def)
apply(auto simp: Abs_Pairbase_inject null_option_def bot_option_def bot_Pairbase_def null_Pairbase_def)
by(simp add: Abs_Pairbase_inverse)

subsection‹Algebraic Execution Properties›

lemma proj1_exec [simp, code_unfold] : "Pair{X,Y} .First() = (if (υ Y) then X else invalid endif)"
apply(rule ext, rename_tac "τ", simp add: foundation22[symmetric])
apply(case_tac "¬(τ  υ Y)")
apply(erule foundation7'[THEN iffD2, 
                         THEN foundation15[THEN iffD2, 
                                           THEN StrongEq_L_subst2_rev]],simp_all)
apply(subgoal_tac "τ  υ Y")
apply(erule foundation13[THEN iffD2, THEN StrongEq_L_subst2_rev], simp_all)
by(erule 1)

lemma proj2_exec [simp, code_unfold] : "Pair{X,Y} .Second() = (if (υ X) then Y else invalid endif)"
apply(rule ext, rename_tac "τ", simp add: foundation22[symmetric])
apply(case_tac "¬(τ  υ X)")
apply(erule foundation7'[THEN iffD2, THEN foundation15[THEN iffD2, 
                                  THEN StrongEq_L_subst2_rev]],simp_all)
apply(subgoal_tac "τ  υ X")
apply(erule foundation13[THEN iffD2, THEN StrongEq_L_subst2_rev], simp_all)
by(erule 2)

(* < *)

subsection‹Test Statements›
(*
Assert   "(τ ⊨ (Pair{λ_. ⌊⌊x⌋⌋,λ_. ⌊⌊x⌋⌋} ≐ Pair{λ_. ⌊⌊x⌋⌋,λ_. ⌊⌊x⌋⌋}))"
Assert   "(τ ⊨ (Pair{λ_. ⌊x⌋,λ_. ⌊x⌋} ≐ Pair{λ_. ⌊x⌋,λ_. ⌊x⌋}))"
*)

instantiation Pairbase  :: (equal,equal)equal
begin
  definition "HOL.equal k l   (k::('a::equal,'b::equal)Pairbase) =  l"
  instance   by standard (rule equal_Pairbase_def)
end

lemma equal_Pairbase_code [code]:
  "HOL.equal k (l::('a::{equal,null},'b::{equal,null})Pairbase)  Rep_Pairbase k = Rep_Pairbase l"
  by (auto simp add: equal Pairbase.Rep_Pairbase_inject)

Assert "τ  invalid .First()  invalid "
Assert "τ  null .First()  invalid "
Assert "τ  null .Second()  invalid .Second() "
Assert "τ  Pair{invalid, true}  invalid "
Assert "τ  υ(Pair{null, true}.First())"
Assert "τ  (Pair{null, true}).First()  null "
Assert "τ  (Pair{null, Pair{true,invalid}}).First()  invalid "


(*
Assert   "¬ (τ ⊨ (Pair{𝟭,𝟮} ≐ Pair{𝟮,𝟭}))"
*)

(* > *)

end