src/Tools/isac/MathEngine/solve.sml
author Walther Neuper <walther.neuper@jku.at>
Thu, 19 Dec 2019 16:41:57 +0100
changeset 59749 cc3b1807f72e
parent 59747 8e5335c63475
child 59751 fa26464c66bf
permissions -rw-r--r--
cleanup fun solve, shift from Solve --> Step_Solve
neuper@41968
     1
(* Title:  solve an example by interpreting a method's script
neuper@41968
     2
   Author: Walther Neuper 1999
neuper@41968
     3
   (c) copyright due to lincense terms.
neuper@37906
     4
*)
neuper@37906
     5
wneuper@59562
     6
signature SOLVE_PHASE =
wneuper@59286
     7
sig
wneuper@59286
     8
  datatype auto = CompleteCalc | CompleteCalcHead | CompleteModel | CompleteSubpbl 
walther@59747
     9
  | CompleteToSubpbl | Steps of int
wneuper@59286
    10
  val autoord : auto -> int
wneuper@59286
    11
  type tac'_
wneuper@59571
    12
  val mk_tac'_ : Tactic.input -> string * Tactic.input
wneuper@59286
    13
  val specsteps : string list
wneuper@59286
    14
wneuper@59286
    15
  val all_solve : auto -> Ctree.pos' list -> Ctree.state ->
wneuper@59286
    16
    string * Ctree.pos' list * (Ctree.ctree * (int list * Ctree.pos_))
wneuper@59286
    17
  val complete_solve :
wneuper@59286
    18
     auto -> Ctree.pos' list -> Ctree.state -> string * Ctree.pos' list * Ctree.state
walther@59749
    19
(*
wneuper@59571
    20
  val solve : string * Tactic.T -> Ctree.state -> string * Chead.calcstate'
walther@59749
    21
*)
wneuper@59562
    22
  val inform : Chead.calcstate' -> string -> string * Chead.calcstate'
wneuper@59562
    23
wneuper@59286
    24
  val detailrls : Ctree.ctree -> Ctree.pos' -> string * Ctree.ctree * Ctree.pos'
wneuper@59310
    25
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
wneuper@59286
    26
  val get_form : tac'_ -> Ctree.pos' -> Ctree.ctree -> Generate.mout
walther@59702
    27
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
wneuper@59570
    28
  (*NONE*)
walther@59702
    29
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
wneuper@59310
    30
wneuper@59310
    31
(*----- unused code, kept as hints to design ideas ---------------------------------------------*)
wneuper@59310
    32
  (* NONE *)
wneuper@59286
    33
end
wneuper@59286
    34
wneuper@59286
    35
(**)
wneuper@59562
    36
structure Solve(**): SOLVE_PHASE(**) =
wneuper@59273
    37
struct
wneuper@59286
    38
(**)
wneuper@59276
    39
open Ctree;
walther@59695
    40
open Pos
wneuper@59273
    41
neuper@37906
    42
type mstID = string;
wneuper@59571
    43
type tac'_ = mstID * Tactic.input; (*DG <-> ME*)
neuper@37906
    44
wneuper@59286
    45
fun mk_tac'_   m = case m of (* scr not cleaned -- will disappear eventually *)
wneuper@59571
    46
  Tactic.Init_Proof (ppc, spec)    => ("Init_Proof", Tactic.Init_Proof (ppc, spec )) 
wneuper@59571
    47
| Tactic.Model_Problem             => ("Model_Problem", Tactic.Model_Problem)
wneuper@59571
    48
| Tactic.Refine_Tacitly pblID      => ("Refine_Tacitly", Tactic.Refine_Tacitly pblID)
wneuper@59571
    49
| Tactic.Refine_Problem pblID      => ("Refine_Problem", Tactic.Refine_Problem pblID)
wneuper@59571
    50
| Tactic.Add_Given cterm'          => ("Add_Given", Tactic.Add_Given cterm') 
wneuper@59571
    51
| Tactic.Del_Given cterm'          => ("Del_Given", Tactic.Del_Given cterm') 
wneuper@59571
    52
| Tactic.Add_Find cterm'           => ("Add_Find", Tactic.Add_Find cterm') 
wneuper@59571
    53
