(* Author: David Cock - David.Cock@nicta.com.au *)
section"A Shallow Embedding of pGCL in HOL"
theory Embedding imports Misc Inductionbegin
subsection‹Core Primitives and Syntax›
text_raw‹\label{s:syntax}›
text‹A pGCL program is embedded directly as its strict or liberal transformer. This is
with an additional parameter, specifying which semantics should be obeyed.›
text‹@{term Abort} either always fails, @{term "λP s. 0"}, or always succeeds,
{term "λP s. 1"}.› definition Abort :: "'s prog" where"Abort ≡ λab P s. if ab then 0 else 1"
text‹@{term Skip} does nothing at all.› definition Skip :: "'s prog" where"Skip ≡ λab P. P"
text‹@{term Apply} lifts a state transformer into the space of programs.› definitionApply :: "('s ==> 's) ==> 's prog" where"Apply f ≡ λab P s. P (f s)"
text‹@{term Seq} is sequential composition.› definition Seq :: "'s prog ==> 's prog ==> 's prog"
(infixl‹;;›59) where"Seq a b ≡ (λab. a ab o b ab)"
text‹@{term PC} is \emph{probabilistic} choice between programs.› definition PC :: "'s prog ==> ('s ==> real) ==> 's prog ==> 's prog"
(‹_ ⊕ _› [58,57,57] 57) where"PC a P b ≡ λab Q s. P s * a ab Q s + (1 - P s) * b ab Q s"
text‹@{term DC} is \emph{demonic} choice between programs.› definition DC :: "'s prog ==> 's prog ==> 's prog" (‹_ ⊓ _› [58,57] 57) where"DC a b ≡ λab Q s. min (a ab Q s) (b ab Q s)"
text‹@{term AC} is \emph{angelic} choice between programs.› definition AC :: "'s prog ==> 's prog ==> 's prog" (‹_ ⊔ _› [58,57] 57) where"AC a b ≡ λab Q s. max (a ab Q s) (b ab Q s)"
text‹@{term Embed} allows any expectation transformer to be treated
syntactically as a program, by ignoring the failure flag.› definition Embed :: "'s trans ==> 's prog" where"Embed t = (λab. t)"
text‹@{term Mu} is the recursive primitive, and is either then
least or greatest fixed point.› definition Mu :: "('s prog ==> 's prog) ==> 's prog" (binder‹μ›50) where"Mu(T) ≡ (λab. if ab then lfp_trans (λt. T (Embed t) ab) else gfp_trans (λt. T (Embed t) ab))"
text‹@{term repeat} expresses finite repetition› primrec
repeat :: "nat ==> 'a prog ==> 'a prog" where "repeat 0 p = Skip" | "repeat (Suc n) p = p ;; repeat n p"
text‹@{term SetDC} is demonic choice between a set of alternatives,
which may depend on the state.› definition SetDC :: "('a ==> 's prog) ==> ('s ==> 'a set) ==> 's prog" where"SetDC f S ≡ λab P s. Inf ((λa. f a ab P s) ` S s)"
text‹The above syntax allows us to write @{term "⊓x∈S. Apply f"}›
text‹@{term SetPC} is \emph{probabilistic} choice from a set. Note that this is only
for distributions of finite support.› definition
SetPC :: "('a ==> 's prog) ==> ('s ==> 'a ==> real) ==> 's prog" where "SetPC f p ≡ λab P s. ∑a∈supp (p s). p s a * f a ab P s"
text‹@{term Bind} allows us to name an expression in the current state, and re-use it later.› definition
Bind :: "('s ==> 'a) ==> ('a ==> 's prog) ==> 's prog" where "Bind g f ab ≡ λP s. let a = g s in f a ab P s"
text‹This gives us something like let syntax› syntax"_Bind" :: "pttrn => ('s => 'a) => 's prog => 's prog"
(‹_ is _ in _› [55,55,55]55)
syntax_consts "_Bind" == Bind translations"x is f in a" => "CONST Bind f (%x. a)"
definition flip :: "('a ==> 'b ==> 'c) ==> 'b ==> 'a ==> 'c" where [simp]: "flip f = (λb a. f a b)"
text‹The following pair of translations introduce let-style syntax
for @{term SetPC} and @{term SetDC}, respectively.› syntax"_PBind" :: "pttrn => ('s => real) => 's prog => 's prog"
(‹bind _ at _ in _› [55,55,55]55)
syntax_consts "_PBind" == SetPC translations"bind x at p in a" => "CONST SetPC (%x. a) (CONST flip (%x. p))"
syntax"_DBind" :: "pttrn => ('s => 'a set) ==> 's prog => 's prog"
(‹bind _ from _ in _› [55,55,55]55)
syntax_consts "_DBind" == SetDC translations"bind x from S in a" => "CONST SetDC (%x. a) S"
text‹The following syntax translations are for convenience when
using a record as the state type.› syntax "_assign" :: "ident => 'a => 's prog" (‹_ := _› [1000,900]900)
ML ‹
fun assign_tr _ [Const (name,_), arg] =
Const ("Embedding.Apply", dummyT) $
Abs ("s", dummyT,
Syntax.const (suffix Record.updateN name) $
Abs (Name.uu_, dummyT, arg $ Bound 1) $ Bound 0)
| assign_tr _ ts = raise TERM ("assign_tr", ts) › parse_translation‹[(@{syntax_const "_assign"}, assign_tr)]›
text‹If-Then-Else as a degenerate probabilistic choice.› abbreviation(input)
if_then_else :: "['s ==> bool, 's prog, 's prog] ==> 's prog"
(‹If _ Then _ Else _›58) where "If P Then a Else b == a <guillemotleft>P¬⊕ b"
text‹Syntax for loops› abbreviation
do_while :: "['s ==> bool, 's prog] ==> 's prog"
(‹do _ ⟶// (4 _) //od›) where "do_while P a ≡ μ x. If P Then a ;; x Else Skip"
subsection‹Unfolding rules for non-recursive primitives›
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.