author | wenzelm |
Sat, 16 Oct 2010 11:34:46 +0100 | |
changeset 39855 | d4299b415879 |
parent 35145 | f132a4fd8679 |
child 41229 | d797baa3d57c |
permissions | -rw-r--r-- |
15425 | 1 |
theory OG_Syntax |
2 |
imports OG_Tactics Quote_Antiquote |
|
3 |
begin |
|
13020 | 4 |
|
5 |
text{* Syntax for commands and for assertions and boolean expressions in |
|
6 |
commands @{text com} and annotated commands @{text ann_com}. *} |
|
7 |
||
35107 | 8 |
abbreviation Skip :: "'a com" ("SKIP" 63) |
9 |
where "SKIP \<equiv> Basic id" |
|
10 |
||
11 |
abbreviation AnnSkip :: "'a assn \<Rightarrow> 'a ann_com" ("_//SKIP" [90] 63) |
|
12 |
where "r SKIP \<equiv> AnnBasic r id" |
|
13 |
||
14 |
notation |
|
15 |
Seq ("_,,/ _" [55, 56] 55) and |
|
16 |
AnnSeq ("_;;/ _" [60,61] 60) |
|
17 |
||
13020 | 18 |
syntax |
19 |
"_Assign" :: "idt \<Rightarrow> 'b \<Rightarrow> 'a com" ("(\<acute>_ :=/ _)" [70, 65] 61) |
|
20 |
"_AnnAssign" :: "'a assn \<Rightarrow> idt \<Rightarrow> 'b \<Rightarrow> 'a com" ("(_ \<acute>_ :=/ _)" [90,70,65] 61) |
|
21 |
||
22 |
translations |
|
35107 | 23 |
"\<acute>x := a" \<rightharpoonup> "CONST Basic \<guillemotleft>\<acute>(_update_name x (\<lambda>_. a))\<guillemotright>" |
24 |
"r \<acute>x := a" \<rightharpoonup> "CONST AnnBasic r \<guillemotleft>\<acute>(_update_name x (\<lambda>_. a))\<guillemotright>" |
|
13020 | 25 |
|
26 |
syntax |
|
27 |
"_AnnCond1" :: "'a assn \<Rightarrow> 'a bexp \<Rightarrow> 'a ann_com \<Rightarrow> 'a ann_com \<Rightarrow> 'a ann_com" |
|
28 |
("_ //IF _ /THEN _ /ELSE _ /FI" [90,0,0,0] 61) |
|
29 |
"_AnnCond2" :: "'a assn \<Rightarrow> 'a bexp \<Rightarrow> 'a ann_com \<Rightarrow> 'a ann_com" |
|
30 |
("_ //IF _ /THEN _ /FI" [90,0,0] 61) |
|
31 |
"_AnnWhile" :: "'a assn \<Rightarrow> 'a bexp \<Rightarrow> 'a assn \<Rightarrow> 'a ann_com \<Rightarrow> 'a ann_com" |
|
32 |
("_ //WHILE _ /INV _ //DO _//OD" [90,0,0,0] 61) |
|
33 |
"_AnnAwait" :: "'a assn \<Rightarrow> 'a bexp \<Rightarrow> 'a com \<Rightarrow> 'a ann_com" |
|
34 |
("_ //AWAIT _ /THEN /_ /END" [90,0,0] 61) |
|
35 |
"_AnnAtom" :: "'a assn \<Rightarrow> 'a com \<Rightarrow> 'a ann_com" ("_//\<langle>_\<rangle>" [90,0] 61) |
|
36 |
"_AnnWait" :: "'a assn \<Rightarrow> 'a bexp \<Rightarrow> 'a ann_com" ("_//WAIT _ END" [90,0] 61) |
|
37 |
||
38 |
"_Cond" :: "'a bexp \<Rightarrow> 'a com \<Rightarrow> 'a com \<Rightarrow> 'a com" |
|
39 |
("(0IF _/ THEN _/ ELSE _/ FI)" [0, 0, 0] 61) |
|
40 |
"_Cond2" :: "'a bexp \<Rightarrow> 'a com \<Rightarrow> 'a com" ("IF _ THEN _ FI" [0,0] 56) |
|
41 |
"_While_inv" :: "'a bexp \<Rightarrow> 'a assn \<Rightarrow> 'a com \<Rightarrow> 'a com" |
|
42 |
("(0WHILE _/ INV _ //DO _ /OD)" [0, 0, 0] 61) |
|
43 |
"_While" :: "'a bexp \<Rightarrow> 'a com \<Rightarrow> 'a com" |
|
44 |
("(0WHILE _ //DO _ /OD)" [0, 0] 61) |
|
45 |
||
46 |
translations |
|
35107 | 47 |
"IF b THEN c1 ELSE c2 FI" \<rightharpoonup> "CONST Cond .{b}. c1 c2" |
13020 | 48 |
"IF b THEN c FI" \<rightleftharpoons> "IF b THEN c ELSE SKIP FI" |
35113 | 49 |
"WHILE b INV i DO c OD" \<rightharpoonup> "CONST While .{b}. i c" |
28524 | 50 |
"WHILE b DO c OD" \<rightleftharpoons> "WHILE b INV CONST undefined DO c OD" |
13020 | 51 |
|
35107 | 52 |
"r IF b THEN c1 ELSE c2 FI" \<rightharpoonup> "CONST AnnCond1 r .{b}. c1 c2" |
53 |
"r IF b THEN c FI" \<rightharpoonup> "CONST AnnCond2 r .{b}. c" |
|
54 |
"r WHILE b INV i DO c OD" \<rightharpoonup> "CONST AnnWhile r .{b}. i c" |
|
55 |
"r AWAIT b THEN c END" \<rightharpoonup> "CONST AnnAwait r .{b}. c" |
|
35113 | 56 |
"r \<langle>c\<rangle>" \<rightleftharpoons> "r AWAIT CONST True THEN c END" |
13020 | 57 |
"r WAIT b END" \<rightleftharpoons> "r AWAIT b THEN SKIP END" |
58 |
||
59 |
nonterminals |
|
60 |
prgs |
|
61 |
||
62 |
syntax |
|
63 |
"_PAR" :: "prgs \<Rightarrow> 'a" ("COBEGIN//_//COEND" [57] 56) |
|
64 |
"_prg" :: "['a, 'a] \<Rightarrow> prgs" ("_//_" [60, 90] 57) |
|
65 |
"_prgs" :: "['a, 'a, prgs] \<Rightarrow> prgs" ("_//_//\<parallel>//_" [60,90,57] 57) |
|
66 |
||
67 |
"_prg_scheme" :: "['a, 'a, 'a, 'a, 'a] \<Rightarrow> prgs" |
|
68 |
("SCHEME [_ \<le> _ < _] _// _" [0,0,0,60, 90] 57) |
|
69 |
||
70 |
translations |
|
35107 | 71 |
"_prg c q" \<rightleftharpoons> "[(CONST Some c, q)]" |
72 |
"_prgs c q ps" \<rightleftharpoons> "(CONST Some c, q) # ps" |
|
73 |
"_PAR ps" \<rightleftharpoons> "CONST Parallel ps" |
|
13020 | 74 |
|
35107 | 75 |
"_prg_scheme j i k c q" \<rightleftharpoons> "CONST map (\<lambda>i. (CONST Some c, q)) [j..<k]" |
13020 | 76 |
|
77 |
print_translation {* |
|
78 |
let |
|
79 |
fun quote_tr' f (t :: ts) = |
|
35113 | 80 |
Term.list_comb (f $ Syntax.quote_tr' @{syntax_const "_antiquote"} t, ts) |
13020 | 81 |
| quote_tr' _ _ = raise Match; |
82 |
||
83 |
fun annquote_tr' f (r :: t :: ts) = |
|
35113 | 84 |
Term.list_comb (f $ r $ Syntax.quote_tr' @{syntax_const "_antiquote"} t, ts) |
13020 | 85 |
| annquote_tr' _ _ = raise Match; |
86 |
||
35113 | 87 |
val assert_tr' = quote_tr' (Syntax.const @{syntax_const "_Assert"}); |
13020 | 88 |
|
35107 | 89 |
fun bexp_tr' name ((Const (@{const_syntax Collect}, _) $ t) :: ts) = |
13020 | 90 |
quote_tr' (Syntax.const name) (t :: ts) |
91 |
| bexp_tr' _ _ = raise Match; |
|
92 |
||
35107 | 93 |
fun annbexp_tr' name (r :: (Const (@{const_syntax Collect}, _) $ t) :: ts) = |
13020 | 94 |
annquote_tr' (Syntax.const name) (r :: t :: ts) |
95 |
| annbexp_tr' _ _ = raise Match; |
|
96 |
||
35107 | 97 |
fun K_tr' (Abs (_, _, t)) = |
98 |
if null (loose_bnos t) then t else raise Match |
|
99 |
| K_tr' (Abs (_, _, Abs (_, _, t) $ Bound 0)) = |
|
100 |
if null (loose_bnos t) then t else raise Match |
|
25706 | 101 |
| K_tr' _ = raise Match; |
102 |
||
103 |
fun assign_tr' (Abs (x, _, f $ k $ Bound 0) :: ts) = |
|
35145
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
35113
diff
changeset
|
104 |
quote_tr' (Syntax.const @{syntax_const "_Assign"} $ Syntax.update_name_tr' f) |
25706 | 105 |
(Abs (x, dummyT, K_tr' k) :: ts) |
13020 | 106 |
| assign_tr' _ = raise Match; |
107 |
||
25706 | 108 |
fun annassign_tr' (r :: Abs (x, _, f $ k $ Bound 0) :: ts) = |
35145
f132a4fd8679
moved generic update_name to Pure syntax -- not specific to HOL/record;
wenzelm
parents:
35113
diff
changeset
|
109 |
quote_tr' (Syntax.const @{syntax_const "_AnnAssign"} $ r $ Syntax.update_name_tr' f) |
25706 | 110 |
(Abs (x, dummyT, K_tr' k) :: ts) |
13020 | 111 |
| annassign_tr' _ = raise Match; |
112 |
||
35107 | 113 |
fun Parallel_PAR [(Const (@{const_syntax Cons}, _) $ |
114 |
(Const (@{const_syntax Pair}, _) $ (Const (@{const_syntax Some},_) $ t1 ) $ t2) $ |
|
35113 | 115 |
Const (@{const_syntax Nil}, _))] = Syntax.const @{syntax_const "_prg"} $ t1 $ t2 |
35107 | 116 |
| Parallel_PAR [(Const (@{const_syntax Cons}, _) $ |
117 |
(Const (@{const_syntax Pair}, _) $ (Const (@{const_syntax Some}, _) $ t1) $ t2) $ ts)] = |
|
35113 | 118 |
Syntax.const @{syntax_const "_prgs"} $ t1 $ t2 $ Parallel_PAR [ts] |
13020 | 119 |
| Parallel_PAR _ = raise Match; |
120 |
||
35113 | 121 |
fun Parallel_tr' ts = Syntax.const @{syntax_const "_PAR"} $ Parallel_PAR ts; |
13020 | 122 |
in |
35107 | 123 |
[(@{const_syntax Collect}, assert_tr'), |
124 |
(@{const_syntax Basic}, assign_tr'), |
|
125 |
(@{const_syntax Cond}, bexp_tr' "_Cond"), |
|
126 |
(@{const_syntax While}, bexp_tr' "_While_inv"), |
|
127 |
(@{const_syntax AnnBasic}, annassign_tr'), |
|
128 |
(@{const_syntax AnnWhile}, annbexp_tr' "_AnnWhile"), |
|
129 |
(@{const_syntax AnnAwait}, annbexp_tr' "_AnnAwait"), |
|
130 |
(@{const_syntax AnnCond1}, annbexp_tr' "_AnnCond1"), |
|
131 |
(@{const_syntax AnnCond2}, annbexp_tr' "_AnnCond2")] |
|
132 |
end; |
|
13020 | 133 |
*} |
134 |
||
135 |
end |