src/Tools/isac/Interpret/solve-step.sml
author Walther Neuper <walther.neuper@jku.at>
Sat, 02 May 2020 16:34:42 +0200
changeset 59929 d2be99d0bf1e
parent 59928 7601a1fa20b9
child 59931 cc5b51681c4b
permissions -rw-r--r--
Solve_Check: postpone parsing input to _ option
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@59921
    11
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
walther@59921
    12
  (*NONE*)                                                     
walther@59921
    13
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
walther@59921
    14
  (*NONE*)                                                     
walther@59921
    15
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
walther@59920
    16
end
walther@59920
    17
walther@59920
    18
(**)
walther@59920
    19
structure Solve_Step(** ): SOLVE_STEP( **) =
walther@59920
    20
struct
walther@59920
    21
(**)
walther@59920
    22
walther@59922
    23
(*
walther@59922
    24
  check tactics (input by the user, mostly) for applicability
walther@59922
    25
  and determine as much of the result of the tactic as possible initially.
walther@59922
    26
*)
walther@59928
    27
fun check (Tactic.Calculate op_) (cs as (pt, (p, _))) =
walther@59923
    28
      let 
walther@59928
    29
        val (msg, thy', isa_fn) = ApplicableOLD.from_pblobj_or_detail_calc op_ p pt;
walther@59928
    30
        val f = Calc.current_formula cs;
walther@59923
    31
      in
walther@59923
    32
        if msg = "OK"
walther@59923
    33
        then
walther@59923
    34
    	    case Rewrite.calculate_ (ThyC.get_theory thy') isa_fn f of
walther@59923
    35
    	      SOME (f', (id, thm))
walther@59923
    36
    	        => Applicable.Yes (Tactic.Calculate' (thy', op_, f, (f', (id, thm))))
walther@59929
    37
    	    | NONE => Applicable.No ("'calculate " ^ op_ ^ "' not applicable") 
walther@59923
    38
        else Applicable.No msg                                              
walther@59923
    39
      end
walther@59928
    40
  | check (Tactic.Check_Postcond pI) (_, _) = (*TODO: only applicable, if evaluating to True*)
walther@59928
    41
      Applicable.Yes (Tactic.Check_Postcond' (pI, TermC.empty))
walther@59928
    42
  | check (Tactic.Check_elementwise pred) cs =
walther@59923
    43
      let 
walther@59928
    44
        val f = Calc.current_formula cs;
walther@59923
    45
      in
walther@59928
    46
        Applicable.Yes (Tactic.Check_elementwise' (f, pred, (f, [])))
walther@59923
    47
      end
walther@59923
    48
  | check Tactic.Empty_Tac _ = Applicable.No "Empty_Tac is not applicable"
walther@59929
    49
  | check (Tactic.Free_Solve) _ = Applicable.Yes (Tactic.Free_Solve')
walther@59929
    50
  | check Tactic.Or_to_List cs =
walther@59928
    51
       let 
walther@59929
    52
        val f = Calc.current_formula cs;
walther@59929
    53
        val ls = Prog_Expr.or2list f;
walther@59929
    54
      in
walther@59929
    55
        Applicable.Yes (Tactic.Or_to_List' (f, ls))
walther@59923
    56
      end
walther@59929
    57
  | check (Tactic.Rewrite thm) (cs as (pt, (p, _))) = 
walther@59923
    58
      let
walther@59929
    59
        val (msg, thy', ro, rls', _) = ApplicableOLD.from_pblobj_or_detail_thm thm p pt;
walther@59923
    60
        val thy = ThyC.get_theory thy';
walther@59928
    61
        val f = Calc.current_formula cs;
walther@59923
    62
      in
walther@59923
    63
        if msg = "OK" 
walther@59923
    64
        then
walther@59929
    65
          case Rewrite.rewrite_ thy (Rewrite_Ord.assoc_rew_ord ro) rls' false (snd thm) f of
walther@59929
    66
            SOME (f',asm) => Applicable.Yes (Tactic.Rewrite' (thy', ro, rls', false, thm, f, (f', asm)))
walther@59929
    67
          | NONE => Applicable.No ((thm |> fst |> quote) ^ " not applicable") 
walther@59923
    68
        else Applicable.No msg
walther@59923
    69
      end
walther@59929
    70
  | check (Tactic.Rewrite_Inst (subs, thm)) (cs as (pt, (p, _))) = 
walther@59921
    71
      let 
walther@59921
    72
        val pp = Ctree.par_pblobj pt p;
walther@59921
    73
        val thy' = Ctree.get_obj Ctree.g_domID pt pp;
walther@59921
    74
        val thy = ThyC.get_theory thy';
walther@59921
    75
        val {rew_ord' = ro', erls = erls, ...} = Specify.get_met (Ctree.get_obj Ctree.g_metID pt pp);
walther@59928
    76
        val f = Calc.current_formula cs;
walther@59929
    77
        val subst = Subst.T_from_input thy subs; (*TODO: input requires parse _: _ -> _ option*)
walther@59921
    78
      in 
walther@59929
    79
        case Rewrite.rewrite_inst_ thy (Rewrite_Ord.assoc_rew_ord ro') erls false subst (snd thm) f of
walther@59929
    80
          SOME (f', asm) =>
walther@59929
    81
            Applicable.Yes (Tactic.Rewrite_Inst' (thy', ro', erls, false, subst, thm, f, (f', asm)))
walther@59929
    82
        | NONE => Applicable.No (fst thm ^ " not applicable")
walther@59921
    83
      end
walther@59928
    84
  | check (Tactic.Rewrite_Set rls) (cs as (pt, (p, _))) =
walther@59921
    85
      let 
walther@59923
    86
        val pp = Ctree.par_pblobj pt p; 
walther@59921
    87
        val thy' = Ctree.get_obj Ctree.g_domID pt pp;
walther@59928
    88
        val f = Calc.current_formula cs;
walther@59923
    89
      in
walther@59923
    90
        case Rewrite.rewrite_set_ (ThyC.get_theory thy') false (assoc_rls rls) f of
walther@59921
    91
          SOME (f', asm)
walther@59923
    92
            => Applicable.Yes (Tactic.Rewrite_Set' (thy', false, assoc_rls rls, f, (f', asm)))
walther@59923
    93
          | NONE => Applicable.No (rls ^ " not applicable")
walther@59921
    94
      end
walther@59929
    95
  | check (Tactic.Rewrite_Set_Inst (subs, rls)) (cs as (pt, (p, _))) =
walther@59921
    96
      let 
walther@59921
    97
        val pp = Ctree.par_pblobj pt p;
walther@59921
    98
        val thy' = Ctree.get_obj Ctree.g_domID pt pp;
walther@59921
    99
        val thy = ThyC.get_theory thy';
walther@59928
   100
        val f = Calc.current_formula cs;
walther@59929
   101
    	  val subst = Subst.T_from_input thy subs; (*TODO: input requires parse _: _ -> _ option*)
walther@59921
   102
      in 
walther@59928
   103
        case Rewrite.rewrite_set_inst_ thy false subst (assoc_rls rls) f of
walther@59928
   104
          SOME (f', asm)
walther@59921
   105
            => Applicable.Yes (Tactic.Rewrite_Set_Inst' (thy', false, subst, assoc_rls rls, f, (f', asm)))
walther@59921
   106
        | NONE => Applicable.No (rls ^ " not applicable")
walther@59921
   107
      end
walther@59928
   108
  | check (Tactic.Subproblem (domID, pblID)) (_, _) = 
walther@59928
   109
      Applicable.Yes (Tactic.Subproblem' ((domID, pblID, Method.id_empty), [], 
walther@59928
   110
			  TermC.empty, [], ContextC.empty, Auto_Prog.subpbl domID pblID))
walther@59929
   111
   | check (Tactic.Substitute sube) (cs as (pt, (p, _))) =
walther@59928
   112
      let
walther@59928
   113
        val pp = Ctree.par_pblobj pt p
walther@59928
   114
        val thy = ThyC.get_theory (Ctree.get_obj Ctree.g_domID pt pp)
walther@59928
   115
        val f = Calc.current_formula cs;
walther@59928
   116
		    val {rew_ord', erls, ...} = Specify.get_met (Ctree.get_obj Ctree.g_metID pt pp)
walther@59929
   117
		    val subte = Subst.input_to_terms sube (*TODO: input requires parse _: _ -> _ option*)
walther@59928
   118
		    val subst = Subst.T_from_string_eqs thy sube
walther@59928
   119
		    val ro = Rewrite_Ord.assoc_rew_ord rew_ord'
walther@59928
   120
		  in
walther@59928
   121
		    if foldl and_ (true, map TermC.contains_Var subte)
walther@59928
   122
		    then (*1*)
walther@59928
   123
		      let val f' = subst_atomic subst f
walther@59928
   124
		      in if f = f'
walther@59928
   125
		        then Applicable.No (Subst.string_eqs_to_string sube ^ " not applicable")
walther@59928
   126
		        else Applicable.Yes (Tactic.Substitute' (ro, erls, subte, f, f'))
walther@59928
   127
		      end
walther@59928
   128
		    else (*2*)
walther@59928
   129
		      case Rewrite.rewrite_terms_ thy ro erls subte f of
walther@59928
   130
		        SOME (f', _) =>  Applicable.Yes (Tactic.Substitute' (ro, erls, subte, f, f'))
walther@59928
   131
		      | NONE => Applicable.No (Subst.string_eqs_to_string sube ^ " not applicable")
walther@59928
   132
		  end
walther@59928
   133
  | check (Tactic.Tac id) (cs as (pt, (p, _))) =
walther@59929
   134
      let 
walther@59929
   135
        val pp = Ctree.par_pblobj pt p; 
walther@59929
   136
        val thy' = Ctree.get_obj Ctree.g_domID pt pp;
walther@59929
   137
        val thy = ThyC.get_theory thy';
walther@59929
   138
        val f = Calc.current_formula cs;
walther@59929
   139
      in case id of
walther@59929
   140
        "subproblem_equation_dummy" =>
walther@59929
   141
    	  if TermC.is_expliceq f
walther@59929
   142
    	  then Applicable.Yes (Tactic.Tac_ (thy, UnparseC.term f, id, "subproblem_equation_dummy (" ^ UnparseC.term f ^ ")"))
walther@59929
   143
    	  else Applicable.No "applicable only to equations made explicit"
walther@59929
   144
      | "solve_equation_dummy" =>
walther@59929
   145
    	  let val (id', f') = ApplicableOLD.split_dummy (UnparseC.term f);
walther@59929
   146
    	  in
walther@59929
   147
    	    if id' <> "subproblem_equation_dummy"
walther@59929
   148
    	    then Applicable.No "no subproblem"
walther@59929
   149
    	    else if (ThyC.to_ctxt thy, f') |-> TermC.parseNEW |> the |> TermC.is_expliceq
walther@59929
   150
    		    then Applicable.Yes (Tactic.Tac_ (thy, UnparseC.term f, id, "[" ^ f' ^ "]"))
walther@59929
   151
    		    else error ("Solve_Step.check: f= " ^ f')
walther@59929
   152
        end
walther@59929
   153
      | _ => Applicable.Yes (Tactic.Tac_ (thy, UnparseC.term f, id, UnparseC.term f))
walther@59921
   154
      end
walther@59923
   155
  | check (Tactic.Take str) _ = Applicable.Yes (Tactic.Take' (TermC.str2term str)) (* always applicable ?*)
walther@59929
   156
  | check (Tactic.Begin_Trans) cs =
walther@59929
   157
      Applicable.Yes (Tactic.Begin_Trans' (Calc.current_formula cs))
walther@59923
   158
  | check (Tactic.End_Trans) (pt, (p, p_)) = (*TODO: check parent branches*)
walther@59923
   159
    if p_ = Pos.Res 
walther@59923
   160
	  then Applicable.Yes (Tactic.End_Trans' (Ctree.get_obj Ctree.g_result pt p))
walther@59923
   161
    else Applicable.No "'End_Trans' is not applicable at the beginning of a transitive sequence"
walther@59921
   162
  | check Tactic.End_Proof' _ = Applicable.Yes Tactic.End_Proof''
walther@59921
   163
  | check m _ = raise ERROR ("Solve_Step.check called for " ^ Tactic.input_to_string m);
walther@59920
   164
walther@59920
   165
(**)end(**);