src/Tools/isac/MathEngine/mathengine-stateless.sml
author Walther Neuper <walther.neuper@jku.at>
Tue, 11 Feb 2020 11:58:45 +0100
changeset 59804 403f00b309ef
parent 59794 8f7b67b1d5e2
child 59806 1e69c59424e5
permissions -rw-r--r--
introduce Step.by_tactic, part 1
walther@59787
     1
(* MathEngine/mathengine-stateless.sml
walther@59787
     2
   The _functional_ mathematics engine, ie. without a state.
neuper@37906
     3
   Input and output are Isabelle's formulae as strings.
neuper@37906
     4
   authors: Walther Neuper 2000
neuper@37906
     5
   (c) due to copyright terms
neuper@37906
     6
*)
neuper@37906
     7
wneuper@59261
     8
signature MATH_ENGINE =
wneuper@59261
     9
sig
wneuper@59261
    10
  type NEW (* TODO: refactor "fun me" with calcstate and remove *)
walther@59749
    11
  val me : Tactic.input -> Ctree.pos' -> NEW -> Ctree.ctree ->
walther@59749
    12
    Ctree.pos' * NEW * Generate.mout * Tactic.input * Telem.safe * Ctree.ctree
walther@59775
    13
  val autocalc : Ctree.pos' list -> Ctree.pos' -> (Calc.T) * Generate.taci list ->
walther@59775
    14
    Solve.auto -> string * Ctree.pos' list * (Calc.T)
wneuper@59411
    15
  val detailstep : Ctree.ctree -> Ctree.pos' -> string * Ctree.ctree * Ctree.pos'
neuper@37906
    16
walther@59773
    17
  val initcontext_met : Ctree.ctree -> Ctree.pos' -> bool * string list * Program.T * Model.itm list * (bool * term) list
wneuper@59316
    18
  val initcontext_pbl : Ctree.ctree -> Ctree.pos' -> bool * string list * term * Model.itm list * (bool * term) list
walther@59773
    19
  val context_met : Celem.metID -> Ctree.ctree -> Ctree.pos -> bool * Celem.metID * Program.T * Model.itm list * (bool * term) list
wneuper@59405
    20
  val context_pbl : Celem.pblID -> Ctree.ctree -> Ctree.pos -> bool * Celem.pblID * term * Model.itm list * (bool * term) list
walther@59775
    21
  val set_method : Celem.metID -> Calc.T -> Ctree.ctree * Ctree.ocalhd
walther@59775
    22
  val set_problem : Celem.pblID -> Calc.T -> Ctree.ctree * Ctree.ocalhd
walther@59775
    23
  val set_theory : Rule.thyID -> Calc.T -> Ctree.ctree * Ctree.ocalhd
wneuper@59405
    24
  val tryrefine : Celem.pblID -> Ctree.ctree -> Ctree.pos' -> bool * Celem.pblID * term * Model.itm list * (bool * term) list
wneuper@59310
    25
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
walther@59749
    26
  val CalcTreeTEST : Selem.fmz list -> Ctree.pos' * NEW * Generate.mout * Tactic.input * Telem.safe * Ctree.ctree
wneuper@59416
    27
  val f2str : Generate.mout -> Rule.cterm'
walther@59785
    28
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
wneuper@59261
    29
  type nxt_
walther@59775
    30
  val TESTg_form : Calc.T -> Generate.mout
walther@59785
    31
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
wneuper@59310
    32
wneuper@59310
    33
(*----- unused code, kept as hints to design ideas ---------------------------------------------*)
wneuper@59310
    34
  (* NONE *)
wneuper@59261
    35
end
neuper@37906
    36
wneuper@59261
    37
(**)
wneuper@59261
    38
structure Math_Engine(**): MATH_ENGINE(**) =
wneuper@59261
    39
struct
wneuper@59261
    40
(**)
neuper@37906
    41
neuper@37906
    42
datatype nxt_ =
wneuper@59261
    43
	HElpless  (**)
wneuper@59265
    44
| Nexts of Chead.calcstate (**)
neuper@37906
    45
wneuper@59261
    46
