src/Tools/isac/MathEngine/step.sml
author Walther Neuper <walther.neuper@jku.at>
Tue, 11 Feb 2020 17:25:45 +0100
changeset 59805 3bd8f1094f60
parent 59804 403f00b309ef
child 59806 1e69c59424e5
permissions -rw-r--r--
introduce Step.by_tactic, part 2
     1 (* Title:  MathEngine/step.sml
     2    Author: Walther Neuper 2019
     3    (c) due to copyright terms
     4 *)
     5 
     6 (* survey on results of by_tactic, find_next, by_term:
     7    * Step_Specify
     8      * by_tactic "ok", "ok", 
     9    * Step_Solve              
    10      * locate_input_tactic  : "ok", "unsafe-ok", "not-applicable", "end-of-calculation"
    11      * locate_input_term : "ok", "syntax error", "same-formula", "no derivation found",
    12         "error pattern #" ^ errpatID (the latter doesn't fit into the pattern of fixed string)
    13      * find_next_formula    : "ok", "helpless", "no-fmz-spec"(=helpless), "end-of-calculation"
    14  *)
    15 signature STEP =
    16 sig
    17   val do_next: Ctree.pos' -> Chead.calcstate -> string * Chead.calcstate'
    18   val by_tactic: Tactic.input -> Calc.T -> string * Chead.calcstate'
    19 (*
    20   val by_tactic = Step_Solve.by_tactic
    21                 = Step_Specify.by_tactic ? depending on PIDE, see TODO.thy
    22   val by_term   = Step_Solve.by_term: Calc.T -> term -> string * Chead.calcstate'
    23 TODO --------------------------------------"----------> ??????   Calc.T 
    24                 = Step_Specify.by_tactic ? depending on PIDE, see TODO.thy
    25 *)
    26 (* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
    27   (*NONE*)                                                     
    28 (*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
    29   val specify_do_next: Calc.T -> string * Chead.calcstate'
    30   val switch_specify_solve: Pos.pos_ -> Calc.T -> string * Chead.calcstate'
    31 
    32   datatype lOc_ = ERror of string | UNsafe of Chead.calcstate' | Updated of Chead.calcstate'
    33   val loc_solve_ : Tactic.T -> Calc.T -> lOc_
    34   val loc_specify_ : Tactic.T -> Calc.T -> lOc_
    35 ( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
    36 end
    37 
    38 (**)
    39 structure Step(**): STEP(**) =
    40 struct
    41 (**)
    42 
    43 (** do a next step **)
    44 
    45 fun specify_do_next (ptp as (pt, (p, p_))) =
    46   let
    47     val (_, (p_', tac)) = SpecifyNEW.find_next_step ptp
    48     val ist_ctxt =  Ctree.get_loc pt (p, p_)
    49   in
    50     case tac of
    51   	  Tactic.Apply_Method mI =>
    52   	    (*vvvvvvvvvv-------------------------------------------------------------- solve-phase *)
    53   	    LI.by_tactic (Tactic.Apply_Method' (mI, NONE, Istate.e_istate, ContextC.e_ctxt))
    54   	      ist_ctxt (pt, (p, p_'))
    55     | _ => ("dummy", Step_Specify.by_tactic tac (pt, (p, p_')))
    56   end
    57 
    58 fun switch_specify_solve state_pos (pt, input_pos) =
    59   let
    60     val result =
    61       if member op = [Pos.Pbl, Pos.Met] state_pos
    62   	  then specify_do_next (pt, input_pos)
    63       else LI.do_next (pt, input_pos)
    64   in
    65     case result of
    66       (_, cs as ([], _, _)) => ("helpless", cs)
    67     | scs => scs
    68   end
    69 
    70 (* does a step forward; returns tactic used, ctree updated (i.e. with NEW term/calchead) *)
    71 fun do_next (ip as (_, p_)) (ptp as (pt, p), tacis) =
    72   let val pIopt = Ctree.get_pblID (pt, ip);
    73   in
    74     if ip = ([], Pos.Res)
    75     then ("end-of-calculation", (tacis, [], ptp): Chead.calcstate') 
    76     else
    77       case tacis of
    78         (_ :: _) => 
    79           if ip = p (*the request is done where ptp waits for*)
    80           then 
    81             let val (pt', c', p') = Generate.generate tacis (pt, [], p)
    82   	        in ("ok", (tacis, c', (pt', p'))) end
    83           else
    84             switch_specify_solve p_ (pt, ip)
    85       | _ => case pIopt of
    86   	            NONE => ("no-fmz-spec", ([], [], ptp))
    87   	          | SOME _ => switch_specify_solve p_ (pt, ip)
    88   end
    89 
    90 
    91 (** locate an input tactic **)
    92 
    93 datatype lOc_ = ERror of string | UNsafe of Chead.calcstate' | Updated of Chead.calcstate'
    94 (*  loc_specify_ : Tactic.T -> Calc.T -> lOc_*)
    95 fun loc_specify_ m (pt, pos) =
    96   let
    97     val (p, _, f, _, _, pt) = Chead.specify m pos [] pt;
    98   in
    99     case f of (Generate.Error' e) => ERror e | _ => Updated ([], [], (pt,p))
   100   end
   101 (*  loc_solve_ : Tactic.T -> Calc.T -> lOc_*)
   102 fun loc_solve_ m (pt, pos) =
   103   let
   104     val (msg, cs') = Step_Solve.by_tactic m (pt, pos); (*..sig.OK*)
   105   in
   106     case msg of "ok" => Updated cs' | msg => ERror msg 
   107   end
   108 
   109 (* locate a tactic in a program and apply it if possible;
   110    report of tacs' applicability in tacis; pt is dropped in setNextTactic*)
   111 fun by_tactic _ (ptp as (_, ([], Pos.Res))) = ("end-of-calculation", ([], [], ptp))
   112   | by_tactic m (ptp as (pt, p)) =
   113     case Applicable.applicable_in p pt m of
   114 	    Applicable.Notappl _ => ("not-applicable", ([],[],  ptp): Chead.calcstate')
   115 	  | Applicable.Appl m =>
   116 	    let 
   117         val x = if Tactic.for_specify' m
   118 		      then loc_specify_ m ptp else loc_solve_ m ptp
   119 	    in 
   120 	      case x of 
   121 		      ERror _ => ("failure", ([], [], ptp))
   122 		      (*FIXXXXXME: loc_specify_, loc_solve_ TOGETHER with dropping meOLD+detail.sml*)
   123 		    | UNsafe cs' => ("unsafe-ok", cs')
   124 		    | Updated (cs' as (_, _, (_, p'))) => (*ev.SEVER.tacs like Begin_Trans*)
   125 		      (if p' = ([], Pos.Res) then "end-of-calculation" else "ok", cs')
   126           (*for SEVER.tacs user to ask ? *)
   127 	    end
   128 
   129 (**)end(**)