src/HOL/IMP/AExp.thy
author wenzelm
Tue, 13 Aug 2013 16:25:47 +0200
changeset 53015 a1119cf551e8
parent 52460 92ae850a9bfd
child 54252 a4a00347e59f
permissions -rw-r--r--
standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
     1
header "Arithmetic and Boolean Expressions"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
     2
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
     3
theory AExp imports Main begin
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
     4
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
     5
subsection "Arithmetic Expressions"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
     6
45212
e87feee00a4c renamed name -> vname
nipkow
parents: 45015
diff changeset
     7
type_synonym vname = string
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
     8
type_synonym val = int
45212
e87feee00a4c renamed name -> vname
nipkow
parents: 45015
diff changeset
     9
type_synonym state = "vname \<Rightarrow> val"
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    10
49191
3601bf546775 tuned latex
nipkow
parents: 45255
diff changeset
    11
text_raw{*\snip{AExpaexpdef}{2}{1}{% *}
45212
e87feee00a4c renamed name -> vname
nipkow
parents: 45015
diff changeset
    12
datatype aexp = N int | V vname | Plus aexp aexp
49191
3601bf546775 tuned latex
nipkow
parents: 45255
diff changeset
    13
text_raw{*}%endsnip*}
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    14
49191
3601bf546775 tuned latex
nipkow
parents: 45255
diff changeset
    15
text_raw{*\snip{AExpavaldef}{1}{2}{% *}
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    16
fun aval :: "aexp \<Rightarrow> state \<Rightarrow> val" where
45216
nipkow
parents: 45212
diff changeset
    17
"aval (N n) s = n" |
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    18
"aval (V x) s = s x" |
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52460
diff changeset
    19
"aval (Plus a\<^sub>1 a\<^sub>2) s = aval a\<^sub>1 s + aval a\<^sub>2 s"
49191
3601bf546775 tuned latex
nipkow
parents: 45255
diff changeset
    20
text_raw{*}%endsnip*}
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    21
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    22
44923
b80108b346a9 cleand up AbsInt fixpoint iteration; tuned syntax
nipkow
parents: 44036
diff changeset
    23
value "aval (Plus (V ''x'') (N 5)) (\<lambda>x. if x = ''x'' then 7 else 0)"
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    24
43158
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    25
text {* The same state more concisely: *}
44923
b80108b346a9 cleand up AbsInt fixpoint iteration; tuned syntax
nipkow
parents: 44036
diff changeset
    26
value "aval (Plus (V ''x'') (N 5)) ((\<lambda>x. 0) (''x'':= 7))"
43158
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    27
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    28
text {* A little syntax magic to write larger states compactly: *}
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    29
44923
b80108b346a9 cleand up AbsInt fixpoint iteration; tuned syntax
nipkow
parents: 44036
diff changeset
    30
definition null_state ("<>") where
b80108b346a9 cleand up AbsInt fixpoint iteration; tuned syntax
nipkow
parents: 44036
diff changeset
    31
  "null_state \<equiv> \<lambda>x. 0"
44036
d03f9f28d01d new state syntax with less conflicts
kleing
parents: 43250
diff changeset
    32
syntax 
d03f9f28d01d new state syntax with less conflicts
kleing
parents: 43250
diff changeset
    33
  "_State" :: "updbinds => 'a" ("<_>")
43158
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    34
translations
51040
faf7f0d4f9eb tuned state display
nipkow
parents: 49191
diff changeset
    35
  "_State ms" == "_Update <> ms"
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    36
49191
3601bf546775 tuned latex
nipkow
parents: 45255
diff changeset
    37
text {* \noindent
43158
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    38
  We can now write a series of updates to the function @{text "\<lambda>x. 0"} compactly:
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    39
*}
44923
b80108b346a9 cleand up AbsInt fixpoint iteration; tuned syntax
nipkow
parents: 44036
diff changeset
    40
lemma "<a := Suc 0, b := 2> = (<> (a := Suc 0)) (b := 2)"
43158
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    41
  by (rule refl)
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    42
44036
d03f9f28d01d new state syntax with less conflicts
kleing
parents: 43250
diff changeset
    43
value "aval (Plus (V ''x'') (N 5)) <''x'' := 7>"
43158
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    44
44923
b80108b346a9 cleand up AbsInt fixpoint iteration; tuned syntax
nipkow
parents: 44036
diff changeset
    45
49191
3601bf546775 tuned latex
nipkow
parents: 45255
diff changeset
    46
text {* In  the @{term[source] "<a := b>"} syntax, variables that are not mentioned are 0 by default:
44923
b80108b346a9 cleand up AbsInt fixpoint iteration; tuned syntax
nipkow
parents: 44036
diff changeset
    47
*}
44036
d03f9f28d01d new state syntax with less conflicts
kleing
parents: 43250
diff changeset
    48
value "aval (Plus (V ''x'') (N 5)) <''y'' := 7>"
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    49
44923
b80108b346a9 cleand up AbsInt fixpoint iteration; tuned syntax
nipkow
parents: 44036
diff changeset
    50