(* specify a new method; WN0512 impl.incomplete, see 'nxt_specif (Specify_Method ' *)
wneuper@59276
    47
fun set_method mI ptp =
wneuper@59261
    48
  let
wneuper@59261
    49
    val (mits, pt, p) =
walther@59764
    50
      case Step_Specify.by_tactic (Tactic.Specify_Method mI) ptp of
wneuper@59571
    51
        ([(_, Tactic.Specify_Method' (_, _, mits), _)], [], (pt, (p, _))) => (mits, pt, p)
wneuper@59261
    52
      | _ => error "set_method: case 1 uncovered"
wneuper@59261
    53
  	val pre = []        (*...from Specify_Method'*)
wneuper@59261
    54
  	val complete = true (*...from Specify_Method'*)
wneuper@59261
    55
  	(*from Specify_Method'  ?   vvv,  vvv ?*)
wneuper@59261
    56
    val (hdf, spec) =
wneuper@59276
    57
      case Ctree.get_obj I pt p of
wneuper@59276
    58
        Ctree.PblObj {origin = (_, _, hdf), spec, ...} => (hdf, spec)
wneuper@59276
    59
      | Ctree.PrfObj _ => error "set_method: case 2 uncovered"
wneuper@59261
    60
  in
walther@59694
    61
    (pt, (complete, Pos.Met, hdf, mits, pre, spec) : Ctree.ocalhd)
wneuper@59261
    62
  end
neuper@37906
    63
wneuper@59261
    64
(* specify a new problem; WN0512 impl.incomplete, see 'nxt_specif (Specify_Problem ' *)
wneuper@59276
    65
fun set_problem pI ptp =
wneuper@59261
    66
  let
wneuper@59261
    67
    val (complete, pits, pre, pt, p) =
walther@59794
    68
      case Step_Specify.by_tactic (Tactic.Specify_Problem pI) ptp of
walther@59794
    69
        ([(_, Tactic.Specify_Problem' (_, (complete, (pits, pre))), _)], _, (pt, (p, _)))
wneuper@59261
    70
          => (complete, pits, pre, pt, p)
wneuper@59261
    71
      | _ => error "set_problem: case 1 uncovered"
wneuper@59261
    72
    (*from Specify_Problem' ?   vvv,  vvv ?*)
wneuper@59261
    73
    val (hdf, spec) =
wneuper@59276
    74
      case Ctree.get_obj I pt p of
wneuper@59276
    75
        Ctree.PblObj {origin = (_, _, hdf), spec, ...} => (hdf, spec)
wneuper@59276
    76
      | Ctree.PrfObj _ => error "set_problem: case 2 uncovered"
wneuper@59261
    77
  in
walther@59694
    78
    (pt, (complete, Pos.Pbl, hdf, pits, pre, spec) : Ctree.ocalhd)
wneuper@59261
    79
  end
neuper@37906
    80
wneuper@59276
    81
fun set_theory tI ptp =
wneuper@59261
    82
  let
wneuper@59261
    83
    val (complete, pits, pre, pt, p) =
walther@59764
    84
      case Step_Specify.by_tactic (Tactic.Specify_Theory tI) ptp of  
wneuper@59571
    85
        ([(_, Tactic.Specify_Problem' (_, (complete, (pits, pre))), _)], _, (pt, (p, _)))
wneuper@59261
    86
          => (complete, pits, pre, pt, p)
wneuper@59261
    87
      | _ => error "set_theory: case 1 uncovered"
wneuper@59261
    88
  	(*from Specify_Theory'  ?   vvv,  vvv ?*)
wneuper@59261
    89
    val (hdf, spec) =
wneuper@59276
    90
      case Ctree.get_obj I pt p of
wneuper@59276
    91
        Ctree.PblObj {origin = (_, _, hdf), spec, ...} => (hdf, spec)
wneuper@59276
    92
      | Ctree.PrfObj _ => error "set_theory: case 2 uncovered"
walther@59694
    93
  in (pt, (complete, Pos.Pbl, hdf, pits, pre, spec) : Ctree.ocalhd) end;
neuper@37906
    94
wneuper@59261
    95
(* does several steps within one calculation as given by "type auto";
neuper@37906
    96
   the steps may arbitrarily go into and leave different phases, 
wneuper@59569
    97
   i.e. specify-phase and solve-phase *)
walther@59747
    98
fun autocalc c ip cs (Solve.Steps s) =
neuper@37906
    99
    if s <= 1
wneuper@59261
   100
    then
walther@59765
   101
      let val (str, (_, c', ptp)) = Step.do_next ip cs;      (* autoord = 1, probably does 1 do_next too much*)
wneuper@59261
   102
	    in (str, c@c', ptp) end
wneuper@59261
   103
    else
walther@59765
   104
      let val (str, (_, c', ptp as (_, p))) = Step.do_next ip cs;
wneuper@59261
   105
      in
wneuper@59261
   106
        if str = "ok" 
walther@59747
   107
        then autocalc (c@c') p (ptp, []) (Solve.Steps (s - 1))
wneuper@59261
   108
        else (str, c@c', ptp) end
wneuper@59261
   109
    (* handles autoord <= 3, autoord > 3 handled by all_solve, complete_solve *)
wneuper@59261
   110
  | autocalc c (pos as (_, p_)) ((pt, _), _(*tacis could help 1x in solve*)) auto =
wneuper@59276
   111
    if Solve.autoord auto > 3 andalso Ctree.just_created (pt, pos)
wneuper@59261
   112
    then
wneuper@59265
   113
      let val ptp = Chead.all_modspec (pt, pos);
wneuper@59273
   114
      in Solve.all_solve auto c ptp end                    (*... auto = 4 | 5 | 6 *)
wneuper@59261
   115
    else
walther@59694
   116
      if member op = [Pos.Pbl, Pos.Met] p_
wneuper@59261
   117
      then
wneuper@59265
   118
        if not (Chead.is_complete_mod (pt, pos))
wneuper@59261
   119
   	    then
wneuper@59265
   120
   	      let val ptp = Chead.complete_mod (pt, pos)      (*... auto = 2 | 3 *)
wneuper@59261
   121
   		    in
wneuper@59273
   122
   		      if Solve.autoord auto < 3 then ("ok", c, ptp)
wneuper@59261
   123
   		      else
wneuper@59265
   124
   		       if not (Chead.is_complete_spec ptp)
wneuper@59261
   125
   			     then
wneuper@59265
   126
   			       let val ptp = Chead.complete_spec ptp
wneuper@59261
   127
   			       in
wneuper@59273
   128
   			         if Solve.autoord auto = 3 then ("ok", c, ptp) else Solve.all_solve auto c ptp
wneuper@59261
   129
   			       end
wneuper@59273
   130
   			     else if Solve.autoord auto = 3 then ("ok", c, ptp) else Solve.all_solve auto c ptp 
wneuper@59261
   131
   		    end
wneuper@59261
   132
   	    else 
wneuper@59265
   133
   		    if not (Chead.is_complete_spec (pt,pos))
wneuper@59261
   134
   		    then
wneuper@59265
   135
   		      let val ptp = Chead.complete_spec (pt, pos)
wneuper@59261
   136
   		      in
wneuper@59273
   137
   		        if Solve.autoord auto = 3 then ("ok", c, ptp) else Solve.all_solve auto c ptp
wneuper@59261
   138
   		      end
wneuper@59261
   139
   		    else
wneuper@59273
   140
   		      if Solve.autoord auto = 3 then ("ok", c, (pt, pos)) else Solve.all_solve auto c (pt, pos)
wneuper@59273
   141
   	  else Solve.complete_solve auto c (pt, pos);
neuper@37906
   142
neuper@37906
   143
(*.initialiye matching; before 'tryMatch' get the pblID to match with:
neuper@37906
   144
   if no pbl has been specified, take the init from origin.*)
wneuper@59261
   145
fun initcontext_pbl pt (p, _) =
wneuper@59261
   146
  let
wneuper@59261
   147
    val (probl, os, pI, hdl, pI') =
wneuper@59276
   148
      case Ctree.get_obj I pt p of
wneuper@59276
   149
        Ctree.PblObj {probl, origin = (os, (_, pI, _), hdl), spec=(_, pI', _), ...}
wneuper@59261
   150
          => (probl, os, pI, hdl, pI')
wneuper@59276
   151
      | Ctree.PrfObj _ => error "initcontext_pbl: uncovered case"
wneuper@59261
   152
  	val pblID =
wneuper@59405
   153
  	  if pI' = Celem.e_pblID 
wneuper@59261
   154
  		then (* TODO.WN051125 (#init o get_pbt) pI *) takelast (2, pI)
wneuper@59261
   155
  		else pI'
wneuper@59269
   156
  	val {ppc, where_, prls, ...} = Specify.get_pbt pblID
wneuper@59592
   157
  	val (model_ok, (pbl, pre)) = Specify.match_itms_oris (Celem.assoc_thy "Isac_Knowledge") probl (ppc, where_, prls) os
wneuper@59261
   158
  in
wneuper@59261
   159
    (model_ok, pblID, hdl, pbl, pre)
wneuper@59261
   160
  end
neuper@37906
   161
wneuper@59261
   162
fun initcontext_met pt (p,_) =
wneuper@59261
   163
  let
wneuper@59261
   164
    val (meth, os, mI, mI') =
wneuper@59276
   165
      case Ctree.get_obj I pt p of
wneuper@59276
   166
        Ctree.PblObj {meth, origin = (os, (_, _, mI), _), spec=(_, _, mI'), ...} => (meth, os, mI, mI')
wneuper@59276
   167
      | Ctree.PrfObj _ => error "initcontext_met: uncovered case"
wneuper@59405
   168
  	val metID = if mI' = Celem.e_metID 
wneuper@59261
   169
  		    then (*TODO.WN051125 (#init o get_pbt) pI *) takelast (2, mI)
wneuper@59261
   170
  		    else mI'
wneuper@59269
   171
  	val {ppc, pre, prls, scr, ...} = Specify.get_met metID
wneuper@59592
   172
  	val (model_ok, (pbl, pre)) = Specify.match_itms_oris (Celem.assoc_thy "Isac_Knowledge") meth (ppc,pre,prls) os
wneuper@59261
   173
  in
wneuper@59261
   174
    (model_ok, metID, scr, pbl, pre)
wneuper@59261
   175
  end
neuper@37906
   176
wneuper@59261
   177
(* match the model of a problem at pos p with the model-pattern of the problem with pblID *)
wneuper@59276
   178
fun context_pbl pI pt p =
wneuper@59261
   179
  let
wneuper@59261
   180
    val (probl, os, hdl) =
wneuper@59276
   181
      case Ctree.get_obj I pt p of
wneuper@59276
   182
        Ctree.PblObj {probl,origin = (os, _, hdl),...} => (probl, os, hdl)
wneuper@59276
   183
      | Ctree.PrfObj _ => error "context_pbl: uncovered case"
wneuper@59269
   184
  	val {ppc,where_,prls,...} = Specify.get_pbt pI
wneuper@59592
   185
  	val (model_ok, (pbl, pre)) = Specify.match_itms_oris (Celem.assoc_thy "Isac_Knowledge") probl (ppc,where_,prls) os
wneuper@59261
   186
  in
wneuper@59261
   187
    (model_ok, pI, hdl, pbl, pre)
wneuper@59261
   188
  end
neuper@37906
   189
wneuper@59276
   190
fun context_met mI pt p =
wneuper@59261
   191
  let
wneuper@59261
   192
    val (meth, os) =
wneuper@59276
   193
      case Ctree.get_obj I pt p of
wneuper@59276
   194
        Ctree.PblObj {meth, origin = (os, _, _),...} => (meth, os)
wneuper@59276
   195
      | Ctree.PrfObj _ => error "context_met: uncovered case"
wneuper@59269
   196
  	val {ppc,pre,prls,scr,...} = Specify.get_met mI
wneuper@59592
   197
  	val (model_ok, (pbl, pre)) = Specify.match_itms_oris (Celem.assoc_thy "Isac_Knowledge") meth (ppc,pre,prls) os
wneuper@59261
   198
  in
wneuper@59261
   199
    (model_ok, mI, scr, pbl, pre)
wneuper@59261
   200
  end
neuper@37906
   201
wneuper@59261
   202
fun tryrefine pI pt (p,_) =
wneuper@59261
   203
  let
wneuper@59261
   204
    val (probl, os, hdl) =
wneuper@59276
   205
      case Ctree.get_obj I pt p of
wneuper@59276
   206
        Ctree.PblObj {probl, origin = (os, _, hdl), ...} => (probl, os, hdl)
wneuper@59276
   207
      | Ctree.PrfObj _ => error "context_met: uncovered case"
wneuper@59261
   208
  in
wneuper@59592
   209
    case Specify.refine_pbl (Celem.assoc_thy "Isac_Knowledge") pI probl of
wneuper@59261
   210
  	  NONE => (*copy from context_pbl*)
wneuper@59261
   211
  	    let
wneuper@59269
   212
  	      val {ppc,where_,prls,...} = Specify.get_pbt pI
wneuper@59592
   213
  	      val (_, (pbl, pre)) = Specify.match_itms_oris (Celem.assoc_thy "Isac_Knowledge") probl (ppc,where_,prls) os
wneuper@59261
   214
        in
wneuper@59261
   215
          (false, pI, hdl, pbl, pre)
wneuper@59261
   216
        end
wneuper@59261
   217
  	| SOME (pI, (pbl, pre)) => (true, pI, hdl, pbl, pre) 
wneuper@59261
   218
  end
neuper@37906
   219
wneuper@59276
   220
fun detailstep pt (pos as (p, _)) = 
neuper@55471
   221
  let 
wneuper@59276
   222
    val nd = Ctree.get_nd pt p
wneuper@59276
   223
    val cn = Ctree.children nd
neuper@55471
   224
  in 
neuper@55471
   225
    if null cn 
wneuper@59261
   226
    then
wneuper@59571
   227
      if (Tactic.is_rewset o (Ctree.get_obj Ctree.g_tac nd)) [(*root of nd*)]
wneuper@59273
   228
      then Solve.detailrls pt pos
walther@59694
   229
      else ("no-Rewrite_Set...", Ctree.EmptyPtree, Pos.e_pos')
walther@59694
   230
    else ("donesteps", pt, (p @ [length (Ctree.children (Ctree.get_nd pt p))], Pos.Res)) 
neuper@55471
   231
  end;
neuper@37906
   232
neuper@37906
   233
wneuper@59261
   234
(*** for mathematics authoring on sml-toplevel; no XML ***)
neuper@37906
   235
neuper@37906
   236
type NEW = int list;
neuper@37906
   237
wneuper@59261
   238
(* 15.8.03 for me with loc_specify/solve, nxt_specify/solve
wneuper@59261
   239
   delete as soon as TESTg_form -> _mout_ dropped           *)
neuper@37906
   240
fun TESTg_form ptp =
wneuper@59261
   241
  let
wneuper@59265
   242
    val (form, _, _) = Chead.pt_extract ptp
wneuper@59261
   243
  in
wneuper@59261
   244
    case form of
wneuper@59416
   245
      Ctree.Form t => Generate.FormKF (Rule.term2str t)
wneuper@59276
   246
    | Ctree.ModSpec (_, p_, _, gfr, pre, _) =>
wneuper@59271
   247
      Generate.PpcKF (
walther@59694
   248
        (case p_ of Pos.Pbl => Generate.Problem []
walther@59694
   249
          | Pos.Met => Generate.Method []
wneuper@59313
   250
          | _ => error "TESTg_form: uncovered case",
wneuper@59592
   251
 			Specify.itms2itemppc (Celem.assoc_thy"Isac_Knowledge") gfr pre))
wneuper@59267
   252
   end
neuper@37906
   253
wneuper@59261
   254
(* create a calc-tree; for use within sml: thus "^^^" NOT decoded to "^" etc;
wneuper@59261
   255
   compare "fun CalcTree" which DOES decode                        *)
wneuper@59261
   256
fun CalcTreeTEST [(fmz, sp)] = 
neuper@42011
   257
  let
walther@59764
   258
    val ((pt, p), tacis) = SpecifyNEW.nxt_specify_init_calc (fmz, sp)
wneuper@59571
   259
	  val tac = case tacis of [] => Tactic.Empty_Tac | _ => (#1 o hd) tacis
neuper@42011
   260
	  val f = TESTg_form (pt,p)
walther@59749
   261
  in (p, []:NEW, f, tac, Telem.Sundef, pt) end
wneuper@59261
   262
| CalcTreeTEST _ = error "CalcTreeTEST: uncovered case"
neuper@37906
   263
       
neuper@37906
   264
(*for tests > 15.8.03 after separation setnexttactic / nextTac:
neuper@37906
   265
  external view: me should be used by math-authors as done so far
neuper@37906
   266
  internal view: loc_specify/solve, nxt_specify/solve used
neuper@37906
   267
                 i.e. same as in setnexttactic / nextTac*)
neuper@37906
   268
(*ENDE TESTPHASE 08/10.03:
walther@59804
   269
  NEW loeschen, eigene Version von Step.by_tactic, do_next
neuper@37906
   270
  meNEW, CalcTreeTEST: tac'_ -replace-> tac, remove [](cid) *)
neuper@37906
   271
walther@59749
   272
fun me (*(Empty_Tac) p _ _ = raise ERROR ("me:  Empty_Tac at " ^ pos'2str p)
walther@59749
   273
  | me*) tac p _(*NEW remove*) pt =
wneuper@59592
   274
    let 
wneuper@59592
   275
      val (pt, p) = 
walther@59804
   276
  	    (*Step.by_tactic is here for testing by me; do_next would suffice in me*)
walther@59804
   277
  	    case Step.by_tactic tac (pt, p) of
wneuper@59592
   278
  		    ("ok", (_, _, ptp)) => ptp
wneuper@59592
   279
  	    | ("unsafe-ok", (_, _, ptp)) => ptp
wneuper@59592
   280
  	    | ("not-applicable",_) => (pt, p)
wneuper@59592
   281
  	    | ("end-of-calculation", (_, _, ptp)) => ptp
wneuper@59592
   282
  	    | ("failure", _) => error "sys-error"
walther@59804
   283
  	    | _ => error "me: uncovered case Step.by_tactic"
wneuper@59592
   284
  	  val (_, ts) =
walther@59655
   285
        (*step's correct ctree is not tested in me, but in autocalc*)
walther@59765
   286
  	    (case Step.do_next p ((pt, Pos.e_pos'), []) of
wneuper@59592
   287
  		    ("ok", (ts as (_, _, _) :: _, _, _)) => ("", ts)
wneuper@59592
   288
  	    | ("helpless", _) => ("helpless: cannot propose tac", [])
walther@59762
   289
  	    | ("dummy", (ts as (_, _, _) :: _, _, _)) => ("", ts) (*intermed.from specify*)
wneuper@59592
   290
  	    | ("end-of-calculation", (ts, _, _)) => ("", ts)
walther@59762
   291
  	    | _ => error  "me: uncovered case do_next")
wneuper@59592
   292
  	  val tac = 
wneuper@59592
   293
        case ts of 
wneuper@59592
   294
          tacis as (_::_) => let val (tac, _, _) = last_elem tacis in tac end 
walther@59694
   295
  		  | _ => if p = ([], Pos.Res) then Tactic.End_Proof' else Tactic.Empty_Tac;
walther@59654
   296
    in
walther@59804
   297
      (p, [] : NEW, TESTg_form (pt, p) (* form output comes from Step.by_tactic *), 
walther@59749
   298
  	    tac, Telem.Sundef, pt)
wneuper@59592
   299
    end
neuper@37906
   300
wneuper@59261
   301
(* for quick test-print-out, until 'type inout' is removed *)
wneuper@59313
   302
fun f2str (Generate.FormKF cterm') = cterm'
walther@59749
   303
  | f2str _ = error "f2str: uncovered case in fun.def.";                           
neuper@37906
   304
walther@59749
   305
(**)end(**)