src/HOL/IMP/AExp.thy
author nipkow
Wed, 13 Feb 2013 11:28:44 +0100
changeset 52222 faf7f0d4f9eb
parent 50206 3601bf546775
child 53597 92ae850a9bfd
permissions -rw-r--r--
tuned state display
     1 header "Arithmetic and Boolean Expressions"
     2 
     3 theory AExp imports Main begin
     4 
     5 subsection "Arithmetic Expressions"
     6 
     7 type_synonym vname = string
     8 type_synonym val = int
     9 type_synonym state = "vname \<Rightarrow> val"
    10 
    11 text_raw{*\snip{AExpaexpdef}{2}{1}{% *}
    12 datatype aexp = N int | V vname | Plus aexp aexp
    13 text_raw{*}%endsnip*}
    14 
    15 text_raw{*\snip{AExpavaldef}{1}{2}{% *}
    16 fun aval :: "aexp \<Rightarrow> state \<Rightarrow> val" where
    17 "aval (N n) s = n" |
    18 "aval (V x) s = s x" |
    19 "aval (Plus a1 a2) s = aval a1 s + aval a2 s"
    20 text_raw{*}%endsnip*}
    21 
    22 
    23 value "aval (Plus (V ''x'') (N 5)) (\<lambda>x. if x = ''x'' then 7 else 0)"
    24 
    25 text {* The same state more concisely: *}
    26 value "aval (Plus (V ''x'') (N 5)) ((\<lambda>x. 0) (''x'':= 7))"
    27 
    28 text {* A little syntax magic to write larger states compactly: *}
    29 
    30 definition null_state ("<>") where
    31   "null_state \<equiv> \<lambda>x. 0"
    32 syntax 
    33   "_State" :: "updbinds => 'a" ("<_>")
    34 translations
    35   "_State ms" == "_Update <> ms"
    36 
    37 text {* \noindent
    38   We can now write a series of updates to the function @{text "\<lambda>x. 0"} compactly:
    39 *}
    40 lemma "<a := Suc 0, b := 2> = (<> (a := Suc 0)) (b := 2)"
    41   by (rule refl)
    42 
    43 value "aval (Plus (V ''x'') (N 5)) <''x'' := 7>"
    44 
    45 
    46 text {* In  the @{term[source] "<a := b>"} syntax, variables that are not mentioned are 0 by default:
    47 *}
    48 value "aval (Plus (V ''x'') (N 5)) <''y'' := 7>"
    49 
    50 text{* Note that this @{text"<\<dots>>"} syntax works for any function space
    51 @{text"\<tau>\<^isub>1 \<Rightarrow> \<tau>\<^isub>2"} where @{text "\<tau>\<^isub>2"} has a @{text 0}. *}
    52 
    53 
    54 subsection "Constant Folding"
    55 
    56 text{* Evaluate constant subsexpressions: *}
    57 
    58 text_raw{*\snip{AExpasimpconstdef}{0}{2}{% *}
    59 fun asimp_const :: "aexp \<Rightarrow> aexp" where
    60 "asimp_const (N n) = N n" |
    61 "asimp_const (V x) = V x" |
    62 "asimp_const (Plus a\<^isub>1 a\<^isub>2) =
    63   (case (asimp_const a\<^isub>1, asimp_const a\<^isub>2) of
    64     (N n\<^isub>1, N n\<^isub>2) \<Rightarrow> N(n\<^isub>1+n\<^isub>2) |
    65     (b\<^isub>1,b\<^isub>2) \<Rightarrow> Plus b\<^isub>1 b\<^isub>2)"
    66 text_raw{*}%endsnip*}
    67 
    68 theorem aval_asimp_const:
    69   "aval (asimp_const a) s = aval a s"
    70 apply(induction a)
    71 apply (auto split: aexp.split)
    72 done
    73 
    74 text{* Now we also eliminate all occurrences 0 in additions. The standard
    75 method: optimized versions of the constructors: *}
    76 
    77 text_raw{*\snip{AExpplusdef}{0}{2}{% *}
    78 fun plus :: "aexp \<Rightarrow> aexp \<Rightarrow> aexp" where
    79 "plus (N i\<^isub>1) (N i\<^isub>2) = N(i\<^isub>1+i\<^isub>2)" |
    80 "plus (N i) a = (if i=0 then a else Plus (N i) a)" |
    81 "plus a (N i) = (if i=0 then a else Plus a (N i))" |
    82 "plus a\<^isub>1 a\<^isub>2 = Plus a\<^isub>1 a\<^isub>2"
    83 text_raw{*}%endsnip*}
    84 
    85 lemma aval_plus[simp]:
    86   "aval (plus a1 a2) s = aval a1 s + aval a2 s"
    87 apply(induction a1 a2 rule: plus.induct)
    88 apply simp_all (* just for a change from auto *)
    89 done
    90 
    91 text_raw{*\snip{AExpasimpdef}{2}{0}{% *}
    92 fun asimp :: "aexp \<Rightarrow> aexp" where
    93 "asimp (N n) = N n" |
    94 "asimp (V x) = V x" |
    95 "asimp (Plus a\<^isub>1 a\<^isub>2) = plus (asimp a\<^isub>1) (asimp a\<^isub>2)"
    96 text_raw{*}%endsnip*}
    97 
    98 text{* Note that in @{const asimp_const} the optimized constructor was
    99 inlined. Making it a separate function @{const plus} improves modularity of
   100 the code and the proofs. *}
   101 
   102 value "asimp (Plus (Plus (N 0) (N 0)) (Plus (V ''x'') (N 0)))"
   103 
   104 theorem aval_asimp[simp]:
   105   "aval (asimp a) s = aval a s"
   106 apply(induction a)
   107 apply simp_all
   108 done
   109 
   110 end