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