theory utp_rel imports
utp_pred_laws
utp_healthy
utp_lift
utp_tactics begin
text‹ An alphabetised relation is simply a predicate whose state-space is a product type. In this
theory we construct the core operators of the relational calculus, and prove a libary of
associated theorems, based on Chapters 2 and 5 of the UTP book~cite‹"Hoare&98"›. ›
subsection‹ Relational Alphabets ›
text‹ We set up convenient syntax to refer to the input and output parts of the alphabet, as is
common in UTP. Since we are in a product space, these are simply the lenses @{term "fstL"} and
@{term "sndL"}. ›
text‹ We create type synonyms for conditions (which are simply predicates) -- i.e. relations
without dashed variables --, alphabetised relations where the input and output alphabet can
be different, and finally homogeneous relations. ›
text‹ We define a specialised version of the conditional where the condition can refer only to
undashed variables, as is usually the case in programs, but not universally in UTP models.
We implement this by lifting the condition predicate into the relational state-space with
construction @{term "⌈b⌉<"}. ›
definition lift_rcond (‹⌈_⌉\←›) where
[upred_defs]: "⌈b⌉\<leftarrow> = ⌈b⌉<"
text‹ Sequential composition is heterogeneous, and simply requires that the output alphabet
of the first matches then input alphabet of the second. We define it by lifting HOL's
built-in relational composition operator (@{term "(O)"}). Since this returns a set, the
definition states that the state binding $b$ is an element of this set. ›
lift_definition seqr::"('α, 'β) urel ==> ('β, 'γ) urel ==> ('α × 'γ) upred" is"λ P Q b. b ∈ ({p. P p} O {q. Q q})" .
adhoc_overloading
useq ⇌ seqr
text‹ We also set up a homogeneous sequential composition operator, and versions of @{term true}
and @{term false} that are explicitly typed by a homogeneous alphabet. ›
abbreviation seqh :: "'α hrel ==> 'α hrel ==> 'α hrel" (infixr‹;;h›61) where "seqh P Q ≡ (P ;; Q)"
abbreviation truer :: "'α hrel" (‹trueh›) where "truer ≡ true"
abbreviation falser :: "'α hrel" (‹falseh›) where "falser ≡ false"
text‹ We define the relational converse operator as an alphabet extrusion on the bijective
lens @{term swapL} that swaps the elements of the product state-space. ›
text‹ Assignment is defined using substitutions, where latter defines what each variable should
map to. This approach, which is originally due to Back~cite‹"Back1998"›, permits more general
assignment expressions. The definition of the operator identifies the after state binding, $b'$,
with the substitution function applied to the before state binding $b$. ›
text‹ Non-deterministic assignment, also known as ``choose'', assigns an arbitrarily chosen value
to the given variable ›
definition nd_assign :: "('a ==> 'α) ==> 'α hrel"where
[urel_defs]: "nd_assign x = (⊓ v ∙ assigns_r [x ↦s«v¬])"
text‹ We set up iterated sequential composition which iterates an indexed predicate over the
elements of a list. ›
definition seqr_iter :: "'a list ==> ('a ==> 'b hrel) ==> 'b hrel"where
[urel_defs]: "seqr_iter xs P = foldr (λ i Q. P(i) ;; Q) xs II"
text‹ A singleton assignment simply applies a singleton substitution function, and similarly
for a double assignment. ›
abbreviation assign_r :: "('t ==> 'α) ==> ('t, 'α) uexpr ==> 'α hrel" where"assign_r x v ≡⟨[x ↦s v]⟩a"
abbreviation assign_2_r :: "('t1 ==> 'α) ==> ('t2 ==> 'α) ==> ('t1, 'α) uexpr ==> ('t2, 'α) uexpr ==> 'α hrel" where"assign_2_r x y u v ≡ assigns_r [x ↦s u, y ↦s v]"
text‹ We also define the alphabetised skip operator that identifies all input and output variables
in the given alphabet lens. All other variables are unrestricted. We also set up syntax for it. ›
text‹ Assumptions ($c^{\top}$) and assertions ($c_{\bot}$) are encoded as conditionals. An assumption
behaves like skip if the condition is true, and otherwise behaves like @{term false} (miracle).
An assertion is the same, but yields @{term true}, which is an abort. They are the same as
tests, as in Kleene Algebra with Tests~cite‹"kozen1997kleene" and "Armstrong2015"›
(KAT), which embeds a Boolean algebra into a Kleene algebra to represent conditions. ›
definition rassume :: "'α upred ==> 'α hrel"where
[urel_defs]: "rassume c = II ◃ c ▹r false"
definition rassert :: "'α upred ==> 'α hrel"where
[urel_defs]: "rassert c = II ◃ c ▹r true"
text‹ We define two variants of while loops based on strongest and weakest fixed points. The former
is @{term false} for an infinite loop, and the latter is @{term true}. ›
definition while_top :: "'α cond ==> 'α hrel ==> 'α hrel"where
[urel_defs]: "while_top b P = (ν X ∙ (P ;; X) ◃ b ▹r II)"
definition while_bot :: "'α cond ==> 'α hrel ==> 'α hrel"where
[urel_defs]: "while_bot b P = (μ X ∙ (P ;; X) ◃ b ▹r II)"
text‹ While loops with invariant decoration (cf. cite‹"Armstrong2015"›) -- partial correctness. ›
definition while_inv :: "'α cond ==> 'α cond ==> 'α hrel ==> 'α hrel"where
[urel_defs]: "while_inv b p S = while_top b S"
text‹ While loops with invariant decoration -- total correctness. ›
definition while_inv_bot :: "'α cond ==> 'α cond ==> 'α hrel ==> 'α hrel"where
[urel_defs]: "while_inv_bot b p S = while_bot b S"
text‹ While loops with invariant and variant decorations -- total correctness. ›
definition while_vrt :: "'α cond ==> 'α cond ==> (nat, 'α) uexpr ==> 'α hrel ==> 'α hrel"where
[urel_defs]: "while_vrt b p v S = while_bot b S"
syntax_consts "_uassume" == rassume and "_uassert" == rassert and "_uwhile""_uwhile_top" == while_top and "_uwhile_bot" == while_bot and "_uwhile_inv" == while_inv and "_uwhile_inv_bot" == while_inv_bot and "_uwhile_vrt" == while_vrt
translations "_uassume b" == "CONST rassume b" "_uassert b" == "CONST rassert b" "_uwhile b P" == "CONST while_top b P" "_uwhile_top b P" == "CONST while_top b P" "_uwhile_bot b P" == "CONST while_bot b P" "_uwhile_inv b p S" == "CONST while_inv b p S" "_uwhile_inv_bot b p S" == "CONST while_inv_bot b p S" "_uwhile_vrt b p v S" == "CONST while_vrt b p v S"
text‹ We implement a poor man's version of alphabet restriction that hides a variable within
a relation. ›
text‹ Alphabet extension and restriction add additional variables by the given lens in both
their primed and unprimed versions. ›
definition rel_aext :: "'β hrel ==> ('β ==> 'α) ==> 'α hrel" where [upred_defs]: "rel_aext P a = P ⊕p (a ×L a)"
definition rel_ares :: "'α hrel ==> ('β ==> 'α) ==> 'β hrel" where [upred_defs]: "rel_ares P a = (P 🛇p (a × a))"
text‹ We next describe frames and antiframes with the help of lenses. A frame states that $P$
defines how variables in $a$ changed, and all those outside of $a$ remain the same. An
antiframe describes the converse: all variables outside $a$ are specified by $P$, and all those in
remain the same. For more information please see cite‹"Morgan90a"›.›
definition frame :: "('a ==> 'α) ==> 'α hrel ==> 'α hrel"where
[urel_defs]: "frame a P = (P ∧ $v🍋 =u $v⊕ $v🍋 on &a)"
definition antiframe :: "('a ==> 'α) ==> 'α hrel ==> 'α hrel"where
[urel_defs]: "antiframe a P = (P ∧ $v🍋 =u $v🍋⊕ $v on &a)"
text‹ Frame extension combines alphabet extension with the frame operator to both add additional
variables and then frame those. ›
definition rel_frext :: "('β ==> 'α) ==> 'β hrel ==> 'α hrel"where
[upred_defs]: "rel_frext a P = frame a (rel_aext P a)"
text‹ The nameset operator can be used to hide a portion of the after-state that lies outside
the lens $a$. It can be useful to partition a relation's variables in order to conjoin it
with another relation. ›
definition nameset :: "('a ==> 'α) ==> 'α hrel ==> 'α hrel"where
[urel_defs]: "nameset a P = (P 🛇v {$v,$a🍋})"
translations "_utp_if b P Q" => "P ◃ b ▹r Q" ";; x : l ∙ P"⇌"(CONST seqr_iter) l (λx. P)" "_mk_usubst σ (_svid_unit x) v"⇌"σ(&x ↦s v)" "_mk_usubst σ (_svid_list x xs) (_uexprs v vs)"⇌"(_mk_usubst (σ(&x ↦s v)) xs vs)" "_assignment xs vs" => "CONST uassigns (_mk_usubst (CONST id) xs vs)" "_assignment x v" <= "CONST uassigns (CONST subst_upd (CONST id) x v)" "_assignment x v" <= "_assignment (_spvar x) v" "_nd_assign x" => "CONST nd_assign (_mk_svid_list x)" "_nd_assign x" <= "CONST nd_assign x" "x,y := u,v" <= "CONST uassigns (CONST subst_upd (CONST subst_upd (CONST id) (CONST svar x) u) (CONST svar y) v)" "_skip_ra v"⇌"CONST skip_ra v" "_frame x P" => "CONST frame x P" "_frame (_salphaset (_salphamk x)) P" <= "CONST frame x P" "_antiframe x P" => "CONST antiframe x P" "_antiframe (_salphaset (_salphamk x)) P" <= "CONST antiframe x P" "_nameset x P" == "CONST nameset x P" "_rel_aext P a" == "CONST rel_aext P a" "_rel_ares P a" == "CONST rel_ares P a" "_rel_frext a P" == "CONST rel_frext a P"
text‹ The following code sets up pretty-printing for homogeneous relational expressions. We cannot
do this via the ``translations'' command as we only want the rule to apply when the input and output
alphabet types are the same. The code has to deconstruct a @{typ "('a, 'α) uexpr"} type, determine
that it is relational (product alphabet), and then checks if the types \emph{alpha} and \emph{beta}
are the same. If they are, the type is printed as a \emph{hexpr}. Otherwise, we have no match.
We then set up a regular translation for the \emph{hrel} type that uses this. ›
print_translation‹
tr' ctxt [ a
, Const (@{type_syntax "prod"},_) $ alpha $ beta ] =
if (alpha = beta)
then Syntax.const @{type_syntax "hexpr"} $ a $ alpha
else raise Match;
[(@{type_syntax "uexpr"},tr')]
text‹ We describe some properties of relations, including functional and injective relations. We
also provide operators for extracting the domain and range of a UTP relation. ›
definition ufunctional :: "('a, 'b) urel ==> bool" where [urel_defs]: "ufunctional R ⟷ II ⊑ R- ;; R"
definition uinj :: "('a, 'b) urel ==> bool" where [urel_defs]: "uinj R ⟷ II ⊑ R ;; R-"
definition Dom :: "'α hrel ==> 'α upred" where [upred_defs]: "Dom P = ⌊∃ $v🍋∙ P⌋<"
definition Ran :: "'α hrel ==> 'α upred" where [upred_defs]: "Ran P = ⌊∃ $v∙ P⌋>"
text‹ The following laws support substitution in heterogeneous relations for polymorphically
typed literal expressions. These cannot be supported more generically due to limitations
in HOL's type system. The laws are presented in a slightly strange way so as to be as
general as possible. ›
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.