src/HOL/IMP/AExp.thy
author bulwahn
Thu, 07 Jul 2011 23:33:14 +0200
changeset 43704 47b0be18ccbe
parent 43250 c729110a9f08
child 44036 d03f9f28d01d
permissions -rw-r--r--
floor and ceiling definitions are not code equations -- this enables trivial evaluation of floor and ceiling
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
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    19
value "aval (Plus (V ''x'') (N 5)) (%x. if x = ''x'' then 7 else 0)"
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: *}
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    22
value "aval (Plus (V ''x'') (N 5)) ((%x. 0) (''x'':= 7))"
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
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    26
nonterminal funlets and funlet
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    27
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    28
syntax
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    29
  "_funlet"  :: "['a, 'a] => funlet"             ("_ /->/ _")
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    30
   ""         :: "funlet => funlets"             ("_")
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    31
  "_Funlets" :: "[funlet, funlets] => funlets"   ("_,/ _")
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    32
  "_Fun"     :: "funlets => 'a => 'b"            ("(1[_])")
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    33
  "_FunUpd"  :: "['a => 'b, funlets] => 'a => 'b" ("_/'(_')" [900,0]900)
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    34
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    35
syntax (xsymbols)
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    36
  "_funlet"  :: "['a, 'a] => funlet"             ("_ /\<rightarrow>/ _")
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    37
43250
c729110a9f08 use null_heap instead of %_. 0 to avoid printing problems
kleing
parents: 43158
diff changeset
    38
definition
c729110a9f08 use null_heap instead of %_. 0 to avoid printing problems
kleing
parents: 43158
diff changeset
    39
  "null_heap \<equiv> \<lambda>x. 0"
c729110a9f08 use null_heap instead of %_. 0 to avoid printing problems
kleing
parents: 43158
diff changeset
    40
43158
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    41
translations
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    42
  "_FunUpd m (_Funlets xy ms)"  == "_FunUpd (_FunUpd m xy) ms"
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    43
  "_FunUpd m (_funlet  x y)"    == "m(x := y)"
43250
c729110a9f08 use null_heap instead of %_. 0 to avoid printing problems
kleing
parents: 43158
diff changeset
    44
  "_Fun ms"                     == "_FunUpd (CONST null_heap) ms"
43158
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    45
  "_Fun (_Funlets ms1 ms2)"     <= "_FunUpd (_Fun ms1) ms2"
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    46
  "_Funlets ms1 (_Funlets ms2 ms3)" <= "_Funlets (_Funlets ms1 ms2) ms3"
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    47
43158
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    48
text {* 
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    49
  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
    50
*}
43250
c729110a9f08 use null_heap instead of %_. 0 to avoid printing problems
kleing
parents: 43158
diff changeset
    51
lemma "[a \<rightarrow> Suc 0, b \<rightarrow> 2] = (null_heap (a := Suc 0)) (b := 2)"
43158
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    52
  by (rule refl)
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    53
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    54
value "aval (Plus (V ''x'') (N 5)) [''x'' \<rightarrow> 7]"
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    55
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    56
text {* Variables that are not mentioned in the state are 0 by default in 
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    57
  the @{term "[a \<rightarrow> b::int]"} syntax: 
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    58
*}   
686fa0a0696e imported rest of new IMP
kleing
parents: 43141
diff changeset
    59
value "aval (Plus (V ''x'') (N 5)) [''y'' \<rightarrow> 7]"
43141
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    60
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    61
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    62
subsection "Optimization"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    63
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    64
text{* Evaluate constant subsexpressions: *}
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    65
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    66
fun asimp_const :: "aexp \<Rightarrow> aexp" where
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    67
"asimp_const (N n) = N n" |
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    68
"asimp_const (V x) = V x" |
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    69
"asimp_const (Plus a1 a2) =
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    70
  (case (asimp_const a1, asimp_const a2) of
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    71
    (N n1, N n2) \<Rightarrow> N(n1+n2) |
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    72
    (a1',a2') \<Rightarrow> Plus a1' a2')"
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
theorem aval_asimp_const[simp]:
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    75
  "aval (asimp_const a) s = aval a s"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    76
apply(induct a)
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    77
apply (auto split: aexp.split)
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    78
done
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    79
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    80
text{* Now we also eliminate all occurrences 0 in additions. The standard
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    81
method: optimized versions of the constructors: *}
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    82
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    83
fun plus :: "aexp \<Rightarrow> aexp \<Rightarrow> aexp" where
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    84
"plus (N i1) (N i2) = N(i1+i2)" |
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    85
"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
    86
"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
    87
"plus a1 a2 = Plus a1 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
code_thms plus
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    90
code_thms plus
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    91
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    92
(* FIXME: dropping subsumed code eqns?? *)
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    93
lemma aval_plus[simp]:
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    94
  "aval (plus a1 a2) s = aval a1 s + aval a2 s"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    95
apply(induct a1 a2 rule: plus.induct)
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    96
apply simp_all (* just for a change from auto *)
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    97
done
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    98
code_thms plus
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
    99
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   100
fun asimp :: "aexp \<Rightarrow> aexp" where
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   101
"asimp (N n) = N n" |
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   102
"asimp (V x) = V x" |
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   103
"asimp (Plus a1 a2) = plus (asimp a1) (asimp a2)"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   104
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   105
text{* Note that in @{const asimp_const} the optimized constructor was
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   106
inlined. Making it a separate function @{const plus} improves modularity of
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   107
the code and the proofs. *}
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   108
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   109
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
   110
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   111
theorem aval_asimp[simp]:
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   112
  "aval (asimp a) s = aval a s"
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   113
apply(induct a)
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   114
apply simp_all
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   115
done
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   116
11fce8564415 Replacing old IMP with new Semantics material
nipkow
parents:
diff changeset
   117
end