src/HOL/HoareParallel/OG_Syntax.thy
author haftmann
Fri Apr 20 17:58:25 2007 +0200 (2007-04-20)
changeset 22759 e4a3f49eb924
parent 22741 4bd02e03305c
child 25706 45d090186bbe
permissions -rw-r--r--
reverted to classical syntax for K_record
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
prensani@13020
     8
syntax
prensani@13020
     9
  "_Assign"      :: "idt \<Rightarrow> 'b \<Rightarrow> 'a com"    ("(\<acute>_ :=/ _)" [70, 65] 61)
prensani@13020
    10
  "_AnnAssign"   :: "'a assn \<Rightarrow> idt \<Rightarrow> 'b \<Rightarrow> 'a com"    ("(_ \<acute>_ :=/ _)" [90,70,65] 61)
prensani@13020
    11
prensani@13020
    12
translations
haftmann@22759
    13
  "\<acute>\<spacespace>x := a" \<rightharpoonup> "Basic \<guillemotleft>\<acute>\<spacespace>(_update_name x (K_record a))\<guillemotright>"
haftmann@22759
    14
  "r \<acute>\<spacespace>x := a" \<rightharpoonup> "AnnBasic r \<guillemotleft>\<acute>\<spacespace>(_update_name x (K_record a))\<guillemotright>"
prensani@13020
    15
prensani@13020
    16
syntax
prensani@13020
    17
  "_AnnSkip"     :: "'a assn \<Rightarrow> 'a ann_com"              ("_//SKIP" [90] 63)
prensani@13020
    18
  "_AnnSeq"      :: "'a ann_com \<Rightarrow> 'a ann_com \<Rightarrow> 'a ann_com"  ("_;;/ _" [60,61] 60)
prensani@13020
    19
  
prensani@13020
    20
  "_AnnCond1"    :: "'a assn \<Rightarrow> 'a bexp  \<Rightarrow> 'a ann_com  \<Rightarrow> 'a ann_com \<Rightarrow> 'a ann_com"
prensani@13020
    21
                    ("_ //IF _ /THEN _ /ELSE _ /FI"  [90,0,0,0] 61)
prensani@13020
    22
  "_AnnCond2"    :: "'a assn \<Rightarrow> 'a bexp  \<Rightarrow> 'a ann_com \<Rightarrow> 'a ann_com"
prensani@13020
    23
                    ("_ //IF _ /THEN _ /FI"  [90,0,0] 61)
prensani@13020
    24
  "_AnnWhile"    :: "'a assn \<Rightarrow> 'a bexp  \<Rightarrow> 'a assn \<Rightarrow> 'a ann_com \<Rightarrow> 'a ann_com" 
prensani@13020
    25
                    ("_ //WHILE _ /INV _ //DO _//OD"  [90,0,0,0] 61)
prensani@13020
    26
  "_AnnAwait"    :: "'a assn \<Rightarrow> 'a bexp  \<Rightarrow> 'a com \<Rightarrow> 'a ann_com"
prensani@13020
    27
                    ("_ //AWAIT _ /THEN /_ /END"  [90,0,0] 61)
prensani@13020
    28
  "_AnnAtom"     :: "'a assn  \<Rightarrow> 'a com \<Rightarrow> 'a ann_com"   ("_//\<langle>_\<rangle>" [90,0] 61)
prensani@13020
    29
  "_AnnWait"     :: "'a assn \<Rightarrow> 'a bexp \<Rightarrow> 'a ann_com"   ("_//WAIT _ END" [90,0] 61)
prensani@13020
    30
prensani@13020
    31
  "_Skip"        :: "'a com"                 ("SKIP" 63)
prensani@13020
    32
  "_Seq"         :: "'a com \<Rightarrow> 'a com \<Rightarrow> 'a com" ("_,,/ _" [55, 56] 55)
prensani@13020
    33
  "_Cond"        :: "'a bexp \<Rightarrow> 'a com \<Rightarrow> 'a com \<Rightarrow> 'a com" 
prensani@13020
    34
                                  ("(0IF _/ THEN _/ ELSE _/ FI)" [0, 0, 0] 61)
prensani@13020
    35
  "_Cond2"       :: "'a bexp \<Rightarrow> 'a com \<Rightarrow> 'a com"   ("IF _ THEN _ FI" [0,0] 56)
