src/Tools/isac/Interpret/li-tool.sml
author wneuper <walther.neuper@jku.at>
Sat, 14 Aug 2021 16:07:07 +0200
changeset 60372 5e79b72e59d2
parent 60335 7701598a2182
child 60397 9f5eba248269
permissions -rw-r--r--
tuned cf.9b839d8ce74a
walther@59845
     1
(* Title:  Interpret/li-tool.sml
walther@59790
     2
   Author: Walther Neuper 2000
walther@59790
     3
   (c) due to copyright terms
walther@59845
     4
walther@59845
     5
Tools for Lucas_Interpreter
walther@59790
     6
*)
walther@59790
     7
walther@59790
     8
signature LUCAS_INTERPRETER_TOOL =
walther@59790
     9
sig
walther@59790
    10
  datatype ass =
walther@59906
    11
    Associated of Tactic.T * term (*..result*) * Proof.context
walther@59906
    12
  | Ass_Weak of Tactic.T * term (*..result*) * Proof.context
walther@59844
    13
  | Not_Associated;
walther@59845
    14
  val associate: Ctree.ctree -> Proof.context -> (Tactic.T * term (*..Prog_Tac*)) -> ass
walther@59790
    15
  
walther@60154
    16
  val init_pstate: Rule_Set.T -> Proof.context -> I_Model.T -> MethodC.id ->
walther@59826
    17
    Istate.T * Proof.context * Program.T
walther@59906
    18
  val resume_prog: ThyC.id (*..for lookup in Know_Store*) -> Pos.pos' -> Ctree.ctree -> 
walther@59831
    19
    (Istate.T * Proof.context) * Program.T
walther@59824
    20
walther@59906
    21
  val check_leaf: string -> Proof.context -> Rule_Set.T -> (Env.T * (term option * term)) -> term -> 
walther@59790
    22
      Program.leaf * term option
walther@59901
    23
walther@59906
    24
  val implicit_take: Program.T -> (term * term) list -> term option
walther@59906
    25
  val get_simplifier: Calc.T -> Rule_Set.T
walther@59906
    26
  val tac_from_prog: Ctree.ctree -> theory (*..for lookup in Know_Store*) -> term -> Tactic.input
walther@59906
    27
walther@59901
    28
  val trace_on: bool Unsynchronized.ref
wenzelm@60223
    29
\<^isac_test>\<open>
walther@60154
    30
  val arguments_from_model : MethodC.id -> I_Model.T -> term list
wenzelm@60223
    31
\<close>
walther@59790
    32
end 
walther@59790
    33
walther@59790
    34
(**)
walther@59790
    35
structure LItool(**): LUCAS_INTERPRETER_TOOL(**) =
walther@59790
    36
struct
walther@59790
    37
(**)
walther@59790
    38
open Ctree
walther@59790
    39
open Pos
walther@59790
    40
walther@59901
    41
(* traces the leaves (ie. non-tactical nodes) of Prog found by find_next_step *)   
walther@59901
    42
