test/Tools/isac/ADDTESTS/course/SignalProcess/Build_Inverse_Z_Transform.thy
changeset 42368 3afe632cd527
parent 42367 c1ebb7e759f9
child 42369 3fa947c99a28
equal deleted inserted replaced
42367:c1ebb7e759f9 42368:3afe632cd527
     7 
     7 
     8 theory Build_Inverse_Z_Transform imports Isac
     8 theory Build_Inverse_Z_Transform imports Isac
     9   
     9   
    10 begin
    10 begin
    11 
    11 
    12 text{* We stepwise build \texttt{Inverse\_Z\_Transform.thy} as an exercise.
    12 text{* We stepwise build \ttfamily Inverse\_Z\_Transform.thy \normalfont as an 
    13   Because subsection "Stepwise Check the Program" requires 
    13   exercise. Because subsection~\ref{sec:stepcheck} requires 
    14   \texttt{Inverse\_Z\_Transform.thy} as a subtheory of Isac.thy, the setup has been changed 
    14   \ttfamily Inverse\_Z\_Transform.thy \normalfont as a subtheory of \ttfamily 
    15   from "theory Inverse_Z_Transform imports Isac begin.." to the above.
    15   Isac.thy\normalfont, the setup has been changed from \ttfamily theory 
    16 
    16   Inverse\_Z\_Transform imports Isac \normalfont to the above one.
    17   ATTENTION WITH NAMES OF IDENTIFIERS WHEN GOING INTO INTERNALS:
    17   \par \noindent
       
    18   \begin{center} 
       
    19   \textbf{ATTENTION WITH NAMES OF IDENTIFIERS WHEN GOING INTO INTERNALS}
       
    20   \end{center}
    18   Here in this theory there are the internal names twice, for instance we have
    21   Here in this theory there are the internal names twice, for instance we have
    19   (Thm.derivation_name @{thm rule1} = "Build_Inverse_Z_Transform.rule1") = true;
    22   \ttfamily (Thm.derivation\_name @{thm rule1} = 
    20   but actually in us will be "Inverse_Z_Transform.rule1"
    23   "Build\_Inverse\_Z\_Transform.rule1") = true; \normalfont
    21 *}
    24   but actually in us will be \ttfamily Inverse\_Z\_Transform.rule1 \normalfont
       
    25 *}
       
    26 
       
    27 section {*Trials towards the Z-Transform\label{sec:trials}*}
       
    28 
    22 ML {*val thy = @{theory Isac};*}
    29 ML {*val thy = @{theory Isac};*}
    23 
    30 
    24 
    31 subsection {*Notations and Terms*}
    25 section {*trials towards Z transform *}
    32 text{*\noindent Try which notations we are able to use.*}
    26 text{*===============================*}
    33 ML {*
    27 subsection {*terms*}
    34   @{term "1 < || z ||"};
    28 ML {*
    35   @{term "z / (z - 1)"};
    29 @{term "1 < || z ||"};
    36   @{term "-u -n - 1"};
    30 @{term "z / (z - 1)"};
    37   @{term "-u [-n - 1]"}; (*[ ] denotes lists !!!*)
    31 @{term "-u -n - 1"};
    38   @{term "z /(z - 1) = -u [-n - 1]"};Isac
    32 @{term "-u [-n - 1]"}; (*[ ] denotes lists !!!*)
    39   @{term "1 < || z || ==> z / (z - 1) = -u [-n - 1]"};
    33 @{term "z /(z - 1) = -u [-n - 1]"};Isac
    40   term2str @{term "1 < || z || ==> z / (z - 1) = -u [-n - 1]"};
    34 @{term "1 < || z || ==> z / (z - 1) = -u [-n - 1]"};
    41 *}
    35 term2str @{term "1 < || z || ==> z / (z - 1) = -u [-n - 1]"};
    42 text{*\noindent Try which symbols we are able to use and how we generate them.*}
    36 *}
    43 ML {*
    37 ML {*
    44   (*alpha -->  "</alpha>" *)
    38 (*alpha -->  "</alpha>" *)
    45   @{term "\<alpha> "};
    39 @{term "\<alpha> "};
    46   @{term "\<delta> "};
    40 @{term "\<delta> "};
    47   @{term "\<phi> "};
    41 @{term "\<phi> "};
    48   @{term "\<rho> "};
    42 @{term "\<rho> "};
    49   term2str @{term "\<rho> "};
    43 term2str @{term "\<rho> "};
    50 *}
    44 *}
    51 
    45 
    52 subsection {*Rules*}
    46 subsection {*rules*}
    53 (*axiomatization "z / (z - 1) = -u [-n - 1]"
    47 (*axiomatization "z / (z - 1) = -u [-n - 1]" Illegal variable name: "z / (z - 1) = -u [-n - 1]" *)
    54   Illegal variable name: "z / (z - 1) = -u [-n - 1]" *)
    48 (*definition     "z / (z - 1) = -u [-n - 1]" Bad head of lhs: existing constant "op /"*)
    55 (*definition     "z / (z - 1) = -u [-n - 1]"
       
    56   Bad head of lhs: existing constant "op /"*)
    49 axiomatization where 
    57 axiomatization where 
    50   rule1: "1 = \<delta>[n]" and
    58   rule1: "1 = \<delta>[n]" and
    51   rule2: "|| z || > 1 ==> z / (z - 1) = u [n]" and
    59   rule2: "|| z || > 1 ==> z / (z - 1) = u [n]" and
    52   rule3: "|| z || < 1 ==> z / (z - 1) = -u [-n - 1]" and 
    60   rule3: "|| z || < 1 ==> z / (z - 1) = -u [-n - 1]" and 
    53   rule4: "|| z || > || \<alpha> || ==> z / (z - \<alpha>) = \<alpha>^^^n * u [n]" and
    61   rule4: "|| z || > || \<alpha> || ==> z / (z - \<alpha>) = \<alpha>^^^n * u [n]" and
    54   rule5: "|| z || < || \<alpha> || ==> z / (z - \<alpha>) = -(\<alpha>^^^n) * u [-n - 1]" and
    62   rule5: "|| z || < || \<alpha> || ==> z / (z - \<alpha>) = -(\<alpha>^^^n) * u [-n - 1]" and
    55   rule6: "|| z || > 1 ==> z/(z - 1)^^^2 = n * u [n]"
    63   rule6: "|| z || > 1 ==> z/(z - 1)^^^2 = n * u [n]"
    56 ML {*
    64 
    57 @{thm rule1};
    65 text{*\noindent Check the rules for their correct notation. 
    58 @{thm rule2};
    66       (See the machine output.)*}
    59 @{thm rule3};
    67 ML {*
    60 @{thm rule4};
    68   @{thm rule1};
    61 *}
    69   @{thm rule2};
    62 
    70   @{thm rule3};
    63 subsection {*apply rules*}
    71   @{thm rule4};
    64 ML {*
    72 *}
    65 val inverse_Z = append_rls "inverse_Z" e_rls
    73 
    66   [ Thm  ("rule3",num_str @{thm rule3}),
    74 subsection {*Apply Rules*}
    67     Thm  ("rule4",num_str @{thm rule4}),
    75 text{*\noindent We try to apply the rules to a given expression.*}
    68     Thm  ("rule1",num_str @{thm rule1})   
    76 
    69   ];
    77 ML {*
    70 
    78   val inverse_Z = append_rls "inverse_Z" e_rls
    71 val t = str2term "z / (z - 1) + z / (z - \<alpha>) + 1";
    79     [ Thm  ("rule3",num_str @{thm rule3}),
    72 val SOME (t', asm) = rewrite_set_ thy true inverse_Z t;
    80       Thm  ("rule4",num_str @{thm rule4}),
    73 term2str t' = "z / (z - ?\<delta> [?n]) + z / (z - \<alpha>) + ?\<delta> [?n]"; (*attention rule1 !!!*)
    81       Thm  ("rule1",num_str @{thm rule1})   
    74 *}
    82     ];
    75 ML {*
    83 
    76 val (thy, ro, er) = (@{theory Isac}, tless_true, eval_rls);
    84   val t = str2term "z / (z - 1) + z / (z - \<alpha>) + 1";
    77 *}
    85   val SOME (t', asm) = rewrite_set_ thy true inverse_Z t;
    78 ML {*
    86   term2str t' = "z / (z - ?\<delta> [?n]) + z / (z - \<alpha>) + ?\<delta> [?n]";
    79 val SOME (t, asm1) = rewrite_ thy ro er true (num_str @{thm rule3}) t;
    87   (*
    80 term2str t = "- ?u [- ?n - 1] + z / (z - \<alpha>) + 1"; (*- real *)
    88    * Attention rule1 is applied before the expression is 
    81 term2str t;*}
    89    * checked for rule4 which would be correct!!!
    82 ML {*
    90    *)
    83 val SOME (t, asm2) = rewrite_ thy ro er true (num_str @{thm rule4}) t;
    91 *}
    84 term2str t = "- ?u [- ?n - 1] + \<alpha> ^^^ ?n * ?u [?n] + 1"; (*- real *)
    92 
    85 term2str t;
    93 ML {* val (thy, ro, er) = (@{theory Isac}, tless_true, eval_rls); *}
    86 *}
    94 ML {*
    87 ML {*
    95   val SOME (t, asm1) = 
    88 val SOME (t, asm3) = rewrite_ thy ro er true (num_str @{thm rule1}) t;
    96     rewrite_ thy ro er true (num_str @{thm rule3}) t;
    89 term2str t = "- ?u [- ?n - 1] + \<alpha> ^^^ ?n * ?u [?n] + ?\<delta> [?n]"; (*- real *)
    97   term2str t = "- ?u [- ?n - 1] + z / (z - \<alpha>) + 1";
    90 term2str t;
    98   (*- real *)
    91 *}
    99   term2str t;
    92 ML {*
   100 
    93 terms2str (asm1 @ asm2 @ asm3);
   101   val SOME (t, asm2) = 
    94 *}
   102     rewrite_ thy ro er true (num_str @{thm rule4}) t;
    95 
   103   term2str t = "- ?u [- ?n - 1] + \<alpha> ^^^ ?n * ?u [?n] + 1";
    96 section {*Prepare steps for CTP-based programming language*}
   104   (*- real *)
    97 text{*TODO insert Calculation (Referenz?!)
   105   term2str t;
    98 
   106 
    99 The goal... realized in sections below, in Sect.\ref{spec-meth} and Sect.\ref{prog-steps} 
   107   val SOME (t, asm3) = 
   100 
   108     rewrite_ thy ro er true (num_str @{thm rule1}) t;
   101 the reader is advised to jump between the subsequent subsections and the respective steps in Sect.\ref{prog-steps} 
   109   term2str t = "- ?u [- ?n - 1] + \<alpha> ^^^ ?n * ?u [?n] + ?\<delta> [?n]";
   102 
   110   (*- real *)
   103 *}
   111   term2str t;
   104 subsection {*prepare expression \label{prep-expr}*}
   112 *}
   105 ML {*
   113 ML {* terms2str (asm1 @ asm2 @ asm3); *}
   106 val ctxt = ProofContext.init_global @{theory Isac};
   114 
   107 val ctxt = declare_constraints' [@{term "z::real"}] ctxt;
   115 section{*Prepare Steps for CTP-based programming Language\label{sec:prepstep}*}
   108 
   116 text{*
   109 val SOME fun1 = parseNEW ctxt "X z = 3 / (z - 1/4 + -1/8 * z ^^^ -1)"; term2str fun1;
   117       \par \noindent The following sections are challanging with the CTP-based 
   110 val SOME fun1' = parseNEW ctxt "X z = 3 / (z - 1/4 + -1/8 * (1/z))"; term2str fun1';
   118       possibilities of building the programm. The goal is realized in 
   111 *}
   119       Section~\ref{spec-meth} and Section~\ref{prog-steps}.
   112 
   120       \par The reader is advised to jump between the subsequent subsections and 
   113 subsubsection {*multply with z*}
   121       the respective steps in Section~\ref{prog-steps}. By comparing 
       
   122       Section~\ref{sec:calc:ztrans} the calculation can be comprehended step 
       
   123       by step.
       
   124 *}
       
   125 
       
   126 subsection {*Prepare Expression\label{prep-expr}*}
       
   127 ML {*
       
   128   val ctxt = ProofContext.init_global @{theory Isac};
       
   129   val ctxt = declare_constraints' [@{term "z::real"}] ctxt;
       
   130 
       
   131   val SOME fun1 = 
       
   132     parseNEW ctxt "X z = 3 / (z - 1/4 + -1/8 * z ^^^ -1)"; term2str fun1;
       
   133   val SOME fun1' = 
       
   134     parseNEW ctxt "X z = 3 / (z - 1/4 + -1/8 * (1/z))"; term2str fun1';
       
   135 *}
       
   136 
       
   137 subsubsection {*Prepare Numerator and Denominator*}
       
   138 text{*\noindent The partial fraction decomposion is only possible ig we
       
   139        get the bound variable out of the numerator. Therefor we divide
       
   140        the expression by $z$. Follow up the Calculation at 
       
   141        Section~\ref{sec:calc:ztrans} line number 02.*}
       
   142 
   114 axiomatization where
   143 axiomatization where
   115   ruleZY: "(X z = a / b) = (X' z = a / (z * b))"
   144   ruleZY: "(X z = a / b) = (X' z = a / (z * b))"
   116 
   145 
   117 ML {*
   146 ML {*
   118 val (thy, ro, er) = (@{theory Isac}, tless_true, eval_rls);
   147   val (thy, ro, er) = (@{theory Isac}, tless_true, eval_rls);
   119 val SOME (fun2, asm1) = rewrite_ thy ro er true  @{thm ruleZY} fun1; term2str fun2;
   148   val SOME (fun2, asm1) = 
   120 val SOME (fun2', asm1) = rewrite_ thy ro er true  @{thm ruleZY} fun1'; term2str fun2';
   149     rewrite_ thy ro er true  @{thm ruleZY} fun1; term2str fun2;
   121 
   150   val SOME (fun2', asm1) = 
   122 val SOME (fun3,_) = rewrite_set_ @{theory Isac} false norm_Rational fun2;
   151     rewrite_ thy ro er true  @{thm ruleZY} fun1'; term2str fun2';
   123 term2str fun3; (*fails on x^^^(-1) TODO*)
   152 
   124 val SOME (fun3',_) = rewrite_set_ @{theory Isac} false norm_Rational fun2';
   153   val SOME (fun3,_) = 
   125 term2str fun3'; (*OK*)
   154     rewrite_set_ @{theory Isac} false norm_Rational fun2;
   126 *}
   155   term2str fun3;
   127 
   156   (*
   128 subsubsection {*get argument of X': z is the variable the equation is solved for*}
   157    * Fails on x^^^(-1)
   129 text{*grep... Atools.thy, Tools.thy contain general utilities: eval_argument_in, eval_rhs, eval_lhs,...
   158    * We solve this problem by using 1/x as a workaround.
   130 
   159    *)
   131 grep -r "fun eva_" ... shows all functions witch can be used in a script.
   160   val SOME (fun3',_) = 
   132 lookup this files how to build and handle such functions.
   161     rewrite_set_ @{theory Isac} false norm_Rational fun2';
   133 
   162   term2str fun3';
   134 the next section shows how to introduce such a function.
   163   (*
   135 *}
   164    * OK - workaround!
   136 
   165    *)
   137 subsubsection {*Decompose given term into lhs = rhs*}
   166 *}
       
   167 
       
   168 subsubsection {*Get the Argument of the Expression X'*}
       
   169 text{*\noindent We use \texttt{grep} for finding possibilities how we can
       
   170        extract the bound variable in the expression. \ttfamily Atools.thy, 
       
   171        Tools.thy \normalfont contain general utilities: \ttfamily 
       
   172        eval\_argument\_in, eval\_rhs, eval\_lhs,\ldots \normalfont
       
   173        \ttfamily grep -r "fun eva\_" * \normalfont shows all functions 
       
   174        witch can be used in a script. Lookup this files how to build 
       
   175        and handle such functions.
       
   176        \par The next section shows how to introduce such a function.
       
   177 *}
       
   178 
       
   179 subsubsection{*Decompose the Given Term Into lhs and rhs\footnote{Note:
       
   180                lhs means \em Left Hand Side \normalfont and rhs means 
       
   181                \em Right Hand Side \normalfont and indicates the left or 
       
   182                the right part of an equation.}*}
   138 ML {*
   183 ML {*
   139   val (_, expr) = HOLogic.dest_eq fun3'; term2str expr;
   184   val (_, expr) = HOLogic.dest_eq fun3'; term2str expr;
   140   val (_, denom) = HOLogic.dest_bin "Rings.inverse_class.divide" (type_of expr) expr;
   185   val (_, denom) = 
       
   186     HOLogic.dest_bin "Rings.inverse_class.divide" (type_of expr) expr;
   141   term2str denom = "-1 + -2 * z + 8 * z ^^^ 2";
   187   term2str denom = "-1 + -2 * z + 8 * z ^^^ 2";
   142 *}
   188 *}
   143 text {*we have rhs in the Script language, but we need a function 
   189 
   144   which gets the denominator of a fraction*}
   190 text{*\noindent We have rhs in the Script language, but we need a function 
   145 
   191        which gets the denominator of a fraction.*}
   146 
   192 
   147 subsubsection {*get the denominator and numerator out of a fraction*}
   193 subsubsection{*Get the Denominator and Numerator out of a Fraction*}
   148 text {*get denominator should become a constant for the isabelle parser: *}
   194 text{*\noindent The selv written functions in e.g. \texttt{get\_denominator}
       
   195        should become a constant for the isabelle parser:*}
   149 
   196 
   150 consts
   197 consts
   151   get_denominator :: "real => real"
   198   get_denominator :: "real => real"
   152   get_numerator :: "real => real"
   199   get_numerator :: "real => real"
   153 
   200 
   154 text {* With the above definition we run into problems with parsing the Script InverseZTransform:
   201 text {*\noindent With the above definition we run into problems when we parse
   155   This leads to "ambiguous parse trees" and we avoid this by shifting the definition
   202         the Script \texttt{InverseZTransform}. This leads to \em ambiguous
   156   to Rational.thy and re-building Isac.
   203         parse trees. \normalfont We avoid this by moving the definition
   157   ATTENTION: from now on Build_Inverse_Z_Transform mimics a build from scratch;
   204         to \ttfamily Rational.thy \normalfont and re-building Isac.
   158   it only works due to re-building Isac several times (indicated explicityl).
   205         \par \noindent ATTENTION: From now on \ttfamily 
   159 *}
   206         Build\_Inverse\_Z\_Transform \normalfont mimics a build from scratch;
   160 
   207         it only works due to re-building Isac several times (indicated 
   161 ML {*
   208         explicityl).
   162 (*("get_denominator", ("Rational.get_denominator", eval_get_denominator ""))*)
   209 *}
       
   210 
       
   211 ML {*
       
   212 (*
       
   213  *("get_denominator",
       
   214  *  ("Rational.get_denominator", eval_get_denominator ""))
       
   215  *)
   163 fun eval_get_denominator (thmid:string) _ 
   216 fun eval_get_denominator (thmid:string) _ 
   164 		      (t as Const ("Rational.get_denominator", _) $
   217 		      (t as Const ("Rational.get_denominator", _) $
   165               (Const ("Rings.inverse_class.divide", _) $ num $
   218               (Const ("Rings.inverse_class.divide", _) $ num $
   166                 denom)) thy = 
   219                 denom)) thy = 
   167         SOME (mk_thmid thmid "" 
   220         SOME (mk_thmid thmid "" 
   168             (Print_Mode.setmp [] (Syntax.string_of_term (thy2ctxt thy)) denom) "", 
   221             (Print_Mode.setmp [] 
       
   222               (Syntax.string_of_term (thy2ctxt thy)) denom) "", 
   169 	          Trueprop $ (mk_equality (t, denom)))
   223 	          Trueprop $ (mk_equality (t, denom)))
   170   | eval_get_denominator _ _ _ _ = NONE; 
   224   | eval_get_denominator _ _ _ _ = NONE; 
   171 
       
   172 *}
   225 *}
   173 text {* tests of eval_get_denominator see test/Knowledge/rational.sml*}
   226 text {* tests of eval_get_denominator see test/Knowledge/rational.sml*}
   174 
   227 
   175 text {*get numerator should also become a constant for the isabelle parser: *}
   228 text {*get numerator should also become a constant for the isabelle parser: *}
   176 
   229 
   886 ML {*
   939 ML {*
   887 val Script sc = (#scr o get_met) ["SignalProcessing","Z_Transform","inverse"];
   940 val Script sc = (#scr o get_met) ["SignalProcessing","Z_Transform","inverse"];
   888 atomty sc;
   941 atomty sc;
   889 *}
   942 *}
   890 
   943 
   891 subsubsection {*Stepwise check the program*}
   944 subsubsection {*Stepwise check the program\label{sec:stepcheck}*}
   892 ML {*
   945 ML {*
   893 trace_rewrite := false;
   946 trace_rewrite := false;
   894 trace_script := false; print_depth 9;
   947 trace_script := false; print_depth 9;
   895 val fmz = ["filterExpression (X z = 3 / (z - 1/4 + -1/8 * (1/(z::real))))", 
   948 val fmz = ["filterExpression (X z = 3 / (z - 1/4 + -1/8 * (1/(z::real))))", 
   896   "stepResponse (x[n::real]::bool)"];
   949   "stepResponse (x[n::real]::bool)"];