src/Tools/isac/MathEngine/step.sml
author wneuper <Walther.Neuper@jku.at>
Wed, 19 Oct 2022 10:43:04 +0200
changeset 60567 bb3140a02f3d
parent 60556 486223010ea8
child 60578 baf06b1b2aaa
permissions -rw-r--r--
eliminate term2str in src, Prog_Tac.*_adapt_to_type
walther@59747
     1
(* Title:  MathEngine/step.sml
walther@59747
     2
   Author: Walther Neuper 2019
walther@59747
     3
   (c) due to copyright terms
walther@59920
     4
walther@59920
     5
Unify function of structure Step_Specify and structure Step_Solve
walther@59920
     6
as well as of structure Specify_Step and structure Solve_Step.
walther@59747
     7
*)
walther@59747
     8
walther@59747
     9
signature STEP =
walther@59747
    10
sig
walther@59981
    11
  val do_next: Pos.pos' -> Calc.state_pre -> string * Calc.state_post
walther@59981
    12
  val by_tactic: Tactic.input -> Calc.T -> string * Calc.state_post
walther@59981
    13
(*val by_term  = Step_Solve.by_term: Calc.T -> term -> string * Calc.state_post NOT for specify*)
walther@59921
    14
  val check: Tactic.input -> Calc.T -> Applicable.T
walther@59959
    15
  val add: Tactic.T -> Istate_Def.T * Proof.context -> Calc.T -> Test_Out.T
walther@59959
    16
walther@59959
    17
  val inconsistent: Subst.input option * ThmC.T -> term -> Istate_Def.T * Proof.context ->
walther@59959
    18
    Pos.pos' -> Ctree.ctree -> Calc.T
Walther@60567
    19
wenzelm@60223
    20
\<^isac_test>\<open>
walther@59981
    21
  val specify_do_next: Calc.T -> string * Calc.state_post
walther@59981
    22
  val switch_specify_solve: Pos.pos_ -> Calc.T -> string * Calc.state_post
wenzelm@60223
    23
\<close>
walther@59747
    24
end
walther@59747
    25
walther@59747
    26
(**)
walther@59747
    27
structure Step(**): STEP(**) =
walther@59747
    28
struct
walther@59803
    29
(**)
walther@59747
    30
walther@59921
    31
(** check a tactic for applicability **)
walther@59921
    32
walther@59921
    33
fun check tac (ctree, pos) =
walther@59932
    34
  if Tactic.for_specify tac
walther@59922
    35
  then Specify_Step.check tac (ctree, pos)
walther@59932
    36
  else Solve_Step.check tac (ctree, pos);
walther@59932
    37
walther@59932
    38
walther@59932
    39
(** add a step to a calculation **)
walther@59932
    40
walther@59932
    41
val add = Solve_Step.add_general;
walther@59921
    42
walther@59921
    43
walther@59920
    44
