src/HOL/IMP/AExp.thy
changeset 53015 a1119cf551e8
parent 52460 92ae850a9bfd
child 54252 a4a00347e59f
equal deleted inserted replaced
53009:bb18eed53ed6 53015:a1119cf551e8
    14 
    14 
    15 text_raw{*\snip{AExpavaldef}{1}{2}{% *}
    15 text_raw{*\snip{AExpavaldef}{1}{2}{% *}
    16 fun aval :: "aexp \<Rightarrow> state \<Rightarrow> val" where
    16 fun aval :: "aexp \<Rightarrow> state \<Rightarrow> val" where
    17 "aval (N n) s = n" |
    17 "aval (N n) s = n" |
    18 "aval (V x) s = s x" |
    18 "aval (V x) s = s x" |
    19 "aval (Plus a\<^isub>1 a\<^isub>2) s = aval a\<^isub>1 s + aval a\<^isub>2 s"
    19 "aval (Plus a\<^sub>1 a\<^sub>2) s = aval a\<^sub>1 s + aval a\<^sub>2 s"
    20 text_raw{*}%endsnip*}
    20 text_raw{*}%endsnip*}
    21 
    21 
    22 
    22 
    23 value "aval (Plus (V ''x'') (N 5)) (\<lambda>x. if x = ''x'' then 7 else 0)"
    23 value "aval (Plus (V ''x'') (N 5)) (\<lambda>x. if x = ''x'' then 7 else 0)"
    24 
    24 
    46 text {* In  the @{term[source] "<a := b>"} syntax, variables that are not mentioned are 0 by default:
    46 text {* In  the @{term[source] "<a := b>"} syntax, variables that are not mentioned are 0 by default:
    47 *}
    47 *}
    48 value "aval (Plus (V ''x'') (N 5)) <''y'' := 7>"
    48 value "aval (Plus (V ''x'') (N 5)) <''y'' := 7>"
    49 
    49 
    50 text{* Note that this @{text"<\<dots>>"} syntax works for any function space
    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}. *}
    51 @{text"\<tau>\<^sub>1 \<Rightarrow> \<tau>\<^sub>2"} where @{text "\<tau>\<^sub>2"} has a @{text 0}. *}
    52 
    52 
    53 
    53 
    54 subsection "Constant Folding"
    54 subsection "Constant Folding"
    55 
    55 
    56 text{* Evaluate constant subsexpressions: *}
    56 text{* Evaluate constant subsexpressions: *}
    57 
    57 
    58 text_raw{*\snip{AExpasimpconstdef}{0}{2}{% *}
    58 text_raw{*\snip{AExpasimpconstdef}{0}{2}{% *}
    59 fun asimp_const :: "aexp \<Rightarrow> aexp" where
    59 fun asimp_const :: "aexp \<Rightarrow> aexp" where
    60 "asimp_const (N n) = N n" |
    60 "asimp_const (N n) = N n" |
    61 "asimp_const (V x) = V x" |
    61 "asimp_const (V x) = V x" |
    62 "asimp_const (Plus a\<^isub>1 a\<^isub>2) =
    62 "asimp_const (Plus a\<^sub>1 a\<^sub>2) =
    63   (case (asimp_const a\<^isub>1, asimp_const a\<^isub>2) of
    63   (case (asimp_const a\<^sub>1, asimp_const a\<^sub>2) of
    64     (N n\<^isub>1, N n\<^isub>2) \<Rightarrow> N(n\<^isub>1+n\<^isub>2) |
    64     (N n\<^sub>1, N n\<^sub>2) \<Rightarrow> N(n\<^sub>1+n\<^sub>2) |
    65     (b\<^isub>1,b\<^isub>2) \<Rightarrow> Plus b\<^isub>1 b\<^isub>2)"
    65     (b\<^sub>1,b\<^sub>2) \<Rightarrow> Plus b\<^sub>1 b\<^sub>2)"
    66 text_raw{*}%endsnip*}
    66 text_raw{*}%endsnip*}
    67 
    67 
    68 theorem aval_asimp_const:
    68 theorem aval_asimp_const:
    69   "aval (asimp_const a) s = aval a s"
    69   "aval (asimp_const a) s = aval a s"
    70 apply(induction a)
    70 apply(induction a)
    74 text{* Now we also eliminate all occurrences 0 in additions. The standard
    74 text{* Now we also eliminate all occurrences 0 in additions. The standard
    75 method: optimized versions of the constructors: *}
    75 method: optimized versions of the constructors: *}
    76 
    76 
    77 text_raw{*\snip{AExpplusdef}{0}{2}{% *}
    77 text_raw{*\snip{AExpplusdef}{0}{2}{% *}
    78 fun plus :: "aexp \<Rightarrow> aexp \<Rightarrow> aexp" where
    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)" |
    79 "plus (N i\<^sub>1) (N i\<^sub>2) = N(i\<^sub>1+i\<^sub>2)" |
    80 "plus (N i) a = (if i=0 then a else Plus (N i) a)" |
    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))" |
    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"
    82 "plus a\<^sub>1 a\<^sub>2 = Plus a\<^sub>1 a\<^sub>2"
    83 text_raw{*}%endsnip*}
    83 text_raw{*}%endsnip*}
    84 
    84 
    85 lemma aval_plus[simp]:
    85 lemma aval_plus[simp]:
    86   "aval (plus a1 a2) s = aval a1 s + aval a2 s"
    86   "aval (plus a1 a2) s = aval a1 s + aval a2 s"
    87 apply(induction a1 a2 rule: plus.induct)
    87 apply(induction a1 a2 rule: plus.induct)
    90 
    90 
    91 text_raw{*\snip{AExpasimpdef}{2}{0}{% *}
    91 text_raw{*\snip{AExpasimpdef}{2}{0}{% *}
    92 fun asimp :: "aexp \<Rightarrow> aexp" where
    92 fun asimp :: "aexp \<Rightarrow> aexp" where
    93 "asimp (N n) = N n" |
    93 "asimp (N n) = N n" |
    94 "asimp (V x) = V x" |
    94 "asimp (V x) = V x" |
    95 "asimp (Plus a\<^isub>1 a\<^isub>2) = plus (asimp a\<^isub>1) (asimp a\<^isub>2)"
    95 "asimp (Plus a\<^sub>1 a\<^sub>2) = plus (asimp a\<^sub>1) (asimp a\<^sub>2)"
    96 text_raw{*}%endsnip*}
    96 text_raw{*}%endsnip*}
    97 
    97 
    98 text{* Note that in @{const asimp_const} the optimized constructor was
    98 text{* Note that in @{const asimp_const} the optimized constructor was
    99 inlined. Making it a separate function @{const plus} improves modularity of
    99 inlined. Making it a separate function @{const plus} improves modularity of
   100 the code and the proofs. *}
   100 the code and the proofs. *}