src/HOL/Hoare_Parallel/OG_Syntax.thy
author wenzelm
Wed Apr 10 21:20:35 2013 +0200 (2013-04-10)
changeset 51692 ecd34f863242
parent 42286 24075ad39ca2
child 52143 36ffe23b25f8
permissions -rw-r--r--
tuned pretty layout: avoid nested Pretty.string_of, which merely happens to work with Isabelle/jEdit since formatting is delegated to Scala side;
declare command "print_case_translations" where it is actually defined;
nipkow@15425
     1
theory OG_Syntax
nipkow@15425
     2
imports OG_Tactics Quote_Antiquote
nipkow@15425
     3
begin
prensani@13020
     4
prensani@13020
     5
text{* Syntax for commands and for assertions and boolean expressions in 
prensani@13020
     6
 commands @{text com} and annotated commands @{text ann_com}. *}
prensani@13020
     7
wenzelm@35107
     8
abbreviation Skip :: "'a com"  ("SKIP" 63)
wenzelm@35107
     9
  where "SKIP \<equiv> Basic id"
wenzelm@35107
    10
wenzelm@35107
    11
abbreviation AnnSkip :: "'a assn \<Rightarrow> 'a ann_com"  ("_//SKIP" [90] 63)
wenzelm@35107
    12
  where "r SKIP \<equiv> AnnBasic r id"
wenzelm@35107
    13
wenzelm@35107
    14
notation
wenzelm@35107
    15
  Seq  ("_,,/ _" [55, 56] 55) and
wenzelm@35107
    16
  AnnSeq  ("_;;/ _" [60,61] 60)
wenzelm@35107
    17
prensani@13020
    18
syntax
prensani@13020
    19
  "_Assign"      :: "idt \<Rightarrow> 'b \<Rightarrow> 'a com"    ("(\<acute>_ :=/ _)" [70, 65] 61)
prensani@13020
    20
  "_AnnAssign"   :: "'a assn \<Rightarrow> idt \<Rightarrow> 'b \<Rightarrow> 'a com"    ("(_ \<acute>_ :=/ _)" [90,70,65] 61)
prensani@13020
    21
prensani@13020
    22
translations
wenzelm@35107
    23
  "\<acute>x := a" \<rightharpoonup> "CONST Basic \<guillemotleft>\<acute>(_update_name x (\<lambda>_. a))\<guillemotright>"
wenzelm@35107
    24
  "r \<acute>x := a" \<rightharpoonup> "CONST AnnBasic r \<guillemotleft>\<acute>(_update_name x (\<lambda>_. a))\<guillemotright>"
prensani@13020
    25
prensani@13020
    26
syntax
prensani@13020
    27
  "_AnnCond1"    :: "'a assn \<Rightarrow> 'a bexp  \<Rightarrow> 'a ann_com  \<Rightarrow> 'a ann_com \<Rightarrow> 'a ann_com"
prensani@13020
    28
                    ("_ //IF _ /THEN _ /ELSE _ /FI"  [90,0,0,0] 61)
prensani@13020
    29
  "_AnnCond2"    :: "'a assn \<Rightarrow> 'a bexp  \<Rightarrow> 'a ann_com \<Rightarrow> 'a ann_com"
prensani@13020
    30
                    ("_ //IF _ /THEN _ /FI"  [90,0,0] 61)
prensani@13020
    31
  "_AnnWhile"    :: "'a assn \<Rightarrow> 'a bexp  \<Rightarrow> 'a assn \<Rightarrow> 'a ann_com \<Rightarrow> 'a ann_com" 
prensani@13020
    32
                    ("_ //WHILE _ /INV _ //DO _//OD"  [90,0,0,0] 61)
prensani@13020
    33
  "_AnnAwait"    :: "'a assn \<Rightarrow> 'a bexp  \<Rightarrow> 'a com \<Rightarrow> 'a ann_com"
prensani@13020
    34
                    ("_ //AWAIT _ /THEN /_ /END"  [90,0,0] 61)
prensani@13020
    35
  "_AnnAtom"     :: "'a assn  \<Rightarrow> 'a com \<Rightarrow> 'a ann_com"   ("_//\<langle>_\<rangle>" [90,0] 61)
prensani@13020
    36
  "_AnnWait"     :: "'a assn \<Rightarrow> 'a bexp \<Rightarrow> 'a ann_com"   ("_//WAIT _ END" [90,0] 61)
prensani@13020
    37