(* survey on results of by_tactic, find_next, by_term:
walther@59920
    45
   * Step_Specify
walther@59920
    46
     * by_tactic "ok", "ok", 
walther@59920
    47
   * Step_Solve              
walther@59920
    48
     * locate_input_tactic  : "ok", "unsafe-ok", "not-applicable", "end-of-calculation"
walther@59920
    49
     * locate_input_term : "ok", "syntax error", "same-formula", "no derivation found",
walther@59920
    50
        "error pattern #" ^ errpatID (the latter doesn't fit into the pattern of fixed string)
walther@59920
    51
     * find_next_formula    : "ok", "helpless", "no-fmz-spec"(=helpless), "end-of-calculation"
walther@59920
    52
 *)
walther@59920
    53
walther@59804
    54
(** do a next step **)
walther@59804
    55
walther@59763
    56
fun specify_do_next (ptp as (pt, (p, p_))) =
walther@59763
    57
  let
walther@59976
    58
    val (_, (p_', tac)) = Specify.find_next_step ptp
walther@59763
    59
    val ist_ctxt =  Ctree.get_loc pt (p, p_)
walther@59763
    60
  in
walther@59763
    61
    case tac of
walther@59803
    62
  	  Tactic.Apply_Method mI =>
walther@59803
    63
  	    (*vvvvvvvvvv-------------------------------------------------------------- solve-phase *)
walther@59846
    64
  	    LI.by_tactic (Tactic.Apply_Method' (mI, NONE, Istate.empty, ContextC.empty))
walther@59763
    65
  	      ist_ctxt (pt, (p, p_'))
Walther@60556
    66
    | _ => Step_Specify.by_tactic_input  tac (pt, (p, p_'))
walther@59763
    67
  end
walther@59763
    68
walther@59803
    69
fun switch_specify_solve state_pos (pt, input_pos) =
walther@59803
    70
  let
walther@59803
    71
    val result =
walther@59932
    72
      if Pos.on_specification ([], state_pos)
walther@59803
    73
  	  then specify_do_next (pt, input_pos)
walther@59803
    74
      else LI.do_next (pt, input_pos)
walther@59803
    75
  in
walther@59803
    76
    case result of
walther@60021
    77
    (*unclean alternatives*)
walther@60021
    78
      scs as ("ok", _) => scs
walther@60021
    79
    | (_, cs as ([], _, _)) => ("helpless", cs)
walther@60269
    80
    | _ => raise ERROR "switch_specify_solve: uncovered case in fun.def."
walther@59803
    81
  end
walther@59803
    82
walther@59794
    83
(* does a step forward; returns tactic used, ctree updated (i.e. with NEW term/calchead) *)
walther@59781
    84
fun do_next (ip as (_, p_)) (ptp as (pt, p), tacis) =
walther@60011
    85
  if Pos.on_calc_end ip then
walther@60011
    86
    ("end-of-calculation", (tacis, [], ptp): Calc.state_post) 
walther@60011
    87
  else
walther@60011
    88
    let
walther@60011
    89
      val (_, probl_id, _) = Calc.references (pt, p);
walther@60011
    90
    in
walther@60011
    91
      case tacis of
walther@60011
    92
        (_ :: _) => 
walther@60021
    93
          if ip = p (*the request is done where ptp waits for*) then                 
walther@60021
    94
            let     (*spurious -------------vvvvvvvvvvvvvv*)
walther@60011
    95
              val (pt', c', p') = Solve_Step.s_add_general tacis (pt, [], p)
walther@60011
    96
            in
walther@60011
    97
             ("ok", (tacis, c', (pt', p')))
walther@60011
    98
            end
walther@60011
    99
         else
walther@60011
   100
            switch_specify_solve p_ (pt, ip)
walther@60011
   101
      | _ =>
walther@60011
   102
        if probl_id = Problem.id_empty then
walther@60011
   103
           ("no-fmz-spec", ([], [], ptp))
walther@60011
   104
        else switch_specify_solve p_ (pt, ip)
walther@60011
   105
    end;
walther@60011
   106
walther@59805
   107
walther@59804
   108
(** locate an input tactic **)
walther@59804
   109
walther@59806
   110
(* LEGACY: report of tacs' applicability in tacis; pt is dropped in setNextTactic*)
walther@59804
   111
fun by_tactic _ (ptp as (_, ([], Pos.Res))) = ("end-of-calculation", ([], [], ptp))
walther@59921
   112
  | by_tactic tac (ptp as (pt, p)) =
walther@59921
   113
    case check tac (pt, p) of
walther@59981
   114
	    Applicable.No _ => ("not-applicable", ([],[],  ptp): Calc.state_post)
walther@59921
   115
	  | Applicable.Yes tac' =>
walther@59921
   116
	    if Tactic.for_specify' tac'
walther@59921
   117
	    then Step_Specify.by_tactic tac' ptp
walther@59921
   118
	    else Step_Solve.by_tactic tac' ptp
walther@59921
   119
walther@59804
   120
walther@59959
   121
(** add an inconsisten step **)
walther@59959
   122
walther@59959
   123
fun inconsistent (subs_opt, thm') f' (is, ctxt) (pos as (p,_)) pt =
walther@59959
   124
  let
walther@59959
   125
    val f = Ctree.get_curr_formula (pt, pos)
walther@59959
   126
    val pos' as (p', _) = (Pos.lev_on p, Pos.Res)
walther@59959
   127
    val (pt, _) = case subs_opt of
walther@59959
   128
      NONE => Ctree.cappend_atomic pt p' (is, ctxt) f
walther@59959
   129
        (Tactic.Rewrite thm') (f', []) Ctree.Inconsistent
walther@59959
   130
    | SOME subs => Ctree.cappend_atomic pt p' (is, ctxt) f
walther@59959
   131
        (Tactic.Rewrite_Inst (subs, thm')) (f', []) Ctree.Inconsistent
walther@59959
   132
    val pt = Ctree.update_branch pt p' Ctree.TransitiveB
walther@59959
   133
  in (pt, pos') end
walther@59959
   134
walther@59803
   135
(**)end(**)