src/Tools/isac/BridgeLibisabelle/interface.sml
changeset 60549 c0a775618258
parent 60266 4921574fd67f
child 60557 0be383bdb883
equal deleted inserted replaced
60548:5765bd0f7055 60549:c0a775618258
     6    as long as many tests imitate interaction via libisabelle.
     6    as long as many tests imitate interaction via libisabelle.
     7 *)
     7 *)
     8 
     8 
     9 signature KERNEL =
     9 signature KERNEL =
    10   sig
    10   sig
    11     val appendFormula : calcID -> TermC.as_string -> XML.tree (*unit future*)
    11     val appendFormula : States.calcID -> TermC.as_string -> XML.tree (*unit future*)
    12     val autoCalculate : calcID -> Solve.auto -> XML.tree (*unit future*)
    12     val autoCalculate : States.calcID -> Solve.auto -> XML.tree (*unit future*)
    13     val applyTactic : calcID -> Pos.pos' -> Tactic.input -> XML.tree
    13     val applyTactic : States.calcID -> Pos.pos' -> Tactic.input -> XML.tree
    14     val CalcTree : Formalise.T list -> XML.tree
    14     val CalcTree : Formalise.T list -> XML.tree
    15     val DEconstrCalcTree : calcID -> XML.tree
    15     val DEconstrCalcTree : States.calcID -> XML.tree
    16     val fetchApplicableTactics : calcID -> int -> Pos.pos' -> XML.tree
    16     val fetchApplicableTactics : States.calcID -> int -> Pos.pos' -> XML.tree
    17     val fetchProposedTactic : calcID -> XML.tree
    17     val fetchProposedTactic : States.calcID -> XML.tree
    18     val findFillpatterns : calcID -> Error_Pattern_Def.id -> XML.tree
    18     val findFillpatterns : States.calcID -> Error_Pattern_Def.id -> XML.tree
    19     val getAccumulatedAsms : calcID -> Pos.pos' -> XML.tree
    19     val getAccumulatedAsms : States.calcID -> Pos.pos' -> XML.tree
    20     val getActiveFormula : calcID -> XML.tree
    20     val getActiveFormula : States.calcID -> XML.tree
    21     val getAssumptions : calcID -> Pos.pos' -> XML.tree
    21     val getAssumptions : States.calcID -> Pos.pos' -> XML.tree
    22     val getFormulaeFromTo : calcID -> Pos.pos' -> Pos.pos' -> int -> bool -> XML.tree
    22     val getFormulaeFromTo : States.calcID -> Pos.pos' -> Pos.pos' -> int -> bool -> XML.tree
    23     val getTactic : calcID -> Pos.pos' -> XML.tree
    23     val getTactic : States.calcID -> Pos.pos' -> XML.tree
    24     val inputFillFormula: calcID -> string -> XML.tree
    24     val inputFillFormula: States.calcID -> string -> XML.tree
    25     val interSteps : calcID -> Pos.pos' -> XML.tree
    25     val interSteps : States.calcID -> Pos.pos' -> XML.tree
    26     val Iterator : calcID -> XML.tree
    26     val Iterator : States.calcID -> XML.tree
    27     val IteratorTEST : calcID -> iterID
    27     val IteratorTEST : States.calcID -> States.iterID
    28     val modelProblem : calcID -> XML.tree
    28     val modelProblem : States.calcID -> XML.tree
    29     val modifyCalcHead : calcID -> P_Spec.icalhd -> XML.tree
    29     val modifyCalcHead : States.calcID -> P_Spec.icalhd -> XML.tree
    30     val moveActiveCalcHead : calcID -> XML.tree
    30     val moveActiveCalcHead : States.calcID -> XML.tree
    31     val moveActiveDown : calcID -> XML.tree
    31     val moveActiveDown : States.calcID -> XML.tree
    32     val moveActiveFormula : calcID -> Pos.pos' -> XML.tree
    32     val moveActiveFormula : States.calcID -> Pos.pos' -> XML.tree
    33     val moveActiveLevelDown : calcID -> XML.tree
    33     val moveActiveLevelDown : States.calcID -> XML.tree
    34     val moveActiveLevelUp : calcID -> XML.tree
    34     val moveActiveLevelUp : States.calcID -> XML.tree
    35     val moveActiveRoot : calcID -> XML.tree
    35     val moveActiveRoot : States.calcID -> XML.tree
    36     val moveActiveRootTEST : calcID -> XML.tree
    36     val moveActiveRootTEST : States.calcID -> XML.tree
    37     val moveActiveUp : calcID -> XML.tree
    37     val moveActiveUp : States.calcID -> XML.tree
    38     val moveCalcHead : calcID -> Pos.pos' -> XML.tree
    38     val moveCalcHead : States.calcID -> Pos.pos' -> XML.tree
    39     val moveDown : calcID -> Pos.pos' -> XML.tree
    39     val moveDown : States.calcID -> Pos.pos' -> XML.tree
    40     val moveLevelDown : calcID -> Pos.pos' -> XML.tree
    40     val moveLevelDown : States.calcID -> Pos.pos' -> XML.tree
    41     val moveLevelUp : calcID -> Pos.pos' -> XML.tree
    41     val moveLevelUp : States.calcID -> Pos.pos' -> XML.tree
    42     val moveRoot : calcID -> XML.tree
    42     val moveRoot : States.calcID -> XML.tree
    43     val moveUp : calcID -> Pos.pos' -> XML.tree
    43     val moveUp : States.calcID -> Pos.pos' -> XML.tree
    44     val refFormula : calcID -> Pos.pos' -> XML.tree
    44     val refFormula : States.calcID -> Pos.pos' -> XML.tree
    45     val refineProblem : calcID -> Pos.pos' -> Check_Unique.id -> XML.tree
    45     val refineProblem : States.calcID -> Pos.pos' -> Check_Unique.id -> XML.tree
    46     val replaceFormula : calcID -> TermC.as_string -> XML.tree
    46     val replaceFormula : States.calcID -> TermC.as_string -> XML.tree
    47     val requestFillformula : calcID -> Error_Pattern_Def.id * Error_Pattern_Def.fill_in_id -> XML.tree
    47     val requestFillformula : States.calcID -> Error_Pattern_Def.id * Error_Pattern_Def.fill_in_id -> XML.tree
    48     val resetCalcHead : calcID -> XML.tree
    48     val resetCalcHead : States.calcID -> XML.tree
    49     val setMethod : calcID -> MethodC.id -> XML.tree
    49     val setMethod : States.calcID -> MethodC.id -> XML.tree
    50     val setNextTactic : calcID -> Tactic.input -> XML.tree
    50     val setNextTactic : States.calcID -> Tactic.input -> XML.tree
    51     val setProblem : calcID -> Problem.id -> XML.tree
    51     val setProblem : States.calcID -> Problem.id -> XML.tree
    52     val setTheory : calcID -> ThyC.id -> XML.tree
    52     val setTheory : States.calcID -> ThyC.id -> XML.tree
    53   end
    53   end
    54 
    54 
    55 (**)
    55 (**)
    56 structure Kernel(**): KERNEL(**) =
    56 structure Kernel(**): KERNEL(**) =
    57 struct
    57 struct
    65 
    65 
    66 (*.'Iterator 1' must exist with each CalcTree;
    66 (*.'Iterator 1' must exist with each CalcTree;
    67    the only for updating the calc-tree
    67    the only for updating the calc-tree
    68    WN.0411: only 'Iterator 1' is stored,
    68    WN.0411: only 'Iterator 1' is stored,
    69    all others are just calculated on the fly
    69    all others are just calculated on the fly
    70    TODO: adapt Iterator, add_user(= add_iterator!),etc. accordingly .*)
    70    TODO: adapt Iterator, States.add_user(= add_iterator!),etc. accordingly .*)
    71 fun Iterator cI = (*returned ID unnecessary after WN.0411*)
    71 fun Iterator cI = (*returned ID unnecessary after WN.0411*)
    72   case \<^try>\<open> (adduserOK2xml cI (add_user cI ))\<close> of
    72   case \<^try>\<open> (adduserOK2xml cI (States.add_user cI ))\<close> of
    73     SOME xml => xml
    73     SOME xml => xml
    74   | NONE => sysERROR2xml cI "error in kernel 1";
    74   | NONE => sysERROR2xml cI "error in kernel 1";
    75 fun IteratorTEST cI = add_user cI;
    75 fun IteratorTEST cI = States.add_user cI;
    76 
    76 
    77 (* create a calc-tree; compare "fun CalcTreeTEST" *)
    77 (* create a calc-tree; compare "fun CalcTreeTEST" *)
    78 fun CalcTree [(fmz, sp)] (*for several variants lateron*) =
    78 fun CalcTree [(fmz, sp)] (*for several variants lateron*) =
    79    (case \<^try>\<open>
    79    (case \<^try>\<open>
    80     	let
    80     	let
    81     	    val cs = Step_Specify.nxt_specify_init_calc (fmz, sp)
    81     	    val cs = Step_Specify.nxt_specify_init_calc (fmz, sp)
    82     	    val cI = add_calc cs (*FIXME.WN.8.03: error-handling missing*)
    82     	    val cI = States.add_calc cs (*FIXME.WN.8.03: error-handling missing*)
    83     	   in calctreeOK2xml cI end
    83     	   in calctreeOK2xml cI end
    84       \<close> of
    84       \<close> of
    85       SOME xml => xml
    85       SOME xml => xml
    86     | NONE => sysERROR2xml 0 "error in kernel 2")
    86     | NONE => sysERROR2xml 0 "error in kernel 2")
    87 	| CalcTree [] = raise ERROR "CalcTree: called with []"
    87 	| CalcTree [] = raise ERROR "CalcTree: called with []"
    88 	| CalcTree _ = raise ERROR "CalcTree: undef.case in fun.def"
    88 	| CalcTree _ = raise ERROR "CalcTree: undef.case in fun.def"
    89 
    89 
    90 fun DEconstrCalcTree cI = deconstructcalctreeOK2xml (del_calc cI);
    90 fun DEconstrCalcTree cI = deconstructcalctreeOK2xml (States.del_calc cI);
    91 fun getActiveFormula cI = iteratorOK2xml cI (get_pos cI 1);
    91 fun getActiveFormula cI = iteratorOK2xml cI (States.get_pos cI 1);
    92 
    92 
    93 fun moveActiveFormula cI p =
    93 fun moveActiveFormula cI p =
    94   let val ((pt,_),_) = get_calc cI
    94   let val ((pt,_),_) = States.get_calc cI
    95   in
    95   in
    96     if existpt' p pt 
    96     if existpt' p pt 
    97     then (upd_ipos cI 1 p; iteratorOK2xml cI p)
    97     then (States.upd_ipos cI 1 p; iteratorOK2xml cI p)
    98     else sysERROR2xml cI "frontend sends a non-existing pos"
    98     else sysERROR2xml cI "frontend sends a non-existing pos"
    99   end
    99   end
   100 
   100 
   101 (*. set the next tactic to be applied: dont't change the calc-tree,
   101 (*. set the next tactic to be applied: dont't change the calc-tree,
   102     but remember the envisaged changes for fun autoCalculate;
   102     but remember the envisaged changes for fun autoCalculate;
   103     compare force NextTactic .*)
   103     compare force NextTactic .*)
   104 fun setNextTactic cI tac =
   104 fun setNextTactic cI tac =
   105   let
   105   let
   106     val ((pt, _), _) = get_calc cI                     (* retrieve Calc.state_pre from states *)
   106     val ((pt, _), _) = States.get_calc cI                     (* retrieve Calc.state_pre from states *)
   107     val ip = get_pos cI 1                              (* retrieve position from states  *)
   107     val ip = States.get_pos cI 1                              (* retrieve position from states  *)
   108   in
   108   in
   109     case Step.by_tactic tac (pt, ip) of                               
   109     case Step.by_tactic tac (pt, ip) of                               
   110       ("ok", (tacis, _, _)) =>                         (* update Calc.state_pre in states     *)
   110       ("ok", (tacis, _, _)) =>                         (* update Calc.state_pre in states     *)
   111         (upd_calc cI ((pt, ip), tacis); setnexttactic2xml cI "ok")
   111         (States.upd_calc cI ((pt, ip), tacis); setnexttactic2xml cI "ok")
   112   	| ("unsafe-ok", (tacis, _, _)) =>
   112   	| ("unsafe-ok", (tacis, _, _)) =>
   113   	    (upd_calc cI ((pt, ip), tacis); setnexttactic2xml cI "unsafe-ok")
   113   	    (States.upd_calc cI ((pt, ip), tacis); setnexttactic2xml cI "unsafe-ok")
   114   	| ("not-applicable", _) => setnexttactic2xml cI "not-applicable"
   114   	| ("not-applicable", _) => setnexttactic2xml cI "not-applicable"
   115   	| ("end-of-calculation", _) => setnexttactic2xml cI "end-of-calculation"
   115   	| ("end-of-calculation", _) => setnexttactic2xml cI "end-of-calculation"
   116   	| (msg, _) => sysERROR2xml cI ("setNextTactic: Step.by_tactic returns " ^ msg)
   116   	| (msg, _) => sysERROR2xml cI ("setNextTactic: Step.by_tactic returns " ^ msg)
   117   end;
   117   end;
   118 
   118 
   119 (*. apply a tactic at a position and update the calc-tree if applicable .*)
   119 (*. apply a tactic at a position and update the calc-tree if applicable .*)
   120 (*WN080226 java-code is missing, errors smltest/Knowledge/polyminus.sml*)
   120 (*WN080226 java-code is missing, errors smltest/Knowledge/polyminus.sml*)
   121 fun applyTactic cI ip tac =
   121 fun applyTactic cI ip tac =
   122   let
   122   let
   123     val ((pt, _), _) = get_calc cI
   123     val ((pt, _), _) = States.get_calc cI
   124 	  val p = get_pos cI 1
   124 	  val p = States.get_pos cI 1
   125   in
   125   in
   126     case Step.by_tactic tac (pt, ip) of
   126     case Step.by_tactic tac (pt, ip) of
   127 	    ("ok", (_, c, ptp as (_, p'))) =>
   127 	    ("ok", (_, c, ptp as (_, p'))) =>
   128 	      (upd_calc cI (ptp, []);
   128 	      (States.upd_calc cI (ptp, []);
   129 	       upd_ipos cI 1 p';
   129 	       States.upd_ipos cI 1 p';
   130 	       autocalculateOK2xml cI p (if null c then p' else last_elem c) p')
   130 	       autocalculateOK2xml cI p (if null c then p' else last_elem c) p')
   131 	  | ("unsafe-ok", (_, c, ptp as (_, p'))) =>
   131 	  | ("unsafe-ok", (_, c, ptp as (_, p'))) =>
   132 	      (upd_calc cI (ptp, []);
   132 	      (States.upd_calc cI (ptp, []);
   133 	       upd_ipos cI 1 p';
   133 	       States.upd_ipos cI 1 p';
   134 	       autocalculateOK2xml cI p (if null c then p' else last_elem c) p')
   134 	       autocalculateOK2xml cI p (if null c then p' else last_elem c) p')
   135 	  | ("end-of-calculation", (_, c, ptp as (_, p'))) =>
   135 	  | ("end-of-calculation", (_, c, ptp as (_, p'))) =>
   136 	      (upd_calc cI (ptp, []);
   136 	      (States.upd_calc cI (ptp, []);
   137 	       upd_ipos cI 1 p';
   137 	       States.upd_ipos cI 1 p';
   138 	       autocalculateOK2xml cI p (if null c then p' else last_elem c) p')
   138 	       autocalculateOK2xml cI p (if null c then p' else last_elem c) p')
   139 	  | (str, _) => autocalculateERROR2xml cI ("applyTactic: Step.by_tactic returns " ^ str)
   139 	  | (str, _) => autocalculateERROR2xml cI ("applyTactic: Step.by_tactic returns " ^ str)
   140   end;
   140   end;
   141 
   141 
   142 fun fetchProposedTactic cI =
   142 fun fetchProposedTactic cI =
   143   case \<^try>\<open>
   143   case \<^try>\<open>
   144     (case Step.do_next (get_pos cI 1) (get_calc cI) of
   144     (case Step.do_next (States.get_pos cI 1) (States.get_calc cI) of
   145   	  ("ok", (tacis, _, _)) =>
   145   	  ("ok", (tacis, _, _)) =>
   146     	  let
   146     	  let
   147     	    val _ = upd_tacis cI tacis
   147     	    val _ = States.upd_tacis cI tacis
   148     	    val (tac, _, _) = last_elem tacis
   148     	    val (tac, _, _) = last_elem tacis
   149     	  in fetchproposedtacticOK2xml cI tac (Error_Pattern.from_store tac) end
   149     	  in fetchproposedtacticOK2xml cI tac (Error_Pattern.from_store tac) end
   150   	| ("helpless", _) => fetchproposedtacticERROR2xml cI "helpless"
   150   	| ("helpless", _) => fetchproposedtacticERROR2xml cI "helpless"
   151   	| ("no-fmz-spec", _) => fetchproposedtacticERROR2xml cI "no-fmz-spec"
   151   	| ("no-fmz-spec", _) => fetchproposedtacticERROR2xml cI "no-fmz-spec"
   152   	| ("end-of-calculation", _) => fetchproposedtacticERROR2xml cI "end-of-calculation"
   152   	| ("end-of-calculation", _) => fetchproposedtacticERROR2xml cI "end-of-calculation"
   155     SOME xml => xml
   155     SOME xml => xml
   156   | NONE => sysERROR2xml cI "error in kernel 3";
   156   | NONE => sysERROR2xml cI "error in kernel 3";
   157 
   157 
   158 fun autoCalculate cI auto = (*Future.fork
   158 fun autoCalculate cI auto = (*Future.fork
   159   (fn () => (( *)let
   159   (fn () => (( *)let
   160      val pold = get_pos cI 1
   160      val pold = States.get_pos cI 1
   161   in
   161   in
   162     case Math_Engine.autocalc [] pold (get_calc cI) auto of
   162     case Math_Engine.autocalc [] pold (States.get_calc cI) auto of
   163       ("ok", c, ptp as (_,p)) =>
   163       ("ok", c, ptp as (_,p)) =>
   164         (upd_calc cI (ptp, []); upd_ipos cI 1 p;
   164         (States.upd_calc cI (ptp, []); States.upd_ipos cI 1 p;
   165          autocalculateOK2xml cI pold (if null c then pold else last_elem c) p)
   165          autocalculateOK2xml cI pold (if null c then pold else last_elem c) p)
   166     | ("dummy", c, ptp as (_,p)) => (*prelim. until \<open>Review modelling- + specification-phase\<close>*)
   166     | ("dummy", c, ptp as (_,p)) => (*prelim. until \<open>Review modelling- + specification-phase\<close>*)
   167         (upd_calc cI (ptp, []); upd_ipos cI 1 p;
   167         (States.upd_calc cI (ptp, []); States.upd_ipos cI 1 p;
   168          autocalculateOK2xml cI pold (if null c then pold else last_elem c) p)
   168          autocalculateOK2xml cI pold (if null c then pold else last_elem c) p)
   169     | ("end-of-calculation", c, ptp as (_,p)) =>
   169     | ("end-of-calculation", c, ptp as (_,p)) =>
   170         (upd_calc cI (ptp, []); upd_ipos cI 1 p;
   170         (States.upd_calc cI (ptp, []); States.upd_ipos cI 1 p;
   171          autocalculateOK2xml cI pold (if null c then pold else last_elem c) p)
   171          autocalculateOK2xml cI pold (if null c then pold else last_elem c) p)
   172     | (str, _, _) => autocalculateERROR2xml cI str
   172     | (str, _, _) => autocalculateERROR2xml cI str
   173   end (* ) *)
   173   end (* ) *)
   174   handle ERROR msg => sysERROR2xml cI ("error in kernel 4: " ^ msg)(* )) *);
   174   handle ERROR msg => sysERROR2xml cI ("error in kernel 4: " ^ msg)(* )) *);
   175 
   175 
   176 fun getTactic cI (p:pos') =
   176 fun getTactic cI (p:pos') =
   177   case \<^try>\<open>
   177   case \<^try>\<open>
   178     let 
   178     let 
   179       val ((pt, _), _) = get_calc cI
   179       val ((pt, _), _) = States.get_calc cI
   180       val (_, tac, _) = ME_Misc.pt_extract (pt, p)
   180       val (_, tac, _) = ME_Misc.pt_extract (pt, p)
   181     in 
   181     in 
   182       case tac of
   182       case tac of
   183    	    SOME ta => gettacticOK2xml cI ta
   183    	    SOME ta => gettacticOK2xml cI ta
   184    	  | NONE => gettacticERROR2xml cI ("no tactic at position " ^ pos'2str p)
   184    	  | NONE => gettacticERROR2xml cI ("no tactic at position " ^ pos'2str p)
   194     @see #TACTICS_CURRENT_METHOD  ..the only impl.WN040307
   194     @see #TACTICS_CURRENT_METHOD  ..the only impl.WN040307
   195   LItool.specific_from_prog waits to be used here
   195   LItool.specific_from_prog waits to be used here
   196 *)
   196 *)
   197 fun fetchApplicableTactics cI (_(*scope*): int) (p : pos') =
   197 fun fetchApplicableTactics cI (_(*scope*): int) (p : pos') =
   198   case \<^try>\<open>
   198   case \<^try>\<open>
   199     let val ((pt, _), _) = get_calc cI
   199     let val ((pt, _), _) = States.get_calc cI
   200     in (applicabletacticsOK cI (Fetch_Tacs.from_prog pt p))
   200     in (applicabletacticsOK cI (Fetch_Tacs.from_prog pt p))
   201        handle PTREE str => sysERROR2xml cI str
   201        handle PTREE str => sysERROR2xml cI str
   202     end
   202     end
   203     \<close> of
   203     \<close> of
   204     SOME xml => xml
   204     SOME xml => xml
   205   | NONE => sysERROR2xml cI "error in kernel 5";
   205   | NONE => sysERROR2xml cI "error in kernel 5";
   206 
   206 
   207 fun getAssumptions cI (p:pos') =
   207 fun getAssumptions cI (p:pos') =
   208   case \<^try>\<open>
   208   case \<^try>\<open>
   209     let 
   209     let 
   210       val ((pt, _), _) = get_calc cI
   210       val ((pt, _), _) = States.get_calc cI
   211   	  val (_, _, asms) = ME_Misc.pt_extract (pt, p)
   211   	  val (_, _, asms) = ME_Misc.pt_extract (pt, p)
   212     in getasmsOK2xml cI asms end
   212     in getasmsOK2xml cI asms end
   213     \<close> of
   213     \<close> of
   214     SOME xml => xml
   214     SOME xml => xml
   215   | NONE => sysERROR2xml cI "syserror in getAssumptions"
   215   | NONE => sysERROR2xml cI "syserror in getAssumptions"
   216 
   216 
   217 (*WN0502 @see ME/ctree: type asms: illdesigned, thus no positions returned*)
   217 (*WN0502 @see ME/ctree: type asms: illdesigned, thus no positions returned*)
   218 fun getAccumulatedAsms cI (p:pos') =
   218 fun getAccumulatedAsms cI (p:pos') =
   219   case \<^try>\<open>
   219   case \<^try>\<open>
   220     let
   220     let
   221       val ((pt, _), _) = get_calc cI
   221       val ((pt, _), _) = States.get_calc cI
   222       val ass = Ctree.get_assumptions pt p
   222       val ass = Ctree.get_assumptions pt p
   223     in getasmsOK2xml cI ass end
   223     in getasmsOK2xml cI ass end
   224     \<close> of
   224     \<close> of
   225     SOME xml => xml
   225     SOME xml => xml
   226   | NONE => sysERROR2xml cI "syserror in getAccumulatedAsms"
   226   | NONE => sysERROR2xml cI "syserror in getAccumulatedAsms"
   227 
   227 
   228 fun refFormula cI (p: pos') = (*WN0501 rename to "fun getElement" !*)
   228 fun refFormula cI (p: pos') = (*WN0501 rename to "fun getElement" !*)
   229   case \<^try>\<open>
   229   case \<^try>\<open>
   230     let 
   230     let 
   231       val ((pt, _), _) = get_calc cI
   231       val ((pt, _), _) = States.get_calc cI
   232   	  val (form, _, _) = ME_Misc.pt_extract (pt, p)
   232   	  val (form, _, _) = ME_Misc.pt_extract (pt, p)
   233     in refformulaOK2xml cI p form end
   233     in refformulaOK2xml cI p form end
   234     \<close> of
   234     \<close> of
   235     SOME xml => xml
   235     SOME xml => xml
   236   | NONE => sysERROR2xml cI ("error in kernel 6: refFormula " ^ int2str cI ^ " " ^ pos'2str p)
   236   | NONE => sysERROR2xml cI ("error in kernel 6: refFormula " ^ int2str cI ^ " " ^ pos'2str p)
   240    "from" "to" are designed for use by iterators of calcChangedEvent,
   240    "from" "to" are designed for use by iterators of calcChangedEvent,
   241    thus "from" is the last unchanged position *)
   241    thus "from" is the last unchanged position *)
   242 fun getFormulaeFromTo cI (([], Pbl) : pos') (to as ([], Pbl) : pos') _ false =
   242 fun getFormulaeFromTo cI (([], Pbl) : pos') (to as ([], Pbl) : pos') _ false =
   243     (case \<^try>\<open>
   243     (case \<^try>\<open>
   244       let 
   244       let 
   245         val ((pt,_),_) = get_calc cI
   245         val ((pt,_),_) = States.get_calc cI
   246         val headline =
   246         val headline =
   247           case ME_Misc.pt_extract (pt, to) of
   247           case ME_Misc.pt_extract (pt, to) of
   248               (ModSpec (_, _, headline, _, _, _), _, _) => headline
   248               (ModSpec (_, _, headline, _, _, _), _, _) => headline
   249           | _ => raise ERROR "getFormulaeFromTo: uncovered case of ME_Misc.pt_extract"
   249           | _ => raise ERROR "getFormulaeFromTo: uncovered case of ME_Misc.pt_extract"
   250       in getintervalOK cI [(to, headline, NONE)] end
   250       in getintervalOK cI [(to, headline, NONE)] end
   261       (case from of
   261       (case from of
   262          ([], Res) => sysERROR2xml cI ("getFormulaeFromTo does: moveDown " ^ 
   262          ([], Res) => sysERROR2xml cI ("getFormulaeFromTo does: moveDown " ^ 
   263            "from=([],Res) .. goes beyond result")
   263            "from=([],Res) .. goes beyond result")
   264        | _ => 
   264        | _ => 
   265          let 
   265          let 
   266            val ((pt,_),_) = get_calc cI
   266            val ((pt,_),_) = States.get_calc cI
   267            val f = move_dn [] pt from
   267            val f = move_dn [] pt from
   268            fun max (a,b) = if a < b then b else a
   268            fun max (a,b) = if a < b then b else a
   269            val lev = max (level, max (lev_of from, lev_of to)) (*... must reach margins *)
   269            val lev = max (level, max (lev_of from, lev_of to)) (*... must reach margins *)
   270          in getintervalOK cI (ME_Misc.get_interval f to lev pt) end)
   270          in getintervalOK cI (ME_Misc.get_interval f to lev pt) end)
   271       \<close> of
   271       \<close> of
   275     sysERROR2xml cI ("getFormulaeFromTo impl.for formulae only, " ^ 
   275     sysERROR2xml cI ("getFormulaeFromTo impl.for formulae only, " ^ 
   276       "i.e. last arg only impl. for false, _NOT_ true");
   276       "i.e. last arg only impl. for false, _NOT_ true");
   277 
   277 
   278 fun interSteps cI ip =
   278 fun interSteps cI ip =
   279   case \<^try>\<open>
   279   case \<^try>\<open>
   280     let val ((pt, p), tacis) = get_calc cI
   280     let val ((pt, p), tacis) = States.get_calc cI
   281     in 
   281     in 
   282       if (not o is_interpos) ip
   282       if (not o is_interpos) ip
   283       then interStepsERROR cI ("only formulae with position (_,Res) " ^ 
   283       then interStepsERROR cI ("only formulae with position (_,Res) " ^ 
   284         "may have intermediate steps above them")
   284         "may have intermediate steps above them")
   285       else 
   285       else 
   286         let val ip' = lev_pred' pt ip
   286         let val ip' = lev_pred' pt ip
   287         in case Detail_Step.go pt ip of
   287         in case Detail_Step.go pt ip of
   288           ("detailrls", pt, lastpos) =>
   288           ("detailrls", pt, lastpos) =>
   289             (upd_calc cI ((pt, p), tacis); interStepsOK cI ip' ip' lastpos)
   289             (States.upd_calc cI ((pt, p), tacis); interStepsOK cI ip' ip' lastpos)
   290         | ("no-Rewrite_Set...", _, _) => sysERROR2xml cI "no Rewrite_Set..."
   290         | ("no-Rewrite_Set...", _, _) => sysERROR2xml cI "no Rewrite_Set..."
   291         | (_, _, lastpos) => interStepsOK cI ip' ip' lastpos
   291         | (_, _, lastpos) => interStepsOK cI ip' ip' lastpos
   292         end
   292         end
   293     end
   293     end
   294     \<close> of
   294     \<close> of
   296   | NONE => sysERROR2xml cI "error in kernel 8";
   296   | NONE => sysERROR2xml cI "error in kernel 8";
   297 
   297 
   298 fun modifyCalcHead cI (ichd as ((p,_),_,_,_,_) : P_Spec.icalhd) =
   298 fun modifyCalcHead cI (ichd as ((p,_),_,_,_,_) : P_Spec.icalhd) =
   299   case \<^try>\<open>
   299   case \<^try>\<open>
   300     let 
   300     let 
   301       val ((pt,_),_) = get_calc cI
   301       val ((pt,_),_) = States.get_calc cI
   302       val (pt, chd as (_,p_,_,_,_,_)) = P_Spec.input_icalhd pt ichd
   302       val (pt, chd as (_,p_,_,_,_,_)) = P_Spec.input_icalhd pt ichd
   303     in (upd_calc cI ((pt, (p,p_)), []); modifycalcheadOK2xml cI chd) end
   303     in (States.upd_calc cI ((pt, (p,p_)), []); modifycalcheadOK2xml cI chd) end
   304     \<close> of
   304     \<close> of
   305     SOME xml => xml
   305     SOME xml => xml
   306   | NONE => sysERROR2xml cI "error in kernel 9";
   306   | NONE => sysERROR2xml cI "error in kernel 9";
   307 
   307 
   308 (* at the activeFormula set the Model, the Guard and the Specification to empty 
   308 (* at the activeFormula set the Model, the Guard and the Specification to empty 
   309    and return a CalcHead; the 'origin' remains (for reconstructing all that) *)
   309    and return a CalcHead; the 'origin' remains (for reconstructing all that) *)
   310 fun resetCalcHead cI =
   310 fun resetCalcHead cI =
   311   case \<^try>\<open>
   311   case \<^try>\<open>
   312     let 
   312     let 
   313       val (ptp,_) = get_calc cI
   313       val (ptp,_) = States.get_calc cI
   314       val ptp = SpecificationC.reset ptp
   314       val ptp = SpecificationC.reset ptp
   315     in (upd_calc cI (ptp, []); modifycalcheadOK2xml cI (SpecificationC.get ptp)) end
   315     in (States.upd_calc cI (ptp, []); modifycalcheadOK2xml cI (SpecificationC.get ptp)) end
   316     \<close> of
   316     \<close> of
   317     SOME xml => xml
   317     SOME xml => xml
   318   | NONE => sysERROR2xml cI "error in kernel 10";
   318   | NONE => sysERROR2xml cI "error in kernel 10";
   319 
   319 
   320 (* at the activeFormula insert all the Descriptions in the Model and return a CalcHead;
   320 (* at the activeFormula insert all the Descriptions in the Model and return a CalcHead;
   321    the Descriptions are for user-guidance; the rest of the items are left empty for user-input *)
   321    the Descriptions are for user-guidance; the rest of the items are left empty for user-input *)
   322 fun modelProblem cI =
   322 fun modelProblem cI =
   323   case \<^try>\<open>
   323   case \<^try>\<open>
   324     let val (ptp, _) = get_calc cI
   324     let val (ptp, _) = States.get_calc cI
   325     	val ptp = SpecificationC.reset ptp
   325     	val ptp = SpecificationC.reset ptp
   326     	val (_, (_, _, ptp)) = Step_Specify.by_tactic_input Tactic.Model_Problem ptp
   326     	val (_, (_, _, ptp)) = Step_Specify.by_tactic_input Tactic.Model_Problem ptp
   327     in (upd_calc cI (ptp, []); modifycalcheadOK2xml cI (SpecificationC.get ptp)) end
   327     in (States.upd_calc cI (ptp, []); modifycalcheadOK2xml cI (SpecificationC.get ptp)) end
   328     \<close> of
   328     \<close> of
   329     SOME xml => xml
   329     SOME xml => xml
   330   | NONE => sysERROR2xml cI "error in kernel 11";
   330   | NONE => sysERROR2xml cI "error in kernel 11";
   331 
   331 
   332 
   332 
   333 (* specify the MethodC at the activeFormula and return a CalcHead containing the Guard.
   333 (* specify the MethodC at the activeFormula and return a CalcHead containing the Guard.
   334    WN0512 impl.incomplete, see "nxt_specif (Specify_Method" *)
   334    WN0512 impl.incomplete, see "nxt_specif (Specify_Method" *)
   335 fun setMethod cI mI =
   335 fun setMethod cI mI =
   336   case \<^try>\<open>
   336   case \<^try>\<open>
   337     let 
   337     let 
   338       val ((pt, _), _) = get_calc cI
   338       val ((pt, _), _) = States.get_calc cI
   339       val ip as (_, p_) = get_pos cI 1
   339       val ip as (_, p_) = States.get_pos cI 1
   340     in 
   340     in 
   341       if member op = [Pbl,Met] p_
   341       if member op = [Pbl,Met] p_
   342       then 
   342       then 
   343         let val (pt, chd) = Math_Engine.set_method mI (pt, ip)
   343         let val (pt, chd) = Math_Engine.set_method mI (pt, ip)
   344         in (upd_calc cI ((pt, ip), []); modifycalcheadOK2xml cI chd) end
   344         in (States.upd_calc cI ((pt, ip), []); modifycalcheadOK2xml cI chd) end
   345       else sysERROR2xml cI "setMethod requires ActiveFormula on CalcHead"
   345       else sysERROR2xml cI "setMethod requires ActiveFormula on CalcHead"
   346     end
   346     end
   347     \<close> of
   347     \<close> of
   348     SOME xml => xml
   348     SOME xml => xml
   349   | NONE => sysERROR2xml cI "error in kernel 12";
   349   | NONE => sysERROR2xml cI "error in kernel 12";
   351 (* specify the Problem at the activeFormula and return a CalcHead containing the Model; 
   351 (* specify the Problem at the activeFormula and return a CalcHead containing the Model; 
   352    special case of checkContext; WN0512 impl.incomplete, see "nxt_specif (Specify_Problem" *)
   352    special case of checkContext; WN0512 impl.incomplete, see "nxt_specif (Specify_Problem" *)
   353 fun setProblem cI pI =
   353 fun setProblem cI pI =
   354   case \<^try>\<open>
   354   case \<^try>\<open>
   355     let 
   355     let 
   356       val ((pt, _), _) = get_calc cI
   356       val ((pt, _), _) = States.get_calc cI
   357       val ip as (_, p_) = get_pos cI 1
   357       val ip as (_, p_) = States.get_pos cI 1
   358     in 
   358     in 
   359       if member op = [Pbl,Met] p_
   359       if member op = [Pbl,Met] p_
   360       then 
   360       then 
   361         let val (pt, chd) = Math_Engine.set_problem pI (pt, ip)
   361         let val (pt, chd) = Math_Engine.set_problem pI (pt, ip)
   362   	    in (upd_calc cI ((pt, ip), []); modifycalcheadOK2xml cI chd) end
   362   	    in (States.upd_calc cI ((pt, ip), []); modifycalcheadOK2xml cI chd) end
   363       else sysERROR2xml cI "setProblem requires ActiveFormula on CalcHead"
   363       else sysERROR2xml cI "setProblem requires ActiveFormula on CalcHead"
   364     end
   364     end
   365     \<close> of
   365     \<close> of
   366     SOME xml => xml
   366     SOME xml => xml
   367   | NONE => sysERROR2xml cI "error in kernel 13";
   367   | NONE => sysERROR2xml cI "error in kernel 13";
   369 (* specify the Theory at the activeFormula and return a CalcHead;
   369 (* specify the Theory at the activeFormula and return a CalcHead;
   370    special case of checkContext; WN0512 impl.incomplete, see "nxt_specif (Specify_Method" *)
   370    special case of checkContext; WN0512 impl.incomplete, see "nxt_specif (Specify_Method" *)
   371 fun setTheory cI tI =
   371 fun setTheory cI tI =
   372   case \<^try>\<open>
   372   case \<^try>\<open>
   373     let 
   373     let 
   374       val ((pt, _), _) = get_calc cI
   374       val ((pt, _), _) = States.get_calc cI
   375       val ip as (_, p_) = get_pos cI 1
   375       val ip as (_, p_) = States.get_pos cI 1
   376     in 
   376     in 
   377       if member op = [Pbl,Met] p_
   377       if member op = [Pbl,Met] p_
   378       then 
   378       then 
   379         let val (pt, chd) = Math_Engine.set_theory tI (pt, ip)
   379         let val (pt, chd) = Math_Engine.set_theory tI (pt, ip)
   380         in (upd_calc cI ((pt, ip), []); modifycalcheadOK2xml cI chd) end
   380         in (States.upd_calc cI ((pt, ip), []); modifycalcheadOK2xml cI chd) end
   381       else sysERROR2xml cI "setProblem requires ActiveFormula on CalcHead"
   381       else sysERROR2xml cI "setProblem requires ActiveFormula on CalcHead"
   382     end
   382     end
   383     \<close> of
   383     \<close> of
   384     SOME xml => xml
   384     SOME xml => xml
   385   | NONE => sysERROR2xml cI "error in kernel 14";
   385   | NONE => sysERROR2xml cI "error in kernel 14";
   388   Not used in isac-java (see comment WN0825 for setContext) but tryrefine is used within isabisac *)
   388   Not used in isac-java (see comment WN0825 for setContext) but tryrefine is used within isabisac *)
   389 fun refineProblem cI (p, p_) guh =
   389 fun refineProblem cI (p, p_) guh =
   390   case \<^try>\<open>
   390   case \<^try>\<open>
   391     let 
   391     let 
   392       val pblID = Ptool.guh2kestoreID guh
   392       val pblID = Ptool.guh2kestoreID guh
   393   	  val ((pt,_),_) = get_calc cI
   393   	  val ((pt,_),_) = States.get_calc cI
   394   	  val pp = par_pblobj pt p
   394   	  val pp = par_pblobj pt p
   395   	  val chd = Math_Engine.tryrefine pblID pt (pp, p_)
   395   	  val chd = Math_Engine.tryrefine pblID pt (pp, p_)
   396     in contextpblOK2xml cI chd end
   396     in contextpblOK2xml cI chd end
   397     \<close> of
   397     \<close> of
   398     SOME xml => xml
   398     SOME xml => xml
   399   | NONE => sysERROR2xml cI "error in kernel 16";
   399   | NONE => sysERROR2xml cI "error in kernel 16";
   400 
   400 
   401 (* append a formula to the calculation *)
   401 (* append a formula to the calculation *)
   402 fun appendFormula' cI (ifo: TermC.as_string) =
   402 fun appendFormula' cI (ifo: TermC.as_string) =
   403   (let
   403   (let
   404     val cs = get_calc cI
   404     val cs = States.get_calc cI
   405     val pos = get_pos cI 1
   405     val pos = States.get_pos cI 1
   406   in
   406   in
   407     case Step.do_next pos cs of
   407     case Step.do_next pos cs of
   408 	    ("ok", (_, _, ptp)) =>
   408 	    ("ok", (_, _, ptp)) =>
   409 	      (case Step_Solve.by_term ptp (encode ifo) of
   409 	      (case Step_Solve.by_term ptp (encode ifo) of
   410 	        ("ok", (_(*use in DG !!!*), c, ptp as (_, p))) =>
   410 	        ("ok", (_(*use in DG !!!*), c, ptp as (_, p))) =>
   411 	          (upd_calc cI (ptp, []); upd_ipos cI 1 p;
   411 	          (States.upd_calc cI (ptp, []); States.upd_ipos cI 1 p;
   412 	          appendformulaOK2xml cI pos (if null c then pos else last_elem c) p)
   412 	          appendformulaOK2xml cI pos (if null c then pos else last_elem c) p)
   413 	      | ("same-formula", (_, c, ptp as (_, p))) =>
   413 	      | ("same-formula", (_, c, ptp as (_, p))) =>
   414 	          (upd_calc cI (ptp, []); upd_ipos cI 1 p;
   414 	          (States.upd_calc cI (ptp, []); States.upd_ipos cI 1 p;
   415 	          appendformulaOK2xml cI pos (if null c then pos else last_elem c) p)
   415 	          appendformulaOK2xml cI pos (if null c then pos else last_elem c) p)
   416 	      | (msg, _) => appendformulaERROR2xml cI msg)
   416 	      | (msg, _) => appendformulaERROR2xml cI msg)
   417 	  | (msg, _) => appendformulaERROR2xml cI msg
   417 	  | (msg, _) => appendformulaERROR2xml cI msg
   418   end)
   418   end)
   419   handle ERROR msg => sysERROR2xml cI ("error in kernel 17: " ^ msg);
   419   handle ERROR msg => sysERROR2xml cI ("error in kernel 17: " ^ msg);
   422 
   422 
   423 (* replace a formula with_in_ a calculation; this applies for initial CAS-commands, too *)
   423 (* replace a formula with_in_ a calculation; this applies for initial CAS-commands, too *)
   424 fun replaceFormula cI ifo =
   424 fun replaceFormula cI ifo =
   425   case \<^try>\<open>
   425   case \<^try>\<open>
   426     let
   426     let
   427       val ((pt, _), _) = get_calc cI
   427       val ((pt, _), _) = States.get_calc cI
   428       val p = get_pos cI 1
   428       val p = States.get_pos cI 1
   429     in
   429     in
   430       case Step_Solve.by_term (pt, p) (encode ifo) of
   430       case Step_Solve.by_term (pt, p) (encode ifo) of
   431   	    ("ok", (_(*tacs used for DG ?*), c, ptp' as (_, p'))) =>
   431   	    ("ok", (_(*tacs used for DG ?*), c, ptp' as (_, p'))) =>
   432   	      let
   432   	      let
   433   	        val unc = if null (fst p) then p else move_up [] pt p
   433   	        val unc = if null (fst p) then p else move_up [] pt p
   434   	        val _ = upd_calc cI (ptp', [])
   434   	        val _ = States.upd_calc cI (ptp', [])
   435   	        val _ = upd_ipos cI 1 p'
   435   	        val _ = States.upd_ipos cI 1 p'
   436   	      in replaceformulaOK2xml cI unc (if null c then unc else last_elem c) p' end
   436   	      in replaceformulaOK2xml cI unc (if null c then unc else last_elem c) p' end
   437   	  | ("same-formula", _) => replaceformulaERROR2xml cI "formula not changed"
   437   	  | ("same-formula", _) => replaceformulaERROR2xml cI "formula not changed"
   438   	  | (msg, _) => replaceformulaERROR2xml cI msg
   438   	  | (msg, _) => replaceformulaERROR2xml cI msg
   439     end
   439     end
   440     \<close> of
   440     \<close> of
   447     move*:       compute the new iterator from the old one on the fly
   447     move*:       compute the new iterator from the old one on the fly
   448 ***)
   448 ***)
   449 
   449 
   450 fun moveActiveRoot cI =
   450 fun moveActiveRoot cI =
   451   case \<^try>\<open>
   451   case \<^try>\<open>
   452     let val _ = upd_ipos cI 1 ([], Pbl)
   452     let val _ = States.upd_ipos cI 1 ([], Pbl)
   453     in iteratorOK2xml cI ([], Pbl) end
   453     in iteratorOK2xml cI ([], Pbl) end
   454     \<close> of
   454     \<close> of
   455     SOME xml => xml
   455     SOME xml => xml
   456   | NONE => sysERROR2xml cI "error in kernel 19"
   456   | NONE => sysERROR2xml cI "error in kernel 19"
   457 fun moveRoot cI =
   457 fun moveRoot cI =
   460     \<close> of
   460     \<close> of
   461     SOME xml => xml
   461     SOME xml => xml
   462   | NONE => sysERROR2xml cI "";
   462   | NONE => sysERROR2xml cI "";
   463 fun moveActiveRootTEST cI =
   463 fun moveActiveRootTEST cI =
   464   case \<^try>\<open>
   464   case \<^try>\<open>
   465     let val _ = upd_ipos cI 1 ([], Pbl)
   465     let val _ = States.upd_ipos cI 1 ([], Pbl)
   466     in iteratorOK2xml cI ([], Pbl) end
   466     in iteratorOK2xml cI ([], Pbl) end
   467     \<close> of
   467     \<close> of
   468     SOME xml => xml
   468     SOME xml => xml
   469   | NONE => sysERROR2xml cI "error in kernel 20"
   469   | NONE => sysERROR2xml cI "error in kernel 20"
   470 
   470 
   471 fun moveActiveDown cI =
   471 fun moveActiveDown cI =
   472   case \<^try>\<open>
   472   case \<^try>\<open>
   473     (let 
   473     (let 
   474       val ((pt, _), _) = get_calc cI
   474       val ((pt, _), _) = States.get_calc cI
   475   	  val ip' = move_dn [] pt (get_pos cI 1)
   475   	  val ip' = move_dn [] pt (States.get_pos cI 1)
   476   	  val _ = upd_ipos cI 1 ip'
   476   	  val _ = States.upd_ipos cI 1 ip'
   477     in iteratorOK2xml cI ip' end)
   477     in iteratorOK2xml cI ip' end)
   478       handle (PTREE _) => iteratorERROR2xml cI
   478       handle (PTREE _) => iteratorERROR2xml cI
   479     \<close> of
   479     \<close> of
   480     SOME xml => xml
   480     SOME xml => xml
   481   | NONE => sysERROR2xml cI "error in kernel 21"
   481   | NONE => sysERROR2xml cI "error in kernel 21"
   482 fun moveDown cI p =
   482 fun moveDown cI p =
   483   case \<^try>\<open>
   483   case \<^try>\<open>
   484     (let 
   484     (let 
   485       val ((pt, _), _) = get_calc cI
   485       val ((pt, _), _) = States.get_calc cI
   486   	  val ip' = move_dn [] pt p
   486   	  val ip' = move_dn [] pt p
   487     in iteratorOK2xml cI ip' end)
   487     in iteratorOK2xml cI ip' end)
   488       handle (PTREE _) => iteratorERROR2xml cI
   488       handle (PTREE _) => iteratorERROR2xml cI
   489     \<close> of
   489     \<close> of
   490     SOME xml => xml
   490     SOME xml => xml
   491   | NONE => sysERROR2xml cI "error in kernel 22"
   491   | NONE => sysERROR2xml cI "error in kernel 22"
   492 
   492 
   493 fun moveActiveLevelDown cI =
   493 fun moveActiveLevelDown cI =
   494   case \<^try>\<open>
   494   case \<^try>\<open>
   495     (let 
   495     (let 
   496       val ((pt, _) ,_) = get_calc cI
   496       val ((pt, _) ,_) = States.get_calc cI
   497     	val ip' = movelevel_dn [] pt (get_pos cI 1)
   497     	val ip' = movelevel_dn [] pt (States.get_pos cI 1)
   498     	val _ = upd_ipos cI 1 ip'
   498     	val _ = States.upd_ipos cI 1 ip'
   499     in iteratorOK2xml cI ip' end)
   499     in iteratorOK2xml cI ip' end)
   500       handle (PTREE _) => iteratorERROR2xml cI
   500       handle (PTREE _) => iteratorERROR2xml cI
   501     \<close> of
   501     \<close> of
   502     SOME xml => xml
   502     SOME xml => xml
   503   | NONE => sysERROR2xml cI "error in kernel 23"
   503   | NONE => sysERROR2xml cI "error in kernel 23"
   504 fun moveLevelDown cI p =
   504 fun moveLevelDown cI p =
   505   case \<^try>\<open>
   505   case \<^try>\<open>
   506     (let 
   506     (let 
   507       val ((pt, _), _) = get_calc cI
   507       val ((pt, _), _) = States.get_calc cI
   508       val ip' = movelevel_dn [] pt p
   508       val ip' = movelevel_dn [] pt p
   509     in iteratorOK2xml cI ip' end)
   509     in iteratorOK2xml cI ip' end)
   510       handle (PTREE _) => iteratorERROR2xml cI
   510       handle (PTREE _) => iteratorERROR2xml cI
   511     \<close> of
   511     \<close> of
   512     SOME xml => xml
   512     SOME xml => xml
   513   | NONE => sysERROR2xml cI "error in kernel 24"
   513   | NONE => sysERROR2xml cI "error in kernel 24"
   514 
   514 
   515 fun moveActiveUp cI =
   515 fun moveActiveUp cI =
   516   case \<^try>\<open>
   516   case \<^try>\<open>
   517     (let 
   517     (let 
   518       val ((pt, _), _) = get_calc cI
   518       val ((pt, _), _) = States.get_calc cI
   519   	  val ip' = move_up [] pt (get_pos cI 1)
   519   	  val ip' = move_up [] pt (States.get_pos cI 1)
   520   	  val _ = upd_ipos cI 1 ip'
   520   	  val _ = States.upd_ipos cI 1 ip'
   521     in iteratorOK2xml cI ip' end)
   521     in iteratorOK2xml cI ip' end)
   522       handle PTREE _ => iteratorERROR2xml cI
   522       handle PTREE _ => iteratorERROR2xml cI
   523     \<close> of
   523     \<close> of
   524     SOME xml => xml
   524     SOME xml => xml
   525   | NONE => sysERROR2xml cI "error in kernel 25"
   525   | NONE => sysERROR2xml cI "error in kernel 25"
   526 fun moveUp cI p =
   526 fun moveUp cI p =
   527   case \<^try>\<open>
   527   case \<^try>\<open>
   528     (let 
   528     (let 
   529       val ((pt, _), _) = get_calc cI
   529       val ((pt, _), _) = States.get_calc cI
   530   	  val ip' = move_up [] pt p
   530   	  val ip' = move_up [] pt p
   531     in iteratorOK2xml cI ip' end)
   531     in iteratorOK2xml cI ip' end)
   532       handle PTREE _ => iteratorERROR2xml cI
   532       handle PTREE _ => iteratorERROR2xml cI
   533     \<close> of
   533     \<close> of
   534     SOME xml => xml
   534     SOME xml => xml
   535   | NONE => sysERROR2xml cI "error in kernel 26"
   535   | NONE => sysERROR2xml cI "error in kernel 26"
   536 
   536 
   537 fun moveActiveLevelUp cI =
   537 fun moveActiveLevelUp cI =
   538   case \<^try>\<open>
   538   case \<^try>\<open>
   539     (let 
   539     (let 
   540       val ((pt, _), _) = get_calc cI
   540       val ((pt, _), _) = States.get_calc cI
   541       val ip' = movelevel_up [] pt (get_pos cI 1)
   541       val ip' = movelevel_up [] pt (States.get_pos cI 1)
   542       val _ = upd_ipos cI 1 ip'
   542       val _ = States.upd_ipos cI 1 ip'
   543     in iteratorOK2xml cI ip' end)
   543     in iteratorOK2xml cI ip' end)
   544       handle PTREE _ => iteratorERROR2xml cI
   544       handle PTREE _ => iteratorERROR2xml cI
   545     \<close> of
   545     \<close> of
   546     SOME xml => xml
   546     SOME xml => xml
   547   | NONE => sysERROR2xml cI "error in kernel 27"
   547   | NONE => sysERROR2xml cI "error in kernel 27"
   548 fun moveLevelUp cI p =
   548 fun moveLevelUp cI p =
   549   case \<^try>\<open>
   549   case \<^try>\<open>
   550     (let 
   550     (let 
   551       val ((pt, _), _) = get_calc cI
   551       val ((pt, _), _) = States.get_calc cI
   552   	  val ip' = movelevel_up [] pt p
   552   	  val ip' = movelevel_up [] pt p
   553     in iteratorOK2xml cI ip' end)
   553     in iteratorOK2xml cI ip' end)
   554       handle PTREE _ => iteratorERROR2xml cI
   554       handle PTREE _ => iteratorERROR2xml cI
   555     \<close> of
   555     \<close> of
   556     SOME xml => xml
   556     SOME xml => xml
   557   | NONE => sysERROR2xml cI "error in kernel 28"
   557   | NONE => sysERROR2xml cI "error in kernel 28"
   558 
   558 
   559 fun moveActiveCalcHead cI =
   559 fun moveActiveCalcHead cI =
   560   case \<^try>\<open>
   560   case \<^try>\<open>
   561     (let 
   561     (let 
   562     val ((pt, _), _) = get_calc cI
   562     val ((pt, _), _) = States.get_calc cI
   563 	  val ip' = movecalchd_up pt (get_pos cI 1)
   563 	  val ip' = movecalchd_up pt (States.get_pos cI 1)
   564 	  val _ = upd_ipos cI 1 ip'
   564 	  val _ = States.upd_ipos cI 1 ip'
   565   in iteratorOK2xml cI ip' end)
   565   in iteratorOK2xml cI ip' end)
   566     handle PTREE _ => iteratorERROR2xml cI
   566     handle PTREE _ => iteratorERROR2xml cI
   567     \<close> of
   567     \<close> of
   568     SOME xml => xml
   568     SOME xml => xml
   569   | NONE => sysERROR2xml cI "error in kernel 29"
   569   | NONE => sysERROR2xml cI "error in kernel 29"
   570 fun moveCalcHead cI p =
   570 fun moveCalcHead cI p =
   571   case \<^try>\<open>
   571   case \<^try>\<open>
   572     (let 
   572     (let 
   573       val ((pt, _), _) = get_calc cI
   573       val ((pt, _), _) = States.get_calc cI
   574   	  val ip' = movecalchd_up pt p
   574   	  val ip' = movecalchd_up pt p
   575     in iteratorOK2xml cI ip' end)
   575     in iteratorOK2xml cI ip' end)
   576       handle PTREE _ => iteratorERROR2xml cI
   576       handle PTREE _ => iteratorERROR2xml cI
   577     \<close> of
   577     \<close> of
   578     SOME xml => xml
   578     SOME xml => xml
   582   for an Error_Pattern.id find "(fill_in_id * fill_in) list"
   582   for an Error_Pattern.id find "(fill_in_id * fill_in) list"
   583    which is applicable to the current formula.
   583    which is applicable to the current formula.
   584 *)
   584 *)
   585 fun findFillpatterns cI errpatID =
   585 fun findFillpatterns cI errpatID =
   586   let
   586   let
   587     val ((pt, _), _) = get_calc cI
   587     val ((pt, _), _) = States.get_calc cI
   588 		val pos = get_pos cI 1;
   588 		val pos = States.get_pos cI 1;
   589 		val fillpats = Error_Pattern.find_fill_patterns (pt, pos) errpatID
   589 		val fillpats = Error_Pattern.find_fill_patterns (pt, pos) errpatID
   590   in findFillpatterns2xml cI (map #1 fillpats) end
   590   in findFillpatterns2xml cI (map #1 fillpats) end
   591 
   591 
   592 (* given a fillpatID propose a fillform to the learner on the worksheet;
   592 (* given a fillpatID propose a fillform to the learner on the worksheet;
   593   the "ctree" is extended with fillpat and "ostate Inconsistent",
   593   the "ctree" is extended with fillpat and "ostate Inconsistent",
   595   returns CalcChanged.
   595   returns CalcChanged.
   596   arg errpatID: required because there is no dialog-related state in the math-kernel.
   596   arg errpatID: required because there is no dialog-related state in the math-kernel.
   597 *)
   597 *)
   598 fun requestFillformula cI (errpatID, fillpatID) =
   598 fun requestFillformula cI (errpatID, fillpatID) =
   599   let
   599   let
   600     val ((pt, _), _) = get_calc cI
   600     val ((pt, _), _) = States.get_calc cI
   601 		val pos = get_pos cI 1
   601 		val pos = States.get_pos cI 1
   602     val fillforms = Error_Pattern.find_fill_patterns (pt, pos) errpatID
   602     val fillforms = Error_Pattern.find_fill_patterns (pt, pos) errpatID
   603   in
   603   in
   604     case filter ((curry op = fillpatID) o
   604     case filter ((curry op = fillpatID) o
   605         (#1: (Error_Pattern_Def.fill_in_id * term * thm * (Subst.input option) -> Error_Pattern_Def.fill_in_id))) fillforms of
   605         (#1: (Error_Pattern_Def.fill_in_id * term * thm * (Subst.input option) -> Error_Pattern_Def.fill_in_id))) fillforms of
   606       (_, fillform, thm, sube_opt) :: _ =>
   606       (_, fillform, thm, sube_opt) :: _ =>
   607         let
   607         let
   608           val (pt, pos') = Step.inconsistent (sube_opt, ThmC.of_thm thm)
   608           val (pt, pos') = Step.inconsistent (sube_opt, ThmC.of_thm thm)
   609             fillform (get_loc pt pos) pos pt
   609             fillform (get_loc pt pos) pos pt
   610         in
   610         in
   611           (upd_calc cI ((pt, pos'), []); (*upd_ipos cI 1 pos';*)
   611           (States.upd_calc cI ((pt, pos'), []); (*States.upd_ipos cI 1 pos';*)
   612            autocalculateOK2xml cI pos pos' pos')
   612            autocalculateOK2xml cI pos pos' pos')
   613         end
   613         end
   614     | _ => autocalculateERROR2xml cI "no fillpattern found"
   614     | _ => autocalculateERROR2xml cI "no fillpattern found"
   615   end;
   615   end;
   616 
   616 
   618    presented to the learner immediately before by "requestFillformula (errpatID, fillpatID)":
   618    presented to the learner immediately before by "requestFillformula (errpatID, fillpatID)":
   619    errpatID: lhs of the respective thm = lhs of fillformula with fillpatID.
   619    errpatID: lhs of the respective thm = lhs of fillformula with fillpatID.
   620    The respective thm is stored in the ctree. *)
   620    The respective thm is stored in the ctree. *)
   621 fun inputFillFormula cI ifo =
   621 fun inputFillFormula cI ifo =
   622   let
   622   let
   623     val ((pt, _), _) = get_calc cI
   623     val ((pt, _), _) = States.get_calc cI
   624     val pos = get_pos cI 1;
   624     val pos = States.get_pos cI 1;
   625   in
   625   in
   626     case Error_Pattern.filled_exactly (pt, pos) ifo of
   626     case Error_Pattern.filled_exactly (pt, pos) ifo of
   627       ("ok", tac) =>
   627       ("ok", tac) =>
   628         let
   628         let
   629         in (* cp from applyTactic *)
   629         in (* cp from applyTactic *)
   630           case Step.by_tactic tac (pt, pos) of
   630           case Step.by_tactic tac (pt, pos) of
   631             ("ok", (_, c, ptp as (_,p'))) =>
   631             ("ok", (_, c, ptp as (_,p'))) =>
   632               (upd_calc cI (ptp, []);
   632               (States.upd_calc cI (ptp, []);
   633                upd_ipos cI 1 p';
   633                States.upd_ipos cI 1 p';
   634                autocalculateOK2xml cI pos (if null c then p' else last_elem c) p')
   634                autocalculateOK2xml cI pos (if null c then p' else last_elem c) p')
   635           | ("unsafe-ok", (_, c, ptp as (_,p'))) =>
   635           | ("unsafe-ok", (_, c, ptp as (_,p'))) =>
   636               (upd_calc cI (ptp, []);
   636               (States.upd_calc cI (ptp, []);
   637                upd_ipos cI 1 p';
   637                States.upd_ipos cI 1 p';
   638                autocalculateOK2xml cI pos (if null c then p' else last_elem c) p')
   638                autocalculateOK2xml cI pos (if null c then p' else last_elem c) p')
   639           | ("end-of-calculation", (_, c, ptp as (_,p'))) =>
   639           | ("end-of-calculation", (_, c, ptp as (_,p'))) =>
   640               (upd_calc cI (ptp, []);
   640               (States.upd_calc cI (ptp, []);
   641                upd_ipos cI 1 p';
   641                States.upd_ipos cI 1 p';
   642                autocalculateOK2xml cI pos (if null c then p' else last_elem c) p')
   642                autocalculateOK2xml cI pos (if null c then p' else last_elem c) p')
   643           | _ => autocalculateERROR2xml cI "failure"
   643           | _ => autocalculateERROR2xml cI "failure"
   644         end
   644         end
   645     | (msg, _) => message2xml cI msg
   645     | (msg, _) => message2xml cI msg
   646   end
   646   end