prensani@13020
    38
  "_Cond"        :: "'a bexp \<Rightarrow> 'a com \<Rightarrow> 'a com \<Rightarrow> 'a com" 
prensani@13020
    39
                                  ("(0IF _/ THEN _/ ELSE _/ FI)" [0, 0, 0] 61)
prensani@13020
    40
  "_Cond2"       :: "'a bexp \<Rightarrow> 'a com \<Rightarrow> 'a com"   ("IF _ THEN _ FI" [0,0] 56)
prensani@13020
    41
  "_While_inv"   :: "'a bexp \<Rightarrow> 'a assn \<Rightarrow> 'a com \<Rightarrow> 'a com"
prensani@13020
    42
                    ("(0WHILE _/ INV _ //DO _ /OD)"  [0, 0, 0] 61)
prensani@13020
    43
  "_While"       :: "'a bexp \<Rightarrow> 'a com \<Rightarrow> 'a com"
prensani@13020
    44
                    ("(0WHILE _ //DO _ /OD)"  [0, 0] 61)
prensani@13020
    45
prensani@13020
    46
translations
wenzelm@35107
    47
  "IF b THEN c1 ELSE c2 FI" \<rightharpoonup> "CONST Cond .{b}. c1 c2"
prensani@13020
    48
  "IF b THEN c FI" \<rightleftharpoons> "IF b THEN c ELSE SKIP FI"
wenzelm@35113
    49
  "WHILE b INV i DO c OD" \<rightharpoonup> "CONST While .{b}. i c"
haftmann@28524
    50
  "WHILE b DO c OD" \<rightleftharpoons> "WHILE b INV CONST undefined DO c OD"
prensani@13020
    51
wenzelm@35107
    52
  "r IF b THEN c1 ELSE c2 FI" \<rightharpoonup> "CONST AnnCond1 r .{b}. c1 c2"
wenzelm@35107
    53
  "r IF b THEN c FI" \<rightharpoonup> "CONST AnnCond2 r .{b}. c"
wenzelm@35107
    54
  "r WHILE b INV i DO c OD" \<rightharpoonup> "CONST AnnWhile r .{b}. i c"
wenzelm@35107
    55
  "r AWAIT b THEN c END" \<rightharpoonup> "CONST AnnAwait r .{b}. c"
wenzelm@35113
    56
  "r \<langle>c\<rangle>" \<rightleftharpoons> "r AWAIT CONST True THEN c END"
prensani@13020
    57
  "r WAIT b END" \<rightleftharpoons> "r AWAIT b THEN SKIP END"
prensani@13020
    58
 
wenzelm@41229
    59
nonterminal prgs
prensani@13020
    60
prensani@13020
    61
syntax
prensani@13020
    62
  "_PAR" :: "prgs \<Rightarrow> 'a"              ("COBEGIN//_//COEND" [57] 56)
prensani@13020
    63
  "_prg" :: "['a, 'a] \<Rightarrow> prgs"        ("_//_" [60, 90] 57)
prensani@13020
    64
  "_prgs" :: "['a, 'a, prgs] \<Rightarrow> prgs"  ("_//_//\<parallel>//_" [60,90,57] 57)
prensani@13020
    65
prensani@13020
    66
  "_prg_scheme" :: "['a, 'a, 'a, 'a, 'a] \<Rightarrow> prgs"  
prensani@13020
    67
                  ("SCHEME [_ \<le> _ < _] _// _" [0,0,0,60, 90] 57)
prensani@13020
    68
prensani@13020
    69
translations
wenzelm@35107
    70
  "_prg c q" \<rightleftharpoons> "[(CONST Some c, q)]"
wenzelm@35107
    71
  "_prgs c q ps" \<rightleftharpoons> "(CONST Some c, q) # ps"
wenzelm@35107
    72
  "_PAR ps" \<rightleftharpoons> "CONST Parallel ps"
prensani@13020
    73
wenzelm@35107
    74
  "_prg_scheme j i k c q" \<rightleftharpoons> "CONST map (\<lambda>i. (CONST Some c, q)) [j..<k]"
prensani@13020
    75
prensani@13020
    76
