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