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