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