prensani@13020
    36
  "_While_inv"   :: "'a bexp \<Rightarrow> 'a assn \<Rightarrow> 'a com \<Rightarrow> 'a com"
prensani@13020
    37
                    ("(0WHILE _/ INV _ //DO _ /OD)"  [0, 0, 0] 61)
prensani@13020
    38
  "_While"       :: "'a bexp \<Rightarrow> 'a com \<Rightarrow> 'a com"
prensani@13020
    39
                    ("(0WHILE _ //DO _ /OD)"  [0, 0] 61)
prensani@13020
    40
prensani@13020
    41
translations
prensani@13020
    42
  "SKIP" \<rightleftharpoons> "Basic id"
prensani@13020
    43
  "c_1,, c_2" \<rightleftharpoons> "Seq c_1 c_2"
prensani@13020
    44
prensani@13020
    45
  "IF b THEN c1 ELSE c2 FI" \<rightharpoonup> "Cond .{b}. c1 c2"
prensani@13020
    46
  "IF b THEN c FI" \<rightleftharpoons> "IF b THEN c ELSE SKIP FI"
prensani@13020
    47
  "WHILE b INV i DO c OD" \<rightharpoonup> "While .{b}. i c"
prensani@13020
    48
  "WHILE b DO c OD" \<rightleftharpoons> "WHILE b INV arbitrary DO c OD"
prensani@13020
    49
prensani@13020
    50
  "r SKIP" \<rightleftharpoons> "AnnBasic r id"
prensani@13020
    51
  "c_1;; c_2" \<rightleftharpoons> "AnnSeq c_1 c_2" 
prensani@13020
    52
  "r IF b THEN c1 ELSE c2 FI" \<rightharpoonup> "AnnCond1 r .{b}. c1 c2"
prensani@13020
    53
  "r IF b THEN c FI" \<rightharpoonup> "AnnCond2 r .{b}. c"
prensani@13020
    54
  "r WHILE b INV i DO c OD" \<rightharpoonup> "AnnWhile r .{b}. i c"
prensani@13020
    55
  "r AWAIT b THEN c END" \<rightharpoonup> "AnnAwait r .{b}. c"
prensani@13020
    56
  "r \<langle>c\<rangle>" \<rightleftharpoons> "r AWAIT True THEN c END"
prensani@13020
    57
  "r WAIT b END" \<rightleftharpoons> "r AWAIT b THEN SKIP END"
prensani@13020
    58
 
prensani@13020
    59
nonterminals
prensani@13020
    60
  prgs
prensani@13020
    61
prensani@13020
    62
syntax
prensani@13020
    63
  "_PAR" :: "prgs \<Rightarrow> 'a"              ("COBEGIN//_//COEND" [57] 56)
prensani@13020
    64
  "_prg" :: "['a, 'a] \<Rightarrow> prgs"        ("_//_" [60, 90] 57)
prensani@13020
    65
  "_prgs" :: "['a, 'a, prgs] \<Rightarrow> prgs"  ("_//_//\<parallel>//_" [60,90,57] 57)
prensani@13020
    66
prensani@13020
    67
  "_prg_scheme" :: "['a, 'a, 'a, 'a, 'a] \<Rightarrow> prgs"  
prensani@13020
    68
                  ("SCHEME [_ \<le> _ < _] _// _" [0,0,0,60, 90] 57)
prensani@13020
    69
prensani@13020
    70
translations
prensani@13020
    71
  "_prg c q" \<rightleftharpoons> "[(Some c, q)]"
prensani@13020
    72
  "_prgs c q ps" \<rightleftharpoons> "(Some c, q) # ps"
prensani@13020
    73
  "_PAR ps" \<rightleftharpoons> "Parallel ps"
prensani@13020
    74
nipkow@15425
    75
  "_prg_scheme j i k c q" \<rightleftharpoons> "map (\<lambda>i. (Some c, q)) [j..<k]"
prensani@13020
    76
prensani@13020
    77
