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