src/HOL/IMP/AExp.thy
author nipkow
Tue, 20 Sep 2011 05:48:23 +0200
changeset 45015 fdac1e9880eb
parent 44923 b80108b346a9
child 45212 e87feee00a4c
permissions -rw-r--r--
Updated IMP to use new induction method
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
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
     7
type_synonym name = string
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
     8
type_synonym val = int
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
     9
type_synonym state = "name \<Rightarrow> val"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    10
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    11
datatype aexp = N int | V name | Plus aexp aexp
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    12
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    13
fun aval :: "aexp \<Rightarrow> state \<Rightarrow> val" where
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    14
"aval (N n) _ = n" |
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    15
"aval (V x) s = s x" |
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    16
"aval (Plus a1 a2) s = aval a1 s + aval a2 s"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    17
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    18
44923
b80108b346a9 cleand up AbsInt fixpoint iteration; tuned syntax
nipkow
parents: 44036
diff changeset
    19
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
    20
43158
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    21
text {* The same state more concisely: *}
44923
b80108b346a9 cleand up AbsInt fixpoint iteration; tuned syntax
nipkow
parents: 44036
diff changeset
    22
value "aval (Plus (V ''x'') (N 5)) ((\<lambda>x. 0) (''x'':= 7))"
43158
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    23
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    24
text {* A little syntax magic to write larger states compactly: *}
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    25
44923
b80108b346a9 cleand up AbsInt fixpoint iteration; tuned syntax
nipkow
parents: 44036
diff changeset
    26
definition null_state ("<>") where
b80108b346a9 cleand up AbsInt fixpoint iteration; tuned syntax
nipkow
parents: 44036
diff changeset
    27
  "null_state \<equiv> \<lambda>x. 0"
44036
d03f9f28d01d new state syntax with less conflicts
kleing
parents: 43250
diff changeset
    28
syntax 
d03f9f28d01d new state syntax with less conflicts
kleing
parents: 43250
diff changeset
    29
  "_State" :: "updbinds => 'a" ("<_>")
43158
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    30
translations
44923
b80108b346a9 cleand up AbsInt fixpoint iteration; tuned syntax
nipkow
parents: 44036
diff changeset
    31
  "_State ms" => "_Update <> ms"
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    32
43158
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    33
text {* 
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    34
  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
    35
*}
44923
b80108b346a9 cleand up AbsInt fixpoint iteration; tuned syntax
nipkow
parents: 44036
diff changeset
    36
lemma "<a := Suc 0, b := 2> = (<> (a := Suc 0)) (b := 2)"
43158
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    37
  by (rule refl)
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    38
44036
d03f9f28d01d new state syntax with less conflicts
kleing
parents: 43250
diff changeset
    39
value "aval (Plus (V ''x'') (N 5)) <''x'' := 7>"
43158
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    40
44923
b80108b346a9 cleand up AbsInt fixpoint iteration; tuned syntax
nipkow
parents: 44036
diff changeset
    41
43158
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    42
text {* Variables that are not mentioned in the state are 0 by default in 
44036
d03f9f28d01d new state syntax with less conflicts
kleing
parents: 43250
diff changeset
    43
  the @{term "<a := b::int>"} syntax: 
44923
b80108b346a9 cleand up AbsInt fixpoint iteration; tuned syntax
nipkow
parents: 44036
diff changeset
    44
*}
44036
d03f9f28d01d new state syntax with less conflicts
kleing
parents: 43250
diff changeset
    45
value "aval (Plus (V ''x'') (N 5)) <''y'' := 7>"
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    46
44923
b80108b346a9 cleand up AbsInt fixpoint iteration; tuned syntax
nipkow
parents: 44036
diff changeset
    47
text{* Note that this @{text"<\<dots>>"} syntax works for any function space
b80108b346a9 cleand up AbsInt fixpoint iteration; tuned syntax
nipkow
parents: 44036
diff changeset
    48
@{text"\<tau>\<^isub>1 \<Rightarrow> \<tau>\<^isub>2"} where @{text "\<tau>\<^isub>2"} has a @{text 0}. *}
b80108b346a9 cleand up AbsInt fixpoint iteration; tuned syntax
nipkow
parents: 44036
diff changeset
    49
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    50
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    51
subsection "Optimization"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    52
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    53
text{* Evaluate constant subsexpressions: *}
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    54
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    55
fun asimp_const :: "aexp \<Rightarrow> aexp" where
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    56
"asimp_const (N n) = N n" |
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    57
"asimp_const (V x) = V x" |
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    58
"asimp_const (Plus a1 a2) =
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    59
  (case (asimp_const a1, asimp_const a2) of
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    60
    (N n1, N n2) \<Rightarrow> N(n1+n2) |
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    61
    (a1',a2') \<Rightarrow> Plus a1' a2')"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    62
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    63
theorem aval_asimp_const[simp]:
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    64
  "aval (asimp_const a) s = aval a s"
45015
fdac1e9880eb Updated IMP to use new induction method
nipkow
parents: 44923
diff changeset
    65
apply(induction a)
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    66
apply (auto split: aexp.split)
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    67
done
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    68
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    69
text{* Now we also eliminate all occurrences 0 in additions. The standard
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    70
method: optimized versions of the constructors: *}
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    71
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    72
fun plus :: "aexp \<Rightarrow> aexp \<Rightarrow> aexp" where
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    73
"plus (N i1) (N i2) = N(i1+i2)" |
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    74
"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
    75
"plus a (N i) = (if i=0 then a else Plus a (N i))" |
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    76
"plus a1 a2 = Plus a1 a2"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    77
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    78
lemma aval_plus[simp]:
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    79
  "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
    80
apply(induction a1 a2 rule: plus.induct)
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    81
apply simp_all (* just for a change from auto *)
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    82
done
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    83
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    84
fun asimp :: "aexp \<Rightarrow> aexp" where
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    85
"asimp (N n) = N n" |
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    86
"asimp (V x) = V x" |
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    87
"asimp (Plus a1 a2) = plus (asimp a1) (asimp a2)"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    88
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    89
text{* Note that in @{const asimp_const} the optimized constructor was
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    90
inlined. Making it a separate function @{const plus} improves modularity of
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    91
the code and the proofs. *}
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    92
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    93
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
    94
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    95
theorem aval_asimp[simp]:
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    96
  "aval (asimp a) s = aval a s"
45015
fdac1e9880eb Updated IMP to use new induction method
nipkow
parents: 44923
diff changeset
    97
apply(induction a)
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    98
apply simp_all
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    99
done
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   100
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   101
end