test/Tools/isac/ADDTESTS/course/SignalProcess/Build_Inverse_Z_Transform.thy
author Walther Neuper <walther.neuper@jku.at>
Fri, 15 May 2020 11:46:43 +0200
changeset 59983 f1fdb213717b
parent 59977 e635534c5f63
child 59984 08296690e7a6
permissions -rw-r--r--
shift code from Specification to appropriate locations
     1 (* Title:  Build_Inverse_Z_Transform
     2    Author: Jan Rocnik
     3    (c) copyright due to license terms.
     4 *)
     5 
     6 theory Build_Inverse_Z_Transform imports Isac.Inverse_Z_Transform
     7   
     8 begin
     9 
    10 text\<open>We stepwise build \ttfamily Inverse\_Z\_Transform.thy \normalfont as an 
    11   exercise. Because Subsection~\ref{sec:stepcheck} requires 
    12   \ttfamily Inverse\_Z\_Transform.thy \normalfont as a subtheory of \ttfamily 
    13   Isac.thy\normalfont, the setup has been changed from \ttfamily theory 
    14   Inverse\_Z\_Transform imports Isac \normalfont to the above one.
    15   \par \noindent
    16   \begin{center} 
    17   \textbf{Attention with the names of identifiers when going into internals!}
    18   \end{center}
    19   Here in this theory there are the internal names twice, for instance we have
    20   \ttfamily (Thm.derivation\_name @{thm rule1} = 
    21   "Build\_Inverse\_Z\_Transform.rule1") = true; \normalfont
    22   but actually in us will be \ttfamily Inverse\_Z\_Transform.rule1 \normalfont
    23 \<close>
    24 
    25 section \<open>Trials towards the Z-Transform\label{sec:trials}\<close>
    26 
    27 ML \<open>val thy = @{theory};\<close>
    28 
    29 subsection \<open>Notations and Terms\<close>
    30 text\<open>\noindent Try which notations we are able to use.\<close>
    31 ML \<open>
    32   @{term "1 < || z ||"};
    33   @{term "z / (z - 1)"};
    34   @{term "-u -n - 1"};
    35   @{term "-u [-n - 1]"}; (*[ ] denotes lists !!!*)
    36   @{term "z /(z - 1) = -u [-n - 1]"};
    37   @{term "1 < || z || ==> z / (z - 1) = -u [-n - 1]"};
    38   UnparseC.term @{term "1 < || z || ==> z / (z - 1) = -u [-n - 1]"};
    39 \<close>
    40 text\<open>\noindent Try which symbols we are able to use and how we generate them.\<close>
    41 ML \<open>
    42   (*alpha -->  "</alpha>" *)
    43   @{term "\<alpha> "};
    44   @{term "\<delta> "};
    45   @{term "\<phi> "};
    46   @{term "\<rho> "};
    47   UnparseC.term @{term "\<rho> "};
    48 \<close>
    49 
    50 subsection \<open>Rules\<close>
    51 (*axiomatization "z / (z - 1) = -u [-n - 1]"
    52   Illegal variable name: "z / (z - 1) = -u [-n - 1]" *)
    53 (*definition     "z / (z - 1) = -u [-n - 1]"
    54   Bad head of lhs: existing constant "op /"*)
    55 axiomatization where 
    56   rule1: "1 = \<delta>[n]" and
    57   rule2: "|| z || > 1 ==> z / (z - 1) = u [n]" and
    58   rule3: "|| z || < 1 ==> z / (z - 1) = -u [-n - 1]" and 
    59   rule4: "|| z || > || \<alpha> || ==> z / (z - \<alpha>) = \<alpha>^^^n * u [n]" and
    60   rule5: "|| z || < || \<alpha> || ==> z / (z - \<alpha>) = -(\<alpha>^^^n) * u [-n - 1]" and
    61   rule6: "|| z || > 1 ==> z/(z - 1)^^^2 = n * u [n]"
    62 
    63 text\<open>\noindent Check the rules for their correct notation. 
    64       (See the machine output.)\<close>
    65 ML \<open>
    66   @{thm rule1};
    67   @{thm rule2};
    68   @{thm rule3};
    69   @{thm rule4};
    70 \<close>
    71 
    72 subsection \<open>Apply Rules\<close>
    73 text\<open>\noindent We try to apply the rules to a given expression.\<close>
    74 
    75 ML \<open>
    76   val inverse_Z = Rule_Set.append_rules "inverse_Z" Rule_Set.empty
    77     [ Thm  ("rule3",ThmC.numerals_to_Free @{thm rule3}),
    78       Thm  ("rule4",ThmC.numerals_to_Free @{thm rule4}),
    79       Thm  ("rule1",ThmC.numerals_to_Free @{thm rule1})   
    80     ];
    81 
    82   val t = TermC.str2term "z / (z - 1) + z / (z - \<alpha>) + 1";
    83   val SOME (t', asm) = Rewrite.rewrite_set_ thy true inverse_Z t;
    84   UnparseC.term t' = "z / (z - ?\<delta> [?n]) + z / (z - \<alpha>) + ?\<delta> [?n]";
    85   (*
    86    * Attention rule1 is applied before the expression is 
    87    * checked for rule4 which would be correct!!!
    88    *)
    89 \<close>
    90 
    91 ML \<open>val (thy, ro, er) = (@{theory}, tless_true, eval_rls);\<close>
    92 ML \<open>
    93   val SOME (t, asm1) = 
    94     Rewrite.rewrite_ thy ro er true (ThmC.numerals_to_Free @{thm rule3}) t;
    95   UnparseC.term t = "- ?u [- ?n - 1] + z / (z - \<alpha>) + 1";
    96   (*- real *)
    97   UnparseC.term t;
    98 
    99   val SOME (t, asm2) = 
   100     Rewrite.rewrite_ thy ro er true (ThmC.numerals_to_Free @{thm rule4}) t;
   101   UnparseC.term t = "- ?u [- ?n - 1] + \<alpha> ^^^ ?n * ?u [?n] + 1";
   102   (*- real *)
   103   UnparseC.term t;
   104 
   105   val SOME (t, asm3) = 
   106     Rewrite.rewrite_ thy ro er true (ThmC.numerals_to_Free @{thm rule1}) t;
   107   UnparseC.term t = "- ?u [- ?n - 1] + \<alpha> ^^^ ?n * ?u [?n] + ?\<delta> [?n]";
   108   (*- real *)
   109   UnparseC.term t;
   110 \<close>
   111 ML \<open>UnparseC.terms (asm1 @ asm2 @ asm3);\<close>
   112 
   113 section\<open>Prepare Steps for TP-based programming Language\label{sec:prepstep}\<close>
   114 text\<open>
   115       \par \noindent The following sections are challenging with the CTP-based 
   116       possibilities of building the program. The goal is realized in 
   117       Section~\ref{spec-meth} and Section~\ref{prog-steps}.
   118       \par The reader is advised to jump between the subsequent subsections and 
   119       the respective steps in Section~\ref{prog-steps}. By comparing 
   120       Section~\ref{sec:calc:ztrans} the calculation can be comprehended step 
   121       by step.
   122 \<close>
   123 
   124 subsection \<open>Prepare Expression\label{prep-expr}\<close>
   125 text\<open>\noindent We try two different notations and check which of them 
   126        Isabelle can handle best.\<close>
   127 ML \<open>
   128   val ctxt = Proof_Context.init_global @{theory};
   129 (*val ctxt = ContextC.declare_constraints' [@{term "z::real"}] ctxt;*)
   130 
   131   val SOME fun1 = 
   132     TermC.parseNEW ctxt "X z = 3 / (z - 1/4 + -1/8 * z ^^^ -1)"; UnparseC.term fun1;
   133   val SOME fun1' = 
   134     TermC.parseNEW ctxt "X z = 3 / (z - 1/4 + -1/8 * (1/z))"; UnparseC.term fun1';
   135 \<close>
   136 
   137 subsubsection \<open>Prepare Numerator and Denominator\<close>
   138 text\<open>\noindent The partial fraction decomposition is only possible if 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.\<close>
   142 
   143 axiomatization where
   144   ruleZY: "(X z = a / b) = (X' z = a / (z * b))"
   145 
   146 ML \<open>
   147   val (thy, ro, er) = (@{theory}, tless_true, eval_rls);
   148   val SOME (fun2, asm1) = 
   149     Rewrite.rewrite_ thy ro er true  @{thm ruleZY} fun1; UnparseC.term fun2;
   150   val SOME (fun2', asm1) = 
   151     Rewrite.rewrite_ thy ro er true  @{thm ruleZY} fun1'; UnparseC.term fun2';
   152 
   153   val SOME (fun3,_) = 
   154     Rewrite.rewrite_set_ @{theory} false norm_Rational fun2;
   155   UnparseC.term fun3;
   156   (*
   157    * Fails on x^^^(-1)
   158    * We solve this problem by using 1/x as a workaround.
   159    *)
   160   val SOME (fun3',_) = 
   161     Rewrite.rewrite_set_ @{theory} false norm_Rational fun2';
   162   UnparseC.term fun3';
   163   (*
   164    * OK - workaround!
   165    *)
   166 \<close>
   167 
   168 subsubsection \<open>Get the Argument of the Expression X'\<close>
   169 text\<open>\noindent We use \texttt{grep} for finding possibilities how we can
   170        extract the bound variable in the expression. \ttfamily Prog_Expr.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 \<close>
   178 
   179 subsubsection\<open>Decompose the Given Term Into lhs and rhs\<close>
   180 ML \<open>
   181   val (_, expr) = HOLogic.dest_eq fun3'; UnparseC.term expr;
   182   val (_, denom) = 
   183     HOLogic.dest_bin "Rings.divide_class.divide" (type_of expr) expr;
   184   UnparseC.term denom = "-1 + -2 * z + 8 * z ^^^ 2";
   185 \<close>
   186 
   187 text\<open>\noindent We have rhs\footnote{Note: lhs means \em Left Hand Side
   188       \normalfont and rhs means \em Right Hand Side \normalfont and indicates
   189       the left or the right part of an equation.} in the Program language, but
   190       we need a function which gets the denominator of a fraction.\<close>
   191 
   192 subsubsection\<open>Get the Denominator and Numerator out of a Fraction\<close>
   193 text\<open>\noindent The self written functions in e.g. \texttt{get\_denominator}
   194        should become a constant for the Isabelle parser:\<close>
   195 
   196 consts
   197   get_denominator :: "real => real"
   198   get_numerator :: "real => real"
   199 
   200 text \<open>\noindent With the above definition we run into problems when we parse
   201         the Program \texttt{InverseZTransform}. This leads to \em ambiguous
   202         parse trees. \normalfont We avoid this by moving the definition
   203         to \ttfamily Rational.thy \normalfont and re-building {\sisac}.
   204         \par \noindent ATTENTION: From now on \ttfamily 
   205         Build\_Inverse\_Z\_Transform \normalfont mimics a build from scratch;
   206         it only works due to re-building {\sisac} several times (indicated 
   207         explicitly).
   208 \<close>
   209 
   210 ML \<open>
   211 (*
   212  *("get_denominator",
   213  *  ("Rational.get_denominator", eval_get_denominator ""))
   214  *)
   215 fun eval_get_denominator (thmid:string) _ 
   216 		      (t as Const ("Rational.get_denominator", _) $
   217               (Const ("Rings.divide_class.divide", _) $num 
   218                 $denom)) thy = 
   219         SOME (TermC.mk_thmid thmid (UnparseC.term_in_thy thy denom) "", 
   220 	        HOLogic.Trueprop $ (TermC.mk_equality (t, denom)))
   221   | eval_get_denominator _ _ _ _ = NONE; 
   222 \<close>
   223 text \<open>\noindent For the tests of \ttfamily eval\_get\_denominator \normalfont
   224         see \ttfamily test/Knowledge/rational.sml\normalfont\<close>
   225 
   226 text \<open>\noindent \ttfamily get\_numerator \normalfont should also become a
   227         constant for the Isabelle parser, follow up the \texttt{const}
   228         declaration above.\<close>
   229 
   230 ML \<open>
   231 (*
   232  *("get_numerator",
   233  *  ("Rational.get_numerator", eval_get_numerator ""))
   234  *)
   235 fun eval_get_numerator (thmid:string) _ 
   236 		      (t as Const ("Rational.get_numerator", _) $
   237               (Const ("Rings.divide_class.divide", _) $num
   238                 $denom )) thy = 
   239         SOME (TermC.mk_thmid thmid (UnparseC.term_in_thy thy num) "", 
   240 	        HOLogic.Trueprop $ (TermC.mk_equality (t, num)))
   241   | eval_get_numerator _ _ _ _ = NONE; 
   242 \<close>
   243 
   244 text \<open>\noindent We discovered several problems by implementing the 
   245        \ttfamily get\_numerator \normalfont function. Remember when 
   246        putting new functions to {\sisac}, put them in a thy file and rebuild 
   247        {\sisac}, also put them in the ruleset for the script!\<close>
   248 
   249 subsection \<open>Solve Equation\label{sec:solveq}\<close>
   250 text \<open>\noindent We have to find the zeros of the term, therefor we use our
   251        \ttfamily get\_denominator \normalfont function from the step before
   252        and try to solve the second order equation. (Follow up the Calculation
   253        in Section~\ref{sec:calc:ztrans} Subproblem 2) Note: This type of
   254        equation is too general for the present program.
   255        \par We know that this equation can be categorized as \em univariate
   256        equation \normalfont and solved with the functions {\sisac} provides
   257        for this equation type. Later on {\sisac} should determine the type
   258        of the given equation self.\<close>
   259 ML \<open>
   260   val denominator = TermC.parseNEW ctxt "z^^^2 - 1/4*z - 1/8 = 0";
   261   val fmz = ["equality (z^^^2 - 1/4*z - 1/8 = (0::real))",
   262              "solveFor z",
   263              "solutions L"];
   264   val (dI',pI',mI') =("Isac_Knowledge", ["univariate","equation"], ["no_met"]);
   265 \<close>
   266 text \<open>\noindent Check if the given equation matches the specification of this
   267         equation type.\<close>
   268 ML \<open>
   269   Model.match_pbl fmz (Problem.from_store ["univariate","equation"]);
   270 \<close>
   271 
   272 text\<open>\noindent We switch up to the {\sisac} Context and try to solve the 
   273        equation with a more specific type definition.\<close>
   274 
   275 ML \<open>
   276   Context.theory_name thy = "Isac_Knowledge";
   277   val denominator = TermC.parseNEW ctxt "-1 + -2 * z + 8 * z ^^^ 2 = 0";
   278   val fmz =                                             (*specification*)
   279     ["equality (-1 + -2 * z + 8 * z ^^^ 2 = (0::real))",(*equality*)
   280      "solveFor z",                                      (*bound variable*)
   281      "solutions L"];                                    (*identifier for
   282                                                           solution*)
   283   val (dI',pI',mI') =
   284     ("Isac_Knowledge", 
   285       ["abcFormula","degree_2","polynomial","univariate","equation"],
   286       ["no_met"]);
   287 \<close>
   288 
   289 text \<open>\noindent Check if the (other) given equation matches the 
   290         specification of this equation type.\<close>
   291         
   292 ML \<open>
   293   Model.match_pbl fmz (Problem.from_store
   294     ["abcFormula","degree_2","polynomial","univariate","equation"]);
   295 \<close>
   296 
   297 text \<open>\noindent We stepwise solve the equation. This is done by the
   298         use of a so called calc tree seen downwards.\<close>
   299 
   300 ML \<open>
   301   val (p,_,f,nxt,_,pt) = CalcTreeTEST [(fmz, (dI',pI',mI'))];
   302   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
   303   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
   304   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
   305   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
   306   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
   307   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
   308   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
   309   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
   310   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
   311   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
   312   val (p,_,f,nxt,_,pt) = me nxt p [] pt;         
   313   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
   314   (*
   315    * nxt =..,Check_elementwise "Assumptions") 
   316    *)
   317   val (p,_,f,nxt,_,pt) = me nxt p [] pt;         
   318   val (p,_,f,nxt,_,pt) = me nxt p [] pt; f2str f;
   319   (*
   320    * [z = 1 / 2, z = -1 / 4]
   321    *)
   322   Test_Tool.show_pt pt; 
   323   val SOME f = parseNEW ctxt "[z=1/2, z=-1/4]";
   324 \<close>
   325 
   326 subsection \<open>Partial Fraction Decomposition\label{sec:pbz}\<close>
   327 text\<open>\noindent We go on with the decomposition of our expression. Follow up the
   328        Calculation in Section~\ref{sec:calc:ztrans} Step~3 and later on
   329        Subproblem~1.\<close>
   330 subsubsection \<open>Solutions of the Equation\<close>
   331 text\<open>\noindent We get the solutions of the before solved equation in a list.\<close>
   332 
   333 ML \<open>
   334   val SOME solutions = parseNEW ctxt "[z=1/2, z=-1/4]";
   335   UnparseC.term solutions;
   336   atomty solutions;
   337 \<close>
   338 
   339 subsubsection \<open>Get Solutions out of a List\<close>
   340 text \<open>\noindent In {\sisac}'s TP-based programming language: 
   341 \begin{verbatim}
   342   let $ $ s_1 = NTH 1 $ solutions; $ s_2 = NTH 2... $
   343 \end{verbatim}
   344        can be useful.
   345 \<close>
   346 
   347 ML \<open>
   348   val Const ("List.list.Cons", _) $ s_1 $ (Const ("List.list.Cons", _)
   349         $ s_2 $ Const ("List.list.Nil", _)) = solutions;
   350   UnparseC.term s_1;
   351   UnparseC.term s_2;
   352 \<close>
   353 
   354 text\<open>\noindent The ansatz for the \em Partial Fraction Decomposition \normalfont
   355       requires to get the denominators of the partial fractions out of the 
   356       Solutions as:
   357       \begin{itemize}
   358         \item $Denominator_{1}=z-Zeropoint_{1}$
   359         \item $Denominator_{2}=z-Zeropoint_{2}$
   360         \item \ldots
   361       \end{itemize}
   362 \<close>
   363       
   364 ML \<open>
   365   val xx = HOLogic.dest_eq s_1;
   366   val s_1' = HOLogic.mk_binop "Groups.minus_class.minus" xx;
   367   val xx = HOLogic.dest_eq s_2;
   368   val s_2' = HOLogic.mk_binop "Groups.minus_class.minus" xx;
   369   UnparseC.term s_1';
   370   UnparseC.term s_2';
   371 \<close>
   372 
   373 text \<open>\noindent For the programming language a function collecting all the 
   374         above manipulations is helpful.\<close>
   375 
   376 ML \<open>
   377   fun fac_from_sol s =
   378     let val (lhs, rhs) = HOLogic.dest_eq s
   379     in HOLogic.mk_binop "Groups.minus_class.minus" (lhs, rhs) end;
   380 \<close>
   381 
   382 ML \<open>
   383   fun mk_prod prod [] =
   384         if prod = TermC.empty
   385         then error "mk_prod called with []" 
   386         else prod
   387     | mk_prod prod (t :: []) =
   388         if prod = TermC.empty
   389         then t
   390         else HOLogic.mk_binop "Groups.times_class.times" (prod, t)
   391     | mk_prod prod (t1 :: t2 :: ts) =
   392           if prod = TermC.empty 
   393           then 
   394              let val p = 
   395                HOLogic.mk_binop "Groups.times_class.times" (t1, t2)
   396              in mk_prod p ts end 
   397           else 
   398              let val p =
   399                HOLogic.mk_binop "Groups.times_class.times" (prod, t1)
   400              in mk_prod p (t2 :: ts) end 
   401 \<close>
   402 (* ML {* 
   403 probably keep these test in test/Tools/isac/...
   404 (*mk_prod TermC.empty [];*)
   405 
   406 val prod = mk_prod TermC.empty [str2term "x + 123"]; 
   407 UnparseC.term prod = "x + 123";
   408 
   409 val sol = str2term "[z = 1 / 2, z = -1 / 4]";
   410 val sols = HOLogic.dest_list sol;
   411 val facs = map fac_from_sol sols;
   412 val prod = mk_prod TermC.empty facs; 
   413 UnparseC.term prod = "(z + -1 * (1 / 2)) * (z + -1 * (-1 / 4))";
   414 
   415 val prod = 
   416   mk_prod TermC.empty [str2term "x + 1", str2term "x + 2", str2term "x + 3"]; 
   417 UnparseC.term prod = "(x + 1) * (x + 2) * (x + 3)";
   418 *} *)
   419 ML \<open>
   420   fun factors_from_solution sol = 
   421     let val ts = HOLogic.dest_list sol
   422     in mk_prod TermC.empty (map fac_from_sol ts) end;
   423 \<close>
   424 (* ML {*
   425 val sol = str2term "[z = 1 / 2, z = -1 / 4]";
   426 val fs = factors_from_solution sol;
   427 UnparseC.term fs = "(z + -1 * (1 / 2)) * (z + -1 * (-1 / 4))"
   428 *} *)
   429 text \<open>\noindent This function needs to be packed such that it can be evaluated
   430         by the Lucas-Interpreter. Therefor we moved the function to the
   431         corresponding \ttfamily Equation.thy \normalfont in our case
   432         \ttfamily PartialFractions.thy \normalfont. The necessary steps
   433         are quit the same as we have done with \ttfamily get\_denominator 
   434         \normalfont before.\<close>
   435 ML \<open>
   436   (*("factors_from_solution",
   437     ("Partial_Fractions.factors_from_solution",
   438       eval_factors_from_solution ""))*)
   439       
   440   fun eval_factors_from_solution (thmid:string) _
   441        (t as Const ("Partial_Fractions.factors_from_solution", _) $ sol)
   442          thy = ((let val prod = factors_from_solution sol
   443                    in SOME (mk_thmid thmid "" (UnparseC.term_in_thy thy prod) "",
   444                          Trueprop $ (mk_equality (t, prod)))
   445                 end)
   446                handle _ => NONE)
   447    | eval_factors_from_solution _ _ _ _ = NONE;
   448 \<close>
   449 
   450 text \<open>\noindent The tracing output of the calc tree after applying this
   451        function was:
   452 \begin{verbatim}
   453   24 / factors_from_solution [z = 1/ 2, z = -1 / 4])]
   454 \end{verbatim}
   455        and the next step:
   456 \begin{verbatim}
   457   val nxt = ("Empty_Tac", ...): tac'_)
   458 \end{verbatim}
   459        These observations indicate, that the Lucas-Interpreter (LIP) 
   460        does not know how to evaluate \ttfamily factors\_from\_solution
   461        \normalfont, so we knew that there is something wrong or missing.
   462 \<close>
   463        
   464 text\<open>\noindent First we isolate the difficulty in the program as follows:
   465 \begin{verbatim}      
   466   " (L_L::bool list) = (SubProblem (PolyEqX,      " ^
   467   "   [abcFormula, degree_2, polynomial,          " ^
   468   "    univariate,equation],                      " ^
   469   "   [no_met])                                   " ^
   470   "   [BOOL equ, REAL zzz]);                      " ^
   471   " (facs::real) = factors_from_solution L_L;     " ^
   472   " (foo::real) = Take facs                       " ^
   473 \end{verbatim}
   474 
   475       \par \noindent And see the tracing output:
   476       
   477 \begin{verbatim}
   478   [(([], Frm), Problem (Isac, [inverse, 
   479                                Z_Transform,
   480                                 SignalProcessing])),
   481    (([1], Frm), X z = 3 / (z - 1 / 4 + -1 / 8 * (1 / z))),
   482    (([1], Res), ?X' z = 3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z)))),
   483    (([2], Res), ?X' z = 24 / (-1 + -2 * z + 8 * z ^^^ 2)),
   484    (([3], Pbl), solve (-1 + -2 * z + 8 * z ^^^ 2 = 0, z)),
   485    (([3,1], Frm), -1 + -2 * z + 8 * z ^^^ 2 = 0),
   486    (([3,1], Res), z = (- -2 + sqrt (-2 ^^^ 2 - 4 * 8 * -1)) / (2 * 8)|
   487                   z = (- -2 - sqrt (-2 ^^^ 2 - 4 * 8 * -1)) / (2 * 8)),
   488    (([3,2], Res), z = 1 / 2 | z = -1 / 4),
   489    (([3,3], Res), [ z = 1 / 2, z = -1 / 4]),
   490    (([3,4], Res), [ z = 1 / 2, z = -1 / 4]),
   491    (([3], Res), [ z = 1 / 2, z = -1 / 4]),
   492    (([4], Frm), factors_from_solution [z = 1 / 2, z = -1 / 4])]
   493 \end{verbatim}      
   494       
   495       \par \noindent In particular that:
   496       
   497 \begin{verbatim}
   498   (([3], Pbl), solve (-1 + -2 * z + 8 * z ^^^ 2 = 0, z)),
   499 \end{verbatim}
   500       \par \noindent Shows the equation which has been created in
   501       the program by: 
   502 \begin{verbatim}
   503   "(denom::real) = get_denominator funterm;      " ^ 
   504     (* get_denominator *)
   505   "(equ::bool) = (denom = (0::real));            " ^
   506 \end{verbatim}
   507         
   508       \noindent \ttfamily get\_denominator \normalfont has been evaluated successfully,
   509       but not\\ \ttfamily factors\_from\_solution.\normalfont
   510       So we stepwise compare with an analogous case, \ttfamily get\_denominator
   511       \normalfont successfully done above: We know that LIP evaluates
   512       expressions in the program by use of the \emph{srls}, so we try to get
   513       the original \emph{srls}.\\
   514 
   515 \begin{verbatim}
   516   val {srls,...} = Method.from_store ["SignalProcessing",
   517                             "Z_Transform",
   518                             "Inverse"];
   519 \end{verbatim}
   520           
   521       \par \noindent Create 2 good example terms:
   522 
   523 \begin{verbatim}
   524 val SOME t1 =
   525   parseNEW ctxt "get_denominator ((111::real) / 222)";
   526 val SOME t2 =
   527   parseNEW ctxt "factors_from_solution [(z::real)=1/2, z=-1/4]";
   528 \end{verbatim}
   529 
   530       \par \noindent Rewrite the terms using srls:\\
   531       \ttfamily \par \noindent rewrite\_set\_ thy true srls t1;\\
   532         rewrite\_set\_ thy true srls t2;\\
   533       \par \noindent \normalfont Now we see a difference: \texttt{t1} gives
   534       \texttt{SOME} but \texttt{t2} gives \texttt{NONE}. We look at the 
   535       \emph{srls}:
   536 \begin{verbatim}
   537   val srls = 
   538     Rule_Set.Repeat{id = "srls_InverseZTransform",
   539         rules = [Eval("Rational.get_numerator",
   540                    eval_get_numerator "Rational.get_numerator"),
   541                  Eval("Partial_Fractions.factors_from_solution",
   542                    eval_factors_from_solution 
   543                      "Partial_Fractions.factors_from_solution")]}
   544 \end{verbatim}                
   545       \par \noindent Here everthing is perfect. So the error can
   546       only be in the SML code of \ttfamily eval\_factors\_from\_solution.
   547       \normalfont We try to check the code with an existing test; since the 
   548       \emph{code} is in 
   549       \begin{center}\ttfamily src/Tools/isac/Knowledge/Partial\_Fractions.thy
   550       \normalfont\end{center}
   551       the \emph{test} should be in
   552       \begin{center}\ttfamily test/Tools/isac/Knowledge/partial\_fractions.sml
   553       \normalfont\end{center}
   554       \par \noindent After updating the function \ttfamily
   555       factors\_from\_solution \normalfont to a new version and putting a
   556       test-case to \ttfamily Partial\_Fractions.sml \normalfont we tried again
   557       to evaluate the term with the same result.
   558       \par We opened the test \ttfamily Test\_Isac.thy \normalfont and saw that
   559       everything is working fine. Also we checked that the test \ttfamily 
   560       partial\_fractions.sml \normalfont is used in \ttfamily Test\_Isac.thy 
   561       \normalfont
   562       \begin{center}use \ttfamily "Knowledge/partial\_fractions.sml"
   563       \normalfont \end{center}
   564       and \ttfamily Partial\_Fractions.thy \normalfont is part is part of
   565       {\sisac} by evaluating
   566 
   567 \begin{verbatim}
   568   val thy = @{theory "Inverse_Z_Transform"};
   569 \end{verbatim}
   570 
   571       After rebuilding {\sisac} again it worked.
   572 \<close>
   573 
   574 subsubsection \<open>Build Expression\<close>
   575 text \<open>\noindent In {\sisac}'s TP-based programming language we can build
   576        expressions by:\\
   577        \ttfamily let s\_1 = Take numerator / (s\_1 * s\_2) \normalfont\<close>
   578        
   579 ML \<open>
   580   (*
   581    * The main denominator is the multiplication of the denominators of
   582    * all partial fractions.
   583    *)
   584    
   585   val denominator' = HOLogic.mk_binop 
   586     "Groups.times_class.times" (s_1', s_2') ;
   587   val SOME numerator = parseNEW ctxt "3::real";
   588 
   589   val expr' = HOLogic.mk_binop
   590     "Rings.divide_class.divide" (numerator, denominator');
   591   UnparseC.term expr';
   592 \<close>
   593 
   594 subsubsection \<open>Apply the Partial Fraction Decomposion Ansatz\<close>
   595 
   596 text\<open>\noindent We use the Ansatz of the Partial Fraction Decomposition for our
   597       expression 2nd order. Follow up the calculation in 
   598       Section~\ref{sec:calc:ztrans} Step~03.\<close>
   599 
   600 ML \<open>Context.theory_name thy = "Isac_Knowledge"\<close>
   601 
   602 text\<open>\noindent We define two axiomatization, the first one is the main ansatz,
   603       the next one is just an equivalent transformation of the resulting
   604       equation. Both axiomatizations were moved to \ttfamily
   605       Partial\_Fractions.thy \normalfont and got their own rulesets. In later
   606       programs it is possible to use the rulesets and the machine will find
   607       the correct ansatz and equivalent transformation itself.\<close>
   608 
   609 axiomatization where
   610   ansatz_2nd_order: "n / (a*b) = A/a + B/b" and
   611   equival_trans_2nd_order: "(n/(a*b) = A/a + B/b) = (n = A*b + B*a)"
   612 
   613 text\<open>\noindent We use our \ttfamily ansatz\_2nd\_order \normalfont to rewrite
   614        our expression and get an equation with our expression on the left
   615        and the partial fractions of it on the right hand side.\<close>
   616   
   617 ML \<open>
   618   val SOME (t1,_) = 
   619     rewrite_ @{theory} e_rew_ord Rule_Set.empty false 
   620              @{thm ansatz_2nd_order} expr';
   621   UnparseC.term t1; atomty t1;
   622   val eq1 = HOLogic.mk_eq (expr', t1);
   623   UnparseC.term eq1;
   624 \<close>
   625 
   626 text\<open>\noindent Eliminate the denominators by multiplying the left and the
   627       right hand side of the equation with the main denominator. This is an
   628       simple equivalent transformation. Later on we use an own ruleset
   629       defined in \ttfamily Partial\_Fractions.thy \normalfont for doing this.
   630       Follow up the calculation in Section~\ref{sec:calc:ztrans} Step~04.\<close>
   631 
   632 ML \<open>
   633   val SOME (eq2,_) = 
   634     rewrite_ @{theory} e_rew_ord Rule_Set.empty false 
   635              @{thm equival_trans_2nd_order} eq1;
   636   UnparseC.term eq2;
   637 \<close>
   638 
   639 text\<open>\noindent We use the existing ruleset \ttfamily norm\_Rational \normalfont 
   640      for simplifications on expressions.\<close>
   641 
   642 ML \<open>
   643   val SOME (eq3,_) = rewrite_set_ @{theory} false norm_Rational eq2;
   644   UnparseC.term eq3;
   645   (*
   646    * ?A ?B not simplified
   647    *)
   648 \<close>
   649 
   650 text\<open>\noindent In Example~\ref{eg:gap} of my thesis I'm describing a problem about
   651       simplifications. The problem that we would like to have only a specific degree
   652       of simplification occurs right here, in the next step.\<close>
   653 
   654 ML \<open>
   655   Rewrite.trace_on := false;
   656   val SOME fract1 =
   657     parseNEW ctxt "(z - 1/2)*(z - -1/4) * (A/(z - 1/2) + B/(z - -1/4))";
   658   (*
   659    * A B !
   660    *)
   661   val SOME (fract2,_) = 
   662     rewrite_set_ @{theory} false norm_Rational fract1;
   663   UnparseC.term fract2 = "(A + -2 * B + 4 * A * z + 4 * B * z) / 4";
   664   (*
   665    * UnparseC.term fract2 = "A * (1 / 4 + z) + B * (-1 / 2 + z)" 
   666    * would be more traditional...
   667    *)
   668 \<close>
   669 
   670 text\<open>\noindent We walk around this problem by generating our new equation first.\<close>
   671 
   672 ML \<open>
   673   val (numerator, denominator) = HOLogic.dest_eq eq3;
   674   val eq3' = HOLogic.mk_eq (numerator, fract1);
   675   (*
   676    * A B !
   677    *)
   678   UnparseC.term eq3';
   679   (*
   680    * MANDATORY: simplify (and remove denominator) otherwise 3 = 0
   681    *)
   682   val SOME (eq3'' ,_) = 
   683     rewrite_set_ @{theory} false norm_Rational eq3';
   684   UnparseC.term eq3'';
   685 \<close>
   686 
   687 text\<open>\noindent Still working at {\sisac}\ldots\<close>
   688 
   689 ML \<open>Context.theory_name thy = "Isac_Knowledge"\<close>
   690 
   691 subsubsection \<open>Build a Rule-Set for the Ansatz\<close>
   692 text \<open>\noindent The \emph{ansatz} rules violate the principle that each
   693        variable on the right-hand-side must also occur on the
   694        left-hand-side of the rule: A, B, etc. don't do that. Thus the
   695        rewriter marks these variables with question marks: ?A, ?B, etc.
   696        These question marks can be dropped by \ttfamily fun
   697        drop\_questionmarks\normalfont.\<close>
   698        
   699 ML \<open>
   700   val ansatz_rls = prep_rls @{theory} (
   701     Rule_Set.Repeat {id = "ansatz_rls", preconds = [], rew_ord = ("dummy_ord",dummy_ord),
   702       erls = Rule_Set.empty, srls = Rule_Set.Empty, calc = [], errpatts = [],
   703       rules = [
   704         Thm ("ansatz_2nd_order",ThmC.numerals_to_Free @{thm ansatz_2nd_order}),
   705         Thm ("equival_trans_2nd_order",ThmC.numerals_to_Free @{thm equival_trans_2nd_order})
   706               ], 
   707       scr = Empty_Prog});
   708 \<close>
   709 
   710 text\<open>\noindent We apply the ruleset\ldots\<close>
   711 
   712 ML \<open>
   713   val SOME (ttttt,_) = 
   714     rewrite_set_ @{theory} false ansatz_rls expr';
   715 \<close>
   716 
   717 text\<open>\noindent And check the output\ldots\<close>
   718 
   719 ML \<open>
   720   UnparseC.term expr' = "3 / ((z - 1 / 2) * (z - -1 / 4))";
   721   UnparseC.term ttttt = "?A / (z - 1 / 2) + ?B / (z - -1 / 4)";
   722 \<close>
   723 
   724 subsubsection \<open>Get the First Coefficient\<close>
   725 
   726 text\<open>\noindent Now it's up to get the two coefficients A and B, which will be
   727       the numerators of our partial fractions. Continue following up the 
   728       Calculation in Section~\ref{sec:calc:ztrans} Subproblem~1.\<close>
   729       
   730 text\<open>\noindent To get the first coefficient we substitute $z$ with the first
   731       zero-point we determined in Section~\ref{sec:solveq}.\<close>
   732 
   733 ML \<open>
   734   val SOME (eq4_1,_) =
   735     rewrite_terms_ @{theory} e_rew_ord Rule_Set.empty [s_1] eq3'';
   736   UnparseC.term eq4_1;
   737   val SOME (eq4_2,_) =
   738     rewrite_set_ @{theory} false norm_Rational eq4_1;
   739   UnparseC.term eq4_2;
   740 
   741   val fmz = ["equality (3=3*A/(4::real))", "solveFor A","solutions L"];
   742   val (dI',pI',mI') =("Isac_Knowledge", ["univariate","equation"], ["no_met"]);
   743   (*
   744    * Solve the simple linear equation for A:
   745    * Return eq, not list of eq's
   746    *)
   747   val (p,_,fa,nxt,_,pt) = CalcTreeTEST [(fmz, (dI',pI',mI'))];
   748   val (p,_,fa,nxt,_,pt) = me nxt p [] pt;
   749     (*Add_Given "equality (3=3*A/4)"*)
   750   val (p,_,fa,nxt,_,pt) = me nxt p [] pt;
   751     (*Add_Given "solveFor A"*)
   752   val (p,_,fa,nxt,_,pt) = me nxt p [] pt; 
   753     (*Add_Find "solutions L"*)
   754   val (p,_,fa,nxt,_,pt) = me nxt p [] pt;
   755     (*Specify_Theory "Isac_Knowledge"*)
   756   val (p,_,fa,nxt,_,pt) = me nxt p [] pt;
   757     (*Specify_Problem ["normalise","polynomial",
   758                        "univariate","equation"])*)
   759   val (p,_,fa,nxt,_,pt) = me nxt p [] pt;
   760     (* Specify_Method["PolyEq","normalise_poly"]*)
   761   val (p,_,fa,nxt,_,pt) = me nxt p [] pt;
   762     (*Apply_Method["PolyEq","normalise_poly"]*)
   763   val (p,_,fa,nxt,_,pt) = me nxt p [] pt;
   764     (*Rewrite ("all_left","PolyEq.all_left")*)
   765   val (p,_,fa,nxt,_,pt) = me nxt p [] pt;
   766     (*Rewrite_Set_Inst(["(''bdv'',A)"],"make_ratpoly_in")*)
   767   val (p,_,fa,nxt,_,pt) = me nxt p [] pt;
   768     (*Rewrite_Set "polyeq_simplify"*)
   769   val (p,_,fa,nxt,_,pt) = me nxt p [] pt; 
   770   val (p,_,fa,nxt,_,pt) = me nxt p [] pt; 
   771   val (p,_,fa,nxt,_,pt) = me nxt p [] pt;
   772     (*Add_Given "equality (3 + -3 / 4 * A =0)"*)
   773   val (p,_,fa,nxt,_,pt) = me nxt p [] pt;
   774     (*Add_Given "solveFor A"*)
   775   val (p,_,fa,nxt,_,pt) = me nxt p [] pt;
   776     (*Add_Find "solutions A_i"*)
   777   val (p,_,fa,nxt,_,pt) = me nxt p [] pt; 
   778   val (p,_,fa,nxt,_,pt) = me nxt p [] pt; 
   779   val (p,_,fa,nxt,_,pt) = me nxt p [] pt; 
   780   val (p,_,fa,nxt,_,pt) = me nxt p [] pt; 
   781     (*Apply_Method ["PolyEq","solve_d1_polyeq_equation"]*)
   782   val (p,_,fa,nxt,_,pt) = me nxt p [] pt;
   783     (*Rewrite_Set_Inst(["(''bdv'',A)"],"d1_polyeq_simplify")*)
   784   val (p,_,fa,nxt,_,pt) = me nxt p [] pt;
   785     (*Rewrite_Set "polyeq_simplify"*)
   786   val (p,_,fa,nxt,_,pt) = me nxt p [] pt;
   787     (*Rewrite_Set "norm_Rational_parenthesized"*)
   788   val (p,_,fa,nxt,_,pt) = me nxt p [] pt;
   789     (*Or_to_List*)
   790   val (p,_,fa,nxt,_,pt) = me nxt p [] pt;
   791     (*Check_elementwise "Assumptions"*)
   792   val (p,_,fa,nxt,_,pt) = me nxt p [] pt;
   793     (*Check_Postcond ["degree_1","polynomial",
   794                       "univariate","equation"]*)
   795   val (p,_,fa,nxt,_,pt) = me nxt p [] pt;
   796     (*Check_Postcond ["normalise","polynomial",
   797                       "univariate","equation"]*)
   798   val (p,_,fa,nxt,_,pt) = me nxt p [] pt;
   799     (*End_Proof'*)
   800   f2str fa;
   801 \<close>
   802 
   803 subsubsection \<open>Get Second Coefficient\<close>
   804 
   805 text\<open>\noindent With the use of \texttt{thy} we check which theories the 
   806       interpreter knows.\<close>
   807 
   808 ML \<open>thy\<close>
   809 
   810 text\<open>\noindent To get the second coefficient we substitute $z$ with the second
   811       zero-point we determined in Section~\ref{sec:solveq}.\<close>
   812 
   813 ML \<open>
   814   val SOME (eq4b_1,_) =
   815     rewrite_terms_ @{theory} e_rew_ord Rule_Set.empty [s_2] eq3'';
   816   UnparseC.term eq4b_1;
   817   val SOME (eq4b_2,_) =
   818     rewrite_set_ @{theory} false norm_Rational eq4b_1;
   819   UnparseC.term eq4b_2;
   820 
   821   val fmz = ["equality (3= -3*B/(4::real))", "solveFor B","solutions L"];
   822   val (dI',pI',mI') =("Isac_Knowledge", ["univariate","equation"], ["no_met"]);
   823   val (p,_,fb,nxt,_,pt) = CalcTreeTEST [(fmz, (dI',pI',mI'))];
   824   val (p,_,fb,nxt,_,pt) = me nxt p [] pt;
   825   val (p,_,fb,nxt,_,pt) = me nxt p [] pt;
   826   val (p,_,fb,nxt,_,pt) = me nxt p [] pt;
   827   val (p,_,fb,nxt,_,pt) = me nxt p [] pt;
   828   val (p,_,fb,nxt,_,pt) = me nxt p [] pt;
   829   val (p,_,fb,nxt,_,pt) = me nxt p [] pt;
   830   val (p,_,fb,nxt,_,pt) = me nxt p [] pt;
   831   val (p,_,fb,nxt,_,pt) = me nxt p [] pt;
   832   val (p,_,fb,nxt,_,pt) = me nxt p [] pt;
   833   val (p,_,fb,nxt,_,pt) = me nxt p [] pt;
   834   val (p,_,fb,nxt,_,pt) = me nxt p [] pt;
   835   val (p,_,fb,nxt,_,pt) = me nxt p [] pt;
   836   val (p,_,fb,nxt,_,pt) = me nxt p [] pt;
   837   val (p,_,fb,nxt,_,pt) = me nxt p [] pt;
   838   val (p,_,fb,nxt,_,pt) = me nxt p [] pt;
   839   val (p,_,fb,nxt,_,pt) = me nxt p [] pt;
   840   val (p,_,fb,nxt,_,pt) = me nxt p [] pt;
   841   val (p,_,fb,nxt,_,pt) = me nxt p [] pt;
   842   val (p,_,fb,nxt,_,pt) = me nxt p [] pt;
   843   val (p,_,fb,nxt,_,pt) = me nxt p [] pt;
   844   val (p,_,fb,nxt,_,pt) = me nxt p [] pt;
   845   val (p,_,fb,nxt,_,pt) = me nxt p [] pt;
   846   val (p,_,fb,nxt,_,pt) = me nxt p [] pt;
   847   val (p,_,fb,nxt,_,pt) = me nxt p [] pt;
   848   val (p,_,fb,nxt,_,pt) = me nxt p [] pt;
   849   val (p,_,fb,nxt,_,pt) = me nxt p [] pt;
   850   val (p,_,fb,nxt,_,pt) = me nxt p [] pt; 
   851   f2str fb;
   852 \<close>
   853 
   854 text\<open>\noindent We compare our results with the pre calculated upshot.\<close>
   855 
   856 ML \<open>
   857   if f2str fa = "[A = 4]" then () else error "part.fract. eq4_1";
   858   if f2str fb = "[B = -4]" then () else error "part.fract. eq4_1";
   859 \<close>
   860 
   861 section \<open>Implement the Specification and the Method \label{spec-meth}\<close>
   862 
   863 text\<open>\noindent Now everything we need for solving the problem has been
   864       tested out. We now start by creating new nodes for our methods and
   865       further on our new program in the interpreter.\<close>
   866 
   867 subsection\<open>Define the Field Descriptions for the 
   868             Specification\label{sec:deffdes}\<close>
   869 
   870 text\<open>\noindent We define the fields \em filterExpression \normalfont and
   871       \em stepResponse \normalfont both as equations, they represent the in- and
   872       output of the program.\<close>
   873 
   874 consts
   875   filterExpression  :: "bool => una"
   876   stepResponse      :: "bool => una"
   877 
   878 subsection\<open>Define the Specification\<close>
   879 
   880 text\<open>\noindent The next step is defining the specifications as nodes in the
   881       designated part. We have to create the hierarchy node by node and start
   882       with \em SignalProcessing \normalfont and go on by creating the node
   883       \em Z\_Transform\normalfont.\<close>
   884 
   885 setup \<open>KEStore_Elems.add_pbts
   886   [Problem.prep_input thy "pbl_SP" [] Problem.id_empty (["SignalProcessing"], [], Rule_Set.empty, NONE, []),
   887     Problem.prep_input thy "pbl_SP_Ztrans" [] Problem.id_empty
   888       (["Z_Transform","SignalProcessing"], [], Rule_Set.empty, NONE, [])]\<close>
   889 
   890 text\<open>\noindent For the suddenly created node we have to define the input
   891        and output parameters. We already prepared their definition in
   892        Section~\ref{sec:deffdes}.\<close>
   893 
   894 setup \<open>KEStore_Elems.add_pbts
   895   [Problem.prep_input thy "pbl_SP_Ztrans_inv" [] Problem.id_empty
   896       (["Inverse", "Z_Transform", "SignalProcessing"],
   897         [("#Given" ,["filterExpression X_eq"]),
   898           ("#Find", ["stepResponse n_eq"])],
   899         Rule_Set.append_rules "empty" Rule_Set.empty [(*for preds in where_*)],
   900         NONE,
   901         [["SignalProcessing","Z_Transform","Inverse"]])]\<close>
   902 ML \<open>
   903   Test_Tool.show_ptyps() ();
   904   Problem.from_store ["Inverse","Z_Transform","SignalProcessing"];
   905 \<close>
   906 
   907 subsection \<open>Define Name and Signature for the Method\<close>
   908 
   909 text\<open>\noindent As a next step we store the definition of our new method as a
   910       constant for the interpreter.\<close>
   911 
   912 consts
   913   InverseZTransform :: "[bool, bool] => bool"
   914     ("((Program InverseZTransform (_ =))// (_))" 9)
   915 
   916 subsection \<open>Setup Parent Nodes in Hierarchy of Method\label{sec:cparentnode}\<close>
   917 
   918 text\<open>\noindent Again we have to generate the nodes step by step, first the
   919       parent node and then the originally \em Z\_Transformation 
   920       \normalfont node. We have to define both nodes first with an empty script
   921       as content.\<close>
   922 
   923 setup \<open>KEStore_Elems.add_mets
   924   [Method.prep_input thy "met_SP" [] e_metID
   925       (["SignalProcessing"], [],
   926         {rew_ord'="tless_true", rls'= Rule_Set.empty, calc = [], srls = Rule_Set.empty, prls = Rule_Set.empty, crls = Rule_Set.empty,
   927           errpats = [], nrls = Rule_Set.empty},
   928         "empty_script"),
   929     Method.prep_input thy "met_SP_Ztrans" [] e_metID
   930       (["SignalProcessing", "Z_Transform"], [],
   931         {rew_ord'="tless_true", rls'= Rule_Set.empty, calc = [], srls = Rule_Set.empty, prls = Rule_Set.empty, crls = Rule_Set.empty,
   932           errpats = [], nrls = Rule_Set.empty},
   933         "empty_script")]
   934 \<close>
   935 
   936 text\<open>\noindent After we generated both nodes, we can fill the containing
   937       script we want to implement later. First we define the specifications
   938       of the script in e.g. the in- and output.\<close>
   939 
   940 setup \<open>KEStore_Elems.add_mets
   941   [Method.prep_input thy "met_SP_Ztrans_inv" [] e_metID
   942       (["SignalProcessing", "Z_Transform", "Inverse"], 
   943         [("#Given" ,["filterExpression X_eq", "boundVariable X_z"]),
   944           ("#Find", ["stepResponse n_eq"])],
   945         {rew_ord'="tless_true", rls'= Rule_Set.empty, calc = [], srls = Rule_Set.empty, prls = Rule_Set.empty, crls = Rule_Set.empty,
   946           errpats = [], nrls = Rule_Set.empty},
   947         "empty_script")]
   948 \<close>
   949 
   950 text\<open>\noindent After we stored the definition we can start implementing the
   951       script itself. As a first try we define only three rows containing one
   952       simple operation.\<close>
   953 
   954 setup \<open>KEStore_Elems.add_mets
   955   [Method.prep_input thy "met_SP_Ztrans_inv" [] e_metID
   956       (["SignalProcessing", "Z_Transform", "Inverse"], 
   957         [("#Given" , ["filterExpression X_eq", "boundVariable X_z"]),
   958           ("#Find", ["stepResponse n_eq"])],
   959         {rew_ord'="tless_true", rls'= Rule_Set.empty, calc = [], srls = Rule_Set.empty, prls = Rule_Set.empty, crls = Rule_Set.empty,
   960           errpats = [], nrls = Rule_Set.empty},
   961         "Program InverseZTransform (Xeq::bool) =" ^ (*TODO boundVariable X_z*)
   962           " (let X = Take Xeq;" ^
   963           "      X = Rewrite ruleZY X" ^
   964           "  in X)")]
   965 \<close>
   966 
   967 text\<open>\noindent Check if the method has been stored correctly\ldots\<close>
   968 
   969 ML \<open>
   970   show_mets(); 
   971 \<close>
   972 
   973 text\<open>\noindent If yes we can get the method by stepping backwards through
   974       the hierarchy.\<close>
   975 
   976 ML \<open>
   977   Method.from_store ["SignalProcessing","Z_Transform","Inverse"];
   978 \<close>
   979 
   980 section \<open>Program in TP-based language \label{prog-steps}\<close>
   981 
   982 text\<open>\noindent We start stepwise expanding our program. The script is a
   983       simple string containing several manipulation instructions.
   984       \par The first script we try contains no instruction, we only test if
   985       building scripts that way work.\<close>
   986 
   987 subsection \<open>Stepwise Extend the Program\<close>
   988 ML \<open>
   989   val str = 
   990     "Program InverseZTransform (Xeq::bool) =                          "^
   991     " Xeq";
   992 \<close>
   993 
   994 text\<open>\noindent Next we put some instructions in the script and try if we are
   995       able to solve our first equation.\<close>
   996 
   997 ML \<open>
   998   val str = 
   999     "Program InverseZTransform (Xeq::bool) =                          "^
  1000     (*
  1001      * 1/z) instead of z ^^^ -1
  1002      *)
  1003     " (let X = Take Xeq;                                             "^
  1004     "      X' = Rewrite ruleZY False X;                              "^
  1005     (*
  1006      * z * denominator
  1007      *)
  1008     "      X' = (Rewrite_Set norm_Rational False) X'                 "^
  1009     (*
  1010      * simplify
  1011      *)
  1012     "  in X)";
  1013     (*
  1014      * NONE
  1015      *)
  1016     "Program InverseZTransform (Xeq::bool) =                          "^
  1017     (*
  1018      * (1/z) instead of z ^^^ -1
  1019      *)
  1020     " (let X = Take Xeq;                                             "^
  1021     "      X' = Rewrite ruleZY False X;                              "^
  1022     (*
  1023      * z * denominator
  1024      *)
  1025     "      X' = (Rewrite_Set norm_Rational False) X';                "^
  1026     (*
  1027      * simplify
  1028      *)
  1029     "      X' = (SubProblem (IsacX,[pqFormula,degree_2,              "^
  1030     "                               polynomial,univariate,equation], "^
  1031     "                              [no_met])                         "^
  1032     "                              [BOOL e_e, REAL v_v])             "^
  1033     "            in X)";
  1034 \<close>
  1035 
  1036 text\<open>\noindent To solve the equation it is necessary to drop the left hand
  1037       side, now we only need the denominator of the right hand side. The first
  1038       equation solves the zeros of our expression.\<close>
  1039 
  1040 ML \<open>
  1041   val str = 
  1042     "Program InverseZTransform (Xeq::bool) =                          "^
  1043     " (let X = Take Xeq;                                             "^
  1044     "      X' = Rewrite ruleZY False X;                              "^
  1045     "      X' = (Rewrite_Set norm_Rational False) X';                "^
  1046     "      funterm = rhs X'                                          "^
  1047     (*
  1048      * drop X'= for equation solving
  1049      *)
  1050     "  in X)";
  1051 \<close>
  1052 
  1053 text\<open>\noindent As mentioned above, we need the denominator of the right hand
  1054       side. The equation itself consists of this denominator and tries to find
  1055       a $z$ for this the denominator is equal to zero.\<close>
  1056 
  1057 text \<open> dropped during switch from Program to partial_function:
  1058   val str = 
  1059     "Program InverseZTransform (X_eq::bool) =                         "^
  1060     " (let X = Take X_eq;                                            "^
  1061     "      X' = Rewrite ruleZY False X;                              "^
  1062     "      X' = (Rewrite_Set norm_Rational False) X';                "^
  1063     "      (X'_z::real) = lhs X';                                    "^
  1064     "      (z::real) = argument_in X'_z;                             "^
  1065     "      (funterm::real) = rhs X';                                 "^
  1066     "      (denom::real) = get_denominator funterm;                  "^
  1067     (*
  1068      * get_denominator
  1069      *)
  1070     "      (equ::bool) = (denom = (0::real));                        "^
  1071     "      (L_L::bool list) =                                        "^
  1072     "            (SubProblem (Test,                                 "^
  1073     "                         [LINEAR,univariate,equation,test],     "^
  1074     "                         [Test,solve_linear])                   "^
  1075     "                         [BOOL equ, REAL z])                    "^
  1076     "  in X)";
  1077 
  1078   parse thy str;
  1079   val sc = (inst_abs o Thm.term_of o the o (parse thy)) str;
  1080   atomty sc;
  1081 \<close>
  1082 
  1083 text \<open>\noindent This ruleset contains all functions that are in the script; 
  1084        The evaluation of the functions is done by rewriting using this ruleset.\<close>
  1085 
  1086 ML \<open>
  1087   val srls = 
  1088     Rule_Set.Repeat {id="srls_InverseZTransform", 
  1089          preconds = [],
  1090          rew_ord = ("termlessI",termlessI),
  1091          erls = Rule_Set.append_rules "erls_in_srls_InverseZTransform" Rule_Set.empty
  1092            [(*for asm in NTH_CONS ...*)
  1093             Eval ("Orderings.ord_class.less",eval_equ "#less_"),
  1094             (*2nd NTH_CONS pushes n+-1 into asms*)
  1095             Eval("Groups.plus_class.plus", eval_binop "#add_")
  1096            ], 
  1097          srls = Rule_Set.Empty, calc = [], errpatts = [],
  1098          rules = [
  1099                   Thm ("NTH_CONS",ThmC.numerals_to_Free @{thm NTH_CONS}),
  1100                   Eval("Groups.plus_class.plus", eval_binop "#add_"),
  1101                   Thm ("NTH_NIL",ThmC.numerals_to_Free @{thm NTH_NIL}),
  1102                   Eval("Prog_Expr.lhs", eval_lhs"eval_lhs_"),
  1103                   Eval("Prog_Expr.rhs", eval_rhs"eval_rhs_"),
  1104                   Eval("Prog_Expr.argument'_in", eval_argument_in "Prog_Expr.argument'_in"),
  1105                   Eval("Rational.get_denominator", eval_get_denominator "#get_denominator"),
  1106                   Eval("Rational.get_numerator", eval_get_numerator "#get_numerator"),
  1107                   Eval("Partial_Fractions.factors_from_solution",
  1108                        eval_factors_from_solution "#factors_from_solution"),
  1109                   Eval("Partial_Fractions.drop_questionmarks",
  1110                        eval_drop_questionmarks "#drop_?")
  1111                  ],
  1112          scr = Empty_Prog};
  1113 \<close>
  1114 
  1115 
  1116 subsection \<open>Store Final Version of Program for Execution\<close>
  1117 
  1118 text\<open>\noindent After we also tested how to write scripts and run them,
  1119       we start creating the final version of our script and store it into
  1120       the method for which we created a node in Section~\ref{sec:cparentnode}
  1121       Note that we also did this stepwise, but we didn't kept every
  1122       subversion.\<close>
  1123 
  1124 setup \<open>KEStore_Elems.add_mets
  1125   [Method.prep_input thy "met_SP_Ztrans_inv" [] e_metID
  1126       (["SignalProcessing", "Z_Transform", "Inverse"], 
  1127         [("#Given" , ["filterExpression X_eq"]), (*TODO boundVariable X_z*)
  1128           ("#Find", ["stepResponse n_eq"])],
  1129         {rew_ord'="tless_true", rls'= Rule_Set.empty, calc = [], srls = srls, prls = Rule_Set.empty, crls = Rule_Set.empty,
  1130           errpats = [], nrls = Rule_Set.empty},
  1131         "Program InverseZTransform (X_eq::bool) =                        "^
  1132           (*(1/z) instead of z ^^^ -1*)
  1133           "(let X = Take X_eq;                                            "^
  1134           "      X' = Rewrite ruleZY False X;                             "^
  1135           (*z * denominator*)
  1136           "      (num_orig::real) = get_numerator (rhs X');               "^
  1137           "      X' = (Rewrite_Set norm_Rational False) X';               "^
  1138           (*simplify*)
  1139           "      (X'_z::real) = lhs X';                                   "^
  1140           "      (zzz::real) = argument_in X'_z;                          "^
  1141           "      (funterm::real) = rhs X';                                "^
  1142           (*drop X' z = for equation solving*)
  1143           "      (denom::real) = get_denominator funterm;                 "^
  1144           (*get_denominator*)
  1145           "      (num::real) = get_numerator funterm;                     "^
  1146           (*get_numerator*)
  1147           "      (equ::bool) = (denom = (0::real));                       "^
  1148           "      (L_L::bool list) = (SubProblem (PolyEqX,                 "^
  1149           "         [abcFormula,degree_2,polynomial,univariate,equation], "^
  1150           "         [no_met])                                             "^
  1151           "         [BOOL equ, REAL zzz]);                                "^
  1152           "      (facs::real) = factors_from_solution L_L;                "^
  1153           "      (eql::real) = Take (num_orig / facs);                    "^ 
  1154 
  1155           "      (eqr::real) = (Try (Rewrite_Set ansatz_rls False)) eql;  "^
  1156 
  1157           "      (eq::bool) = Take (eql = eqr);                           "^
  1158           (*Maybe possible to use HOLogic.mk_eq ??*)
  1159           "      eq = (Try (Rewrite_Set equival_trans False)) eq;         "^ 
  1160 
  1161           "      eq = drop_questionmarks eq;                              "^
  1162           "      (z1::real) = (rhs (NTH 1 L_L));                          "^
  1163           (* 
  1164           * prepare equation for a - eq_a
  1165           * therefor substitute z with solution 1 - z1
  1166           *)
  1167           "      (z2::real) = (rhs (NTH 2 L_L));                          "^
  1168 
  1169           "      (eq_a::bool) = Take eq;                                  "^
  1170           "      eq_a = (Substitute [zzz=z1]) eq;                         "^
  1171           "      eq_a = (Rewrite_Set norm_Rational False) eq_a;           "^
  1172           "      (sol_a::bool list) =                                     "^
  1173           "                 (SubProblem (IsacX,                           "^
  1174           "                              [univariate,equation],[no_met])  "^
  1175           "                              [BOOL eq_a, REAL (A::real)]);    "^
  1176           "      (a::real) = (rhs(NTH 1 sol_a));                          "^
  1177 
  1178           "      (eq_b::bool) = Take eq;                                  "^
  1179           "      eq_b =  (Substitute [zzz=z2]) eq_b;                      "^
  1180           "      eq_b = (Rewrite_Set norm_Rational False) eq_b;           "^
  1181           "      (sol_b::bool list) =                                     "^
  1182           "                 (SubProblem (IsacX,                           "^
  1183           "                              [univariate,equation],[no_met])  "^
  1184           "                              [BOOL eq_b, REAL (B::real)]);    "^
  1185           "      (b::real) = (rhs(NTH 1 sol_b));                          "^
  1186 
  1187           "      eqr = drop_questionmarks eqr;                            "^
  1188           "      (pbz::real) = Take eqr;                                  "^
  1189           "      pbz = ((Substitute [A=a, B=b]) pbz);                     "^
  1190 
  1191           "      pbz = Rewrite ruleYZ False pbz;                          "^
  1192           "      pbz = drop_questionmarks pbz;                            "^
  1193 
  1194           "      (X_z::bool) = Take (X_z = pbz);                          "^
  1195           "      (n_eq::bool) = (Rewrite_Set inverse_z False) X_z;        "^
  1196           "      n_eq = drop_questionmarks n_eq                           "^
  1197           "in n_eq)")]
  1198 \<close>
  1199 
  1200 
  1201 subsection \<open>Check the Program\<close>
  1202 text\<open>\noindent When the script is ready we can start checking our work.\<close>
  1203 subsubsection \<open>Check the Formalization\<close>
  1204 text\<open>\noindent First we want to check the formalization of the in and
  1205        output of our program.\<close>
  1206 
  1207 ML \<open>
  1208   val fmz = 
  1209     ["filterExpression (X  = 3 / (z - 1/4 + -1/8 * (1/(z::real))))",
  1210      "stepResponse (x[n::real]::bool)"];
  1211   val (dI,pI,mI) = 
  1212     ("Isac_Knowledge", ["Inverse", "Z_Transform", "SignalProcessing"], 
  1213              ["SignalProcessing","Z_Transform","Inverse"]);
  1214 
  1215   val ([
  1216           (
  1217             1,
  1218             [1],
  1219             "#Given",
  1220             Const ("Inverse_Z_Transform.filterExpression", _),
  1221             [Const ("HOL.eq", _) $ _ $ _]
  1222           ),
  1223           (
  1224             2,
  1225             [1],
  1226             "#Find",
  1227             Const ("Inverse_Z_Transform.stepResponse", _),
  1228             [Free ("x", _) $ _]
  1229           )
  1230        ],_
  1231       ) = O_Model.init fmz thy ((#ppc o Problem.from_store) pI);
  1232 
  1233   val Prog sc 
  1234     = (#scr o Method.from_store) ["SignalProcessing",
  1235                         "Z_Transform",
  1236                         "Inverse"];
  1237   atomty sc;
  1238 \<close>
  1239 
  1240 subsubsection \<open>Stepwise Check the Program\label{sec:stepcheck}\<close>
  1241 text\<open>\noindent We start to stepwise execute our new program in a calculation
  1242       tree and check if every node implements that what we have wanted.\<close>
  1243       
  1244 ML \<open>
  1245   Rewrite.trace_on := false; (*true*)
  1246   LItool.trace_on := false; (*true*)
  1247   print_depth 9;
  1248   
  1249   val fmz =
  1250     ["filterExpression (X z = 3 / ((z::real) + 1/10 - 1/50*(1/z)))",
  1251      "stepResponse (x[n::real]::bool)"];
  1252      
  1253   val (dI,pI,mI) =
  1254     ("Isac_Knowledge", ["Inverse", "Z_Transform", "SignalProcessing"], 
  1255              ["SignalProcessing","Z_Transform","Inverse"]);
  1256              
  1257   val (p,_,f,nxt,_,pt)  = CalcTreeTEST [(fmz, (dI,pI,mI))];
  1258   val (p,_,f,nxt,_,pt) = me nxt p [] pt; 
  1259     "Add_Given";
  1260   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1261     "Add_Find";
  1262   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1263     "Specify_Theory";
  1264   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1265     "Specify_Problem";
  1266   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1267     "Specify_Method";
  1268   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1269     "Apply_Method";
  1270   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1271     "Rewrite (ruleZY, Inverse_Z_Transform.ruleZY)";
  1272     "--> X z = 3 / (z - 1 / 4 + -1 / 8 * (1 / z))";
  1273   (*
  1274    * TODO naming!
  1275    *)
  1276   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1277     "Rewrite_Set norm_Rational";
  1278     " --> X' z = 3 / (z * (z - 1 / 4 + -1 / 8 * (1 / z)))";
  1279   val (p,_,f,nxt,_,pt) = me nxt p [] pt; 
  1280     "SubProblem";
  1281   print_depth 3;
  1282 \<close>
  1283 
  1284 text \<open>\noindent Instead of \ttfamily nxt = Subproblem \normalfont above we had
  1285        \ttfamily Empty\_Tac; \normalfont the search for the reason considered
  1286        the following points:\begin{itemize}
  1287        \item What shows \ttfamily show\_pt pt;\normalfont\ldots?
  1288 \begin{verbatim}(([2], Res), ?X' z = 24 / (-1 + -2 * z + 8 * z ^^^ 2))]\end{verbatim}
  1289          The calculation is ok but no \ttfamily next \normalfont step found:
  1290          Should be\\ \ttfamily nxt = Subproblem\normalfont!
  1291        \item What shows \ttfamily trace\_script := true; \normalfont we read
  1292          bottom up\ldots
  1293      \begin{verbatim}
  1294      @@@next leaf 'SubProblem
  1295      (PolyEq',[abcFormula, degree_2, polynomial, 
  1296                univariate, equation], no_meth)
  1297      [BOOL equ, REAL z]' 
  1298        ---> Program.Tac 'SubProblem (PolyEq',
  1299               [abcFormula, degree_2, polynomial,
  1300                univariate, equation], no_meth)
  1301      [BOOL (-1 + -2 * z + 8 * z \^\^\^ ~2 = 0), REAL z]'
  1302      \end{verbatim}
  1303          We see the SubProblem with correct arguments from searching next
  1304          step (program text !!!--->!!! Program.Tac (script tactic) with arguments
  1305          evaluated.)
  1306      \item Do we have the right Program \ldots difference in the
  1307          arguments in the arguments\ldots
  1308          \begin{verbatim}
  1309      val Prog s =
  1310      (#scr o Method.from_store) ["SignalProcessing",
  1311                        "Z_Transform",
  1312                        "Inverse"];
  1313      writeln (UnparseC.term s);
  1314          \end{verbatim}
  1315          \ldots shows the right script. Difference in the arguments.
  1316      \item Test --- Why helpless here ? --- shows: \ttfamily replace
  1317          no\_meth by [no\_meth] \normalfont in Program
  1318      \end{itemize}
  1319 \<close>
  1320 
  1321 ML \<open>
  1322   val (p,_,f,nxt,_,pt) = me nxt p [] pt; 
  1323     (*Model_Problem";*)
  1324 \<close>
  1325 
  1326 text \<open>\noindent Instead of \ttfamily nxt = Model\_Problem \normalfont above
  1327        we had \ttfamily Empty\_Tac; \normalfont the search for the reason 
  1328        considered the following points:\begin{itemize}
  1329        \item Difference in the arguments
  1330        \item Comparison with Subsection~\ref{sec:solveq}: There solving this
  1331          equation works, so there must be some difference in the arguments
  1332          of the Subproblem: RIGHT: we had \ttfamily [no\_meth] \normalfont
  1333          instead of \ttfamily [no\_met] \normalfont ;-)
  1334        \end{itemize}\<close>
  1335 
  1336 ML \<open>
  1337   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1338     (*Add_Given equality (-1 + -2 * z + 8 * z ^^^ 2 = 0)";*)
  1339   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1340     (*Add_Given solveFor z";*)
  1341   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1342     (*Add_Find solutions z_i";*)
  1343   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1344     (*Specify_Theory Isac";*)
  1345 \<close>
  1346 
  1347 text \<open>\noindent We had \ttfamily nxt = Empty\_Tac instead Specify\_Theory;
  1348        \normalfont The search for the reason considered the following points:
  1349        \begin{itemize}
  1350        \item Was there an error message? NO -- ok
  1351        \item Has \ttfamily nxt = Add\_Find \normalfont been inserted in pt:\\
  1352          \ttfamily get\_obj g\_pbl pt (fst p);\normalfont? YES -- ok
  1353        \item What is the returned formula:
  1354 \begin{verbatim}
  1355 print_depth 999; f; print_depth 3;
  1356 { Find = [ Correct "solutions z_i"],
  1357   With = [],
  1358   Given = [Correct "equality (-1 + -2*z + 8*z ^^^ 2 = 0)",
  1359            Correct "solveFor z"],
  1360   Where = [...],
  1361   Relate = [] }
  1362 \end{verbatim}
  1363      \normalfont The only False is the reason: the Where (the precondition) is
  1364      False for good reasons: The precondition seems to check for linear
  1365      equations, not for the one we want to solve! Removed this error by
  1366      correcting the Program from \ttfamily SubProblem (PolyEq',
  1367      \lbrack linear,univariate,equation,
  1368        test\rbrack, \lbrack Test,solve\_linear\rbrack \normalfont to
  1369      \ttfamily SubProblem (PolyEq',\\ \lbrack abcFormula,degree\_2,
  1370        polynomial,univariate,equation\rbrack,\\
  1371                    \lbrack PolyEq,solve\_d2\_polyeq\_abc\_equation
  1372                    \rbrack\normalfont
  1373      You find the appropriate type of equation at the
  1374      {\sisac}-WEB-Page\footnote{
  1375      \href{http://www.ist.tugraz.at/projects/isac/www/kbase/pbl/index\_pbl.html}
  1376           {http://www.ist.tugraz.at/projects/isac/www/kbase/pbl/index\_pbl.html}
  1377                                }
  1378      And the respective method in \ttfamily Knowledge/PolyEq.thy \normalfont
  1379      at the respective \ttfamily store\_pbt. \normalfont Or you leave the
  1380      selection of the appropriate type to isac as done in the final Program ;-))
  1381   \end{itemize}\<close>
  1382   
  1383 ML \<open>
  1384   val (p,_,f,nxt,_,pt) = me nxt p [] pt; 
  1385     (*Specify_Problem [abcFormula,...";*)
  1386   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1387     (*Specify_Method [PolyEq,solve_d2_polyeq_abc_equation";*)
  1388   val (p,_,f,nxt,_,pt) = me nxt p [] pt; 
  1389     (*Apply_Method [PolyEq,solve_d2_polyeq_abc_equation";*)
  1390   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1391     (*Rewrite_Set_Inst [(''bdv'', z)], d2_polyeq_abcFormula_simplify";*)
  1392   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1393   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1394   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1395   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1396   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1397   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1398   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1399   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1400   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1401   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1402   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1403   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1404   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1405   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1406   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1407   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1408   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1409     (*Specify_Problem ["normalise","polynomial","univariate","equation"]*)
  1410   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1411     (*Specify_Method ["PolyEq", "normalise_poly"]*)
  1412   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1413     (*Apply_Method ["PolyEq", "normalise_poly"]*)
  1414   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1415     (*Rewrite ("all_left", "PolyEq.all_left")*)
  1416   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1417     (*Rewrite_Set_Inst (["(''bdv'', A)"], "make_ratpoly_in")*)
  1418   val (p,_,f,nxt,_,pt) = me nxt p [] pt; 
  1419     (*Rewrite_Set "polyeq_simplify"*)
  1420   val (p,_,f,nxt,_,pt) = me nxt p [] pt; 
  1421     (*Subproblem("Isac_Knowledge",["degree_1","polynomial","univariate","equation"])*)
  1422   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1423   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1424   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1425   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1426   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1427   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1428   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1429   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1430   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1431   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1432   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1433   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1434   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1435   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1436   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1437   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1438   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1439   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1440   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1441   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1442   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1443   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1444   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1445   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1446   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1447   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1448   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1449   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1450   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1451   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1452   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1453   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1454   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1455   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1456   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1457   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1458   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1459   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1460   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1461   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1462   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1463   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1464   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1465   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1466   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1467   val (p,_,f,nxt,_,pt) = me nxt p [] pt; (*([11, 4, 5], Res) Check_Postcond*)
  1468   val (p,_,f,nxt,_,pt) = me nxt p [] pt; (*([11, 4], Res) Check_Postcond*)
  1469   val (p,_,f,nxt,_,pt) = me nxt p [] pt; (*([11], Res) Take*)
  1470   val (p,_,f,nxt,_,pt) = me nxt p [] pt; (*([12], Frm) Substitute*)
  1471   val (p,_,f,nxt,_,pt) = me nxt p [] pt; (*([12], Res) Rewrite*)
  1472   val (p,_,f,nxt,_,pt) = me nxt p [] pt; (*([13], Res) Take*)
  1473   val (p,_,f,nxt,_,pt) = me nxt p [] pt; (*([14], Frm) Empty_Tac*)
  1474 \<close>
  1475 ML \<open>
  1476   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1477 \<close>
  1478 ML \<open>
  1479   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1480 \<close>
  1481 ML \<open>
  1482   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1483 \<close>
  1484 ML \<open>
  1485   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1486 \<close>
  1487 ML \<open>
  1488   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1489 \<close>
  1490 ML \<open>
  1491   val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1492 \<close>
  1493 
  1494 ML \<open>
  1495 LItool.trace_on := true;
  1496 \<close>
  1497 ML \<open>
  1498 val (p,_,f,nxt,_,pt) = me nxt p [] pt;
  1499 \<close>
  1500 ML \<open>
  1501 Test_Tool.show_pt pt;
  1502 \<close>
  1503 ML \<open>
  1504 \<close> 
  1505 ML \<open>
  1506 \<close> 
  1507 ML \<open>
  1508 \<close> 
  1509 ML \<open>
  1510 \<close> 
  1511 ML \<open>
  1512 \<close> 
  1513 ML \<open>
  1514 \<close> 
  1515 
  1516 text\<open>\noindent As a last step we check the tracing output of the last calc
  1517       tree instruction and compare it with the pre-calculated result.\<close>
  1518 
  1519 section \<open>Improve and Transfer into Knowledge\<close>
  1520 text \<open>
  1521   We want to improve the very long program \ttfamily InverseZTransform
  1522   \normalfont by modularisation: partial fraction decomposition shall
  1523   become a sub-problem.
  1524 
  1525   We could transfer all knowledge in \ttfamily Build\_Inverse\_Z\_Transform.thy 
  1526   \normalfont first to the \ttfamily Knowledge/Inverse\_Z\_Transform.thy 
  1527   \normalfont and then modularise. In this case TODO problems?!?
  1528 
  1529   We chose another way and go bottom up: first we build the sub-problem in
  1530   \ttfamily Partial\_Fractions.thy \normalfont with the term:
  1531 
  1532       $$\frac{3}{x\cdot(z - \frac{1}{4} + \frac{-1}{8}\cdot\frac{1}{z})}$$
  1533 
  1534   \noindent (how this still can be improved see \ttfamily Partial\_Fractions.thy\normalfont),
  1535   and re-use all stuff prepared in \ttfamily Build\_Inverse\_Z\_Transform.thy:
  1536   \normalfont The knowledge will be transferred to \ttfamily src/../Partial\_Fractions.thy 
  1537   \normalfont and the respective tests to:
  1538   \begin{center}\ttfamily test/../sartial\_fractions.sml\normalfont\end{center}
  1539 \<close>
  1540 
  1541 subsection \<open>Transfer to Partial\_Fractions.thy\<close>
  1542 text \<open>
  1543   First we transfer both, knowledge and tests into:
  1544   \begin{center}\ttfamily src/../Partial\_Fractions.thy\normalfont\end{center}
  1545   in order to immediately have the test results.
  1546 
  1547   We copy \ttfamily factors\_from\_solution, drop\_questionmarks,\\
  1548   ansatz\_2nd\_order \normalfont and rule-sets --- no problem.
  1549   
  1550   Also \ttfamily store\_pbt ..\\ "pbl\_simp\_rat\_partfrac"
  1551   \normalfont is easy.
  1552 
  1553   But then we copy from:\\
  1554   (1) \ttfamily Build\_Inverse\_Z\_Transform.thy store\_met\ldots "met\_SP\_Ztrans\_inv"
  1555   \normalfont\\ to\\ 
  1556   (2) \ttfamily Partial\_Fractions.thy store\_met\ldots "met\_SP\_Ztrans\_inv" 
  1557   \normalfont\\ and cut out the respective part from the program. First we ensure that
  1558   the string is correct. When we insert the string into (2)
  1559   \ttfamily store\_met .. "met\_partial\_fraction" \normalfont --- and get an error.
  1560 \<close>
  1561 
  1562 subsubsection \<open>'Programming' in ISAC's TP-based Language\<close>
  1563 text \<open>
  1564   At the present state writing programs in {\sisac} is particularly cumbersome.
  1565   So we give hints how to cope with the many obstacles. Below we describe the
  1566   steps we did in making (2) run.
  1567   
  1568   \begin{enumerate}
  1569     \item We check if the \textbf{string} containing the program is correct.
  1570     \item We check if the \textbf{types in the program} are correct.
  1571       For this purpose we start start with the first and last lines
  1572      \begin{verbatim}
  1573      "PartFracScript (f_f::real) (v_v::real) =       " ^
  1574      " (let X = Take f_f;                            " ^
  1575      "      pbz = ((Substitute []) X)                " ^
  1576      "  in pbz)"
  1577      \end{verbatim}
  1578        The last but one line helps not to bother with ';'.
  1579      \item Then we add line by line. Already the first line causes the error. 
  1580         So we investigate it by
  1581       \begin{verbatim}
  1582       val ctxt = Proof_Context.init_global @{theory "Inverse_Z_Transform"} ;
  1583       val SOME t = 
  1584         parseNEW ctxt "(num_orig::real) = 
  1585                           get_numerator(rhs f_f)";
  1586       \end{verbatim}
  1587         and see a type clash: \ttfamily rhs \normalfont from (1) requires type 
  1588         \ttfamily bool \normalfont while (2) wants to have \ttfamily (f\_f::real).
  1589         \normalfont Of course, we don't need \ttfamily rhs \normalfont anymore.
  1590       \item Type-checking can be very tedious. One might even inspect the
  1591         parse-tree of the program with {\sisac}'s specific debug tools:
  1592       \begin{verbatim}
  1593       val {scr = Prog t,...} = 
  1594         Method.from_store ["simplification",
  1595                  "of_rationals",
  1596                  "to_partial_fraction"];
  1597       atomty_thy @{theory "Inverse_Z_Transform"} t ;
  1598       \end{verbatim}
  1599       \item We check if the \textbf{semantics of the program} by stepwise evaluation
  1600         of the program. Evaluation is done by the Lucas-Interpreter, which works
  1601         using the knowledge in theory Isac_Knowledge; so we have to re-build Isac. And the
  1602         test are performed simplest in a file which is loaded with Isac.
  1603         See \ttfamily tests/../partial\_fractions.sml \normalfont.
  1604   \end{enumerate}
  1605 \<close>
  1606 
  1607 subsection \<open>Transfer to Inverse\_Z\_Transform.thy\<close>
  1608 text \<open>
  1609   It was not possible to complete this task, because we ran out of time.
  1610 \<close>
  1611 
  1612 
  1613 end
  1614