doc-src/TutorialI/Ifexpr/Ifexpr.thy
changeset 11456 7eb63f63e6c6
parent 10978 5eebea8f359f
child 12327 5a4d78204492
equal deleted inserted replaced
11455:e07927b980ec 11456:7eb63f63e6c6
     2 theory Ifexpr = Main:;
     2 theory Ifexpr = Main:;
     3 (*>*)
     3 (*>*)
     4 
     4 
     5 subsection{*Case Study: Boolean Expressions*}
     5 subsection{*Case Study: Boolean Expressions*}
     6 
     6 
     7 text{*\label{sec:boolex}
     7 text{*\label{sec:boolex}\index{boolean expressions example|(}
     8 The aim of this case study is twofold: it shows how to model boolean
     8 The aim of this case study is twofold: it shows how to model boolean
     9 expressions and some algorithms for manipulating them, and it demonstrates
     9 expressions and some algorithms for manipulating them, and it demonstrates
    10 the constructs introduced above.
    10 the constructs introduced above.
    11 *}
    11 *}
    12 
    12 
   118 "norm (CIF b)    = CIF b"
   118 "norm (CIF b)    = CIF b"
   119 "norm (VIF x)    = VIF x"
   119 "norm (VIF x)    = VIF x"
   120 "norm (IF b t e) = normif b (norm t) (norm e)";
   120 "norm (IF b t e) = normif b (norm t) (norm e)";
   121 
   121 
   122 text{*\noindent
   122 text{*\noindent
   123 Their interplay is a bit tricky, and we leave it to the reader to develop an
   123 Their interplay is tricky; we leave it to you to develop an
   124 intuitive understanding. Fortunately, Isabelle can help us to verify that the
   124 intuitive understanding. Fortunately, Isabelle can help us to verify that the
   125 transformation preserves the value of the expression:
   125 transformation preserves the value of the expression:
   126 *}
   126 *}
   127 
   127 
   128 theorem "valif (norm b) env = valif b env";(*<*)oops;(*>*)
   128 theorem "valif (norm b) env = valif b env";(*<*)oops;(*>*)
   129 
   129 
   130 text{*\noindent
   130 text{*\noindent
   131 The proof is canonical, provided we first show the following simplification
   131 The proof is canonical, provided we first show the following simplification
   132 lemma (which also helps to understand what @{term"normif"} does):
   132 lemma, which also helps to understand what @{term"normif"} does:
   133 *}
   133 *}
   134 
   134 
   135 lemma [simp]:
   135 lemma [simp]:
   136   "\<forall>t e. valif (normif b t e) env = valif (IF b t e) env";
   136   "\<forall>t e. valif (normif b t e) env = valif (IF b t e) env";
   137 (*<*)
   137 (*<*)
   145 text{*\noindent
   145 text{*\noindent
   146 Note that the lemma does not have a name, but is implicitly used in the proof
   146 Note that the lemma does not have a name, but is implicitly used in the proof
   147 of the theorem shown above because of the @{text"[simp]"} attribute.
   147 of the theorem shown above because of the @{text"[simp]"} attribute.
   148 
   148 
   149 But how can we be sure that @{term"norm"} really produces a normal form in
   149 But how can we be sure that @{term"norm"} really produces a normal form in
   150 the above sense? We define a function that tests If-expressions for normality
   150 the above sense? We define a function that tests If-expressions for normality:
   151 *}
   151 *}
   152 
   152 
   153 consts normal :: "ifex \<Rightarrow> bool";
   153 consts normal :: "ifex \<Rightarrow> bool";
   154 primrec
   154 primrec
   155 "normal(CIF b) = True"
   155 "normal(CIF b) = True"
   156 "normal(VIF x) = True"
   156 "normal(VIF x) = True"
   157 "normal(IF b t e) = (normal t \<and> normal e \<and>
   157 "normal(IF b t e) = (normal t \<and> normal e \<and>
   158      (case b of CIF b \<Rightarrow> True | VIF x \<Rightarrow> True | IF x y z \<Rightarrow> False))";
   158      (case b of CIF b \<Rightarrow> True | VIF x \<Rightarrow> True | IF x y z \<Rightarrow> False))";
   159 
   159 
   160 text{*\noindent
   160 text{*\noindent
   161 and prove @{term"normal(norm b)"}. Of course, this requires a lemma about
   161 Now we prove @{term"normal(norm b)"}. Of course, this requires a lemma about
   162 normality of @{term"normif"}:
   162 normality of @{term"normif"}:
   163 *}
   163 *}
   164 
   164 
   165 lemma[simp]: "\<forall>t e. normal(normif b t e) = (normal t \<and> normal e)";
   165 lemma[simp]: "\<forall>t e. normal(normif b t e) = (normal t \<and> normal e)";
   166 (*<*)
   166 (*<*)
   184   the first argument of all @{term IF}s must be a variable. Adapt the above
   184   the first argument of all @{term IF}s must be a variable. Adapt the above
   185   development to this changed requirement. (Hint: you may need to formulate
   185   development to this changed requirement. (Hint: you may need to formulate
   186   some of the goals as implications (@{text"\<longrightarrow>"}) rather than
   186   some of the goals as implications (@{text"\<longrightarrow>"}) rather than
   187   equalities (@{text"="}).)
   187   equalities (@{text"="}).)
   188 \end{exercise}
   188 \end{exercise}
       
   189 \index{boolean expressions example|)}
   189 *}
   190 *}
   190 (*<*)
   191 (*<*)
   191 end
   192 end
   192 (*>*)
   193 (*>*)