| Tactic.Del_Find cterm'           => ("Del_Find", Tactic.Del_Find cterm') 
wneuper@59571
    54
| Tactic.Add_Relation cterm'       => ("Add_Relation", Tactic.Add_Relation cterm') 
wneuper@59571
    55
| Tactic.Del_Relation cterm'       => ("Del_Relation", Tactic.Del_Relation cterm') 
neuper@37906
    56
wneuper@59571
    57
| Tactic.Specify_Theory domID	    => ("Specify_Theory", Tactic.Specify_Theory domID) 
wneuper@59571
    58
| Tactic.Specify_Problem pblID     => ("Specify_Problem", Tactic.Specify_Problem pblID)
wneuper@59571
    59
| Tactic.Specify_Method metID	    => ("Specify_Method", Tactic.Specify_Method metID) 
wneuper@59571
    60
| Tactic.Apply_Method metID	    => ("Apply_Method", Tactic.Apply_Method metID) 
wneuper@59571
    61
| Tactic.Check_Postcond pblID	    => ("Check_Postcond", Tactic.Check_Postcond pblID)
wneuper@59571
    62
| Tactic.Free_Solve                => ("Free_Solve", Tactic.Free_Solve)
neuper@37906
    63
		    
wneuper@59571
    64
| Tactic.Rewrite_Inst (subs, thm') => ("Rewrite_Inst", Tactic.Rewrite_Inst (subs, thm')) 
wneuper@59571
    65
| Tactic.Rewrite thm'		    => ("Rewrite", Tactic.Rewrite thm') 
wneuper@59571
    66
| Tactic.Rewrite_Asm thm'	    => ("Rewrite_Asm", Tactic.Rewrite_Asm thm') 
wneuper@59571
    67
| Tactic.Rewrite_Set_Inst (subs, rls')
wneuper@59571
    68
               => ("Rewrite_Set_Inst", Tactic.Rewrite_Set_Inst (subs, rls')) 
wneuper@59571
    69
| Tactic.Rewrite_Set rls'          => ("Rewrite_Set", Tactic.Rewrite_Set rls') 
wneuper@59571
    70
| Tactic.End_Ruleset		    => ("End_Ruleset", Tactic.End_Ruleset)
neuper@37906
    71
wneuper@59571
    72
| Tactic.End_Detail                => ("End_Detail", Tactic.End_Detail)
wneuper@59571
    73
| Tactic.Detail_Set rls'           => ("Detail_Set", Tactic.Detail_Set rls')
wneuper@59571
    74
| Tactic.Detail_Set_Inst (s, rls') => ("Detail_Set_Inst", Tactic.Detail_Set_Inst (s, rls'))
neuper@37906
    75
wneuper@59571
    76
| Tactic.Calculate op_             => ("Calculate", Tactic.Calculate op_)
wneuper@59571
    77
| Tactic.Substitute sube           => ("Substitute", Tactic.Substitute sube) 
wneuper@59571
    78
| Tactic.Apply_Assumption cts'	    => ("Apply_Assumption", Tactic.Apply_Assumption cts')
neuper@37906
    79
wneuper@59571
    80
| Tactic.Take cterm'               => ("Take", Tactic.Take cterm') 
wneuper@59571
    81
| Tactic.Take_Inst cterm'          => ("Take_Inst", Tactic.Take_Inst cterm') 
wneuper@59571
    82
| Tactic.Subproblem (domID, pblID) => ("Subproblem", Tactic.Subproblem (domID, pblID)) 
neuper@37906
    83
(*
wneuper@59571
    84
| Tactic.Subproblem_Full(spec,cts')=> ("Subproblem_Full", Tactic.Subproblem_Full(spec,cts')) 
neuper@37906
    85
*)
wneuper@59571
    86
| Tactic.End_Subproblem            => ("End_Subproblem", Tactic.End_Subproblem)
wneuper@59571
    87
| Tactic.CAScmd cterm'		    => ("CAScmd", Tactic.CAScmd cterm')
neuper@37906
    88
			    
wneuper@59571
    89
| Tactic.Split_And                 => ("Split_And", Tactic.Split_And) 
wneuper@59571
    90
| Tactic.Conclude_And		    => ("Conclude_And", Tactic.Conclude_And) 
wneuper@59571
    91
