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