src/Tools/isac/Interpret/step-solve.sml
author wneuper <Walther.Neuper@jku.at>
Mon, 02 Oct 2023 12:02:59 +0200
changeset 60756 b1ae5a019fa1
parent 60658 1c089105f581
permissions -rw-r--r--
prepare 10: check completeness of Specification regards variants and Position.T
walther@59823
     1
(* Title:  Interpret/step-solve.sml
walther@59747
     2
   Author: Walther Neuper 2019
walther@59747
     3
   (c) due to copyright terms
walther@59747
     4
*)
walther@59747
     5
walther@59748
     6
signature STEP_SOLVE =
walther@59747
     7
sig
walther@59981
     8
  val do_next: Calc.T -> string * Calc.state_post
walther@59981
     9
  val by_tactic : Tactic.T -> Calc.T -> string * Calc.state_post
walther@59981
    10
  val by_term : Calc.T -> string -> string * Calc.state_post (*TODO: string * Calc.T*)
walther@59747
    11
end
walther@59747
    12
walther@59747
    13
(**)
walther@59748
    14
structure Step_Solve(**): STEP_SOLVE(**) =
walther@59747
    15
struct
walther@59748
    16
(**)
walther@59749
    17
open Ctree;
walther@59749
    18
open Pos
walther@59749
    19
walther@59837
    20
