src/Tools/isac/Frontend/interface.sml
author Walther Neuper <wneuper@ist.tugraz.at>
Mon, 12 Dec 2016 18:08:13 +0100
changeset 59265 ee68ccda7977
parent 59263 0fde9446eda2
child 59266 56762e8a672e
permissions -rw-r--r--
added structure Chead : CALC_HEAD
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.
wneuper@55502
     3
   This is the only file which does not adhere to Isabelle coding standards,
wneuper@55502
     4
   rather the function names are according to the respective Java methods.
neuper@37906
     5
   authors: Walther Neuper 2002
neuper@37906
     6
   (c) due to copyright terms
neuper@37906
     7
neuper@37947
     8
use"Frontend/interface.sml";
neuper@37906
     9
use"interface.sml";
neuper@37906
    10
*)
neuper@37906
    11
wneuper@59260
    12
signature KERNEL =
neuper@37906
    13
  sig
wneuper@59131
    14
    val appendFormula : calcID -> cterm' -> XML.tree (*unit future*)
wneuper@59129
    15
    val autoCalculate : calcID -> auto -> XML.tree (*unit future*)
wneuper@59153
    16
    val applyTactic : calcID -> pos' -> tac -> XML.tree
wneuper@59153
    17
    val CalcTree : fmz list -> XML.tree
wneuper@59133
    18
    val checkContext : calcID -> pos' -> guh -> XML.tree
wneuper@59153
    19
    val DEconstrCalcTree : calcID -> XML.tree
wneuper@59134
    20
    val fetchApplicableTactics : calcID -> int -> pos' -> XML.tree
wneuper@59136
    21
    val fetchProposedTactic : calcID -> XML.tree
wneuper@59153
    22
    val findFillpatterns : calcID -> errpatID -> XML.tree
wneuper@59136
    23
    val getAccumulatedAsms : calcID -> pos' -> XML.tree
wneuper@59126
    24
    val getActiveFormula : calcID -> XML.tree
wneuper@59136
    25
    val getAssumptions : calcID -> pos' -> XML.tree
wneuper@59127
    26
    val getFormulaeFromTo : calcID -> pos' -> pos' -> int -> bool -> XML.tree
wneuper@59136
    27
    val getTactic : calcID -> pos' -> XML.tree
wneuper@59153
    28
    val initContext : calcID -> ketype -> pos' -> XML.tree
wneuper@59153
    29
    val inputFillFormula: calcID -> string -> XML.tree
wneuper@59131
    30
    val interSteps : calcID -> pos' -> XML.tree
wneuper@59153
    31
    val Iterator : calcID -> XML.tree
wneuper@59153
    32
    val IteratorTEST : calcID -> iterID
wneuper@59153
    33
    val modelProblem : calcID -> XML.tree
wneuper@59262
    34
    val modifyCalcHead : calcID -> Inform.icalhd -> XML.tree
wneuper@59126
    35
    val moveActiveCalcHead : calcID -> XML.tree
wneuper@59126
    36
    val moveActiveDown : calcID -> XML.tree
wneuper@59126
    37
    val moveActiveFormula : calcID -> pos' -> XML.tree
wneuper@59126
    38
    val moveActiveLevelDown : calcID -> XML.tree
wneuper@59126
    39
    val moveActiveLevelUp : calcID -> XML.tree
wneuper@59126
    40
    val moveActiveRoot : calcID -> XML.tree
wneuper@59136
    41
    val moveActiveRootTEST : calcID -> XML.tree
wneuper@59126
    42
    val moveActiveUp : calcID -> XML.tree
wneuper@59126
    43
    val moveCalcHead : calcID -> pos' -> XML.tree
wneuper@59126
    44
    val moveDown : calcID -> pos' -> XML.tree
wneuper@59126
    45
    val moveLevelDown : calcID -> pos' -> XML.tree
wneuper@59126
    46
    val moveLevelUp : calcID -> pos' -> XML.tree
wneuper@59126
    47
    val moveRoot : calcID -> XML.tree
wneuper@59126
    48
    val moveUp : calcID -> pos' -> XML.tree
wneuper@59127
    49
    val refFormula : calcID -> pos' -> XML.tree
wneuper@59153
    50
    val refineProblem : calcID -> pos' -> guh -> XML.tree
wneuper@59131
    51
    val replaceFormula : calcID -> cterm' -> XML.tree
wneuper@59153
    52
    val requestFillformula : calcID -> errpatID * fillpatID -> XML.tree
wneuper@59129
    53
    val resetCalcHead : calcID -> XML.tree
wneuper@59129
    54
    val setContext : calcID -> pos' -> guh -> XML.tree
wneuper@59129
    55
    val setMethod : calcID -> metID -> XML.tree
wneuper@59136
    56
    val setNextTactic : calcID -> tac -> XML.tree
wneuper@59129
    57
    val setProblem : calcID -> pblID -> XML.tree
wneuper@59129
    58
    val setTheory : calcID -> thyID -> XML.tree
wneuper@59261
    59
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
wneuper@59260
    60
    (* NONE *)
wneuper@59260
    61
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
neuper@37906
    62
  end
neuper@37906
    63
wneuper@59260
    64
(**)
wneuper@59260
    65
structure Kernel(**): KERNEL(**) =
neuper@37906
    66
struct
wneuper@59260
    67
(**)
neuper@38056
    68
(* encode "Isabelle"-strings as seen by the user to the
neuper@37906
    69
   format accepted by Isabelle.
neuper@37947
    70
   encode "^" ---> "^^^"; see Knowledge/Atools.thy;
neuper@37906
    71
   called for each cterm', icalhd, fmz in this interface;
wneuper@59154
    72
   + see "fun en/decode" in xmlsrc/mathml.sml *)
wneuper@59262
    73
fun encode_imodel (imodel : Inform.imodel) =
wneuper@59262
    74
  let fun enc (Inform.Given ifos) = Inform.Given (map encode ifos)
wneuper@59262
    75
	| enc (Inform.Find ifos) = Inform.Find (map encode ifos)
wneuper@59262
    76
	| enc (Inform.Relate ifos) = Inform.Relate (map encode ifos)
wneuper@59262
    77
  in map enc imodel:Inform.imodel end;
wneuper@59262
    78