print_translation {*
prensani@13020
    78
  let
prensani@13020
    79
    fun quote_tr' f (t :: ts) =
prensani@13020
    80
          Term.list_comb (f $ Syntax.quote_tr' "_antiquote" t, ts)
prensani@13020
    81
      | quote_tr' _ _ = raise Match;
prensani@13020
    82
prensani@13020
    83
    fun annquote_tr' f (r :: t :: ts) =
prensani@13020
    84
          Term.list_comb (f $ r $ Syntax.quote_tr' "_antiquote" t, ts)
prensani@13020
    85
      | annquote_tr' _ _ = raise Match;
prensani@13020
    86
prensani@13020
    87
    val assert_tr' = quote_tr' (Syntax.const "_Assert");
prensani@13020
    88
prensani@13020
    89
    fun bexp_tr' name ((Const ("Collect", _) $ t) :: ts) =
prensani@13020
    90
          quote_tr' (Syntax.const name) (t :: ts)
prensani@13020
    91
      | bexp_tr' _ _ = raise Match;
prensani@13020
    92
prensani@13020
    93
    fun annbexp_tr' name (r :: (Const ("Collect", _) $ t) :: ts) =
prensani@13020
    94
          annquote_tr' (Syntax.const name) (r :: t :: ts)
prensani@13020
    95
      | annbexp_tr' _ _ = raise Match;
prensani@13020
    96
prensani@13020
    97
    fun upd_tr' (x_upd, T) =
prensani@13020
    98
      (case try (unsuffix RecordPackage.updateN) x_upd of
skalberg@15531
    99
        SOME x => (x, if T = dummyT then T else Term.domain_type T)
skalberg@15531
   100
      | NONE => raise Match);
prensani@13020
   101
prensani@13020
   102
    fun update_name_tr' (Free x) = Free (upd_tr' x)
prensani@13020
   103
      | update_name_tr' ((c as Const ("_free", _)) $ Free x) =
prensani@13020
   104
          c $ Free (upd_tr' x)
prensani@13020
   105
      | update_name_tr' (Const x) = Const (upd_tr' x)
prensani@13020
   106
      | update_name_tr' _ = raise Match;
prensani@13020
   107
haftmann@22741
   108
    fun assign_tr' (Abs (x, _, f $ (Const (@{const_syntax "K_record"},_)$t) $ Bound 0) :: ts) =
prensani@13020
   109
          quote_tr' (Syntax.const "_Assign" $ update_name_tr' f)
prensani@13020
   110
            (Abs (x, dummyT, t) :: ts)
prensani@13020
   111
      | assign_tr' _ = raise Match;
prensani@13020
   112
haftmann@22741
   113
    fun annassign_tr' (r :: Abs (x, _, f $ (Const (@{const_syntax "K_record"},_)$t) $ Bound 0) :: ts) =
prensani@13020
   114
          quote_tr' (Syntax.const "_AnnAssign" $ r $ update_name_tr' f)
prensani@13020
   115
            (Abs (x, dummyT, t) :: ts)
prensani@13020
   116
      | annassign_tr' _ = raise Match;
prensani@13020
   117
prensani@13020
   118
    fun Parallel_PAR [(Const ("Cons",_) $ (Const ("Pair",_) $ (Const ("Some",_) $ t1 ) $ t2) $ Const ("Nil",_))] = 
prensani@13020
   119
                   (Syntax.const "_prg" $ t1 $ t2)
prensani@13020
   120
      | Parallel_PAR [(Const ("Cons",_) $ (Const ("Pair",_) $ (Const ("Some",_) $ t1) $ t2) $ ts)] =
prensani@13020
   121
                     (Syntax.const "_prgs" $ t1 $ t2 $ Parallel_PAR [ts])
prensani@13020
   122
      | Parallel_PAR _ = raise Match;
prensani@13020
   123
prensani@13020
   124
fun Parallel_tr' ts = Syntax.const "_PAR" $ Parallel_PAR ts;
prensani@13020
   125
  in
prensani@13020
   126
    [("Collect", assert_tr'), ("Basic", assign_tr'), 
prensani@13020
   127
      ("Cond", bexp_tr' "_Cond"), ("While", bexp_tr' "_While_inv"),
prensani@13020
   128
      ("AnnBasic", annassign_tr'), 
prensani@13020
   129
      ("AnnWhile", annbexp_tr' "_AnnWhile"), ("AnnAwait", annbexp_tr' "_AnnAwait"),
prensani@13020
   130
      ("AnnCond1", annbexp_tr' "_AnnCond1"), ("AnnCond2", annbexp_tr' "_AnnCond2")]
prensani@13020
   131
prensani@13020
   132
  end
prensani@13020
   133
prensani@13020
   134
*}
prensani@13020
   135
prensani@13020
   136
end