fun by_tactic (tac as Tactic.Apply_Method' _) (ptp as (pt, p)) =
walther@59837
    21
    LI.by_tactic tac (get_istate_LI pt p, get_ctxt_LI pt p) ptp
walther@59842
    22
  | by_tactic (tac as Tactic.Check_Postcond' _) (ptp as (pt, p)) =
walther@59842
    23
    LI.by_tactic tac (get_istate_LI pt p, get_ctxt_LI pt p) ptp
walther@59751
    24
  | by_tactic (Tactic.Free_Solve')  (pt, po as (p, _)) =
walther@59749
    25
    let
walther@59749
    26
      val p' = lev_dn_ (p, Res);
walther@60154
    27
      val pt = update_metID pt (par_pblobj pt p) MethodC.id_empty;
walther@59749
    28
    in
walther@59846
    29
      ("ok", ([(Tactic.Empty_Tac, Tactic.Empty_Tac_, (po, (Istate.Uistate, ContextC.empty)))], [], (pt,p')))
walther@59749
    30
    end
walther@59759
    31
  | by_tactic (Tactic.End_Proof'') ptp = ("end-proof", ([], [], ptp))
walther@59751
    32
  | by_tactic (Tactic.End_Detail' t) (pt, (p, p_)) = (* could be done by generate1 ?!? *)
walther@59749
    33
    let (*Rewrite_Set* done at Detail_Set*: this result is already in ctree*)
walther@59749
    34
      val pr = (lev_up p, Res)
walther@59749
    35
    in
walther@59749
    36
      ("ok", ([(Tactic.End_Detail, Tactic.End_Detail' t , ((p, p_), get_loc pt (p, p_)))], [], (pt, pr)))
walther@59749
    37
    end
walther@59751
    38
  | by_tactic m (pt, po as (p, p_)) =
Walther@60641
    39
    let
Walther@60641
    40
      val ctxt = get_ctxt pt po
Walther@60641
    41
      val thy = Proof_Context.theory_of ctxt
Walther@60641
    42
    in
Walther@60641
    43
      if MethodC.id_empty = get_obj g_metID pt (par_pblobj pt p) (*29.8.02: could be detail, too !!*)
Walther@60641
    44
      then
Walther@60641
    45
        let val ((p, p_), ps, _, pt) = Solve_Step.add_general m (Istate.empty, ctxt) (pt, (p, p_))
Walther@60641
    46
  	    in ("no-method-specified", (*Free_Solve*)
Walther@60641
    47
  	      ([(Tactic.Empty_Tac, Tactic.Empty_Tac_, ((p, p_), (Istate.Uistate, ctxt)))], ps, (pt, (p, p_))))
Walther@60641
    48
        end
Walther@60641
    49
      else
Walther@60641
    50
  	    let val (is, sc) = LItool.resume_prog (p,p_) pt;
Walther@60641
    51
  	    in
Walther@60641
    52
          case LI.locate_input_tactic sc (pt, po) (fst is) (snd is) m of
Walther@60641
    53
            LI.Safe_Step (istate, ctxt, tac) =>
Walther@60641
    54
              let
Walther@60641
    55
                 val p' = next_in_prog po
Walther@60641
    56
                 val (p'', _, _,pt') = Solve_Step.add_general tac (istate, ctxt) (pt, (p', p_))
Walther@60641
    57
              in
Walther@60641
    58
         		    ("ok", ([(Tactic.input_from_T ctxt tac, tac, (p'', (istate, ctxt)))],
Walther@60641
    59
                  [(*Ctree NOT cut*)], (pt', p'')))
Walther@60641
    60
              end
Walther@60641
    61
  	      | LI.Unsafe_Step (istate, ctxt, tac) =>
Walther@60641
    62
              let
Walther@60641
    63
                 val p' = 
Walther@60641
    64
                   case p_ of Frm => p 
Walther@60641
    65
                   | Res => lev_on p
Walther@60641
    66
                   | _ => raise ERROR ("Step_Solve.by_tactic: call by " ^ pos'2str (p, p_));
Walther@60641
    67
                 val (p'', _, _,pt') = Solve_Step.add_general tac (istate, ctxt) (pt, (p', p_));
Walther@60641
    68
              in
Walther@60641
    69
         		    ("ok", ([(Tactic.input_from_T ctxt tac, tac, (p'', (istate, ctxt)))],
Walther@60641
    70
                  [(*Ctree NOT cut*)], (pt', p'')))
Walther@60641
    71
              end
Walther@60641
    72
  	      | _ => (* NotLocatable *)
Walther@60641
    73
  	        let val (p,ps, _, pt) = Solve_Step.add_hard thy m (p, p_) pt;
Walther@60641
    74
  	        in
Walther@60641
    75
  	          ("not-found-in-program", ([(Tactic.input_from_T ContextC.empty m, m, (po, is))], ps, (pt, p)))
walther@59749
    76
            end
Walther@60641
    77
  	    end
Walther@60641
    78
    end;
walther@59747
    79
walther@59791
    80
val do_next = LI.do_next
walther@59760
    81
walther@59848
    82
walther@59848
    83
(*
walther@59848
    84
  Locate a step in a program, which has been determined by input of a term.
walther@59848
    85
  Special case: if the term is a CAS-command, then create a calchead, and do 1 step.
walther@59909
    86
  If "no derivation found" then Error_Pattern.check_for.
walther@59909
    87
  (Error_Pattern.check_for *within* compare_step seems too expensive)
walther@59848
    88
*)
walther@59981
    89
(*fun by_term (next_cs as (_, _, (pt, pos as (p, _))): Calc.state_post) istr =*)
Walther@60658
    90
(*will come directly from PIDE--vvvvvvvvvvv*)
Walther@60756
    91
fun by_term (pt, pos as (p, _)) istr =
Walther@60658
    92
  let
Walther@60658
    93
    val f_in = Syntax.read_term (Ctree.get_ctxt pt pos) istr
Walther@60658
    94
      handle ERROR msg => error (msg (*^ Position.here pp*))
Walther@60658
    95
    val pos_pred = lev_back(*'*) pos
Walther@60658
    96
	  val f_pred = Ctree.get_curr_formula (pt, pos_pred);
Walther@60658
    97
	  val f_succ = Ctree.get_curr_formula (pt, pos);
Walther@60658
    98
  in
Walther@60658
    99
    if f_succ = f_in
Walther@60658
   100
    then ("same-formula", ([], [], (pt, pos))) (* ctree not cut with replaceFormula *)
Walther@60658
   101
    else
Walther@60658
   102
      case CAS_Cmd.input f_in of
Walther@60658
   103
        SOME (pt, _) => ("ok", ([], [], (pt, (p, Pos.Met))))
Walther@60658
   104
	    | NONE =>
Walther@60658
   105
        case LI.locate_input_term (pt, pos) f_in of
Walther@60658
   106
          LI.Found_Step cstate =>
Walther@60658
   107
          ("ok" , ([], [], cstate (* already contains istate, ctxt *)))
Walther@60658
   108
        | LI.Not_Derivable =>
Walther@60658
   109
          let
Walther@60658
   110
            val ctxt = Ctree.get_ctxt pt pos
Walther@60658
   111
            val met_id = Ctree.get_obj Ctree.g_metID pt (Ctree.par_pblobj pt p)
Walther@60658
   112
        	  val (errpats, rew_rls, prog) = (* after shift to Diff --vvvvvvvvvvvvvvv ctxt should be sufficient*)
Walther@60658
   113
        	    case MethodC.from_store' (Know_Store.get_via_last_thy "Build_Thydata") met_id of
Walther@60658
   114
        		    {errpats, rew_rls, program = Rule.Prog prog, ...} => (errpats, rew_rls, prog)
Walther@60658
   115
        		  | _ => raise ERROR "inform: uncovered case of get_met"
Walther@60658
   116
        	  val {env, ...} = Ctree.get_istate_LI pt pos |> Istate.the_pstate
Walther@60658
   117
        	in
Walther@60658
   118
        	  case Error_Pattern.check_for (Ctree.get_ctxt pt pos) (f_pred, f_in) (prog, env) (errpats, rew_rls) of
Walther@60658
   119
        	    SOME errpatID => ("error pattern #" ^ errpatID ^ "#", Calc.state_empty_post)
Walther@60658
   120
        	  | NONE => ("no derivation found", Calc.state_empty_post)
Walther@60658
   121
          end
Walther@60658
   122
  end
Walther@60658
   123
walther@59797
   124
walther@59747
   125
end