9 type_synonym vname = string |
9 type_synonym vname = string |
10 type_synonym state = "vname \<Rightarrow> val" |
10 type_synonym state = "vname \<Rightarrow> val" |
11 |
11 |
12 datatype aexp = Ic int | Rc real | V vname | Plus aexp aexp |
12 datatype aexp = Ic int | Rc real | V vname | Plus aexp aexp |
13 |
13 |
|
14 text_raw{*\snip{tavalDef}{0}{2}{% *} |
14 inductive taval :: "aexp \<Rightarrow> state \<Rightarrow> val \<Rightarrow> bool" where |
15 inductive taval :: "aexp \<Rightarrow> state \<Rightarrow> val \<Rightarrow> bool" where |
15 "taval (Ic i) s (Iv i)" | |
16 "taval (Ic i) s (Iv i)" | |
16 "taval (Rc r) s (Rv r)" | |
17 "taval (Rc r) s (Rv r)" | |
17 "taval (V x) s (s x)" | |
18 "taval (V x) s (s x)" | |
18 "taval a1 s (Iv i1) \<Longrightarrow> taval a2 s (Iv i2) |
19 "taval a1 s (Iv i1) \<Longrightarrow> taval a2 s (Iv i2) |
19 \<Longrightarrow> taval (Plus a1 a2) s (Iv(i1+i2))" | |
20 \<Longrightarrow> taval (Plus a1 a2) s (Iv(i1+i2))" | |
20 "taval a1 s (Rv r1) \<Longrightarrow> taval a2 s (Rv r2) |
21 "taval a1 s (Rv r1) \<Longrightarrow> taval a2 s (Rv r2) |
21 \<Longrightarrow> taval (Plus a1 a2) s (Rv(r1+r2))" |
22 \<Longrightarrow> taval (Plus a1 a2) s (Rv(r1+r2))" |
|
23 text_raw{*}%endsnip*} |
22 |
24 |
23 inductive_cases [elim!]: |
25 inductive_cases [elim!]: |
24 "taval (Ic i) s v" "taval (Rc i) s v" |
26 "taval (Ic i) s v" "taval (Rc i) s v" |
25 "taval (V x) s v" |
27 "taval (V x) s v" |
26 "taval (Plus a1 a2) s v" |
28 "taval (Plus a1 a2) s v" |
27 |
29 |
28 subsection "Boolean Expressions" |
30 subsection "Boolean Expressions" |
29 |
31 |
30 datatype bexp = Bc bool | Not bexp | And bexp bexp | Less aexp aexp |
32 datatype bexp = Bc bool | Not bexp | And bexp bexp | Less aexp aexp |
31 |
33 |
|
34 text_raw{*\snip{tbvalDef}{0}{2}{% *} |
32 inductive tbval :: "bexp \<Rightarrow> state \<Rightarrow> bool \<Rightarrow> bool" where |
35 inductive tbval :: "bexp \<Rightarrow> state \<Rightarrow> bool \<Rightarrow> bool" where |
33 "tbval (Bc v) s v" | |
36 "tbval (Bc v) s v" | |
34 "tbval b s bv \<Longrightarrow> tbval (Not b) s (\<not> bv)" | |
37 "tbval b s bv \<Longrightarrow> tbval (Not b) s (\<not> bv)" | |
35 "tbval b1 s bv1 \<Longrightarrow> tbval b2 s bv2 \<Longrightarrow> tbval (And b1 b2) s (bv1 & bv2)" | |
38 "tbval b1 s bv1 \<Longrightarrow> tbval b2 s bv2 \<Longrightarrow> tbval (And b1 b2) s (bv1 & bv2)" | |
36 "taval a1 s (Iv i1) \<Longrightarrow> taval a2 s (Iv i2) \<Longrightarrow> tbval (Less a1 a2) s (i1 < i2)" | |
39 "taval a1 s (Iv i1) \<Longrightarrow> taval a2 s (Iv i2) \<Longrightarrow> tbval (Less a1 a2) s (i1 < i2)" | |
37 "taval a1 s (Rv r1) \<Longrightarrow> taval a2 s (Rv r2) \<Longrightarrow> tbval (Less a1 a2) s (r1 < r2)" |
40 "taval a1 s (Rv r1) \<Longrightarrow> taval a2 s (Rv r2) \<Longrightarrow> tbval (Less a1 a2) s (r1 < r2)" |
|
41 text_raw{*}%endsnip*} |
38 |
42 |
39 subsection "Syntax of Commands" |
43 subsection "Syntax of Commands" |
40 (* a copy of Com.thy - keep in sync! *) |
44 (* a copy of Com.thy - keep in sync! *) |
41 |
45 |
42 datatype |
46 datatype |
59 |
64 |
60 IfTrue: "tbval b s True \<Longrightarrow> (IF b THEN c1 ELSE c2,s) \<rightarrow> (c1,s)" | |
65 IfTrue: "tbval b s True \<Longrightarrow> (IF b THEN c1 ELSE c2,s) \<rightarrow> (c1,s)" | |
61 IfFalse: "tbval b s False \<Longrightarrow> (IF b THEN c1 ELSE c2,s) \<rightarrow> (c2,s)" | |
66 IfFalse: "tbval b s False \<Longrightarrow> (IF b THEN c1 ELSE c2,s) \<rightarrow> (c2,s)" | |
62 |
67 |
63 While: "(WHILE b DO c,s) \<rightarrow> (IF b THEN c; WHILE b DO c ELSE SKIP,s)" |
68 While: "(WHILE b DO c,s) \<rightarrow> (IF b THEN c; WHILE b DO c ELSE SKIP,s)" |
|
69 text_raw{*}%endsnip*} |
64 |
70 |
65 lemmas small_step_induct = small_step.induct[split_format(complete)] |
71 lemmas small_step_induct = small_step.induct[split_format(complete)] |
66 |
72 |
67 subsection "The Type System" |
73 subsection "The Type System" |
68 |
74 |
69 datatype ty = Ity | Rty |
75 datatype ty = Ity | Rty |
70 |
76 |
71 type_synonym tyenv = "vname \<Rightarrow> ty" |
77 type_synonym tyenv = "vname \<Rightarrow> ty" |
72 |
78 |
|
79 text_raw{*\snip{atypingDef}{0}{2}{% *} |
73 inductive atyping :: "tyenv \<Rightarrow> aexp \<Rightarrow> ty \<Rightarrow> bool" |
80 inductive atyping :: "tyenv \<Rightarrow> aexp \<Rightarrow> ty \<Rightarrow> bool" |
74 ("(1_/ \<turnstile>/ (_ :/ _))" [50,0,50] 50) |
81 ("(1_/ \<turnstile>/ (_ :/ _))" [50,0,50] 50) |
75 where |
82 where |
76 Ic_ty: "\<Gamma> \<turnstile> Ic i : Ity" | |
83 Ic_ty: "\<Gamma> \<turnstile> Ic i : Ity" | |
77 Rc_ty: "\<Gamma> \<turnstile> Rc r : Rty" | |
84 Rc_ty: "\<Gamma> \<turnstile> Rc r : Rty" | |
78 V_ty: "\<Gamma> \<turnstile> V x : \<Gamma> x" | |
85 V_ty: "\<Gamma> \<turnstile> V x : \<Gamma> x" | |
79 Plus_ty: "\<Gamma> \<turnstile> a1 : \<tau> \<Longrightarrow> \<Gamma> \<turnstile> a2 : \<tau> \<Longrightarrow> \<Gamma> \<turnstile> Plus a1 a2 : \<tau>" |
86 Plus_ty: "\<Gamma> \<turnstile> a1 : \<tau> \<Longrightarrow> \<Gamma> \<turnstile> a2 : \<tau> \<Longrightarrow> \<Gamma> \<turnstile> Plus a1 a2 : \<tau>" |
|
87 text_raw{*}%endsnip*} |
80 |
88 |
81 text{* Warning: the ``:'' notation leads to syntactic ambiguities, |
89 text{* Warning: the ``:'' notation leads to syntactic ambiguities, |
82 i.e. multiple parse trees, because ``:'' also stands for set membership. |
90 i.e. multiple parse trees, because ``:'' also stands for set membership. |
83 In most situations Isabelle's type system will reject all but one parse tree, |
91 In most situations Isabelle's type system will reject all but one parse tree, |
84 but will still inform you of the potential ambiguity. *} |
92 but will still inform you of the potential ambiguity. *} |
85 |
93 |
|
94 text_raw{*\snip{btypingDef}{0}{2}{% *} |
86 inductive btyping :: "tyenv \<Rightarrow> bexp \<Rightarrow> bool" (infix "\<turnstile>" 50) |
95 inductive btyping :: "tyenv \<Rightarrow> bexp \<Rightarrow> bool" (infix "\<turnstile>" 50) |
87 where |
96 where |
88 B_ty: "\<Gamma> \<turnstile> Bc v" | |
97 B_ty: "\<Gamma> \<turnstile> Bc v" | |
89 Not_ty: "\<Gamma> \<turnstile> b \<Longrightarrow> \<Gamma> \<turnstile> Not b" | |
98 Not_ty: "\<Gamma> \<turnstile> b \<Longrightarrow> \<Gamma> \<turnstile> Not b" | |
90 And_ty: "\<Gamma> \<turnstile> b1 \<Longrightarrow> \<Gamma> \<turnstile> b2 \<Longrightarrow> \<Gamma> \<turnstile> And b1 b2" | |
99 And_ty: "\<Gamma> \<turnstile> b1 \<Longrightarrow> \<Gamma> \<turnstile> b2 \<Longrightarrow> \<Gamma> \<turnstile> And b1 b2" | |
91 Less_ty: "\<Gamma> \<turnstile> a1 : \<tau> \<Longrightarrow> \<Gamma> \<turnstile> a2 : \<tau> \<Longrightarrow> \<Gamma> \<turnstile> Less a1 a2" |
100 Less_ty: "\<Gamma> \<turnstile> a1 : \<tau> \<Longrightarrow> \<Gamma> \<turnstile> a2 : \<tau> \<Longrightarrow> \<Gamma> \<turnstile> Less a1 a2" |
92 |
101 text_raw{*}%endsnip*} |
|
102 |
|
103 text_raw{*\snip{ctypingDef}{0}{2}{% *} |
93 inductive ctyping :: "tyenv \<Rightarrow> com \<Rightarrow> bool" (infix "\<turnstile>" 50) where |
104 inductive ctyping :: "tyenv \<Rightarrow> com \<Rightarrow> bool" (infix "\<turnstile>" 50) where |
94 Skip_ty: "\<Gamma> \<turnstile> SKIP" | |
105 Skip_ty: "\<Gamma> \<turnstile> SKIP" | |
95 Assign_ty: "\<Gamma> \<turnstile> a : \<Gamma>(x) \<Longrightarrow> \<Gamma> \<turnstile> x ::= a" | |
106 Assign_ty: "\<Gamma> \<turnstile> a : \<Gamma>(x) \<Longrightarrow> \<Gamma> \<turnstile> x ::= a" | |
96 Seq_ty: "\<Gamma> \<turnstile> c1 \<Longrightarrow> \<Gamma> \<turnstile> c2 \<Longrightarrow> \<Gamma> \<turnstile> c1;c2" | |
107 Seq_ty: "\<Gamma> \<turnstile> c1 \<Longrightarrow> \<Gamma> \<turnstile> c2 \<Longrightarrow> \<Gamma> \<turnstile> c1;c2" | |
97 If_ty: "\<Gamma> \<turnstile> b \<Longrightarrow> \<Gamma> \<turnstile> c1 \<Longrightarrow> \<Gamma> \<turnstile> c2 \<Longrightarrow> \<Gamma> \<turnstile> IF b THEN c1 ELSE c2" | |
108 If_ty: "\<Gamma> \<turnstile> b \<Longrightarrow> \<Gamma> \<turnstile> c1 \<Longrightarrow> \<Gamma> \<turnstile> c2 \<Longrightarrow> \<Gamma> \<turnstile> IF b THEN c1 ELSE c2" | |
98 While_ty: "\<Gamma> \<turnstile> b \<Longrightarrow> \<Gamma> \<turnstile> c \<Longrightarrow> \<Gamma> \<turnstile> WHILE b DO c" |
109 While_ty: "\<Gamma> \<turnstile> b \<Longrightarrow> \<Gamma> \<turnstile> c \<Longrightarrow> \<Gamma> \<turnstile> WHILE b DO c" |
|
110 text_raw{*}%endsnip*} |
99 |
111 |
100 inductive_cases [elim!]: |
112 inductive_cases [elim!]: |
101 "\<Gamma> \<turnstile> x ::= a" "\<Gamma> \<turnstile> c1;c2" |
113 "\<Gamma> \<turnstile> x ::= a" "\<Gamma> \<turnstile> c1;c2" |
102 "\<Gamma> \<turnstile> IF b THEN c1 ELSE c2" |
114 "\<Gamma> \<turnstile> IF b THEN c1 ELSE c2" |
103 "\<Gamma> \<turnstile> WHILE b DO c" |
115 "\<Gamma> \<turnstile> WHILE b DO c" |