fun encode_icalhd ((pos', headl, imodel, pos_, spec) : Inform.icalhd) =
wneuper@59262
    79
  (pos', encode headl, encode_imodel imodel, pos_, spec) : Inform.icalhd;
neuper@38056
    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*)
wneuper@59260
    93
  (adduserOK2xml cI (add_user (cI:calcID)))
wneuper@59260
    94
  handle _ => sysERROR2xml cI "error in kernel 1";
neuper@37906
    95
fun IteratorTEST (cI:calcID) = add_user (cI:calcID);
neuper@37906
    96
neuper@37906
    97
(*.create a calc-tree; for calls from java: thus ^^^ decoded to ^;
neuper@37906
    98
   compare "fun CalcTreeTEST" which does NOT decode.*)
neuper@38056
    99
fun CalcTree [(fmz, sp) : fmz] (*for several variants lateron*) =
wneuper@59260
   100
  	((let
wneuper@59265
   101
  	    val cs = Chead.nxt_specify_init_calc (encode_fmz (fmz, sp))
wneuper@59260
   102
  	    val cI = add_calc cs (*FIXME.WN.8.03: error-handling missing*)
wneuper@59260
   103
  	   in calctreeOK2xml cI end)
wneuper@59260
   104
  	 handle _ => sysERROR2xml 0 "error in kernel 2")
wneuper@59260
   105
	| CalcTree [] = error "CalcTree: called with []"
neuper@37906
   106
wneuper@59260
   107
fun DEconstrCalcTree (cI:calcID) = deconstructcalctreeOK2xml (del_calc cI);
neuper@37906
   108
fun getActiveFormula (cI:calcID) = iteratorOK2xml cI (get_pos cI 1);
neuper@37906
   109
neuper@37906
   110
fun moveActiveFormula (cI:calcID) (p:pos') =
wneuper@59260
   111
  let val ((pt,_),_) = get_calc cI
wneuper@59260
   112
  in
wneuper@59260
   113
    if existpt' p pt 
wneuper@59260
   114
    then (upd_ipos cI 1 p; iteratorOK2xml cI p)
wneuper@59260
   115
    else sysERROR2xml cI "frontend sends a non-existing pos"
wneuper@59260
   116
  end
neuper@37906
   117
neuper@37906
   118
(*. set the next tactic to be applied: dont't change the calc-tree,
neuper@37906
   119
    but remember the envisaged changes for fun autoCalculate;
neuper@37906
   120
    compare force NextTactic .*)
neuper@37906
   121
fun setNextTactic (cI:calcID) tac =
s1210629013@55402
   122
  let
wneuper@59254
   123
    val ((pt, _), _) = get_calc cI                     (* retrieve calcstate from states *)
wneuper@59254
   124
    val ip = get_pos cI 1                              (* retrieve position from states  *)
wneuper@59260
   125
  in
wneuper@59261
   126
    case Math_Engine.locatetac tac (pt, ip) of                               
wneuper@59260
   127
      ("ok", (tacis, _, _)) =>                         (* update calcstate in states     *)
wneuper@59260
   128
        (upd_calc cI ((pt, ip), tacis); setnexttactic2xml cI "ok")
wneuper@59260
   129
  	| ("unsafe-ok", (tacis, _, _)) =>
wneuper@59260
   130
  	    (upd_calc cI ((pt, ip), tacis); setnexttactic2xml cI "unsafe-ok")
wneuper@59260
   131
  	| ("not-applicable", _) => setnexttactic2xml cI "not-applicable"
wneuper@59260
   132
  	| ("end-of-calculation", _) => setnexttactic2xml cI "end-of-calculation"
wneuper@59260
   133
  	| (msg, _) => sysERROR2xml cI ("setNextTactic: locatetac returns " ^ msg)
wneuper@59260
   134
  end;
neuper@37906
   135
neuper@37906
   136
(*. apply a tactic at a position and update the calc-tree if applicable .*)
neuper@37947
   137
(*WN080226 java-code is missing, errors smltest/Knowledge/polyminus.sml*)
neuper@37906
   138
fun applyTactic (cI:calcID) ip tac =
neuper@42437
   139
  let
neuper@42437
   140
    val ((pt, _), _) = get_calc cI
neuper@42437
   141
	  val p = get_pos cI 1
s1210629013@55402
   142
  in
wneuper@59261
   143
    case Math_Engine.locatetac tac (pt, ip) of
wneuper@59260
   144
	    ("ok", (_, c, ptp as (_, p'))) =>
neuper@42437
   145
	      (upd_calc cI (ptp, []);
neuper@42437
   146
	       upd_ipos cI 1 p';
neuper@42437
   147
	       autocalculateOK2xml cI p (if null c then p' else last_elem c) p')
wneuper@59260
   148
	  | ("unsafe-ok", (_, c, ptp as (_, p'))) =>
neuper@42437
   149
	      (upd_calc cI (ptp, []);
neuper@42437
   150
	       upd_ipos cI 1 p';
neuper@42437
   151
	       autocalculateOK2xml cI p (if null c then p' else last_elem c) p')
wneuper@59260
   152
	  | ("end-of-calculation", (_, c, ptp as (_, p'))) =>
neuper@42437
   153
	      (upd_calc cI (ptp, []);
neuper@42437
   154
	       upd_ipos cI 1 p';
neuper@42437
   155
	       autocalculateOK2xml cI p (if null c then p' else last_elem c) p')
wneuper@59260
   156
	  | (str, _) => autocalculateERROR2xml cI ("applyTactic: locatetac returns " ^ str)
neuper@42437
   157
  end;
neuper@37906
   158
neuper@37906
   159
fun fetchProposedTactic (cI:calcID) =
wneuper@59261
   160
  (case Math_Engine.step (get_pos cI 1) (get_calc cI) of
wneuper@59260
   161
	  ("ok", (tacis, _, _)) =>
wneuper@59260
   162
  	  let
wneuper@59260
   163
  	    val _= upd_tacis cI tacis
wneuper@59260
   164
  	    val (tac, _, _) = last_elem tacis
wneuper@59262
   165
  	  in fetchproposedtacticOK2xml cI tac (Inform.fetchErrorpatterns tac) end
wneuper@59260
   166
	| ("helpless", _) => fetchproposedtacticERROR2xml cI "helpless"
wneuper@59260
   167
	| ("no-fmz-spec", _) => fetchproposedtacticERROR2xml cI "no-fmz-spec"
wneuper@59260
   168
	| ("end-of-calculation", _) =>
wneuper@59260
   169
	  fetchproposedtacticERROR2xml cI "end-of-calculation")
wneuper@59260
   170
   handle _ => sysERROR2xml cI "error in kernel 3";
neuper@42412
   171
wneuper@59123
   172
fun autoCalculate (cI:calcID) auto = (*Future.fork
wneuper@59260
   173
  (fn () => (( *)let
wneuper@59260
   174
     val pold = get_pos cI 1
wneuper@59261
   175
     val x = Math_Engine.autocalc [] pold (get_calc cI) auto
wneuper@59260
   176
  in
wneuper@59260
   177
    case x of
wneuper@59260
   178
      ("ok", c, ptp as (_,p)) =>
wneuper@59260
   179
        (upd_calc cI (ptp, []); upd_ipos cI 1 p;
wneuper@59260
   180
         autocalculateOK2xml cI pold (if null c then pold else last_elem c) p)
wneuper@59260
   181
    | ("end-of-calculation", c, ptp as (_,p)) =>
wneuper@59260
   182
        (upd_calc cI (ptp, []); upd_ipos cI 1 p;
wneuper@59260
   183
         autocalculateOK2xml cI pold (if null c then pold else last_elem c) p)
wneuper@59260
   184
    | (str, _, _) => autocalculateERROR2xml cI str
wneuper@59260
   185
  end (* ) *)
wneuper@59260
   186
  handle ERROR msg => sysERROR2xml cI ("error in kernel 4: " ^ msg)(* )) *);
s1210629013@55402
   187
neuper@37906
   188
fun getTactic cI (p:pos') =
wneuper@59260
   189
  (let 
wneuper@59260
   190
    val ((pt, _), _) = get_calc cI
wneuper@59265
   191
    val (_, tac, _) = Chead.pt_extract (pt, p)
wneuper@59260
   192
  in 
wneuper@59260
   193
    case tac of
wneuper@59260
   194
 	    SOME ta => gettacticOK2xml cI ta
wneuper@59260
   195
 	  | NONE => gettacticERROR2xml cI ("no tactic at position " ^ pos'2str p)
wneuper@59260
   196
  end)
wneuper@59137
   197
    handle _ => sysERROR2xml cI "syserror in getTactic";
neuper@37906
   198
wneuper@59260
   199
(* Fetch tactics to be applied to a particular step.
wneuper@59260
   200
  Requirements are not yet implemented, see ICalcIterator#fetchApplicableTactics"
wneuper@59260
   201
    @see #TACTICS_ALL
wneuper@59260
   202
    @see #TACTICS_CURRENT_THEORY
wneuper@59260
   203
    @see #TACTICS_CURRENT_METHOD  ..the only impl.WN040307
wneuper@59260
   204
  Lucin.sel_appl_atomic_tacs waits to be used here
neuper@42438
   205
*)
wneuper@59260
   206
fun fetchApplicableTactics cI (_(*scope*): int) (p : pos') =
wneuper@59260
   207
  (let val ((pt, _), _) = get_calc cI
wneuper@59260
   208
  in (applicabletacticsOK cI (Lucin.sel_rules pt p))
wneuper@59260
   209
     handle PTREE str => sysERROR2xml cI str
wneuper@59260
   210
  end)
wneuper@59140
   211
    handle _ => sysERROR2xml cI "error in kernel 5";
wneuper@59134
   212
neuper@37906
   213
fun getAssumptions cI (p:pos') =
wneuper@59260
   214
  (let 
wneuper@59260
   215
    val ((pt, _), _) = get_calc cI
wneuper@59265
   216
	  val (_, _, asms) = Chead.pt_extract (pt, p)
wneuper@59260
   217
  in getasmsOK2xml cI asms end)
wneuper@59260
   218
    handle _ => sysERROR2xml cI "syserror in getAssumptions"
neuper@37906
   219
neuper@37906
   220
(*WN0502 @see ME/ctree: type asms: illdesigned, thus no positions returned*)
neuper@37906
   221
fun getAccumulatedAsms cI (p:pos') =
wneuper@59260
   222
  (let
wneuper@59260
   223
    val ((pt, _), _) = get_calc cI
wneuper@59260
   224
    val ass = get_assumptions_ pt p
wneuper@59260
   225
  in getasmsOK2xml cI ass end)
wneuper@59260
   226
    handle _ => sysERROR2xml cI "syserror in getAccumulatedAsms"
neuper@37906
   227
wneuper@59260
   228
fun refFormula cI (p: pos') = (*WN0501 rename to "fun getElement" !*)
wneuper@59152
   229
  (let 
wneuper@59152
   230
    val ((pt, _), _) = get_calc cI
wneuper@59265
   231
	  val (form, _, _) = Chead.pt_extract (pt, p)
wneuper@59152
   232
  in refformulaOK2xml cI p form end)
wneuper@59260
   233
    handle _ => sysERROR2xml cI ("error in kernel 6: refFormula " ^ int2str cI ^ " " ^ pos'2str p)
neuper@37906
   234
wneuper@59260
   235
(* get formulae "from" "to" w.r.t. ordering in Position#compareTo(Position p);
wneuper@59260
   236
   "level" is adjusted such that an "interval" of formulae is returned;
wneuper@59260
   237
   "from" "to" are designed for use by iterators of calcChangedEvent,
wneuper@59260
   238
   thus "from" is the last unchanged position *)
wneuper@59260
   239
fun getFormulaeFromTo cI (([], Pbl) : pos') (to as ([], Pbl) : pos') _ false =
wneuper@59127
   240
    ((let 
wneuper@59260
   241
        val ((pt,_),_) = get_calc cI
wneuper@59265
   242
        val (ModSpec (_, _, headline, _, _, _), _, _) = Chead.pt_extract (pt, to)
wneuper@59260
   243
    in getintervalOK cI [(to, headline)] end)
wneuper@59260
   244
      handle _ => sysERROR2xml cI "error in kernel 7")
wneuper@59260
   245
  | getFormulaeFromTo cI ([], Pbl) ([], Met) _ false =
wneuper@59260
   246
      getFormulaeFromTo cI ([], Pbl) ([], Pbl) (~00000) false
wneuper@59260
   247
  (* ^^^ separated cases, because "from" is _before_ the first elements to be returned*)
wneuper@59260
   248
  | getFormulaeFromTo cI from to level false =
wneuper@59260
   249
    (if from = to 
wneuper@59260
   250
    then sysERROR2xml cI "getFormulaeFromTo: From = To"
wneuper@59260
   251
    else
wneuper@59260
   252
      (case from of
wneuper@59260
   253
         ([], Res) => sysERROR2xml cI ("getFormulaeFromTo does: moveDown " ^ 
wneuper@59260
   254
           "from=([],Res) .. goes beyond result")
wneuper@59260
   255
       | _ => 
wneuper@59260
   256
         let 
wneuper@59260
   257
           val ((pt,_),_) = get_calc cI
wneuper@59260
   258
           val f = move_dn [] pt from
wneuper@59260
   259
           fun max (a,b) = if a < b then b else a
wneuper@59260
   260
           val lev = max (level, max (lev_of from, lev_of to)) (*... must reach margins *)
wneuper@59265
   261
         in getintervalOK cI (Chead.get_interval f to lev pt) end)
wneuper@59137
   262
    handle _ => sysERROR2xml cI "error in getFormulaeFromTo")
neuper@37906
   263
  | getFormulaeFromTo cI from to level true =
wneuper@59137
   264
    sysERROR2xml cI ("getFormulaeFromTo impl.for formulae only, " ^ 
wneuper@59127
   265
      "i.e. last arg only impl. for false, _NOT_ true");
neuper@37906
   266
neuper@37906
   267
fun interSteps cI ip =
wneuper@59260
   268
  (let val ((pt, p), tacis) = get_calc cI
neuper@55471
   269
  in 
neuper@55471
   270
    if (not o is_interpos) ip
neuper@55471
   271
    then interStepsERROR cI ("only formulae with position (_,Res) " ^ 
neuper@55471
   272
      "may have intermediate steps above them")
neuper@55471
   273
    else 
neuper@55471
   274
      let val ip' = lev_pred' pt ip
wneuper@59261
   275
      in case Math_Engine.detailstep pt ip of
wneuper@59260
   276
        ("detailrls", pt, lastpos) =>
wneuper@59260
   277
          (upd_calc cI ((pt, p), tacis); interStepsOK cI ip' ip' lastpos)
wneuper@59260
   278
      | ("no-Rewrite_Set...", _, _) => sysERROR2xml cI "no Rewrite_Set..."
wneuper@59260
   279
      | (_, _, lastpos) => interStepsOK cI ip' ip' lastpos
neuper@55471
   280
      end
neuper@55471
   281
  end)
wneuper@59260
   282
    handle _ => sysERROR2xml cI "error in kernel 8";
neuper@37906
   283
wneuper@59262
   284
fun modifyCalcHead cI (ichd as ((p,_),_,_,_,_) : Inform.icalhd) =
wneuper@59260
   285
  (let 
wneuper@59260
   286
    val ((pt,_),_) = get_calc cI
wneuper@59262
   287
    val (pt, chd as (_,p_,_,_,_,_)) = Inform.input_icalhd pt ichd
wneuper@59260
   288
  in (upd_calc cI ((pt, (p,p_)), []); modifycalcheadOK2xml cI chd) end)
wneuper@59140
   289
    handle _ => sysERROR2xml cI "error in kernel 9";
neuper@37906
   290
wneuper@59260
   291
(* at the activeFormula set the Model, the Guard and the Specification to empty 
wneuper@59260
   292
   and return a CalcHead; the 'origin' remains (for reconstructing all that) *)
wneuper@59260
   293
fun resetCalcHead cI =
wneuper@59260
   294
  (let 
wneuper@59260
   295
    val (ptp,_) = get_calc cI
wneuper@59265
   296
    val ptp = Chead.reset_calchead ptp
wneuper@59265
   297
  in (upd_calc cI (ptp, []); modifycalcheadOK2xml cI (Chead.get_ocalhd ptp)) end)
wneuper@59140
   298
    handle _ => sysERROR2xml cI "error in kernel 10";
neuper@37906
   299
wneuper@59260
   300
(* at the activeFormula insert all the Descriptions in the Model and return a CalcHead;
wneuper@59260
   301
   the Descriptions are for user-guidance; the rest of the items are left empty for user-input *)
wneuper@59260
   302
fun modelProblem cI =
wneuper@59260
   303
  (let val (ptp, _) = get_calc cI
wneuper@59265
   304
  	val ptp = Chead.reset_calchead ptp
wneuper@59265
   305
  	val (_, _, ptp) = Chead.nxt_specif Model_Problem ptp
wneuper@59265
   306
  in (upd_calc cI (ptp, []); modifycalcheadOK2xml cI (Chead.get_ocalhd ptp)) end)
wneuper@59140
   307
    handle _ => sysERROR2xml cI "error in kernel 11";
neuper@37906
   308
wneuper@59166
   309
(* set the UContext determined on a knowledgebrowser to the current calc 
wneuper@59166
   310
  WN0825: not implemented in isac-java.
wneuper@59166
   311
  Recalling the state of development after summer-term 2006 (Robert Koenishofer et.al.):
wneuper@59260
   312
  Main success was showing UContext from Worksheet to the Example/Method/Problem/TheoryBrowser,
wneuper@59166
   313
  while the buttons on these browsers <To Worksheet> <Try Refine> have no
wneuper@59166
   314
  code in isac-java (and only partial, untested code in isabisac).
wneuper@59166
   315
*)
wneuper@59260
   316
fun setContext cI (ip as (_,p_):pos') (guh:guh) =
wneuper@59128
   317
  case (implode o (take_fromto 1 4) o Symbol.explode) guh of
wneuper@59260
   318
    "thy_" =>
wneuper@59260
   319
	    if member op = [Pbl, Met] p_
wneuper@59260
   320
      then message2xml cI "thy-context not to calchead"
wneuper@59260
   321
      else if ip = ([], Res) then message2xml cI "no thy-context at result"
wneuper@59263
   322
	    else if Rtools.no_thycontext guh then message2xml cI ("no thy-context for '" ^ guh ^ "'")
wneuper@59260
   323
  	  else
wneuper@59260
   324
        ((let
wneuper@59260
   325
          val (ptp as (pt, pold), _) = get_calc cI
wneuper@59260
   326
		      val is = get_istate pt ip
wneuper@59263
   327
		      val subs = Rtools.subs_from is "dummy" guh
wneuper@59263
   328
		      val tac = Rtools.guh2rewtac guh subs
wneuper@59260
   329
  	    in
wneuper@59261
   330
          case Math_Engine.locatetac tac (pt, ip) of (*='fun setNextTactic'+step*)
wneuper@59260
   331
		        ("ok", (tacis, c, ptp as (_, p))) =>
wneuper@59260
   332
		          (upd_calc cI ((pt, p), []);
wneuper@59260
   333
		           autocalculateOK2xml cI pold (if null c then pold else last_elem c) p)
wneuper@59260
   334
		      | ("unsafe-ok", (tacis, c, ptp as (_ ,p))) =>
wneuper@59260
   335
  	         (upd_calc cI ((pt, p), []);
wneuper@59260
   336
	            autocalculateOK2xml cI pold (if null c then pold else last_elem c) p)
wneuper@59260
   337
		      | ("end-of-calculation", _) => message2xml cI "end-of-calculation"
wneuper@59260
   338
		      | ("failure", _) => sysERROR2xml cI "failure"
wneuper@59260
   339
		      | ("not-applicable", _) => (*the rule comes from anywhere..*)
wneuper@59260
   340
		          (case applicable_in ip pt tac of
wneuper@59265
   341
		            Chead.Notappl e => message2xml cI ("'" ^ tac2str tac ^ "' not-applicable")
wneuper@59265
   342
	            | Chead.Appl m =>
wneuper@59260
   343
		              let
wneuper@59260
   344
                    val ctxt = get_ctxt pt pold
wneuper@59260
   345
                    val (p, c, _, pt) =
wneuper@59260
   346
                      generate1 (assoc_thy "Isac") m (Uistate, ctxt) ip pt
wneuper@59260
   347
                  in upd_calc cI ((pt, p), []);
neuper@41993
   348
	                  autocalculateOK2xml cI pold (if null c then pold else last_elem c) p
wneuper@59260
   349
		 	            end)
wneuper@59260
   350
	      end
wneuper@59260
   351
     ) handle _ => sysERROR2xml cI "setContext: thy_ ???")
wneuper@59260
   352
  | "pbl_" =>
wneuper@59260
   353
	  ((let
wneuper@59260
   354
        val pI = guh2kestoreID guh
wneuper@59260
   355
	      val ((pt, _), _) = get_calc cI
wneuper@59260
   356
	    in
wneuper@59260
   357
        if member op = [Pbl, Met] p_
wneuper@59260
   358
	      then
wneuper@59261
   359
          let val (pt, chd) = Math_Engine.set_problem pI (pt, ip)
wneuper@59260
   360
		      in (upd_calc cI ((pt, ip), []); modifycalcheadOK2xml cI chd) end
wneuper@59260
   361
	      else sysERROR2xml cI "setContext for pbl requires ActiveFormula on CalcHead"
wneuper@59260
   362
	   end
wneuper@59260
   363
    ) handle _ => sysERROR2xml cI "setContext: pbl_ ???")
wneuper@59260
   364
  | "met_" =>
wneuper@59260
   365
	  ((let
wneuper@59260
   366
        val mI = guh2kestoreID guh
wneuper@59260
   367
	      val ((pt, _), _) = get_calc cI
wneuper@59260
   368
	    in
wneuper@59260
   369
        if member op = [Pbl, Met] p_
wneuper@59260
   370
	      then            
wneuper@59261
   371
          let val (pt, chd) = Math_Engine.set_method mI (pt, ip)
wneuper@59260
   372
		      in (upd_calc cI ((pt, ip), []); modifycalcheadOK2xml cI chd) end
wneuper@59260
   373
	      else sysERROR2xml cI "setContext for met requires ActiveFormula on CalcHead"
wneuper@59260
   374
	    end
wneuper@59260
   375
    ) handle _ => sysERROR2xml cI "setContext: met_ ???")
neuper@37906
   376
neuper@37906
   377
wneuper@59260
   378
(* specify the Method at the activeFormula and return a CalcHead containing the Guard.
wneuper@59260
   379
   WN0512 impl.incomplete, see "nxt_specif (Specify_Method" *)
wneuper@59260
   380
fun setMethod cI mI =
wneuper@59129
   381
  (let 
wneuper@59129
   382
    val ((pt, _), _) = get_calc cI
wneuper@59129
   383
    val ip as (_, p_) = get_pos cI 1
wneuper@59260
   384
  in 
wneuper@59260
   385
    if member op = [Pbl,Met] p_
wneuper@59260
   386
    then 
wneuper@59261
   387
      let val (pt, chd) = Math_Engine.set_method mI (pt, ip)
wneuper@59260
   388
      in (upd_calc cI ((pt, ip), []); modifycalcheadOK2xml cI chd) end
wneuper@59260
   389
    else sysERROR2xml cI "setMethod requires ActiveFormula on CalcHead"
wneuper@59260
   390
  end
wneuper@59260
   391
  ) handle _ => sysERROR2xml cI "error in kernel 12";
neuper@37906
   392
wneuper@59260
   393
(* specify the Problem at the activeFormula and return a CalcHead containing the Model; 
wneuper@59260
   394
   special case of checkContext; WN0512 impl.incomplete, see "nxt_specif (Specify_Problem" *)
wneuper@59260
   395
fun setProblem cI pI =
wneuper@59129
   396
  (let 
wneuper@59129
   397
    val ((pt, _), _) = get_calc cI
wneuper@59129
   398
    val ip as (_, p_) = get_pos cI 1
wneuper@59129
   399
  in 
wneuper@59129
   400
    if member op = [Pbl,Met] p_
wneuper@59129
   401
    then 
wneuper@59261
   402
      let val (pt, chd) = Math_Engine.set_problem pI (pt, ip)
wneuper@59129
   403
	    in (upd_calc cI ((pt, ip), []); modifycalcheadOK2xml cI chd) end
wneuper@59137
   404
    else sysERROR2xml cI "setProblem requires ActiveFormula on CalcHead"
wneuper@59129
   405
  end
wneuper@59140
   406
  ) handle _ => sysERROR2xml cI "error in kernel 13";
neuper@37906
   407
wneuper@59260
   408
(* specify the Theory at the activeFormula and return a CalcHead;
wneuper@59260
   409
   special case of checkContext; WN0512 impl.incomplete, see "nxt_specif (Specify_Method" *)
wneuper@59260
   410
fun setTheory cI tI =
wneuper@59129
   411
  (let 
wneuper@59129
   412
    val ((pt, _), _) = get_calc cI
wneuper@59129
   413
    val ip as (_, p_) = get_pos cI 1
wneuper@59260
   414
  in 
wneuper@59260
   415
    if member op = [Pbl,Met] p_
wneuper@59260
   416
    then 
wneuper@59261
   417
      let val (pt, chd) = Math_Engine.set_theory tI (pt, ip)
wneuper@59260
   418
      in (upd_calc cI ((pt, ip), []); modifycalcheadOK2xml cI chd) end
wneuper@59260
   419
    else sysERROR2xml cI "setProblem requires ActiveFormula on CalcHead"
wneuper@59129
   420
  end
wneuper@59140
   421
  ) handle _ => sysERROR2xml cI "error in kernel 14";
neuper@37906
   422
wneuper@59260
   423
(* refinement for the parent-problem of the position,
wneuper@59260
   424
  Not used in isac-java (see comment WN0825 for setContext) but tryrefine is used within isabisac *)
wneuper@59260
   425
fun refineProblem cI (p, p_) guh =
wneuper@59260
   426
  (let 
wneuper@59260
   427
    val pblID = guh2kestoreID guh
wneuper@59260
   428
	  val ((pt,_),_) = get_calc cI
wneuper@59260
   429
	  val pp = par_pblobj pt p
wneuper@59261
   430
	  val chd = Math_Engine.tryrefine pblID pt (pp, p_)
wneuper@59260
   431
  in contextpblOK2xml cI chd end)
wneuper@59140
   432
    handle _ => sysERROR2xml cI "error in kernel 16";
neuper@37906
   433
neuper@42423
   434
(* append a formula to the calculation *)
s1210629013@55402
   435
fun appendFormula' cI (ifo:cterm') =
neuper@42423
   436
  (let
neuper@42423
   437
    val cs = get_calc cI
wneuper@59260
   438
    val pos = get_pos cI 1
neuper@42423
   439
  in
wneuper@59261
   440
    case Math_Engine.step pos cs of
neuper@37906
   441
	    ("ok", cs') =>
wneuper@59262
   442
	      (case Inform.inform cs' (encode ifo) of
wneuper@59260
   443
	        ("ok", (_(*use in DG !!!*), c, ptp as (_, p))) =>
neuper@42423
   444
	          (upd_calc cI (ptp, []); upd_ipos cI 1 p;
neuper@42423
   445
	          appendformulaOK2xml cI pos (if null c then pos else last_elem c) p)
wneuper@59260
   446
	      | ("same-formula", (_, c, ptp as (_, p))) =>
neuper@42423
   447
	          (upd_calc cI (ptp, []); upd_ipos cI 1 p;
neuper@42423
   448
	          appendformulaOK2xml cI pos (if null c then pos else last_elem c) p)
neuper@42423
   449
	      | (msg, _) => appendformulaERROR2xml cI msg)
wneuper@59260
   450
	  | (msg, _) => appendformulaERROR2xml cI msg
neuper@42423
   451
  end)
wneuper@59140
   452
  handle ERROR msg => sysERROR2xml cI ("error in kernel 17: " ^ msg);
neuper@37906
   453
wneuper@59123
   454
fun appendFormula cI ifo = (*Future.fork (fn () => *)appendFormula' cI ifo(* ) *);
s1210629013@55402
   455
wneuper@59260
   456
(* replace a formula with_in_ a calculation; this applies for initial CAS-commands, too *)
neuper@37906
   457
fun replaceFormula cI (ifo:cterm') =
wneuper@59260
   458
  (let
neuper@42437
   459
    val ((pt, _), _) = get_calc cI
neuper@42437
   460
    val p = get_pos cI 1
neuper@42437
   461
  in
wneuper@59265
   462
    case Inform.inform (([], [], (pt, p)): Chead.calcstate') (encode ifo) of
wneuper@59260
   463
	    ("ok", (_(*tacs used for DG ?*), c, ptp' as (_, p'))) =>
neuper@42437
   464
	      let
neuper@42437
   465
	        val unc = if null (fst p) then p else move_up [] pt p
neuper@42437
   466
	        val _ = upd_calc cI (ptp', [])
neuper@42437
   467
	        val _ = upd_ipos cI 1 p'
neuper@42437
   468
	      in replaceformulaOK2xml cI unc (if null c then unc else last_elem c) p' end
neuper@42437
   469
	  | ("same-formula", _) => replaceformulaERROR2xml cI "formula not changed"
neuper@42437
   470
	  | (msg, _) => replaceformulaERROR2xml cI msg
neuper@42437
   471
  end)
wneuper@59140
   472
    handle _ => sysERROR2xml cI "error in kernel 18";
neuper@37906
   473
wneuper@59260
   474
(*** CalcIterator
neuper@37906
   475
    moveActive*: set the pos' of the active formula stored with the calctree
neuper@37906
   476
                 could take pos' as argument for consistency checks
neuper@37906
   477
    move*:       compute the new iterator from the old one on the fly
wneuper@59260
   478
***)
neuper@37906
   479
neuper@37906
   480
fun moveActiveRoot cI =
wneuper@59260
   481
  (let val _ = upd_ipos cI 1 ([], Pbl)
wneuper@59260
   482
  in iteratorOK2xml cI ([], Pbl) end)
wneuper@59260
   483
  handle _ => sysERROR2xml cI "error in kernel 19"
neuper@37906
   484
fun moveRoot cI =
wneuper@59260
   485
  (iteratorOK2xml cI ([], Pbl))
wneuper@59260
   486
  handle _ => sysERROR2xml cI "";
neuper@37906
   487
fun moveActiveRootTEST cI =
wneuper@59260
   488
  (let val _ = upd_ipos cI 1 ([], Pbl)
wneuper@59260
   489
  in iteratorOK2xml cI ([], Pbl) end)
wneuper@59260
   490
  handle _ => sysERROR2xml cI "error in kernel 20"
neuper@37906
   491
neuper@37906
   492
fun moveActiveDown cI =
wneuper@59260
   493
  ((let 
wneuper@59260
   494
    val ((pt, _), _) = get_calc cI
neuper@37906
   495
	  val ip' = move_dn [] pt (get_pos cI 1)
neuper@37906
   496
	  val _ = upd_ipos cI 1 ip'
wneuper@59260
   497
  in iteratorOK2xml cI ip' end)
wneuper@59260
   498
    handle (PTREE _) => iteratorERROR2xml cI) handle _ => sysERROR2xml cI "error in kernel 21"
wneuper@59260
   499
fun moveDown cI p =
wneuper@59260
   500
  ((let 
wneuper@59260
   501
    val ((pt, _), _) = get_calc cI
neuper@37906
   502
	  val ip' = move_dn [] pt p
wneuper@59260
   503
  in iteratorOK2xml cI ip' end)
wneuper@59260
   504
    handle (PTREE _) => iteratorERROR2xml cI) handle _ => sysERROR2xml cI "error in kernel 22"
neuper@37906
   505
neuper@37906
   506
fun moveActiveLevelDown cI =
wneuper@59260
   507
  ((let 
wneuper@59260
   508
    val ((pt, _) ,_) = get_calc cI
neuper@37906
   509
	  val ip' = movelevel_dn [] pt (get_pos cI 1)
neuper@37906
   510
	  val _ = upd_ipos cI 1 ip'
wneuper@59260
   511
  in iteratorOK2xml cI ip' end)
wneuper@59260
   512
    handle (PTREE _) => iteratorERROR2xml cI) handle _ => sysERROR2xml cI "error in kernel 23"
wneuper@59260
   513
fun moveLevelDown cI p =
wneuper@59260
   514
  ((let 
wneuper@59260
   515
    val ((pt, _), _) = get_calc cI
wneuper@59260
   516
    val ip' = movelevel_dn [] pt p
wneuper@59260
   517
  in iteratorOK2xml cI ip' end)
wneuper@59260
   518
    handle (PTREE _) => iteratorERROR2xml cI) handle _ => sysERROR2xml cI "error in kernel 24"
neuper@37906
   519
neuper@37906
   520
fun moveActiveUp cI =
wneuper@59260
   521
  ((let 
wneuper@59260
   522
    val ((pt, _), _) = get_calc cI
neuper@37906
   523
	  val ip' = move_up [] pt (get_pos cI 1)
neuper@37906
   524
	  val _ = upd_ipos cI 1 ip'
wneuper@59260
   525
  in iteratorOK2xml cI ip' end)
wneuper@59260
   526
    handle PTREE _ => iteratorERROR2xml cI) handle _ => sysERROR2xml cI "error in kernel 25"
wneuper@59260
   527
fun moveUp cI p =
wneuper@59260
   528
  ((let 
wneuper@59260
   529
    val ((pt, _), _) = get_calc cI
neuper@37906
   530
	  val ip' = move_up [] pt p
wneuper@59260
   531
  in iteratorOK2xml cI ip' end)
wneuper@59260
   532
    handle PTREE _ => iteratorERROR2xml cI) handle _ => sysERROR2xml cI "error in kernel 26"
neuper@37906
   533
neuper@37906
   534
fun moveActiveLevelUp cI =
wneuper@59260
   535
  ((let
wneuper@59260
   536
    val ((pt, _), _) = get_calc cI
wneuper@59260
   537
    val ip' = movelevel_up [] pt (get_pos cI 1)
wneuper@59260
   538
    val _ = upd_ipos cI 1 ip'
wneuper@59260
   539
  in iteratorOK2xml cI ip' end)
wneuper@59260
   540
    handle PTREE _ => iteratorERROR2xml cI) handle _ => sysERROR2xml cI "error in kernel 27"
wneuper@59260
   541
fun moveLevelUp cI p =
wneuper@59260
   542
  ((let
wneuper@59260
   543
    val ((pt, _), _) = get_calc cI
neuper@37906
   544
	  val ip' = movelevel_up [] pt p
wneuper@59260
   545
  in iteratorOK2xml cI ip' end)
wneuper@59260
   546
    handle PTREE _ => iteratorERROR2xml cI) handle _ => sysERROR2xml cI "error in kernel 28"
neuper@37906
   547
neuper@37906
   548
fun moveActiveCalcHead cI =
wneuper@59260
   549
  ((let 
wneuper@59260
   550
    val ((pt, _), _) = get_calc cI
neuper@37906
   551
	  val ip' = movecalchd_up pt (get_pos cI 1)
neuper@37906
   552
	  val _ = upd_ipos cI 1 ip'
wneuper@59260
   553
  in iteratorOK2xml cI ip' end)
wneuper@59260
   554
    handle PTREE _ => iteratorERROR2xml cI) handle _ => sysERROR2xml cI "error in kernel 29"
wneuper@59260
   555
fun moveCalcHead cI p =
wneuper@59260
   556
  ((let
wneuper@59260
   557
    val ((pt, _), _) = get_calc cI
neuper@37906
   558
	  val ip' = movecalchd_up pt p
wneuper@59260
   559
  in iteratorOK2xml cI ip' end)
wneuper@59260
   560
    handle PTREE _ => iteratorERROR2xml cI) handle _ => sysERROR2xml cI "error in kernel 30"
neuper@37906
   561
s1210629013@55402
   562
(*.initContext Thy_ is conceptually impossible at [Pbl,Met]
neuper@37906
   563
   and at positions with Check_Postcond and End_Trans;
neuper@37906
   564
   at possible pos's there can be NO rewrite (returned as a context, too).*)
wneuper@59260
   565
fun initContext cI Thy_ (pos as (p, p_):pos') =
wneuper@55499
   566
    ((if member op = [Pbl, Met] p_
wneuper@59138
   567
      then message2xml cI "thy-context not to calchead"
wneuper@59138
   568
      else if pos = ([], Res) then message2xml cI "no thy-context at result"
wneuper@55499
   569
      else 
wneuper@55499
   570
        let val cs as (ptp as (pt, _), _) = get_calc cI
wneuper@55499
   571
        in 
wneuper@55499
   572
          if exist_lev_on' pt pos 
wneuper@55499
   573
          then 
wneuper@55499
   574
            let
wneuper@55499
   575
              val pos' = lev_on' pt pos
wneuper@59263
   576
              val tac = Rtools.get_tac_checked pt pos'
wneuper@55499
   577
            in
wneuper@55499
   578
              if is_rewtac tac
wneuper@59263
   579
              then contextthyOK2xml cI (Rtools.context_thy (pt, pos) tac)
wneuper@59138
   580
              else message2xml cI ("no thy-context at tac '" ^ tac2str tac ^ "'")
wneuper@55499
   581
            end
wneuper@55499
   582
          else if is_curr_endof_calc pt pos
wneuper@55499
   583
          then 
wneuper@59261
   584
            case Math_Engine.step pos cs of
wneuper@55499
   585
              ("ok", (tacis, _, (pt, _))) =>
wneuper@55499
   586
                let val tac = fst3 (last_elem tacis)
wneuper@55499
   587
                in 
wneuper@55499
   588
                  if is_rewtac tac
wneuper@59263
   589
                  then contextthyOK2xml cI (Rtools.context_thy ptp tac)
wneuper@59138
   590
                  else message2xml cI ("no thy-context at tac '" ^ tac2str tac ^ "'")
wneuper@55499
   591
                end
wneuper@59138
   592
            | (msg, _) => message2xml cI msg
wneuper@59138
   593
          else message2xml cI "no thy-context at this position"
wneuper@55499
   594
        end)
wneuper@59140
   595
        handle _ => sysERROR2xml cI "error in kernel 31")
wneuper@59260
   596
  | initContext cI Pbl_ (p, p_) =
wneuper@59260
   597
    ((let 
wneuper@59260
   598
      val ((pt, _), _) = get_calc cI
wneuper@59260
   599
      val pp = par_pblobj pt p
wneuper@59261
   600
      val chd = Math_Engine.initcontext_pbl pt (pp,p_)
wneuper@59260
   601
    in contextpblOK2xml cI chd end)
wneuper@59260
   602
      handle _ => sysERROR2xml cI "error in kernel 32")
wneuper@59260
   603
  | initContext cI Met_ (p, p_) =
wneuper@59260
   604
    ((let
wneuper@59260
   605
      val ((pt,_),_) = get_calc cI
wneuper@59260
   606
      val pp = par_pblobj pt p
wneuper@59261
   607
      val chd = Math_Engine.initcontext_met pt (pp,p_)
wneuper@59260
   608
    in contextmetOK2xml cI chd end)
wneuper@59260
   609
      handle _ => sysERROR2xml cI "error in kernel 33");
neuper@37906
   610
neuper@42430
   611
(* match a theorem, a ruleset (etc., selected in the knowledge-browser)
neuper@42430
   612
  with the formula in the focus on the worksheet;
neuper@42430
   613
  string contains the thy, thus it is unique as thmID, rlsID for this thy;
neuper@42430
   614
  take the substitution from the istate of the formula *)
wneuper@59260
   615
fun checkContext cI (pos as (p, p_)) guh =
wneuper@59132
   616
  case (implode o (take_fromto 1 4) o Symbol.explode) guh of
wneuper@59132
   617
	  "thy_" =>
wneuper@59260
   618
	    if member op = [Pbl, Met] p_
wneuper@59138
   619
      then message2xml cI "thy-context not to calchead"
wneuper@59138
   620
      else if pos = ([],Res) then message2xml cI "no thy-context at result"
wneuper@59263
   621
      else if Rtools.no_thycontext guh then message2xml cI ("no thy-context for '" ^ guh ^ "'")
wneuper@59132
   622
      else 
wneuper@59132
   623
        ((let 
wneuper@59260
   624
          val ((pt,_),_) = get_calc cI
wneuper@59132
   625
    		  val is = get_istate pt pos
wneuper@59263
   626
    		  val subs = Rtools.subs_from is "dummy" guh
wneuper@59263
   627
    		  val tac = Rtools.guh2rewtac guh subs
wneuper@59263
   628
	      in contextthyOK2xml cI (Rtools.context_thy (pt, pos) tac) end
wneuper@59140
   629
        ) handle _ => sysERROR2xml cI "error in kernel 34")
wneuper@59132
   630
    (*.match the model of a problem at pos p
wneuper@59132
   631
      with the model-pattern of the problem with pblID.*)
wneuper@59132
   632
  | "pbl_" =>
wneuper@59132
   633
	    ((let 
wneuper@59260
   634
	      val ((pt, _), _) = get_calc cI
wneuper@59132
   635
  	    val pp = par_pblobj pt p
wneuper@59132
   636
  	    val keID = guh2kestoreID guh
wneuper@59261
   637
  	    val chd = Math_Engine.context_pbl keID pt pp
wneuper@59172
   638
	    in contextpblOK2xml cI chd end
wneuper@59140
   639
      ) handle _ => sysERROR2xml cI "error in kernel 35")
wneuper@59132
   640
   | "met_" =>
wneuper@59132
   641
	     ((let 
wneuper@59260
   642
	       val ((pt, _), _) = get_calc cI
wneuper@59132
   643
	       val pp = par_pblobj pt p
wneuper@59132
   644
	       val keID = guh2kestoreID guh
wneuper@59261
   645
	       val chd = Math_Engine.context_met keID pt pp
wneuper@59172
   646
	     in contextmetOK2xml cI chd end
wneuper@59140
   647
      ) handle _ => sysERROR2xml cI "error in kernel 36")
wneuper@59137
   648
   | str => sysERROR2xml cI ("checkContext: str = " ^ str)
neuper@37906
   649
s1210629013@55402
   650
(* for an errpatID find "(fillpatID, fillform)" list
wneuper@59260
   651
   which dedicated to this errpat and which is applicable at current formula *)
s1210629013@55402
   652
fun findFillpatterns cI errpatID =
neuper@42430
   653
  let
neuper@42430
   654
    val ((pt, _), _) = get_calc cI
wneuper@59260
   655
		val pos = get_pos cI 1;
wneuper@59262
   656
		val fillpats = Inform.find_fillpatterns (pt, pos) errpatID
wneuper@59260
   657
  in findFillpatterns2xml cI (map #1 fillpats) end
neuper@42432
   658
neuper@42432
   659
(* given a fillpatID propose a fillform to the learner on the worksheet;
neuper@42433
   660
  the "ctree" is extended with fillpat and "ostate Inconsistent",
neuper@42433
   661
  the "istate" is copied and NOT updated;
neuper@42432
   662
  returns CalcChanged.
neuper@42432
   663
  arg errpatID: required because there is no dialog-related state in the math-kernel.
neuper@42432
   664
*)
neuper@42432
   665
fun requestFillformula cI (errpatID, fillpatID) =
neuper@42432
   666
  let
neuper@42432
   667
    val ((pt, _), _) = get_calc cI
neuper@42436
   668
		val pos = get_pos cI 1
wneuper@59262
   669
    val fillforms = Inform.find_fillpatterns (pt, pos) errpatID
neuper@42432
   670
  in
s1210629013@55402
   671
    case filter ((curry op = fillpatID) o
neuper@42433
   672
        (#1: (fillpatID * term * thm * (subs option) -> fillpatID))) fillforms of
neuper@42434
   673
      (_, fillform, thm, sube_opt) :: _ =>
neuper@42432
   674
        let
wneuper@59250
   675
          val (pt, pos') = generate_inconsistent_rew (sube_opt, thm''_of_thm thm)
neuper@42437
   676
            fillform (get_loc pt pos) pos pt
neuper@42432
   677
        in
neuper@42432
   678
          (upd_calc cI ((pt, pos'), []); (*upd_ipos cI 1 pos';*)
neuper@42432
   679
           autocalculateOK2xml cI pos pos' pos')
neuper@42432
   680
        end
s1210629013@55402
   681
    | _ => autocalculateERROR2xml cI "no fillpattern found"
s1210629013@55402
   682
  end;
neuper@37906
   683
neuper@42437
   684
(* input a formula which exactly fills the gaps in a "fillformula"
neuper@42437
   685
   presented to the learner immediately before by "requestFillformula (errpatID, fillpatID)":
neuper@42437
   686
   errpatID: lhs of the respective thm = lhs of fillformula with fillpatID.
neuper@42437
   687
   The respective thm is stored in the ctree. *)
neuper@42437
   688
fun inputFillFormula cI ifo =
neuper@42437
   689
  let
neuper@42437
   690
    val ((pt, _), _) = get_calc cI
neuper@42437
   691
    val pos = get_pos cI 1;
neuper@42437
   692
  in
wneuper@59262
   693
    case Inform.is_exactly_equal (pt, pos) ifo of
neuper@42437
   694
      ("ok", tac) =>
neuper@42437
   695
        let
neuper@42437
   696
        in (* cp from applyTactic *)
wneuper@59261
   697
          case Math_Engine.locatetac tac (pt, pos) of
neuper@42437
   698
            ("ok", (_, c, ptp as (_,p'))) =>
neuper@42437
   699
              (upd_calc cI (ptp, []);
neuper@42437
   700
               upd_ipos cI 1 p';
neuper@42437
   701
               autocalculateOK2xml cI pos (if null c then p' else last_elem c) p')
neuper@42437
   702
          | ("unsafe-ok", (_, c, ptp as (_,p'))) =>
neuper@42437
   703
              (upd_calc cI (ptp, []);
neuper@42437
   704
               upd_ipos cI 1 p';
neuper@42437
   705
               autocalculateOK2xml cI pos (if null c then p' else last_elem c) p')
neuper@42437
   706
          | ("end-of-calculation", (_, c, ptp as (_,p'))) =>
neuper@42437
   707
              (upd_calc cI (ptp, []);
neuper@42437
   708
               upd_ipos cI 1 p';
neuper@42437
   709
               autocalculateOK2xml cI pos (if null c then p' else last_elem c) p')
wneuper@59260
   710
          | _ => autocalculateERROR2xml cI "failure"
neuper@42437
   711
        end
wneuper@59138
   712
    | (msg, _) => message2xml cI msg
wneuper@59260
   713
  end
wneuper@59260
   714
(**)
neuper@37906
   715
end
wneuper@59260
   716
(**)