src/Tools/isac/Interpret/solve-step.sml
author Walther Neuper <walther.neuper@jku.at>
Sat, 02 May 2020 17:39:04 +0200
changeset 59931 cc5b51681c4b
parent 59929 d2be99d0bf1e
child 59932 87336f3b021f
permissions -rw-r--r--
prep. new Solve_Step.add
walther@59920
     1
(* Title:  Specify/solve-step.sml
walther@59920
     2
   Author: Walther Neuper
walther@59920
     3
   (c) due to copyright terms
walther@59920
     4
walther@59920
     5
Code for the solve-phase in analogy to structure Specify_Step for the specify-phase.
walther@59920
     6
*)
walther@59920
     7
walther@59920
     8
signature SOLVE_STEP =
walther@59920
     9
sig
walther@59921
    10
  val check: Tactic.input -> Calc.T -> Applicable.T
walther@59931
    11
  val add: Tactic.T -> Istate_Def.T * Proof.context -> Calc.T -> Generate.test_out
walther@59921
    12
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
walther@59921
    13
  (*NONE*)                                                     
walther@59921
    14
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
walther@59921
    15
  (*NONE*)                                                     
walther@59921
    16
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
walther@59920
    17
end
walther@59920
    18
walther@59920
    19
(**)
walther@59920
    20
structure Solve_Step(** ): SOLVE_STEP( **) =
walther@59920
    21
struct
walther@59920
    22
(**)
walther@59920
    23
walther@59922
    24
(*
walther@59922
    25
  check tactics (input by the user, mostly) for applicability
walther@59922
    26
  and determine as much of the result of the tactic as possible initially.
walther@59922
    27
*)
walther@59928
    28
fun check (Tactic.Calculate op_) (cs as (pt, (p, _))) =
walther@59923
    29
      let 