| Tactic.Split_Or                  => ("Split_Or", Tactic.Split_Or) 
wneuper@59571
    92
| Tactic.Conclude_Or		    => ("Conclude_Or", Tactic.Conclude_Or) 
wneuper@59571
    93
| Tactic.Begin_Trans               => ("Begin_Trans", Tactic.Begin_Trans) 
wneuper@59571
    94
| Tactic.End_Trans		    => ("End_Trans", Tactic.End_Trans) 
wneuper@59571
    95
| Tactic.Begin_Sequ                => ("Begin_Sequ", Tactic.Begin_Sequ) 
wneuper@59571
    96
| Tactic.End_Sequ                  => ("End_Sequ", Tactic.Begin_Sequ) 
wneuper@59571
    97
| Tactic.Split_Intersect           => ("Split_Intersect", Tactic.Split_Intersect) 
wneuper@59571
    98
| Tactic.End_Intersect		    => ("End_Intersect", Tactic.End_Intersect) 
wneuper@59571
    99
| Tactic.Check_elementwise cterm'  => ("Check_elementwise", Tactic.Check_elementwise cterm')
wneuper@59571
   100
| Tactic.Or_to_List                => ("Or_to_List", Tactic.Or_to_List) 
wneuper@59571
   101
| Tactic.Collect_Trues	            => ("Collect_Results", Tactic.Collect_Trues) 
neuper@37906
   102
			    
wneuper@59571
   103
| Tactic.Empty_Tac               => ("Empty_Tac", Tactic.Empty_Tac)
wneuper@59571
   104
| Tactic.Tac string              => ("Tac", Tactic.Tac string)
wneuper@59571
   105
| Tactic.End_Proof'                => ("End_Proof'", Tactic.End_Proof')
wneuper@59286
   106
| _ => error "mk_tac'_: uncovered case"; 
neuper@37906
   107
wneuper@59279
   108
type squ = ctree; (* TODO: safe etc. *)
neuper@37906
   109
wneuper@59286
   110
val specsteps = ["Init_Proof", "Refine_Tacitly", "Refine_Problem", "Model_Problem",
wneuper@59286
   111
  "Add_Given", "Del_Given", "Add_Find", "Del_Find", "Add_Relation", "Del_Relation",
wneuper@59286
   112
  "Specify_Theory", "Specify_Problem", "Specify_Method"];
neuper@37906
   113
wneuper@59578
   114
