src/Tools/isac/Interpret/inform.sml
author Mathias Lehnfeld <s1210629013@students.fh-hagenberg.at>
Fri, 13 Jun 2014 09:55:49 +0200
changeset 55443 46613d0a9fc9
parent 55415 f8d44cb86330
child 55485 b10eb9fd4c02
permissions -rw-r--r--
trivial removals of the_generic_context done.
only one missing (~~/src/Tools/isac/scrtools.sml:490; fun prep_rls)
Tests and parallel calls to autoCalculate working properly, but they are not technically thread-safe yet.
     1 (* Handle user-input during the specify- and the solve-phase. 
     2    author: Walther Neuper
     3    0603
     4    (c) due to copyright terms
     5 
     6 use"ME/inform.sml";
     7 use"inform.sml";
     8 *)
     9 
    10 signature INFORM =
    11   sig
    12 
    13     type icalhd
    14 
    15    (* type iitem *)
    16     datatype
    17       iitem =
    18           Find of cterm' list
    19         | Given of cterm' list
    20         | Relate of cterm' list
    21     type imodel
    22     val imodel2fstr : iitem list -> (string * cterm') list
    23 
    24     
    25     val Isac : 'a -> theory
    26     val appl_add' :
    27        theory' ->
    28        SpecifyTools.ori list ->
    29        SpecifyTools.itm list ->
    30        ('a * (Term.term * Term.term)) list ->
    31        string * cterm' -> SpecifyTools.itm
    32   (*  val appl_adds :
    33        theory' ->
    34        SpecifyTools.ori list ->
    35        SpecifyTools.itm list ->
    36        (string * (Term.term * Term.term)) list ->
    37        (string * string) list -> SpecifyTools.itm list *)
    38    (* val cas_input : string -> ptree * ocalhd *)
    39    (* val cas_input_ :
    40        spec ->
    41        (Term.term * Term.term list) list ->
    42        pblID * SpecifyTools.itm list * metID * SpecifyTools.itm list *
    43        (bool * Term.term) list  *)
    44     val compare_step :
    45        calcstate' -> Term.term -> string * calcstate'
    46    (* val concat_deriv :
    47        'a * ((Term.term * Term.term) list -> Term.term * Term.term -> bool)
    48        ->
    49        rls ->
    50        rule list ->
    51        Term.term ->
    52        Term.term ->
    53        bool * (Term.term * rule * (Term.term * Term.term list)) list *)
    54     val dropwhile' :   (* systest/auto-inform.sml *)
    55        ('a -> 'b -> bool) -> 'a list -> 'b list -> 'a list * 'b list
    56    (* val dtss2itm_ :
    57        pbt_ list ->
    58        Term.term * Term.term list ->
    59        int list * bool * string * SpecifyTools.itm_ *)
    60    (* val e_icalhd : icalhd *)
    61     val eq7 : ''a * ''b -> ''a * (''b * 'c) -> bool
    62     val equal : ''a -> ''a -> bool
    63    (* val filter_dsc :
    64        SpecifyTools.ori list -> SpecifyTools.itm -> SpecifyTools.ori list *)
    65    (* val filter_sep : ('a -> bool) -> 'a list -> 'a list * 'a list *)
    66    (* val flattup2 : 'a * ('b * 'c * 'd * 'e) -> 'a * 'b * 'c * 'd * 'e *)
    67    (* val fstr2itm_ :
    68        theory ->
    69        (''a * (Term.term * Term.term)) list ->
    70        ''a * string -> int list * bool * ''a * SpecifyTools.itm_ *)
    71     val inform :
    72        calcstate' -> cterm' -> string * calcstate'   
    73     val input_icalhd : ptree -> icalhd -> ptree * ocalhd
    74    (* val is_Par : SpecifyTools.itm -> bool *)
    75    (* val is_casinput : cterm' -> fmz -> bool *)
    76    (* val is_e_ts : Term.term list -> bool *)
    77    (* val itms2fstr : SpecifyTools.itm -> string * string *)
    78    (* val mk_tacis :
    79        rew_ord' * 'a ->
    80        rls ->
    81        Term.term * rule * (Term.term * Term.term list) ->
    82        tac * tac_ * (pos' * istate)      *)
    83     val oris2itms :
    84        'a -> int -> SpecifyTools.ori list -> SpecifyTools.itm list
    85    (* val par2fstr : SpecifyTools.itm -> string * cterm' *)
    86    (* val parsitm : theory -> SpecifyTools.itm -> SpecifyTools.itm *)
    87     val rev_deriv' : 'a * rule * ('b * 'c) -> 'b * rule * ('a * 'c)
    88    (* val unknown_expl :
    89        theory' ->
    90        (string * (Term.term * Term.term)) list ->
    91        (string * string) list -> SpecifyTools.itm list *)
    92   end
    93 
    94 
    95 
    96 
    97 
    98 
    99 (***. handle an input calc-head .***)
   100 
   101 (*------------------------------------------------------------------(**)
   102 structure inform :INFORM =
   103 struct
   104 (**)------------------------------------------------------------------*)
   105 
   106 datatype iitem = 
   107   Given of cterm' list
   108 (*Where is never input*) 
   109 | Find  of cterm' list
   110 | Relate  of cterm' list;
   111 
   112 type imodel = iitem list;
   113 
   114 (*calc-head as input*)
   115 type icalhd =
   116      pos' *     (*the position of the calc-head in the calc-tree
   117 		 pos' as (p,p_) where p_ is neglected due to pos_ below*) 
   118      cterm' *   (*the headline*)
   119      imodel *   (*the model (without Find) of the calc-head*)
   120      pos_ *     (*model belongs to Pbl or Met*)
   121      spec;      (*specification: domID, pblID, metID*)
   122 val e_icalhd = (e_pos', "", [Given [""]], Pbl, e_spec): icalhd;
   123 
   124 fun is_casinput (hdf: cterm') ((fmz_, spec): fmz) =
   125     hdf <> "" andalso fmz_ = [] andalso spec = e_spec;
   126 
   127 (*.handle an input as into an algebra system.*)
   128 fun dtss2itm_ ppc (d, ts) =
   129     let val (f, (d, id)) = the (find_first ((curry op= d) o 
   130 					    (#1: (term * term) -> term) o
   131 					    (#2: pbt_ -> (term * term))) ppc)
   132     in ([1], true, f, Cor ((d, ts), (id, ts))) end;
   133 
   134 fun flattup2 (a,(b,c,d,e)) = (a,b,c,d,e);
   135 
   136 fun cas_input_ ((dI,pI,mI): spec) dtss = (*WN110515 reconsider thy..ctxt*)
   137   let
   138     val thy = assoc_thy dI
   139 	  val {ppc, ...} = get_pbt pI
   140 	  val its_ = map (dtss2itm_ ppc) dtss (*([1],true,"#Given",Cor (...))*)
   141 	  val its = add_id its_
   142 	  val pits = map flattup2 its
   143 	  val (pI, mI) =
   144       if mI <> ["no_met"]
   145       then (pI, mI)
   146 		  else
   147         case refine_pbl thy pI pits of
   148 			    SOME (pI,_) => (pI, (hd o #met o get_pbt) pI)
   149 			  | NONE => (pI, (hd o #met o get_pbt) pI)
   150 	  val {ppc, pre, prls, ...} = get_met mI
   151 	  val its_ = map (dtss2itm_ ppc) dtss (*([1],true,"#Given",Cor (...))*)
   152 	  val its = add_id its_
   153 	  val mits = map flattup2 its
   154 	  val pre = check_preconds thy prls pre mits
   155     val ctxt = e_ctxt (*WN110515 cas_input_ DOESNT WORK*)
   156   in (pI, pits: itm list, mI, mits: itm list, pre, ctxt) end;
   157 
   158 
   159 (* check if the input term is a CAScmd and return a ptree with a _complete_ calchead *)
   160 fun cas_input hdt =
   161   let val (h,argl) = strip_comb hdt
   162   in
   163     case assoc_cas (assoc_thy "Isac") h of
   164      NONE => NONE
   165    | SOME (spec as (dI,_,_), argl2dtss) =>
   166 	      let
   167           val dtss = argl2dtss argl
   168 	        val (pI, pits, mI, mits, pre, ctxt) = cas_input_ spec dtss
   169 	        val spec = (dI, pI, mI)
   170 	        val (pt,_) = 
   171 		        cappend_problem e_ptree [] (e_istate, e_ctxt) ([], e_spec) ([], e_spec, hdt)
   172 	        val pt = update_spec pt [] spec
   173 	        val pt = update_pbl pt [] pits
   174 	        val pt = update_met pt [] mits
   175 	        val pt = update_ctxt pt [] ctxt
   176 	      in SOME (pt, (true, Met, hdt, mits, pre, spec) : ocalhd) end
   177   end;
   178 
   179 (*lazy evaluation for (Thy_Info.get_theory "Isac")*)
   180 fun Isac _  = assoc_thy "Isac";
   181 
   182 (*re-parse itms with a new thy and prepare for checking with ori list*)
   183 fun parsitm dI (itm as (i,v,b,f, Cor ((d,ts),_)):itm) =
   184     (let
   185       val t = comp_dts (d,ts);
   186       val s = term_to_string''' dI t;
   187        (*this    ^ should raise the exn on unability of re-parsing dts*)
   188     in itm end
   189     handle _ => ((i,v,false,f, Syn (term2str t)):itm))
   190   | parsitm dI (itm as (i,v,b,f, Syn str)) =
   191     (let val t = (term_of o the o (parse dI)) str
   192     in (i,v,b,f, Par str) end
   193     handle _ => (i,v,b,f, Syn str))
   194   | parsitm dI (itm as (i,v,b,f, Typ str)) =
   195     (let val t = (term_of o the o (parse dI)) str
   196      in (i,v,b,f, Par str) end
   197      handle _ => (i,v,b,f, Syn str))
   198   | parsitm dI (itm as (i,v,_,f, Inc ((d,ts),_))) =
   199     (let val t = comp_dts (d,ts);
   200 	       val s = term_to_string''' dI t;
   201      (*this    ^ should raise the exn on unability of re-parsing dts*)
   202      in itm end
   203      handle _ => ((i,v,false,f, Syn (term2str t)):itm))
   204   | parsitm dI (itm as (i,v,_,f, Sup (d,ts))) =
   205     (let val t = comp_dts (d,ts);
   206 	       val s = term_to_string''' dI t;
   207      (*this    ^ should raise the exn on unability of re-parsing dts*)
   208     in itm end
   209     handle _ => ((i,v,false,f, Syn (term2str t)):itm))
   210   | parsitm dI (itm as (i,v,_,f, Mis (d,t'))) =
   211     (let val t = d $ t';
   212 	       val s = term_to_string''' dI t;
   213      (*this    ^ should raise the exn on unability of re-parsing dts*)
   214     in itm end
   215     handle _ => ((i,v,false,f, Syn (term2str t)):itm))
   216   | parsitm dI (itm as (i,v,_,f, Par _)) = 
   217     error ("parsitm (" ^ itm2str_ (thy2ctxt dI) itm ^ "): Par should be internal");
   218 
   219 (*separate a list to a pair of elements that do NOT satisfy the predicate,
   220  and of elements that satisfy the predicate, i.e. (false, true)*)
   221 fun filter_sep pred xs =
   222   let fun filt ab [] = ab
   223         | filt (a,b) (x :: xs) = if pred x 
   224 				 then filt (a,b@[x]) xs 
   225 				 else filt (a@[x],b) xs
   226   in filt ([],[]) xs end;
   227 fun is_Par ((_,_,_,_,Par _):itm) = true
   228   | is_Par _ = false;
   229 
   230 fun is_e_ts [] = true
   231   | is_e_ts [Const ("List.list.Nil", _)] = true
   232   | is_e_ts _ = false;
   233 
   234 (*WN.9.11.03 copied from fun appl_add (in modspec.sml)*)
   235 (* val (sel,ct) = selct;
   236    val (dI, oris, ppc, pbt, (sel, ct))=
   237        (#1 (some_spec ospec spec), oris, []:itm list,
   238 	((#ppc o get_pbt) (#2 (some_spec ospec spec))),
   239 	hd (imodel2fstr imodel));
   240    *)
   241 fun appl_add' dI oris ppc pbt (sel, ct) = 
   242     let 
   243       val ctxt = assoc_thy dI |> thy2ctxt;
   244     in case parseNEW ctxt ct of
   245 	   NONE => (0,[],false,sel, Syn ct):itm
   246 	 | SOME t => (case is_known ctxt sel oris t of
   247          ("", ori', all) =>
   248            (case is_notyet_input ctxt ppc all ori' pbt of
   249 	         ("",itm)  => itm
   250 	       | (msg,_) => error ("appl_add': " ^ msg))
   251      | (_, (i, v, _, d, ts), _) => if is_e_ts ts then
   252          (i, v, false, sel, Inc ((d, ts), (e_term, []))) else
   253          (i, v, false, sel, Sup (d, ts)))
   254     end;
   255 
   256 (*.generate preliminary itm_ from a strin (with field "#Given" etc.).*)
   257 (* val (f, str) = hd selcts;
   258    *)
   259 fun eq7 (f, d) (f', (d', _)) = f=f' andalso d=d';
   260 fun fstr2itm_ thy pbt (f, str) =
   261     let val topt = parse thy str
   262     in case topt of
   263 	   NONE => ([], false, f, Syn str)
   264 	 | SOME ct => 
   265 (* val SOME ct = parse thy str;
   266    *)
   267 	   let val (d,ts) = (split_dts o term_of) ct
   268 	       val popt = find_first (eq7 (f,d)) pbt
   269 	   in case popt of
   270 		  NONE => ([1](*??*), true(*??*), f, Sup (d,ts))
   271 		| SOME (f, (d, id)) => ([1], true, f, Cor ((d,ts), (id, ts))) 
   272 	   end
   273     end; 
   274 
   275 
   276 (*.input into empty PblObj, i.e. empty fmz+origin (unknown example).*)
   277 fun unknown_expl dI pbt selcts =
   278   let
   279     val thy = assoc_thy dI
   280     val its_ = map (fstr2itm_ thy pbt) selcts (*([1],true,"#Given",Cor (...))*)
   281     val its = add_id its_ 
   282 in (map flattup2 its): itm list end;
   283 
   284 
   285 
   286 
   287 (*WN.11.03 for input_icalhd, ~ specify_additem for Add_Given/_Find/_Relation
   288  appl_add': generate 1 item 
   289  appl_add' . is_known: parse, get data from oris (vats, all (elems if list)..)
   290  appl_add' . is_notyet_input: compare with items in model already input
   291  insert_ppc': insert this 1 item*)
   292 (* val (dI,oris,ppc,pbt,selcts) =((#1 (some_spec ospec spec)),oris,[(*!!*)],
   293 			       ((#ppc o get_pbt) (#2 (some_spec ospec spec))),
   294 			       (imodel2fstr imodel));
   295    *)
   296 fun appl_adds dI [] _ pbt selcts = unknown_expl dI pbt selcts
   297   (*already present itms in model are being overwritten*)
   298   | appl_adds dI oris ppc pbt [] = ppc
   299   | appl_adds dI oris ppc pbt (selct::ss) =
   300     (* val selct = (sel, string_of_cterm ct);
   301        *)
   302     let val itm = appl_add' dI oris ppc pbt selct;
   303     in appl_adds dI oris (insert_ppc' itm ppc) pbt ss end;
   304 (* val (dI, oris, ppc, pbt, selct::ss) = 
   305        (dI, pors, probl, ppc, map itms2fstr probl);
   306    ...vvv
   307    *)
   308 (* val (dI, oris, ppc, pbt, (selct::ss))=
   309        (#1 (some_spec ospec spec), oris, []:itm list,
   310 	((#ppc o get_pbt) (#2 (some_spec ospec spec))),(imodel2fstr imodel));
   311    val iii = appl_adds dI oris ppc pbt (selct::ss); 
   312    tracing(itms2str_ thy iii);
   313 
   314  val itm = appl_add' dI oris ppc pbt selct;
   315  val ppc = insert_ppc' itm ppc;
   316 
   317  val _::selct::ss = (selct::ss);
   318  val itm = appl_add' dI oris ppc pbt selct;
   319  val ppc = insert_ppc' itm ppc;
   320 
   321  val _::selct::ss = (selct::ss);
   322  val itm = appl_add' dI oris ppc pbt selct;
   323  val ppc = insert_ppc' itm ppc;
   324  tracing(itms2str_ thy ppc);
   325 
   326  val _::selct::ss = (selct::ss);
   327  val itm = appl_add' dI oris ppc pbt selct;
   328  val ppc = insert_ppc' itm ppc;
   329    *)
   330 
   331 
   332 fun oris2itms _ _ ([]:ori list) = ([]:itm list)
   333   | oris2itms pbt vat ((i,v,f,d,ts)::(os: ori list)) =
   334     if member op = vat v 
   335     then (i,v,true,f,Cor ((d,ts),(e_term,[])))::(oris2itms pbt vat os)
   336     else oris2itms pbt vat os;
   337 
   338 fun filter_dsc oris itm = 
   339     filter_out ((curry op= ((d_in o #5) (itm:itm))) o 
   340 		(#4:ori -> term)) oris;
   341 
   342 
   343 
   344 
   345 fun par2fstr ((_,_,_,f, Par s):itm) = (f, s)
   346   | par2fstr itm = error ("par2fstr: called with " ^
   347 			      itm2str_ (thy2ctxt' "Isac") itm);
   348 fun itms2fstr ((_,_,_,f, Cor ((d,ts),_)):itm) = (f, comp_dts'' (d,ts))
   349   | itms2fstr (_,_,_,f, Syn str) = (f, str)
   350   | itms2fstr (_,_,_,f, Typ str) = (f, str)
   351   | itms2fstr (_,_,_,f, Inc ((d,ts),_)) = (f, comp_dts'' (d,ts))
   352   | itms2fstr (_,_,_,f, Sup (d,ts)) = (f, comp_dts'' (d,ts))
   353   | itms2fstr (_,_,_,f, Mis (d,t)) = (f, term2str (d $ t))
   354   | itms2fstr (itm as (_,_,_,f, Par _)) = 
   355     error ("parsitm ("^itm2str_ (thy2ctxt' "Isac") itm ^
   356 		 "): Par should be internal");
   357 
   358 fun imodel2fstr iitems = 
   359   let 
   360     fun xxx is [] = is
   361 	    | xxx is ((Given strs)::iis) = xxx (is @ (map (pair "#Given") strs)) iis
   362 	    | xxx is ((Find strs)::iis) = xxx (is @ (map (pair "#Find") strs)) iis
   363 	    | xxx is ((Relate strs)::iis) = xxx (is @ (map (pair "#Relate") strs)) iis
   364   in xxx [] iitems end;
   365 
   366 (* input a calchead, WN110505 "prep_oris -> (_, ctxt)" not handled properly *)
   367 fun input_icalhd pt (((p,_), hdf, imodel, Pbl, spec as (dI,pI,mI)):icalhd) =
   368     let val PblObj {fmz = fmz as (fmz_,_), origin = (oris, ospec, hdf'), 
   369 		      spec = sspec as (sdI,spI,smI), probl, meth, ...} = get_obj I pt p;
   370     in if is_casinput hdf fmz then the (cas_input (str2term hdf)) 
   371        else        (*hacked WN0602 ~~~            ~~~~~~~~~,   ..dropped !*)
   372          let val (pos_, pits, mits) = 
   373 	         if dI <> sdI
   374 	         then let val its = map (parsitm (assoc_thy dI)) probl;
   375 			            val (its, trms) = filter_sep is_Par its;
   376 			            val pbt = (#ppc o get_pbt) (#2(some_spec ospec sspec));
   377 		            in (Pbl, appl_adds dI oris its pbt  (map par2fstr trms), meth) end 
   378            else if pI <> spI 
   379 	              then if pI = snd3 ospec then (Pbl, probl, meth) 
   380                      else
   381 		                   let val pbt = (#ppc o get_pbt) pI
   382 			                   val dI' = #1 (some_spec ospec spec)
   383 			                   val oris = if pI = #2 ospec then oris 
   384 				                            else prep_ori fmz_(assoc_thy"Isac") pbt |> #1;
   385 		                   in (Pbl, appl_adds dI' oris probl pbt 
   386 				                 (map itms2fstr probl), meth) end 
   387                 else if mI <> smI (*FIXME.WN0311: what if probl is incomplete?!*)
   388 	                   then let val met = (#ppc o get_met) mI
   389 		                        val mits = complete_metitms oris probl meth met
   390 		                      in if foldl and_ (true, map #3 mits)
   391 		                         then (Pbl, probl, mits) else (Met, probl, mits) 
   392 		                      end 
   393                      else (Pbl, appl_adds (#1 (some_spec ospec spec)) oris [(*!!!*)]
   394 			                     ((#ppc o get_pbt) (#2 (some_spec ospec spec)))
   395 			                     (imodel2fstr imodel), meth);
   396 	         val pt = update_spec pt p spec;
   397          in if pos_ = Pbl
   398 	          then let val {prls,where_,...} = get_pbt (#2 (some_spec ospec spec))
   399 		               val pre =check_preconds(assoc_thy"Isac")prls where_ pits
   400 	               in (update_pbl pt p pits,
   401 		                 (ocalhd_complete pits pre spec, Pbl, hdf', pits, pre, spec):ocalhd) 
   402                  end
   403 	           else let val {prls,pre,...} = get_met (#3 (some_spec ospec spec))
   404 		                val pre = check_preconds (assoc_thy"Isac") prls pre mits
   405 	                in (update_met pt p mits,
   406 		                  (ocalhd_complete mits pre spec, Met, hdf', mits, pre, spec):ocalhd)
   407                   end
   408          end 
   409     end
   410   | input_icalhd pt ((p,_), hdf, imodel, _(*Met*), spec as (dI,pI,mI)) =
   411     error "input_icalhd Met not impl.";
   412 
   413 
   414 (***. handle an input formula .***)
   415 (*
   416 Untersuchung zur Formeleingabe (appendFormula, replaceFormla) zu einer Anregung von Alan Krempler:
   417 Welche RICHTIGEN Formeln koennen NICHT abgeleitet werden, 
   418 wenn Abteilungen nur auf gleichem Level gesucht werden ?
   419 WN.040216 
   420 
   421 Beispiele zum Equationsolver von Richard Lang aus /src/sml/kbtest/rlang.sml
   422 
   423 ------------------------------------------------------------------------------
   424 "Schalk I s.87 Bsp 52a ((5*x)/(x - 2) - x/(x+2)=4)";
   425 ------------------------------------------------------------------------------
   426 1. "5 * x / (x - 2) - x / (x + 2) = 4"
   427 ...
   428 4. "12 * x + 4 * x ^^^ 2 = 4 * (-4 + x ^^^ 2)",Subproblem["normalize", "poly"..
   429 ...
   430 4.3. "16 + 12 * x = 0", Subproblem["degree_1", "polynomial", "univariate"..
   431 ...
   432 4.3.3. "[x = -4 / 3]")), Check_elementwise "Assumptions"
   433 ...
   434 "[x = -4 / 3]"
   435 ------------------------------------------------------------------------------
   436 (1)..(6): keine 'richtige' Eingabe kann abgeleitet werden, die einen Summanden auf die andere Seite verschiebt [Ableitung ware in 4.3.n]
   437 
   438 (4.1)..(4.3): keine 'richtige' Eingabe kann abgeleitet werden, die einen Summanden auf die andere Seite verschiebt [Ableitung ware in 4.3.n]
   439 ------------------------------------------------------------------------------
   440 
   441 
   442 ------------------------------------------------------------------------------
   443 "Schalk I s.87 Bsp 55b (x/(x^^^2 - 6*x+9) - 1/(x^^^2 - 3*x) =1/x)";
   444 ------------------------------------------------------------------------------
   445 1. "x / (x ^^^ 2 - 6 * x + 9) - 1 / (x ^^^ 2 - 3 * x) = 1 / x"
   446 ...
   447 4. "(3 + (-1 * x + x ^^^ 2)) * x = 1 * (9 * x + (x ^^^ 3 + -6 * x ^^^ 2))"
   448                          Subproblem["normalize", "polynomial", "univariate"..
   449 ...
   450 4.4. "-6 * x + 5 * x ^^^ 2 = 0", Subproblem["bdv_only", "degree_2", "poly"..
   451 ...
   452 4.4.4. "[x = 0, x = 6 / 5]", Check_elementwise "Assumptions"
   453 4.4.5. "[x = 0, x = 6 / 5]"
   454 ...
   455 5. "[x = 0, x = 6 / 5]", Check_elementwise "Assumptions"
   456    "[x = 6 / 5]"
   457 ------------------------------------------------------------------------------
   458 (1)..(4): keine 'richtige' Eingabe kann abgeleitet werden, die einen Summanden auf die andere Seite schiebt [Ableitung waere in 4.4.x]
   459 
   460 (4.1)..(4.4.5): keine 'richtige' Eingabe kann abgeleitet werden, die dem Ergebnis "[x = 6 / 5]" aequivalent ist [Ableitung waere in 5.]
   461 ------------------------------------------------------------------------------
   462 
   463 
   464 ------------------------------------------------------------------------------
   465 "Schalk II s.56 Bsp 73b (sqrt(x+1)+sqrt(4*x+4)=sqrt(9*x+9))";
   466 ------------------------------------------------------------------------------
   467 1. "sqrt (x + 1) + sqrt (4 * x + 4) = sqrt (9 * x + 9)"
   468 ...
   469 6. "13 + 13 * x + -2 * sqrt ((4 + 4 * x) * (9 + 9 * x)) = 1 + x"
   470                              Subproblem["sq", "root'", "univariate", "equation"]
   471 ...
   472 6.6. "144 + 288 * x + 144 * x ^^^ 2 = 144 + x ^^^ 2 + 288 * x + 143 * x ^^^ 2"
   473                 Subproblem["normalize", "polynomial", "univariate", "equation"]
   474 ...
   475 6.6.3 "0 = 0"    Subproblem["degree_0", "polynomial", "univariate", "equation"]
   476 ...                                       Or_to_List
   477 6.6.3.2 "UniversalList"
   478 ------------------------------------------------------------------------------
   479 (1)..(6): keine 'richtige' Eingabe kann abgeleitet werden, die eine der Wurzeln auf die andere Seite verschieb [Ableitung ware in 6.6.n]
   480 
   481 (6.1)..(6.3): keine 'richtige' Eingabe kann abgeleitet werden, die einen Summanden auf die andere Seite verschiebt [Ableitung ware in 6.6.n]
   482 ------------------------------------------------------------------------------
   483 *)
   484 (*sh. comments auf 498*)
   485 
   486 fun equal a b = a=b;
   487 
   488 (*the lists contain eq-al elem-pairs at the beginning;
   489   return first list reverted (again) - ie. in order as required subsequently*)
   490 fun dropwhile' equal (f1::f2::fs) (i1::i2::is) =
   491     if equal f1 i1 then
   492 	 if equal f2 i2 then dropwhile' equal (f2::fs) (i2::is)
   493 	 else (rev (f1::f2::fs), i1::i2::is)
   494     else error "dropwhile': did not start with equal elements"
   495   | dropwhile' equal (f::fs) [i] =
   496     if equal f i then (rev (f::fs), [i])
   497     else error "dropwhile': did not start with equal elements"
   498   | dropwhile' equal [f] (i::is) =
   499     if equal f i then ([f], i::is)
   500     else error "dropwhile': did not start with equal elements";
   501 (*
   502  fun equal a b = a=b;
   503  val foder = [0,1,2,3,4,5]; val ifoder = [11,12,3,4,5];
   504  val r_foder = rev foder;  val r_ifoder = rev ifoder;
   505  dropwhile' equal r_foder r_ifoder;
   506 > vval it = ([0, 1, 2, 3], [3, 12, 11]) : int list * int list
   507 
   508  val foder = [3,4,5]; val ifoder = [11,12,3,4,5];
   509  val r_foder = rev foder;  val r_ifoder = rev ifoder;
   510  dropwhile' equal r_foder r_ifoder;
   511 > val it = ([3], [3, 12, 11]) : int list * int list
   512 
   513  val foder = [5]; val ifoder = [11,12,3,4,5];
   514  val r_foder = rev foder;  val r_ifoder = rev ifoder;
   515  dropwhile' equal r_foder r_ifoder;
   516 > val it = ([5], [5, 4, 3, 12, 11]) : int list * int list
   517 
   518  val foder = [10,11,12,13,14,15]; val ifoder = [11,12,3,4,5];
   519  val r_foder = rev foder;  val r_ifoder = rev ifoder;
   520  dropwhile' equal r_foder r_ifoder;
   521 > *** dropwhile': did not start with equal elements*)
   522 
   523 (*040214: version for concat_deriv*)
   524 fun rev_deriv' (t, r, (t', a)) = (t', sym_Thm r, (t, a));
   525 
   526 fun mk_tacis ro erls (t, r as Thm _, (t', a)) = 
   527       (Rewrite (rule2thm' r), 
   528          Rewrite' ("Isac", fst ro, erls, false, rule2thm' r, t, (t', a)),
   529        (e_pos'(*to be updated before generate tacis!!!*), (Uistate, e_ctxt)))
   530   | mk_tacis ro erls (t, r as Rls_ rls, (t', a)) = 
   531       (Rewrite_Set (rule2rls' r), 
   532          Rewrite_Set' ("Isac", false, rls, t, (t', a)),
   533        (e_pos'(*to be updated before generate tacis!!!*), (Uistate, e_ctxt)));
   534 
   535 (*fo = ifo excluded already in inform*)
   536 fun concat_deriv rew_ord erls rules fo ifo =
   537     let fun derivat ([]:(term * rule * (term * term list)) list) = e_term
   538 	  | derivat dt = (#1 o #3 o last_elem) dt
   539         fun equal (_,_,(t1, _)) (_,_,(t2, _)) = t1=t2
   540 	val  fod = make_deriv (Isac"") erls rules (snd rew_ord) NONE  fo
   541 	val ifod = make_deriv (Isac"") erls rules (snd rew_ord) NONE ifo
   542     in case (fod, ifod) of
   543 	   ([], []) => if fo = ifo then (true, [])
   544 		       else (false, [])
   545 	 | (fod, []) => if derivat fod = ifo 
   546 			then (true, fod) (*ifo is normal form*)
   547 			else (false, [])
   548 	 | ([], ifod) => if fo = derivat ifod 
   549 			 then (true, ((map rev_deriv') o rev) ifod)
   550 			 else (false, [])
   551 	 | (fod, ifod) =>
   552 	   if derivat fod = derivat ifod (*common normal form found*)
   553 	   then let val (fod', rifod') = 
   554 			dropwhile' equal (rev fod) (rev ifod)
   555 		in (true, fod' @ (map rev_deriv' rifod')) end
   556 	   else (false, [])
   557     end;
   558 (*
   559  val ({rew_ord, erls, rules,...}, fo, ifo) = 
   560      (rep_rls Test_simplify, str2term "x+1+ -1*2=0", str2term "-2*1+(x+1)=0");
   561  (tracing o trtas2str) fod';
   562 > ["
   563 (x + 1 + -1 * 2 = 0, Thm ("radd_commute","?m + ?n = ?n + ?m"), (-1 * 2 + (x + 1) = 0, []))","
   564 (-1 * 2 + (x + 1) = 0, Thm ("radd_commute","?m + ?n = ?n + ?m"), (-1 * 2 + (1 + x) = 0, []))","
   565 (-1 * 2 + (1 + x) = 0, Thm ("radd_left_commute","?x + (?y + ?z) = ?y + (?x + ?z)"), (1 + (-1 * 2 + x) = 0, []))","
   566 (1 + (-1 * 2 + x) = 0, Thm ("#mult_Float ((~1,0), (0,0)) __ ((2,0), (0,0))","-1 * 2 = -2"), (1 + (-2 + x) = 0, []))"]
   567 val it = () : unit
   568  (tracing o trtas2str) (map rev_deriv' rifod');
   569 > ["
   570 (1 + (-2 + x) = 0, Thm ("sym_#mult_Float ((~2,0), (0,0)) __ ((1,0), (0,0))","-2 = -2 * 1"), (1 + (-2 * 1 + x) = 0, []))","
   571 (1 + (-2 * 1 + x) = 0, Thm ("sym_radd_left_commute","?y + (?x + ?z) = ?x + (?y + ?z)"), (-2 * 1 + (1 + x) = 0, []))","
   572 (-2 * 1 + (1 + x) = 0, Thm ("sym_radd_commute","?n + ?m = ?m + ?n"), (-2 * 1 + (x + 1) = 0, []))"]
   573 val it = () : unit
   574 *)
   575 
   576 
   577 (* compare inform with ctree.form at current pos by nrls;
   578    if found, embed the derivation generated during comparison
   579    if not, let the mat-engine compute the next ctree.form *)
   580 (*structure copied from complete_solve
   581   CAUTION: tacis in returned calcstate' do NOT construct resulting ptp --
   582            all_modspec etc. has to be inserted at Subproblem'*)
   583 fun compare_step ((tacis, c, ptp as (pt, pos as (p,p_))): calcstate') ifo =
   584   let
   585     val fo =
   586       case p_ of
   587         Frm => get_obj g_form pt p
   588 			| Res => (fst o (get_obj g_result pt)) p
   589 			| _ => e_term (*on PblObj is fo <> ifo*);
   590 	  val {nrls, ...} = get_met (get_obj g_metID pt (par_pblobj pt p))
   591 	  val {rew_ord, erls, rules, ...} = rep_rls nrls
   592 	  val (found, der) = concat_deriv rew_ord erls rules fo ifo; (*<---------------*)
   593   in
   594     if found
   595     then
   596        let
   597          val tacis' = map (mk_tacis rew_ord erls) der;
   598 		     val (c', ptp) = embed_deriv tacis' ptp;
   599 	     in ("ok", (tacis (*@ tacis'?WN050408*), c @ c', ptp)) end
   600      else 
   601 	     if pos = ([], Res) 
   602 	     then ("no derivation found", (tacis, c, ptp): calcstate') 
   603 	     else
   604          let
   605            val cs' as (tacis, c', ptp) = nxt_solve_ ptp; (*<---------------------*)
   606 		       val cs' as (tacis, c'', ptp) = 
   607 			       case tacis of
   608 			         ((Subproblem _, _, _)::_) => 
   609 			           let
   610                    val ptp as (pt, (p,_)) = all_modspec ptp
   611 				           val mI = get_obj g_metID pt p
   612 			           in
   613 			             nxt_solv (Apply_Method' (mI, NONE, e_istate, e_ctxt)) (e_istate, e_ctxt) ptp
   614                  end
   615 			       | _ => cs';
   616 		     in compare_step (tacis, c @ c' @ c'', ptp) ifo end
   617   end;
   618 
   619 (* check if (agreed result, input formula) matches the error pattern "pat" modulo simplifier rls *)
   620 fun check_err_patt (res, inf) (subst: subst) (errpatID: errpatID, pat) rls =
   621   let 
   622     val (res', _, _, rewritten) =
   623       rew_sub (Isac()) 1 subst e_rew_ord e_rls false [] (Trueprop $ pat) res;
   624   in
   625     if rewritten
   626     then 
   627       let
   628         val norm_res = case rewrite_set_inst_ (Isac()) false subst rls  res' of
   629           NONE => res'
   630         | SOME (norm_res, _) => norm_res
   631         val norm_inf = case rewrite_set_inst_ (Isac()) false subst rls inf of
   632           NONE => inf
   633         | SOME (norm_inf, _) => norm_inf
   634       in if norm_res = norm_inf then SOME errpatID else NONE
   635       end
   636     else NONE
   637   end;
   638 
   639 (* check if (agreed result, input formula) matches SOME error pattern modulo simplifier rls;
   640    (prog, env) for retrieval of casual substitution for bdv in the pattern. *)
   641 fun check_error_patterns (res, inf) (prog, env) (errpats, rls) =
   642   let
   643     val (_, subst) = get_bdv_subst prog env
   644     fun scan (_, [], _) = NONE
   645       | scan (errpatID, errpat :: errpats, _) =
   646           case check_err_patt (res, inf) (subst: subst) (errpatID, errpat) rls of
   647             NONE => scan (errpatID, errpats, [])
   648           | SOME errpatID => SOME errpatID
   649     fun scans [] = NONE
   650       | scans (group :: groups) =
   651           case scan group of
   652             NONE => scans groups
   653           | SOME errpatID => SOME errpatID
   654   in scans errpats end;
   655 
   656 (*.handle a user-input formula, which may be a CAS-command, too.
   657 CAS-command:
   658    create a calchead, and do 1 step
   659    TOOODO.WN0602 works only for the root-problem !!!
   660 formula, which is no CAS-command:
   661    compare iform with calc-tree.form at pos by equ_nrls and all subsequent pos;
   662    collect all the tacs applied by the way;
   663    if "no derivation found" then check_error_patterns.
   664    ALTERNATIVE: check_error_patterns _within_ compare_step seems too expensive.*)
   665 fun inform (cs as (_, _, ptp as (pt, pos as (p, p_))): calcstate') istr =
   666   case parse (assoc_thy "Isac") istr of
   667 	  SOME f_in =>
   668 	    let
   669 	      val f_in = term_of f_in
   670 	      val f_succ = get_curr_formula (pt, pos);
   671 			in
   672 			  if f_succ = f_in
   673 			  then ("same-formula", cs) (* ctree not cut with replaceFormula *)
   674 			  else
   675 			    case cas_input f_in of
   676 			      SOME (pt, _) => ("ok",([], [], (pt, (p, Met))))
   677 			    | NONE =>
   678 			        let
   679 			          val pos_pred = lev_back' pos
   680 			          (* f_pred ---"step pos cs"---> f_succ in appendFormula *)
   681 			          val f_pred = get_curr_formula (pt, pos_pred)
   682 			          val msg_calcstate' = compare_step ([], [], (pt, pos_pred)) f_in (*<<=====*)
   683 			          (*last step re-calc in compare_step TODO before WN09*)
   684 			        in
   685 			          case msg_calcstate' of
   686 			            ("no derivation found", calcstate') => 
   687 			               let
   688 			                 val pp = par_pblobj pt p
   689 			                 val {errpats, nrls, scr = Prog prog, ...} = get_met (get_obj g_metID pt pp)
   690 			                 val ScrState (env, _, _, _, _, _) = get_istate pt pos
   691 			               in
   692 			                 case check_error_patterns (f_pred, f_in) (prog, env) (errpats, nrls) of
   693 			                   SOME errpatID => ("error pattern #" ^ errpatID ^ "#", calcstate')
   694 			                 | NONE => msg_calcstate'
   695 			               end
   696 			          | _ => msg_calcstate'
   697 			        end
   698 			end
   699     | NONE => ("syntax error in '" ^ istr ^ "'", e_calcstate');
   700 
   701 (* fill-in patterns an forms.
   702   returns thm required by "fun in_fillform *)
   703 fun get_fillform (subs_opt, subst) (thm, form) errpatID ((fillpatID, pat, erpaID): fillpat) =
   704   let
   705     val (form', _, _, rewritten) =
   706       rew_sub (Isac()) 1 subst e_rew_ord e_rls false [] (Trueprop $ pat) form;
   707   in (*the fillpat of the thm must be dedicated to errpatID*)
   708     if errpatID = erpaID andalso rewritten
   709     then SOME (fillpatID, HOLogic.mk_eq (form, form'), thm, subs_opt) else NONE end;
   710 
   711 fun get_fillpats subst form errpatID thm =
   712       let
   713         val thmDeriv = Thm.get_name_hint thm
   714         val (part, thyID) = thy_containing_thm thmDeriv
   715         val theID = [part, thyID, "Theorems", thmID_of_derivation_name thmDeriv]
   716         val Hthm {fillpats, ...} = get_the theID
   717         val some = map (get_fillform subst (thm, form) errpatID) fillpats
   718       in some |> filter is_some |> map the end;
   719 
   720 fun find_fillpatterns (pt, pos as (p, _): pos') errpatID =
   721   let 
   722     val f_curr = get_curr_formula (pt, pos);
   723     val pp = par_pblobj pt p
   724 			    val {errpats, scr = Prog prog, ...} = get_met (get_obj g_metID pt pp)
   725     val ScrState (env, _, _, _, _, _) = get_istate pt pos
   726     val subst = get_bdv_subst prog env
   727     val errpatthms = errpats
   728       |> filter ((curry op = errpatID) o (#1: errpat -> errpatID))
   729       |> map (#3: errpat -> thm list)
   730       |> flat
   731   in map (get_fillpats subst f_curr errpatID) errpatthms |> flat end;
   732 
   733 (* check if an input formula is exactly equal the rewrite from a rule
   734    which is stored at the pos where the input is stored of "ok". *)
   735 fun is_exactly_equal (pt, pos as (p, _)) istr =
   736   case parseNEW (assoc_thy "Isac" |> thy2ctxt) istr of
   737     NONE => ("syntax error in '" ^ istr ^ "'", Tac "")
   738   | SOME ifo => 
   739       let
   740         val p' = lev_on p;
   741         val tac = get_obj g_tac pt p';
   742       in 
   743         case applicable_in pos pt tac of
   744           Notappl msg => (msg, Tac "")
   745         | Appl rew =>
   746             let
   747               val res = case rew of
   748                   Rewrite_Inst' (_, _, _, _, _, _, _, (res, _)) => res
   749                 | Rewrite' (_, _, _, _, _, _, (res, _)) => res
   750             in 
   751               if not (ifo = res)
   752               then ("input does not exactly fill the gaps", Tac "")
   753               else ("ok", tac)
   754             end
   755       end
   756 
   757 (* fetch errpatIDs from an arbitrary tactic *)
   758 fun fetchErrorpatterns tac =
   759   let
   760     val rlsID =
   761       case tac of
   762         Rewrite_Set rlsID => rlsID
   763       | Rewrite_Set_Inst (_, rlsID) => rlsID
   764       | _ => "e_rls"
   765     val (part, thyID) = thy_containing_rls "Isac" rlsID;
   766     val Hrls {thy_rls = (_, rls), ...} = get_the [part, thyID, "Rulesets", rlsID]
   767   in case rls of
   768         Rls {errpatts, ...} => errpatts
   769       | Seq {errpatts, ...} => errpatts
   770       | Rrls {errpatts, ...} => errpatts
   771       | Erls => []
   772   end
   773 
   774 (*------------------------------------------------------------------(**)
   775 end
   776 open inform; 
   777 (**)------------------------------------------------------------------*)