src/Tools/isac/Interpret/solve-step.sml
author Walther Neuper <walther.neuper@jku.at>
Sat, 02 May 2020 10:57:04 +0200
changeset 59925 caf3839e53c5
parent 59923 cd730f07c9ac
child 59927 877d6bc38715
permissions -rw-r--r--
remove unused tactics, part 1
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@59923
    27
fun check (Tactic.CAScmd ct') _ =
walther@59923
    28
    error ("Solve_Step.check: not impl. for " ^ Tactic.input_to_string (Tactic.CAScmd ct'))  
walther@59923
    29
  | check (m as Tactic.Calculate op_) (pt, (p, p_)) =
walther@59923
    30
    if member op = [Pos.Pbl, Pos.Met] p_
walther@59923
    31
    then Applicable.No ((Tactic.input_to_string m)^" not for pos "^(Pos.pos'2str (p,p_)))
walther@59923
    32
    else
walther@59923
    33
      let 
walther@59923
    34
        val (msg,thy',isa_fn) = ApplicableOLD.from_pblobj_or_detail_calc op_ p pt;
walther@59923
    35
        val f = case p_ of
walther@59923
    36
          Frm => Ctree.get_obj Ctree.g_form pt p
walther@59923
    37
    	  | Pos.Res => (fst o (Ctree.get_obj Ctree.g_result pt)) p
walther@59923
    38
      	| _ => raise ERROR ("Solve_Step.check: call by " ^ Pos.pos'2str (p, p_));
walther@59923
    39
      in
walther@59923
    40
        if msg = "OK"
walther@59923
    41
        then
walther@59923
    42
    	    case Rewrite.calculate_ (ThyC.get_theory thy') isa_fn f of
walther@59923
    43
    	      SOME (f', (id, thm))
walther@59923
    44
    	        => Applicable.Yes (Tactic.Calculate' (thy', op_, f, (f', (id, thm))))
walther@59923
    45
    	    | NONE => Applicable.No ("'calculate "^op_^"' not applicable") 
walther@59923
    46
        else Applicable.No msg                                              
walther@59923
    47
      end
walther@59923
    48
  | check (Tactic.Check_Postcond pI) (_, (p, p_)) =
walther@59921
    49
      if member op = [Pos.Pbl, Pos.Met] p_                  
walther@59921
    50
      then Applicable.No ((Tactic.input_to_string (Tactic.Check_Postcond pI)) ^ " not for pos " ^ Pos.pos'2str (p, p_))
walther@59921
    51
      else Applicable.Yes (Tactic.Check_Postcond' (pI, TermC.empty))
walther@59923
    52
  | check (m as Tactic.Check_elementwise pred) (pt, (p, p_)) =
walther@59923
    53
    if member op = [Pos.Pbl, Pos.Met] p_ 
walther@59923
    54
    then Applicable.No ((Tactic.input_to_string m) ^ " not for pos " ^ Pos.pos'2str (p, p_))
walther@59923
    55
    else
walther@59923
    56
      let 
walther@59923
    57
        val pp = Ctree.par_pblobj pt p; 
walther@59923
    58
        val thy' = Ctree.get_obj Ctree.g_domID pt pp;
walther@59923
    59
        val thy = ThyC.get_theory thy'
walther@59923
    60
        val metID = (Ctree.get_obj Ctree.g_metID pt pp)
walther@59923
    61
        val {crls, ...} =  Specify.get_met metID
walther@59923
    62
        val (f, asm) = case p_ of
walther@59923
    63
          Frm => (Ctree.get_obj Ctree.g_form pt p , [])
walther@59923
    64
        | Pos.Res => Ctree.get_obj Ctree.g_result pt p
walther@59923
    65
        | _ => error ("Solve_Step.check: call by " ^ Pos.pos'2str (p, p_));
walther@59923
    66
        val vp = (ThyC.to_ctxt thy, pred) |-> TermC.parseNEW |> the |> ApplicableOLD.mk_set thy pt p f;
walther@59923
    67
      in
walther@59923
    68
        Applicable.Yes (Tactic.Check_elementwise' (f, pred, (f, asm)))
walther@59923
    69
      end
walther@59923
    70
(*RM* )| Derive of Rule_Set.id( *RM*)
walther@59923
    71
  | check Tactic.Empty_Tac _ = Applicable.No "Empty_Tac is not applicable"
walther@59921
    72
  | check (Tactic.Free_Solve) _ = Applicable.Yes (Tactic.Free_Solve')        (* always applicable *)
walther@59923
    73
  | check  (Tactic.Apply_Assumption cts') _ =
walther@59923
    74
    raise ERROR ("Solve_Step.check: not impl. for " ^ Tactic.input_to_string (Tactic.Apply_Assumption cts'))
walther@59923
    75
    (* 'logical' applicability wrt. script in locate_input_tactic: Inconsistent? *)
walther@59923
    76
  | check Tactic.Or_to_List (pt, (p, p_)) =
walther@59923
    77
    if member op = [Pos.Pbl, Pos.Met] p_ 
walther@59923
    78
    then Applicable.No ((Tactic.input_to_string Tactic.Or_to_List)^" not for pos "^(Pos.pos'2str (p,p_)))
walther@59923
    79
    else
walther@59923
    80
      let 
walther@59923
    81
        val f = case p_ of
walther@59923
    82
          Frm => Ctree.get_obj Ctree.g_form pt p
walther@59923
    83
    	  | Pos.Res => (fst o (Ctree.get_obj Ctree.g_result pt)) p
walther@59923
    84
        | _ => error ("Solve_Step.check: call by " ^ Pos.pos'2str (p, p_));
walther@59923
    85
      in (let val ls = Prog_Expr.or2list f
walther@59923
    86
          in Applicable.Yes (Tactic.Or_to_List' (f, ls)) end) 
walther@59923
    87
         handle _ => Applicable.No ("'Or_to_List' not applicable to " ^ UnparseC.term f)
walther@59923
    88
      end
walther@59923
    89
  | check (m as Tactic.Rewrite thm'') (pt, (p, p_)) = 
walther@59923
    90
    if member op = [Pos.Pbl, Pos.Met] p_ 
walther@59923
    91
    then Applicable.No ((Tactic.input_to_string m)^" not for pos "^(Pos.pos'2str (p, p_)))
walther@59923
    92
    else
walther@59923
    93
      let
walther@59923
    94
        val (msg, thy', ro, rls', _)= ApplicableOLD.from_pblobj_or_detail_thm thm'' p pt;
walther@59923
    95
        val thy = ThyC.get_theory thy';
walther@59923
    96
        val f = case p_ of
walther@59923
    97
          Frm => Ctree.get_obj Ctree.g_form pt p
walther@59923
    98
	      | Pos.Res => (fst o (Ctree.get_obj Ctree.g_result pt)) p
walther@59923
    99
	      | _ => error ("Solve_Step.check Rewrite: call by " ^ Pos.pos'2str (p, p_));
walther@59923
   100
      in
walther@59923
   101
        if msg = "OK" 
walther@59923
   102
        then
walther@59923
   103
          case Rewrite.rewrite_ thy (Rewrite_Ord.assoc_rew_ord ro) rls' false (snd thm'') f of
walther@59923
   104
            SOME (f',asm) => Applicable.Yes (Tactic.Rewrite' (thy', ro, rls', false, thm'', f, (f', asm)))
walther@59923
   105
          | NONE => Applicable.No ("'" ^ fst thm'' ^"' not applicable") 
walther@59923
   106
        else Applicable.No msg
walther@59923
   107
      end
walther@59921
   108
  | check (m as Tactic.Rewrite_Inst (subs, thm'')) (pt, (p, p_)) = 
walther@59921
   109
    if member op = [Pos.Pbl, Pos.Met] p_ 
walther@59921
   110
    then Applicable.No ((Tactic.input_to_string m)^" not for pos " ^ Pos.pos'2str (p, p_))
walther@59921
   111
    else
walther@59921
   112
      let 
walther@59921
   113
        val pp = Ctree.par_pblobj pt p;
walther@59921
   114
        val thy' = Ctree.get_obj Ctree.g_domID pt pp;
walther@59921
   115
        val thy = ThyC.get_theory thy';
walther@59921
   116
        val {rew_ord' = ro', erls = erls, ...} = Specify.get_met (Ctree.get_obj Ctree.g_metID pt pp);
walther@59921
   117
        val (f, _) = case p_ of (*p 12.4.00 unnecessary*)
walther@59921
   118
                      Frm => (Ctree.get_obj Ctree.g_form pt p, p)
walther@59921
   119
                    | Pos.Res => ((fst o (Ctree.get_obj Ctree.g_result pt)) p, Pos.lev_on p)
walther@59921
   120
                    | _ => error ("Solve_Step.check: call by " ^ Pos.pos'2str (p, p_));
walther@59921
   121
      in 
walther@59921
   122
        let
walther@59921
   123
          val subst = Subst.T_from_input thy subs;
walther@59921
   124
        in
walther@59921
   125
          case Rewrite.rewrite_inst_ thy (Rewrite_Ord.assoc_rew_ord ro') erls false subst (snd thm'') f of
walther@59921
   126
            SOME (f',asm) =>
walther@59921
   127
              Applicable.Yes (Tactic.Rewrite_Inst' (thy', ro', erls, false, subst, thm'', f, (f', asm)))
walther@59921
   128
          | NONE => Applicable.No ((fst thm'')^" not applicable")
walther@59921
   129
        end
walther@59921
   130
        handle _ => Applicable.No ("syntax error in "^(subs2str subs))
walther@59921
   131
      end
walther@59923
   132
  | check (m as Tactic.Rewrite_Set rls) (pt, (p, p_)) =
walther@59921
   133
    if member op = [Pos.Pbl, Pos.Met] p_ 
walther@59923
   134
    then Applicable.No (Tactic.input_to_string m ^ " not for pos " ^ Pos.pos'2str (p, p_))
walther@59921
   135
    else
walther@59921
   136
      let 
walther@59923
   137
        val pp = Ctree.par_pblobj pt p; 
walther@59921
   138
        val thy' = Ctree.get_obj Ctree.g_domID pt pp;
walther@59923
   139
        val (f, _) = case p_ of
walther@59923
   140
          Frm => (Ctree.get_obj Ctree.g_form pt p, p)
walther@59923
   141
    	  | Pos.Res => ((fst o (Ctree.get_obj Ctree.g_result pt)) p, Pos.lev_on p)
walther@59923
   142
    	  | _ => error ("Solve_Step.check: call by " ^ Pos.pos'2str (p, p_));
walther@59923
   143
      in
walther@59923
   144
        case Rewrite.rewrite_set_ (ThyC.get_theory thy') false (assoc_rls rls) f of
walther@59921
   145
          SOME (f', asm)
walther@59923
   146
            => Applicable.Yes (Tactic.Rewrite_Set' (thy', false, assoc_rls rls, f, (f', asm)))
walther@59923
   147
          | NONE => Applicable.No (rls ^ " not applicable")
walther@59921
   148
      end
walther@59921
   149
  | check (m as Tactic.Rewrite_Set_Inst (subs, rls)) (pt, (p, p_)) =
walther@59921
   150
    if member op = [Pos.Pbl, Pos.Met] p_ 
walther@59921
   151
    then Applicable.No ((Tactic.input_to_string m)^" not for pos "^(Pos.pos'2str (p,p_)))
walther@59921
   152
    else
walther@59921
   153
      let 
walther@59921
   154
        val pp = Ctree.par_pblobj pt p;
walther@59921
   155
        val thy' = Ctree.get_obj Ctree.g_domID pt pp;
walther@59921
   156
        val thy = ThyC.get_theory thy';
walther@59921
   157
        val (f, _) = case p_ of
walther@59921
   158
          Frm => (Ctree.get_obj Ctree.g_form pt p, p)
walther@59921
   159
    	  | Pos.Res => ((fst o (Ctree.get_obj Ctree.g_result pt)) p, Pos.lev_on p)
walther@59921
   160
    	  | _ => error ("Solve_Step.check: call by " ^ Pos.pos'2str (p, p_));
walther@59921
   161
    	  val subst = Subst.T_from_input thy subs;
walther@59921
   162
      in 
walther@59921
   163
        case Rewrite.rewrite_set_inst_ thy (*put_asm*)false subst (assoc_rls rls) f of
walther@59921
   164
          SOME (f',asm)
walther@59921
   165
            => Applicable.Yes (Tactic.Rewrite_Set_Inst' (thy', false, subst, assoc_rls rls, f, (f', asm)))
walther@59921
   166
        | NONE => Applicable.No (rls ^ " not applicable")
walther@59921
   167
        handle _ => Applicable.No ("syntax error in " ^(subs2str subs))
walther@59921
   168
      end
walther@59923
   169
  | check (m as Tactic.Subproblem (domID, pblID)) (_, (p, p_)) = 
walther@59923
   170
     if Pos.on_specification p_
walther@59923
   171
     then
walther@59923
   172
       Applicable.No (Tactic.input_to_string m ^ " not for pos " ^ Pos.pos'2str (p, p_))
walther@59923
   173
     else (*some fields filled later in LI*)
walther@59923
   174
       Applicable.Yes (Tactic.Subproblem' ((domID, pblID, Method.id_empty), [], 
walther@59923
   175
			   TermC.empty, [], ContextC.empty, Auto_Prog.subpbl domID pblID))
walther@59921
   176
    (*Substitute combines two different kind of "substitution":
walther@59921
   177
      (1) subst_atomic: for ?a..?z
walther@59921
   178
      (2) Pattern.match: for solving equational systems (which raises exn for ?a..?z)*)
walther@59921
   179
  | check (m as Tactic.Substitute sube) (pt, (p, p_)) =
walther@59921
   180
      if member op = [Pos.Pbl, Pos.Met] p_ 
walther@59921
   181
      then Applicable.No (Tactic.input_to_string m ^ " not for pos " ^ Pos.pos'2str (p, p_))
walther@59921
   182
      else 
walther@59921
   183
        let
walther@59921
   184
          val pp = Ctree.par_pblobj pt p
walther@59921
   185
          val thy = ThyC.get_theory (Ctree.get_obj Ctree.g_domID pt pp)
walther@59921
   186
          val f = case p_ of
walther@59921
   187
		        Frm => Ctree.get_obj Ctree.g_form pt p
walther@59921
   188
		      | Pos.Res => (fst o (Ctree.get_obj Ctree.g_result pt)) p
walther@59921
   189
      	  | _ => error ("Solve_Step.check: call by " ^ Pos.pos'2str (p, p_));
walther@59921
   190
		      val {rew_ord', erls, ...} = Specify.get_met (Ctree.get_obj Ctree.g_metID pt pp)
walther@59921
   191
		      val subte = Subst.input_to_terms sube
walther@59921
   192
		      val subst = Subst.T_from_string_eqs thy sube
walther@59921
   193
		      val ro = Rewrite_Ord.assoc_rew_ord rew_ord'
walther@59921
   194
		    in
walther@59921
   195
		      if foldl and_ (true, map TermC.contains_Var subte)
walther@59921
   196
		      then (*1*)
walther@59921
   197
		        let val f' = subst_atomic subst f
walther@59921
   198
		        in if f = f'
walther@59921
   199
		          then Applicable.No (Subst.string_eqs_to_string sube ^ " not applicable")
walther@59921
   200
		          else Applicable.Yes (Tactic.Substitute' (ro, erls, subte, f, f'))
walther@59921
   201
		        end
walther@59921
   202
		      else (*2*)
walther@59921
   203
		        case Rewrite.rewrite_terms_ thy ro erls subte f of
walther@59921
   204
		          SOME (f', _) =>  Applicable.Yes (Tactic.Substitute' (ro, erls, subte, f, f'))
walther@59921
   205
		        | NONE => Applicable.No (Subst.string_eqs_to_string sube ^ " not applicable")
walther@59921
   206
		    end
walther@59921
   207
  | check (Tactic.Tac id) (pt, (p, p_)) =
walther@59921
   208
    let 
walther@59921
   209
      val pp = Ctree.par_pblobj pt p; 
walther@59921
   210
      val thy' = Ctree.get_obj Ctree.g_domID pt pp;
walther@59921
   211
      val thy = ThyC.get_theory thy';
walther@59921
   212
      val f = case p_ of
walther@59921
   213
         Frm => Ctree.get_obj Ctree.g_form pt p
walther@59921
   214
      | Pos.Pbl => error "Solve_Step.check (p,Pos.Pbl) pt (Tac id): not at Pos.Pbl"
walther@59921
   215
  	  | Pos.Res => (fst o (Ctree.get_obj Ctree.g_result pt)) p
walther@59921
   216
      | _ => error ("Solve_Step.check: call by " ^ Pos.pos'2str (p, p_));
walther@59921
   217
    in case id of
walther@59921
   218
      "subproblem_equation_dummy" =>
walther@59921
   219
  	  if TermC.is_expliceq f
walther@59921
   220
  	  then Applicable.Yes (Tactic.Tac_ (thy, UnparseC.term f, id, "subproblem_equation_dummy (" ^ UnparseC.term f ^ ")"))
walther@59921
   221
  	  else Applicable.No "applicable only to equations made explicit"
walther@59921
   222
    | "solve_equation_dummy" =>
walther@59921
   223
  	  let val (id', f') = ApplicableOLD.split_dummy (UnparseC.term f);
walther@59921
   224
  	  in
walther@59921
   225
  	    if id' <> "subproblem_equation_dummy"
walther@59921
   226
  	    then Applicable.No "no subproblem"
walther@59921
   227
  	    else if (ThyC.to_ctxt thy, f') |-> TermC.parseNEW |> the |> TermC.is_expliceq
walther@59921
   228
  		    then Applicable.Yes (Tactic.Tac_ (thy, UnparseC.term f, id, "[" ^ f' ^ "]"))
walther@59921
   229
  		    else error ("Solve_Step.check: f= " ^ f')
walther@59921
   230
      end
walther@59921
   231
    | _ => Applicable.Yes (Tactic.Tac_ (thy, UnparseC.term f, id, UnparseC.term f))
walther@59921
   232
    end
walther@59923
   233
  | check (Tactic.Take str) _ = Applicable.Yes (Tactic.Take' (TermC.str2term str)) (* always applicable ?*)
walther@59923
   234
  | check (Tactic.Begin_Trans) (pt, (p, p_)) =
walther@59923
   235
    let
walther@59923
   236
      val (f, _) = case p_ of   (*p 12.4.00 unnecessary, implizit Take in gen*)
walther@59923
   237
        Pos.Frm => (Ctree.get_obj Ctree.g_form pt p, (Pos.lev_on o Pos.lev_dn) p)
walther@59923
   238
      | Pos.Res => ((fst o (Ctree.get_obj Ctree.g_result pt)) p, (Pos.lev_on o Pos.lev_dn o Pos.lev_on) p)
walther@59923
   239
      | _ => error ("Solve_Step.check: call by " ^ Pos.pos'2str (p, p_));
walther@59923
   240
    in (Applicable.Yes (Tactic.Begin_Trans' f))
walther@59923
   241
      handle _ => raise ERROR ("Solve_Step.check: Begin_Trans finds  syntaxerror in '" ^ UnparseC.term f ^ "'")
walther@59923
   242
    end
walther@59923
   243
  | check (Tactic.End_Trans) (pt, (p, p_)) = (*TODO: check parent branches*)
walther@59923
   244
    if p_ = Pos.Res 
walther@59923
   245
	  then Applicable.Yes (Tactic.End_Trans' (Ctree.get_obj Ctree.g_result pt p))
walther@59923
   246
    else Applicable.No "'End_Trans' is not applicable at the beginning of a transitive sequence"
walther@59921
   247
  | check Tactic.End_Proof' _ = Applicable.Yes Tactic.End_Proof''
walther@59921
   248
  | check m _ = raise ERROR ("Solve_Step.check called for " ^ Tactic.input_to_string m);
walther@59920
   249
walther@59920
   250
(**)end(**);