text{* Note that this @{text"<\<dots>>"} syntax works for any function space
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52460
diff changeset
    51
@{text"\<tau>\<^sub>1 \<Rightarrow> \<tau>\<^sub>2"} where @{text "\<tau>\<^sub>2"} has a @{text 0}. *}
44923
b80108b346a9 cleand up AbsInt fixpoint iteration; tuned syntax
nipkow
parents: 44036
diff changeset
    52
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    53
45255
nipkow
parents: 45246
diff changeset
    54
subsection "Constant Folding"
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    55
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    56
text{* Evaluate constant subsexpressions: *}
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    57
49191
3601bf546775 tuned latex
nipkow
parents: 45255
diff changeset
    58
text_raw{*\snip{AExpasimpconstdef}{0}{2}{% *}
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    59
fun asimp_const :: "aexp \<Rightarrow> aexp" where
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    60
"asimp_const (N n) = N n" |
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    61
"asimp_const (V x) = V x" |
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52460
diff changeset
    62
"asimp_const (Plus a\<^sub>1 a\<^sub>2) =
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52460
diff changeset
    63
  (case (asimp_const a\<^sub>1, asimp_const a\<^sub>2) of
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52460
diff changeset
    64
    (N n\<^sub>1, N n\<^sub>2) \<Rightarrow> N(n\<^sub>1+n\<^sub>2) |
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52460
diff changeset
    65
    (b\<^sub>1,b\<^sub>2) \<Rightarrow> Plus b\<^sub>1 b\<^sub>2)"
49191
3601bf546775 tuned latex
nipkow
parents: 45255
diff changeset
    66
text_raw{*}%endsnip*}
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    67
45238
nipkow
parents: 45216
diff changeset
    68
theorem aval_asimp_const:
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    69
  "aval (asimp_const a) s = aval a s"
45015
fdac1e9880eb Updated IMP to use new induction method
nipkow
parents: 44923
diff changeset
    70
apply(induction a)
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    71
apply (auto split: aexp.split)
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    72
done
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    73
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    74
text{* Now we also eliminate all occurrences 0 in additions. The standard
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    75
method: optimized versions of the constructors: *}
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    76
49191
3601bf546775 tuned latex
nipkow
parents: 45255
diff changeset
    77
text_raw{*\snip{AExpplusdef}{0}{2}{% *}
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    78
fun plus :: "aexp \<Rightarrow> aexp \<Rightarrow> aexp" where
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52460
diff changeset
    79
"plus (N i\<^sub>1) (N i\<^sub>2) = N(i\<^sub>1+i\<^sub>2)" |
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    80
"plus (N i) a = (if i=0 then a else Plus (N i) a)" |
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    81
"plus a (N i) = (if i=0 then a else Plus a (N i))" |
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52460
diff changeset
    82
"plus a\<^sub>1 a\<^sub>2 = Plus a\<^sub>1 a\<^sub>2"
49191
3601bf546775 tuned latex
nipkow
parents: 45255
diff changeset
    83
text_raw{*}%endsnip*}
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    84
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    85
lemma aval_plus[simp]:
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    86
  "aval (plus a1 a2) s = aval a1 s + aval a2 s"
45015
fdac1e9880eb Updated IMP to use new induction method
nipkow
parents: 44923
diff changeset
    87
apply(induction a1 a2 rule: plus.induct)
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    88
apply simp_all (* just for a change from auto *)
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    89
done
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    90
49191
3601bf546775 tuned latex
nipkow
parents: 45255
diff changeset
    91
text_raw{*\snip{AExpasimpdef}{2}{0}{% *}
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    92
fun asimp :: "aexp \<Rightarrow> aexp" where
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    93
"asimp (N n) = N n" |
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    94
"asimp (V x) = V x" |
53015
a1119cf551e8 standardized symbols via "isabelle update_sub_sup", excluding src/Pure and src/Tools/WWW_Find;
wenzelm
parents: 52460
diff changeset
    95
"asimp (Plus a\<^sub>1 a\<^sub>2) = plus (asimp a\<^sub>1) (asimp a\<^sub>2)"
49191
3601bf546775 tuned latex
nipkow
parents: 45255
diff changeset
    96
text_raw{*}%endsnip*}
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    97
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    98
text{* Note that in @{const asimp_const} the optimized constructor was
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    99
inlined. Making it a separate function @{const plus} improves modularity of
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   100
the code and the proofs. *}
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   101
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   102
value "asimp (Plus (Plus (N 0) (N 0)) (Plus (V ''x'') (N 0)))"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   103
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   104
theorem aval_asimp[simp]:
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   105
  "aval (asimp a) s = aval a s"
45015
fdac1e9880eb Updated IMP to use new induction method
nipkow
parents: 44923
diff changeset
   106
apply(induction a)
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   107
apply simp_all
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   108
done
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   109
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   110
end