val trace_on = Unsynchronized.ref false; (* TODO: adopt Isabelle's tracing *)
walther@59901
    43
walther@59848
    44
(* find the formal argument of a tactic in case there is only one (e.g. in simplification) *)
walther@59848
    45
fun implicit_take (Rule.Prog prog) env =
walther@60372
    46
    Option.map (subst_atomic env) (Prog_Tac.get_first_argument prog)
walther@59962
    47
  | implicit_take _ _ = raise ERROR "implicit_take: no match";
walther@59790
    48
walther@59848
    49
(*  *)
walther@59848
    50
val error_msg_1 = "ERROR: the guard is missing (#ppc in 'type met' added in prep_met)."
walther@59868
    51
fun error_msg_2 d itms = ("arguments_from_model: '" ^ UnparseC.term d ^ "' not in itms:\n" ^
walther@59942
    52
  "itms:\n" ^ I_Model.to_string @{context} itms)
walther@60152
    53
(* turn Model-items into arguments for a program *)
walther@59848
    54
fun arguments_from_model mI itms =
walther@59790
    55
  let
walther@59943
    56
    val mvat = I_Model.max_vt itms
walther@59790
    57
    fun okv mvat (_, vats, b, _, _) = member op = vats mvat andalso b
walther@59790
    58
    val itms = filter (okv mvat) itms
walther@59943
    59
    fun test_dsc d (_, _, _, _, itm_) = (d = I_Model.d_in itm_)
walther@59790
    60
    fun itm2arg itms (_,(d,_)) =
walther@59848
    61
      case find_first (test_dsc d) itms of
walther@59848
    62
        NONE => raise ERROR (error_msg_2 d itms)
walther@59943
    63
      | SOME (_, _, _, _, itm_) => I_Model.penvval_in itm_
walther@60154
    64
    val pats = (#ppc o MethodC.from_store) mI
walther@59848
    65
    val _ = if pats = [] then raise ERROR error_msg_1 else ()
walther@59790
    66
  in (flat o (map (itm2arg itms))) pats end;
walther@59790
    67
walther@59848
    68
(* convert a Prog_Tac to Tactic.input; specific for "Prog_Tac.SubProblem" *)
walther@60335
    69
fun tac_from_prog _ thy (Const (\<^const_name>\<open>Prog_Tac.Rewrite\<close>, _) $ thmID $ _) =
walther@59790
    70
    let
walther@59790
    71
      val tid = HOLogic.dest_string thmID
walther@59876
    72
    in (Tactic.Rewrite (tid, ThmC.thm_from_thy thy tid)) end
walther@60335
    73
  | tac_from_prog _ thy (Const (\<^const_name>\<open>Prog_Tac.Rewrite_Inst\<close>, _) $ sub $ thmID $ _) =
walther@59790
    74
    let
walther@59790
    75
      val tid = HOLogic.dest_string thmID
walther@59912
    76
    in (Tactic.Rewrite_Inst (Subst.program_to_input sub, (tid, ThmC.thm_from_thy thy tid))) end
walther@60335
    77
  | tac_from_prog _ _ (Const (\<^const_name>\<open>Prog_Tac.Rewrite_Set\<close>,_) $ rls $ _) =
walther@59825
    78
     (Tactic.Rewrite_Set (HOLogic.dest_string rls))
walther@60335
    79
  | tac_from_prog _ _ (Const (\<^const_name>\<open>Prog_Tac.Rewrite_Set_Inst\<close>, _) $ sub $ rls $ _) =
walther@59912
    80
      (Tactic.Rewrite_Set_Inst (Subst.program_to_input sub, HOLogic.dest_string rls))
walther@60335
    81
  | tac_from_prog _ _ (Const (\<^const_name>\<open>Prog_Tac.Calculate\<close>, _) $ op_ $ _) =
walther@59825
    82
      (Tactic.Calculate (HOLogic.dest_string op_))
walther@60335
    83
  | tac_from_prog _ _ (Const (\<^const_name>\<open>Prog_Tac.Take\<close>, _) $ t) = (Tactic.Take (UnparseC.term t))
walther@60335
    84
  | tac_from_prog _ _ (Const (\<^const_name>\<open>Prog_Tac.Substitute\<close>, _) $ isasub $ _) =
walther@59912
    85
    (Tactic.Substitute ((Subst.eqs_to_input o TermC.isalist2list) isasub))
walther@60335
    86
  | tac_from_prog _ thy (Const (\<^const_name>\<open>Prog_Tac.Check_elementwise\<close>, _) $ _ $ 
wenzelm@60309
    87
    (Const (\<^const_name>\<open>Collect\<close>, _) $ Abs (_, _, pred))) =
walther@59870
    88
      (Tactic.Check_elementwise (UnparseC.term_in_thy thy pred))
walther@60335
    89
  | tac_from_prog _ _ (Const (\<^const_name>\<open>Prog_Tac.Or_to_List\<close>, _) $ _ ) = Tactic.Or_to_List
walther@60335
    90
  | tac_from_prog pt _ (ptac as Const (\<^const_name>\<open>Prog_Tac.SubProblem\<close>, _) $ _ $ _) =
walther@59825
    91
    fst (Sub_Problem.tac_from_prog pt ptac) (*might involve problem refinement etc*)
walther@59962
    92
  | tac_from_prog _ thy t = raise ERROR ("stac2tac_ TODO: no match for " ^ UnparseC.term_in_thy thy t);
walther@59790
    93
walther@59790
    94
datatype ass =
walther@59844
    95
    Associated of
walther@59848
    96
      Tactic.T         (* Subproblem' gets args instantiated in associate *)
walther@59848
    97
      * term           (* resulting from application of Tactic.T          *)
walther@59848
    98
      * Proof.context  (* updated by assumptioons from Rewrite*           *)
walther@59790
    99
  | Ass_Weak of Tactic.T * term * Proof.context
walther@59844
   100
  | Not_Associated;
walther@59790
   101
walther@59906
   102
(*
walther@59906
   103
  associate is the ONLY code within by_tactic, which handles Tactic.T individually;
walther@59906
   104
  thus it does ContextC.insert_assumptions in case of Rewrite*.
walther@59906
   105
  The argument Ctree.ctree is required only for Subproblem'.
walther@59906
   106
*)
walther@59848
   107
fun trace_msg_3 tac =
walther@59901
   108
  if (!trace_on) then
walther@59848
   109
    tracing("@@@ the \"tac_\" proposed to apply does NOT match the leaf found in the program:\n" ^
walther@59848
   110
      "@@@ tac_ = \"" ^ Tactic.string_of tac ^ "\"")
walther@59848
   111
  else ();
walther@59790
   112
fun associate _ ctxt (m as Tactic.Rewrite_Inst'
walther@59848
   113
        (_, _, _, _, _, thm'' as (thmID, _), f, (f', asms')), stac) =
walther@59848
   114
      (case stac of
walther@60335
   115
  	    (Const (\<^const_name>\<open>Prog_Tac.Rewrite_Inst\<close>, _) $ _ $ thmID_ $ f_) =>
walther@59848
   116
  	      if thmID = HOLogic.dest_string thmID_ then
walther@59848
   117
  	        if f = f_ then 
walther@59848
   118
              Associated (m, f', ContextC.insert_assumptions asms' ctxt)
walther@59848
   119
  	        else Ass_Weak (m, f', ContextC.insert_assumptions asms' ctxt)
walther@59848
   120
  	      else Not_Associated
walther@60335
   121
      | (Const (\<^const_name>\<open>Prog_Tac.Rewrite_Set_Inst\<close>,_) $ _ $ rls_ $ f_) =>
walther@59914
   122
  	      if Rule_Set.contains (Rule.Thm thm'') (assoc_rls (HOLogic.dest_string rls_)) then
walther@59848
   123
            if f = f_ then
walther@59848
   124
              Associated (m, f', ContextC.insert_assumptions asms' ctxt)
walther@59848
   125
            else Ass_Weak (m, f', ContextC.insert_assumptions asms' ctxt)
walther@59848
   126
  	      else Not_Associated
walther@59848
   127
      | _ => Not_Associated)
walther@59790
   128
  | associate _ ctxt (m as Tactic.Rewrite' (_, _, _, _, thm'' as (thmID, _), f, (f', asms')), stac) =
walther@59848
   129
      (case stac of
walther@60335
   130
  	    (Const (\<^const_name>\<open>Prog_Tac.Rewrite\<close>, _) $ thmID_ $ f_) =>
walther@59848
   131
  	      if thmID = HOLogic.dest_string thmID_ then
walther@59848
   132
  	        if f = f_ then
walther@59848
   133
              Associated (m, f', ContextC.insert_assumptions asms' ctxt)
walther@59848
   134
  	        else Ass_Weak (m, f', ContextC.insert_assumptions asms' ctxt)
walther@59848
   135
  	      else Not_Associated
walther@60335
   136
      | (Const (\<^const_name>\<open>Prog_Tac.Rewrite_Set\<close>, _) $ rls_ $ f_) =>
walther@59914
   137
  	       if Rule_Set.contains (Rule.Thm thm'') (assoc_rls (HOLogic.dest_string rls_)) then
walther@59848
   138
             if f = f_ then
walther@59848
   139
               Associated (m, f', ContextC.insert_assumptions asms' ctxt)
walther@59848
   140
  	         else Ass_Weak (m, f', ContextC.insert_assumptions asms' ctxt)
walther@59848
   141
  	       else Not_Associated
walther@59848
   142
      | _ => Not_Associated)
walther@59790
   143
  | associate _ ctxt (m as Tactic.Rewrite_Set_Inst' (_, _, _, rls, f, (f', asms')),
walther@60335
   144
        (Const (\<^const_name>\<open>Prog_Tac.Rewrite_Set_Inst\<close>, _) $ _ $ rls_ $ f_)) = 
walther@59867
   145
      if Rule_Set.id rls = HOLogic.dest_string rls_ then
walther@59848
   146
        if f = f_ then
walther@59848
   147
          Associated (m, f', ContextC.insert_assumptions asms' ctxt)
walther@59848
   148
        else Ass_Weak (m ,f', ContextC.insert_assumptions asms' ctxt)
walther@59848
   149
      else Not_Associated
walther@59790
   150
  | associate _ ctxt (m as Tactic.Rewrite_Set' (_, _, rls, f, (f', asms')),
walther@60335
   151
        (Const (\<^const_name>\<open>Prog_Tac.Rewrite_Set\<close>, _) $ rls_ $ f_)) = 
walther@59867
   152
      if Rule_Set.id rls = HOLogic.dest_string rls_ then
walther@59848
   153
        if f = f_ then
walther@59848
   154
          Associated (m, f', ContextC.insert_assumptions asms' ctxt)
walther@59848
   155
        else Ass_Weak (m ,f', ContextC.insert_assumptions asms' ctxt)
walther@59848
   156
      else Not_Associated
walther@59790
   157
  | associate _ ctxt (m as Tactic.Calculate' (_, op_, f, (f', _)), stac) =
walther@59848
   158
      (case stac of
walther@60335
   159
  	    (Const (\<^const_name>\<open>Prog_Tac.Calculate\<close>,_) $ op__ $ f_) =>
walther@59848
   160
  	      if op_ = HOLogic.dest_string op__ then
walther@59848
   161
            if f = f_ then Associated (m, f', ctxt) else Ass_Weak (m ,f', ctxt)
walther@59848
   162
  	      else Not_Associated
walther@60335
   163
      | (Const (\<^const_name>\<open>Prog_Tac.Rewrite_Set_Inst\<close>, _) $ _ $ rls_ $ f_)  =>
walther@59914
   164
          if Rule_Set.contains (Rule.Eval (assoc_calc' (ThyC.get_theory "Isac_Knowledge")
walther@59848
   165
            op_ |> snd)) (assoc_rls (HOLogic.dest_string rls_)) then
walther@59848
   166
            if f = f_ then Associated (m, f', ctxt) else Ass_Weak (m ,f', ctxt)
walther@59844
   167
          else Not_Associated
walther@60335
   168
      | (Const (\<^const_name>\<open>Prog_Tac.Rewrite_Set\<close>,_) $ rls_ $ f_) =>
walther@59914
   169
          if Rule_Set.contains (Rule.Eval (assoc_calc' ( ThyC.get_theory "Isac_Knowledge")
walther@59848
   170
            op_ |> snd)) (assoc_rls (HOLogic.dest_string rls_)) then
walther@59848
   171
            if f = f_ then Associated (m, f', ctxt) else Ass_Weak (m ,f', ctxt)
walther@59844
   172
          else Not_Associated
walther@59848
   173
      | _ => Not_Associated)
walther@59790
   174
  | associate _ ctxt (m as Tactic.Check_elementwise' (consts, _, (consts_chkd, _)),
walther@60335
   175
        (Const (\<^const_name>\<open>Prog_Tac.Check_elementwise\<close>,_) $ consts' $ _)) =
walther@59848
   176
      if consts = consts' then Associated (m, consts_chkd, ctxt) else Not_Associated
walther@60335
   177
  | associate _ ctxt (m as Tactic.Or_to_List' (_, list), (Const (\<^const_name>\<open>Prog_Tac.Or_to_List\<close>, _) $ _)) =
walther@59848
   178
      Associated (m, list, ctxt)
walther@60335
   179
  | associate _ ctxt (m as Tactic.Take' term, (Const (\<^const_name>\<open>Prog_Tac.Take\<close>, _) $ _)) =
walther@59848
   180
      Associated (m, term, ctxt)
walther@59790
   181
  | associate _ ctxt (m as Tactic.Substitute' (ro, erls, subte, f, f'),
walther@60335
   182
        (Const (\<^const_name>\<open>Prog_Tac.Substitute\<close>, _) $ _ $ t)) =
walther@59848
   183
  	  if f = t then
walther@59848
   184
  	   Associated (m, f', ctxt)
walther@59848
   185
  	  else (*compare | applicable_in (p,p_) pt (m as Substitute sube)*)
walther@59848
   186
  		  if foldl and_ (true, map TermC.contains_Var subte) then
walther@59848
   187
  		    let
walther@59848
   188
  		      val t' = subst_atomic (map HOLogic.dest_eq subte) t
walther@59848
   189
  		    in
walther@59962
   190
  		      if t = t' then raise ERROR "associate: Substitute' not applicable to val of Expr"
walther@59848
   191
  		      else Associated (Tactic.Substitute' (ro, erls, subte, t, t'), t', ctxt)
walther@59848
   192
  		    end
walther@59848
   193
  		  else
walther@59854
   194
  		    (case Rewrite.rewrite_terms_ (ThyC.Isac ()) ro erls subte t of
walther@59848
   195
  		      SOME (t', _) =>  Associated (Tactic.Substitute' (ro, erls, subte, t, t'), t', ctxt)
walther@59962
   196
  		    | NONE => raise ERROR "associate: Substitute' not applicable to val of Expr")
walther@59817
   197
  | associate pt ctxt (Tactic.Subproblem' ((domID, pblID, _), _, _, _, _, _),
walther@60335
   198
        (stac as Const (\<^const_name>\<open>Prog_Tac.SubProblem\<close>, _) $ _ $ _)) =
walther@59848
   199
      (case Sub_Problem.tac_from_prog pt stac of
walther@59848
   200
        (_, tac as Tactic.Subproblem' ((dI, pI, _), _, _, _, _, f)) =>
walther@59848
   201
          if domID = dI andalso pblID = pI then Associated (tac, f, ctxt) else Not_Associated
walther@59848
   202
      | _ => raise ERROR ("associate: uncovered case"))
walther@59846
   203
  | associate _ _ (tac, _) = 
walther@59846
   204
    (trace_msg_3 tac; Not_Associated);
walther@59790
   205
walther@59790
   206
(* create the initial interpreter state
walther@59848
   207
  from the items of the guard and the formal arguments of the program.
walther@59790
   208
Note on progression from (a) type fmz_ \<longrightarrow> (e) arguments of the partial_function:
walther@59790
   209
  (a) fmz is given by a math author
walther@59790
   210
  (b) fmz_ is transformed to ori list as a prerequisite for efficient interactive modelling
walther@59790
   211
  (c) modelling creates an itm list from ori list + possible user input
walther@59848
   212
  (d) specifying a theory might add some items and create a guard for the program
walther@59848
   213
  (e) fun relate_args creates an environment for evaluating the program.
walther@59848
   214
Note on sequence-relation (a) -- (e), i.e. the (formal) arguments of the program:
walther@60125
   215
  * Since the arguments of the partial_function have no description (see Input_Descript.thy),
walther@59790
   216
    (e) depends on the sequence in fmz_ and on the types of the formal arguments.
walther@59790
   217
  * pairing formal arguments with itm's follows the sequence
walther@59790
   218
  * Thus the resulting sequence-relation can be ambiguous.
walther@59790
   219
  * Ambiguities are done by rearranging fmz_ or the formal arguments.
walther@59848
   220
  * The latter is easier, albeit not optimal: a fmz_ can be used by different programs.
walther@59790
   221
  *)
walther@59790
   222
local
walther@59790
   223
val errmsg = "ERROR: found no actual arguments for prog. of "
walther@59790
   224
fun msg_miss sc metID caller f formals actuals =
walther@59790
   225
  "ERROR in creating the environment from formal args. of partial_function \"" ^ fst (Program.get_fun_id sc) ^ "\"\n" ^
walther@60154
   226
	"and the actual args., ie. items of the guard of \"" ^ MethodC.id_to_string metID ^ "\" by \"" ^ caller ^ "\":\n" ^
walther@59868
   227
	"formal arg \"" ^ UnparseC.term f ^ "\" doesn't mach an actual arg.\n" ^
walther@59790
   228
	"with:\n" ^
walther@59868
   229
	(string_of_int o length) formals ^ " formal args: " ^ UnparseC.terms formals ^ "\n" ^
walther@59868
   230
	(string_of_int o length) actuals ^ " actual args: " ^ UnparseC.terms actuals
walther@59790
   231
fun msg_miss_type sc metID f formals actuals =
walther@59790
   232
  "ERROR in creating the environment from formal args. of partial_function \"" ^ fst (Program.get_fun_id sc) ^ "\"\n" ^
walther@60154
   233
	"and the actual args., ie. items of the guard of \"" ^ MethodC.id_to_string metID ^ "\" by \"assoc_by_type\":\n" ^
walther@59868
   234
	"formal arg \"" ^ UnparseC.term f ^ "\" of type \"" ^ UnparseC.typ (type_of f) ^
walther@59790
   235
  "\" doesn't mach an actual arg.\nwith:\n" ^
walther@59868
   236
	(string_of_int o length) formals ^ " formal args: " ^ UnparseC.terms formals ^ "\n" ^
walther@59868
   237
	(string_of_int o length) actuals ^ " actual args: " ^ UnparseC.terms actuals ^ "\n" ^
walther@59868
   238
  "   with types: " ^ (strs2str o (map (UnparseC.typ o type_of))) actuals
walther@59790
   239
fun msg_ambiguous sc metID f aas formals actuals =
walther@59790
   240
  "AMBIGUITY in creating the environment from formal args. of partial_function \"" ^ fst (Program.get_fun_id sc) ^ "\"\n" ^
walther@60154
   241
	"and the actual args., ie. items of the guard of \"" ^ MethodC.id_to_string metID ^ "\" by \"assoc_by_type\":\n" ^
walther@59868
   242
  "formal arg. \"" ^ UnparseC.term f ^ "\" type-matches with several..."  ^
walther@59868
   243
  "actual args. \"" ^ UnparseC.terms aas ^ "\"\n" ^
walther@59868
   244
  "selected \"" ^ UnparseC.term (hd aas) ^ "\"\n" ^
walther@59790
   245
	"with\n" ^
walther@59868
   246
	"formals: " ^ UnparseC.terms formals ^ "\n" ^
walther@59868
   247
	"actuals: " ^ UnparseC.terms actuals
walther@59790
   248
fun trace_init metID =
walther@59901
   249
  if (!trace_on) 
walther@59846
   250
  then tracing("@@@ program \"" ^ strs2str metID ^ "\"")
walther@59790
   251
  else ();
walther@59790
   252
in
walther@59790
   253
fun init_pstate srls ctxt itms metID =
walther@59790
   254
  let
walther@59848
   255
    val actuals = arguments_from_model metID itms
walther@59790
   256
    val _ = if actuals <> [] then () else raise ERROR (errmsg ^ strs2str' metID)
walther@60154
   257
    val (scr, sc) = (case (#scr o MethodC.from_store) metID of
walther@59790
   258
           scr as Rule.Prog sc => (trace_init metID; (scr, sc))
walther@60154
   259
       | _ => raise ERROR ("init_pstate with " ^ MethodC.id_to_string metID))
walther@59790
   260
    val formals = Program.formal_args sc    
walther@59790
   261
    fun assoc_by_type f aa =
walther@59790
   262
      case filter (curry (fn (f, a) => type_of f = type_of a) f) aa of
walther@59962
   263
        [] => raise ERROR (msg_miss_type sc metID f formals actuals)
walther@59790
   264
      | [a] => (f, a)
walther@59790
   265
      | aas as (a :: _) => (writeln (msg_ambiguous sc metID f aas formals actuals); (f, a));
walther@59962
   266
	  fun relate_args _ (f::_)  [] = raise ERROR (msg_miss sc metID "relate_args" f formals actuals)
walther@59790
   267
	    | relate_args env [] _ = env (*may drop Find?*)
walther@59790
   268
	    | relate_args env (f::ff) (aas as (a::aa)) = 
walther@59790
   269
	      if type_of f = type_of a 
walther@59790
   270
	      then relate_args (env @ [(f, a)]) ff aa
walther@59790
   271
        else
walther@59790
   272
          let val (f, a) = assoc_by_type f aas
walther@59790
   273
          in relate_args (env @ [(f, a)]) ff (remove op = a aas) end
walther@60154
   274
    val {pre, prls, ...} = MethodC.from_store metID;
walther@60018
   275
    val pres = Pre_Conds.check prls pre itms 0 |> snd |> map snd;
walther@59790
   276
    val ctxt = ctxt |> ContextC.insert_assumptions pres;
walther@59790
   277
    val ist = Istate.e_pstate
walther@59790
   278
      |> Istate.set_eval srls
walther@59790
   279
      |> Istate.set_env_true (relate_args [] formals actuals)
walther@59790
   280
  in
walther@59790
   281
    (Istate.Pstate ist, ctxt, scr)
walther@59790
   282
  end;
walther@59790
   283
end (*local*)
walther@59790
   284
walther@59848
   285
(* get the simplifier of the current method *)
walther@59790
   286
fun get_simplifier (pt, (p, _)) =
walther@59790
   287
  let
walther@59790
   288
    val p' = Ctree.par_pblobj pt p
walther@59790
   289
	  val metID = Ctree.get_obj Ctree.g_metID pt p'
walther@60154
   290
	  val {srls, ...} = MethodC.from_store metID
walther@59790
   291
  in srls end
walther@59790
   292
walther@59848
   293
(* resume program interpretation at the beginning of a step *)
walther@59831
   294
fun resume_prog thy (p, p_) pt =
walther@59838
   295
  let
walther@59838
   296
    val (is_problem, p', rls') = parent_node pt p
walther@59838
   297
  in
walther@59848
   298
    if is_problem then
walther@59838
   299
      let
walther@59838
   300
	      val metID = get_obj g_metID pt p'
walther@60154
   301
	      val {srls, ...} = MethodC.from_store metID
walther@59838
   302
	      val (is, ctxt) =
walther@59838
   303
	        case get_loc pt (p, p_) of
walther@59838
   304
	           (Istate.Pstate ist, ctxt) => (Istate.Pstate (Istate.set_eval srls ist), ctxt)
walther@59838
   305
	        | _ => raise ERROR "resume_prog: unexpected result from get_loc"
walther@59848
   306
	    in
walther@60154
   307
	      ((is, ctxt), (#scr o MethodC.from_store) metID)
walther@59848
   308
	    end
walther@59848
   309
    else
walther@59838
   310
      (get_loc pt (p, p_),
walther@59881
   311
      Rule.Prog (Auto_Prog.gen (ThyC.get_theory thy) (get_last_formula (pt, (p, p_))) rls'))
walther@59838
   312
  end
walther@59790
   313
walther@59848
   314
walther@59845
   315
fun trace_msg_1 call t stac =
walther@59901
   316
  if (! trace_on) then
walther@59868
   317
	  tracing ("@@@ " ^ call ^ " leaf \"" ^ UnparseC.term t ^ "\" \<longrightarrow> Tac \"" ^ UnparseC.term stac ^ "\"")
walther@59845
   318
	else ();
walther@59845
   319
fun trace_msg_2 call t lexpr' =
walther@59901
   320
  if (! trace_on) then
walther@59868
   321
	  tracing("@@@ " ^ call ^ " leaf '" ^ UnparseC.term t ^ "' \<longrightarrow> Expr \"" ^ UnparseC.term lexpr' ^ "\"")
walther@59845
   322
	else ();
walther@59848
   323
(*
walther@59848
   324
  check a leaf at the end of recursive descent; a leaf is either a Prog_Tac or a Prog_Expr.
walther@59848
   325
  snd of return value is the formal arguments in case of currying.
walther@59848
   326
*)
walther@59790
   327
fun check_leaf call ctxt srls (E, (a, v)) t =
walther@59790
   328
    case Prog_Tac.eval_leaf E a v t of
walther@59790
   329
	    (Program.Tac stac, a') =>
walther@59848
   330
	      let
walther@59848
   331
	        val stac' = Rewrite.eval_prog_expr (Proof_Context.theory_of ctxt) srls 
walther@59801
   332
              (subst_atomic (Env.update_opt E (a, v)) stac)
walther@59790
   333
	      in
walther@59845
   334
          (trace_msg_1 call t stac; (Program.Tac stac', a'))
walther@59790
   335
	      end
walther@59790
   336
    | (Program.Expr lexpr, a') =>
walther@59848
   337
	      let
walther@59848
   338
	        val lexpr' = Rewrite.eval_prog_expr (Proof_Context.theory_of ctxt) srls
walther@59801
   339
              (subst_atomic (Env.update_opt E (a, v)) lexpr)
walther@59790
   340
	      in
walther@59845
   341
          (trace_msg_2 call t lexpr'; (Program.Expr lexpr', a'))
walther@59790
   342
	      end;
walther@59790
   343
walther@59790
   344
(**)end(**)