12951
|
1 |
(* Title: HOL/MicroJava/J/JListExample.thy
|
12442
|
2 |
ID: $Id$
|
|
3 |
Author: Stefan Berghofer
|
|
4 |
*)
|
|
5 |
|
12911
|
6 |
header {* \isaheader{Example for generating executable code from Java semantics} *}
|
12442
|
7 |
|
12951
|
8 |
theory JListExample = Eval + SystemClasses:
|
12442
|
9 |
|
|
10 |
ML {* Syntax.ambiguity_level := 100000 *}
|
|
11 |
|
|
12 |
consts
|
|
13 |
list_name :: cname
|
|
14 |
append_name :: mname
|
|
15 |
val_nam :: vnam
|
|
16 |
next_nam :: vnam
|
|
17 |
l_nam :: vnam
|
|
18 |
l1_nam :: vnam
|
|
19 |
l2_nam :: vnam
|
|
20 |
l3_nam :: vnam
|
|
21 |
l4_nam :: vnam
|
|
22 |
|
|
23 |
constdefs
|
|
24 |
val_name :: vname
|
|
25 |
"val_name == VName val_nam"
|
|
26 |
|
|
27 |
next_name :: vname
|
|
28 |
"next_name == VName next_nam"
|
|
29 |
|
|
30 |
l_name :: vname
|
|
31 |
"l_name == VName l_nam"
|
|
32 |
|
|
33 |
l1_name :: vname
|
|
34 |
"l1_name == VName l1_nam"
|
|
35 |
|
|
36 |
l2_name :: vname
|
|
37 |
"l2_name == VName l2_nam"
|
|
38 |
|
|
39 |
l3_name :: vname
|
|
40 |
"l3_name == VName l3_nam"
|
|
41 |
|
|
42 |
l4_name :: vname
|
|
43 |
"l4_name == VName l4_nam"
|
|
44 |
|
|
45 |
list_class :: "java_mb class"
|
|
46 |
"list_class ==
|
|
47 |
(Object,
|
|
48 |
[(val_name, PrimT Integer), (next_name, RefT (ClassT list_name))],
|
|
49 |
[((append_name, [RefT (ClassT list_name)]), PrimT Void,
|
|
50 |
([l_name], [],
|
|
51 |
If(BinOp Eq ({list_name}(LAcc This)..next_name) (Lit Null))
|
|
52 |
Expr ({list_name}(LAcc This)..next_name:=LAcc l_name)
|
|
53 |
Else
|
|
54 |
Expr ({list_name}({list_name}(LAcc This)..next_name)..
|
|
55 |
append_name({[RefT (ClassT list_name)]}[LAcc l_name])),
|
|
56 |
Lit Unit))])"
|
|
57 |
|
|
58 |
example_prg :: "java_mb prog"
|
|
59 |
"example_prg == [ObjectC, (list_name, list_class)]"
|
|
60 |
|
|
61 |
types_code
|
|
62 |
cname ("string")
|
|
63 |
vnam ("string")
|
|
64 |
mname ("string")
|
13672
|
65 |
loc_ ("int")
|
12442
|
66 |
|
|
67 |
consts_code
|
13672
|
68 |
"new_Addr" ("new'_addr {* %x. case x of None => True | Some y => False *}/ {* None *} {* Loc *}")
|
12442
|
69 |
|
|
70 |
"arbitrary" ("(raise ERROR)")
|
|
71 |
"arbitrary" :: "val" ("{* Unit *}")
|
|
72 |
"arbitrary" :: "cname" ("\"\"")
|
|
73 |
|
|
74 |
"Object" ("\"Object\"")
|
|
75 |
"list_name" ("\"list\"")
|
|
76 |
"append_name" ("\"append\"")
|
|
77 |
"val_nam" ("\"val\"")
|
|
78 |
"next_nam" ("\"next\"")
|
|
79 |
"l_nam" ("\"l\"")
|
|
80 |
"l1_nam" ("\"l1\"")
|
|
81 |
"l2_nam" ("\"l2\"")
|
|
82 |
"l3_nam" ("\"l3\"")
|
|
83 |
"l4_nam" ("\"l4\"")
|
|
84 |
|
|
85 |
ML {*
|
13672
|
86 |
fun new_addr p none loc hp =
|
|
87 |
let fun nr i = if p (hp (loc i)) then (loc i, none) else nr (i+1);
|
12442
|
88 |
in nr 0 end;
|
|
89 |
*}
|
|
90 |
|
|
91 |
generate_code
|
13890
|
92 |
test = "example_prg\<turnstile>Norm (empty, empty)
|
12442
|
93 |
-(Expr (l1_name::=NewC list_name);;
|
|
94 |
Expr ({list_name}(LAcc l1_name)..val_name:=Lit (Intg 1));;
|
|
95 |
Expr (l2_name::=NewC list_name);;
|
|
96 |
Expr ({list_name}(LAcc l2_name)..val_name:=Lit (Intg 2));;
|
|
97 |
Expr (l3_name::=NewC list_name);;
|
|
98 |
Expr ({list_name}(LAcc l3_name)..val_name:=Lit (Intg 3));;
|
|
99 |
Expr (l4_name::=NewC list_name);;
|
|
100 |
Expr ({list_name}(LAcc l4_name)..val_name:=Lit (Intg 4));;
|
|
101 |
Expr ({list_name}(LAcc l1_name)..
|
|
102 |
append_name({[RefT (ClassT list_name)]}[LAcc l2_name]));;
|
|
103 |
Expr ({list_name}(LAcc l1_name)..
|
|
104 |
append_name({[RefT (ClassT list_name)]}[LAcc l3_name]));;
|
|
105 |
Expr ({list_name}(LAcc l1_name)..
|
12565
|
106 |
append_name({[RefT (ClassT list_name)]}[LAcc l4_name])))-> _"
|
12442
|
107 |
|
12911
|
108 |
section {* Big step execution *}
|
12442
|
109 |
|
|
110 |
ML {*
|
|
111 |
|
|
112 |
val Library.Some ((_, (heap, locs)), _) = Seq.pull test;
|
|
113 |
locs l1_name;
|
|
114 |
locs l2_name;
|
|
115 |
locs l3_name;
|
|
116 |
locs l4_name;
|
13672
|
117 |
snd (the (heap (Loc 0))) (val_name, "list");
|
|
118 |
snd (the (heap (Loc 0))) (next_name, "list");
|
|
119 |
snd (the (heap (Loc 1))) (val_name, "list");
|
|
120 |
snd (the (heap (Loc 1))) (next_name, "list");
|
|
121 |
snd (the (heap (Loc 2))) (val_name, "list");
|
|
122 |
snd (the (heap (Loc 2))) (next_name, "list");
|
|
123 |
snd (the (heap (Loc 3))) (val_name, "list");
|
|
124 |
snd (the (heap (Loc 3))) (next_name, "list");
|
12442
|
125 |
|
|
126 |
*}
|
|
127 |
|
|
128 |
end
|