src/Tools/isac/Frontend/interface.sml
author Walther Neuper <neuper@ist.tugraz.at>
Wed, 25 Aug 2010 16:20:07 +0200
branchisac-update-Isa09-2
changeset 37947 22235e4dbe5f
parent 37942 src/Tools/isac/FE-interface/interface.sml@ba35790353b2
child 38050 4c52ad406c20
permissions -rw-r--r--
renamed isac's directories and Build_Isac.thy

Scripts --> ProgLang
ME --> Interpret
IsacKnowledge --> Knowledge
neuper@37906
     1
(* the interface between the isac-kernel and the java-frontend;
neuper@37906
     2
   the isac-kernel holds calc-trees; stdout in XML-format.
neuper@37906
     3
   authors: Walther Neuper 2002
neuper@37906
     4
   (c) due to copyright terms
neuper@37906
     5
neuper@37947
     6
use"Frontend/interface.sml";
neuper@37906
     7
use"interface.sml";
neuper@37906
     8
*)
neuper@37906
     9
neuper@37906
    10
signature INTERFACE =
neuper@37906
    11
  sig
neuper@37906
    12
    val CalcTree : fmz list -> unit
neuper@37906
    13
    val DEconstrCalcTree : calcID -> unit
neuper@37906
    14
    val Iterator : calcID -> unit
neuper@37906
    15
    val IteratorTEST : calcID -> iterID
neuper@37906
    16
    val appendFormula : calcID -> cterm' -> unit
neuper@37906
    17
    val autoCalculate : calcID -> auto -> unit
neuper@37906
    18
    val checkContext : calcID -> pos' -> guh -> unit
neuper@37906
    19
    val fetchApplicableTactics : calcID -> int -> pos' -> unit
neuper@37906
    20
    val fetchProposedTactic : calcID -> unit
neuper@37906
    21
    val applyTactic : calcID -> pos' -> tac -> unit
neuper@37906
    22
    val getAccumulatedAsms : calcID -> pos' -> unit
neuper@37906
    23
    val getActiveFormula : calcID -> unit
neuper@37906
    24
    val getAssumptions : calcID -> pos' -> unit
neuper@37906
    25
    val initContext : calcID -> ketype -> pos' -> unit
neuper@37906
    26
    val getFormulaeFromTo : calcID -> pos' -> pos' -> int -> bool -> unit
neuper@37906
    27
    val getTactic : calcID -> pos' -> unit
neuper@37906
    28
    val interSteps : calcID -> pos' -> unit
neuper@37906
    29
    val modifyCalcHead : calcID -> icalhd -> unit
neuper@37906
    30
    val moveActiveCalcHead : calcID -> unit
neuper@37906
    31
    val moveActiveDown : calcID -> unit
neuper@37906
    32
    val moveActiveDownTEST : calcID -> unit
neuper@37906
    33
    val moveActiveFormula : calcID -> pos' -> unit
neuper@37906
    34
    val moveActiveLevelDown : calcID -> unit
neuper@37906
    35
    val moveActiveLevelUp : calcID -> unit
neuper@37906
    36
    val moveActiveRoot : calcID -> unit
neuper@37906
    37
    val moveActiveRootTEST : calcID -> unit
neuper@37906
    38
    val moveActiveUp : calcID -> unit
neuper@37906
    39
    val moveCalcHead : calcID -> pos' -> unit
neuper@37906
    40
    val moveDown : calcID -> pos' -> unit
neuper@37906
    41
    val moveLevelDown : calcID -> pos' -> unit
neuper@37906
    42
    val moveLevelUp : calcID -> pos' -> unit
neuper@37906
    43
    val moveRoot : calcID -> unit
neuper@37906
    44
    val moveUp : calcID -> pos' -> unit
neuper@37906
    45
    val refFormula : calcID -> pos' -> unit
neuper@37906
    46
    val replaceFormula : calcID -> cterm' -> unit
neuper@37906
    47
    val resetCalcHead : calcID -> unit
neuper@37906
    48
    val modelProblem : calcID -> unit
neuper@37906
    49
    val refineProblem : calcID -> pos' -> guh -> unit
neuper@37906
    50
    val setContext : calcID -> pos' -> guh -> unit
neuper@37906
    51
    val setMethod : calcID -> metID -> unit
neuper@37906
    52
    val setNextTactic : calcID -> tac -> unit
neuper@37906
    53
    val setProblem : calcID -> pblID -> unit
neuper@37906
    54
    val setTheory : calcID -> thyID -> unit
neuper@37906
    55
  end
neuper@37906
    56
neuper@37906
    57
neuper@37906
    58
(*------------------------------------------------------------------*)
neuper@37906
    59
structure interface : INTERFACE =
neuper@37906
    60
struct
neuper@37906
    61
(*------------------------------------------------------------------*)
neuper@37906
    62
neuper@37906
    63
(*.encode "Isabelle"-strings as seen by the user to the
neuper@37906
    64
   format accepted by Isabelle.
neuper@37947
    65
   encode "^" ---> "^^^"; see Knowledge/Atools.thy;
neuper@37906
    66
   called for each cterm', icalhd, fmz in this interface;
neuper@37906
    67
   + see "fun decode" in xmlsrc/mathml.sml.*)
neuper@37906
    68
