14 "aval (N n) _ = n" | |
14 "aval (N n) _ = n" | |
15 "aval (V x) s = s x" | |
15 "aval (V x) s = s x" | |
16 "aval (Plus a1 a2) s = aval a1 s + aval a2 s" |
16 "aval (Plus a1 a2) s = aval a1 s + aval a2 s" |
17 |
17 |
18 |
18 |
19 value "aval (Plus (V ''x'') (N 5)) (%x. if x = ''x'' then 7 else 0)" |
19 value "aval (Plus (V ''x'') (N 5)) (\<lambda>x. if x = ''x'' then 7 else 0)" |
20 |
20 |
21 text {* The same state more concisely: *} |
21 text {* The same state more concisely: *} |
22 value "aval (Plus (V ''x'') (N 5)) ((%x. 0) (''x'':= 7))" |
22 value "aval (Plus (V ''x'') (N 5)) ((\<lambda>x. 0) (''x'':= 7))" |
23 |
23 |
24 text {* A little syntax magic to write larger states compactly: *} |
24 text {* A little syntax magic to write larger states compactly: *} |
25 |
25 |
26 definition |
26 definition null_state ("<>") where |
27 "null_heap \<equiv> \<lambda>x. 0" |
27 "null_state \<equiv> \<lambda>x. 0" |
28 syntax |
28 syntax |
29 "_State" :: "updbinds => 'a" ("<_>") |
29 "_State" :: "updbinds => 'a" ("<_>") |
30 translations |
30 translations |
31 "_State ms" => "_Update (CONST null_heap) ms" |
31 "_State ms" => "_Update <> ms" |
32 |
32 |
33 text {* |
33 text {* |
34 We can now write a series of updates to the function @{text "\<lambda>x. 0"} compactly: |
34 We can now write a series of updates to the function @{text "\<lambda>x. 0"} compactly: |
35 *} |
35 *} |
36 lemma "<a := Suc 0, b := 2> = (null_heap (a := Suc 0)) (b := 2)" |
36 lemma "<a := Suc 0, b := 2> = (<> (a := Suc 0)) (b := 2)" |
37 by (rule refl) |
37 by (rule refl) |
38 |
38 |
39 value "aval (Plus (V ''x'') (N 5)) <''x'' := 7>" |
39 value "aval (Plus (V ''x'') (N 5)) <''x'' := 7>" |
40 |
40 |
|
41 |
41 text {* Variables that are not mentioned in the state are 0 by default in |
42 text {* Variables that are not mentioned in the state are 0 by default in |
42 the @{term "<a := b::int>"} syntax: |
43 the @{term "<a := b::int>"} syntax: |
43 *} |
44 *} |
44 value "aval (Plus (V ''x'') (N 5)) <''y'' := 7>" |
45 value "aval (Plus (V ''x'') (N 5)) <''y'' := 7>" |
|
46 |
|
47 text{* Note that this @{text"<\<dots>>"} syntax works for any function space |
|
48 @{text"\<tau>\<^isub>1 \<Rightarrow> \<tau>\<^isub>2"} where @{text "\<tau>\<^isub>2"} has a @{text 0}. *} |
45 |
49 |
46 |
50 |
47 subsection "Optimization" |
51 subsection "Optimization" |
48 |
52 |
49 text{* Evaluate constant subsexpressions: *} |
53 text{* Evaluate constant subsexpressions: *} |
69 "plus (N i1) (N i2) = N(i1+i2)" | |
73 "plus (N i1) (N i2) = N(i1+i2)" | |
70 "plus (N i) a = (if i=0 then a else Plus (N i) a)" | |
74 "plus (N i) a = (if i=0 then a else Plus (N i) a)" | |
71 "plus a (N i) = (if i=0 then a else Plus a (N i))" | |
75 "plus a (N i) = (if i=0 then a else Plus a (N i))" | |
72 "plus a1 a2 = Plus a1 a2" |
76 "plus a1 a2 = Plus a1 a2" |
73 |
77 |
74 code_thms plus |
|
75 code_thms plus |
|
76 |
|
77 (* FIXME: dropping subsumed code eqns?? *) |
|
78 lemma aval_plus[simp]: |
78 lemma aval_plus[simp]: |
79 "aval (plus a1 a2) s = aval a1 s + aval a2 s" |
79 "aval (plus a1 a2) s = aval a1 s + aval a2 s" |
80 apply(induct a1 a2 rule: plus.induct) |
80 apply(induct a1 a2 rule: plus.induct) |
81 apply simp_all (* just for a change from auto *) |
81 apply simp_all (* just for a change from auto *) |
82 done |
82 done |
83 code_thms plus |
|
84 |
83 |
85 fun asimp :: "aexp \<Rightarrow> aexp" where |
84 fun asimp :: "aexp \<Rightarrow> aexp" where |
86 "asimp (N n) = N n" | |
85 "asimp (N n) = N n" | |
87 "asimp (V x) = V x" | |
86 "asimp (V x) = V x" | |
88 "asimp (Plus a1 a2) = plus (asimp a1) (asimp a2)" |
87 "asimp (Plus a1 a2) = plus (asimp a1) (asimp a2)" |