src/Tools/isac/Specify/specification.sml
author Walther Neuper <walther.neuper@jku.at>
Sat, 16 May 2020 16:54:39 +0200
changeset 59989 31f54ab9b2ce
parent 59988 9a6aa40d1962
child 59990 ca6f741c0ca3
permissions -rw-r--r--
shift code from Specification to P_Model, rename ids
     1 (* Title:  Specify/specification.sml
     2    Author: Walther Neuper, Mathias Lehnfeld
     3    (c) due to copyright terms
     4 *)
     5 (* Survey on type fmz_ .. type ori .. type itm (in probl, meth) .. formal args of root-/SubProblem
     6    and relations between respective list elements:                                       #1#
     7                   fmz      =             [ dsc $ v......................................(dsc $ v)..]
     8 root problem on pos = ([], _)            
     9                   fmz_     =             [(dsc, v).......................................(dsc, v)..]
    10                   \<down>                                                                       
    11                   oris     =             [(dsc, v)..(dsc, v),(dsc, v),(dsc, v)...........(dsc, v)..]
    12                   \<down>                       #Given,..,#Relate  #Find     #undef............#undef   \<down>
    13                   \<down>                                                     \<down>                 \<down>       \<down>
    14   Specify_Problem + pbl.ppc=             [(dsc,id)..(dsc,id),(dsc,id)]  \<down>                 \<down>       \<down>
    15                   \<down>                                                     \<down>                 \<down>       \<down>
    16                   itms     =             [(dsc, v)..(dsc, v),(dsc, v)]  \<down>                 \<down>       \<down>
    17   int.modelling                           +Cor ++++++++++Cor +Cor       \<down>                 \<down>       \<down>
    18                   \<down>                                                     \<down>                 \<down>       \<down>
    19   Specify_Method  + met.ppc=             [(dsc,id)..(dsc,id),(dsc,id)]  \<down>                 \<down>       \<down>
    20                   \<down>                                                     \<down>                 \<down>       \<down>
    21                   itms     =             [(dsc, v)..(dsc, v),(dsc, v),(dsc, v)..(dsc, v)] \<down>       \<down>
    22   int.modelling                                                       +Cor ++++++Cor      \<down>       \<down>
    23                   form.args=             [ id ..... id      , ///////  id ....... id    ] \<down>       \<down>
    24                   env =                  [(id, v)..(id, v)  , /////// (id, v)....(id, v)] \<down>       \<down>
    25   interpret code env = [(id, v)..(id, v),(id, v)..(id, v)  , /////// (id, v)....(id, v)] \<down>       \<down>
    26     ..extends env       ^^^^^^^^^^^^^^^^   \<down>                           \<down>   \<down>              \<down>       \<down>
    27                                    \<down>       \<down>                           \<down>   \<down>              \<down>       \<down>
    28 SubProblem on pos = ([2], _)       \<down>       \<down>                           \<down>   \<down>              \<down>       \<down>
    29                   form.args=      [id ................................ id ]\<down>              \<down>       \<down>
    30                   \<down>                REAL..BOOL..                            \<down>              \<down>       \<down>
    31                   \<down>                                                        \<down>              \<down>       \<down>
    32                   + met.ppc=      [(dsc,id).......................(dsc,id)]\<down>              \<down>       \<down>
    33                                     \<down>                               \<down>      \<down>              \<down>       \<down>
    34                   oris     =      [(dsc, v)...................... (dsc,   v) ........... (dsc, v)]\<down>
    35   Specify_Problem, int.modelling, Specify_Method, interpret code as above                 #1#    \<down>
    36                                                                                                   \<down>
    37 SubProblem on pos = ([3, 4], _)                                                                   \<down>
    38                   form.args=              [id ...................... id ]                         \<down>
    39                   \<down>                        REAL..BOOL..                                           \<down>
    40                   + met.ppc=              [(dsc,id).............(dsc,id)]                         \<down>
    41                   oris     =              [(dsc, v).............(dsc, v)...................(dsc, v)] 
    42   Specify_Problem, int.modelling, Specify_Method, interpret code as above                    #1#
    43 
    44 Notes:
    45 (1) SubProblem implements sub-function calls such, that the arguments (+ pre- + post-condition)
    46     of the functions become concern of interactive modelling.
    47 (2) In Isac-terms, the above concerns the phases of modelling and 
    48     and of specifying (Specify_Problem, Specify_Method),
    49     while stepwise construction of solutions is called solving.
    50     The latter is supported by Lucas-Interpretation of the functions' body.
    51 (3) ori list is determined by fmz_ (in root-pbl) or by args of SubProblem;
    52     it is as complete as possible (this has been different up to now).
    53 (4) itm list is initialised by pbl-ppc | met-pps and completed (Cor) by stepwise user input.
    54 (5) model-pattern of pbl can omit technical items, e.g. boundVariable or functionName,
    55     add them to the model-pattern of met and let this input be done automatically;
    56     respective items must be in fmz.
    57 *)
    58 signature SPECIFICATION =
    59 sig
    60 
    61   type T = Specification_Def.T
    62 
    63 (*val reset: Calc.T -> Calc.T*)
    64   val reset_calchead: Calc.T -> Calc.T
    65 (*val get: Calc.T -> T*)
    66   val get_ocalhd: Calc.T -> T
    67 (*val complete: I_Model.T -> Pre_Conds.T -> ThyC.id * Problem.id * Method.id -> bool*)
    68   val ocalhd_complete: I_Model.T -> Pre_Conds.T -> ThyC.id * Problem.id * Method.id -> bool
    69 (*val is_complete: Calc.T -> bool*)
    70   val is_complete_mod: Calc.T -> bool
    71 
    72 (*/------- to Specify from Specification -------\* )
    73 (*val find_next_step': Pos.pos_ -> bool -> O_Model.T -> References.T -> I_Model.T * I_Model.T ->
    74     Model_Pattern.T * Model_Pattern.T -> References.T -> Pos.pos_ * Tactic.input*)
    75   val nxt_spec: Pos.pos_ -> bool -> O_Model.T -> References.T -> I_Model.T * I_Model.T ->
    76     Model_Pattern.T * Model_Pattern.T -> References.T -> Pos.pos_ * Tactic.input
    77 (*val do_all : Calc.T -> Calc.T*)
    78   val all_modspec: Calc.T -> Calc.T 
    79 (*val finish_phase: Calc.T -> Calc.T*)
    80   val complete_mod: Calc.T -> Calc.T
    81 (*val nxt_add: theory -> O_Model.T -> Model_Pattern.T -> I_Model.T -> (string * string) option*)
    82   val nxt_add: theory -> O_Model.T -> Model_Pattern.T -> I_Model.T -> (string * string) option
    83 (*val nxt_specif_additem: string -> TermC.as_string -> Calc.T -> Calc.state_post*)
    84   val nxt_specif_additem: string -> TermC.as_string -> Calc.T -> Calc.state_post
    85 (*val specify_additem: string -> TermC.as_string -> Calc.T -> string * Calc.state_post*)
    86   val specify_additem: string -> TermC.as_string -> Calc.T -> string * Calc.state_post
    87 ( *\------- to Specify from Specification -------/*)
    88 
    89 (* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
    90   (*NONE*)
    91 (*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
    92   val get: Calc.T -> I_Model.T * O_Model.T * References.T * I_Model.T * References.T * Proof.context
    93   val make: I_Model.m_field -> TermC.as_string * I_Model.T -> Tactic.T
    94 ( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
    95 
    96 (*----- kept as hints to initial design ideas; NOT in src/, NOT in test ------------------------*)
    97   val variants_in: term list -> int
    98   val is_untouched: I_Model.single -> bool
    99   val is_list_type: typ -> bool
   100   val parse_ok: I_Model.feedback list -> bool
   101   val all_dsc_in: I_Model.feedback list -> term list
   102   val chktyps: theory -> term list * term list -> term list (* only in old tests*)
   103   val is_complete_modspec: Calc.T -> bool
   104   val get_formress: (string * Pos.pos' * term) list list -> Pos.pos -> Ctree.ctree list ->
   105     (string * Pos.pos' * term) list
   106   val get_forms: (string * Pos.pos' * term) list list -> Pos.pos -> Ctree.ctree list ->
   107     (string * Pos.pos' * term) list
   108 end
   109 
   110 (**)
   111 structure Specification(** ): SPECIFICATION( **) =
   112 struct
   113 (**)
   114 
   115 type T = Specification_Def.T;
   116 
   117 (* is the calchead complete ? *)
   118 fun ocalhd_complete its pre (dI, pI, mI) = 
   119   foldl and_ (true, map #3 (its: I_Model.T)) andalso 
   120   foldl and_ (true, map #1 (pre: Pre_Conds.T)) andalso 
   121   dI <> ThyC.id_empty andalso pI <> Problem.id_empty andalso mI <> Method.id_empty
   122 
   123 (* get the number of variants in a problem in 'original',
   124    assumes equal descriptions in immediate sequence    *)
   125 fun variants_in ts =
   126   let
   127     fun eq (x, y) = head_of x = head_of y
   128     fun cnt _ [] _ n = ([n], [])
   129       | cnt eq (x :: xs) y n = if eq (x, y) then cnt eq xs y (n + 1) else ([n], x :: xs)
   130     fun coll _  xs [] = xs
   131       | coll eq  xs (y :: ys) = 
   132         let val (n, ys') = cnt eq (y :: ys) y 0
   133         in if ys' = [] then xs @ n else coll eq  (xs @ n) ys' end
   134     val vts = subtract op = [1] (distinct (coll eq [] ts))
   135   in
   136     case vts of 
   137       [] => 1
   138     | [n] => n
   139     | _ => raise ERROR "different variants in formalization"
   140   end
   141 
   142 fun is_list_type (Type ("List.list", _)) = true
   143   | is_list_type _ = false
   144 fun is_parsed (I_Model.Syn _) = false
   145   | is_parsed _ = true
   146 fun parse_ok its = foldl and_ (true, map is_parsed its)
   147 
   148 fun all_dsc_in itm_s =
   149   let    
   150     fun d_in (I_Model.Cor ((d, _), _)) = [d]
   151       | d_in (I_Model.Syn _) = []
   152       | d_in (I_Model.Typ _) = []
   153       | d_in (I_Model.Inc ((d,_),_)) = [d]
   154       | d_in (I_Model.Sup (d,_)) = [d]
   155       | d_in (I_Model.Mis (d,_)) = [d]
   156       | d_in i = raise ERROR ("all_dsc_in: uncovered case with " ^ I_Model.feedback_to_string' i)
   157   in (flat o (map d_in)) itm_s end; 
   158 
   159 (* get the first term in ts from ori *)
   160 fun getr_ct thy (_, _, fd, d, ts) =
   161   (fd, ((UnparseC.term_in_thy thy) o Input_Descript.join) (d,[hd ts]))
   162 
   163 (* get a term from ori, notyet input in itm.
   164    the term from ori is thrown back to a string in order to reuse
   165    machinery for immediate input by the user. *)
   166 fun geti_ct thy (_, _, _, d, ts) (_, _, _, fd, itm_) =
   167   (fd, ((UnparseC.term_in_thy thy) o Input_Descript.join) (d, subtract op = (I_Model.ts_in itm_) ts))
   168 
   169 (* in FE dsc, not dat: this is in itms ...*)
   170 fun is_untouched (_, _, false, _, I_Model.Inc ((_, []), _)) = true
   171   | is_untouched _ = false
   172 
   173 (* select an item in oris, notyet input in itms 
   174    (precondition: in itms are only I_Model.Cor, I_Model.Sup, I_Model.Inc) *)
   175 (*args of nxt_add
   176   thy : for?
   177   oris: from formalization 'type fmz', structured for efficient access 
   178   pbt : the problem-pattern to be matched with oris in order to get itms
   179   itms: already input items
   180 *)
   181 fun nxt_add thy [] pbt itms = (*root (only) ori...fmz=[]*)
   182     let
   183       fun test_d d (i, _, _, _, itm_) = (d = (I_Model.d_in itm_)) andalso i <> 0
   184       fun is_elem itms (_, (d, _)) = 
   185         case find_first (test_d d) itms of SOME _ => true | NONE => false
   186     in
   187       case filter_out (is_elem itms) pbt of
   188         (f, (d, _)) :: _ => SOME (f, ((UnparseC.term_in_thy thy) o Input_Descript.join) (d, []))
   189       | _ => NONE
   190     end
   191   | nxt_add thy oris _ itms =
   192     let
   193       fun testr_vt v ori = member op= (#2 (ori : O_Model.single)) v andalso (#3 ori) <> "#undef"
   194       fun testi_vt v itm = member op= (#2 (itm : I_Model.single)) v
   195       fun test_id ids r = member op= ids (#1 (r : O_Model.single))
   196       fun test_subset itm (_, _, _, d, ts) = 
   197         (I_Model.d_in (#5 (itm: I_Model.single))) = d andalso subset op = (I_Model.ts_in (#5 itm), ts)
   198       fun false_and_not_Sup (_, _, false, _, I_Model.Sup _) = false
   199         | false_and_not_Sup (_, _, false, _, _) = true
   200         | false_and_not_Sup _ = false
   201       val v = if itms = [] then 1 else I_Model.max_vt itms
   202       val vors = if v = 0 then oris else filter (testr_vt v) oris  (* oris..vat *)
   203       val vits =
   204         if v = 0
   205         then itms                                 (* because of dsc without dat *)
   206   	    else filter (testi_vt v) itms;                             (* itms..vat *)
   207       val icl = filter false_and_not_Sup vits;                    (* incomplete *)
   208     in
   209       if icl = [] 
   210       then case filter_out (test_id (map #1 vits)) vors of
   211   	    [] => NONE
   212   	  | miss => SOME (getr_ct thy (hd miss))
   213       else
   214         case find_first (test_subset (hd icl)) vors of
   215           NONE => raise ERROR "nxt_add: types or dsc DO NOT MATCH BETWEEN fmz --- pbt"
   216         | SOME ori => SOME (geti_ct thy ori (hd icl))
   217     end
   218 
   219 (*/------- to Specify from Specification -------\*)
   220 (* 
   221   TODO: unify with Specify.find_next_step ! ! ! USED ONLY to determine Pos.Pbl .. Pos.Met
   222 
   223    determine the next step of specification;
   224    not done here: Refine_Tacitly (otherwise *** unknown method: (..., no_met))
   225    eg. in rootpbl 'no_met': 
   226 args:
   227   preok          predicates are _all_ ok (and problem matches completely)
   228   oris           immediately from formalization 
   229   (dI',pI',mI')  specification coming from author/parent-problem
   230   (pbl,          item lists specified by user
   231    met)          -"-, tacitly completed by copy_probl
   232   (dI,pI,mI)     specification explicitly done by the user
   233   (pbt, mpc)     problem type, guard of method
   234 *)
   235 fun nxt_spec Pos.Pbl preok oris (dI', pI', mI') (pbl, met) (pbt, mpc) (dI, pI, mI) = 
   236     (if dI' = ThyC.id_empty andalso dI = ThyC.id_empty then (Pos.Pbl, Tactic.Specify_Theory dI')
   237      else if pI' = Problem.id_empty andalso pI = Problem.id_empty then (Pos.Pbl, Tactic.Specify_Problem pI')
   238      else
   239        case find_first (I_Model.is_error o #5) pbl of
   240 	       SOME (_, _, _, fd, itm_) =>
   241 	         (Pos.Pbl, P_Model.mk_delete (ThyC.get_theory (if dI = ThyC.id_empty then dI' else dI)) fd itm_)
   242 	     | NONE => 
   243 	       (case nxt_add (ThyC.get_theory (if dI = ThyC.id_empty then dI' else dI)) oris pbt pbl of
   244 	          SOME (fd, ct') => (Pos.Pbl, P_Model.mk_additem fd ct')
   245 	        | NONE => (*pbl-items complete*)
   246 	          if not preok then (Pos.Pbl, Tactic.Refine_Problem pI')
   247 	          else if dI = ThyC.id_empty then (Pos.Pbl, Tactic.Specify_Theory dI')
   248 		        else if pI = Problem.id_empty then (Pos.Pbl, Tactic.Specify_Problem pI')
   249 		        else if mI = Method.id_empty then (Pos.Pbl, Tactic.Specify_Method mI')
   250 		        else
   251 			        case find_first (I_Model.is_error o #5) met of
   252 			          SOME (_, _, _, fd, itm_) => (Pos.Met, P_Model.mk_delete (ThyC.get_theory dI) fd itm_)
   253 			        | NONE => 
   254 			          (case nxt_add (ThyC.get_theory dI) oris mpc met of
   255 				          SOME (fd, ct') => (Pos.Met, P_Model.mk_additem fd ct') (*30.8.01: pre?!?*)
   256 				        | NONE => (Pos.Met, Tactic.Apply_Method mI))))
   257   | nxt_spec Pos.Met preok oris (dI', pI', _) (_, met) (_ ,mpc) (dI, pI, mI) = 
   258     (case find_first (I_Model.is_error o #5) met of
   259       SOME (_,_,_,fd,itm_) => (Pos.Met, P_Model.mk_delete (ThyC.get_theory (if dI = ThyC.id_empty then dI' else dI)) fd itm_)
   260     | NONE => 
   261       case nxt_add (ThyC.get_theory (if dI = ThyC.id_empty then dI' else dI)) oris mpc met of
   262 	      SOME (fd,ct') => (Pos.Met, P_Model.mk_additem fd ct')
   263       | NONE => 
   264 	      (if dI = ThyC.id_empty then (Pos.Met, Tactic.Specify_Theory dI')
   265 	       else if pI = Problem.id_empty then (Pos.Met, Tactic.Specify_Problem pI')
   266 		     else if not preok then (Pos.Met, Tactic.Specify_Method mI)
   267 		     else (Pos.Met, Tactic.Apply_Method mI)))
   268   | nxt_spec p _ _ _ _ _ _ = raise ERROR ("nxt_spec: uncovered case with " ^ Pos.pos_2str p)
   269 (*\------- to Specify from Specification -------/*)
   270 
   271 (* output the headline to a ppc *)
   272 fun header p_ pI mI =
   273   case p_ of
   274     Pos.Pbl => Test_Out.Problem (if pI = Problem.id_empty then [] else pI) 
   275 	| Pos.Met => Test_Out.Method mI
   276 	| pos => raise ERROR ("header called with "^ Pos.pos_2str pos)
   277 
   278 fun make m_field (term_as_string, i_model) =
   279   case m_field of
   280     "#Given" => Tactic.Add_Given' (term_as_string, i_model)
   281   | "#Find" => Tactic.Add_Find' (term_as_string, i_model)
   282   | "#Relate" => Tactic.Add_Relation'(term_as_string, i_model)
   283   | str => raise ERROR ("specify_additem Frm, Pbl: uncovered case with " ^ str);
   284 fun get (pt, (p, _)) =
   285   case Ctree.get_obj I pt p of
   286     (Ctree.PblObj {meth, origin = (oris, (dI', pI', mI'), _), probl, spec = (dI, pI, mI), ctxt, ...})
   287       => (meth, oris, (dI', pI', mI'), probl, (dI, pI, mI), ctxt)
   288   | _ => raise ERROR "specify_additem: uncovered case of get_obj I pt p";
   289 fun specify_additem sel ct (pt, pos as (p, Pos.Met)) = 
   290       let
   291         val (met, oris, (dI', pI', mI'), pbl, (dI, pI, mI), ctxt) = get (pt, pos)
   292         val thy = if dI = ThyC.id_empty then ThyC.get_theory dI' else ThyC.get_theory dI
   293         val cpI = if pI = Problem.id_empty then pI' else pI
   294         val cmI = if mI = Method.id_empty then mI' else mI
   295         val {ppc, pre, prls, ...} = Method.from_store cmI
   296       in 
   297         case I_Model.check_single ctxt sel oris met ppc ct of
   298           I_Model.Add itm =>  (*..union old input *)
   299     	      let
   300               val met' = I_Model.add_single thy itm met
   301     	        val (p, pt') =
   302     	         case Specify_Step.add (make sel (ct, met')) (Istate_Def.Uistate, ctxt) (pt, (p, Pos.Met)) of
   303     	          ((p, Pos.Met), _, _, pt') => (p, pt')
   304     	        | _ => raise ERROR "specify_additem: uncovered case of generate1"
   305     	        val pre' = Pre_Conds.check' thy prls pre met'
   306     	        val pb = foldl and_ (true, map fst pre')
   307     	        val (p_, _) =
   308     	          nxt_spec Pos.Met pb oris (dI',pI',mI') (pbl,met') 
   309     	            ((#ppc o Problem.from_store) cpI,ppc) (dI,pI,mI);
   310     	      in 
   311               ("ok", ([], [], (pt', (p, p_))))
   312             end
   313         | I_Model.Err msg =>
   314     	      let
   315               val pre' = Pre_Conds.check' thy prls pre met
   316     	        val pb = foldl and_ (true, map fst pre')
   317     	        val (p_, _) =
   318     	          nxt_spec Pos.Met pb oris (dI',pI',mI') (pbl,met) 
   319     	            ((#ppc o Problem.from_store) cpI,(#ppc o Method.from_store) cmI) (dI,pI,mI);
   320     	      in
   321               (msg, ([], [], (pt, (p, p_))))
   322     	      end
   323       end
   324   | specify_additem sel ct (pt, pos as (p, _(*Frm, Pbl*))) =
   325       let
   326         val (met, oris, (dI', pI', mI'), pbl, (dI, pI, mI), ctxt) = get (pt, pos)
   327         val thy = if dI = ThyC.id_empty then ThyC.get_theory dI' else ThyC.get_theory dI
   328         val cpI = if pI = Problem.id_empty then pI' else pI
   329         val cmI = if mI = Method.id_empty then mI' else mI
   330         val {ppc, where_, prls, ...} = Problem.from_store cpI
   331       in
   332         case I_Model.check_single ctxt sel oris pbl ppc ct of
   333           I_Model.Add itm => (*..union old input *)
   334 	          let
   335 	            val pbl' = I_Model.add_single thy itm pbl
   336 	            val (p, pt') =
   337 	              case Specify_Step.add (make sel (ct, pbl')) (Istate_Def.Uistate, ctxt) (pt, (p, Pos.Pbl)) of
   338   	              ((p, Pos.Pbl), _, _, pt') => (p, pt')
   339   	            | _ => raise ERROR "specify_additem: uncovered case of Specify_Step.add"
   340 (* only for getting final p_ ..*)
   341 	            val pre = Pre_Conds.check' thy prls where_ pbl';
   342 	            val pb = foldl and_ (true, map fst pre);
   343 	            val (p_, _) =
   344 	              nxt_spec Pos.Pbl pb oris (dI',pI',mI') (pbl',met) (ppc, (#ppc o Method.from_store) cmI) (dI, pI, mI);
   345 	          in
   346               ("ok", ([], [], (pt', (p, p_))))
   347             end
   348         | I_Model.Err msg =>
   349 	          let
   350               val pre = Pre_Conds.check' thy prls where_ pbl
   351 	            val pb = foldl and_ (true, map fst pre)
   352 	            val (p_, _(*Tactic.input*)) =
   353 	              nxt_spec Pos.Pbl pb oris (dI', pI', mI') (pbl, met) 
   354 	                (ppc, (#ppc o Method.from_store) cmI) (dI, pI, mI)
   355 	          in
   356             (msg, ([], [], (pt, (p, p_))))
   357 	          end
   358       end
   359 
   360 (*FIXME.WN110515 declare_constraints for ct (without dsc) into PblObj{ctxt, ...}
   361   -- for input from scratch*)
   362 fun nxt_specif_additem sel ct (ptp as (pt, (p, Pos.Pbl))) = 
   363     let
   364       val (oris, dI', pI', dI, pI, pbl, ctxt) = case Ctree.get_obj I pt p of
   365         Ctree.PblObj {origin = (oris, (dI', pI', _), _), spec = (dI, pI, _), probl = pbl, ctxt, ...} =>
   366            (oris, dI', pI', dI, pI, pbl, ctxt)
   367       | _ => raise ERROR "specify (Specify_Theory': uncovered case get_obj"
   368       val thy = if dI = ThyC.id_empty then ThyC.get_theory dI' else ThyC.get_theory dI;
   369       val cpI = if pI = Problem.id_empty then pI' else pI;
   370     in
   371       case I_Model.check_single ctxt sel oris pbl ((#ppc o Problem.from_store) cpI) ct of
   372 	      I_Model.Add itm (*..union old input *) =>
   373 	        let
   374 	          val pbl' = I_Model.add_single thy itm pbl
   375 	          val (tac, tac_) = case sel of
   376 		          "#Given" => (Tactic.Add_Given    ct, Tactic.Add_Given'   (ct, pbl'))
   377 		        | "#Find"  => (Tactic.Add_Find     ct, Tactic.Add_Find'    (ct, pbl'))
   378 		        | "#Relate"=> (Tactic.Add_Relation ct, Tactic.Add_Relation'(ct, pbl'))
   379 		        | sel => raise ERROR ("nxt_specif_additem: uncovered case of" ^ sel)
   380 		        val (p, c, pt') =
   381 		          case Specify_Step.add tac_ (Istate_Def.Uistate, ctxt) (pt, (p, Pos.Pbl)) of
   382   		          ((p, Pos.Pbl), c, _, pt') =>  (p, c, pt')
   383   		        | _ => raise ERROR "nxt_specif_additem: uncovered case generate1"
   384 	        in
   385 	          ([(tac, tac_, ((p, Pos.Pbl), (Istate_Def.Uistate, ctxt)))], c, (pt', (p, Pos.Pbl)))
   386           end	       
   387 	    | I_Model.Err msg => (*TODO.WN03 pass error-msgs to the frontend..
   388                      FIXME ..and dont abuse a tactic for that purpose*)
   389 	        ([(Tactic.Tac msg, Tactic.Tac_ (ThyC.get_theory "Isac_Knowledge", msg,msg,msg),
   390 	          (Pos.e_pos', (Istate_Def.empty, ContextC.empty)))], [], ptp) 
   391     end
   392   | nxt_specif_additem sel ct (ptp as (pt, (p, Pos.Met))) = 
   393     let
   394       val (oris, dI', mI', dI, mI, met, ctxt) = case Ctree.get_obj I pt p of
   395         Ctree.PblObj {origin = (oris, (dI', _, mI'), _), spec = (dI, _, mI), meth = met,ctxt, ...} =>
   396            (oris, dI', mI', dI, mI, met, ctxt)
   397       | _ => raise ERROR "nxt_specif_additem Met: uncovered case get_obj"
   398       val thy = if dI = ThyC.id_empty then ThyC.get_theory dI' else ThyC.get_theory dI;
   399       val cmI = if mI = Method.id_empty then mI' else mI;
   400     in 
   401       case I_Model.check_single ctxt sel oris met ((#ppc o Method.from_store) cmI) ct of
   402         I_Model.Add itm (*..union old input *) =>
   403 	        let
   404 	          val met' = I_Model.add_single thy itm met;
   405 	          val (tac,tac_) = case sel of
   406 		          "#Given" => (Tactic.Add_Given    ct, Tactic.Add_Given'   (ct, met'))
   407 		        | "#Find"  => (Tactic.Add_Find     ct, Tactic.Add_Find'    (ct, met'))
   408 		        | "#Relate"=> (Tactic.Add_Relation ct, Tactic.Add_Relation'(ct, met'))
   409 		        | sel => raise ERROR ("nxt_specif_additem Met: uncovered case of" ^ sel)
   410 	          val (p, c, pt') =
   411 	            case Specify_Step.add tac_ (Istate_Def.Uistate, ctxt) (pt, (p, Pos.Met)) of
   412   	            ((p, Pos.Met), c, _, pt') => (p, c, pt')
   413   		        | _ => raise ERROR "nxt_specif_additem: uncovered case generate1 (WARNING WHY ?)"
   414 	        in
   415 	          ([(tac, tac_, ((p, Pos.Met), (Istate_Def.Uistate, ctxt)))], c, (pt', (p, Pos.Met)))
   416 	        end
   417       | I_Model.Err _ => ([(*tacis*)], [], ptp) (*nxt_me collects tacis until not hide; here just no progress*)
   418     end
   419   | nxt_specif_additem _ _ (_, p) = raise ERROR ("nxt_specif_additem not impl. for" ^ Pos.pos'2str p)
   420 
   421 (* fun tag_form thy (formal, given) = Thm.global_cterm_of thy
   422 	 (((head_of o Thm.term_of) given) $ (Thm.term_of formal)); WN100819 *)
   423 fun tag_form thy (formal, given) =
   424  (let
   425     val gf = (head_of given) $ formal;
   426     val _ = Thm.global_cterm_of thy gf
   427   in gf end)
   428   handle _ => raise ERROR ("calchead.tag_form: " ^ UnparseC.term_in_thy thy given ^
   429     " .. " ^ UnparseC.term_in_thy thy formal ^ " ..types do not match")
   430 
   431 fun chktyps thy (fs, gs) = map (tag_form thy) (fs ~~ gs)
   432 
   433 (* complete _NON_empty calc-head for autocalc (sub-)pbl from oris
   434   + met from fmz; assumes pos on PblObj, meth = []                    *)
   435 fun complete_mod (pt, pos as (p, p_)) =
   436   let
   437     val _ = if p_ <> Pos.Pbl 
   438 	    then raise ERROR ("###complete_mod: only impl.for Pbl, called with " ^ Pos.pos'2str pos)
   439 	    else ()
   440 	  val (oris, ospec, probl, spec) = case Ctree.get_obj I pt p of
   441 	    Ctree.PblObj {origin = (oris, ospec, _), probl, spec, ...} => (oris, ospec, probl, spec)
   442 	  | _ => raise ERROR "complete_mod: unvered case get_obj"
   443   	val (_, pI, mI) = References.select ospec spec
   444   	val mpc = (#ppc o Method.from_store) mI
   445   	val ppc = (#ppc o Problem.from_store) pI
   446   	val (pits, mits) = I_Model.complete_method (oris, mpc, ppc, probl)
   447     val pt = Ctree.update_pblppc pt p pits
   448 	  val pt = Ctree.update_metppc pt p mits
   449   in (pt, (p, Pos.Met)) end
   450 
   451 (* do all specification in one single step:
   452    complete calc-head for autocalc (sub-)pbl from oris (+ met from fmz);
   453    oris and spec (incl. pbl-refinement) given from init_calc or SubProblem
   454 *)
   455 fun all_modspec (pt, (p, _)) =
   456   let
   457     val (pors, dI, pI, mI) = case Ctree.get_obj I pt p of
   458       Ctree.PblObj {origin = (pors, (dI, pI, mI), _), ...}
   459         => (pors, dI, pI, mI)
   460     | _ => raise ERROR "all_modspec: uncovered case get_obj"
   461 	  val {ppc, ...} = Method.from_store mI
   462     val (_, vals) = O_Model.values' pors
   463 	  val ctxt = ContextC.initialise dI vals
   464     val (pt, _) = Ctree.cupdate_problem pt p ((dI, pI, mI),
   465       map (I_Model.complete' ppc) pors, map (I_Model.complete' ppc) pors, ctxt)
   466   in
   467     (pt, (p, Pos.Met))
   468   end
   469 
   470 (*/------- to I_Model from Specification -------\* )
   471 (* WN0312: use in nxt_spec, too ? what about variants ??? *)
   472 fun is_complete_mod_ [] = false
   473   | is_complete_mod_ itms = foldl and_ (true, (map #3 itms))
   474 ( *\------- to I_Model from Specification -------/*)
   475 
   476 fun is_complete_mod (pt, pos as (p, Pos.Pbl)) =
   477     if (Ctree.is_pblobj o (Ctree.get_obj I pt)) p 
   478     then (I_Model.is_complete o (Ctree.get_obj Ctree.g_pbl pt)) p
   479     else raise ERROR ("is_complete_mod: called by PrfObj at " ^ Pos.pos'2str pos)
   480   | is_complete_mod (pt, pos as (p, Pos.Met)) = 
   481     if (Ctree.is_pblobj o (Ctree.get_obj I pt)) p 
   482     then (I_Model.is_complete o (Ctree.get_obj Ctree.g_met pt)) p
   483     else raise ERROR ("is_complete_mod: called by PrfObj at " ^ Pos.pos'2str pos)
   484   | is_complete_mod (_, pos) =
   485     raise ERROR ("is_complete_mod called by " ^ Pos.pos'2str pos ^ " (should be Pbl or Met)")
   486 
   487 fun is_complete_modspec ptp = is_complete_mod ptp andalso References.are_complete ptp
   488 
   489 
   490 (** get the formula from a ctree-node **)
   491 (*
   492   take form+res from PblObj and 1.PrfObj and (PrfObj after PblObj)
   493   take res from all other PrfObj's
   494   Designed for interSteps, outcommented 04 in favour of calcChangedEvent
   495 *)
   496 fun formres p (Ctree.Nd (Ctree.PblObj {origin = (_, _, h), result = (r, _), ...}, _)) =
   497     [("headline", (p, Pos.Frm), h), ("stepform", (p, Pos.Res), r)]
   498   | formres p (Ctree.Nd (Ctree.PrfObj {form, result = (r, _), ...}, _)) = 
   499     [("stepform", (p, Pos.Frm), form), ("stepform", (p, Pos.Res), r)]
   500   | formres _ _ = raise ERROR "formres: uncovered definition" 
   501 fun form p (Ctree.Nd (Ctree.PrfObj {result = (r, _), ...}, _)) = 
   502     [("stepform", (p, Pos.Res), r)]
   503   | form _ _ = raise ERROR "form: uncovered definition" 
   504 
   505 (* assumes to take whole level, in particular hd -- for use in interSteps *)
   506 fun get_formress fs _ [] = flat fs
   507   | get_formress fs p (nd::nds) =
   508     (* start with   'form+res'       and continue with trying 'res' only*)
   509     get_forms (fs @ [formres p nd]) (Pos.lev_on p) nds
   510 and get_forms fs _ [] = flat fs
   511   | get_forms fs p (nd::nds) =
   512     if Ctree.is_pblnd nd
   513     (* start again with      'form+res' ///ugly repeat with Check_elementwise
   514     then get_formress (fs @ [formres p nd]) (lev_on p) nds                   *)
   515     then get_forms    (fs @ [formres p nd]) (Pos.lev_on p) nds
   516     (* continue with trying 'res' only*)
   517     else get_forms    (fs @ [form    p nd]) (Pos.lev_on p) nds;
   518 
   519 
   520 (* get a calchead from a PblObj-node in the ctree; preconditions must be calculated *)
   521 fun get_ocalhd (pt, (p, Pos.Pbl)) = 
   522     let
   523 	    val (ospec, hdf', spec, probl) = case Ctree.get_obj I pt p of
   524 	      Ctree.PblObj {origin = (_, ospec, hdf'), spec, probl,...} => (ospec, hdf', spec, probl)
   525 	    | _ => raise ERROR "get_ocalhd Pbl: uncovered case get_obj"
   526       val {prls, where_, ...} = Problem.from_store (#2 (References.select ospec spec))
   527       val pre = Pre_Conds.check' (ThyC.get_theory "Isac_Knowledge") prls where_ probl
   528     in
   529       (ocalhd_complete probl pre spec, Pos.Pbl, hdf', probl, pre, spec)
   530     end
   531   | get_ocalhd (pt, (p, Pos.Met)) = 
   532     let
   533 		  val (ospec, hdf', spec, meth) = case Ctree.get_obj I pt p of
   534 		    Ctree.PblObj {origin = (_, ospec, hdf'), spec, meth, ...} => (ospec, hdf', spec, meth)
   535 		  | _ => raise ERROR "get_ocalhd Met: uncovered case get_obj"
   536       val {prls, pre, ...} = Method.from_store (#3 (References.select ospec spec))
   537       val pre = Pre_Conds.check' (ThyC.get_theory "Isac_Knowledge") prls pre meth
   538     in
   539       (ocalhd_complete meth pre spec, Pos.Met, hdf', meth, pre, spec)
   540     end
   541   | get_ocalhd _ = raise ERROR "get_ocalhd: uncovered definition"
   542 
   543 (* at the activeFormula set the Specification 
   544    to empty and return a CalcHead;
   545    the 'origin' remains (for reconstructing all that) *)
   546 fun reset_calchead (pt, (p,_)) = 
   547   let
   548     val () = case Ctree.get_obj I pt p of
   549       Ctree.PblObj _ => ()
   550     | _ => raise ERROR "reset_calchead: uncovered case get_obj"
   551     val pt = Ctree.update_pbl pt p []
   552     val pt = Ctree.update_met pt p []
   553     val pt = Ctree.update_spec pt p References.empty
   554   in (pt, (p, Pos.Pbl)) end
   555 
   556 (**)end(**)