print_translation {*
prensani@13020
    77
  let
prensani@13020
    78
    fun quote_tr' f (t :: ts) =
wenzelm@42284
    79
          Term.list_comb (f $ Syntax_Trans.quote_tr' @{syntax_const "_antiquote"} t, ts)
prensani@13020
    80
      | quote_tr' _ _ = raise Match;
prensani@13020
    81
prensani@13020
    82
    fun annquote_tr' f (r :: t :: ts) =
wenzelm@42284
    83
          Term.list_comb (f $ r $ Syntax_Trans.quote_tr' @{syntax_const "_antiquote"} t, ts)
prensani@13020
    84
      | annquote_tr' _ _ = raise Match;
prensani@13020
    85
wenzelm@35113
    86
    val assert_tr' = quote_tr' (Syntax.const @{syntax_const "_Assert"});
prensani@13020
    87
wenzelm@35107
    88
    fun bexp_tr' name ((Const (@{const_syntax Collect}, _) $ t) :: ts) =
prensani@13020
    89
          quote_tr' (Syntax.const name) (t :: ts)
prensani@13020
    90
      | bexp_tr' _ _ = raise Match;
prensani@13020
    91
wenzelm@35107
    92
    fun annbexp_tr' name (r :: (Const (@{const_syntax Collect}, _) $ t) :: ts) =
prensani@13020
    93
          annquote_tr' (Syntax.const name) (r :: t :: ts)
prensani@13020
    94
      | annbexp_tr' _ _ = raise Match;
prensani@13020
    95
schirmer@25706
    96
    fun assign_tr' (Abs (x, _, f $ k $ Bound 0) :: ts) =
wenzelm@42284
    97
          quote_tr' (Syntax.const @{syntax_const "_Assign"} $ Syntax_Trans.update_name_tr' f)
wenzelm@42284
    98
            (Abs (x, dummyT, Syntax_Trans.const_abs_tr' k) :: ts)
prensani@13020
    99
      | assign_tr' _ = raise Match;
prensani@13020
   100
schirmer@25706
   101
    fun annassign_tr' (r :: Abs (x, _, f $ k $ Bound 0) :: ts) =
wenzelm@42284
   102
          quote_tr' (Syntax.const @{syntax_const "_AnnAssign"} $ r $ Syntax_Trans.update_name_tr' f)
wenzelm@42284
   103
            (Abs (x, dummyT, Syntax_Trans.const_abs_tr' k) :: ts)
prensani@13020
   104
      | annassign_tr' _ = raise Match;
prensani@13020
   105
wenzelm@35107
   106
    fun Parallel_PAR [(Const (@{const_syntax Cons}, _) $
wenzelm@35107
   107
            (Const (@{const_syntax Pair}, _) $ (Const (@{const_syntax Some},_) $ t1 ) $ t2) $
wenzelm@35113
   108
            Const (@{const_syntax Nil}, _))] = Syntax.const @{syntax_const "_prg"} $ t1 $ t2
wenzelm@35107
   109
      | Parallel_PAR [(Const (@{const_syntax Cons}, _) $
wenzelm@35107
   110
            (Const (@{const_syntax Pair}, _) $ (Const (@{const_syntax Some}, _) $ t1) $ t2) $ ts)] =
wenzelm@35113
   111
          Syntax.const @{syntax_const "_prgs"} $ t1 $ t2 $ Parallel_PAR [ts]
prensani@13020
   112
      | Parallel_PAR _ = raise Match;
prensani@13020
   113
wenzelm@35113
   114
    fun Parallel_tr' ts = Syntax.const @{syntax_const "_PAR"} $ Parallel_PAR ts;
prensani@13020
   115
  in
wenzelm@35107
   116
   [(@{const_syntax Collect}, assert_tr'),
wenzelm@35107
   117
    (@{const_syntax Basic}, assign_tr'),
wenzelm@42286
   118
    (@{const_syntax Cond}, bexp_tr' @{syntax_const "_Cond"}),
wenzelm@42286
   119
    (@{const_syntax While}, bexp_tr' @{syntax_const "_While_inv"}),
wenzelm@35107
   120
    (@{const_syntax AnnBasic}, annassign_tr'),
wenzelm@42286
   121
    (@{const_syntax AnnWhile}, annbexp_tr' @{syntax_const "_AnnWhile"}),
wenzelm@42286
   122
    (@{const_syntax AnnAwait}, annbexp_tr' @{syntax_const "_AnnAwait"}),
wenzelm@42286
   123
    (@{const_syntax AnnCond1}, annbexp_tr' @{syntax_const "_AnnCond1"}),
wenzelm@42286
   124
    (@{const_syntax AnnCond2}, annbexp_tr' @{syntax_const "_AnnCond2"})]
wenzelm@35107
   125
  end;
prensani@13020
   126
*}
prensani@13020
   127
prensani@13020
   128
end