walther@59928
    30
        val (msg, thy', isa_fn) = ApplicableOLD.from_pblobj_or_detail_calc op_ p pt;
walther@59928
    31
        val f = Calc.current_formula cs;
walther@59923
    32
      in
walther@59923
    33
        if msg = "OK"
walther@59923
    34
        then
walther@59923
    35
    	    case Rewrite.calculate_ (ThyC.get_theory thy') isa_fn f of
walther@59923
    36
    	      SOME (f', (id, thm))
walther@59923
    37
    	        => Applicable.Yes (Tactic.Calculate' (thy', op_, f, (f', (id, thm))))
walther@59929
    38
    	    | NONE => Applicable.No ("'calculate " ^ op_ ^ "' not applicable") 
walther@59923
    39
        else Applicable.No msg                                              
walther@59923
    40
      end
walther@59928
    41
  | check (Tactic.Check_Postcond pI) (_, _) = (*TODO: only applicable, if evaluating to True*)
walther@59928
    42
      Applicable.Yes (Tactic.Check_Postcond' (pI, TermC.empty))
walther@59928
    43
  | check (Tactic.Check_elementwise pred) cs =
walther@59923
    44
      let 
walther@59928
    45
        val f = Calc.current_formula cs;
walther@59923
    46
      in
walther@59928
    47
        Applicable.Yes (Tactic.Check_elementwise' (f, pred, (f, [])))
walther@59923
    48
      end
walther@59923
    49
  | check Tactic.Empty_Tac _ = Applicable.No "Empty_Tac is not applicable"
walther@59929
    50
  | check (Tactic.Free_Solve) _ = Applicable.Yes (Tactic.Free_Solve')
walther@59929
    51
  | check Tactic.Or_to_List cs =
walther@59928
    52
       let 
walther@59929
    53
        val f = Calc.current_formula cs;
walther@59929
    54
        val ls = Prog_Expr.or2list f;
walther@59929
    55
      in
walther@59929
    56
        Applicable.Yes (Tactic.Or_to_List' (f, ls))
walther@59923
    57
      end
walther@59929
    58
  | check (Tactic.Rewrite thm) (cs as (pt, (p, _))) = 
walther@59923
    59
      let
walther@59929
    60
        val (msg, thy', ro, rls', _) = ApplicableOLD.from_pblobj_or_detail_thm thm p pt;
walther@59923
    61
        val thy = ThyC.get_theory thy';
walther@59928
    62
        val f = Calc.current_formula cs;
walther@59923
    63
      in
walther@59923
    64
        if msg = "OK" 
walther@59923
    65
        then
walther@59929
    66
          case Rewrite.rewrite_ thy (Rewrite_Ord.assoc_rew_ord ro) rls' false (snd thm) f of
walther@59929
    67
            SOME (f',asm) => Applicable.Yes (Tactic.Rewrite' (thy', ro, rls', false, thm, f, (f', asm)))
walther@59929
    68
          | NONE => Applicable.No ((thm |> fst |> quote) ^ " not applicable") 
walther@59923
    69
        else Applicable.No msg
walther@59923
    70
      end
walther@59929
    71
  | check (Tactic.Rewrite_Inst (subs, thm)) (cs as (pt, (p, _))) = 
walther@59921
    72
      let 
walther@59921
    73
        val pp = Ctree.par_pblobj pt p;
walther@59921
    74
        val thy' = Ctree.get_obj Ctree.g_domID pt pp;
walther@59921
    75
        val thy = ThyC.get_theory thy';
walther@59921
    76
        val {rew_ord' = ro', erls = erls, ...} = Specify.get_met (Ctree.get_obj Ctree.g_metID pt pp);
walther@59928
    77
        val f = Calc.current_formula cs;
walther@59929
    78
        val subst = Subst.T_from_input thy subs; (*TODO: input requires parse _: _ -> _ option*)
walther@59921
    79
      in 
walther@59929
    80
        case Rewrite.rewrite_inst_ thy (Rewrite_Ord.assoc_rew_ord ro') erls false subst (snd thm) f of
walther@59929
    81
          SOME (f', asm) =>
walther@59929
    82
            Applicable.Yes (Tactic.Rewrite_Inst' (thy', ro', erls, false, subst, thm, f, (f', asm)))
walther@59929
    83
        | NONE => Applicable.No (fst thm ^ " not applicable")
walther@59921
    84
      end
walther@59928
    85
  | check (Tactic.Rewrite_Set rls) (cs as (pt, (p, _))) =
walther@59921
    86
      let 
walther@59923
    87
        val pp = Ctree.par_pblobj pt p; 
walther@59921
    88
        val thy' = Ctree.get_obj Ctree.g_domID pt pp;
walther@59928
    89
        val f = Calc.current_formula cs;
walther@59923
    90
      in
walther@59923
    91
        case Rewrite.rewrite_set_ (ThyC.get_theory thy') false (assoc_rls rls) f of
walther@59921
    92
          SOME (f', asm)
walther@59923
    93
            => Applicable.Yes (Tactic.Rewrite_Set' (thy', false, assoc_rls rls, f, (f', asm)))
walther@59923
    94
          | NONE => Applicable.No (rls ^ " not applicable")
walther@59921
    95
      end
walther@59929
    96
  | check (Tactic.Rewrite_Set_Inst (subs, rls)) (cs as (pt, (p, _))) =
walther@59921
    97
      let 
walther@59921
    98
        val pp = Ctree.par_pblobj pt p;
walther@59921
    99
        val thy' = Ctree.get_obj Ctree.g_domID pt pp;
walther@59921
   100
        val thy = ThyC.get_theory thy';
walther@59928
   101
        val f = Calc.current_formula cs;
walther@59929
   102
    	  val subst = Subst.T_from_input thy subs; (*TODO: input requires parse _: _ -> _ option*)
walther@59921
   103
      in 
walther@59928
   104
        case Rewrite.rewrite_set_inst_ thy false subst (assoc_rls rls) f of
walther@59928
   105
          SOME (f', asm)
walther@59921
   106
            => Applicable.Yes (Tactic.Rewrite_Set_Inst' (thy', false, subst, assoc_rls rls, f, (f', asm)))
walther@59921
   107
        | NONE => Applicable.No (rls ^ " not applicable")
walther@59921
   108
      end
walther@59928
   109
  | check (Tactic.Subproblem (domID, pblID)) (_, _) = 
walther@59928
   110
      Applicable.Yes (Tactic.Subproblem' ((domID, pblID, Method.id_empty), [], 
walther@59928
   111
			  TermC.empty, [], ContextC.empty, Auto_Prog.subpbl domID pblID))
walther@59929
   112
   | check (Tactic.Substitute sube) (cs as (pt, (p, _))) =
walther@59928
   113
      let
walther@59928
   114
        val pp = Ctree.par_pblobj pt p
walther@59928
   115
        val thy = ThyC.get_theory (Ctree.get_obj Ctree.g_domID pt pp)
walther@59928
   116
        val f = Calc.current_formula cs;
walther@59928
   117
		    val {rew_ord', erls, ...} = Specify.get_met (Ctree.get_obj Ctree.g_metID pt pp)
walther@59929
   118
		    val subte = Subst.input_to_terms sube (*TODO: input requires parse _: _ -> _ option*)
walther@59928
   119
		    val subst = Subst.T_from_string_eqs thy sube
walther@59928
   120
		    val ro = Rewrite_Ord.assoc_rew_ord rew_ord'
walther@59928
   121
		  in
walther@59928
   122
		    if foldl and_ (true, map TermC.contains_Var subte)
walther@59928
   123
		    then (*1*)
walther@59928
   124
		      let val f' = subst_atomic subst f
walther@59928
   125
		      in if f = f'
walther@59928
   126
		        then Applicable.No (Subst.string_eqs_to_string sube ^ " not applicable")
walther@59928
   127
		        else Applicable.Yes (Tactic.Substitute' (ro, erls, subte, f, f'))
walther@59928
   128
		      end
walther@59928
   129
		    else (*2*)
walther@59928
   130
		      case Rewrite.rewrite_terms_ thy ro erls subte f of
walther@59928
   131
		        SOME (f', _) =>  Applicable.Yes (Tactic.Substitute' (ro, erls, subte, f, f'))
walther@59928
   132
		      | NONE => Applicable.No (Subst.string_eqs_to_string sube ^ " not applicable")
walther@59928
   133
		  end
walther@59928
   134
  | check (Tactic.Tac id) (cs as (pt, (p, _))) =
walther@59929
   135
      let 
walther@59929
   136
        val pp = Ctree.par_pblobj pt p; 
walther@59929
   137
        val thy' = Ctree.get_obj Ctree.g_domID pt pp;
walther@59929
   138
        val thy = ThyC.get_theory thy';
walther@59929
   139
        val f = Calc.current_formula cs;
walther@59929
   140
      in case id of
walther@59929
   141
        "subproblem_equation_dummy" =>
walther@59929
   142
    	  if TermC.is_expliceq f
walther@59929
   143
    	  then Applicable.Yes (Tactic.Tac_ (thy, UnparseC.term f, id, "subproblem_equation_dummy (" ^ UnparseC.term f ^ ")"))
walther@59929
   144
    	  else Applicable.No "applicable only to equations made explicit"
walther@59929
   145
      | "solve_equation_dummy" =>
walther@59929
   146
    	  let val (id', f') = ApplicableOLD.split_dummy (UnparseC.term f);
walther@59929
   147
    	  in
walther@59929
   148
    	    if id' <> "subproblem_equation_dummy"
walther@59929
   149
    	    then Applicable.No "no subproblem"
walther@59929
   150
    	    else if (ThyC.to_ctxt thy, f') |-> TermC.parseNEW |> the |> TermC.is_expliceq
walther@59929
   151
    		    then Applicable.Yes (Tactic.Tac_ (thy, UnparseC.term f, id, "[" ^ f' ^ "]"))
walther@59929
   152
    		    else error ("Solve_Step.check: f= " ^ f')
walther@59929
   153
        end
walther@59929
   154
      | _ => Applicable.Yes (Tactic.Tac_ (thy, UnparseC.term f, id, UnparseC.term f))
walther@59921
   155
      end
walther@59923
   156
  | check (Tactic.Take str) _ = Applicable.Yes (Tactic.Take' (TermC.str2term str)) (* always applicable ?*)
walther@59929
   157
  | check (Tactic.Begin_Trans) cs =
walther@59929
   158
      Applicable.Yes (Tactic.Begin_Trans' (Calc.current_formula cs))
walther@59923
   159
  | check (Tactic.End_Trans) (pt, (p, p_)) = (*TODO: check parent branches*)
walther@59923
   160
    if p_ = Pos.Res 
walther@59923
   161
	  then Applicable.Yes (Tactic.End_Trans' (Ctree.get_obj Ctree.g_result pt p))
walther@59923
   162
    else Applicable.No "'End_Trans' is not applicable at the beginning of a transitive sequence"
walther@59921
   163
  | check Tactic.End_Proof' _ = Applicable.Yes Tactic.End_Proof''
walther@59921
   164
  | check m _ = raise ERROR ("Solve_Step.check called for " ^ Tactic.input_to_string m);
walther@59920
   165
walther@59931
   166
fun add (Tactic.Take' t) l (pt, (p, _)) = (* val (Take' t) = m; *)
walther@59931
   167
    let
walther@59931
   168
      val p =
walther@59931
   169
        let val (ps, p') = split_last p (* no connex to prev.ppobj *)
walther@59931
   170
	      in if p' = 0 then ps @ [1] else p end
walther@59931
   171
      val (pt, c) = Ctree.cappend_form pt p l t
walther@59931
   172
    in
walther@59931
   173
      ((p, Pos.Frm), c, Generate.FormKF (UnparseC.term t), pt)
walther@59931
   174
    end
walther@59931
   175
  | add (Tactic.Begin_Trans' t) l (pt, (p, Pos.Frm)) =
walther@59931
   176
    let
walther@59931
   177
      val (pt, c) = Ctree.cappend_form pt p l t
walther@59931
   178
      val pt = Ctree.update_branch pt p Ctree.TransitiveB (*040312*)
walther@59931
   179
      (* replace the old PrfOjb ~~~~~ *)
walther@59931
   180
      val p = (Pos.lev_on o Pos.lev_dn (* starts with [...,0] *)) p
walther@59931
   181
      val (pt, c') = Ctree.cappend_form pt p l t (*FIXME.0402 same istate ???*)
walther@59931
   182
    in
walther@59931
   183
      ((p, Pos.Frm), c @ c', Generate.FormKF (UnparseC.term t), pt)
walther@59931
   184
    end
walther@59931
   185
  | add (Tactic.Begin_Trans' t) l (pt, (p, Pos.Res)) = 
walther@59931
   186
    (*append after existing PrfObj    vvvvvvvvvvvvv*)
walther@59931
   187
    add (Tactic.Begin_Trans' t) l (pt, (Pos.lev_on p, Pos.Frm))
walther@59931
   188
  | add (Tactic.End_Trans' tasm) l (pt, (p, _)) =
walther@59931
   189
    let
walther@59931
   190
      val p' = Pos.lev_up p
walther@59931
   191
      val (pt, c) = Ctree.append_result pt p' l tasm Ctree.Complete
walther@59931
   192
    in
walther@59931
   193
      ((p', Pos.Res), c, Generate.FormKF "DUMMY" (*term2str t ..ERROR (t) has not been declared*), pt)
walther@59931
   194
    end
walther@59931
   195
  | add (Tactic.Rewrite_Inst' (_, _, _, _, subs', thm', f, (f', asm))) (is, ctxt) (pt, (p, _)) =
walther@59931
   196
    let
walther@59931
   197
      val (pt, c) = Ctree.cappend_atomic pt p (is, ctxt) f
walther@59931
   198
        (Tactic.Rewrite_Inst (Subst.T_to_input subs', thm')) (f',asm) Ctree.Complete;
walther@59931
   199
      val pt = Ctree.update_branch pt p Ctree.TransitiveB
walther@59931
   200
    in
walther@59931
   201
      ((p, Pos.Res), c, Generate.FormKF (UnparseC.term f'), pt)
walther@59931
   202
    end
walther@59931
   203
 | add (Tactic.Rewrite' (_, _, _, _, thm', f, (f', asm))) (is, ctxt) (pt, (p, _)) =
walther@59931
   204
   let
walther@59931
   205
     val (pt, c) = Ctree.cappend_atomic pt p (is, ctxt) f (Tactic.Rewrite thm') (f', asm) Ctree.Complete
walther@59931
   206
     val pt = Ctree.update_branch pt p Ctree.TransitiveB
walther@59931
   207
   in
walther@59931
   208
    ((p, Pos.Res), c, Generate.FormKF (UnparseC.term f'), pt)
walther@59931
   209
   end
walther@59931
   210
  | add (Tactic.Rewrite_Set_Inst' (_, _, subs', rls', f, (f', asm))) (is, ctxt) (pt, (p, _)) =
walther@59931
   211
    let
walther@59931
   212
      val (pt, c) = Ctree.cappend_atomic pt p (is, ctxt) f 
walther@59931
   213
        (Tactic.Rewrite_Set_Inst (Subst.T_to_input subs', Rule_Set.id rls')) (f', asm) Ctree.Complete
walther@59931
   214
      val pt = Ctree.update_branch pt p Ctree.TransitiveB
walther@59931
   215
    in
walther@59931
   216
      ((p, Pos.Res), c, Generate.FormKF (UnparseC.term f'), pt)
walther@59931
   217
    end
walther@59931
   218
  | add (Tactic.Rewrite_Set' (_, _, rls', f, (f', asm))) (is, ctxt) (pt, (p, _)) =
walther@59931
   219
    let
walther@59931
   220
      val (pt, c) = Ctree.cappend_atomic pt p (is, ctxt) f 
walther@59931
   221
        (Tactic.Rewrite_Set (Rule_Set.id rls')) (f', asm) Ctree.Complete
walther@59931
   222
      val pt = Ctree.update_branch pt p Ctree.TransitiveB
walther@59931
   223
    in
walther@59931
   224
      ((p, Pos.Res), c, Generate.FormKF (UnparseC.term f'), pt)
walther@59931
   225
    end
walther@59931
   226
  | add (Tactic.Check_Postcond' (_, scval)) l (pt, (p, _)) =
walther@59931
   227
      let
walther@59931
   228
        val (pt, c) = Ctree.append_result pt p l (scval, []) Ctree.Complete
walther@59931
   229
      in
walther@59931
   230
        ((p, Pos.Res), c, Generate.FormKF (UnparseC.term scval), pt)
walther@59931
   231
      end
walther@59931
   232
  | add (Tactic.Calculate' (_, op_, f, (f', _))) l (pt, (p, _)) =
walther@59931
   233
      let
walther@59931
   234
        val (pt,c) = Ctree.cappend_atomic pt p l f (Tactic.Calculate op_) (f', []) Ctree.Complete
walther@59931
   235
      in
walther@59931
   236
        ((p, Pos.Res), c, Generate.FormKF (UnparseC.term f'), pt)
walther@59931
   237
      end
walther@59931
   238
  | add (Tactic.Check_elementwise' (consts, pred, (f', asm))) l (pt, (p, _)) =
walther@59931
   239
      let
walther@59931
   240
        val (pt,c) = Ctree.cappend_atomic pt p l consts (Tactic.Check_elementwise pred) (f', asm) Ctree.Complete
walther@59931
   241
      in
walther@59931
   242
        ((p, Pos.Res), c, Generate.FormKF (UnparseC.term f'), pt)
walther@59931
   243
      end
walther@59931
   244
  | add (Tactic.Or_to_List' (ors, list)) l (pt, (p, _)) =
walther@59931
   245
      let
walther@59931
   246
        val (pt,c) = Ctree.cappend_atomic pt p l ors Tactic.Or_to_List (list, []) Ctree.Complete
walther@59931
   247
      in
walther@59931
   248
        ((p, Pos.Res), c, Generate.FormKF (UnparseC.term list), pt)
walther@59931
   249
      end
walther@59931
   250
  | add (Tactic.Substitute' (_, _, subte, t, t')) l (pt, (p, _)) =
walther@59931
   251
      let
walther@59931
   252
        val (pt,c) =
walther@59931
   253
          Ctree.cappend_atomic pt p l t (Tactic.Substitute (Subst.eqs_to_input subte)) (t',[]) Ctree.Complete
walther@59931
   254
        in ((p, Pos.Res), c, Generate.FormKF (UnparseC.term t'), pt) 
walther@59931
   255
        end
walther@59931
   256
  | add (Tactic.Tac_ (_, f, id, f')) l (pt, (p, _)) =
walther@59931
   257
      let
walther@59931
   258
        val (pt, c) = Ctree.cappend_atomic pt p l (TermC.str2term f) (Tactic.Tac id) (TermC.str2term f', []) Ctree.Complete
walther@59931
   259
      in
walther@59931
   260
        ((p,Pos.Res), c, Generate.FormKF f', pt)
walther@59931
   261
      end
walther@59931
   262
  | add (Tactic.Subproblem' ((domID, pblID, metID), oris, hdl, fmz_, ctxt_specify, f))
walther@59931
   263
      (l as (_, ctxt)) (pt, (p, _)) =
walther@59931
   264
    let
walther@59931
   265
	    val (pt, c) = Ctree.cappend_problem pt p l (fmz_, (domID, pblID, metID))
walther@59931
   266
	      (oris, (domID, pblID, metID), hdl, ctxt_specify)
walther@59931
   267
	    val f = Syntax.string_of_term (ThyC.to_ctxt (Proof_Context.theory_of ctxt)) f
walther@59931
   268
    in
walther@59931
   269
      ((p, Pos.Pbl), c, Generate.FormKF f, pt)
walther@59931
   270
    end
walther@59931
   271
  | add m' _ _ = raise ERROR ("add: not impl.for " ^ Tactic.string_of m')
walther@59931
   272
walther@59920
   273
(**)end(**);