src/Tools/isac/Interpret/mathengine.sml
author Walther Neuper <wneuper@ist.tugraz.at>
Tue, 22 Nov 2016 10:42:21 +0100
changeset 59261 61a1bcd51e0e
parent 59239 3c4cc053f553
child 59265 ee68ccda7977
permissions -rw-r--r--
added structure Math_Engine : MATH_ENGINE
     1 (* The _functional_ mathematics engine, ie. without a state.
     2    Input and output are Isabelle's formulae as strings.
     3    authors: Walther Neuper 2000
     4    (c) due to copyright terms
     5 
     6 use"mathengine.sml";
     7 *)
     8 
     9 signature MATH_ENGINE =
    10 sig
    11   type NEW (* TODO: refactor "fun me" with calcstate and remove *)
    12   val me : tac'_ -> pos' -> NEW -> ptree -> pos' * NEW * mout * tac'_ * safe * ptree
    13   val autocalc :
    14      pos' list -> pos' -> (ptree * pos') * taci list -> auto -> string * pos' list * (ptree * pos')
    15   val locatetac :
    16     tac -> ptree * (posel list * pos_) -> string * (taci list * pos' list * (ptree * pos'))
    17   val step : pos' -> calcstate -> string * calcstate'
    18   val detailstep :
    19     ptree -> pos' -> string * ptree * pos' val get_pblID : ptree * pos' -> pblID option
    20 
    21   val initcontext_met : ptree -> pos' -> bool * string list * scr * itm list * (bool * term) list
    22   val initcontext_pbl : ptree -> pos' -> bool * string list * term * itm list * (bool * term) list
    23   val context_met : metID -> ptree -> pos -> bool * metID * scr * itm list * (bool * term) list
    24   val context_pbl : pblID -> ptree -> pos -> bool * pblID * term * itm list * (bool * term) list
    25   val set_method : metID -> ptree * pos' -> ptree * ocalhd
    26   val set_problem : pblID -> ptree * pos' -> ptree * ocalhd
    27   val set_theory : thyID -> ptree * pos' -> ptree * ocalhd
    28   val tryrefine : pblID -> ptree -> pos' -> bool * pblID * term * itm list * (bool * term) list
    29 
    30 (*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
    31   type nxt_
    32   type lOc_
    33   val CalcTreeTEST : fmz list -> pos' * NEW * mout * (string * tac) * safe * ptree
    34   val f2str : mout -> cterm'
    35   val loc_solve_ : string * tac_ -> ptree * (pos * pos_) -> lOc_
    36 ( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
    37 end
    38 
    39 (**)
    40 structure Math_Engine(**): MATH_ENGINE(**) =
    41 struct
    42 (**)
    43 
    44 fun get_pblID (pt, (p, _):pos') =
    45   let val p' = par_pblobj pt p
    46   	val (_, pI, _) = get_obj g_spec pt p'
    47   	val (_, (_, oI, _), _) = get_obj g_origin pt p'
    48   in
    49     if pI <> e_pblID
    50     then SOME pI
    51     else
    52       if oI <> e_pblID then SOME oI else NONE end;
    53 
    54 (* introduced for test only *)
    55 val e_tac_ = Tac_ (Pure.thy, "", "", "");
    56 datatype lOc_ =
    57   ERror of string         (*after loc_specify, loc_solve*)
    58 | UNsafe of calcstate'    (*after loc_specify, loc_solve*)
    59 | Updated of calcstate';  (*after loc_specify, loc_solve*)
    60 
    61 fun loc_specify_ m (pt,pos) =
    62   let
    63     val (p, _, f, _, _, pt) = specify m pos [] pt;
    64   in
    65     case f of (Error' (Error_ e)) => ERror e | _ => Updated ([], [], (pt,p))
    66   end
    67 
    68 (* TODO push return-value cs' into solve and rename solve->loc_solve?_? *)
    69 fun loc_solve_ m (pt, pos) =
    70   let
    71     val (msg, cs') = solve m (pt, pos);
    72   in
    73     case msg of "ok" => Updated cs' | msg => ERror msg 
    74   end
    75 
    76 datatype nxt_ =
    77 	HElpless  (**)
    78 | Nexts of calcstate; (**)
    79 
    80 (* locate a tactic in a script and apply it if possible;
    81    report of tacs' applicability in tacis; pt is dropped in setNextTactic*)
    82 fun locatetac _ (ptp as (_, ([], Res))) = ("end-of-calculation", ([], [], ptp))
    83   | locatetac tac (ptp as (pt, p)) =
    84     let
    85       val (mI, m) = mk_tac'_ tac;
    86     in
    87       case applicable_in p pt m of
    88 	      Notappl _ => ("not-applicable", ([],[],  ptp):calcstate')
    89 	    | Appl m =>
    90 	      let 
    91           val x = if member op = specsteps mI
    92 		        then loc_specify_ m ptp else loc_solve_ (mI,m) ptp
    93 	      in 
    94 	        case x of 
    95 		        ERror _ => ("failure", ([], [], ptp))
    96 		        (*FIXXXXXME: loc_specify_, loc_solve_ TOGETHER with dropping meOLD+detail.sml*)
    97 		      | UNsafe cs' => ("unsafe-ok", cs')
    98 		      | Updated (cs' as (_, _, (_, p'))) => (*ev.SEVER.tacs like Begin_Trans*)
    99 		        (if p' = ([],Res) then "end-of-calculation" else "ok", cs')
   100             (*for SEVER.tacs user to ask ? *)
   101 	      end
   102     end
   103 
   104 (* iterated by nxt_me; there (the resulting) ptp dropped
   105    may call nxt_solve Apply_Method --- thus evaluated here after solve.sml *)
   106 fun nxt_specify_ (ptp as (pt, (p, p_))) =
   107   let
   108     val (pblobj, meth, origin, oris, dI', pI', mI', probl, dI, pI, mI) = 
   109   	  case get_obj I pt p of
   110   	    pblobj as (PblObj {meth, origin = origin as (oris, (dI', pI', mI'), _),
   111   		  probl, spec = (dI, pI, mI), ...}) => (pblobj, meth, origin, oris, dI', pI', mI', probl, dI, pI, mI)
   112       | PrfObj _ => error "nxt_specify_: not on PrfObj"
   113   in 
   114     if just_created_ pblobj (*by Subproblem*) andalso origin <> e_origin
   115     then 
   116       case mI' of
   117         ["no_met"] => nxt_specif (Refine_Tacitly pI') (pt, (p, Pbl))
   118       | _ => nxt_specif Model_Problem (pt, (p,Pbl))
   119     else 
   120       let 
   121         val cpI = if pI = e_pblID then pI' else pI;
   122   	    val cmI = if mI = e_metID then mI' else mI;
   123   	    val {ppc, prls, where_, ...} = get_pbt cpI;
   124   	    val pre = check_preconds "thy 100820" prls where_ probl;
   125   	    val pb = foldl and_ (true, map fst pre);
   126   	    (*FIXME.WN0308:    ~~~~~: just check true in itms of pbl/met?*)
   127   	    val (_, tac) =
   128   	      nxt_spec p_ pb oris (dI', pI', mI') (probl, meth) (ppc, (#ppc o get_met) cmI) (dI, pI, mI)
   129       in
   130         case tac of
   131   	      Apply_Method mI => 
   132   	        nxt_solv (Apply_Method' (mI, NONE, e_istate, e_ctxt)) (e_istate, e_ctxt) ptp
   133   	    | _ => nxt_specif tac ptp
   134   	  end
   135   end
   136 
   137 (* specify a new method; WN0512 impl.incomplete, see 'nxt_specif (Specify_Method ' *)
   138 fun set_method (mI:metID) ptp =
   139   let
   140     val (mits, pt, p) =
   141       case nxt_specif (Specify_Method mI) ptp of
   142         ([(_, Specify_Method' (_, _, mits), _)], [], (pt, (p,_))) => (mits, pt, p)
   143       | _ => error "set_method: case 1 uncovered"
   144   	val pre = []        (*...from Specify_Method'*)
   145   	val complete = true (*...from Specify_Method'*)
   146   	(*from Specify_Method'  ?   vvv,  vvv ?*)
   147     val (hdf, spec) =
   148       case get_obj I pt p of
   149         PblObj {origin = (_, _, hdf), spec, ...} => (hdf, spec)
   150       | PrfObj _ => error "set_method: case 2 uncovered"
   151   in
   152     (pt, (complete, Met, hdf, mits, pre, spec) : ocalhd)
   153   end
   154 
   155 (* specify a new problem; WN0512 impl.incomplete, see 'nxt_specif (Specify_Problem ' *)
   156 fun set_problem pI (ptp: ptree * pos') =
   157   let
   158     val (complete, pits, pre, pt, p) =
   159       case nxt_specif (Specify_Problem pI) ptp of
   160         ([(_, Specify_Problem' (_, (complete, (pits, pre))),_)], _, (pt, (p,_)))
   161           => (complete, pits, pre, pt, p)
   162       | _ => error "set_problem: case 1 uncovered"
   163     (*from Specify_Problem' ?   vvv,  vvv ?*)
   164     val (hdf, spec) =
   165       case get_obj I pt p of
   166         PblObj {origin = (_, _, hdf), spec, ...} => (hdf, spec)
   167       | PrfObj _ => error "set_problem: case 2 uncovered"
   168   in
   169     (pt, (complete, Pbl, hdf, pits, pre, spec) : ocalhd)
   170   end
   171 
   172 fun set_theory (tI:thyID) (ptp: ptree * pos') =
   173   let
   174     val (complete, pits, pre, pt, p) =
   175       case nxt_specif (Specify_Theory tI) ptp of  
   176         ([(_, Specify_Problem' (_, (complete, (pits, pre))), _)], _, (pt, (p, _)))
   177           => (complete, pits, pre, pt, p)
   178       | _ => error "set_theory: case 1 uncovered"
   179   	(*from Specify_Theory'  ?   vvv,  vvv ?*)
   180     val (hdf, spec) =
   181       case get_obj I pt p of
   182         PblObj {origin = (_, _, hdf), spec, ...} => (hdf, spec)
   183       | PrfObj _ => error "set_theory: case 2 uncovered"
   184   in (pt, (complete, Pbl, hdf, pits, pre, spec) : ocalhd) end;
   185 
   186 (* does a step forward; returns tactic used, ctree updated.
   187    TODO.WN0512 redesign after specify-phase became more separated from solve-phase *)
   188 fun step ((ip as (_,p_)):pos') ((ptp as (pt,p), tacis):calcstate) =
   189   let val pIopt = get_pblID (pt,ip);
   190   in
   191     if ip = ([],Res)
   192     then ("end-of-calculation", (tacis, [], ptp):calcstate') 
   193     else
   194       case tacis of
   195         (_::_) => 
   196           if ip = p (*the request is done where ptp waits for*)
   197           then 
   198             let val (pt',c',p') = generate tacis (pt,[],p)
   199   	        in ("ok", (tacis, c', (pt', p'))) end
   200           else (case (if member op = [Pbl, Met] p_
   201   	                  then nxt_specify_ (pt, ip) else nxt_solve_ (pt, ip))
   202   	                  handle ERROR msg => (writeln ("*** " ^ msg);
   203   	                    ([], [], ptp)) (*e.g. Add_Given "equality///"*) of
   204   	              cs as ([],_,_) => ("helpless", cs)
   205   	            | cs => ("ok", cs))
   206       | _ => (case pIopt of
   207   	            NONE => ("no-fmz-spec", ([], [], ptp))
   208   	          | SOME _ =>                         (*vvvvvv: Apply_Method without init_form*)
   209   	            (case if member op = [Pbl,Met] p_ andalso is_none (get_obj g_env pt (fst p))
   210   		                then nxt_specify_ (pt, ip) 
   211                       else (nxt_solve_ (pt,ip))
   212   	                    handle ERROR msg => (writeln ("*** " ^ msg);
   213   	                      ([],[],ptp)) (*e.g. Add_Given "equality///"*) of
   214   	               cs as ([],_,_) =>("helpless", cs) (*FIXXME del.handle*)
   215   		           | cs => ("ok", cs)))
   216   end
   217 
   218 (* does several steps within one calculation as given by "type auto";
   219    the steps may arbitrarily go into and leave different phases, 
   220    i.e. specify-phase and solve-phase
   221    TODO.WN0512 ? redesign after the phases have been more separated
   222    at the fron-end in 05: 
   223    eg. CompleteCalcHead could be done by a separate fun !!!*)
   224 fun autocalc c ip cs (Step s) =
   225     if s <= 1
   226     then
   227       let val (str, (_, c', ptp)) = step ip cs;      (* autoord = 1, probably does 1 step too much*)
   228 	    in (str, c@c', ptp) end
   229     else
   230       let val (str, (_, c', ptp as (_, p))) = step ip cs;
   231       in
   232         if str = "ok" 
   233         then autocalc (c@c') p (ptp, []) (Step (s - 1))
   234         else (str, c@c', ptp) end
   235     (* handles autoord <= 3, autoord > 3 handled by all_solve, complete_solve *)
   236   | autocalc c (pos as (_, p_)) ((pt, _), _(*tacis could help 1x in solve*)) auto =
   237     if autoord auto > 3 andalso just_created (pt, pos)
   238     then
   239       let val ptp = all_modspec (pt, pos);
   240       in all_solve auto c ptp end                    (*... auto = 4 | 5 | 6 *)
   241     else
   242       if member op = [Pbl, Met] p_
   243       then
   244         if not (is_complete_mod (pt, pos))
   245    	    then
   246    	      let val ptp = complete_mod (pt, pos)      (*... auto = 2 | 3 *)
   247    		    in
   248    		      if autoord auto < 3 then ("ok", c, ptp)
   249    		      else
   250    		       if not (is_complete_spec ptp)
   251    			     then
   252    			       let val ptp = complete_spec ptp
   253    			       in
   254    			         if autoord auto = 3 then ("ok", c, ptp) else all_solve auto c ptp
   255    			       end
   256    			     else if autoord auto = 3 then ("ok", c, ptp) else all_solve auto c ptp 
   257    		    end
   258    	    else 
   259    		    if not (is_complete_spec (pt,pos))
   260    		    then
   261    		      let val ptp = complete_spec (pt, pos)
   262    		      in
   263    		        if autoord auto = 3 then ("ok", c, ptp) else all_solve auto c ptp
   264    		      end
   265    		    else
   266    		      if autoord auto = 3 then ("ok", c, (pt, pos)) else all_solve auto c (pt, pos)
   267    	  else complete_solve auto c (pt, pos);
   268 
   269 (*.initialiye matching; before 'tryMatch' get the pblID to match with:
   270    if no pbl has been specified, take the init from origin.*)
   271 fun initcontext_pbl pt (p, _) =
   272   let
   273     val (probl, os, pI, hdl, pI') =
   274       case get_obj I pt p of
   275         PblObj {probl, origin = (os, (_, pI, _), hdl), spec=(_, pI', _), ...}
   276           => (probl, os, pI, hdl, pI')
   277       | PrfObj _ => error "initcontext_pbl: uncovered case"
   278   	val pblID =
   279   	  if pI' = e_pblID 
   280   		then (* TODO.WN051125 (#init o get_pbt) pI *) takelast (2, pI)
   281   		else pI'
   282   	val {ppc, where_, prls, ...} = get_pbt pblID
   283   	val (model_ok, (pbl, pre)) = match_itms_oris (assoc_thy "Isac") probl (ppc,where_,prls) os
   284   in
   285     (model_ok, pblID, hdl, pbl, pre)
   286   end
   287 
   288 fun initcontext_met pt (p,_) =
   289   let
   290     val (meth, os, mI, mI') =
   291       case get_obj I pt p of
   292         PblObj {meth, origin = (os, (_, _, mI), _), spec=(_, _, mI'), ...} => (meth, os, mI, mI')
   293       | PrfObj _ => error "initcontext_met: uncovered case"
   294   	val metID = if mI' = e_metID 
   295   		    then (*TODO.WN051125 (#init o get_pbt) pI *) takelast (2, mI)
   296   		    else mI'
   297   	val {ppc, pre, prls, scr, ...} = get_met metID
   298   	val (model_ok, (pbl, pre)) = match_itms_oris (assoc_thy "Isac") meth (ppc,pre,prls) os
   299   in
   300     (model_ok, metID, scr, pbl, pre)
   301   end
   302 
   303 (* match the model of a problem at pos p with the model-pattern of the problem with pblID *)
   304 fun context_pbl pI pt (p:pos) =
   305   let
   306     val (probl, os, hdl) =
   307       case get_obj I pt p of
   308         PblObj {probl,origin = (os, _, hdl),...} => (probl, os, hdl)
   309       | PrfObj _ => error "context_pbl: uncovered case"
   310   	val {ppc,where_,prls,...} = get_pbt pI
   311   	val (model_ok, (pbl, pre)) = match_itms_oris (assoc_thy "Isac") probl (ppc,where_,prls) os
   312   in
   313     (model_ok, pI, hdl, pbl, pre)
   314   end
   315 
   316 fun context_met mI pt (p:pos) =
   317   let
   318     val (meth, os) =
   319       case get_obj I pt p of
   320         PblObj {meth, origin = (os, _, _),...} => (meth, os)
   321       | PrfObj _ => error "context_met: uncovered case"
   322   	val {ppc,pre,prls,scr,...} = get_met mI
   323   	val (model_ok, (pbl, pre)) = match_itms_oris (assoc_thy "Isac") meth (ppc,pre,prls) os
   324   in
   325     (model_ok, mI, scr, pbl, pre)
   326   end
   327 
   328 fun tryrefine pI pt (p,_) =
   329   let
   330     val (probl, os, hdl) =
   331       case get_obj I pt p of
   332         PblObj {probl, origin = (os, _, hdl), ...} => (probl, os, hdl)
   333       | PrfObj _ => error "context_met: uncovered case"
   334   in
   335     case refine_pbl (assoc_thy "Isac") pI probl of
   336   	  NONE => (*copy from context_pbl*)
   337   	    let
   338   	      val {ppc,where_,prls,...} = get_pbt pI
   339   	      val (_, (pbl, pre)) = match_itms_oris (assoc_thy "Isac") probl (ppc,where_,prls) os
   340         in
   341           (false, pI, hdl, pbl, pre)
   342         end
   343   	| SOME (pI, (pbl, pre)) => (true, pI, hdl, pbl, pre) 
   344   end
   345 
   346 fun detailstep pt (pos as (p, _):pos') = 
   347   let 
   348     val nd = get_nd pt p
   349     val cn = children nd
   350   in 
   351     if null cn 
   352     then
   353       if (is_rewset o (get_obj g_tac nd)) [(*root of nd*)]
   354       then detailrls pt pos
   355       else ("no-Rewrite_Set...", EmptyPtree, e_pos')
   356     else ("donesteps", pt, (p @ [length (children (get_nd pt p))], Res)) 
   357   end;
   358 
   359 
   360 (*** for mathematics authoring on sml-toplevel; no XML ***)
   361 
   362 type NEW = int list;
   363 
   364 (* 15.8.03 for me with loc_specify/solve, nxt_specify/solve
   365    delete as soon as TESTg_form -> _mout_ dropped           *)
   366 fun TESTg_form ptp =
   367   let
   368     val (form, _, _) = pt_extract ptp
   369   in
   370     case form of
   371       Form t => Form' (FormKF (~1,EdUndef,0,Nundef,term2str t))
   372     | ModSpec (_,p_, _, gfr, pre, _) => 
   373       Form' (PpcKF (0, EdUndef, 0, Nundef, 
   374         (case p_ of Pbl => Problem [] | Met => Method [] | _ => error "TESTg_form: uncovered case",
   375  			itms2itemppc (assoc_thy"Isac") gfr pre)))
   376    end;
   377 
   378 (* create a calc-tree; for use within sml: thus "^^^" NOT decoded to "^" etc;
   379    compare "fun CalcTree" which DOES decode                        *)
   380 fun CalcTreeTEST [(fmz, sp)] = 
   381   let
   382     val ((pt, p), tacis) = nxt_specify_init_calc (fmz, sp)
   383 	  val tac = case tacis of [] => Empty_Tac | _ => (#1 o hd) tacis
   384 	  val f = TESTg_form (pt,p)
   385   in (p, []:NEW, f, (tac2IDstr tac, tac), Sundef, pt) end
   386 | CalcTreeTEST _ = error "CalcTreeTEST: uncovered case"
   387        
   388 (*for tests > 15.8.03 after separation setnexttactic / nextTac:
   389   external view: me should be used by math-authors as done so far
   390   internal view: loc_specify/solve, nxt_specify/solve used
   391                  i.e. same as in setnexttactic / nextTac*)
   392 (*ENDE TESTPHASE 08/10.03:
   393   NEW loeschen, eigene Version von locatetac, step
   394   meNEW, CalcTreeTEST: tac'_ -replace-> tac, remove [](cid) *)
   395 
   396 fun me (_, tac) p _(*NEW remove*) pt =
   397   let 
   398     val (pt, p) = 
   399 	    (*locatetac is here for testing by me; step would suffice in me*)
   400 	    case locatetac tac (pt,p) of
   401 		    ("ok", (_, _, ptp)) => ptp
   402 	    | ("unsafe-ok", (_, _, ptp)) => ptp
   403 	    | ("not-applicable",_) => (pt, p)
   404 	    | ("end-of-calculation", (_, _, ptp)) => ptp
   405 	    | ("failure", _) => error "sys-error"
   406 	    | _ => error "me: uncovered case"
   407 	  val (_, ts) =
   408 	    (case step p ((pt, e_pos'),[]) of
   409 		    ("ok", (ts as (_, _, _) :: _, _, _)) => ("", ts)
   410 	    | ("helpless", _) => ("helpless: cannot propose tac", [])
   411 	    | ("no-fmz-spec", _) => error "no-fmz-spec"
   412 	    | ("end-of-calculation", (ts, _, _)) => ("", ts)
   413 	    | _ => error "me: uncovered case")
   414 	      handle ERROR msg => raise ERROR msg
   415 	  val tac = 
   416       case ts of 
   417         tacis as (_::_) => let val (tac, _, _) = last_elem tacis in tac end 
   418 		  | _ => if p = ([], Res) then End_Proof' else Empty_Tac;
   419   in (p, [] : NEW, TESTg_form (pt, p) (* form output comes from locatetac *), 
   420 	   (tac2IDstr tac, tac):tac'_, Sundef, pt)
   421   end
   422 
   423 (* for quick test-print-out, until 'type inout' is removed *)
   424 fun f2str (Form' (FormKF (_, _, _, _, cterm'))) = cterm';
   425 
   426 end