fun encode (str:cterm') = 
neuper@37906
    69
    let fun enc [] = []
neuper@37906
    70
	  | enc ("^"::cs) = "^"::"^"::"^"::(enc cs)
neuper@37906
    71
	  | enc (c::cs) = c::(enc cs)
neuper@37906
    72
    in (implode o enc o explode) str:cterm' end;
neuper@37906
    73
fun encode_imodel (imodel:imodel) =
neuper@37906
    74
    let fun enc (Given ifos) = Given (map encode ifos)
neuper@37906
    75
	  | enc (Find ifos) = Find (map encode ifos)
neuper@37906
    76
	  | enc (Relate ifos) = Relate (map encode ifos)
neuper@37906
    77
    in map enc imodel:imodel end;
neuper@37906
    78
fun encode_icalhd ((pos', headl, imodel, pos_, spec):icalhd) =
neuper@37906
    79
    (pos', encode headl, encode_imodel imodel, pos_, spec):icalhd;
neuper@37906
    80
fun encode_fmz ((ifos, spec):fmz) = (map encode ifos, spec):fmz;
neuper@37906
    81
neuper@37906
    82
neuper@37906
    83
(***. CalcTree .***)
neuper@37906
    84
neuper@37906
    85
(** add and delete users **)
neuper@37906
    86
neuper@37906
    87
(*.'Iterator 1' must exist with each CalcTree;
neuper@37906
    88
   the only for updating the calc-tree
neuper@37906
    89
   WN.0411: only 'Iterator 1' is stored,
neuper@37906
    90
   all others are just calculated on the fly
neuper@37906
    91
   TODO: adapt Iterator, add_user(= add_iterator!),etc. accordingly .*)
neuper@37906
    92
fun Iterator (cI:calcID) = (*returned ID unnecessary after WN.0411*)
neuper@37906
    93
    (adduserOK2xml cI (add_user (cI:calcID)))
neuper@37906
    94
    handle _ => sysERROR2xml cI "error in kernel";
neuper@37906
    95
fun IteratorTEST (cI:calcID) = add_user (cI:calcID);
neuper@37906
    96
(*fun DEconstructIterator (cI:calcID) (uI:iterID) =
neuper@37906
    97
    deluserOK2xml (del_user cI uI);*)
neuper@37906
    98
neuper@37906
    99
(*.create a calc-tree; for calls from java: thus ^^^ decoded to ^;
neuper@37906
   100
   compare "fun CalcTreeTEST" which does NOT decode.*)
neuper@37906
   101
fun CalcTree
neuper@37906
   102
	[(fmz, sp):fmz] (*for several variants lateron*) =
neuper@37906
   103
(* val[(fmz,sp):fmz]=[(["fixedValues [r=Arbfix]","maximum A","valuesFor [a,b]",
neuper@37906
   104
             "relations [A=a*b, (a/2)^^^2 + (b/2)^^^2 = r^^^2]",
neuper@37906
   105
             "relations [A=a*b, (a/2)^^^2 + (b/2)^^^2 = r^^^2]",
neuper@37906
   106
             "relations [A=a*b, a/2=r*sin alpha, b/2=r*cos alpha]",
neuper@37906
   107
             "boundVariable a","boundVariable b","boundVariable alpha",
neuper@37906
   108
             "interval {x::real. 0 <= x & x <= 2*r}",
neuper@37906
   109
             "interval {x::real. 0 <= x & x <= 2*r}",
neuper@37906
   110
             "interval {x::real. 0 <= x & x <= pi}",
neuper@37906
   111
             "errorBound (eps=(0::real))"],
neuper@37906
   112
       ("DiffApp.thy", ["maximum_of","function"],
neuper@37906
   113
            ["DiffApp","max_by_calculus"]))];
neuper@37906
   114
neuper@37906
   115
   *)
neuper@37906
   116
	(let val cs = nxt_specify_init_calc (encode_fmz (fmz, sp))
neuper@37906
   117
	     (*FIXME.WN.8.03: error-handling missing*)
neuper@37906
   118
	     val cI = add_calc cs
neuper@37906
   119
	 in calctreeOK2xml cI end)
neuper@37906
   120
	handle _ => sysERROR2xml 0 "error in kernel";
neuper@37906
   121
neuper@37906
   122
fun DEconstrCalcTree (cI:calcID) =
neuper@37906
   123
    deconstructcalctreeOK2xml (del_calc cI);
neuper@37906
   124
neuper@37906
   125
neuper@37906
   126
fun getActiveFormula (cI:calcID) = iteratorOK2xml cI (get_pos cI 1);
neuper@37906
   127
neuper@37906
   128
fun moveActiveFormula (cI:calcID) (p:pos') =
neuper@37906
   129
    let val ((pt,_),_) = get_calc cI
neuper@37906
   130
    in if existpt' p pt then (upd_ipos cI 1 p; iteratorOK2xml cI p)
neuper@37906
   131
       else sysERROR2xml cI "frontend sends a non-existing pos" end;
neuper@37906
   132
neuper@37906
   133
(*. set the next tactic to be applied: dont't change the calc-tree,
neuper@37906
   134
    but remember the envisaged changes for fun autoCalculate;
neuper@37906
   135
    compare force NextTactic .*)
neuper@37906
   136
(* val (cI, tac) = (1, Add_Given "equality (x ^^^ 2 + 4 * x + 3 = 0)");
neuper@37906
   137
   val (cI, tac) = (1, Specify_Theory "PolyEq.thy");
neuper@37906
   138
   val (cI, tac) = (1, Specify_Problem ["normalize","polynomial",
neuper@37906
   139
				   "univariate","equation"]);
neuper@37906
   140
   val (cI, tac) = (1, Subproblem ("Poly.thy",
neuper@37906
   141
			      ["polynomial","univariate","equation"]));
neuper@37906
   142
   val (cI, tac) = (1, Model_Problem["linear","univariate","equation","test"]);
neuper@37906
   143
   val (cI, tac) = (1, Detail_Set "Test_simplify");
neuper@37906
   144
   val (cI, tac) = (1, Apply_Method ["Test", "solve_linear"]);
neuper@37906
   145
   val (cI, tac) = (1, Rewrite_Set "Test_simplify");
neuper@37906
   146
    *)
neuper@37906
   147
fun setNextTactic (cI:calcID) tac =
neuper@37906
   148
    let val ((pt, _), _) = get_calc cI
neuper@37906
   149
	val ip = get_pos cI 1
neuper@37906
   150
    in case locatetac tac (pt, ip) of
neuper@37906
   151
(* val ("ok", (tacis, c, (_,p'))) = locatetac tac (pt, ip);
neuper@37906
   152
   *)
neuper@37906
   153
	   ("ok", (tacis, _, _)) =>
neuper@37906
   154
	   (upd_calc cI ((pt, ip), tacis); setnexttactic2xml cI "ok")
neuper@37906
   155
	 | ("unsafe-ok", (tacis, _, _)) =>
neuper@37906
   156
	   (upd_calc cI ((pt, ip), tacis); setnexttactic2xml cI "unsafe-ok")
neuper@37906
   157
	 | ("not-applicable",_) => setnexttactic2xml cI "not-applicable"
neuper@37906
   158
	 | ("end-of-calculation",_) =>
neuper@37906
   159
	   setnexttactic2xml cI "end-of-calculation"
neuper@37906
   160
	 | ("failure",_) => sysERROR2xml cI "failure"
neuper@37906
   161
    end;
neuper@37906
   162
neuper@37906
   163
(*. apply a tactic at a position and update the calc-tree if applicable .*)
neuper@37947
   164
(*WN080226 java-code is missing, errors smltest/Knowledge/polyminus.sml*)
neuper@37906
   165
(* val (cI, ip, tac) = (1, p, hd appltacs);
neuper@37906
   166
   val (cI, ip, tac) = (1, p, (hd (sel_appl_atomic_tacs pt p)));
neuper@37906
   167
   *)
neuper@37906
   168
fun applyTactic (cI:calcID) ip tac =
neuper@37906
   169
    let val ((pt, _), _) = get_calc cI
neuper@37906
   170
	val p = get_pos cI 1
neuper@37906
   171
    in case locatetac tac (pt, ip) of
neuper@37906
   172
(* val ("ok", (tacis, c, (pt',p'))) = locatetac tac (pt, ip);
neuper@37906
   173
   *)
neuper@37906
   174
	   ("ok", (_, c, ptp as (_,p'))) =>
neuper@37906
   175
	     (upd_calc cI (ptp, []); upd_ipos cI 1 p';
neuper@37906
   176
	      autocalculateOK2xml cI p (if null c then p'
neuper@37906
   177
					   else last_elem c) p')
neuper@37906
   178
	 | ("unsafe-ok", (_, c, ptp as (_,p'))) =>
neuper@37906
   179
	     (upd_calc cI (ptp, []); upd_ipos cI 1 p';
neuper@37906
   180
	      autocalculateOK2xml cI p (if null c then p'
neuper@37906
   181
					   else last_elem c) p')
neuper@37906
   182
	 | ("end-of-calculation", (_, c, ptp as (_,p'))) =>
neuper@37906
   183
	     (upd_calc cI (ptp, []); upd_ipos cI 1 p';
neuper@37906
   184
	      autocalculateOK2xml cI p (if null c then p'
neuper@37906
   185
					   else last_elem c) p')
neuper@37906
   186
neuper@37906
   187
neuper@37906
   188
	 | (str,_) => autocalculateERROR2xml cI "failure"
neuper@37906
   189
    end;
neuper@37906
   190
neuper@37906
   191
neuper@37906
   192
neuper@37906
   193
(* val cI = 1;
neuper@37906
   194
   *)
neuper@37906
   195
fun fetchProposedTactic (cI:calcID) =
neuper@37906
   196
    (case step (get_pos cI 1) (get_calc cI) of
neuper@37906
   197
	   ("ok", (tacis, _, _)) =>
neuper@37906
   198
	   let val _= upd_tacis cI tacis
neuper@37906
   199
	       val (tac,_,_) = last_elem tacis
neuper@37906
   200
	   in fetchproposedtacticOK2xml cI tac end
neuper@37906
   201
	 | ("helpless",_) => fetchproposedtacticERROR2xml cI "helpless"
neuper@37906
   202
	 | ("no-fmz-spec",_) => fetchproposedtacticERROR2xml cI "no-fmz-spec"
neuper@37906
   203
	 | ("end-of-calculation",_) =>
neuper@37906
   204
	   fetchproposedtacticERROR2xml cI "end-of-calculation")
neuper@37906
   205
    handle _ => sysERROR2xml cI "error in kernel";
neuper@37906
   206
neuper@37906
   207
(*datatype auto = FIXXXME040624: does NOT match interfaces/ITOCalc.java
neuper@37906
   208
  Step of int      (*1 do #int steps (may stop in model/specify)
neuper@37906
   209
		     IS VERY INEFFICIENT IN MODEL/SPECIY*)
neuper@37906
   210
| CompleteModel    (*2 complete modeling
neuper@37906
   211
                     if model complete, finish specifying*)
neuper@37906
   212
| CompleteCalcHead (*3 complete model/specify in one go*)
neuper@37906
   213
| CompleteToSubpbl (*4 stop at the next begin of a subproblem,
neuper@37906
   214
                     if none, complete the actual (sub)problem*)
neuper@37906
   215
| CompleteSubpbl   (*5 complete the actual (sub)problem (incl.ev.subproblems)*)
neuper@37906
   216
| CompleteCalc;    (*6 complete the calculation as a whole*)*)
neuper@37906
   217
fun autoCalculate (cI:calcID) auto =
neuper@37906
   218
(* val (cI, auto) = (1,CompleteCalc);
neuper@37906
   219
   val (cI, auto) = (1,CompleteModel);
neuper@37906
   220
   val (cI, auto) = (1,CompleteCalcHead);
neuper@37906
   221
   val (cI, auto) = (1,Step 1);
neuper@37906
   222
   *)
neuper@37906
   223
    (let val pold = get_pos cI 1
neuper@37906
   224
	 val x = autocalc [] pold (get_calc cI) auto
neuper@37906
   225
     in
neuper@37906
   226
	 case x of
neuper@37906
   227
(* val (str, c, ptp as (_,p)) = x;
neuper@37906
   228
 *)
neuper@37906
   229
	     ("ok", c, ptp as (_,p)) =>
neuper@37906
   230
	     (upd_calc cI (ptp, []); upd_ipos cI 1 p;
neuper@37906
   231
	      autocalculateOK2xml cI pold (if null c then pold
neuper@37906
   232
					   else last_elem c) p)
neuper@37906
   233
	   | ("end-of-calculation", c, ptp as (_,p)) =>
neuper@37906
   234
	     (upd_calc cI (ptp, []); upd_ipos cI 1 p;
neuper@37906
   235
	      autocalculateOK2xml cI pold (if null c then pold
neuper@37906
   236
					   else last_elem c) p)
neuper@37906
   237
	   | (str, _, _) => autocalculateERROR2xml cI str
neuper@37906
   238
     end)
neuper@37906
   239
    handle _ => sysERROR2xml cI "error in kernel";
neuper@37906
   240
    
neuper@37906
   241
neuper@37906
   242
(* val (cI:calcID, ichd as ((p,_),_,_,_,_):icalhd) =
neuper@37906
   243
       (1, (([],Pbl), "not used here",
neuper@37906
   244
	[Given ["fixedValues [r=Arbfix]"],
neuper@37906
   245
	 Find ["maximum A", "valuesFor [a,b]"(*new input*)],
neuper@37906
   246
	 Relate ["relations [A=a*b, (a/2)^^^2 + (b/2)^^^2 = r^^^2]"]], Pbl,
neuper@37906
   247
       ("DiffApp.thy", ["maximum_of","function"],
neuper@37906
   248
		   ["DiffApp","max_by_calculus"])));
neuper@37906
   249
 val (cI:calcID, ichd as ((p,_),_,_,_,_):icalhd) =
neuper@37906
   250
       (1, (([],Pbl),"solve (x+1=2, x)",
neuper@37906
   251
		  [Given ["equality (x+1=2)", "solveFor x"],
neuper@37906
   252
		   Find ["solutions L"]],
neuper@37906
   253
		  Pbl,
neuper@37906
   254
		  ("Test.thy", ["linear","univariate","equation","test"],
neuper@37906
   255
		   ["Test","solve_linear"])));
neuper@37906
   256
 val (cI:calcID, ichd as ((p,_),_,_,_,_):icalhd) =
neuper@37906
   257
       (1, (([],Pbl),"solveTest (1+-1*2+x=0,x)", [], Pbl, ("", [], [])));
neuper@37906
   258
 val (cI, p:pos')=(1, ([1],Frm));
neuper@37906
   259
 val (cI, p:pos')=(1, ([1,2,1,3],Res)); 
neuper@37906
   260
   *)
neuper@37906
   261
fun getTactic cI (p:pos') =
neuper@37906
   262
    (let val ((pt,_),_) = get_calc cI
neuper@37906
   263
	 val (form, tac, asms) = pt_extract (pt, p)
neuper@37906
   264
    in case tac of
neuper@37926
   265
(* val SOME ta = tac;
neuper@37906
   266
   *)
neuper@37926
   267
	   SOME ta => gettacticOK2xml cI ta
neuper@37926
   268
	 | NONE => gettacticERROR2xml cI ("no tactic at position "^pos'2str p)
neuper@37906
   269
     end)
neuper@37906
   270
    handle _ => sysERROR2xml cI "syserror in getTactic";
neuper@37906
   271
neuper@37906
   272
(*. see ICalcIterator#fetchApplicableTactics
neuper@37906
   273
 @see #TACTICS_ALL
neuper@37906
   274
 @see #TACTICS_CURRENT_THEORY
neuper@37906
   275
 @see #TACTICS_CURRENT_METHOD  ..the only impl.WN040307.*)
neuper@37906
   276
(*. fetch tactics to be applied to a particular step.*)
neuper@37906
   277
(* WN071231 kept this version for later parametrisation*)
neuper@37906
   278
(*.version 1: fetch _all_ tactics from script .*)
neuper@37906
   279
fun fetchApplicableTactics cI (scope:int) (p:pos') =
neuper@37906
   280
    (let val ((pt, _), _) = get_calc cI
neuper@37906
   281
    in (applicabletacticsOK cI (sel_rules pt p))
neuper@37906
   282
       handle PTREE str => sysERROR2xml cI str 
neuper@37906
   283
     end)
neuper@37906
   284
    handle _ => sysERROR2xml cI "error in kernel";
neuper@37906
   285
(*.version 2: fetch _applicable_ _elementary_ (ie. recursively 
neuper@37906
   286
              decompose rule-sets) Rewrite*, Calculate .*)
neuper@37906
   287
fun fetchApplicableTactics cI (scope:int) (p:pos') =
neuper@37906
   288
    (let val ((pt, _), _) = get_calc cI
neuper@37906
   289
    in (applicabletacticsOK cI (sel_appl_atomic_tacs pt p))
neuper@37906
   290
       handle PTREE str => sysERROR2xml cI str 
neuper@37906
   291
     end)
neuper@37906
   292
    handle _ => sysERROR2xml cI "error in kernel";
neuper@37906
   293
neuper@37906
   294
fun getAssumptions cI (p:pos') =
neuper@37906
   295
    (let val ((pt,_),_) = get_calc cI
neuper@37906
   296
	 val (_, _, asms) = pt_extract (pt, p)
neuper@37906
   297
     in getasmsOK2xml cI asms end)
neuper@37906
   298
    handle _ => sysERROR2xml cI "syserror in getAssumptions";
neuper@37906
   299
neuper@37906
   300
(*WN0502 @see ME/ctree: type asms: illdesigned, thus no positions returned*)
neuper@37906
   301
fun getAccumulatedAsms cI (p:pos') =
neuper@37906
   302
    (let val ((pt, _), _) = get_calc cI
neuper@37906
   303
	 val ass = map fst (get_assumptions_ pt p)
neuper@37906
   304
     in (*getaccuasmsOK2xml cI (get_assumptions_ pt p)*)
neuper@37906
   305
     getasmsOK2xml cI ass end)
neuper@37906
   306
    handle _ => sysERROR2xml cI "syserror in getAccumulatedAsms";
neuper@37906
   307
neuper@37906
   308
neuper@37906
   309
(*since moveActive* does NOT transfer pos java --> sml (only sml --> java)
neuper@37906
   310
  refFormula might become involved in far-off errors !!!*)
neuper@37906
   311
fun refFormula cI (p:pos') = (*WN0501 rename to 'fun getElement' !*)
neuper@37906
   312
(* val (cI, uI) = (1,1);
neuper@37906
   313
   *)
neuper@37906
   314
    (let val ((pt,_),_) = get_calc cI
neuper@37906
   315
	 val (form, tac, asms) = pt_extract (pt, p)
neuper@37906
   316
    in refformulaOK2xml cI p form end)
neuper@37906
   317
    handle _ => sysERROR2xml cI "error in kernel";
neuper@37906
   318
neuper@37906
   319
(*.get formulae 'from' 'to' w.r.t. ordering in Position#compareTo(Position p); 
neuper@37906
   320
   in case of CalcHeads only the headline is taken
neuper@37906
   321
   (the pos' allows distinction between PrfObj and PblObj anyway);
neuper@37906
   322
   'level' is adjusted such that an 'interval' of formulae is returned;
neuper@37906
   323
   'from' 'to' are designed for use by iterators of calcChangedEvent;
neuper@37906
   324
   thus 'from' is the last unchanged position.*)
neuper@37906
   325
fun getFormulaeFromTo cI (from as ([],Pbl):pos') (to as ([],Pbl):pos')_ false =
neuper@37906
   326
(*special case because 'from' is _before_ the first elements to be returned*)
neuper@37906
   327
(* val (cI, from, to, level) = (1, ([],Pbl), ([],Pbl), 1);
neuper@37906
   328
   *)
neuper@37906
   329
    ((let val ((pt,_),_) = get_calc cI
neuper@37906
   330
	val (ModSpec (_,_,headline,_,_,_),_,_) = pt_extract (pt, to)
neuper@37906
   331
    in getintervalOK cI [(to, headline)] end)
neuper@37906
   332
    handle _ => sysERROR2xml cI "error in kernel")
neuper@37906
   333
neuper@37906
   334
  | getFormulaeFromTo cI (from as ([],Pbl):pos') (to as ([],Met):pos')_ false =
neuper@37906
   335
    getFormulaeFromTo cI ([],Pbl) ([],Pbl) (~00000) false
neuper@37906
   336
neuper@37906
   337
  | getFormulaeFromTo cI (from:pos') (to:pos') level false =
neuper@37906
   338
(* val (cI, from, to, level) = (1, unc, gen, 0);
neuper@37906
   339
   val (cI, from, to, level) = (1, unc, gen, 1);
neuper@37906
   340
   val (cI, from, to, level) = (1, ([],Pbl), ([],Met), 1);
neuper@37906
   341
   *)
neuper@37906
   342
    (if from = to then sysERROR2xml cI "getFormulaeFromTo: From = To"
neuper@37906
   343
     else
neuper@37906
   344
	 (case from of
neuper@37906
   345
	      ([],Res) => sysERROR2xml cI "getFormulaeFromTo does: moveDown \
neuper@37906
   346
					  \from=([],Res) .. goes beyond result"
neuper@37906
   347
	    | _ => let val ((pt,_),_) = get_calc cI
neuper@37906
   348
		       val f = move_dn [] pt from
neuper@37906
   349
		       fun max (a,b) = if a < b then b else a
neuper@37906
   350
		       (*must reach margins ...*)
neuper@37906
   351
		       val lev = max (level, max (lev_of from, lev_of to))
neuper@37906
   352
		   in getintervalOK cI (get_interval f to lev pt) end)
neuper@37906
   353
	 handle _ => sysERROR2xml cI "error in getFormulaeFromTo")
neuper@37906
   354
neuper@37906
   355
  | getFormulaeFromTo cI from to level true =
neuper@37906
   356
    sysERROR2xml cI "getFormulaeFromTo impl.for formulae only,\
neuper@37906
   357
		    \i.e. last arg only impl. for false, _NOT_ true";
neuper@37906
   358
neuper@37906
   359
neuper@37906
   360
(* val (cI, ip) = (1, ([1,9], Res));
neuper@37906
   361
   val (cI, ip) = (1, ([], Res));
neuper@37906
   362
   val (cI, ip) = (1, ([2], Res));
neuper@37906
   363
   val (cI, ip) = (1, ([3,1], Res));
neuper@37906
   364
   val (cI, ip) = (1, ([1,2,1], Res));
neuper@37906
   365
   *)
neuper@37906
   366
fun interSteps cI ip =
neuper@37906
   367
    (let val ((pt,p), tacis) = get_calc cI
neuper@37906
   368
     in if (not o is_interpos) ip
neuper@37906
   369
	then interStepsERROR cI "only formulae with position (_,Res) \
neuper@37906
   370
				\may have intermediate steps above them"
neuper@37906
   371
	else let val ip' = lev_pred' pt ip
neuper@37906
   372
(* val (str, pt', lastpos) = detailstep pt ip;
neuper@37906
   373
   *)
neuper@37906
   374
	     in case detailstep pt ip of
neuper@37906
   375
		    ("detailrls", pt(*, pos'forms*), lastpos) =>
neuper@37906
   376
		    (upd_calc cI ((pt, p), tacis);
neuper@37906
   377
		     interStepsOK cI (*pos'forms*) ip' ip' lastpos)
neuper@37906
   378
		  | ("no-Rewrite_Set...", _, _) =>
neuper@37906
   379
		    sysERROR2xml cI "no Rewrite_Set..."
neuper@37906
   380
		  | (_, _(*, pos'formshds*), lastpos) =>
neuper@37906
   381
		    interStepsOK cI (*pos'formshds*) ip' ip' lastpos
neuper@37906
   382
	     end
neuper@37906
   383
     end)
neuper@37906
   384
    handle _ => sysERROR2xml cI "error in kernel";
neuper@37906
   385
neuper@37906
   386
fun modifyCalcHead (cI:calcID) (ichd as ((p,_),_,_,_,_):icalhd) =
neuper@37906
   387
    (let val ((pt,_),_) = get_calc cI
neuper@37906
   388
	val (pt, chd as (_,p_,_,_,_,_)) = input_icalhd pt ichd
neuper@37906
   389
    in (upd_calc cI ((pt, (p,p_)), []); 
neuper@37906
   390
	modifycalcheadOK2xml cI chd) end)
neuper@37906
   391
    handle _ => sysERROR2xml cI "error in kernel";
neuper@37906
   392
neuper@37906
   393
(*.at the activeFormula set the Model, the Guard and the Specification 
neuper@37906
   394
   to empty and return a CalcHead;
neuper@37906
   395
   the 'origin' remains (for reconstructing all that).*)
neuper@37906
   396
fun resetCalcHead (cI:calcID) = 
neuper@37906
   397
    (let val (ptp,_) = get_calc cI
neuper@37906
   398
	val ptp = reset_calchead ptp
neuper@37906
   399
    in (upd_calc cI (ptp, []); 
neuper@37906
   400
	modifycalcheadOK2xml cI (get_ocalhd ptp)) end)
neuper@37906
   401
    handle _ => sysERROR2xml cI "error in kernel";
neuper@37906
   402
neuper@37906
   403
(*.at the activeFormula insert all the Descriptions in the Model 
neuper@37906
   404
   (_not_ in the Guard) and return a CalcHead;
neuper@37906
   405
   the Descriptions are for user-guidance; the rest of the items 
neuper@37906
   406
   are left empty for user-input; 
neuper@37906
   407
   includes a resetCalcHead for the Model and the Guard.*)
neuper@37906
   408
fun modelProblem (cI:calcID) = 
neuper@37906
   409
    (let val (ptp, _) = get_calc cI
neuper@37906
   410
	val ptp = reset_calchead ptp
neuper@37906
   411
	val (_, _, ptp) = nxt_specif Model_Problem ptp
neuper@37906
   412
    in (upd_calc cI (ptp, []); 
neuper@37906
   413
	modifycalcheadOK2xml cI (get_ocalhd ptp)) end)
neuper@37906
   414
    handle _ => sysERROR2xml cI "error in kernel";
neuper@37906
   415
neuper@37906
   416
neuper@37906
   417
(*.set the context determined on a knowledgebrowser to the current calc.*)
neuper@37906
   418
fun setContext (cI:calcID) (ip as (_,p_):pos') (guh:guh) =
neuper@37906
   419
    (case (implode o (take_fromto 1 4) o explode) guh of
neuper@37906
   420
	 "thy_" =>
neuper@37906
   421
(* val (cI, ip as (_,p_), guh) = (1, p, "thy_isac_Test-rls-Test_simplify");
neuper@37906
   422
   *)
neuper@37935
   423
	 if member op = [Pbl,Met] p_
neuper@37930
   424
         then message2xml cI "thy-context not to calchead"
neuper@37906
   425
	 else if ip = ([],Res) then message2xml cI "no thy-context at result"
neuper@37906
   426
	 else if no_thycontext guh then message2xml cI ("no thy-context for '"^
neuper@37906
   427
							guh ^ "'")
neuper@37906
   428
	 else let val (ptp as (pt,pold),_) = get_calc cI
neuper@37906
   429
		  val is = get_istate pt ip
neuper@37906
   430
		  val subs = subs_from is "dummy" guh
neuper@37906
   431
		  val tac = guh2rewtac guh subs
neuper@37906
   432
	      in case locatetac tac (pt, ip) of (*='fun setNextTactic'+step*)
neuper@37906
   433
		     ("ok", (tacis, c, ptp as (_,p))) =>
neuper@37906
   434
(* val (str, (tacis, c, ptp as (_,p))) = locatetac tac (pt, ip);
neuper@37906
   435
   *)
neuper@37906
   436
		     (upd_calc cI ((pt,p), []); 
neuper@37906
   437
		      autocalculateOK2xml cI pold (if null c then pold
neuper@37906
   438
					   else last_elem c) p)
neuper@37906
   439
		   | ("unsafe-ok", (tacis, c, ptp as (_,p))) =>
neuper@37906
   440
		     (upd_calc cI ((pt,p), []); 
neuper@37906
   441
		      autocalculateOK2xml cI pold (if null c then pold
neuper@37906
   442
						   else last_elem c) p)
neuper@37906
   443
		   | ("end-of-calculation",_) =>
neuper@37906
   444
		     message2xml cI "end-of-calculation"
neuper@37906
   445
		   | ("failure",_) => sysERROR2xml cI "failure"
neuper@37906
   446
		   | ("not-applicable",_) => (*the rule comes from anywhere..*)
neuper@37906
   447
		     (case applicable_in ip pt tac of 
neuper@37906
   448
			  
neuper@37906
   449
			  Notappl e => message2xml cI ("'" ^ tac2str tac ^ 
neuper@37906
   450
						       "' not-applicable")
neuper@37906
   451
			| Appl m => 
neuper@37906
   452
			  let val (p,c,_,pt) = generate1 (assoc_thy"Isac.thy") 
neuper@37906
   453
							 m Uistate ip pt
neuper@37906
   454
			  in upd_calc cI ((pt,p),[]);
neuper@37906
   455
			  autocalculateOK2xml cI pold (if null c then pold
neuper@37906
   456
						       else last_elem c) p
neuper@37906
   457
			  end)
neuper@37906
   458
	      end
neuper@37906
   459
(* val (cI, ip as (_,p_), guh) = (1, pos, guh);
neuper@37906
   460
   *)
neuper@37906
   461
       | "pbl_" =>
neuper@37906
   462
	 let val pI = guh2kestoreID guh
neuper@37906
   463
	     val ((pt, _), _) = get_calc cI
neuper@37906
   464
	     (*val ip as (_, p_) = get_pos cI 1*)
neuper@37942
   465
	 in if member op = [Pbl, Met] p_ 
neuper@37906
   466
	    then let val (pt, chd) = set_problem pI (pt, ip)
neuper@37906
   467
		 in (upd_calc cI ((pt, ip), []);
neuper@37906
   468
		     modifycalcheadOK2xml cI chd) end
neuper@37906
   469
	    else sysERROR2xml cI "setContext for pbl requires ActiveFormula \
neuper@37906
   470
				 \on CalcHead"
neuper@37906
   471
	 end
neuper@37906
   472
(* val (cI, ip as (_,p_), guh) = (1, pos, "met_eq_lin");
neuper@37906
   473
   *)
neuper@37906
   474
       | "met_" =>
neuper@37906
   475
	 let val mI = guh2kestoreID guh
neuper@37906
   476
	     val ((pt, _), _) = get_calc cI
neuper@37942
   477
	 in if member op = [Pbl, Met] p_
neuper@37906
   478
	    then let val (pt, chd) = set_method mI (pt, ip)
neuper@37906
   479
		 in (upd_calc cI ((pt, ip), []);
neuper@37906
   480
		     modifycalcheadOK2xml cI chd) end
neuper@37906
   481
	    else sysERROR2xml cI "setContext for met requires ActiveFormula \
neuper@37906
   482
				 \on CalcHead"
neuper@37906
   483
	 end)
neuper@37906
   484
    handle _ => sysERROR2xml cI "error in kernel";
neuper@37906
   485
neuper@37906
   486
neuper@37906
   487
(*.specify the Method at the activeFormula and return a CalcHead
neuper@37906
   488
   containing the Guard.
neuper@37906
   489
   WN0512 impl.incomplete, see 'nxt_specif (Specify_Method '.*)
neuper@37906
   490
fun setMethod (cI:calcID) (mI:metID) = 
neuper@37906
   491
(* val (cI, mI) = (1, ["Test","solve_linear"]);
neuper@37906
   492
   *)
neuper@37906
   493
    (let val ((pt, _), _) = get_calc cI
neuper@37906
   494
	val ip as (_, p_) = get_pos cI 1
neuper@37935
   495
    in if member op = [Pbl,Met] p_ 
neuper@37906
   496
       then let val (pt, chd) = set_method mI (pt, ip)
neuper@37906
   497
	    in (upd_calc cI ((pt, ip), []);
neuper@37906
   498
		modifycalcheadOK2xml cI chd) end
neuper@37906
   499
       else sysERROR2xml cI "setMethod requires ActiveFormula on CalcHead"
neuper@37906
   500
 end)
neuper@37906
   501
    handle _ => sysERROR2xml cI "error in kernel";
neuper@37906
   502
neuper@37906
   503
(*.specify the Problem at the activeFormula and return a CalcHead
neuper@37906
   504
   containing the Model; special case of checkContext;
neuper@37906
   505
   WN0512 impl.incomplete, see 'nxt_specif (Specify_Problem '.*)
neuper@37906
   506
fun setProblem (cI:calcID) (pI:pblID) =
neuper@37906
   507
    (let val ((pt, _), _) = get_calc cI
neuper@37906
   508
	val ip as (_, p_) = get_pos cI 1
neuper@37935
   509
    in if member op = [Pbl,Met] p_
neuper@37906
   510
       then let val (pt, chd) = set_problem pI (pt, ip)
neuper@37906
   511
	    in (upd_calc cI ((pt, ip), []);
neuper@37906
   512
		modifycalcheadOK2xml cI chd) end
neuper@37906
   513
       else sysERROR2xml cI "setProblem requires ActiveFormula on CalcHead"
neuper@37906
   514
 end)
neuper@37906
   515
    handle _ => sysERROR2xml cI "error in kernel";
neuper@37906
   516
neuper@37906
   517
(*.specify the Theory at the activeFormula and return a CalcHead;
neuper@37906
   518
   special case of checkContext;
neuper@37906
   519
   WN0512 impl.incomplete, see 'nxt_specif (Specify_Method '.*)
neuper@37906
   520
fun setTheory (cI:calcID) (tI:thyID) =
neuper@37906
   521
    (let val ((pt, _), _) = get_calc cI
neuper@37906
   522
	val ip as (_, p_) = get_pos cI 1
neuper@37935
   523
    in if member op = [Pbl,Met] p_
neuper@37906
   524
       then let val (pt, chd) = set_theory tI (pt, ip)
neuper@37906
   525
	    in (upd_calc cI ((pt, ip), []);
neuper@37906
   526
		modifycalcheadOK2xml cI chd) end
neuper@37906
   527
       else sysERROR2xml cI "setProblem requires ActiveFormula on CalcHead"
neuper@37906
   528
 end)
neuper@37906
   529
    handle _ => sysERROR2xml cI "error in kernel";
neuper@37906
   530
neuper@37906
   531
neuper@37906
   532
(**. without update of CalcTree .**)
neuper@37906
   533
neuper@37906
   534
(*.match the model of a problem at pos p 
neuper@37906
   535
   with the model-pattern of the problem with pblID*)
neuper@37906
   536
(*fun tryMatchProblem cI pblID =
neuper@37906
   537
    (let val ((pt,_),_) = get_calc cI
neuper@37906
   538
	 val p = get_pos cI 1
neuper@37906
   539
	 val chd = trymatch pblID pt p
neuper@37906
   540
    in trymatchOK2xml cI chd end)
neuper@37906
   541
    handle _ => sysERROR2xml cI "error in kernel";*)
neuper@37906
   542
neuper@37906
   543
(*.refinement for the parent-problem of the position.*)
neuper@37906
   544
(* val (cI, (p,p_), guh) = (1, ([1],Res), "pbl_equ_univ");
neuper@37906
   545
   *)
neuper@37906
   546
fun refineProblem cI ((p,p_) : pos') (guh : guh) =
neuper@37906
   547
    (let val pblID = guh2kestoreID guh
neuper@37906
   548
	 val ((pt,_),_) = get_calc cI
neuper@37906
   549
	 val pp = par_pblobj pt p
neuper@37906
   550
	 val chd = tryrefine pblID pt (pp, p_)
neuper@37906
   551
    in matchpbl2xml cI chd end)
neuper@37906
   552
    handle _ => sysERROR2xml cI "error in kernel";
neuper@37906
   553
neuper@37906
   554
(* val (cI, ifo) = (1, "-2 * 1 + (1 + x) = 0");
neuper@37906
   555
   val (cI, ifo) = (1, "x = 2");
neuper@37906
   556
   val (cI, ifo) = (1, "[x = 3 + -2*1]");
neuper@37906
   557
   val (cI, ifo) = (1, "-1 + x = 0");
neuper@37906
   558
   val (cI, ifo) = (1, "x - 4711 = 0");
neuper@37906
   559
   val (cI, ifo) = (1, "2+ -1 + x = 2");
neuper@37906
   560
   val (cI, ifo) = (1, " x - ");
neuper@37906
   561
   val (cI, ifo) = (1, "(-3 * x + 4 * y + -1 * x * y) / (x * y)");
neuper@37906
   562
   val (cI, ifo) = (1, "(4 * y + -3 * x) / (x * y) + -1");
neuper@37906
   563
   *)
neuper@37906
   564
fun appendFormula cI (ifo:cterm') =
neuper@37906
   565
    (let val cs = get_calc cI
neuper@37906
   566
	 val pos as (_,p_) = get_pos cI 1
neuper@37906
   567
     in case step pos cs of
neuper@37906
   568
(* val (str, cs') = step pos cs;
neuper@37906
   569
   *)
neuper@37906
   570
	    ("ok", cs') =>
neuper@37906
   571
	    (case inform cs' (encode ifo) of
neuper@37906
   572
(* val (str, (_, c, ptp as (_,p))) = inform cs' (encode ifo);
neuper@37906
   573
   *)
neuper@37906
   574
		 ("ok", (_(*use in DG !!!*), c, ptp as (_,p))) =>
neuper@37906
   575
		 (upd_calc cI (ptp, []); upd_ipos cI 1 p;
neuper@37906
   576
		  appendformulaOK2xml cI pos (if null c then pos
neuper@37906
   577
					      else last_elem c) p)
neuper@37906
   578
	       | ("same-formula", (_, c, ptp as (_,p))) =>
neuper@37906
   579
		 (upd_calc cI (ptp, []); upd_ipos cI 1 p;
neuper@37906
   580
		  appendformulaOK2xml cI pos (if null c then pos
neuper@37906
   581
					      else last_elem c) p)
neuper@37906
   582
	       | (msg, _) => appendformulaERROR2xml cI msg)
neuper@37906
   583
	  | (msg, cs') => appendformulaERROR2xml cI msg
neuper@37906
   584
     end)
neuper@37906
   585
    handle _ => sysERROR2xml cI "error in kernel";
neuper@37906
   586
neuper@37906
   587
neuper@37906
   588
neuper@37906
   589
(*.replace a formula with_in_ a calculation;
neuper@37906
   590
   this situation applies for initial CAS-commands, too.*)
neuper@37906
   591
(* val (cI, ifo) = (2, "-1 + x = 0");
neuper@37906
   592
   val (cI, ifo) = (1, "-1 + x = 0");
neuper@37906
   593
   val (cI, ifo) = (1, "x - 1 = 0");
neuper@37906
   594
   val (cI, ifo) = (1, "x = 1");
neuper@37906
   595
   val (cI, ifo) = (1, "solve(x+1=2,x)");
neuper@37906
   596
   val (cI, ifo) = (1, "Simplify (2*a + 3*a)");
neuper@37906
   597
   val (cI, ifo) = (1, "Diff (x^2 + x + 1, x)");
neuper@37906
   598
   *)
neuper@37906
   599
fun replaceFormula cI (ifo:cterm') =
neuper@37906
   600
    (let val ((pt, _), _) = get_calc cI
neuper@37906
   601
	val p = get_pos cI 1
neuper@37906
   602
    in case inform (([], [], (pt, p)): calcstate') (encode ifo) of
neuper@37906
   603
	   ("ok", (_(*tacs used for DG ?*), c, ptp' as (pt',p'))) =>
neuper@37906
   604
(* val (str, (_,c, ptp' as (pt',p')))= inform ([], [], (pt, p)) (encode ifo);
neuper@37906
   605
   *)
neuper@37906
   606
	   let val unc = if null (fst p) then p else move_up [] pt p
neuper@37906
   607
	       val _ = upd_calc cI (ptp', [])
neuper@37906
   608
	       val _ = upd_ipos cI 1 p'
neuper@37906
   609
	   in replaceformulaOK2xml cI unc
neuper@37906
   610
				   (if null c then unc
neuper@37906
   611
				    else last_elem c) p'(*' NEW*) end
neuper@37906
   612
	 | ("same-formula", _) =>
neuper@37906
   613
	   (*TODO.WN0501 MESSAGE !*)
neuper@37906
   614
	   replaceformulaERROR2xml cI "formula not changed"
neuper@37906
   615
	 | (msg, _) => replaceformulaERROR2xml cI msg
neuper@37906
   616
    end)
neuper@37906
   617
    handle _ => sysERROR2xml cI "error in kernel";
neuper@37906
   618
neuper@37906
   619
neuper@37906
   620
neuper@37906
   621
(***. CalcIterator
neuper@37906
   622
    moveActive*: set the pos' of the active formula stored with the calctree
neuper@37906
   623
                 could take pos' as argument for consistency checks
neuper@37906
   624
    move*:       compute the new iterator from the old one on the fly
neuper@37906
   625
neuper@37906
   626
.***)
neuper@37906
   627
neuper@37906
   628
fun moveActiveRoot cI =
neuper@37906
   629
    (let val _ = upd_ipos cI 1 ([],Pbl)
neuper@37906
   630
    in iteratorOK2xml cI ([],Pbl) end)
neuper@37906
   631
    handle e => sysERROR2xml cI "error in kernel";
neuper@37906
   632
fun moveRoot cI =
neuper@37906
   633
    (iteratorOK2xml cI ([],Pbl))
neuper@37906
   634
    handle e => sysERROR2xml cI "";
neuper@37906
   635
fun moveActiveRootTEST cI =
neuper@37906
   636
    (let val _ = upd_ipos cI 1 ([],Pbl)
neuper@37906
   637
    in (*iteratorOK2xml cI ([],Pbl)*)() end)
neuper@37906
   638
    handle e => sysERROR2xml cI "error in kernel";
neuper@37906
   639
neuper@37906
   640
(* val (cI, uI) = (1,1);
neuper@37906
   641
   val (cI, uI) = (1,2);
neuper@37906
   642
   *)
neuper@37906
   643
fun moveActiveDown cI =
neuper@37906
   644
    ((let val ((pt,_),_) = get_calc cI
neuper@37906
   645
(* val (P, (Nd (_, ns)), (p::(ps as (_::_)), p_)) =([]:pos, pt, get_pos cI uI);
neuper@37906
   646
   val (P, (Nd (c, ns)), ([p], p_))               =([]:pos, pt, get_pos cI uI);
neuper@37906
   647
neuper@37906
   648
   print_depth 7;pt
neuper@37906
   649
   *)
neuper@37906
   650
	  val ip' = move_dn [] pt (get_pos cI 1)
neuper@37906
   651
	  val _ = upd_ipos cI 1 ip'
neuper@37906
   652
      in iteratorOK2xml cI ip' end)
neuper@37906
   653
     handle (PTREE e) => iteratorERROR2xml cI)
neuper@37906
   654
    handle _ => sysERROR2xml cI "error in kernel";
neuper@37906
   655
fun moveDown cI (p:pos') =
neuper@37906
   656
    ((let val ((pt,_),_) = get_calc cI
neuper@37906
   657
(* val (P, (Nd (_, ns)), (p::(ps as (_::_)), p_)) =([]:pos, pt, get_pos cI uI);
neuper@37906
   658
   val (P, (Nd (c, ns)), ([p], p_))               =([]:pos, pt, get_pos cI uI);
neuper@37906
   659
neuper@37906
   660
   print_depth 7;pt
neuper@37906
   661
   *)
neuper@37906
   662
	  val ip' = move_dn [] pt p
neuper@37906
   663
      in iteratorOK2xml cI ip' end)
neuper@37906
   664
     handle (PTREE e) => iteratorERROR2xml cI)
neuper@37906
   665
    handle _ => sysERROR2xml cI "error in kernel";
neuper@37906
   666
fun moveActiveDownTEST cI =
neuper@37906
   667
    let val ((pt,_),_) = get_calc cI
neuper@37906
   668
	val ip = get_pos cI 1
neuper@37906
   669
	  val ip' = (move_dn [] pt ip)
neuper@37906
   670
	      handle _ => ip
neuper@37906
   671
	  val _ = upd_ipos cI 1 ip'
neuper@37906
   672
      in (*iteratorOK2xml cI uI*)() end;
neuper@37906
   673
neuper@37906
   674
fun moveActiveLevelDown cI =
neuper@37906
   675
    ((let val ((pt,_),_) = get_calc cI
neuper@37906
   676
	  val ip' = movelevel_dn [] pt (get_pos cI 1)
neuper@37906
   677
	  val _ = upd_ipos cI 1 ip'
neuper@37906
   678
      in iteratorOK2xml cI ip' end)
neuper@37906
   679
     handle (PTREE e) => iteratorERROR2xml cI)
neuper@37906
   680
    handle _ => sysERROR2xml cI "error in kernel";
neuper@37906
   681
fun moveLevelDown cI (p:pos') =
neuper@37906
   682
    ((let val ((pt,_),_) = get_calc cI
neuper@37906
   683
	  val ip' = movelevel_dn [] pt p
neuper@37906
   684
      in iteratorOK2xml cI ip' end)
neuper@37906
   685
     handle (PTREE e) => iteratorERROR2xml cI)
neuper@37906
   686
    handle _ => sysERROR2xml cI "error in kernel";
neuper@37906
   687
neuper@37906
   688
fun moveActiveUp cI =
neuper@37906
   689
    ((let val ((pt,_),_) = get_calc cI
neuper@37906
   690
	  val ip' = move_up [] pt (get_pos cI 1)
neuper@37906
   691
	  val _ = upd_ipos cI 1 ip'
neuper@37906
   692
      in iteratorOK2xml cI ip' end)
neuper@37906
   693
     handle PTREE e => iteratorERROR2xml cI)
neuper@37906
   694
    handle _ => sysERROR2xml cI "error in kernel";
neuper@37906
   695
fun moveUp cI (p:pos') =
neuper@37906
   696
    ((let val ((pt,_),_) = get_calc cI
neuper@37906
   697
	  val ip' = move_up [] pt p
neuper@37906
   698
      in iteratorOK2xml cI ip' end)
neuper@37906
   699
     handle PTREE e => iteratorERROR2xml cI)
neuper@37906
   700
    handle _ => sysERROR2xml cI "error in kernel";
neuper@37906
   701
neuper@37906
   702
fun moveActiveLevelUp cI =
neuper@37906
   703
    ((let val ((pt,_),_) = get_calc cI
neuper@37906
   704
	  val ip' = movelevel_up [] pt (get_pos cI 1)
neuper@37906
   705
	  val _ = upd_ipos cI 1 ip'
neuper@37906
   706
      in iteratorOK2xml cI ip' end)
neuper@37906
   707
     handle PTREE e => iteratorERROR2xml cI)
neuper@37906
   708
    handle _ => sysERROR2xml cI "error in kernel";
neuper@37906
   709
fun moveLevelUp cI (p:pos') =
neuper@37906
   710
    ((let val ((pt,_),_) = get_calc cI
neuper@37906
   711
	  val ip' = movelevel_up [] pt p
neuper@37906
   712
      in iteratorOK2xml cI ip' end)
neuper@37906
   713
     handle PTREE e => iteratorERROR2xml cI)
neuper@37906
   714
    handle _ => sysERROR2xml cI "error in kernel";
neuper@37906
   715
neuper@37906
   716
fun moveActiveCalcHead cI =
neuper@37906
   717
    ((let val ((pt,_),_) = get_calc cI
neuper@37906
   718
	  val ip' = movecalchd_up pt (get_pos cI 1)
neuper@37906
   719
	  val _ = upd_ipos cI 1 ip'
neuper@37906
   720
      in iteratorOK2xml cI ip' end)
neuper@37906
   721
     handle PTREE e => iteratorERROR2xml cI)
neuper@37906
   722
    handle _ => sysERROR2xml cI "error in kernel";
neuper@37906
   723
fun moveCalcHead cI (p:pos') =
neuper@37906
   724
    ((let val ((pt,_),_) = get_calc cI
neuper@37906
   725
	  val ip' = movecalchd_up pt p
neuper@37906
   726
      in iteratorOK2xml cI ip' end)
neuper@37906
   727
     handle PTREE e => iteratorERROR2xml cI)
neuper@37906
   728
    handle _ => sysERROR2xml cI "error in kernel";
neuper@37906
   729
neuper@37906
   730
neuper@37906
   731
(*.initContext Thy_ is conceptually impossible at [Pbl,Met] 
neuper@37906
   732
   and at positions with Check_Postcond and End_Trans;
neuper@37906
   733
   at possible pos's there can be NO rewrite (returned as a context, too).*)
neuper@37906
   734
(* val (cI, Thy_, (pos as (p,p_):pos')) = (1, Thy_, ([1], Frm));
neuper@37906
   735
   val (cI, Thy_, (pos as (p,p_):pos')) = (1, Thy_, ([], Res));
neuper@37906
   736
   val (cI, Thy_, (pos as (p,p_):pos')) = (1, Thy_, ([2], Res));
neuper@37906
   737
   val (cI, Thy_, (pos as (p,p_):pos')) = (1, Thy_, ([1,1], Frm));
neuper@37906
   738
   *)
neuper@37906
   739
fun initContext (cI:calcID) Thy_ (pos as (p,p_):pos') =
neuper@37935
   740
    ((if member op = [Pbl,Met] p_
neuper@37930
   741
      then message2xml cI "thy-context not to calchead"
neuper@37906
   742
      else if pos = ([],Res) then message2xml cI "no thy-context at result"
neuper@37906
   743
      else let val cs as (ptp as (pt,_),_) = get_calc cI
neuper@37906
   744
	   in if exist_lev_on' pt pos
neuper@37906
   745
	      then let val pos' = lev_on' pt pos
neuper@37906
   746
		       val tac = get_tac_checked pt pos'
neuper@37906
   747
		   in if is_rewtac tac 
neuper@37906
   748
		      then contextthyOK2xml cI (context_thy (pt,pos) tac)
neuper@37906
   749
		      else message2xml cI ("no thy-context at tac '" ^
neuper@37906
   750
					   tac2str tac ^ "'")
neuper@37906
   751
		   end
neuper@37906
   752
	      else if is_curr_endof_calc pt pos
neuper@37906
   753
	      then case step pos cs of
neuper@37906
   754
(* val (str, (tacis, _, (pt,_))) = step pos cs;
neuper@37906
   755
   val ("ok", (tacis, _, (pt,_))) = step pos cs;
neuper@37906
   756
   *)
neuper@37906
   757
		       ("ok", (tacis, _, (pt,_))) =>
neuper@37906
   758
		       let val tac = fst3 (last_elem tacis)
neuper@37906
   759
		       in if is_rewtac tac 
neuper@37906
   760
			  then contextthyOK2xml 
neuper@37906
   761
				   cI (context_thy ptp tac)
neuper@37906
   762
			  else message2xml cI ("no thy-context at tac '" ^
neuper@37906
   763
					       tac2str tac ^ "'")
neuper@37906
   764
		       end
neuper@37906
   765
		     | (msg, _) => message2xml cI msg
neuper@37906
   766
	      else message2xml cI "no thy-context at this position"
neuper@37906
   767
	   end)
neuper@37906
   768
     handle _ => sysERROR2xml cI "error in kernel")
neuper@37906
   769
neuper@37906
   770
(* val (cI, Pbl_, pos as (p,p_)) = (1, Pbl_, ([],Pbl));
neuper@37906
   771
   *)
neuper@37906
   772
  | initContext cI Pbl_ (pos as (p,p_):pos') = 
neuper@37906
   773
    ((let val ((pt,_),_) = get_calc cI
neuper@37906
   774
	  val pp = par_pblobj pt p
neuper@37906
   775
	  val chd = initcontext_pbl pt (pp,p_)
neuper@37906
   776
      in matchpbl2xml cI chd end)
neuper@37906
   777
     handle _ => sysERROR2xml cI "error in kernel")
neuper@37906
   778
neuper@37906
   779
  | initContext cI Met_ (pos as (p,p_):pos') =
neuper@37906
   780
    ((let val ((pt,_),_) = get_calc cI
neuper@37906
   781
	  val pp = par_pblobj pt p
neuper@37906
   782
	  val chd = initcontext_met pt (pp,p_)
neuper@37906
   783
      in matchmet2xml cI chd end)
neuper@37906
   784
     handle _ => sysERROR2xml cI "error in kernel");
neuper@37906
   785
neuper@37906
   786
neuper@37906
   787
    
neuper@37906
   788
(*.match a theorem, a ruleset (etc., selected in the knowledge-browser)
neuper@37906
   789
with the formula in the focus on the worksheet;
neuper@37906
   790
string contains the thy, thus it is unique as thmID, rlsID for this thy;
neuper@37906
   791
take the substitution from the istate of the formula.*)
neuper@37947
   792
(* use"../smltest/Knowledge/poly.sml";
neuper@37906
   793
   val (cI, pos as (p,p_), guh) = (1, ([1,1,1], Frm), 
neuper@37906
   794
				   "thy_Poly-thm-real_diff_minus");
neuper@37906
   795
   val (cI, pos as (p,p_), guh) = (1, ([1,1], Frm), "norm_Poly");
neuper@37906
   796
   val (cI, pos as (p,p_), guh) = 
neuper@37906
   797
       (1, ([1], Res), "thy_isac_Test-rls-Test_simplify");
neuper@37906
   798
   *)
neuper@37906
   799
fun checkContext (cI:calcID) (pos:pos' as (p,p_)) (guh:guh) =
neuper@37906
   800
    (case (implode o (take_fromto 1 4) o explode) guh of
neuper@37906
   801
	 "thy_" =>
neuper@37935
   802
	 if member op = [Pbl,Met] p_
neuper@37930
   803
         then message2xml cI "thy-context not to calchead"
neuper@37906
   804
	 else if pos = ([],Res) then message2xml cI "no thy-context at result"
neuper@37906
   805
	 else if no_thycontext guh then message2xml cI ("no thy-context for '"^
neuper@37906
   806
							guh ^ "'")
neuper@37906
   807
	 else let val (ptp as (pt,_),_) = get_calc cI
neuper@37906
   808
		  val is = get_istate pt pos
neuper@37906
   809
		  val subs = subs_from is "dummy" guh
neuper@37906
   810
		  val tac = guh2rewtac guh subs
neuper@37906
   811
	      in contextthyOK2xml cI (context_thy (pt, pos) tac) end
neuper@37906
   812
		  
neuper@37906
   813
       (*.match the model of a problem at pos p 
neuper@37906
   814
          with the model-pattern of the problem with pblID.*)
neuper@37906
   815
(* val (cI, pos:pos' as (p,p_), guh) =
neuper@37906
   816
       (1, p, kestoreID2guh Pbl_ ["univariate","equation"]);
neuper@37906
   817
   val (cI, pos:pos' as (p,p_), guh) =
neuper@37906
   818
       (1, ([],Pbl), kestoreID2guh Pbl_ ["univariate","equation"]);
neuper@37906
   819
   val (cI, pos:pos' as (p,p_), guh) =
neuper@37906
   820
       (1, ([],Pbl), "pbl_equ_univ");
neuper@37906
   821
   *)
neuper@37906
   822
       | "pbl_" => 
neuper@37906
   823
	 let val ((pt,_),_) = get_calc cI
neuper@37906
   824
	     val pp = par_pblobj pt p
neuper@37906
   825
	     val keID = guh2kestoreID guh
neuper@37906
   826
	     val chd = context_pbl keID pt pp
neuper@37906
   827
	 in matchpbl2xml cI chd end
neuper@37906
   828
(* val (cI, pos:pos' as (p,p_), guh) = 
neuper@37906
   829
       (1, ([],Pbl), kestoreID2guh Met_ ["LinEq", "solve_lineq_equation"]);
neuper@37906
   830
   *)
neuper@37906
   831
       | "met_" => 
neuper@37906
   832
	 let val ((pt,_),_) = get_calc cI
neuper@37906
   833
	     val pp = par_pblobj pt p
neuper@37906
   834
	     val keID = guh2kestoreID guh
neuper@37906
   835
	     val chd = context_met keID pt pp
neuper@37906
   836
	 in matchmet2xml cI chd end)
neuper@37906
   837
    handle _ => sysERROR2xml cI "error in kernel";
neuper@37906
   838
neuper@37906
   839
neuper@37906
   840
(*------------------------------------------------------------------*)
neuper@37906
   841
end
neuper@37906
   842
open interface;
neuper@37906
   843
(*------------------------------------------------------------------*)