(*FIXME.WN050821 compare solve    ...   begin_end_prog:
wneuper@59578
   115
        WN190811: solve ("Apply_Method",.. used by root-pbl + by locate_input_tactic !!!
wneuper@59578
   116
*)
walther@59749
   117
(*-------------------------------------------------------------------
walther@59676
   118
fun solve ("Apply_Method", m as Tactic.Apply_Method' (mI, _, _, ctxt)) (pt, (pos as (p, _))) =
walther@59603
   119
    let
wneuper@59286
   120
      val itms = case get_obj I pt p of
wneuper@59286
   121
        PblObj {meth=itms, ...} => itms
wneuper@59286
   122
      | _ => error "solve Apply_Method: uncovered case get_obj"
wneuper@59286
   123
      val thy' = get_obj g_domID pt p;
wneuper@59405
   124
      val thy = Celem.assoc_thy thy';
walther@59677
   125
      val srls = Lucin.get_simplifier (pt, pos)
walther@59677
   126
      val (is, env, ctxt, sc) = case Lucin.init_pstate srls ctxt itms mI of
walther@59685
   127
        (is as Istate.Pstate {env, ...}, ctxt, sc) =>  (is, env, ctxt, sc)
wneuper@59583
   128
      | _ => error "solve Apply_Method: uncovered case init_pstate"
wneuper@59286
   129
      val ini = Lucin.init_form thy sc env;
wneuper@59286
   130
      val p = lev_dn p;
wneuper@59286
   131
    in 
wneuper@59286
   132
      case ini of
wneuper@59286
   133
	      SOME t =>
wneuper@59286
   134
          let val (pos,c,_, pt) = 
wneuper@59571
   135
		        Generate.generate1 thy (Tactic.Apply_Method' (mI, SOME t, is, ctxt))
wneuper@59286
   136
		        (is, ctxt) (lev_on p, Frm)(*implicit Take*) pt;
wneuper@59571
   137
	        in ("ok",([(Tactic.Apply_Method mI, Tactic.Apply_Method' (mI, SOME t, is, ctxt), 
wneuper@59286
   138
		        ((lev_on p, Frm), (is, ctxt)))], c, (pt, pos))) 
wneuper@59286
   139
	        end	      
walther@59696
   140
	    | NONE =>
wneuper@59286
   141
	        let
walther@59745
   142
            val (m', is', ctxt') = 
walther@59745
   143
              case  LucinNEW.find_next_tactic sc (pt, (p, Res)) is ctxt of
walther@59745
   144
                    LucinNEW.Next_Step (ist, ctxt, tac) => (tac, ist, ctxt)
walther@59745
   145
              | _ => raise ERROR ("solve Apply_Method " ^ strs2str' mI)
wneuper@59286
   146
	        in 
wneuper@59569
   147
            case LucinNEW.locate_input_tactic sc (pt, (p, Res)) is' ctxt' m' of
walther@59655
   148
              LucinNEW.Safe_Step (istate, ctxt, tac) =>
walther@59686
   149
                let
walther@59686
   150
                  val (p'', _, _,pt') =
walther@59686
   151
                    Generate.generate1 (Celem.assoc_thy "Isac_Knowledge") tac
walther@59686
   152
                    (* DEL.^^^^^ ContextC.insert_assumptions asms' ctxt 2nd, fst in Lucin.associate *)
walther@59686
   153
                      (istate, ctxt) (lev_on p, Pbl) pt;
walther@59686
   154
                in
walther@59704
   155
         	  	    ("ok", ([(Tactic.input_from_T tac, tac, (p'', (istate, ctxt)))],
walther@59686
   156
                    [(*ctree NOT cut*)], (pt', p'')))
walther@59686
   157
                end
walther@59676
   158
		        | _ => (* NotLocatable, but applicable_in from the beginning *)
walther@59675
   159
		            let
walther@59675
   160
		              val (p, ps, _, pt) =
walther@59675
   161
		                Generate.generate_hard (Celem.assoc_thy "Isac_Knowledge") m (p, Frm) pt;
wneuper@59286
   162
		            in 
walther@59704
   163
		              ("not-found-in-script",([(Tactic.input_from_T m, m, (pos, (is, ctxt)))], ps, (pt, p))) 
wneuper@59286
   164
		            end
wneuper@59286
   165
	        end
wneuper@59286
   166
    end
wneuper@59571
   167
  | solve ("Free_Solve", Tactic.Free_Solve')  (pt, po as (p, _)) =
wneuper@59286
   168
    let
wneuper@59286
   169
      val p' = lev_dn_ (p, Res);
wneuper@59405
   170
      val pt = update_metID pt (par_pblobj pt p) Celem.e_metID;
wneuper@59286
   171
    in
wneuper@59581
   172
      ("ok", ([(Tactic.Empty_Tac, Tactic.Empty_Tac_, (po, (Istate.Uistate, ContextC.e_ctxt)))], [], (pt,p')))
wneuper@59286
   173
    end
walther@59746
   174
  | solve ("Check_Postcond", Tactic.Check_Postcond' (pI, _)) (ptp as (pt, (p, p_))) =
wneuper@59286
   175
    let
wneuper@59286
   176
      val pp = par_pblobj pt p
walther@59746
   177
      val {scr, ...} = Specify.get_met (get_obj g_metID pt pp);
walther@59723
   178
      val (pst, ctxt) = case get_loc pt (p, p_) of (Istate.Pstate pst, ctxt) => (pst, ctxt)
wneuper@59286
   179
      | _ => error "solve Check_Postcond: uncovered case get_loc"
walther@59746
   180
      val prog_res =
walther@59746
   181
         case LucinNEW.find_next_tactic scr (pt, (p, p_)) (Istate.Pstate pst) ctxt of
walther@59746
   182
           LucinNEW.Next_Step (_, _, Tactic.Check_elementwise' (_, _, (prog_res, _))) => prog_res
walther@59746
   183
         | LucinNEW.End_Program (_, Tactic.Check_Postcond' (_, (prog_res, _))) => prog_res
walther@59745
   184
         | _ => raise ERROR ("solve Check_Postcond " ^ strs2str' pI)
walther@59746
   185
    in (*for Applicable.applicable_in not yet available -----------vvvvv*)
walther@59746
   186
      ("ok", LucinNEW.begin_end_prog (Tactic.Check_Postcond' (pI, (prog_res, [(*fill.later*)])))
walther@59746
   187
        (Istate.e_istate, ContextC.e_ctxt) ptp)
walther@59746
   188
   end
wneuper@59571
   189
  | solve (_, Tactic.End_Proof'') (pt, (p, p_)) =
wneuper@59581
   190
    ("end-proof", ([(Tactic.Empty_Tac, Tactic.Empty_Tac_, (([], Res), (Istate.Uistate, ContextC.e_ctxt)))], [], (pt, (p, p_))))
wneuper@59571
   191
  | solve (_, Tactic.End_Detail' t) (pt, (p, p_)) = (* could be done by generate1 ?!? *)
wneuper@59286
   192
    let (*Rewrite_Set* done at Detail_Set*: this result is already in ctree*)
wneuper@59286
   193
      val pr = (lev_up p, Res)
wneuper@59286
   194
    in
wneuper@59571
   195
      ("ok", ([(Tactic.End_Detail, Tactic.End_Detail' t , ((p, p_), get_loc pt (p, p_)))], [], (pt, pr)))
wneuper@59286
   196
    end
walther@59676
   197
    (* ======== general case as fall htrough ======== *)
wneuper@59286
   198
  | solve (_, m) (pt, po as (p, p_)) =
wneuper@59405
   199
    if Celem.e_metID = get_obj g_metID pt (par_pblobj pt p) (*29.8.02: could be detail, too !!*)
wneuper@59286
   200
    then
wneuper@59286
   201
      let
wneuper@59286
   202
        val ctxt = get_ctxt pt po
wneuper@59405
   203
        val ((p,p_),ps,_,pt) = Generate.generate1 (Celem.assoc_thy (get_obj g_domID pt (par_pblobj pt p))) 
wneuper@59572
   204
		      m (Istate.e_istate, ctxt) (p, p_) pt;
wneuper@59286
   205
	    in ("no-method-specified", (*Free_Solve*)
wneuper@59572
   206
	      ([(Tactic.Empty_Tac, Tactic.Empty_Tac_, ((p, p_), (Istate.Uistate, ctxt)))], ps, (pt, (p, p_))))
wneuper@59286
   207
      end
wneuper@59286
   208
    else
wneuper@59286
   209
	    let 
wneuper@59286
   210
	      val thy' = get_obj g_domID pt (par_pblobj pt p);
wneuper@59569
   211
	      val (_, is, sc) = Lucin.from_pblobj_or_detail' thy' (p,p_) pt;
wneuper@59286
   212
	    in
walther@59719
   213
        case LucinNEW.locate_input_tactic sc (pt, po) (fst is) (snd is) m of
walther@59686
   214
          LucinNEW.Safe_Step (istate, ctxt, tac) =>
walther@59686
   215
            let
walther@59686
   216
               val p' = 
walther@59686
   217
                 case p_ of Frm => p 
walther@59686
   218
                 | Res => lev_on p
walther@59686
   219
                 | _ => error ("solve: call by " ^ pos'2str (p, p_));
walther@59686
   220
               val (p'', _, _,pt') =
walther@59686
   221
                 Generate.generate1 (Celem.assoc_thy "Isac_Knowledge") tac
walther@59686
   222
                 (* DEL.^^^^^ ContextC.insert_assumptions asms' ctxt 2nd, fst in Lucin.associate *)
walther@59686
   223
                   (istate, ctxt) (p', p_) pt;
walther@59686
   224
            in
walther@59704
   225
       		    ("ok", ([(Tactic.input_from_T tac, tac, (p'', (istate, ctxt)))],
walther@59686
   226
                [(*ctree NOT cut*)], (pt', p'')))
walther@59686
   227
            end
walther@59655
   228
	      | LucinNEW.Unsafe_Step (istate, ctxt, tac) =>
walther@59686
   229
            let
walther@59686
   230
               val p' = 
walther@59686
   231
                 case p_ of Frm => p 
walther@59686
   232
                 | Res => lev_on p
walther@59686
   233
                 | _ => error ("solve: call by " ^ pos'2str (p, p_));
walther@59686
   234
               val (p'', _, _,pt') =
walther@59686
   235
                 Generate.generate1 (Celem.assoc_thy "Isac_Knowledge") tac
walther@59686
   236
                 (* DEL.^^^^^ ContextC.insert_assumptions asms' ctxt 2nd, fst in Lucin.associate *)
walther@59686
   237
                   (istate, ctxt) (p', p_) pt;
walther@59686
   238
            in
walther@59704
   239
       		    ("ok", ([(Tactic.input_from_T tac, tac, (p'', (istate, ctxt)))],
walther@59686
   240
                [(*ctree NOT cut*)], (pt', p'')))
walther@59686
   241
            end
wneuper@59286
   242
	      | _ => (* NotLocatable *)
wneuper@59286
   243
	        let 
wneuper@59592
   244
	          val (p,ps, _, pt) = Generate.generate_hard (Celem.assoc_thy "Isac_Knowledge") m (p, p_) pt;
wneuper@59286
   245
	        in
walther@59704
   246
	          ("not-found-in-script", ([(Tactic.input_from_T m, m, (po, is))], ps, (pt, p)))
wneuper@59286
   247
          end
wneuper@59286
   248
	    end;
walther@59749
   249
------------------------------------------------*)
neuper@37906
   250
walther@59648
   251
(* tell how may steps of a calculation should be done by "fun autocalc"
wneuper@59286
   252
   FIXXXME040624: does NOT match interfaces/ITOCalc.java
wneuper@59286
   253
   TODO.WN0512 redesign togehter with autocalc ?                     *)
neuper@37906
   254
datatype auto = 
walther@59747
   255
  Steps of int      (*1 do #int steps (may stop in model/specify)
wneuper@59260
   256
                       IS VERY INEFFICIENT IN MODEL/SPECIY                    *)
neuper@37906
   257
| CompleteModel    (*2 complete modeling
wneuper@59260
   258
                       if model complete, finish specifying                   *)
wneuper@59260
   259
| CompleteCalcHead (*3 complete model/specify in one go                       *)
neuper@37906
   260
| CompleteToSubpbl (*4 stop at the next begin of a subproblem,
wneuper@59260
   261
                       if none, complete the actual (sub)problem              *)
wneuper@59260
   262
| CompleteSubpbl   (*5 complete the actual (sub)problem (incl.ev.subproblems) *)
wneuper@59260
   263
| CompleteCalc;    (*6 complete the calculation as a whole                    *)
neuper@42092
   264
walther@59747
   265
fun autoord (Steps _ ) = 1
neuper@37906
   266
  | autoord CompleteModel = 2
neuper@37906
   267
  | autoord CompleteCalcHead = 3
neuper@37906
   268
  | autoord CompleteToSubpbl = 4
neuper@37906
   269
  | autoord CompleteSubpbl = 5
neuper@37906
   270
  | autoord CompleteCalc = 6;
neuper@37906
   271
wneuper@59286
   272
fun complete_solve auto c (ptp as (_, p as (_, p_))) =
wneuper@59286
   273
    if p = ([], Res)
wneuper@59286
   274
    then ("end-of-calculation", [], ptp)
neuper@41997
   275
    else
wneuper@59286
   276
      if member op = [Pbl,Met] p_
wneuper@59286
   277
      then
wneuper@59286
   278
        let
wneuper@59286
   279
          val ptp = Chead.all_modspec ptp
wneuper@59286
   280
	        val (_, c', ptp) = all_solve auto c ptp
wneuper@59286
   281
	      in complete_solve auto (c @ c') ptp end
wneuper@59286
   282
      else
wneuper@59562
   283
        case LucinNEW.do_solve_step ptp of
wneuper@59571
   284
	        ((Tactic.Subproblem _, _, _) :: _, c', ptp') =>
wneuper@59286
   285
	          if autoord auto < 5
wneuper@59286
   286
            then ("ok", c @ c', ptp)
wneuper@59286
   287
	          else
wneuper@59286
   288
              let
wneuper@59286
   289
                val ptp = Chead.all_modspec ptp'
walther@59734
   290
	              val (_, c'', ptp) = all_solve auto (c @ c') ptp
walther@59734
   291
	            in complete_solve auto (c @ c'@ c'') ptp end
wneuper@59571
   292
	      | ((Tactic.Check_Postcond _, _, _) :: _, c', ptp' as (_, p')) =>
wneuper@59286
   293
	        if autoord auto < 6 orelse p' = ([], Res)
neuper@41997
   294
          then ("ok", c @ c', ptp')
neuper@41997
   295
	        else complete_solve auto (c @ c') ptp'
wneuper@59571
   296
	      | ((Tactic.End_Detail, _, _) :: _, c', ptp') => 
neuper@41997
   297
	        if autoord auto < 6
neuper@41997
   298
          then ("ok", c @ c', ptp')
neuper@41997
   299
	        else complete_solve auto (c @ c') ptp'
wneuper@59286
   300
	      | (_, c', ptp') => complete_solve auto (c @ c') ptp'
wneuper@59279
   301
and all_solve auto c (ptp as (pt, pos as (p,_)): ctree * pos') = 
wneuper@59286
   302
    let
wneuper@59286
   303
      val (_, _, mI) = get_obj g_spec pt p
wneuper@59286
   304
      val ctxt = get_ctxt pt pos
wneuper@59572
   305
      val (_, c', ptp) = LucinNEW.begin_end_prog (Tactic.Apply_Method' (mI, NONE, Istate.e_istate, ctxt)) (Istate.e_istate, ctxt) ptp
wneuper@59286
   306
    in
wneuper@59286
   307
      complete_solve auto (c @ c') ptp
wneuper@59286
   308
    end;
neuper@41997
   309
neuper@41997
   310
(* aux.fun for detailrls with Rrls, reverse rewriting *)
wneuper@59286
   311
fun rul_terms_2nds _ nds _ [] = nds
s1210629013@52153
   312
  | rul_terms_2nds thy nds t ((rule, res as (t', _)) :: rts) =
wneuper@59581
   313
    (append_atomic [] (Istate.e_istate, ContextC.e_ctxt) t (Tactic.rule2tac thy [] rule) res Complete EmptyPtree) ::
wneuper@59286
   314
      (rul_terms_2nds thy nds t' rts);
neuper@37906
   315
neuper@41997
   316
(* detail steps done internally by Rewrite_Set* into ctree by use of a script *)
wneuper@59286
   317
fun detailrls pt (pos as (p, _)) = 
neuper@41997
   318
  let
neuper@41997
   319
    val t = get_obj g_form pt p
neuper@41997
   320
	  val tac = get_obj g_tac pt p
wneuper@59571
   321
	  val rls = (assoc_rls o Tactic.rls_of) tac
neuper@41997
   322
    val ctxt = get_ctxt pt pos
neuper@41997
   323
  in
neuper@41997
   324
    case rls of
wneuper@59416
   325
	    Rule.Rrls {scr = Rule.Rfuns {init_state, ...}, ...} => 
wneuper@59286
   326
	    let
wneuper@59286
   327
        val (_, _, _, rul_terms) = init_state t
wneuper@59286
   328
	      val newnds = rul_terms_2nds (Proof_Context.theory_of ctxt) [] t rul_terms
wneuper@59286
   329
	      val pt''' = ins_chn newnds pt p 
wneuper@59286
   330
	    in ("detailrls", pt''', (p @ [length newnds], Res)) end
neuper@41997
   331
	  | _ =>
wneuper@59286
   332
	    let
wneuper@59286
   333
        val is = Generate.init_istate tac t
wneuper@59583
   334
	      (*TODO.WN060602 Pstate (["(t_, Problem (Isac,[equation,univar]))"]
wneuper@59286
   335
			    is wrong for simpl, but working ?!? *)
wneuper@59571
   336
	      val tac_ = Tactic.Apply_Method' (Celem.e_metID(*WN0402: see generate1 !?!*), SOME t, is, ctxt)
wneuper@59286
   337
	      val pos' = ((lev_on o lev_dn) p, Frm)
wneuper@59592
   338
	      val thy = Celem.assoc_thy "Isac_Knowledge"
wneuper@59286
   339
	      val (_, _, _, pt') = Generate.generate1 thy tac_ (is, ctxt) pos' pt (* implicit Take *)
wneuper@59286
   340
	      val (_,_, (pt'', _)) = complete_solve CompleteSubpbl [] (pt', pos')
wneuper@59286
   341
	      val newnds = children (get_nd pt'' p)
wneuper@59286
   342
	      val pt''' = ins_chn newnds pt p (*complete_solve cuts branches after*)
wneuper@59411
   343
	    in ("detailrls", pt''', (p @ [length newnds], Res)) end
neuper@41997
   344
  end;
neuper@37906
   345
wneuper@59286
   346
fun get_form (mI, m) (p,p_) pt = 
wneuper@59286
   347
  case Applicable.applicable_in (p, p_) pt m of
walther@59735
   348
    Applicable.Notappl e => Generate.Error' e
walther@59735
   349
  | Applicable.Appl m => 
walther@59749
   350
      if Tactic.for_specify' m
wneuper@59286
   351
      then
wneuper@59286
   352
        let val (_, _, f, _, _, _) = Chead.specify m (p, p_) [] pt
wneuper@59286
   353
        in f end
wneuper@59286
   354
      else Generate.EmptyMout;
wneuper@59562
   355
wneuper@59562
   356
fun inform (next_cs as (_, _, (pt, pos as (p, _))): Chead.calcstate') istr =
walther@59722
   357
  case TermC.parse (Celem.assoc_thy "Isac_Knowledge") istr of
walther@59722
   358
    NONE => ("syntax error in '" ^ istr ^ "'", Chead.e_calcstate' (*TODO: previous cs'*))
walther@59722
   359
  | SOME f_in =>
walther@59722
   360
    let
walther@59722
   361
  	  val f_in = Thm.term_of f_in
walther@59722
   362
      val pos_pred = lev_back(*'*) pos
walther@59722
   363
  	  val f_pred = Ctree.get_curr_formula (pt, pos_pred);
walther@59722
   364
  	  val f_succ = Ctree.get_curr_formula (pt, pos);
walther@59722
   365
    in
walther@59722
   366
      if f_succ = f_in
walther@59722
   367
      then ("same-formula", next_cs) (* ctree not cut with replaceFormula *)
walther@59722
   368
      else
walther@59722
   369
        case Inform.cas_input f_in of
walther@59722
   370
          SOME (pt, _) => ("ok",([], [], (pt, (p, Pos.Met))))
walther@59722
   371
		    | NONE => (*/-------------------------------------- NEW fun locate_input_formula------*)
walther@59722
   372
          let
walther@59722
   373
            val (_, _, metID) = get_obj g_spec pt (par_pblobj pt p)
walther@59722
   374
            val {scr = prog, ...} = Specify.get_met metID
walther@59722
   375
            val istate = get_istate pt pos
walther@59722
   376
            val ctxt = get_ctxt pt pos
walther@59722
   377
          in
walther@59722
   378
            case LucinNEW.locate_input_formula prog (pt, pos) istate ctxt f_in of
walther@59747
   379
              LucinNEW.Found_Step (cstate, _(*istate*), _(*ctxt*)) =>
walther@59722
   380
              ("ok" , ([], [], cstate (* already contains istate, ctxt *)))
walther@59722
   381
            | LucinNEW.Not_Derivable calcstate' =>
walther@59722
   382
              let
walther@59722
   383
          		  val pp = Ctree.par_pblobj pt p
walther@59722
   384
          		  val (errpats, nrls, prog) = case Specify.get_met (Ctree.get_obj Ctree.g_metID pt pp) of
walther@59722
   385
            		    {errpats, nrls, scr = Rule.Prog prog, ...} => (errpats, nrls, prog)
walther@59722
   386
            		  | _ => error "inform: uncovered case of get_met"
walther@59722
   387
          		  val {env, ...} = Ctree.get_istate pt pos |> Istate.the_pstate
walther@59722
   388
          		in
walther@59722
   389
          		  case Inform.check_error_patterns (f_pred, f_in) (prog, env) (errpats, nrls) of
walther@59722
   390
          		    SOME errpatID => ("error pattern #" ^ errpatID ^ "#", calcstate')
walther@59722
   391
          		  | NONE => ("no derivation found", calcstate')
walther@59722
   392
              end
walther@59722
   393
          end
walther@59722
   394
    end
wneuper@59562
   395
wneuper@59273
   396
end