src/Tools/isac/Test_Code/test-code.sml
author wneuper <Walther.Neuper@jku.at>
Sat, 04 Feb 2023 17:00:25 +0100
changeset 60675 d841c720d288
parent 60668 b5e3823f1cbd
child 60676 8c37f1009457
permissions -rw-r--r--
eliminate use of Thy_Info 22: eliminate UnparseC.term, rename "_in_ctxt" -> ""
     1 (*  Title:      Tools/isac/Testcode/test-code.sml
     2     Author:     Walther Neuper 200222
     3 
     4 Code used ONLY for $ISABELLE_ISAC_TEST/Tools/isac/*
     5 *)
     6 
     7 signature TEST_CODE =
     8 sig
     9   type NEW (* TODO: refactor "fun me" with Calc.state_pre and remove *)
    10   val f2str : Test_Out.mout -> TermC.as_string
    11   val TESTg_form : Proof.context -> Calc.T -> Test_Out.mout
    12 
    13   val init_calc : Proof.context -> Formalise.T list -> Pos.pos' * NEW * Test_Out.mout * Tactic.input * Celem.safe * Ctree.ctree
    14   val me : Tactic.input -> Pos.pos' -> NEW -> Ctree.ctree ->
    15     Pos.pos' * NEW * Test_Out.mout * Tactic.input * Celem.safe * Ctree.ctree
    16 
    17   val trace_ist_ctxt: Calc.T -> Tactic.input -> unit
    18   val me_trace: Calc.T -> Tactic.input -> (Calc.T -> Tactic.input -> unit) ->
    19     Calc.T * Tactic.input * Test_Out.mout
    20 
    21   val init_calc': Proof.context -> Formalise.T list -> Tactic.input * Calc.T
    22   val me': Tactic.input * Calc.T -> Tactic.input * Calc.T
    23 
    24   val tac2tac_ : Ctree.ctree -> Pos.pos' -> Tactic.input -> Tactic.T
    25 end
    26 
    27 (**)
    28 structure Test_Code(**): TEST_CODE(**) =
    29 struct
    30 (**)
    31 open Tactic
    32 open Ctree
    33 
    34 (** test Step.by_tactic + Step.do_next **)
    35 
    36 type NEW = int list; (*TODO remove*)
    37 
    38 (* 15.8.03 for me with loc_specify/solve, nxt_specify/solve
    39    delete as soon as TESTg_form -> _mout_ dropped           *)
    40 fun TESTg_form ctxt ptp =
    41   let
    42     val (form, _, _) = ME_Misc.pt_extract ctxt ptp
    43   in
    44     case form of
    45       Ctree.Form t => Test_Out.FormKF (UnparseC.term ctxt t)
    46     | Ctree.ModSpec (_, p_, _, gfr, where_, _) =>
    47       Test_Out.PpcKF (
    48         (case p_ of Pos.Pbl => Test_Out.Problem []
    49           | Pos.Met => Test_Out.MethodC []
    50           | _ => raise ERROR "TESTg_form: uncovered case",
    51  			P_Model.from (Proof_Context.theory_of ctxt) gfr where_))
    52    end
    53 (* for quick test-print-out, until 'type inout' is removed *)
    54 fun f2str (Test_Out.FormKF cterm') = cterm'
    55   | f2str _ = raise ERROR "f2str: uncovered case in fun.def.";                           
    56 
    57 (* create a calc-tree *)
    58 fun init_calc ctxt [(model, refs as (thy_id, _, _))] =
    59   let
    60 (*old* )val ctxt = thy_id |> ThyC.get_theory_PIDE ctxt |> Proof_Context.init_global
    61 ( *new*)val thy = thy_id |> ThyC.get_theory_PIDE ctxt  (*^^^^^^^^^^^^^^^^^^^^^^^^^ misleading*)
    62     val ((pt, p), tacis) = Step_Specify.initialise' thy (model, refs)
    63 	  val tac = case tacis of [] => Tactic.Empty_Tac | _ => (#1 o hd) tacis
    64 	  val f = TESTg_form ctxt (pt,p)
    65   in (p, []:NEW, f, tac, Celem.Sundef, pt) end
    66 | init_calc _ _ = raise ERROR "Tess_Code.init_calc: uncovered case"
    67        
    68 fun me (*(Empty_Tac) p _ _ = raise ERROR ("me:  Empty_Tac at " ^ pos'2str p)
    69   | me*) tac p _(*NEW remove*) pt =
    70     let
    71       val ctxt = Ctree.get_ctxt pt p
    72       val (pt, p) = 
    73   	    (*Step.by_tactic is here for testing by me; do_next would suffice in me*)
    74   	    case Step.by_tactic tac (pt, p) of
    75   		    ("ok", (_, _, ptp)) => ptp
    76   	    | ("unsafe-ok", (_, _, ptp)) => ptp
    77   	    | ("not-applicable",_) => (pt, p)
    78   	    | ("end-of-calculation", (_, _, ptp)) => ptp
    79   	    | ("failure", _) => raise ERROR "sys-raise ERROR by Step.by_tactic"
    80   	    | _ => raise ERROR "me: uncovered case Step.by_tactic"
    81   	  val (_, ts) =
    82         (*step's correct ctree is not tested in me, but in autocalc or Step.by_term*)
    83   	    (case Step.do_next p ((pt, Pos.e_pos'), []) of
    84   		    ("ok", (ts as (_, _, _) :: _, _, _)) => ("", ts)
    85   	    | ("helpless", _) => ("helpless: cannot propose tac", [])
    86   	    | ("dummy", (ts as (_, _, _) :: _, _, _)) => ("", ts) (*intermed.from specify*)
    87   	    | ("end-of-calculation", (ts, _, _)) => ("", ts)
    88   	    | ("failure", _) => raise ERROR "sys-raise ERROR by Step.do_next"
    89   	    | _ => raise ERROR  "me: uncovered case Step.do_next")
    90   	  val tac = 
    91         case ts of
    92           tacis as (_::_) => let val (tac, _, _) = last_elem tacis in tac end 
    93   		  | _ => if p = ([], Pos.Res) then Tactic.End_Proof' else Tactic.Empty_Tac;
    94     in
    95       (p, [] : NEW, TESTg_form ctxt (pt, p) (* form output comes from Step.by_tactic *), 
    96   	    tac, Celem.Sundef, pt)
    97     end
    98 
    99 
   100 (** traces for me_trace **)
   101 
   102 fun (*quiet (Model_Problem) = true
   103   |*) quiet (Add_Given _) = true
   104   | quiet (Add_Find _) = true
   105   | quiet (Add_Relation _) = true
   106   | quiet (Specify_Method _) = true
   107   | quiet (Specify_Problem _) = true
   108   | quiet (Specify_Theory _) = true
   109   | quiet _ = false
   110 
   111 fun mk_string (ist, ctxt) =
   112   ("(" ^ Istate.string_of' ist ^ ",\n" 
   113   ^ "... ctxt:    " ^ (ctxt |> ContextC.get_assumptions |> UnparseC.terms ctxt) ^ ")")
   114 
   115 fun trace_ist_ctxt (EmptyPtree , ([], Pos.Und)) _ = ()
   116   | trace_ist_ctxt (pt, (p, _)) tac =
   117   if quiet tac
   118   then ()
   119   else
   120     case get_obj g_loc pt p of
   121       (NONE, NONE) =>
   122         writeln   "Frm, Res: (NONE, NONE)"
   123     | (SOME ist_ctxt, NONE) =>
   124         (writeln ("Frm ..... (SOME " ^ mk_string ist_ctxt ^ " ,");
   125          writeln ("Res .....  NONE)"))
   126     | (NONE, SOME ist_ctxt) =>
   127         (writeln ("Frm .....  (NONE,");
   128          writeln ("Res .....  SOME " ^ mk_string ist_ctxt ^ " )"))
   129     | (SOME ic_frm, SOME ic_res) =>
   130         (writeln ("Frm ..... (SOME " ^ mk_string ic_frm ^ " ,");
   131          writeln ("Res .....  SOME " ^ mk_string ic_res ^ " )"))
   132 
   133 
   134 (** test Step.by_tactic + Step.do_next with tracing **)
   135 
   136 fun me_trace (*(Empty_Tac) p _ _ = raise ERROR ("me:  Empty_Tac at " ^ pos'2str p)
   137   | me*) (pt, p) tac trace =
   138     let
   139       val ctxt = Ctree.get_ctxt pt p
   140       val _ = if quiet tac then () else
   141         writeln ("========= " ^ Pos.pos'2str p ^ "========= Step.by_tactic: " ^
   142           Tactic.input_to_string ctxt tac ^ " ==================================");
   143       val (pt', p') = 
   144   	    case Step.by_tactic tac (pt, p) of
   145   		    ("ok", (_, _, ptp)) => ptp
   146   	    | ("unsafe-ok", (_, _, ptp)) => ptp
   147   	    | ("not-applicable",_) => (pt, p)
   148   	    | ("end-of-calculation", (_, _, ptp)) => ptp
   149   	    | ("failure", _) => raise ERROR "sys-raise ERROR by Step.by_tactic"
   150   	    | _ => raise ERROR "me: uncovered case Step.by_tactic"
   151       val _ = trace (pt', p') tac
   152   	  val (_, ts, (pt'', p'')) =
   153   	    (case Step.do_next p' ((pt', Pos.e_pos'), []) of
   154   		    ("ok", (ts as [(_, _, _)(*, _, _, ...*)], _, (pt'', p''))) => ("", ts, (pt'', p''))
   155   	    | ("helpless", _) => ("helpless: cannot propose tac", [], Ctree.e_state)
   156   	    | ("dummy", (ts as (_, _, _) :: _, _, _)) => ("", ts, Ctree.e_state)
   157   	    | ("end-of-calculation", (ts, _, _)) => ("", ts, Ctree.e_state)
   158   	    | ("failure", _) => raise ERROR "sys-raise ERROR by Step.do_next"
   159   	    | _ => raise ERROR  "me: uncovered case Step.do_next")
   160   	  val tac'' = 
   161         case ts of 
   162           tacis as (_::_) => let val (tac, _, _) = last_elem tacis in tac end 
   163   		  | _ => if p' = ([], Pos.Res) then Tactic.End_Proof' else Tactic.Empty_Tac;
   164       val _ = if quiet tac then () else
   165         writeln ("--------- " ^ Pos.pos'2str p' ^ "--------- Step.do_next \<rightarrow> " ^
   166           Tactic.input_to_string ctxt tac'' ^ "----------------------------------");
   167       val _ = trace (pt'', p'') tac
   168     in
   169       ((pt', p'), tac'', TESTg_form ctxt (pt', p') (* form output comes from Step.by_tactic *))
   170     end
   171 
   172 fun tac2tac_ pt p m = 
   173   let
   174     val ctxt = Ctree.get_ctxt pt p
   175   in
   176     case Step.check m (pt, p) of
   177   	  Applicable.Yes m' => m' 
   178     | Applicable.No _ => raise ERROR ("tac2mstp': fails with" ^ Tactic.input_to_string ctxt m)
   179   end;
   180 
   181 
   182 (** me' for |> **)
   183 fun init_calc' ctxt [(model, refs as (thy_id, _, _))] =
   184   let
   185     val thy = thy_id |> ThyC.get_theory_PIDE ctxt
   186     val ((pt, p), _) = Step_Specify.initialise' thy (model, refs)
   187   in
   188     (Tactic.Model_Problem, (pt, p))                       (* thus <Output> shows Tactic on top *)
   189   end
   190 | init_calc' _ _ = raise ERROR "Test_Code.init_calc' @{context}: uncovered case"
   191 
   192 fun me' (tac, (pt, p)) =                                  (* thus <Output> shows Tactic on top *)
   193   let 
   194     val (pt', p') = 
   195       (*Step.by_tactic is here for testing by me; do_next would suffice in me*)
   196       case Step.by_tactic tac (pt, p) of
   197   	    ("ok", (_, _, ptp)) => ptp
   198       | ("unsafe-ok", (_, _, ptp)) => ptp
   199       | ("not-applicable",_) => (pt, p)
   200       | ("end-of-calculation", (_, _, ptp)) => ptp
   201       | ("failure", _) => raise ERROR "sys-raise ERROR by Step.by_tactic"
   202       | _ => raise ERROR "me: uncovered case Step.by_tactic"
   203     val tac' =
   204       (*step's correct ctree is not tested in me, but in autocalc or Step.by_term*)
   205       (case Step.do_next p' ((pt', Pos.e_pos'), []) of
   206   	    ("ok", ((tac', _, _) :: _, _, _)) => tac'
   207       | ("helpless", _) => Tactic.Empty_Tac
   208       | ("dummy", ((tac', _, _) :: _, _, _)) => tac'
   209       | ("end-of-calculation", _) => Tactic.End_Proof'
   210       | ("failure", _) => raise ERROR "sys-raise ERROR by Step.do_next"
   211       | _ => raise ERROR  "me: uncovered case Step.do_next")
   212   in 
   213     (tac', (pt', p'))                                     (* thus <Output> shows Tactic on top *)
   214   (* ^^^^----- is the next step after "(pt', p')", but we prefer ----------------^^^^^^^^^^^^^ *)
   215   end
   216 
   217 (**)end(**)