src/Tools/isac/Interpret/calchead.sml
branchisac-update-Isa09-2
changeset 37947 22235e4dbe5f
parent 37938 f6164be9280d
child 37984 972a73d7c50b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/Tools/isac/Interpret/calchead.sml	Wed Aug 25 16:20:07 2010 +0200
     1.3 @@ -0,0 +1,2257 @@
     1.4 +(* Specify-phase: specifying and modeling a problem or a subproblem. The
     1.5 +   most important types are declared in mstools.sml.
     1.6 +   author: Walther Neuper
     1.7 +   991122
     1.8 +   (c) due to copyright terms
     1.9 +
    1.10 +use"ME/calchead.sml";
    1.11 +use"calchead.sml";
    1.12 +12345678901234567890123456789012345678901234567890123456789012345678901234567890
    1.13 +        10        20        30        40        50        60        70        80
    1.14 +*)
    1.15 +
    1.16 +(* TODO interne Funktionen aus sig entfernen *)
    1.17 +signature CALC_HEAD =
    1.18 +  sig
    1.19 +    datatype additm = Add of SpecifyTools.itm | Err of string
    1.20 +    val all_dsc_in : SpecifyTools.itm_ list -> Term.term list
    1.21 +    val all_modspec : ptree * pos' -> ptree * pos'
    1.22 +    datatype appl = Appl of tac_ | Notappl of string
    1.23 +    val appl_add :
    1.24 +       theory ->
    1.25 +       string ->
    1.26 +       SpecifyTools.ori list ->
    1.27 +       SpecifyTools.itm list ->
    1.28 +       (string * (Term.term * Term.term)) list -> cterm' -> additm
    1.29 +    type calcstate
    1.30 +    type calcstate'
    1.31 +    val chk_vars : term ppc -> string * Term.term list
    1.32 +    val chktyp :
    1.33 +       theory -> int * term list * term list -> term
    1.34 +    val chktyps :
    1.35 +       theory -> term list * term list -> term list
    1.36 +    val complete_metitms :
    1.37 +   SpecifyTools.ori list ->
    1.38 +   SpecifyTools.itm list ->
    1.39 +   SpecifyTools.itm list -> pat list -> SpecifyTools.itm list
    1.40 +    val complete_mod_ : ori list * pat list * pat list * itm list ->
    1.41 +			itm list * itm list
    1.42 +    val complete_mod : ptree * pos' -> ptree * (pos * pos_)
    1.43 +    val complete_spec : ptree * pos' -> ptree * pos'
    1.44 +    val cpy_nam :
    1.45 +       pat list -> preori list -> pat -> preori
    1.46 +    val e_calcstate : calcstate
    1.47 +    val e_calcstate' : calcstate'
    1.48 +    val eq1 : ''a -> 'b * (''a * 'c) -> bool
    1.49 +    val eq3 :
    1.50 +       ''a -> Term.term -> 'b * 'c * 'd * ''a * SpecifyTools.itm_ -> bool
    1.51 +    val eq4 : ''a -> 'b * ''a list * 'c * 'd * 'e -> bool
    1.52 +    val eq5 :
    1.53 +       'a * 'b * 'c * 'd * SpecifyTools.itm_ ->
    1.54 +       'e * 'f * 'g * Term.term * 'h -> bool
    1.55 +    val eq_dsc : SpecifyTools.itm * SpecifyTools.itm -> bool
    1.56 +    val eq_pos' : ''a * pos_ -> ''a * pos_ -> bool
    1.57 +    val f_mout : theory -> mout -> Term.term
    1.58 +    val filter_outs :
    1.59 +       SpecifyTools.ori list ->
    1.60 +       SpecifyTools.itm list -> SpecifyTools.ori list
    1.61 +    val filter_pbt :
    1.62 +       SpecifyTools.ori list ->
    1.63 +       ('a * (Term.term * 'b)) list -> SpecifyTools.ori list
    1.64 +    val foldl1 : ('a * 'a -> 'a) -> 'a list -> 'a
    1.65 +    val foldr1 : ('a * 'a -> 'a) -> 'a list -> 'a
    1.66 +    val form : 'a -> ptree -> (string * ('a * pos_) * Term.term) list
    1.67 +    val formres : 'a -> ptree -> (string * ('a * pos_) * Term.term) list
    1.68 +    val gen_ins' : ('a * 'a -> bool) -> 'a * 'a list -> 'a list
    1.69 +    val get_formress :
    1.70 +       (string * (pos * pos_) * Term.term) list list ->
    1.71 +       pos -> ptree list -> (string * (pos * pos_) * Term.term) list
    1.72 +    val get_forms :
    1.73 +       (string * (pos * pos_) * Term.term) list list ->
    1.74 +       posel list -> ptree list -> (string * (pos * pos_) * Term.term) list
    1.75 +    val get_interval : pos' -> pos' -> int -> ptree -> (pos' * term) list
    1.76 +    val get_ocalhd : ptree * pos' -> ocalhd
    1.77 +    val get_spec_form : tac_ -> pos' -> ptree -> mout
    1.78 +    val geti_ct :
    1.79 +       theory ->
    1.80 +       SpecifyTools.ori -> SpecifyTools.itm -> string * cterm'
    1.81 +    val getr_ct : theory -> SpecifyTools.ori -> string * cterm'
    1.82 +    val has_list_type : Term.term -> bool
    1.83 +    val header : pos_ -> pblID -> metID -> pblmet
    1.84 +    val insert_ppc :
    1.85 +       theory ->
    1.86 +       int * SpecifyTools.vats * bool * string * SpecifyTools.itm_ ->
    1.87 +       SpecifyTools.itm list -> SpecifyTools.itm list
    1.88 +    val insert_ppc' :
    1.89 +       SpecifyTools.itm -> SpecifyTools.itm list -> SpecifyTools.itm list
    1.90 +    val is_complete_mod : ptree * pos' -> bool
    1.91 +    val is_complete_mod_ : SpecifyTools.itm list -> bool
    1.92 +    val is_complete_modspec : ptree * pos' -> bool
    1.93 +    val is_complete_spec : ptree * pos' -> bool
    1.94 +    val is_copy_named : 'a * ('b * Term.term) -> bool
    1.95 +    val is_copy_named_idstr : string -> bool
    1.96 +    val is_error : SpecifyTools.itm_ -> bool
    1.97 +    val is_field_correct : ''a -> ''b -> (''a * ''b list) list -> bool
    1.98 +    val is_known :
    1.99 +       theory ->
   1.100 +       string ->
   1.101 +       SpecifyTools.ori list ->
   1.102 +       Term.term -> string * SpecifyTools.ori * Term.term list
   1.103 +    val is_list_type : Term.typ -> bool
   1.104 +    val is_notyet_input :
   1.105 +       theory ->
   1.106 +       SpecifyTools.itm list ->
   1.107 +       Term.term list ->
   1.108 +       SpecifyTools.ori ->
   1.109 +       ('a * (Term.term * Term.term)) list -> string * SpecifyTools.itm
   1.110 +    val is_parsed : SpecifyTools.itm_ -> bool
   1.111 +    val is_untouched : SpecifyTools.itm -> bool
   1.112 +    val matc :
   1.113 +       theory ->
   1.114 +       pat list ->
   1.115 +       Term.term list ->
   1.116 +       (int list * string * Term.term * Term.term list) list ->
   1.117 +       (int list * string * Term.term * Term.term list) list
   1.118 +    val match_ags :
   1.119 +       theory -> pat list -> Term.term list -> SpecifyTools.ori list
   1.120 +    val maxl : int list -> int
   1.121 +    val match_ags_msg : string list -> Term.term -> Term.term list -> unit
   1.122 +    val memI : ''a list -> ''a -> bool
   1.123 +    val mk_additem : string -> cterm' -> tac
   1.124 +    val mk_delete : theory -> string -> SpecifyTools.itm_ -> tac
   1.125 +    val mtc :
   1.126 +       theory -> pat -> Term.term -> SpecifyTools.preori option
   1.127 +    val nxt_add :
   1.128 +       theory ->
   1.129 +       SpecifyTools.ori list ->
   1.130 +       (string * (Term.term * 'a)) list ->
   1.131 +       SpecifyTools.itm list -> (string * cterm') option
   1.132 +    val nxt_model_pbl : tac_ -> ptree * (int list * pos_) -> tac_
   1.133 +    val nxt_spec :
   1.134 +       pos_ ->
   1.135 +       bool ->
   1.136 +       SpecifyTools.ori list ->
   1.137 +       spec ->
   1.138 +       SpecifyTools.itm list * SpecifyTools.itm list ->
   1.139 +       (string * (Term.term * 'a)) list * (string * (Term.term * 'b)) list ->
   1.140 +       spec -> pos_ * tac
   1.141 +    val nxt_specif : tac -> ptree * (int list * pos_) -> calcstate'
   1.142 +    val nxt_specif_additem :
   1.143 +       string -> cterm' -> ptree * (int list * pos_) -> calcstate'
   1.144 +    val nxt_specify_init_calc : fmz -> calcstate
   1.145 +    val ocalhd_complete :
   1.146 +       SpecifyTools.itm list ->
   1.147 +       (bool * Term.term) list -> domID * pblID * metID -> bool
   1.148 +    val ori2Coritm :
   1.149 +	pat list -> ori -> itm
   1.150 +    val ori_2itm :
   1.151 +       'a ->
   1.152 +       SpecifyTools.itm_ ->
   1.153 +       Term.term -> Term.term list -> SpecifyTools.ori -> SpecifyTools.itm
   1.154 +    val overwrite_ppc :
   1.155 +       theory ->
   1.156 +       int * SpecifyTools.vats * bool * string * SpecifyTools.itm_ ->
   1.157 +       SpecifyTools.itm list ->
   1.158 +       (int * SpecifyTools.vats * bool * string * SpecifyTools.itm_) list
   1.159 +    val parse_ok : SpecifyTools.itm_ list -> bool
   1.160 +    val posform2str : pos' * ptform -> string
   1.161 +    val posforms2str : (pos' * ptform) list -> string
   1.162 +    val posterms2str : (pos' * term) list -> string (*tests only*)
   1.163 +    val ppc135list : 'a SpecifyTools.ppc -> 'a list
   1.164 +    val ppc2list : 'a SpecifyTools.ppc -> 'a list
   1.165 +    val pt_extract :
   1.166 +       ptree * (int list * pos_) ->
   1.167 +       ptform * tac option * Term.term list
   1.168 +    val pt_form : ppobj -> ptform
   1.169 +    val pt_model : ppobj -> pos_ -> ptform
   1.170 +    val reset_calchead : ptree * pos' -> ptree * pos'
   1.171 +    val seek_oridts :
   1.172 +       theory ->
   1.173 +       string ->
   1.174 +       Term.term * Term.term list ->
   1.175 +       (int * SpecifyTools.vats * string * Term.term * Term.term list) list
   1.176 +       -> string * SpecifyTools.ori * Term.term list
   1.177 +    val seek_orits :
   1.178 +       theory ->
   1.179 +       string ->
   1.180 +       Term.term list ->
   1.181 +       (int * SpecifyTools.vats * string * Term.term * Term.term list) list
   1.182 +       -> string * SpecifyTools.ori * Term.term list
   1.183 +    val seek_ppc :
   1.184 +       int -> SpecifyTools.itm list -> SpecifyTools.itm option
   1.185 +    val show_pt : ptree -> unit
   1.186 +    val some_spec : spec -> spec -> spec
   1.187 +    val specify :
   1.188 +       tac_ ->
   1.189 +       pos' ->
   1.190 +       cid ->
   1.191 +       ptree ->
   1.192 +       (posel list * pos_) * ((posel list * pos_) * istate) * mout * tac *
   1.193 +       safe * ptree
   1.194 +    val specify_additem :
   1.195 +       string ->
   1.196 +       cterm' * 'a ->
   1.197 +       int list * pos_ ->
   1.198 +       'b ->
   1.199 +       ptree ->
   1.200 +       (pos * pos_) * ((pos * pos_) * istate) * mout * tac * safe * ptree
   1.201 +    val tag_form : theory -> term * term -> term
   1.202 +    val test_types : theory -> Term.term * Term.term list -> string
   1.203 +    val typeless : Term.term -> Term.term
   1.204 +    val unbound_ppc : term SpecifyTools.ppc -> Term.term list
   1.205 +    val vals_of_oris : SpecifyTools.ori list -> Term.term list
   1.206 +    val variants_in : Term.term list -> int
   1.207 +    val vars_of_pbl_ : ('a * ('b * Term.term)) list -> Term.term list
   1.208 +    val vars_of_pbl_' : ('a * ('b * Term.term)) list -> Term.term list
   1.209 +  end
   1.210 + 
   1.211 +
   1.212 +
   1.213 +
   1.214 +
   1.215 +(*---------------------------------------------------------------------*)
   1.216 +structure CalcHead (**): CALC_HEAD(**) =
   1.217 +
   1.218 +struct
   1.219 +(*---------------------------------------------------------------------*)
   1.220 +
   1.221 +(* datatypes *)
   1.222 +
   1.223 +(*.the state wich is stored after each step of calculation; it contains
   1.224 +   the calc-state and a list of [tac,istate](="tacis") to be applied.
   1.225 +   the last_elem tacis is the first to apply to the calc-state and
   1.226 +   the (only) one shown to the front-end as the 'proposed tac'.
   1.227 +   the calc-state resulting from the application of tacis is not stored,
   1.228 +   because the tacis hold enought information for efficiently rebuilding
   1.229 +   this state just by "fun generate ".*)
   1.230 +type calcstate = 
   1.231 +     (ptree * pos') *    (*the calc-state to which the tacis could be applied*)
   1.232 +     (taci list);        (*ev. several (hidden) steps; 
   1.233 +                           in REVERSE order: first tac_ to apply is last_elem*)
   1.234 +val e_calcstate = ((EmptyPtree, e_pos'), [e_taci]):calcstate;
   1.235 +
   1.236 +(*the state used during one calculation within the mathengine; it contains
   1.237 +  a list of [tac,istate](="tacis") which generated the the calc-state;
   1.238 +  while this state's tacis are extended by each (internal) step,
   1.239 +  the calc-state is used for creating new nodes in the calc-tree
   1.240 +  (eg. applicable_in requires several particular nodes of the calc-tree)
   1.241 +  and then replaced by the the newly created;
   1.242 +  on leave of the mathengine the resuing calc-state is dropped anyway,
   1.243 +  because the tacis hold enought information for efficiently rebuilding
   1.244 +  this state just by "fun generate ".*)
   1.245 +type calcstate' = 
   1.246 +     taci list *        (*cas. several (hidden) steps; 
   1.247 +                          in REVERSE order: first tac_ to apply is last_elem*)
   1.248 +     pos' list *        (*a "continuous" sequence of pos',
   1.249 +			 deleted by application of taci list*)     
   1.250 +     (ptree * pos');    (*the calc-state resulting from the application of tacis*)
   1.251 +val e_calcstate' = ([e_taci], [e_pos'], (EmptyPtree, e_pos')):calcstate';
   1.252 +
   1.253 +(*FIXXXME.WN020430 intermediate hack for fun ass_up*)
   1.254 +fun f_mout thy (Form' (FormKF (_,_,_,_,f))) = (term_of o the o (parse thy)) f
   1.255 +  | f_mout thy _ = raise error "f_mout: not called with formula";
   1.256 +
   1.257 +
   1.258 +(*.is the calchead complete ?.*)
   1.259 +fun ocalhd_complete (its: itm list) (pre: (bool * term) list) (dI,pI,mI) = 
   1.260 +    foldl and_ (true, map #3 its) andalso 
   1.261 +    foldl and_ (true, map #1 pre) andalso 
   1.262 +    dI<>e_domID andalso pI<>e_pblID andalso mI<>e_metID;
   1.263 +
   1.264 +
   1.265 +(* make a term 'typeless' for comparing with another 'typeless' term;
   1.266 +   'type-less' usually is illtyped                                  *)
   1.267 +fun typeless (Const(s,_)) = (Const(s,e_type)) 
   1.268 +  | typeless (Free(s,_)) = (Free(s,e_type))
   1.269 +  | typeless (Var(n,_)) = (Var(n,e_type))
   1.270 +  | typeless (Bound i) = (Bound i)
   1.271 +  | typeless (Abs(s,_,t)) = Abs(s,e_type, typeless t)
   1.272 +  | typeless (t1 $ t2) = (typeless t1) $ (typeless t2);
   1.273 +(*
   1.274 +> val (SOME ct) = parse thy "max_relation (A=#2*a*b - a^^^#2)";
   1.275 +> val (_,t1) = split_dsc_t hs (term_of ct);
   1.276 +> val (SOME ct) = parse thy "A=#2*a*b - a^^^#2";
   1.277 +> val (_,t2) = split_dsc_t hs (term_of ct);
   1.278 +> typeless t1 = typeless t2;
   1.279 +val it = true : bool
   1.280 +*)
   1.281 +
   1.282 +
   1.283 +
   1.284 +(*.to an input (d,ts) find the according ori and insert the ts.*)
   1.285 +(*WN.11.03: + dont take first inter<>[]*)
   1.286 +fun seek_oridts thy sel (d,ts) [] = 
   1.287 +  ("'"^(Syntax.string_of_term (thy2ctxt thy) (comp_dts thy (d,ts)))^
   1.288 +   "' not found (typed)", (0,[],sel,d,ts):ori, [])
   1.289 +  (* val (id,vat,sel',d',ts')::oris = ori;
   1.290 +     val (id,vat,sel',d',ts') = ori;
   1.291 +     *)
   1.292 +  | seek_oridts thy sel (d,ts) ((id,vat,sel',d',ts')::(oris:ori list)) =
   1.293 +    if sel = sel' andalso d=d' andalso (inter op = ts ts') <> [] 
   1.294 +    then if sel = sel' 
   1.295 +	 then ("", 
   1.296 +               (id,vat,sel,d, inter op = ts ts'):ori, 
   1.297 +               ts')
   1.298 +	 else ((Syntax.string_of_term (thy2ctxt thy) (comp_dts thy (d,ts))) 
   1.299 +               ^ " not for " ^ sel, 
   1.300 +               e_ori_, 
   1.301 +               [])
   1.302 +    else seek_oridts thy sel (d,ts) oris;
   1.303 +
   1.304 +(*.to an input (_,ts) find the according ori and insert the ts.*)
   1.305 +fun seek_orits thy sel ts [] = 
   1.306 +  ("'"^
   1.307 +   (strs2str (map (Syntax.string_of_term (thy2ctxt thy)) ts))^
   1.308 +   "' not found (typed)", e_ori_, [])
   1.309 +  | seek_orits thy sel ts ((id,vat,sel',d,ts')::(oris:ori list)) =
   1.310 +    if sel = sel' andalso (inter op = ts ts') <> [] 
   1.311 +      then if sel = sel' 
   1.312 +	   then ("",
   1.313 +                 (id,vat,sel,d, inter op = ts ts'):ori, 
   1.314 +                 ts')
   1.315 +	   else (((strs2str' o map (Syntax.string_of_term (thy2ctxt thy))) ts)
   1.316 +                 ^ " not for "^sel, 
   1.317 +                 e_ori_, 
   1.318 +                 [])
   1.319 +    else seek_orits thy sel ts oris;
   1.320 +(* false
   1.321 +> val ((id,vat,sel',d,ts')::(ori':ori)) = ori;
   1.322 +> seek_orits thy sel ts [(id,vat,sel',d,ts')];
   1.323 +uncaught exception TYPE
   1.324 +> seek_orits thy sel ts [];
   1.325 +uncaught exception TYPE
   1.326 +*)
   1.327 +
   1.328 +(*find_first item with #1 equal to id*)
   1.329 +fun seek_ppc id [] = NONE
   1.330 +  | seek_ppc id (p::(ppc:itm list)) =
   1.331 +    if id = #1 p then SOME p else seek_ppc id ppc;
   1.332 +
   1.333 +
   1.334 +
   1.335 +(*---------------------------------------------(3) nach ptyps.sml 23.3.02*)
   1.336 +
   1.337 +
   1.338 +datatype appl = Appl of tac_ | Notappl of string;
   1.339 +
   1.340 +fun ppc2list ({Given=gis,Where=whs,Find=fis,
   1.341 +	       With=wis,Relate=res}: 'a ppc) =
   1.342 +  gis @ whs @ fis @ wis @ res;
   1.343 +fun ppc135list ({Given=gis,Find=fis,Relate=res,...}: 'a ppc) =
   1.344 +  gis @ fis @ res;
   1.345 +
   1.346 +
   1.347 +
   1.348 +
   1.349 +(* get the number of variants in a problem in 'original',
   1.350 +   assumes equal descriptions in immediate sequence    *)
   1.351 +fun variants_in ts =
   1.352 +  let fun eq(x,y) = head_of x = head_of y;
   1.353 +    fun cnt eq [] y n = ([n],[])
   1.354 +      | cnt eq (x::xs) y n = if eq(x,y) then cnt eq xs y (n+1)
   1.355 +			     else ([n], x::xs);
   1.356 +    fun coll eq  xs [] = xs
   1.357 +      | coll eq  xs (y::ys) = 
   1.358 +      let val (n,ys') = cnt eq (y::ys) y 0;
   1.359 +      in if ys' = [] then xs @ n else coll eq  (xs @ n) ys' end;
   1.360 +    val vts = subtract op = [1] (distinct (coll eq [] ts));
   1.361 +  in case vts of [] => 1 | [n] => n
   1.362 +      | _ => error "different variants in formalization" end;
   1.363 +(*
   1.364 +> cnt (op=) [2,2,2,4,5,5,5,5,5] 2 0;
   1.365 +val it = ([3],[4,5,5,5,5,5]) : int list * int list
   1.366 +> coll (op=) [] [1,2,2,2,4,5,5,5,5,5];
   1.367 +val it = [1,3,1,5] : int list
   1.368 +*)
   1.369 +
   1.370 +fun is_list_type (Type("List.list",_)) = true
   1.371 +  | is_list_type _ = false;
   1.372 +(* fun destr (Type(str,sort)) = (str,sort);
   1.373 +> val (SOME ct) = parse thy "lll::real list";
   1.374 +> val ty = (#T o rep_cterm) ct;
   1.375 +> is_list_type ty;
   1.376 +val it = true : bool 
   1.377 +> destr ty;
   1.378 +val it = ("List.list",["RealDef.real"]) : string * typ list
   1.379 +> atomty ((#t o rep_cterm) ct);
   1.380 +*** -------------
   1.381 +*** Free ( lll, real list)
   1.382 +val it = () : unit
   1.383 + 
   1.384 +> val (SOME ct) = parse thy "[lll::real]";
   1.385 +> val ty = (#T o rep_cterm) ct;
   1.386 +> is_list_type ty;
   1.387 +val it = true : bool 
   1.388 +> destr ty;
   1.389 +val it = ("List.list",["'a"]) : string * typ list
   1.390 +> atomty ((#t o rep_cterm) ct);
   1.391 +*** -------------
   1.392 +*** Const ( List.list.Cons, [real, real list] => real list)
   1.393 +***   Free ( lll, real)
   1.394 +***   Const ( List.list.Nil, real list) 
   1.395 +
   1.396 +> val (SOME ct) = parse thy "lll";
   1.397 +> val ty = (#T o rep_cterm) ct;
   1.398 +> is_list_type ty;
   1.399 +val it = false : bool  *)
   1.400 +
   1.401 +
   1.402 +fun has_list_type (Free(_,T)) = is_list_type T
   1.403 +  | has_list_type _ = false;
   1.404 +(*
   1.405 +> val (SOME ct) = parse thy "lll::real list";
   1.406 +> has_list_type (term_of ct);
   1.407 +val it = true : bool
   1.408 +> val (SOME ct) = parse thy "[lll::real]";
   1.409 +> has_list_type (term_of ct);
   1.410 +val it = false : bool *)
   1.411 +
   1.412 +fun is_parsed (Syn _) = false
   1.413 +  | is_parsed _ = true;
   1.414 +fun parse_ok its = foldl and_ (true, map is_parsed its);
   1.415 +
   1.416 +fun all_dsc_in itm_s =
   1.417 +  let    
   1.418 +    fun d_in (Cor ((d,_),_)) = [d]
   1.419 +      | d_in (Syn c) = []
   1.420 +      | d_in (Typ c) = []
   1.421 +      | d_in (Inc ((d,_),_)) = [d]
   1.422 +      | d_in (Sup (d,_)) = [d]
   1.423 +      | d_in (Mis (d,_)) = [d];
   1.424 +  in (flat o (map d_in)) itm_s end;  
   1.425 +
   1.426 +(* 30.1.00 ---
   1.427 +fun is_Syn (Syn _) = true
   1.428 +  | is_Syn (Typ _) = true
   1.429 +  | is_Syn _ = false;
   1.430 + --- *)
   1.431 +fun is_error (Cor (_,ts)) = false
   1.432 +  | is_error (Sup (_,ts)) = false
   1.433 +  | is_error (Inc (_,ts)) = false
   1.434 +  | is_error (Mis (_,ts)) = false
   1.435 +  | is_error _ = true;
   1.436 +
   1.437 +(* 30.1.00 ---
   1.438 +fun ct_in (Syn (c)) = c
   1.439 +  | ct_in (Typ (c)) = c
   1.440 +  | ct_in _ = raise error "ct_in called for Cor .. Sup";
   1.441 + --- *)
   1.442 +
   1.443 +(*#############################################################*)
   1.444 +(*#############################################################*)
   1.445 +(* vvv--- aus nnewcode.sml am 30.1.00 ---vvv *)
   1.446 +
   1.447 +
   1.448 +(* testdaten besorgen:
   1.449 +   use"test-coil-kernel.sml";
   1.450 +   val (PblObj{origin=(oris,_,_),meth={ppc=itms,...},...}) = 
   1.451 +        get_obj I pt p;
   1.452 +  *)
   1.453 +
   1.454 +(* given oris, ppc, 
   1.455 +   variant V: oris union ppc => int, id ID: oris union ppc => int
   1.456 +
   1.457 +   ppc is_complete == 
   1.458 +     EX vt:V. ALL r:oris --> EX i:ppc. ID r = ID i  &  complete i
   1.459 +
   1.460 +   and
   1.461 +     @vt = max sum(i : ppc) V i
   1.462 +*)
   1.463 +
   1.464 +
   1.465 +
   1.466 +(*
   1.467 +> ((vts_cnt (vts_in itms))) itms;
   1.468 +
   1.469 +
   1.470 +
   1.471 +---^^--test 10.3.
   1.472 +> val vts = vts_in itms;
   1.473 +val vts = [1,2,3] : int list
   1.474 +> val nvts = vts_cnt vts itms;
   1.475 +val nvts = [(1,6),(2,5),(3,7)] : (int * int) list
   1.476 +> val mx = max2 nvts;
   1.477 +val mx = (3,7) : int * int
   1.478 +> val v = max_vt itms;
   1.479 +val v = 3 : int
   1.480 +--------------------------
   1.481 +> 
   1.482 +*)
   1.483 +
   1.484 +(*.get the first term in ts from ori.*)
   1.485 +(* val (_,_,fd,d,ts) = hd miss;
   1.486 +   *)
   1.487 +fun getr_ct thy ((_,_,fd,d,ts):ori) =
   1.488 +  (fd, ((Syntax.string_of_term (thy2ctxt thy)) o 
   1.489 +        (comp_dts thy)) (d,[hd ts]):cterm');
   1.490 +(* val t = comp_dts thy (d,[hd ts]);
   1.491 +   *)
   1.492 +
   1.493 +(* get a term from ori, notyet input in itm *)
   1.494 +fun geti_ct thy ((_,_,_,d,ts):ori) ((_,_,_,fd,itm_):itm) =  
   1.495 +  (fd, ((Syntax.string_of_term (thy2ctxt thy)) o (comp_dts thy)) 
   1.496 +           (d, subtract op = (ts_in itm_) ts):cterm');
   1.497 +(* test-maximum.sml fmy <> [], Init_Proof ...
   1.498 +   val (_,_,_,d,ts) = ori; val (_,_,_,fd,itm_) = hd icl;
   1.499 +   val d' $ ts' = (term_of o the o (parse thy)) "fixedValues [r=Arbfix]";
   1.500 +   atomty d;
   1.501 +   atomty d';
   1.502 +   atomty (hd ts);
   1.503 +   atomty ts';
   1.504 +   cterm_of thy (d $ (hd ts));
   1.505 +   cterm_of thy (d' $ ts');
   1.506 +
   1.507 +   comp_dts thy (d,ts);
   1.508 +   *)
   1.509 +
   1.510 +
   1.511 +(* in FE dsc, not dat: this is in itms ...*)
   1.512 +fun is_untouched ((_,_,false,_,Inc((_,[]),_)):itm) = true
   1.513 +  | is_untouched _ = false;
   1.514 +
   1.515 +
   1.516 +(* select an item in oris, notyet input in itms 
   1.517 +   (precondition: in itms are only Cor, Sup, Inc) *)
   1.518 +local infix mem;
   1.519 +fun x mem [] = false
   1.520 +  | x mem (y :: ys) = x = y orelse x mem ys;
   1.521 +in 
   1.522 +fun nxt_add thy ([]:ori list) pbt itms = (*root (only) ori...fmz=[]*)
   1.523 +  let
   1.524 +    fun test_d d ((i,_,_,_,itm_):itm) = (d = (d_in itm_)) andalso i<>0; 
   1.525 +    fun is_elem itms (f,(d,t)) = 
   1.526 +      case find_first (test_d d) itms of 
   1.527 +	SOME _ => true | NONE => false;
   1.528 +  in case filter_out (is_elem itms) pbt of
   1.529 +(* val ((f,(d,_))::itms) = filter_out (is_elem itms) pbt;
   1.530 +   *)
   1.531 +    (f,(d,_))::itms => 
   1.532 +      SOME (f:string, ((Syntax.string_of_term (thy2ctxt thy)) o comp_dts thy) (d,[]):cterm')
   1.533 +  | _ => NONE end
   1.534 +
   1.535 +(* val (thy,itms) = (assoc_thy (if dI=e_domID then dI' else dI),pbl);
   1.536 +   *)
   1.537 +  | nxt_add thy oris pbt itms =
   1.538 +  let
   1.539 +    fun testr_vt v ori = (curry (op mem) v) (#2 (ori:ori))
   1.540 +      andalso (#3 ori) <>"#undef";
   1.541 +    fun testi_vt v itm = (curry (op mem) v) (#2 (itm:itm));
   1.542 +    fun test_id ids r = curry (op mem) (#1 (r:ori)) ids;
   1.543 +(* val itm = hd icl; val (_,_,_,d,ts) = v6;
   1.544 +   *)
   1.545 +    fun test_subset (itm:itm) ((_,_,_,d,ts):ori) = 
   1.546 +	(d_in (#5 itm)) = d andalso subset op = (ts_in (#5 itm), ts);
   1.547 +    fun false_and_not_Sup((i,v,false,f,Sup _):itm) = false
   1.548 +      | false_and_not_Sup (i,v,false,f, _) = true
   1.549 +      | false_and_not_Sup  _ = false;
   1.550 +
   1.551 +    val v = if itms = [] then 1 else max_vt itms;
   1.552 +    val vors = if v = 0 then oris else filter (testr_vt v) oris;(*oris..vat*)
   1.553 +    val vits = if v = 0 then itms (*because of dsc without dat*)
   1.554 +	       else filter (testi_vt v) itms;                   (*itms..vat*)
   1.555 +    val icl = filter false_and_not_Sup vits; (* incomplete *)
   1.556 +  in if icl = [] 
   1.557 +     then case filter_out (test_id (map #1 vits)) vors of
   1.558 +	      [] => NONE
   1.559 +	    (* val miss = filter_out (test_id (map #1 vits)) vors;
   1.560 +	       *)
   1.561 +	    | miss => SOME (getr_ct thy (hd miss))
   1.562 +     else
   1.563 +	 case find_first (test_subset (hd icl)) vors of
   1.564 +	     (* val SOME ori = find_first (test_subset (hd icl)) vors;
   1.565 +	      *)
   1.566 +	     NONE => raise error "nxt_add: EX itm. not(dat(itm)<=dat(ori))"
   1.567 +	   | SOME ori => SOME (geti_ct thy ori (hd icl))
   1.568 +  end
   1.569 +end;
   1.570 +
   1.571 +
   1.572 +
   1.573 +fun mk_delete thy "#Given"  itm_ = Del_Given   (itm_out thy itm_)
   1.574 +  | mk_delete thy "#Find"   itm_ = Del_Find    (itm_out thy itm_)
   1.575 +  | mk_delete thy "#Relate" itm_ = Del_Relation(itm_out thy itm_)
   1.576 +  | mk_delete thy str _ = 
   1.577 +  raise error ("mk_delete: called with field '"^str^"'");
   1.578 +fun mk_additem "#Given" ct = Add_Given ct
   1.579 +  | mk_additem "#Find"  ct = Add_Find ct    
   1.580 +  | mk_additem "#Relate"ct = Add_Relation ct
   1.581 +  | mk_additem str _ = 
   1.582 +  raise error ("mk_additem: called with field '"^str^"'");
   1.583 +
   1.584 +
   1.585 +
   1.586 +
   1.587 +
   1.588 +(* find the next tac in specify (except nxt_model_pbl)
   1.589 +   4.00.: TODO: do not return a pos !!!
   1.590 +          (sind from DG comes the _OLD_ writepos)*)
   1.591 +(* 
   1.592 +> val (pbl,pbt,mpc) =(pbl',get_pbt cpI,(#ppc o get_met) cmI);
   1.593 +> val (dI,pI,mI) = empty_spec;
   1.594 +> nxt_spec Pbl (oris:ori list) ((dI',pI',mI'):spec(*original*))
   1.595 +  ((pbl:itm list), (met:itm list)) (pbt,mpc) ((dI,pI,mI):spec);
   1.596 +
   1.597 +at Init_Proof:
   1.598 +> val met = [];val (pbt,mpc) = (get_pbt pI',(#ppc o get_met) mI');
   1.599 +> val (dI,pI,mI) = empty_spec;
   1.600 +> nxt_spec Pbl (oris:ori list) ((dI',pI',mI'):spec(*original*))
   1.601 +  ((pbl:itm list), (met:itm list)) (pbt,mpc) ((dI,pI,mI):spec);
   1.602 +  *)
   1.603 +
   1.604 +(*. determine the next step of specification;
   1.605 +    not done here: Refine_Tacitly (otherwise *** unknown method: (..., no_met))
   1.606 +eg. in rootpbl 'no_met': 
   1.607 +args:
   1.608 +  preok          predicates are _all_ ok, or problem matches completely
   1.609 +  oris           immediately from formalization 
   1.610 +  (dI',pI',mI')  specification coming from author/parent-problem
   1.611 +  (pbl,          item lists specified by user
   1.612 +   met)          -"-, tacitly completed by copy_probl
   1.613 +  (dI,pI,mI)     specification explicitly done by the user
   1.614 +  (pbt, mpc)     problem type, guard of method
   1.615 +.*)
   1.616 +(* val (preok,pbl,pbt,mpc)=(pb,pbl',(#ppc o get_pbt) cpI,(#ppc o get_met) cmI);
   1.617 +   val (preok,pbl,pbt,mpc)=(pb,pbl',ppc,(#ppc o get_met) cmI);
   1.618 +   val (Pbl, preok, oris, (dI',pI',mI'), (pbl,met), (pbt,mpc), (dI,pI,mI)) =
   1.619 +       (p_, pb, oris, (dI',pI',mI'), (probl,meth), 
   1.620 +	(ppc, (#ppc o get_met) cmI), (dI,pI,mI));
   1.621 +   *)
   1.622 +fun nxt_spec Pbl preok (oris:ori list) ((dI',pI',mI'):spec)
   1.623 +  ((pbl:itm list), (met:itm list)) (pbt,mpc) ((dI,pI,mI):spec) = 
   1.624 +  ((*writeln"### nxt_spec Pbl";*)
   1.625 +   if dI'=e_domID andalso dI=e_domID then (Pbl, Specify_Theory dI')
   1.626 +   else if pI'=e_pblID andalso pI=e_pblID then (Pbl, Specify_Problem pI')
   1.627 +	else case find_first (is_error o #5) (pbl:itm list) of
   1.628 +	  SOME (_,_,_,fd,itm_) => 
   1.629 +	      (Pbl, mk_delete 
   1.630 +	       (assoc_thy (if dI=e_domID then dI' else dI)) fd itm_)
   1.631 +	| NONE => 
   1.632 +	    ((*writeln"### nxt_spec is_error NONE";*)
   1.633 +	     case nxt_add (assoc_thy (if dI=e_domID then dI' else dI)) 
   1.634 +		 oris pbt pbl of
   1.635 +(* val SOME (fd,ct') = nxt_add (assoc_thy (if dI=e_domID then dI' else dI)) 
   1.636 +                       oris pbt pbl;
   1.637 +  *)
   1.638 +	       SOME (fd,ct') => ((*writeln"### nxt_spec nxt_add SOME";*)
   1.639 +				 (Pbl, mk_additem fd ct'))
   1.640 +	     | NONE => (*pbl-items complete*)
   1.641 +	       if not preok then (Pbl, Refine_Problem pI')
   1.642 +	       else
   1.643 +		 if dI = e_domID then (Pbl, Specify_Theory dI')
   1.644 +		 else if pI = e_pblID then (Pbl, Specify_Problem pI')
   1.645 +		      else if mI = e_metID then (Pbl, Specify_Method mI')
   1.646 +			   else
   1.647 +			     case find_first (is_error o #5) met of
   1.648 +			       SOME (_,_,_,fd,itm_) => 
   1.649 +				   (Met, mk_delete (assoc_thy dI) fd itm_)
   1.650 +			     | NONE => 
   1.651 +				 (case nxt_add (assoc_thy dI) oris mpc met of
   1.652 +				      SOME (fd,ct') => (*30.8.01: pre?!?*)
   1.653 +				      (Met, mk_additem fd ct')
   1.654 +				    | NONE => 
   1.655 +				      ((*Solv 3.4.00*)Met, Apply_Method mI))))
   1.656 +(* val preok=pb; val (pbl, met) = (pbl,met');
   1.657 +   val (pbt,mpc)=((#ppc o get_pbt) cpI,(#ppc o get_met) cmI);
   1.658 +   val (Met, preok, oris, (dI',pI',mI'), (pbl,met), (pbt,mpc), (dI,pI,mI)) =
   1.659 +       (p_, pb, oris, (dI',pI',mI'), (probl,meth), 
   1.660 +	(ppc, (#ppc o get_met) cmI), (dI,pI,mI));
   1.661 +   *)
   1.662 +  | nxt_spec Met preok oris (dI',pI',mI') (pbl, met) (pbt,mpc) (dI,pI,mI) = 
   1.663 +  ((*writeln"### nxt_spec Met"; *)
   1.664 +   case find_first (is_error o #5) met of
   1.665 +     SOME (_,_,_,fd,itm_) => 
   1.666 +	 (Met, mk_delete (assoc_thy (if dI=e_domID then dI' else dI)) fd itm_)
   1.667 +   | NONE => 
   1.668 +       case nxt_add (assoc_thy (if dI=e_domID then dI' else dI))oris mpc met of
   1.669 +	 SOME (fd,ct') => (Met, mk_additem fd ct')
   1.670 +       | NONE => 
   1.671 +	   ((*writeln"### nxt_spec Met: nxt_add NONE";*)
   1.672 +	    if dI = e_domID then (Met, Specify_Theory dI')
   1.673 +	    else if pI = e_pblID then (Met, Specify_Problem pI')
   1.674 +		 else if not preok then (Met, Specify_Method mI)
   1.675 +		      else (Met, Apply_Method mI)));
   1.676 +	  
   1.677 +(* di_ pI_ mI_ pos_
   1.678 +val itms = [(1,[1],true,"#Find",Cor(e_term,[e_term])):itm,
   1.679 +	    (2,[2],true,"#Find",Syn("empty"))];
   1.680 +*)
   1.681 +
   1.682 +
   1.683 +(* ^^^--- aus nnewcode.sml am 30.1.00 ---^^^ *)
   1.684 +(*#############################################################*)
   1.685 +(*#############################################################*)
   1.686 +(* vvv--- aus nnewcode.sml vor 29.1.00 ---vvv *)
   1.687 +
   1.688 +(*3.3.--
   1.689 +fun update_itm (cl,d,ts) ((id,vt,_,sl,Cor (_,_)):itm) = 
   1.690 +  (id,vt,cl,sl,Cor (d,ts)):itm
   1.691 +  | update_itm (cl,d,ts) (id,vt,_,sl,Syn (_)) =   
   1.692 +  raise error ("update_itm "^((Syntax.string_of_term (thy2ctxt thy)) (comp_dts thy (d,ts)))^
   1.693 +	       " not not for Syn (s:cterm')")
   1.694 +  | update_itm (cl,d,ts) (id,vt,_,sl,Typ (_)) = 
   1.695 +  raise error ("update_itm "^((Syntax.string_of_term (thy2ctxt thy)) (comp_dts thy (d,ts)))^
   1.696 +	       " not not for Typ (s:cterm')")
   1.697 +  | update_itm (cl,d,ts) (id,vt,_,sl,Fal (_,_)) =
   1.698 +  (id,vt,cl,sl,Fal (d,ts))
   1.699 +  | update_itm (cl,d,ts) (id,vt,_,sl,Inc (_,_)) =
   1.700 +  (id,vt,cl,sl,Inc (d,ts))
   1.701 +  | update_itm (cl,d,ts) (id,vt,_,sl,Sup (_,_)) =
   1.702 +  (id,vt,cl,sl,Sup (d,ts));
   1.703 +*)
   1.704 +
   1.705 +
   1.706 +
   1.707 +
   1.708 +fun is_field_correct sel d dscpbt =
   1.709 +  case assoc (dscpbt, sel) of
   1.710 +    NONE => false
   1.711 +  | SOME ds => member op = ds d;
   1.712 +
   1.713 +(*. update the itm_ already input, all..from ori .*)
   1.714 +(* val (id,vt,fd,d,ts) = (i,v,f,d,ts\\ts');
   1.715 +   *)
   1.716 +fun ori_2itm thy itm_ pid all ((id,vt,fd,d,ts):ori) = 
   1.717 +  let 
   1.718 +    val ts' = union op = (ts_in itm_) ts;
   1.719 +    val pval = pbl_ids' thy d ts'
   1.720 +	(*WN.9.5.03: FIXXXME [#0, epsilon]
   1.721 +	  here would upd_penv be called for [#0, epsilon] etc. *)
   1.722 +    val complete = if eq_set op = (ts', all) then true else false;
   1.723 +  in case itm_ of
   1.724 +    (Cor _) => 
   1.725 +	(if fd = "#undef" then (id,vt,complete,fd,Sup(d,ts')) 
   1.726 +	 else (id,vt,complete,fd,Cor((d,ts'),(pid, pval)))):itm
   1.727 +  | (Syn c)     => raise error ("ori_2itm wants to overwrite "^c)
   1.728 +  | (Typ c)     => raise error ("ori_2itm wants to overwrite "^c)
   1.729 +  | (Inc _) => if complete
   1.730 +	       then (id,vt,true ,fd, Cor ((d,ts'),(pid, pval)))
   1.731 +	       else (id,vt,false,fd, Inc ((d,ts'),(pid, pval)))
   1.732 +  | (Sup ((*_,_*)d,ts')) => (*4.9.01 lost env*)
   1.733 +	 (*if fd = "#undef" then*) (id,vt,complete,fd,Sup(d,ts'))
   1.734 +	 (*else (id,vt,complete,fd,Cor((d,ts'),e))*)
   1.735 +(* 28.1.00: not completely clear ---^^^ etc.*)
   1.736 +(* 4.9.01: Mis just copied---vvv *)
   1.737 +  | (Mis _) => if complete
   1.738 +		     then (id,vt,true ,fd, Cor ((d,ts'),(pid, pval)))
   1.739 +		     else (id,vt,false,fd, Inc ((d,ts'),(pid, pval)))
   1.740 +  end;
   1.741 +
   1.742 +
   1.743 +fun eq1 d (_,(d',_)) = (d = d');
   1.744 +fun eq3 f d (_,_,_,f',itm_) = f = f' andalso d = (d_in itm_); 
   1.745 +
   1.746 +
   1.747 +(* 'all' ts from ori; ts is the input; (ori carries rest of info)
   1.748 +   9.01: this + ori_2itm is _VERY UNCLEAR_ ? overhead ?
   1.749 +   pval: value for problem-environment _NOT_ checked for 'inter' --
   1.750 +   -- FIXXME.WN.11.03 the generation of penv has to go to insert_ppc
   1.751 +  (as it has been done for input_icalhd+insert_ppc' in 11.03)*)
   1.752 +(*. is_input ori itms <=> 
   1.753 +    EX itm. (1) ori(field,dsc) = itm(field,dsc) & (2..4)
   1.754 +            (2) ori(ts) subset itm(ts)        --- Err "already input"       
   1.755 +	    (3) ori(ts) inter itm(ts) = empty --- new: ori(ts)
   1.756 +	    (4) -"- <> empty                  --- new: ori(ts) \\ inter .*)
   1.757 +(* val(itms,(i,v,f,d,ts)) = (ppc,ori');
   1.758 +   *)
   1.759 +fun is_notyet_input thy (itms:itm list) all ((i,v,f,d,ts):ori) pbt =
   1.760 +  case find_first (eq1 d) pbt of
   1.761 +      SOME (_,(_,pid)) =>(* val SOME (_,(_,pid)) = find_first (eq1 d) pbt;
   1.762 +                            val SOME (_,_,_,_,itm_)=find_first (eq3 f d) itms;
   1.763 +			   *)
   1.764 +      (case find_first (eq3 f d) itms of
   1.765 +	   SOME (_,_,_,_,itm_) =>
   1.766 +	   let 
   1.767 +	       val ts' = inter op = (ts_in itm_) ts;
   1.768 +	   in if subset op = (ts, ts') 
   1.769 +	      then (((strs2str' o 
   1.770 +		      map (Syntax.string_of_term (thy2ctxt thy))) ts')^
   1.771 +		    " already input", e_itm)                            (*2*)
   1.772 +	      else ("", 
   1.773 +                    ori_2itm thy itm_ pid all (i,v,f,d,
   1.774 +                                               subtract op = ts' ts))   (*3,4*)
   1.775 +	   end
   1.776 +	 | NONE => ("", ori_2itm thy (Inc ((e_term,[]),(pid,[]))) 
   1.777 +				 pid all (i,v,f,d,ts))                  (*1*)
   1.778 +	)
   1.779 +    | NONE => ("", ori_2itm thy (Sup (d,ts)) 
   1.780 +			      e_term all (i,v,f,d,ts));
   1.781 +
   1.782 +fun test_types thy (d,ts) =
   1.783 +  let 
   1.784 +    val s = !show_types; val _ = show_types:= true;
   1.785 +    val opt = (try (comp_dts thy)) (d,ts);
   1.786 +    val msg = case opt of 
   1.787 +      SOME _ => "" 
   1.788 +    | NONE => ((Syntax.string_of_term (thy2ctxt thy) d)^" "^
   1.789 +	     ((strs2str' o map (Syntax.string_of_term (thy2ctxt thy))) ts)
   1.790 +	     ^ " is illtyped");
   1.791 +    val _ = show_types:= s
   1.792 +  in msg end;
   1.793 +
   1.794 +
   1.795 +
   1.796 +fun maxl [] = raise error "maxl of []"
   1.797 +  | maxl (y::ys) =
   1.798 +  let fun mx x [] = x
   1.799 +	| mx x (y::ys) = if x < (y:int) then mx y ys else mx x ys
   1.800 +  in mx y ys end;
   1.801 +
   1.802 +
   1.803 +(*. is the input term t known in oris ? 
   1.804 +    give feedback on all(?) strange input;
   1.805 +    return _all_ terms already input to this item (e.g. valuesFor a,b) .*)
   1.806 +(*WN.11.03: from lists*)
   1.807 +fun is_known thy sel ori t =
   1.808 +(* val (ori,t)=(oris,term_of ct);
   1.809 +   *)
   1.810 +  let
   1.811 +    val ots = (distinct o flat o (map #5)) (ori:ori list);
   1.812 +    val oids = ((map (fst o dest_Free)) o distinct o 
   1.813 +		flat o (map vars)) ots;
   1.814 +    val (d,ts(*,pval*)) = split_dts thy t;
   1.815 +    val ids = map (fst o dest_Free) 
   1.816 +      ((distinct o (flat o (map vars))) ts);
   1.817 +  in if (subtract op = oids ids) <> []
   1.818 +     then (("identifiers "^(strs2str' (subtract op = oids ids))^
   1.819 +	    " not in example"), e_ori_, [])
   1.820 +     else 
   1.821 +	 if d = e_term 
   1.822 +	 then 
   1.823 +	     if not (subset op = (map typeless ts, map typeless ots))
   1.824 +	     then (("terms '"^
   1.825 +		    ((strs2str' o (map (Syntax.string_of_term 
   1.826 +					    (thy2ctxt thy)))) ts)^
   1.827 +		    "' not in example (typeless)"), e_ori_, [])
   1.828 +	     else (case seek_orits thy sel ts ori of
   1.829 +		       ("", ori_ as (_,_,_,d,ts), all) =>
   1.830 +		       (case test_types thy (d,ts) of
   1.831 +			    "" => ("", ori_, all)
   1.832 +			  | msg => (msg, e_ori_, []))
   1.833 +		     | (msg,_,_) => (msg, e_ori_, []))
   1.834 +	 else 
   1.835 +	     if member op = (map #4 ori) d
   1.836 +	     then seek_oridts thy sel (d,ts) ori
   1.837 +	     else ((Syntax.string_of_term (thy2ctxt thy) d)^
   1.838 +		   (*" not in example", e_ori_, []) ///11.11.03*)
   1.839 +		   " not in example", (0,[],sel,d,ts), [])
   1.840 +  end;
   1.841 +
   1.842 +
   1.843 +(*. for return-value of appl_add .*)
   1.844 +datatype additm =
   1.845 +	 Add of itm
   1.846 +       | Err of string;    (*error-message*)
   1.847 +
   1.848 +
   1.849 +(*. add an item; check wrt. oris and pbt .*)
   1.850 +
   1.851 +(* in contrary to oris<>[] below, this part handles user-input
   1.852 +   extremely acceptive, i.e. accept input instead error-msg *)
   1.853 +fun appl_add thy sel ([]:ori list) ppc pbt ct' =
   1.854 +(* val (ppc,pbt,ct',env) = (pbl, (#ppc o get_pbt) cpI, ct, []:envv);
   1.855 +   !!!! 28.8.01: env tested _minimally_ !!!
   1.856 +   *)
   1.857 +  let 
   1.858 +    val i = 1 + (if ppc=[] then 0 else maxl (map #1 ppc));
   1.859 +  in case parse thy ct' of (*should be done in applicable_in 4.00.FIXME*)
   1.860 +    NONE => Add (i,[],false,sel,Syn ct')
   1.861 +(* val (SOME ct) = parse thy ct';
   1.862 +   *)
   1.863 +  | SOME ct =>
   1.864 +      let
   1.865 +	val (d,ts(*,pval*)) = split_dts thy (term_of ct);
   1.866 +      in if d = e_term 
   1.867 +	 then Add (i,[],false,sel,Mis (dsc_unknown,hd ts(*24.3.02*)))
   1.868 +      
   1.869 +	 else  
   1.870 +	   (case find_first (eq1 d) pbt of
   1.871 +	     NONE => Add (i,[],true,sel,Sup ((d,ts)))
   1.872 +	   | SOME (f,(_,id)) =>
   1.873 +(* val SOME (f,(_,id)) = find_first (eq1 d) pbt;
   1.874 +   *)
   1.875 +	       let
   1.876 +		 fun eq2 d ((i,_,_,_,itm_):itm) = 
   1.877 +		     (d = (d_in itm_)) andalso i<>0;
   1.878 +	       in case find_first (eq2 d) ppc of 
   1.879 +		 NONE => Add (i,[],true,f, Cor ((d,ts), (id, (*pval*)
   1.880 +							 pbl_ids' thy d ts)))
   1.881 +	       | SOME (i',_,_,_,itm_) => 
   1.882 +(* val SOME (i',_,_,_,itm_) = find_first (eq2 d) ppc;
   1.883 +   val NONE = find_first (eq2 d) ppc;
   1.884 +   *)
   1.885 +		   if is_list_dsc d
   1.886 +		   then let val ts = union op = ts (ts_in itm_) 
   1.887 +			in Add (if ts_in itm_ = [] then i else i',
   1.888 +				 [],true,f,Cor ((d, ts), (id, (*pval*)
   1.889 +							  pbl_ids' thy d ts)))
   1.890 +			end
   1.891 +		   else Add (i',[],true,f,Cor ((d,ts),(id, (*pval*)
   1.892 +						       pbl_ids' thy d ts)))
   1.893 +	       end
   1.894 +	   )
   1.895 +      end
   1.896 +  end
   1.897 +(*. add ct to ppc .*)
   1.898 +(*FIXXME: accept items as Sup, Syn here, too (like appl_add..oris=[] above)*)
   1.899 +(* val (ppc,pbt) = (pbl, ppc);
   1.900 +   val (ppc,pbt) = (met, (#ppc o get_met) cmI);
   1.901 +
   1.902 +   val (ppc,pbt) = (pbl, (#ppc o get_pbt) cpI);
   1.903 +   *)
   1.904 +  | appl_add thy sel oris ppc pbt(*only for upd_envv*) ct = 
   1.905 +  let
   1.906 +    val ctopt = parse thy ct;
   1.907 +  in case ctopt of
   1.908 +    NONE => Err ("syntax error in "^ct)
   1.909 +  | SOME ct =>(* val SOME ct = ctopt;
   1.910 +		 val (msg,ori',all) = is_known thy sel oris (term_of ct);
   1.911 +		 val (msg,itm) = is_notyet_input thy ppc all ori' pbt;
   1.912 +		*) 
   1.913 +    (case is_known thy sel oris (term_of ct) of
   1.914 +	 ("",ori'(*ts='ct'*), all) => 
   1.915 +	 (case is_notyet_input thy ppc all ori' pbt of
   1.916 +	      ("",itm)  => Add itm
   1.917 +	    | (msg,_) => Err msg)
   1.918 +       | (msg,_,_) => Err msg)
   1.919 +  end;
   1.920 +(* 
   1.921 +> val (msg,itm) = is_notyet_input thy ppc all ori';
   1.922 +val itm = (12,[3],false,"#Relate",Cor (Const #,[#,#])) : itm
   1.923 +> val itm_ = #5 itm;
   1.924 +> val ts = ts_in itm_;
   1.925 +> map (atomty) ts; 
   1.926 +*)
   1.927 +
   1.928 +(*---------------------------------------------(4) nach ptyps.sml 23.3.02*)
   1.929 +
   1.930 +
   1.931 +(** make oris from args of the stac SubProblem and from pbt **)
   1.932 +
   1.933 +(*.can this formal argument (of a model-pattern) be omitted in the arg-list
   1.934 +   of a SubProblem ? see ME/ptyps.sml 'type met '.*)
   1.935 +fun is_copy_named_idstr str =
   1.936 +    case (rev o explode) str of
   1.937 +	"_"::_::"_"::_ => true
   1.938 +      | _ => false;
   1.939 +(*> is_copy_named_idstr "v_i_";
   1.940 +val it = true : bool
   1.941 +  > is_copy_named_idstr "e_";
   1.942 +val it = false : bool 
   1.943 +  > is_copy_named_idstr "L___";
   1.944 +val it = true : bool
   1.945 +*)
   1.946 +(*.should this formal argument (of a model-pattern) create a new identifier?.*)
   1.947 +fun is_copy_named_generating_idstr str =
   1.948 +    if is_copy_named_idstr str
   1.949 +    then case (rev o explode) str of
   1.950 +	"_"::"_"::"_"::_ => false
   1.951 +      | _ => true
   1.952 +    else false;
   1.953 +(*> is_copy_named_generating_idstr "v_i_";
   1.954 +val it = true : bool
   1.955 +  > is_copy_named_generating_idstr "L___";
   1.956 +val it = false : bool
   1.957 +*)
   1.958 +
   1.959 +(*.can this formal argument (of a model-pattern) be omitted in the arg-list
   1.960 +   of a SubProblem ? see ME/ptyps.sml 'type met '.*)
   1.961 +fun is_copy_named (_,(_,t)) = (is_copy_named_idstr o free2str) t;
   1.962 +(*.should this formal argument (of a model-pattern) create a new identifier?.*)
   1.963 +fun is_copy_named_generating (_,(_,t)) = 
   1.964 +    (is_copy_named_generating_idstr o free2str) t;
   1.965 +
   1.966 +
   1.967 +(*.split type-wrapper from scr-arg and build part of an ori;
   1.968 +   an type-error is reported immediately, raises an exn, 
   1.969 +   subsequent handling of exn provides 2nd part of error message.*)
   1.970 +(*fun mtc thy ((str, (dsc, _)):pat) (ty $ var) =   WN100820 made cterm to term 
   1.971 +    (* val (thy, (str, (dsc, _)), (ty $ var)) =
   1.972 +	   (thy,  p,               a);
   1.973 +       *)
   1.974 +    (cterm_of thy (dsc $ var);(*type check*)
   1.975 +     SOME ((([1], str, dsc, (*[var]*)
   1.976 +	    split_dts' (dsc, var))): preori)(*:ori without leading #*))
   1.977 +    handle e  as TYPE _ => 
   1.978 +	   (writeln (dashs 70^"\n"
   1.979 +		      ^"*** ERROR while creating the items for the model of the ->problem\n"
   1.980 +		      ^"*** from the ->stac with ->typeconstructor in arglist:\n"
   1.981 +		      ^"*** item (->description ->value): "^term2str dsc^" "^term2str var^"\n"
   1.982 +		      ^"*** description: "^(term_detail2str dsc)
   1.983 +		      ^"*** value: "^(term_detail2str var)
   1.984 +		      ^"*** typeconstructor in script: "^(term_detail2str ty)
   1.985 +		      ^"*** checked by theory: "^(theory2str thy)^"\n"
   1.986 +		      ^"*** "^dots 66);	     
   1.987 +	     print_exn e; (*raises exn again*)
   1.988 +	    NONE);*)
   1.989 +fun mtc thy ((str, (dsc, _)):pat) (ty $ var) =
   1.990 +    (* val (thy, (str, (dsc, _)), (ty $ var)) =
   1.991 +	   (thy,  p,               a);
   1.992 +       *)
   1.993 +    (cterm_of thy (dsc $ var);(*type check*)
   1.994 +     SOME ((([1], str, dsc, (*[var]*)
   1.995 +	    split_dts' (dsc, var))): preori)(*:ori without leading #*))
   1.996 +    handle e  as TYPE _ => 
   1.997 +	   (writeln (dashs 70^"\n"
   1.998 +		      ^"*** ERROR while creating the items for the model of the ->problem\n"
   1.999 +		      ^"*** from the ->stac with ->typeconstructor in arglist:\n"
  1.1000 +		      ^"*** item (->description ->value): "^term2str dsc^" "^term2str var^"\n"
  1.1001 +		      ^"*** description: "^(term_detail2str dsc)
  1.1002 +		      ^"*** value: "^(term_detail2str var)
  1.1003 +		      ^"*** typeconstructor in script: "^(term_detail2str ty)
  1.1004 +		      ^"*** checked by theory: "^(theory2str thy)^"\n"
  1.1005 +		      ^"*** "^dots 66);	     
  1.1006 +	    (*WN100820 postponed: print_exn e; raises exn again*)
  1.1007 +	    NONE);
  1.1008 +(*> val pbt = (#ppc o get_pbt) ["univariate","equation"];
  1.1009 +> val Const ("Script.SubProblem",_) $
  1.1010 +	  (Const ("Pair",_) $ Free (thy', _) $
  1.1011 +		 (Const ("Pair",_) $ pblID' $ metID')) $ ags =
  1.1012 +    str2term"(SubProblem (SqRoot_,[univariate,equation],\
  1.1013 +	    \[SqRoot_,solve_linear]) [bool_ (x+1- 2=0), real_ x])::bool list";
  1.1014 +> val ags = isalist2list ags;
  1.1015 +> mtc thy (hd pbt) (hd ags);
  1.1016 +val it = SOME ([1],"#Given",Const (#,#),[# $ #]) *)
  1.1017 +
  1.1018 +(*.match each pat of the model-pattern with an actual argument;
  1.1019 +   precondition: copy-named vars are filtered out.*)
  1.1020 +fun matc thy ([]:pat list)  _  (oris:preori list) = oris
  1.1021 +  | matc thy pbt [] _ =
  1.1022 +    (writeln (dashs 70);
  1.1023 +     raise error ("actual arg(s) missing for '"^pats2str pbt
  1.1024 +		 ^"' i.e. should be 'copy-named' by '*_._'"))
  1.1025 +  | matc thy ((p as (s,(d,t)))::pbt) (a::ags) oris =
  1.1026 +    (* val (thy, ((p as (s,(d,t)))::pbt), (a::ags), oris) =
  1.1027 +	   (thy,  pbt',                    ags,     []);
  1.1028 +       (*recursion..*)
  1.1029 +       val (thy, ((p as (s,(d,t)))::pbt), (a::ags), oris) =
  1.1030 +	   (thy,  pbt,                     ags,    (oris @ [ori]));
  1.1031 +       *)
  1.1032 +    (*del?..*)if (is_copy_named_idstr o free2str) t then oris
  1.1033 +    else(*..del?*) let val opt = mtc thy p a;  
  1.1034 +	 in case opt of
  1.1035 +		(* val SOME ori = mtc thy p a;
  1.1036 +		   *)
  1.1037 +		SOME ori => matc thy pbt ags (oris @ [ori])
  1.1038 +	      | NONE => [](*WN050903 skipped by exn handled in match_ags*)
  1.1039 +	 end; 
  1.1040 +(* run subp-rooteq.sml until Init_Proof before ...
  1.1041 +> val Nd (PblObj {origin=(oris,_,_),...},_) = pt;(*from test/subp-rooteq.sml*)
  1.1042 +> fun xxxfortest (_,a,b,c,d) = (a,b,c,d);val oris = map xxxfortest oris;
  1.1043 +
  1.1044 + other vars as in mtc ..
  1.1045 +> matc thy (drop_last pbt) ags [];
  1.1046 +val it = ([[1],"#Given",Const #,[#]),(0,[#],"#Given",Const #,[#])],2)*)
  1.1047 +
  1.1048 +
  1.1049 +(*WN051014 outcommented with redesign copy-named (for omitting '#Find'
  1.1050 +  in SubProblem); 
  1.1051 +  kept as initial idea for generating x_1, x_2, ... for equations*)
  1.1052 +fun cpy_nam (pbt:pat list) (oris:preori list) (p as (field,(dsc,t)):pat) =
  1.1053 +(* val ((pbt:pat list), (oris:preori list), ((field,(dsc,t)):pat)) =
  1.1054 +       (pbt',            oris',             hd (*!!!!!*) cy);
  1.1055 +   *)
  1.1056 +  (if is_copy_named_generating p
  1.1057 +   then (*WN051014 kept strange old code ...*)
  1.1058 +       let fun sel (_,_,d,ts) = comp_ts (d, ts) 
  1.1059 +	   val cy' = (implode o drop_last o drop_last o explode o free2str) t
  1.1060 +	   val ext = (last_elem o drop_last o explode o free2str) t
  1.1061 +	   val vars' = map (free2str o snd o snd) pbt(*cpy-nam filtered_out*)
  1.1062 +	   val vals = map sel oris
  1.1063 +	   val cy_ext = (free2str o the) (assoc (vars'~~vals, cy'))^"_"^ext
  1.1064 +       in ([1], field, dsc, [mk_free (type_of t) cy_ext]):preori end
  1.1065 +   else ([1], field, dsc, [t])
  1.1066 +	)
  1.1067 +  handle _ => raise error ("cpy_nam: for "^(term2str t));
  1.1068 +
  1.1069 +(*> val (field,(dsc,t)) = last_elem pbt;
  1.1070 +> cpy_nam pbt (drop_last oris) (field,(dsc,t));
  1.1071 +val it = ([1],"#Find",
  1.1072 +   Const ("Descript.solutions","bool List.list => Tools.toreall"),
  1.1073 +   [Free ("x_i","bool List.list")])                             *)
  1.1074 +
  1.1075 +
  1.1076 +(*.match the actual arguments of a SubProblem with a model-pattern
  1.1077 +   and create an ori list (in root-pbl created from formalization).
  1.1078 +   expects ags:pats = 1:1, while copy-named are filtered out of pats;
  1.1079 +   copy-named pats are appended in order to get them into the model-items.*)
  1.1080 +fun match_ags thy (pbt:pat list) ags =
  1.1081 +(* val (thy, pbt, ags) = (thy, (#ppc o get_pbt) pI, ags);
  1.1082 +   val (thy, pbt, ags) = (thy, pats, ags);
  1.1083 +   *)
  1.1084 +    let fun flattup (i,(var,bool,str,itm_)) = (i,var,bool,str,itm_);
  1.1085 +	val pbt' = filter_out is_copy_named pbt;
  1.1086 +	val cy = filter is_copy_named pbt;
  1.1087 +	val oris' = matc thy pbt' ags [];
  1.1088 +	val cy' = map (cpy_nam pbt' oris') cy;
  1.1089 +	val ors = add_id (oris' @ cy'); 
  1.1090 +    (*appended in order to get ^^^^^ them into the model-items*)
  1.1091 +    in (map flattup ors):ori list end;
  1.1092 +(*vars as above ..
  1.1093 +> match_ags thy pbt ags; 
  1.1094 +val it =
  1.1095 +  [(1,[1],"#Given",Const ("Descript.equality","bool => Tools.una"),
  1.1096 +    [Const # $ (# $ #) $ Free (#,#)]),
  1.1097 +   (2,[1],"#Given",Const ("Descript.solveFor","RealDef.real => Tools.una"),
  1.1098 +    [Free ("x","RealDef.real")]),
  1.1099 +   (3,[1],"#Find",
  1.1100 +    Const ("Descript.solutions","bool List.list => Tools.toreall"),
  1.1101 +    [Free ("x_i","bool List.list")])] : ori list*)
  1.1102 +
  1.1103 +(*.report part of the error-msg which is not available in match_args.*)
  1.1104 +fun match_ags_msg pI stac ags =
  1.1105 +    let val s = !show_types
  1.1106 +	val _ = show_types:= true
  1.1107 +	val pats = (#ppc o get_pbt) pI
  1.1108 +	val msg = (dots 70^"\n"
  1.1109 +		 ^"*** problem "^strs2str pI^" has the ...\n"
  1.1110 +		 ^"*** model-pattern "^pats2str pats^"\n"
  1.1111 +		 ^"*** stac   '"^term2str stac^"' has the ...\n"
  1.1112 +		 ^"*** arg-list "^terms2str ags^"\n"
  1.1113 +		 ^dashs 70)
  1.1114 +	val _ = show_types:= s
  1.1115 +    in writeln msg end;
  1.1116 +
  1.1117 +
  1.1118 +(*get the variables out of a pbl_; FIXME.WN.0311: is_copy_named ...obscure!!!*)
  1.1119 +fun vars_of_pbl_ pbl_ = 
  1.1120 +    let fun var_of_pbl_ (gfr,(dsc,t)) = t
  1.1121 +    in ((map var_of_pbl_) o (filter_out is_copy_named)) pbl_ end;
  1.1122 +fun vars_of_pbl_' pbl_ = 
  1.1123 +    let fun var_of_pbl_ (gfr,(dsc,t)) = t:term
  1.1124 +    in ((map var_of_pbl_)(* o (filter_out is_copy_named)*)) pbl_ end;
  1.1125 +
  1.1126 +fun overwrite_ppc thy itm ppc =
  1.1127 +  let 
  1.1128 +    fun repl ppc' (_,_,_,_,itm_) [] =
  1.1129 +      raise error ("overwrite_ppc: " ^ (itm_2str_ (thy2ctxt thy) itm_) ^ 
  1.1130 +                   " not found")
  1.1131 +      | repl ppc' itm (p::ppc) =
  1.1132 +	if (#1 itm) = (#1 (p:itm)) then ppc' @ [itm] @ ppc
  1.1133 +	else repl (ppc' @ [p]) itm ppc
  1.1134 +  in repl [] itm ppc end;
  1.1135 +
  1.1136 +(*10.3.00: insert the already compiled itm into model;
  1.1137 +   ev. filter_out  untouched (in FE: (0,...)) item related to insert-item *)
  1.1138 +(* val ppc=pbl;
  1.1139 +   *)
  1.1140 +fun insert_ppc thy itm ppc =
  1.1141 +    let 
  1.1142 +	fun eq_untouched d ((0,_,_,_,itm_):itm) = (d = d_in itm_)
  1.1143 +	  | eq_untouched _ _ = false;
  1.1144 +	    val ppc' = 
  1.1145 +		(
  1.1146 +		 (*writeln("### insert_ppc: itm= "^(itm2str_ itm));*)       
  1.1147 +		 case seek_ppc (#1 itm) ppc of
  1.1148 +		     (* val SOME xxx = seek_ppc (#1 itm) ppc;
  1.1149 +		        *)
  1.1150 +		     SOME _ => (*itm updated in is_notyet_input WN.11.03*)
  1.1151 +		     overwrite_ppc thy itm ppc
  1.1152 +		   | NONE => (ppc @ [itm]));
  1.1153 +    in filter_out (eq_untouched ((d_in o #5) itm)) ppc' end;
  1.1154 +
  1.1155 +(*from Isabelle/src/Pure/library.ML, _appends_ a new element*)
  1.1156 +fun gen_ins' eq (x, xs) = if gen_mem eq (x, xs) then xs else xs @ [x];
  1.1157 +
  1.1158 +fun eq_dsc ((_,_,_,_,itm_):itm, (_,_,_,_,iitm_):itm) = 
  1.1159 +    (d_in itm_) = (d_in iitm_);
  1.1160 +(*insert_ppc = insert_ppc' for appl_add', input_icalhd 11.03,
  1.1161 +    handles superfluous items carelessly*)
  1.1162 +fun insert_ppc' itm itms = gen_ins' eq_dsc (itm, itms);
  1.1163 +(* val eee = op=;
  1.1164 + > gen_ins' eee (4,[1,3,5,7]);
  1.1165 +val it = [1, 3, 5, 7, 4] : int list*)
  1.1166 +
  1.1167 +
  1.1168 +(*. output the headline to a ppc .*)
  1.1169 +fun header p_ pI mI =
  1.1170 +    case p_ of Pbl => Problem (if pI = e_pblID then [] else pI) 
  1.1171 +	     | Met => Method mI
  1.1172 +	     | pos => raise error ("header called with "^ pos_2str pos);
  1.1173 +
  1.1174 +
  1.1175 +
  1.1176 +(* test-printouts ---
  1.1177 +val _=writeln("### insert_ppc: (d,ts)="^((Syntax.string_of_term (thy2ctxt thy))(comp_dts thy(d,ts))));
  1.1178 + val _=writeln("### insert_ppc: pts= "^
  1.1179 +(strs2str' o map (Syntax.string_of_term (thy2ctxt thy))) pts);
  1.1180 +
  1.1181 +
  1.1182 + val sel = "#Given"; val Add_Given' ct = m;
  1.1183 +
  1.1184 + val sel = "#Find"; val Add_Find' (ct,_) = m; 
  1.1185 + val (p,_) = p;
  1.1186 + val (_,_,f,nxt',_,pt')= specify_additem sel (ct,[]) (p,Pbl(*!!!!!!!*)) c pt;
  1.1187 +--------------
  1.1188 + val sel = "#Given"; val Add_Given' (ct,_) = nxt; val (p,_) = p;
  1.1189 +  *)
  1.1190 +fun specify_additem sel (ct,_) (p,Met) c pt = 
  1.1191 +    let
  1.1192 +      val (PblObj{meth=met,origin=(oris,(dI',pI',mI'),_),
  1.1193 +		  probl=pbl,spec=(dI,pI,mI),...}) = get_obj I pt p;
  1.1194 +      val thy = if dI = e_domID then assoc_thy dI' else assoc_thy dI;
  1.1195 +    (*val ppt = if pI = e_pblID then get_pbt pI' else get_pbt pI;*)
  1.1196 +      val cpI = if pI = e_pblID then pI' else pI;
  1.1197 +      val cmI = if mI = e_metID then mI' else mI;
  1.1198 +      val {ppc,pre,prls,...} = get_met cmI
  1.1199 +    in case appl_add thy sel oris met ppc ct of
  1.1200 +      Add itm (*..union old input *) =>
  1.1201 +	let (* val Add itm = appl_add thy sel oris met (#ppc (get_met cmI)) ct;
  1.1202 +               *)
  1.1203 +	  val met' = insert_ppc thy itm met;
  1.1204 +	  (*val pt' = update_met pt p met';*)
  1.1205 +	  val ((p,Met),_,_,pt') = 
  1.1206 +	      generate1 thy (case sel of
  1.1207 +				 "#Given" => Add_Given' (ct, met')
  1.1208 +			       | "#Find"  => Add_Find'  (ct, met')
  1.1209 +			       | "#Relate"=> Add_Relation'(ct, met')) 
  1.1210 +			Uistate (p,Met) pt
  1.1211 +	  val pre' = check_preconds thy prls pre met'
  1.1212 +	  val pb = foldl and_ (true, map fst pre')
  1.1213 +	  (*val _=writeln("@@@ specify_additem: Met Add before nxt_spec")*)
  1.1214 +	  val (p_,nxt) =
  1.1215 +	    nxt_spec Met pb oris (dI',pI',mI') (pbl,met') 
  1.1216 +	    ((#ppc o get_pbt) cpI,ppc) (dI,pI,mI);
  1.1217 +	in ((p,p_), ((p,p_),Uistate),
  1.1218 +	    Form' (PpcKF (0,EdUndef,(length p),Nundef,
  1.1219 +			  (Method cmI, itms2itemppc thy met' pre'))),
  1.1220 +	    nxt,Safe,pt') end
  1.1221 +    | Err msg =>
  1.1222 +	  let val pre' = check_preconds thy prls pre met
  1.1223 +	      val pb = foldl and_ (true, map fst pre')
  1.1224 +	    (*val _=writeln("@@@ specify_additem: Met Err before nxt_spec")*)
  1.1225 +	      val (p_,nxt) =
  1.1226 +	    nxt_spec Met pb oris (dI',pI',mI') (pbl,met) 
  1.1227 +	    ((#ppc o get_pbt) cpI,(#ppc o get_met) cmI) (dI,pI,mI);
  1.1228 +	  in ((p,p_), ((p,p_),Uistate), Error' (Error_ msg), nxt, Safe,pt) end
  1.1229 +    end
  1.1230 +(* val (p,_) = p;
  1.1231 +   *)
  1.1232 +| specify_additem sel (ct,_) (p,_(*Frm, Pbl*)) c pt = 
  1.1233 +    let
  1.1234 +      val (PblObj{meth=met,origin=(oris,(dI',pI',mI'),_),
  1.1235 +		  probl=pbl,spec=(dI,pI,mI),...}) = get_obj I pt p;
  1.1236 +      val thy = if dI = e_domID then assoc_thy dI' else assoc_thy dI;
  1.1237 +      val cpI = if pI = e_pblID then pI' else pI;
  1.1238 +      val cmI = if mI = e_metID then mI' else mI;
  1.1239 +      val {ppc,where_,prls,...} = get_pbt cpI;
  1.1240 +    in case appl_add thy sel oris pbl ppc ct of
  1.1241 +      Add itm (*..union old input *) =>
  1.1242 +      (* val Add itm = appl_add thy sel oris pbl ppc ct;
  1.1243 +         *)
  1.1244 +	let
  1.1245 +	    (*val _= writeln("###specify_additem: itm= "^(itm2str_ itm));*)
  1.1246 +	  val pbl' = insert_ppc thy itm pbl
  1.1247 +	  val ((p,Pbl),_,_,pt') = 
  1.1248 +	      generate1 thy (case sel of
  1.1249 +				 "#Given" => Add_Given' (ct, pbl')
  1.1250 +			       | "#Find"  => Add_Find'  (ct, pbl')
  1.1251 +			       | "#Relate"=> Add_Relation'(ct, pbl')) 
  1.1252 +			Uistate (p,Pbl) pt
  1.1253 +	  val pre = check_preconds thy prls where_ pbl'
  1.1254 +	  val pb = foldl and_ (true, map fst pre)
  1.1255 +	(*val _=writeln("@@@ specify_additem: Pbl Add before nxt_spec")*)
  1.1256 +	  val (p_,nxt) =
  1.1257 +	    nxt_spec Pbl pb oris (dI',pI',mI') (pbl',met) 
  1.1258 +		     (ppc,(#ppc o get_met) cmI) (dI,pI,mI);
  1.1259 +	  val ppc = if p_= Pbl then pbl' else met;
  1.1260 +	in ((p,p_), ((p,p_),Uistate),
  1.1261 +	    Form' (PpcKF (0,EdUndef,(length p),Nundef,
  1.1262 +			  (header p_ pI cmI,
  1.1263 +			   itms2itemppc thy ppc pre))), nxt,Safe,pt') end
  1.1264 +
  1.1265 +    | Err msg =>
  1.1266 +	  let val pre = check_preconds thy prls where_ pbl
  1.1267 +	      val pb = foldl and_ (true, map fst pre)
  1.1268 +	    (*val _=writeln("@@@ specify_additem: Pbl Err before nxt_spec")*)
  1.1269 +	      val (p_,nxt) =
  1.1270 +	    nxt_spec Pbl pb oris (dI',pI',mI') (pbl,met) 
  1.1271 +	    (ppc,(#ppc o get_met) cmI) (dI,pI,mI);
  1.1272 +	  in ((p,p_), ((p,p_),Uistate), Error' (Error_ msg), nxt, Safe,pt) end
  1.1273 +    end;
  1.1274 +(* val sel = "#Find"; val (p,_) = p; val Add_Find' ct = nxt;
  1.1275 +   val (_,_,f,nxt',_,pt')= specify_additem sel ct (p,Met) c pt;
  1.1276 +  *)
  1.1277 +
  1.1278 +(* ori
  1.1279 +val (msg,itm) = appl_add thy sel oris ppc ct;
  1.1280 +val (Cor(d,ts)) = #5 itm;
  1.1281 +map (atomty) ts;
  1.1282 +
  1.1283 +pre
  1.1284 +*)
  1.1285 +
  1.1286 +
  1.1287 +(* val Init_Proof' (fmz,(dI',pI',mI')) = m;
  1.1288 +   specify (Init_Proof' (fmz,(dI',pI',mI'))) e_pos' [] EmptyPtree;
  1.1289 +   *)
  1.1290 +fun specify (Init_Proof' (fmz,(dI',pI',mI'))) (_:pos') (_:cid) (_:ptree)= 
  1.1291 +  let          (* either """"""""""""""" all empty or complete *)
  1.1292 +    val thy = assoc_thy dI';
  1.1293 +    val oris = if dI' = e_domID orelse pI' = e_pblID then ([]:ori list)
  1.1294 +	       else prep_ori fmz thy ((#ppc o get_pbt) pI');
  1.1295 +    val (pt,c) = cappend_problem e_ptree [] e_istate (fmz,(dI',pI',mI'))
  1.1296 +				 (oris,(dI',pI',mI'),e_term);
  1.1297 +    val {ppc,prls,where_,...} = get_pbt pI'
  1.1298 +    (*val pbl = init_pbl ppc;  WN.9.03: done in Model/Refine_Problem
  1.1299 +    val pt = update_pbl pt [] pbl;
  1.1300 +    val pre = check_preconds thy prls where_ pbl
  1.1301 +    val pb = foldl and_ (true, map fst pre)*)
  1.1302 +    val (pbl, pre, pb) = ([], [], false)
  1.1303 +  in case mI' of
  1.1304 +	 ["no_met"] => 
  1.1305 +	 (([],Pbl), (([],Pbl),Uistate),
  1.1306 +	  Form' (PpcKF (0,EdUndef,(length []),Nundef,
  1.1307 +			(Problem [], itms2itemppc (assoc_thy dI') pbl pre))),
  1.1308 +	  Refine_Tacitly pI', Safe,pt)
  1.1309 +       | _ => 
  1.1310 +	 (([],Pbl), (([],Pbl),Uistate),
  1.1311 +	  Form' (PpcKF (0,EdUndef,(length []),Nundef,
  1.1312 +			(Problem [], itms2itemppc (assoc_thy dI') pbl pre))),
  1.1313 +	  Model_Problem,
  1.1314 +	  Safe,pt)
  1.1315 +  end
  1.1316 +  (*ONLY for STARTING modeling phase*)
  1.1317 +  | specify (Model_Problem' (_,pbl,met)) (pos as (p,p_)) c pt =
  1.1318 +  let (* val (Model_Problem' (_,pbl), pos as (p,p_)) = (m, (p,p_));
  1.1319 +         *)
  1.1320 +    val (PblObj{origin=(oris,(dI',pI',mI'),_), spec=(dI,_,_),...}) = 
  1.1321 +	get_obj I pt p
  1.1322 +    val thy' = if dI = e_domID then dI' else dI
  1.1323 +    val thy = assoc_thy thy'
  1.1324 +    val {ppc,prls,where_,...} = get_pbt pI'
  1.1325 +    val pre = check_preconds thy prls where_ pbl
  1.1326 +    val pb = foldl and_ (true, map fst pre)
  1.1327 +    val ((p,_),_,_,pt) = 
  1.1328 +	generate1 thy (Model_Problem'([],pbl,met)) Uistate pos pt
  1.1329 +    val (_,nxt) = nxt_spec Pbl pb oris (dI',pI',mI') (pbl,met) 
  1.1330 +		(ppc,(#ppc o get_met) mI') (dI',pI',mI');
  1.1331 +  in ((p,Pbl), ((p,p_),Uistate),
  1.1332 +      Form' (PpcKF (0,EdUndef,(length p),Nundef,
  1.1333 +		    (Problem pI', itms2itemppc (assoc_thy dI') pbl pre))),
  1.1334 +      nxt, Safe, pt) end
  1.1335 +
  1.1336 +(*. called only if no_met is specified .*)     
  1.1337 +  | specify (Refine_Tacitly' (pI,pIre,_,_,_)) (pos as (p,_)) c pt =
  1.1338 +  let (* val Refine_Tacitly' (pI,pIre,_,_,_) = m;
  1.1339 +         *)
  1.1340 +    val (PblObj{origin=(oris,(dI',pI',mI'),_), meth=met, ...}) = 
  1.1341 +	get_obj I pt p;
  1.1342 +    val {prls,met,ppc,thy,where_,...} = get_pbt pIre
  1.1343 +    (*val pbl = init_pbl ppc --- Model_Problem recognizes probl=[]*)
  1.1344 +    (*val pt = update_pbl pt p pbl;
  1.1345 +    val pt = update_orispec pt p 
  1.1346 +		(string_of_thy thy, pIre, 
  1.1347 +		 if length met = 0 then e_metID else hd met);*)
  1.1348 +    val (domID, metID) = (string_of_thy thy, 
  1.1349 +		      if length met = 0 then e_metID else hd met)
  1.1350 +    val ((p,_),_,_,pt) = 
  1.1351 +	generate1 thy (Refine_Tacitly'(pI,pIre,domID,metID,(*pbl*)[])) 
  1.1352 +		  Uistate pos pt
  1.1353 +    (*val pre = check_preconds thy prls where_ pbl
  1.1354 +    val pb = foldl and_ (true, map fst pre)*)
  1.1355 +    val (pbl, pre, pb) = ([], [], false)
  1.1356 +  in ((p,Pbl), (pos,Uistate),
  1.1357 +      Form' (PpcKF (0,EdUndef,(length p),Nundef,
  1.1358 +		    (Problem pIre, itms2itemppc (assoc_thy dI') pbl pre))),
  1.1359 +      Model_Problem, Safe, pt) end
  1.1360 +
  1.1361 +  | specify (Refine_Problem' (rfd as (pI,_))) pos c pt =
  1.1362 +    let val (pos,_,_,pt) = generate1 (assoc_thy "Isac.thy") 
  1.1363 +				     (Refine_Problem' rfd) Uistate pos pt
  1.1364 +    in (pos(*p,Pbl*), (pos(*p,Pbl*),Uistate), Problems (RefinedKF rfd), 
  1.1365 +	Model_Problem, Safe, pt) end
  1.1366 +
  1.1367 +(* val (Specify_Problem' (pI, (ok, (itms, pre)))) = nxt; val (p,_) = p;
  1.1368 +   val (Specify_Problem' (pI, (ok, (itms, pre)))) = m; val (p,_) = p;
  1.1369 +   *)
  1.1370 +  | specify (Specify_Problem' (pI, (ok, (itms, pre)))) (pos as (p,_)) c pt =
  1.1371 +  let val (PblObj {origin=(oris,(dI',pI',mI'),_), spec=(dI,_,mI), 
  1.1372 +		   meth=met, ...}) = get_obj I pt p;
  1.1373 +    (*val pt = update_pbl pt p itms;
  1.1374 +    val pt = update_pblID pt p pI;*)
  1.1375 +    val thy = assoc_thy dI
  1.1376 +    val ((p,Pbl),_,_,pt)= 
  1.1377 +	generate1 thy (Specify_Problem' (pI, (ok, (itms, pre)))) Uistate pos pt
  1.1378 +    val dI'' = assoc_thy (if dI=e_domID then dI' else dI);
  1.1379 +    val mI'' = if mI=e_metID then mI' else mI;
  1.1380 +  (*val _=writeln("@@@ specify (Specify_Problem) before nxt_spec")*)
  1.1381 +    val (_,nxt) = nxt_spec Pbl ok oris (dI',pI',mI') (itms, met) 
  1.1382 +		((#ppc o get_pbt) pI,(#ppc o get_met) mI'') (dI,pI,mI);
  1.1383 +  in ((p,Pbl), (pos,Uistate),
  1.1384 +      Form' (PpcKF (0,EdUndef,(length p),Nundef,
  1.1385 +		    (Problem pI, itms2itemppc dI'' itms pre))),
  1.1386 +      nxt, Safe, pt) end    
  1.1387 +(* val Specify_Method' mID = nxt; val (p,_) = p;
  1.1388 +   val Specify_Method' mID = m;
  1.1389 +   specify (Specify_Method' mID) (p,p_) c pt;
  1.1390 +   *)
  1.1391 +  | specify (Specify_Method' (mID,_,_)) (pos as (p,_)) c pt =
  1.1392 +  let val (PblObj {origin=(oris,(dI',pI',mI'),_), probl=pbl, spec=(dI,pI,mI), 
  1.1393 +		   meth=met, ...}) = get_obj I pt p;
  1.1394 +    val {ppc,pre,prls,...} = get_met mID
  1.1395 +    val thy = assoc_thy dI
  1.1396 +    val oris = add_field' thy ppc oris;
  1.1397 +    (*val pt = update_oris pt p oris; 20.3.02: repl. "#undef"*)
  1.1398 +    val dI'' = if dI=e_domID then dI' else dI;
  1.1399 +    val pI'' = if pI = e_pblID then pI' else pI;
  1.1400 +    val met = if met=[] then pbl else met;
  1.1401 +    val (ok, (itms, pre')) = match_itms_oris thy met (ppc,pre,prls ) oris;
  1.1402 +    (*val pt = update_met pt p itms;
  1.1403 +    val pt = update_metID pt p mID*)
  1.1404 +    val (pos,_,_,pt)= 
  1.1405 +	generate1 thy (Specify_Method' (mID, oris, itms)) Uistate pos pt
  1.1406 +    (*val _=writeln("@@@ specify (Specify_Method) before nxt_spec")*)
  1.1407 +    val (_,nxt) = nxt_spec Met (*ok*)true oris (dI',pI',mI') (pbl, itms) 
  1.1408 +		((#ppc o get_pbt) pI'',ppc) (dI'',pI'',mID);
  1.1409 +  in (pos, (pos,Uistate),
  1.1410 +      Form' (PpcKF (0,EdUndef,(length p),Nundef,
  1.1411 +		    (Method mID, itms2itemppc (assoc_thy dI'') itms pre'))),
  1.1412 +      nxt, Safe, pt) end    
  1.1413 +(* val Add_Find' ct = nxt; val sel = "#Find"; 
  1.1414 +   *)
  1.1415 +  | specify (Add_Given' ct) p c pt = specify_additem "#Given" ct p c pt
  1.1416 +  | specify (Add_Find'  ct) p c pt = specify_additem "#Find"  ct p c pt
  1.1417 +  | specify (Add_Relation' ct) p c pt=specify_additem"#Relate"ct p c pt
  1.1418 +(* val Specify_Theory' domID = m;
  1.1419 +   val (Specify_Theory' domID, (p,p_)) = (m, pos);
  1.1420 +   *)
  1.1421 +  | specify (Specify_Theory' domID) (pos as (p,p_)) c pt =
  1.1422 +    let val p_ = case p_ of Met => Met | _ => Pbl
  1.1423 +      val thy = assoc_thy domID;
  1.1424 +      val (PblObj{origin=(oris,(dI',pI',mI'),_), meth=met,
  1.1425 +		  probl=pbl, spec=(dI,pI,mI),...}) = get_obj I pt p;
  1.1426 +      val mppc = case p_ of Met => met | _ => pbl;
  1.1427 +      val cpI = if pI = e_pblID then pI' else pI;
  1.1428 +      val {prls=per,ppc,where_=pwh,...} = get_pbt cpI
  1.1429 +      val cmI = if mI = e_metID then mI' else mI;
  1.1430 +      val {prls=mer,ppc=mpc,pre=mwh,...} = get_met cmI
  1.1431 +      val pre = 
  1.1432 +	  case p_ of
  1.1433 +	      Met => (check_preconds thy mer mwh met)
  1.1434 +	    | _ => (check_preconds thy per pwh pbl)
  1.1435 +      val pb = foldl and_ (true, map fst pre)
  1.1436 +    in if domID = dI
  1.1437 +       then let 
  1.1438 +	 (*val _=writeln("@@@ specify (Specify_Theory) THEN before nxt_spec")*)
  1.1439 +           val (p_,nxt) = nxt_spec p_ pb oris (dI',pI',mI') 
  1.1440 +				   (pbl,met) (ppc,mpc) (dI,pI,mI);
  1.1441 +	      in ((p,p_), (pos,Uistate), 
  1.1442 +		  Form'(PpcKF (0,EdUndef,(length p), Nundef,
  1.1443 +			       (header p_ pI cmI, itms2itemppc thy mppc pre))),
  1.1444 +		  nxt,Safe,pt) end
  1.1445 +       else (*FIXME: check ppc wrt. (new!) domID ..? still parsable?*)
  1.1446 +	 let 
  1.1447 +	   (*val pt = update_domID pt p domID;11.8.03*)
  1.1448 +	   val ((p,p_),_,_,pt) = generate1 thy (Specify_Theory' domID) 
  1.1449 +					   Uistate (p,p_) pt
  1.1450 +	 (*val _=writeln("@@@ specify (Specify_Theory) ELSE before nxt_spec")*)
  1.1451 +	   val (p_,nxt) = nxt_spec p_ pb oris (dI',pI',mI') (pbl,met) 
  1.1452 +				   (ppc,mpc) (domID,pI,mI);
  1.1453 +	 in ((p,p_), (pos,Uistate), 
  1.1454 +	     Form' (PpcKF (0, EdUndef, (length p),Nundef,
  1.1455 +			   (header p_ pI cmI, itms2itemppc thy mppc pre))),
  1.1456 +	     nxt, Safe,pt) end
  1.1457 +    end
  1.1458 +(* itms2itemppc thy [](*mpc*) pre
  1.1459 +   *)
  1.1460 +  | specify m' _ _ _ = 
  1.1461 +    raise error ("specify: not impl. for "^tac_2str m');
  1.1462 +
  1.1463 +(* val (sel, Add_Given ct, ptp as (pt,(p,Pbl))) = ("#Given", tac, ptp);
  1.1464 +   val (sel, Add_Find  ct, ptp as (pt,(p,Pbl))) = ("#Find", tac, ptp);
  1.1465 +   *)
  1.1466 +fun nxt_specif_additem sel ct (ptp as (pt,(p,Pbl))) = 
  1.1467 +    let
  1.1468 +      val (PblObj{meth=met,origin=(oris,(dI',pI',_),_),
  1.1469 +		  probl=pbl,spec=(dI,pI,_),...}) = get_obj I pt p;
  1.1470 +      val thy = if dI = e_domID then assoc_thy dI' else assoc_thy dI;
  1.1471 +      val cpI = if pI = e_pblID then pI' else pI;
  1.1472 +    in case appl_add thy sel oris pbl ((#ppc o get_pbt) cpI) ct of
  1.1473 +	   Add itm (*..union old input *) =>
  1.1474 +(* val Add itm = appl_add thy sel oris pbl ppc ct;
  1.1475 +   *)
  1.1476 +	   let
  1.1477 +	       (*val _=writeln("###nxt_specif_additem: itm= "^(itm2str_ itm));*)
  1.1478 +	       val pbl' = insert_ppc thy itm pbl
  1.1479 +	       val (tac,tac_) = 
  1.1480 +		   case sel of
  1.1481 +		       "#Given" => (Add_Given    ct, Add_Given'   (ct, pbl'))
  1.1482 +		     | "#Find"  => (Add_Find     ct, Add_Find'    (ct, pbl'))
  1.1483 +		     | "#Relate"=> (Add_Relation ct, Add_Relation'(ct, pbl'))
  1.1484 +	       val ((p,Pbl),c,_,pt') = 
  1.1485 +		   generate1 thy tac_ Uistate (p,Pbl) pt
  1.1486 +	   in ([(tac,tac_,((p,Pbl),Uistate))], c, (pt',(p,Pbl))):calcstate' end
  1.1487 +	       
  1.1488 +	 | Err msg => 
  1.1489 +	   (*TODO.WN03 pass error-msgs to the frontend..
  1.1490 +             FIXME ..and dont abuse a tactic for that purpose*)
  1.1491 +	   ([(Tac msg,
  1.1492 +	      Tac_ (theory "Pure", msg,msg,msg),
  1.1493 +	      (e_pos', e_istate))], [], ptp) 
  1.1494 +    end
  1.1495 +
  1.1496 +(* val sel = "#Find"; val (p,_) = p; val Add_Find' ct = nxt;
  1.1497 +   val (_,_,f,nxt',_,pt')= nxt_specif_additem sel ct (p,Met) c pt;
  1.1498 +  *)
  1.1499 +  | nxt_specif_additem sel ct (ptp as (pt,(p,Met))) = 
  1.1500 +    let
  1.1501 +      val (PblObj{meth=met,origin=(oris,(dI',pI',mI'),_),
  1.1502 +		  probl=pbl,spec=(dI,pI,mI),...}) = get_obj I pt p;
  1.1503 +      val thy = if dI = e_domID then assoc_thy dI' else assoc_thy dI;
  1.1504 +      val cmI = if mI = e_metID then mI' else mI;
  1.1505 +    in case appl_add thy sel oris met ((#ppc o get_met) cmI) ct of
  1.1506 +      Add itm (*..union old input *) =>
  1.1507 +	let (* val Add itm = appl_add thy sel oris met (#ppc (get_met cmI)) ct;
  1.1508 +               *)
  1.1509 +	  val met' = insert_ppc thy itm met;
  1.1510 +	  val (tac,tac_) = 
  1.1511 +	      case sel of
  1.1512 +		  "#Given" => (Add_Given    ct, Add_Given'   (ct, met'))
  1.1513 +		| "#Find"  => (Add_Find     ct, Add_Find'    (ct, met'))
  1.1514 +		| "#Relate"=> (Add_Relation ct, Add_Relation'(ct, met'))
  1.1515 +	  val ((p,Met),c,_,pt') = 
  1.1516 +	      generate1 thy tac_ Uistate (p,Met) pt
  1.1517 +	in ([(tac,tac_,((p,Met), Uistate))], c, (pt',(p,Met))) end
  1.1518 +
  1.1519 +    | Err msg => ([(*tacis*)], [], ptp) 
  1.1520 +    (*nxt_me collects tacis until not hide; here just no progress*)
  1.1521 +    end;
  1.1522 +
  1.1523 +(* ori
  1.1524 +val (msg,itm) = appl_add thy sel oris ppc ct;
  1.1525 +val (Cor(d,ts)) = #5 itm;
  1.1526 +map (atomty) ts;
  1.1527 +
  1.1528 +pre
  1.1529 +*)
  1.1530 +fun ori2Coritm pbt ((i,v,f,d,ts):ori) =
  1.1531 +    (i,v,true,f, Cor ((d,ts),(((snd o snd o the o (find_first (eq1 d))) pbt) 
  1.1532 +			      handle _ => raise error ("ori2Coritm: dsc "^
  1.1533 +						term2str d^
  1.1534 +						"in ori, but not in pbt")
  1.1535 +			      ,ts))):itm;
  1.1536 +fun ori2Coritm (pbt:pat list) ((i,v,f,d,ts):ori) =
  1.1537 +    ((i,v,true,f, Cor ((d,ts),((snd o snd o the o 
  1.1538 +			       (find_first (eq1 d))) pbt,ts))):itm)
  1.1539 +    handle _ => (*dsc in oris, but not in pbl pat list: keep this dsc*)
  1.1540 +    ((i,v,true,f, Cor ((d,ts),(d,ts))):itm);
  1.1541 +
  1.1542 +
  1.1543 +(*filter out oris which have same description in itms*)
  1.1544 +fun filter_outs oris [] = oris
  1.1545 +  | filter_outs oris (i::itms) = 
  1.1546 +    let val ors = filter_out ((curry op= ((d_in o #5) (i:itm))) o 
  1.1547 +			      (#4:ori -> term)) oris;
  1.1548 +    in filter_outs ors itms end;
  1.1549 +
  1.1550 +fun memI a b = member op = a b;
  1.1551 +(*filter oris which are in pbt, too*)
  1.1552 +fun filter_pbt oris pbt =
  1.1553 +    let val dscs = map (fst o snd) pbt
  1.1554 +    in filter ((memI dscs) o (#4: ori -> term)) oris end;
  1.1555 +
  1.1556 +(*.combine itms from pbl + met and complete them wrt. pbt.*)
  1.1557 +(*FIXXXME.WN031205 complete_metitms doesnt handle incorrect itms !*)
  1.1558 +local infix mem;
  1.1559 +fun x mem [] = false
  1.1560 +  | x mem (y :: ys) = x = y orelse x mem ys;
  1.1561 +in 
  1.1562 +fun complete_metitms (oris:ori list) (pits:itm list) (mits:itm list) met = 
  1.1563 +(* val met = (#ppc o get_met) ["DiffApp","max_by_calculus"];
  1.1564 +   *)
  1.1565 +    let val vat = max_vt pits;
  1.1566 +        val itms = pits @ 
  1.1567 +		   (filter ((curry (op mem) vat) o (#2:itm -> int list)) mits);
  1.1568 +	val ors = filter ((curry (op mem) vat) o (#2:ori -> int list)) oris;
  1.1569 +        val os = filter_outs ors itms;
  1.1570 +    (*WN.12.03?: does _NOT_ add itms from met ?!*)
  1.1571 +    in itms @ (map (ori2Coritm met) os) end
  1.1572 +end;
  1.1573 +
  1.1574 +
  1.1575 +
  1.1576 +(*.complete model and guard of a calc-head .*)
  1.1577 +local infix mem;
  1.1578 +fun x mem [] = false
  1.1579 +  | x mem (y :: ys) = x = y orelse x mem ys;
  1.1580 +in 
  1.1581 +fun complete_mod_ (oris, mpc, ppc, probl) =
  1.1582 +    let	val pits = filter_out ((curry op= false) o (#3: itm -> bool)) probl
  1.1583 +	val vat = if probl = [] then 1 else max_vt probl
  1.1584 +	val pors = filter ((curry (op mem) vat) o (#2:ori -> int list)) oris
  1.1585 +	val pors = filter_outs pors pits (*which are in pbl already*)
  1.1586 +        val pors = (filter_pbt pors ppc) (*which are in pbt, too*)
  1.1587 +
  1.1588 +	val pits = pits @ (map (ori2Coritm ppc) pors)
  1.1589 +	val mits = complete_metitms oris pits [] mpc
  1.1590 +    in (pits, mits) end
  1.1591 +end;
  1.1592 +
  1.1593 +fun some_spec ((odI, opI, omI):spec) ((dI, pI, mI):spec) =
  1.1594 +    (if dI = e_domID then odI else dI,
  1.1595 +     if pI = e_pblID then opI else pI,
  1.1596 +     if mI = e_metID then omI else mI):spec;
  1.1597 +
  1.1598 +
  1.1599 +(*.find a next applicable tac (for calcstate) and update ptree
  1.1600 + (for ev. finding several more tacs due to hide).*)
  1.1601 +(*FIXXXME: unify ... fun nxt_specif = nxt_spec + applicable_in + specify !!*)
  1.1602 +(*WN.24.10.03        ~~~~~~~~~~~~~~   -> tac     -> tac_      -> -"- as arg*)
  1.1603 +(*WN.24.10.03        fun nxt_solv   = ...................................??*)
  1.1604 +fun nxt_specif (tac as Model_Problem) (pt, pos as (p,p_)) =
  1.1605 +  let
  1.1606 +    val (PblObj{origin=(oris,ospec,_),probl,spec,...}) = get_obj I pt p
  1.1607 +    val (dI,pI,mI) = some_spec ospec spec
  1.1608 +    val thy = assoc_thy dI
  1.1609 +    val mpc = (#ppc o get_met) mI (*just for reuse complete_mod_*)
  1.1610 +    val {cas,ppc,...} = get_pbt pI
  1.1611 +    val pbl = init_pbl ppc (*fill in descriptions*)
  1.1612 +    (*--------------if you think, this should be done by the Dialog 
  1.1613 +     in the java front-end, search there for WN060225-modelProblem----*)
  1.1614 +    val (pbl,met) = case cas of NONE => (pbl,[])
  1.1615 +			    | _ => complete_mod_ (oris, mpc, ppc, probl)
  1.1616 +    (*----------------------------------------------------------------*)
  1.1617 +    val tac_ = Model_Problem' (pI, pbl, met)
  1.1618 +    val (pos,c,_,pt) = generate1 thy tac_ Uistate pos pt
  1.1619 +  in ([(tac,tac_, (pos, Uistate))], c, (pt,pos)):calcstate' end
  1.1620 +
  1.1621 +(* val Add_Find ct = tac;
  1.1622 +   *)
  1.1623 +  | nxt_specif (Add_Given ct) ptp = nxt_specif_additem "#Given" ct ptp
  1.1624 +  | nxt_specif (Add_Find  ct) ptp = nxt_specif_additem "#Find"  ct ptp
  1.1625 +  | nxt_specif (Add_Relation ct) ptp = nxt_specif_additem"#Relate" ct ptp
  1.1626 +
  1.1627 +(*. called only if no_met is specified .*)     
  1.1628 +  | nxt_specif (Refine_Tacitly pI) (ptp as (pt, pos as (p,_))) =
  1.1629 +    let val (PblObj {origin = (oris, (dI,_,_),_), ...}) = get_obj I pt p
  1.1630 +	val opt = refine_ori oris pI
  1.1631 +    in case opt of
  1.1632 +	   SOME pI' => 
  1.1633 +	   let val {met,ppc,...} = get_pbt pI'
  1.1634 +	       val pbl = init_pbl ppc
  1.1635 +	       (*val pt = update_pbl pt p pbl ..done by Model_Problem*)
  1.1636 +	       val mI = if length met = 0 then e_metID else hd met
  1.1637 +               val thy = assoc_thy dI
  1.1638 +	       val (pos,c,_,pt) = 
  1.1639 +		   generate1 thy (Refine_Tacitly' (pI,pI',dI,mI,(*pbl*)[])) 
  1.1640 +			     Uistate pos pt
  1.1641 +	   in ([(Refine_Tacitly pI, Refine_Tacitly' (pI,pI',dI,mI,(*pbl*)[]),
  1.1642 +		 (pos, Uistate))], c, (pt,pos)) end
  1.1643 +	 | NONE => ([], [], ptp)
  1.1644 +    end
  1.1645 +
  1.1646 +  | nxt_specif (Refine_Problem pI) (ptp as (pt, pos as (p,_))) =
  1.1647 +    let val (PblObj {origin=(_,(dI,_,_),_),spec=(dI',_,_),
  1.1648 +		     probl, ...}) = get_obj I pt p
  1.1649 +	val thy = if dI' = e_domID then dI else dI'
  1.1650 +    in case refine_pbl (assoc_thy thy) pI probl of
  1.1651 +	   NONE => ([], [], ptp)
  1.1652 +	 | SOME (rfd as (pI',_)) => 
  1.1653 +	   let val (pos,c,_,pt) = 
  1.1654 +		   generate1 (assoc_thy thy) 
  1.1655 +			     (Refine_Problem' rfd) Uistate pos pt
  1.1656 +	    in ([(Refine_Problem pI, Refine_Problem' rfd,
  1.1657 +			    (pos, Uistate))], c, (pt,pos)) end
  1.1658 +    end
  1.1659 +
  1.1660 +  | nxt_specif (Specify_Problem pI) (pt, pos as (p,_)) =
  1.1661 +    let val (PblObj {origin=(oris,(dI,_,_),_),spec=(dI',pI',_),
  1.1662 +		     probl, ...}) = get_obj I pt p;
  1.1663 +	val thy = assoc_thy (if dI' = e_domID then dI else dI');
  1.1664 +        val {ppc,where_,prls,...} = get_pbt pI
  1.1665 +	val pbl as (_,(itms,_)) = 
  1.1666 +	    if pI'=e_pblID andalso pI=e_pblID
  1.1667 +	    then (false, (init_pbl ppc, []))
  1.1668 +	    else match_itms_oris thy probl (ppc,where_,prls) oris(*FIXXXXXME?*)
  1.1669 +	(*FIXXXME~~~~~~~~~~~~~~~: take pbl and compare with new pI WN.8.03*)
  1.1670 +	val ((p,Pbl),c,_,pt)= 
  1.1671 +	    generate1 thy (Specify_Problem' (pI, pbl)) Uistate pos pt
  1.1672 +    in ([(Specify_Problem pI, Specify_Problem' (pI, pbl),
  1.1673 +		    (pos,Uistate))], c, (pt,pos)) end
  1.1674 +
  1.1675 +  (*transfers oris (not required in pbl) to met-model for script-env
  1.1676 +    FIXME.WN.8.03: application of several mIDs to SAME model?*)
  1.1677 +  | nxt_specif (Specify_Method mID) (ptp as (pt, pos as (p,_))) = 
  1.1678 +  let val (PblObj {origin=(oris,(dI',pI',mI'),_), probl=pbl, spec=(dI,pI,mI), 
  1.1679 +		   meth=met, ...}) = get_obj I pt p;
  1.1680 +    val {ppc,pre,prls,...} = get_met mID
  1.1681 +    val thy = assoc_thy dI
  1.1682 +    val oris = add_field' thy ppc oris;
  1.1683 +    val dI'' = if dI=e_domID then dI' else dI;
  1.1684 +    val pI'' = if pI = e_pblID then pI' else pI;
  1.1685 +    val met = if met=[] then pbl else met;(*WN0602 what if more itms in met?*)
  1.1686 +    val (ok, (itms, pre')) = match_itms_oris thy met (ppc,pre,prls ) oris;
  1.1687 +    val (pos,c,_,pt)= 
  1.1688 +	generate1 thy (Specify_Method' (mID, oris, itms)) Uistate pos pt
  1.1689 +  in ([(Specify_Method mID, Specify_Method' (mID, oris, itms),
  1.1690 +		  (pos,Uistate))], c, (pt,pos)) end    
  1.1691 +
  1.1692 +  | nxt_specif (Specify_Theory dI) (pt, pos as (p,Pbl)) =
  1.1693 +    let val (dI',_,_) = get_obj g_spec pt p
  1.1694 +	val (pos,c,_,pt) = 
  1.1695 +	    generate1 (assoc_thy "Isac.thy") (Specify_Theory' dI) 
  1.1696 +		      Uistate pos pt
  1.1697 +    in  (*FIXXXME: check if pbl can still be parsed*)
  1.1698 +	([(Specify_Theory dI, Specify_Theory' dI, (pos,Uistate))], c,
  1.1699 +	 (pt, pos)) end
  1.1700 +
  1.1701 +  | nxt_specif (Specify_Theory dI) (pt, pos as (p,Met)) =
  1.1702 +    let val (dI',_,_) = get_obj g_spec pt p
  1.1703 +	val (pos,c,_,pt) = 
  1.1704 +	    generate1 (assoc_thy "Isac.thy") (Specify_Theory' dI) 
  1.1705 +		      Uistate pos pt
  1.1706 +    in  (*FIXXXME: check if met can still be parsed*)
  1.1707 +	([(Specify_Theory dI, Specify_Theory' dI, (pos,Uistate))], c,
  1.1708 +	 (pt, pos)) end
  1.1709 +
  1.1710 +  | nxt_specif m' _ = 
  1.1711 +    raise error ("nxt_specif: not impl. for "^tac2str m');
  1.1712 +
  1.1713 +(*.get the values from oris; handle the term list w.r.t. penv.*)
  1.1714 +
  1.1715 +local infix mem;
  1.1716 +fun x mem [] = false
  1.1717 +  | x mem (y :: ys) = x = y orelse x mem ys;
  1.1718 +in 
  1.1719 +fun vals_of_oris oris =
  1.1720 +    ((map (mkval' o (#5:ori -> term list))) o 
  1.1721 +     (filter ((curry (op mem) 1) o (#2:ori -> int list)))) oris
  1.1722 +end;
  1.1723 +
  1.1724 +
  1.1725 +
  1.1726 +(*.create a calc-tree with oris via an cas.refined pbl.*)
  1.1727 +fun nxt_specify_init_calc (([],(dI,pI,mI)): fmz) =
  1.1728 +(* val ([],(dI,pI,mI)) = (fmz, sp);
  1.1729 +   *)
  1.1730 +    if pI <> [] then (*comes from pbl-browser*)
  1.1731 +	let val {cas,met,ppc,thy,...} = get_pbt pI
  1.1732 +	    val dI = if dI = "" then theory2theory' thy else dI
  1.1733 +	    val thy = assoc_thy dI
  1.1734 +	    val mI = if mI = [] then hd met else mI
  1.1735 +	    val hdl = case cas of NONE => pblterm dI pI | SOME t => t
  1.1736 +	    val (pt,_) = cappend_problem e_ptree [] e_istate ([], (dI,pI,mI))
  1.1737 +					 ([], (dI,pI,mI), hdl)
  1.1738 +	    val pt = update_spec pt [] (dI,pI,mI)
  1.1739 +	    val pits = init_pbl' ppc
  1.1740 +	    val pt = update_pbl pt [] pits
  1.1741 +	in ((pt,([],Pbl)), []): calcstate end
  1.1742 +    else if mI <> [] then (*comes from met-browser*)
  1.1743 +	let val {ppc,...} = get_met mI
  1.1744 +	    val dI = if dI = "" then "Isac.thy" else dI
  1.1745 +	    val thy = assoc_thy dI
  1.1746 +	    val (pt,_) = cappend_problem e_ptree [] e_istate ([], (dI,pI,mI))
  1.1747 +					 ([], (dI,pI,mI), e_term(*FIXME met*))
  1.1748 +	    val pt = update_spec pt [] (dI,pI,mI)
  1.1749 +	    val mits = init_pbl' ppc
  1.1750 +	    val pt = update_met pt [] mits
  1.1751 +	in ((pt,([],Met)), []) end
  1.1752 +    else (*completely new example*)
  1.1753 +	let val (pt,_) = cappend_problem e_ptree [] e_istate ([], e_spec)
  1.1754 +					 ([], e_spec, e_term)
  1.1755 +	in ((pt,([],Pbl)), []) end
  1.1756 +(* val (fmz, (dI,pI,mI)) = (fmz, sp);
  1.1757 +   *)
  1.1758 +  | nxt_specify_init_calc (fmz:fmz_,(dI,pI,mI):spec) = 
  1.1759 +    let            (* either """"""""""""""" all empty or complete *)
  1.1760 +	val thy = assoc_thy dI
  1.1761 +	val (pI, pors, mI) = 
  1.1762 +	    if mI = ["no_met"] 
  1.1763 +	    then let val pors = prep_ori fmz thy ((#ppc o get_pbt) pI)
  1.1764 +		     val pI' = refine_ori' pors pI;
  1.1765 +		 in (pI', pors (*refinement over models with diff.prec only*), 
  1.1766 +		     (hd o #met o get_pbt) pI') end
  1.1767 +	    else (pI, prep_ori fmz thy ((#ppc o get_pbt) pI), mI)
  1.1768 +	val {cas,ppc,thy=thy',...} = get_pbt pI (*take dI from _refined_ pbl*)
  1.1769 +	val dI = theory2theory' (maxthy thy thy');
  1.1770 +	val hdl = case cas of
  1.1771 +		      NONE => pblterm dI pI
  1.1772 +		    | SOME t => subst_atomic ((vars_of_pbl_' ppc) 
  1.1773 +						  ~~~ vals_of_oris pors) t
  1.1774 +    val (pt,_) = cappend_problem e_ptree [] e_istate (fmz,(dI,pI,mI))
  1.1775 +				 (pors,(dI,pI,mI),hdl)
  1.1776 +    (*val pbl = init_pbl ppc  WN.9.03: done by Model/Refine_Problem
  1.1777 +    val pt = update_pbl pt [] pbl*)
  1.1778 +  in ((pt,([],Pbl)), fst3 (nxt_specif Model_Problem (pt, ([],Pbl))))
  1.1779 +  end;
  1.1780 +
  1.1781 +
  1.1782 +
  1.1783 +(*18.12.99*)
  1.1784 +fun get_spec_form (m:tac_) ((p,p_):pos') (pt:ptree) = 
  1.1785 +(*  case appl_spec p pt m of           /// 19.1.00
  1.1786 +    Notappl e => Error' (Error_ e)
  1.1787 +  | Appl => 
  1.1788 +*)    let val (_,_,f,_,_,_) = specify m (p,p_) [] pt
  1.1789 +      in f end;
  1.1790 +
  1.1791 +
  1.1792 +(*fun tag_form thy (formal, given) = cterm_of thy
  1.1793 +	      (((head_of o term_of) given) $ (term_of formal)); WN100819*)
  1.1794 +fun tag_form thy (formal, given) =
  1.1795 +    (let val gf = (head_of given) $ formal;
  1.1796 +         val _ = cterm_of thy gf
  1.1797 +     in gf end)
  1.1798 +    handle _ => raise error ("calchead.tag_form: " ^ 
  1.1799 +                             Syntax.string_of_term (thy2ctxt thy) given ^
  1.1800 +                             " .. " ^
  1.1801 +                             Syntax.string_of_term (thy2ctxt thy) formal ^
  1.1802 +                         " ..types do not match");
  1.1803 +(* val formal = (the o (parse thy)) "[R::real]";
  1.1804 +> val given = (the o (parse thy)) "fixed_values (cs::real list)";
  1.1805 +> tag_form thy (formal, given);
  1.1806 +val it = "fixed_values [R]" : cterm
  1.1807 +*)
  1.1808 +fun chktyp thy (n, fs, gs) = 
  1.1809 +  ((writeln o (Syntax.string_of_term (thy2ctxt thy)) o (nth n)) fs;
  1.1810 +   (writeln o (Syntax.string_of_term (thy2ctxt thy)) o (nth n)) gs;
  1.1811 +   tag_form thy (nth n fs, nth n gs));
  1.1812 +
  1.1813 +fun chktyps thy (fs, gs) = map (tag_form thy) (fs ~~ gs);
  1.1814 +
  1.1815 +(* #####################################################
  1.1816 +   find the failing item:
  1.1817 +> val n = 2;
  1.1818 +> val tag__form = chktyp (n,formals,givens);
  1.1819 +> (type_of o term_of o (nth n)) formals; 
  1.1820 +> (type_of o term_of o (nth n)) givens;
  1.1821 +> atomty ((term_of o (nth n)) formals);
  1.1822 +> atomty ((term_of o (nth n)) givens);
  1.1823 +> atomty (term_of tag__form);
  1.1824 +> use_thy"isa-98-1-HOL-plus/knowl-base/DiffAppl";
  1.1825 + ##################################################### *)
  1.1826 +
  1.1827 +(* #####################################################
  1.1828 +   testdata setup
  1.1829 +val origin = ["sqrt(9+4*x)=sqrt x + sqrt(5+x)","x::rat","(+0)"];
  1.1830 +val formals = map (the o (parse thy)) origin;
  1.1831 +
  1.1832 +val given  = ["equation (lhs=rhs)",
  1.1833 +	     "bound_variable bdv",   (* TODO type *) 
  1.1834 +	     "error_bound apx"];
  1.1835 +val where_ = ["e is_root_equation_in bdv",
  1.1836 +	      "bdv is_var",
  1.1837 +	      "apx is_const_expr"];
  1.1838 +val find   = ["L::rat set"];
  1.1839 +val with_  = ["L = {bdv. || ((%x. lhs) bdv) - ((%x. rhs) bdv) || < apx}"];
  1.1840 +val chkpbl = map (the o (parse thy)) (given @ where_ @ find @ with_);
  1.1841 +val givens = map (the o (parse thy)) given;
  1.1842 +
  1.1843 +val tag__forms = chktyps (formals, givens);
  1.1844 +map ((atomty) o term_of) tag__forms;
  1.1845 + ##################################################### *)
  1.1846 +
  1.1847 +
  1.1848 +(* check pbltypes, announces one failure a time *)
  1.1849 +(*fun chk_vars ctppc = 
  1.1850 +  let val {Given=gi,Where=wh,Find=fi,With=wi,Relate=re} = 
  1.1851 +    appc flat (mappc (vars o term_of) ctppc)
  1.1852 +  in if (wh\\gi) <> [] then ("wh\\gi",wh\\gi)
  1.1853 +     else if (re\\(gi union fi)) <> [] 
  1.1854 +	    then ("re\\(gi union fi)",re\\(gi union fi))
  1.1855 +	  else ("ok",[]) end;*)
  1.1856 +fun chk_vars ctppc = 
  1.1857 +  let val {Given=gi,Where=wh,Find=fi,With=wi,Relate=re} = 
  1.1858 +          appc flat (mappc vars ctppc)
  1.1859 +      val chked = subtract op = gi wh
  1.1860 +  in if chked <> [] then ("wh\\gi", chked)
  1.1861 +     else let val chked = subtract op = (union op = gi fi) re
  1.1862 +          in if chked  <> []
  1.1863 +	     then ("re\\(gi union fi)", chked)
  1.1864 +	     else ("ok", []) 
  1.1865 +          end
  1.1866 +  end;
  1.1867 +
  1.1868 +(* check a new pbltype: variables (Free) unbound by given, find*) 
  1.1869 +fun unbound_ppc ctppc =
  1.1870 +  let val {Given=gi,Find=fi,Relate=re,...} = 
  1.1871 +    appc flat (mappc vars ctppc)
  1.1872 +  in distinct (*re\\(gi union fi)*) 
  1.1873 +              (subtract op = (union op = gi fi) re) end;
  1.1874 +(*
  1.1875 +> val org = {Given=["[R=(R::real)]"],Where=[],
  1.1876 +	   Find=["[A::real]"],With=[],
  1.1877 +	   Relate=["[A=#2*a*b - a^^^#2, (a//#2)^^^#2 + (b//#2)^^^#2 = R^^^#2]"]
  1.1878 +	   }:string ppc;
  1.1879 +> val ctppc = mappc (the o (parse thy)) org;
  1.1880 +> unbound_ppc ctppc;
  1.1881 +val it = [("a","RealDef.real"),("b","RealDef.real")] : (string * typ) list
  1.1882 +*)
  1.1883 +
  1.1884 +
  1.1885 +(* f, a binary operator, is nested rightassociative *)
  1.1886 +fun foldr1 f xs =
  1.1887 +  let
  1.1888 +    fun fld f (x::[]) = x
  1.1889 +      | fld f (x::x'::[]) = f (x',x)
  1.1890 +      | fld f (x::x'::xs) = f (fld f (x'::xs),x);
  1.1891 +  in ((fld f) o rev) xs end;
  1.1892 +(*
  1.1893 +> val (SOME ct) = parse thy "[a=b,c=d,e=f]";
  1.1894 +> val ces = map (cterm_of thy) (isalist2list (term_of ct));
  1.1895 +> val conj = foldr1 HOLogic.mk_conj (isalist2list (term_of ct));
  1.1896 +> cterm_of thy conj;
  1.1897 +val it = "(a = b & c = d) & e = f" : cterm
  1.1898 +*)
  1.1899 +
  1.1900 +(* f, a binary operator, is nested leftassociative *)
  1.1901 +fun foldl1 f (x::[]) = x
  1.1902 +  | foldl1 f (x::x'::[]) = f (x,x')
  1.1903 +  | foldl1 f (x::x'::xs) = f (x,foldl1 f (x'::xs));
  1.1904 +(*
  1.1905 +> val (SOME ct) = parse thy "[a=b,c=d,e=f,g=h]";
  1.1906 +> val ces = map (cterm_of thy) (isalist2list (term_of ct));
  1.1907 +> val conj = foldl1 HOLogic.mk_conj (isalist2list (term_of ct));
  1.1908 +> cterm_of thy conj;
  1.1909 +val it = "a = b & c = d & e = f & g = h" : cterm
  1.1910 +*)
  1.1911 +
  1.1912 +
  1.1913 +(* called only once, if a Subproblem has been located in the script*)
  1.1914 +fun nxt_model_pbl (Subproblem'((_,pblID,metID),_,_,_,_)) ptp =
  1.1915 +(* val (Subproblem'((_,pblID,metID),_,_,_,_),ptp) = (m', (pt,(p,p_)));
  1.1916 +   *)
  1.1917 +    (case metID of
  1.1918 +	 ["no_met"] => 
  1.1919 +	 (snd3 o hd o fst3) (nxt_specif (Refine_Tacitly pblID) ptp)
  1.1920 +       | _ => (snd3 o hd o fst3) (nxt_specif Model_Problem ptp))
  1.1921 +  (*all stored in tac_ itms     ^^^^^^^^^^*)
  1.1922 +  | nxt_model_pbl tac_ _ = 
  1.1923 +    raise error ("nxt_model_pbl: called by tac= "^tac_2str tac_);
  1.1924 +(* run subp_rooteq.sml ''
  1.1925 +   until nxt=("Subproblem",Subproblem ("SqRoot.thy",["univariate","equation"]))
  1.1926 +> val (_, (Subproblem'((_,pblID,metID),_,_,_,_),_,_,_,_,_)) =
  1.1927 +      (last_elem o drop_last) ets'';
  1.1928 +> val mst = (last_elem o drop_last) ets'';
  1.1929 +> nxt_model_pbl mst;
  1.1930 +val it = Refine_Tacitly ["univariate","equation"] : tac
  1.1931 +*)
  1.1932 +
  1.1933 +(*fun eq1 d (_,(d',_)) = (d = d'); ---modspec.sml*)
  1.1934 +fun eq4 v (_,vts,_,_,_) = member op = vts v;
  1.1935 +fun eq5 (_,_,_,_,itm_) (_,_,_,d,_) = d_in itm_ = d;
  1.1936 +
  1.1937 + 
  1.1938 +
  1.1939 +(*
  1.1940 +  writeln (oris2str pors);
  1.1941 +
  1.1942 +  writeln (itms2str_ thy pits);
  1.1943 +  writeln (itms2str_ thy mits);
  1.1944 +   *)
  1.1945 +
  1.1946 +
  1.1947 +(*.complete _NON_empty calc-head for autocalc (sub-)pbl from oris
  1.1948 +  + met from fmz; assumes pos on PblObj, meth = [].*)
  1.1949 +fun complete_mod (pt, pos as (p, p_):pos') =
  1.1950 +(* val (pt, (p, _)) = (pt, p);
  1.1951 +   val (pt, (p, _)) = (pt, pos);
  1.1952 +   *)
  1.1953 +    let val _= if p_ <> Pbl 
  1.1954 +	       then writeln("###complete_mod: only impl.for Pbl, called with "^
  1.1955 +			    pos'2str pos) else ()
  1.1956 +	val (PblObj{origin=(oris, ospec, hdl), probl, spec,...}) =
  1.1957 +	    get_obj I pt p
  1.1958 +	val (dI,pI,mI) = some_spec ospec spec
  1.1959 +	val mpc = (#ppc o get_met) mI
  1.1960 +	val ppc = (#ppc o get_pbt) pI
  1.1961 +	val (pits, mits) = complete_mod_ (oris, mpc, ppc, probl)
  1.1962 +        val pt = update_pblppc pt p pits
  1.1963 +	val pt = update_metppc pt p mits
  1.1964 +    in (pt, (p,Met):pos') end
  1.1965 +;
  1.1966 +(*| complete_mod (pt, pos as (p, Met):pos') =
  1.1967 +    raise error ("###complete_mod: only impl.for Pbl, called with "^
  1.1968 +		 pos'2str pos);*)
  1.1969 +
  1.1970 +(*.complete _EMPTY_ calc-head for autocalc (sub-)pbl from oris(+met from fmz);
  1.1971 +   oris and spec (incl. pbl-refinement) given from init_calc or SubProblem .*)
  1.1972 +fun all_modspec (pt, (p,_):pos') =
  1.1973 +(* val (pt, (p,_)) = ptp;
  1.1974 +   *)
  1.1975 +    let val (PblObj{fmz=(fmz_,_), origin=(pors, spec as (dI,pI,mI), hdl),
  1.1976 +		    ...}) = get_obj I pt p;
  1.1977 +	val thy = assoc_thy dI;
  1.1978 +	val {ppc,...} = get_met mI;
  1.1979 +	val mors = prep_ori fmz_ thy ppc;
  1.1980 +        val pt = update_pblppc pt p (map (ori2Coritm ppc) pors);
  1.1981 +	val pt = update_metppc pt p (map (ori2Coritm ppc) mors);
  1.1982 +	val pt = update_spec pt p (dI,pI,mI);
  1.1983 +    in (pt, (p,Met): pos') end;
  1.1984 +
  1.1985 +(*WN.12.03: use in nxt_spec, too ? what about variants ???*)
  1.1986 +fun is_complete_mod_ ([]: itm list) = false
  1.1987 +  | is_complete_mod_ itms = 
  1.1988 +    foldl and_ (true, (map #3 itms));
  1.1989 +fun is_complete_mod (pt, pos as (p, Pbl): pos') =
  1.1990 +    if (is_pblobj o (get_obj I pt)) p 
  1.1991 +    then (is_complete_mod_ o (get_obj g_pbl pt)) p
  1.1992 +    else raise error ("is_complete_mod: called by PrfObj at "^pos'2str pos)
  1.1993 +  | is_complete_mod (pt, pos as (p, Met)) = 
  1.1994 +    if (is_pblobj o (get_obj I pt)) p 
  1.1995 +    then (is_complete_mod_ o (get_obj g_met pt)) p
  1.1996 +    else raise error ("is_complete_mod: called by PrfObj at "^pos'2str pos)
  1.1997 +  | is_complete_mod (_, pos) =
  1.1998 +    raise error ("is_complete_mod called by "^pos'2str pos^
  1.1999 +		 " (should be Pbl or Met)");
  1.2000 +
  1.2001 +(*.have (thy, pbl, met) _all_ been specified explicitly ?.*)
  1.2002 +fun is_complete_spec (pt, pos as (p,_): pos') = 
  1.2003 +    if (not o is_pblobj o (get_obj I pt)) p 
  1.2004 +    then raise error ("is_complete_spec: called by PrfObj at "^pos'2str pos)
  1.2005 +    else let val (dI,pI,mI) = get_obj g_spec pt p
  1.2006 +	 in dI<>e_domID andalso pI<>e_pblID andalso mI<>e_metID end;
  1.2007 +(*.complete empty items in specification from origin (pbl, met ev.refined);
  1.2008 +  assumes 'is_complete_mod'.*)
  1.2009 +fun complete_spec (pt, pos as (p,_): pos') = 
  1.2010 +    let val PblObj {origin = (_,ospec,_), spec,...} = get_obj I pt p
  1.2011 +	val pt = update_spec pt p (some_spec ospec spec)
  1.2012 +    in (pt, pos) end;
  1.2013 +
  1.2014 +fun is_complete_modspec ptp = 
  1.2015 +    is_complete_mod ptp andalso is_complete_spec ptp;
  1.2016 +
  1.2017 +
  1.2018 +
  1.2019 +
  1.2020 +fun pt_model (PblObj {meth,spec,origin=(_,spec',hdl),...}) Met =
  1.2021 +(* val ((PblObj {meth,spec,origin=(_,spec',hdl),...}), Met) = (ppobj, p_);
  1.2022 +   *)
  1.2023 +    let val (_,_,metID) = get_somespec' spec spec'
  1.2024 +	val pre = 
  1.2025 +	    if metID = e_metID then []
  1.2026 +	    else let val {prls,pre=where_,...} = get_met metID
  1.2027 +		     val pre = check_preconds' prls where_ meth 0
  1.2028 +		 in pre end
  1.2029 +	val allcorrect = is_complete_mod_ meth
  1.2030 +			 andalso foldl and_ (true, (map #1 pre))
  1.2031 +    in ModSpec (allcorrect, Met, hdl, meth, pre, spec) end
  1.2032 +  | pt_model (PblObj {probl,spec,origin=(_,spec',hdl),...}) _(*Frm,Pbl*) =
  1.2033 +(* val ((PblObj {probl,spec,origin=(_,spec',hdl),...}),_) = (ppobj, p_);
  1.2034 +   *)
  1.2035 +    let val (_,pI,_) = get_somespec' spec spec'
  1.2036 +	val pre =
  1.2037 +	    if pI = e_pblID then []
  1.2038 +	    else let val {prls,where_,cas,...} = get_pbt pI
  1.2039 +		     val pre = check_preconds' prls where_ probl 0
  1.2040 +		 in pre end
  1.2041 +	val allcorrect = is_complete_mod_ probl
  1.2042 +			 andalso foldl and_ (true, (map #1 pre))
  1.2043 +    in ModSpec (allcorrect, Pbl, hdl, probl, pre, spec) end;
  1.2044 +
  1.2045 +
  1.2046 +fun pt_form (PrfObj {form,...}) = Form form
  1.2047 +  | pt_form (PblObj {probl,spec,origin=(_,spec',_),...}) =
  1.2048 +    let val (dI, pI, _) = get_somespec' spec spec'
  1.2049 +	val {cas,...} = get_pbt pI
  1.2050 +    in case cas of
  1.2051 +	   NONE => Form (pblterm dI pI)
  1.2052 +	 | SOME t => Form (subst_atomic (mk_env probl) t)
  1.2053 +    end;
  1.2054 +(*vvv takes the tac _generating_ the formula=result, asm ok....
  1.2055 +fun pt_result (PrfObj {result=(t,asm), tac,...}) = 
  1.2056 +    (Form t, 
  1.2057 +     if null asm then NONE else SOME asm, 
  1.2058 +     SOME tac)
  1.2059 +  | pt_result (PblObj {result=(t,asm), origin = (_,ospec,_), spec,...}) =
  1.2060 +    let val (_,_,metID) = some_spec ospec spec
  1.2061 +    in (Form t, 
  1.2062 +	if null asm then NONE else SOME asm, 
  1.2063 +	if metID = e_metID then NONE else SOME (Apply_Method metID)) end;
  1.2064 +-------------------------------------------------------------------------*)
  1.2065 +
  1.2066 +
  1.2067 +(*.pt_extract returns
  1.2068 +      # the formula at pos
  1.2069 +      # the tactic applied to this formula
  1.2070 +      # the list of assumptions generated at this formula
  1.2071 +	(by application of another tac to the preceding formula !)
  1.2072 +   pos is assumed to come from the frontend, ie. generated by moveDown.*)
  1.2073 +(*cannot be in ctree.sml, because ModSpec has to be calculated*)
  1.2074 +fun pt_extract (pt,([],Res)) =
  1.2075 +(* val (pt,([],Res)) = ptp;
  1.2076 +   *)
  1.2077 +    let val (f, asm) = get_obj g_result pt []
  1.2078 +    in (Form f, NONE, asm) end
  1.2079 +(* val p = [3,2];
  1.2080 +   *)
  1.2081 +  | pt_extract (pt,(p,Res)) =
  1.2082 +(* val (pt,(p,Res)) = ptp;
  1.2083 +   *)
  1.2084 +    let val (f, asm) = get_obj g_result pt p
  1.2085 +	val tac = if last_onlev pt p
  1.2086 +		  then if is_pblobj' pt (lev_up p)
  1.2087 +		       then let val (PblObj{spec=(_,pI,_),...}) = 
  1.2088 +				    get_obj I pt (lev_up p)
  1.2089 +			    in if pI = e_pblID then NONE 
  1.2090 +			       else SOME (Check_Postcond pI) end
  1.2091 +		       else SOME End_Trans (*WN0502 TODO for other branches*)
  1.2092 +		  else let val p' = lev_on p
  1.2093 +		       in if is_pblobj' pt p'
  1.2094 +			  then let val (PblObj{origin = (_,(dI,pI,_),_),...}) =
  1.2095 +				       get_obj I pt p'
  1.2096 +			       in SOME (Subproblem (dI, pI)) end
  1.2097 +			  else if f = get_obj g_form pt p'
  1.2098 +			  then SOME (get_obj g_tac pt p')
  1.2099 +			  (*because this Frm          ~~~is not on worksheet*)
  1.2100 +			  else SOME (Take (term2str (get_obj g_form pt p')))
  1.2101 +		       end
  1.2102 +    in (Form f, tac, asm) end
  1.2103 +	
  1.2104 +  | pt_extract (pt, pos as (p,p_(*Frm,Pbl*))) =
  1.2105 +(* val (pt, pos as (p,p_(*Frm,Pbl*))) = ptp;
  1.2106 +   val (pt, pos as (p,p_(*Frm,Pbl*))) = (pt, p);
  1.2107 +   *)
  1.2108 +    let val ppobj = get_obj I pt p
  1.2109 +	val f = if is_pblobj ppobj then pt_model ppobj p_
  1.2110 +		else get_obj pt_form pt p
  1.2111 +	val tac = g_tac ppobj
  1.2112 +    in (f, SOME tac, []) end;
  1.2113 +
  1.2114 +
  1.2115 +(**. get the formula from a ctree-node:
  1.2116 + take form+res from PblObj and 1.PrfObj and (PrfObj after PblObj)
  1.2117 + take res from all other PrfObj's .**)
  1.2118 +(*designed for interSteps, outcommented 04 in favour of calcChangedEvent*)
  1.2119 +fun formres p (Nd (PblObj {origin = (_,_, h), result = (r, _),...}, _)) =
  1.2120 +    [("headline", (p, Frm), h), 
  1.2121 +     ("stepform", (p, Res), r)]
  1.2122 +  | formres p (Nd (PrfObj {form, result = (r, _),...}, _)) = 
  1.2123 +    [("stepform", (p, Frm), form), 
  1.2124 +     ("stepform", (p, Res), r)];
  1.2125 +
  1.2126 +fun form p (Nd (PrfObj {result = (r, _),...}, _)) = 
  1.2127 +    [("stepform", (p, Res), r)]
  1.2128 +
  1.2129 +(*assumes to take whole level, in particular hd -- for use in interSteps*)
  1.2130 +fun get_formress fs p [] = flat fs
  1.2131 +  | get_formress fs p (nd::nds) =
  1.2132 +    (* start with   'form+res'       and continue with trying 'res' only*)
  1.2133 +    get_forms (fs @ [formres p nd]) (lev_on p) nds
  1.2134 +and get_forms fs p [] = flat fs
  1.2135 +  | get_forms fs p (nd::nds) =
  1.2136 +    if is_pblnd nd
  1.2137 +    (* start again with      'form+res' ///ugly repeat with Check_elementwise
  1.2138 +    then get_formress (fs @ [formres p nd]) (lev_on p) nds                   *)
  1.2139 +    then get_forms    (fs @ [formres p nd]) (lev_on p) nds
  1.2140 +    (* continue with trying 'res' only*)
  1.2141 +    else get_forms    (fs @ [form    p nd]) (lev_on p) nds;
  1.2142 +
  1.2143 +(**.get an 'interval' 'from' 'to' of formulae from a ptree.**)
  1.2144 +(*WN050219 made robust against _'to' below or after Complete nodes
  1.2145 +	   by handling exn caused by move_dn*)
  1.2146 +(*WN0401 this functionality belongs to ctree.sml, 
  1.2147 +but fetching a calc_head requires calculations defined in modspec.sml
  1.2148 +transfer to ME/me.sml !!!
  1.2149 +WN051224 ^^^ doesnt hold any longer, since only the headline of a calc_head
  1.2150 +is returned !!!!!!!!!!!!!
  1.2151 +*)
  1.2152 +fun eq_pos' (p1,Frm) (p2,Frm) = p1 = p2
  1.2153 +  | eq_pos' (p1,Res) (p2,Res) = p1 = p2
  1.2154 +  | eq_pos' (p1,Pbl) (p2,p2_) = p1 = p2 andalso (case p2_ of
  1.2155 +						     Pbl => true
  1.2156 +						   | Met => true
  1.2157 +						   | _ => false)
  1.2158 +  | eq_pos' (p1,Met) (p2,p2_) = p1 = p2 andalso (case p2_ of
  1.2159 +						     Pbl => true
  1.2160 +						   | Met => true
  1.2161 +						   | _ => false)
  1.2162 +  | eq_pos' _ _ = false;
  1.2163 +
  1.2164 +(*.get an 'interval' from the ctree; 'interval' is w.r.t. the 
  1.2165 +   total ordering Position#compareTo(Position p) in the java-code
  1.2166 +val get_interval = fn
  1.2167 +    : pos' ->     : from is "move_up 1st-element" to return
  1.2168 +      pos' -> 	  : to the last element to be returned; from < to
  1.2169 +      int -> 	  : level: 0 gets the flattest sub-tree possible
  1.2170 +			   >999 gets the deepest sub-tree possible
  1.2171 +      ptree -> 	  : 
  1.2172 +      (pos' * 	  : of the formula
  1.2173 +       Term.term) : the formula
  1.2174 +	  list
  1.2175 +.*)
  1.2176 +fun get_interval from to level pt =
  1.2177 +(* val (from,level) = (f,lev);
  1.2178 +   val (from, to, level) = (([3, 2, 1], Res), ([],Res), 9999);
  1.2179 +   *)
  1.2180 +    let fun get_inter c (from:pos') (to:pos') lev pt =
  1.2181 +(* val (c, from, to, lev) = ([], from, to, level);
  1.2182 +   ------for recursion.......
  1.2183 +   val (c, from:pos', to:pos') = (c @ [(from, f)], move_dn [] pt from, to);
  1.2184 +   *)
  1.2185 +	    if eq_pos' from to orelse from = ([],Res)
  1.2186 +	    (*orelse ... avoids Exception- PTREE "end of calculation" raised,
  1.2187 +	     if 'to' has values NOT generated by move_dn, see systest/me.sml
  1.2188 +             TODO.WN0501: introduce an order on pos' and check "from > to"..
  1.2189 +             ...there is an order in Java! 
  1.2190 +             WN051224 the hack got worse with returning term instead ptform*)
  1.2191 +	    then let val (f,_,_) = pt_extract (pt, from)
  1.2192 +		 in case f of
  1.2193 +			ModSpec (_,_,headline,_,_,_) => c @ [(from, headline)] 
  1.2194 +		      | Form t => c @ [(from, t)]
  1.2195 +		 end
  1.2196 +	    else 
  1.2197 +		if lev < lev_of from
  1.2198 +		then (get_inter c (move_dn [] pt from) to lev pt)
  1.2199 +		     handle (PTREE _(*from move_dn too far*)) => c
  1.2200 +		else let val (f,_,_) = pt_extract (pt, from)
  1.2201 +			 val term = case f of
  1.2202 +					ModSpec (_,_,headline,_,_,_)=> headline
  1.2203 +				      | Form t => t
  1.2204 +		     in (get_inter (c @ [(from, term)]) 
  1.2205 +				   (move_dn [] pt from) to lev pt)
  1.2206 +			handle (PTREE _(*from move_dn too far*)) 
  1.2207 +			       => c @ [(from, term)] end
  1.2208 +    in get_inter [] from to level pt end;
  1.2209 +
  1.2210 +(*for tests*)
  1.2211 +fun posform2str (pos:pos', form) =
  1.2212 +    "("^ pos'2str pos ^", "^
  1.2213 +    (case form of 
  1.2214 +	 Form f => term2str f
  1.2215 +       | ModSpec c => term2str (#3 c(*the headline*)))
  1.2216 +    ^")";
  1.2217 +fun posforms2str pfs = (strs2str' o (map (curry op ^ "\n")) o 
  1.2218 +			(map posform2str)) pfs;
  1.2219 +fun posterm2str (pos:pos', t) =
  1.2220 +    "("^ pos'2str pos ^", "^term2str t^")";
  1.2221 +fun posterms2str pfs = (strs2str' o (map (curry op ^ "\n")) o 
  1.2222 +			(map posterm2str)) pfs;
  1.2223 +
  1.2224 +
  1.2225 +(*WN050225 omits the last step, if pt is incomplete*)
  1.2226 +fun show_pt pt = 
  1.2227 +    writeln (posterms2str (get_interval ([],Frm) ([],Res) 99999 pt));
  1.2228 +
  1.2229 +(*.get a calchead from a PblObj-node in the ctree; 
  1.2230 +   preconditions must be calculated.*)
  1.2231 +fun get_ocalhd (pt, pos' as (p,Pbl):pos') = 
  1.2232 +    let val PblObj {origin = (oris, ospec, hdf'), spec, probl,...} = 
  1.2233 +	    get_obj I pt p
  1.2234 +	val {prls,where_,...} = get_pbt (#2 (some_spec ospec spec))
  1.2235 +	val pre = check_preconds (assoc_thy"Isac.thy") prls where_ probl
  1.2236 +    in (ocalhd_complete probl pre spec, Pbl, hdf', probl, pre, spec):ocalhd end
  1.2237 +| get_ocalhd (pt, pos' as (p,Met):pos') = 
  1.2238 +    let val PblObj {fmz = fmz as (fmz_,_), origin = (oris, ospec, hdf'), 
  1.2239 +		    spec, meth,...} = 
  1.2240 +	    get_obj I pt p
  1.2241 +	val {prls,pre,...} = get_met (#3 (some_spec ospec spec))
  1.2242 +	val pre = check_preconds (assoc_thy"Isac.thy") prls pre meth
  1.2243 +    in (ocalhd_complete meth pre spec, Met, hdf', meth, pre, spec):ocalhd end;
  1.2244 +
  1.2245 +(*.at the activeFormula set the Model, the Guard and the Specification 
  1.2246 +   to empty and return a CalcHead;
  1.2247 +   the 'origin' remains (for reconstructing all that).*)
  1.2248 +fun reset_calchead (pt, pos' as (p,_):pos') = 
  1.2249 +    let val PblObj {origin = (_, _, hdf'),...} = get_obj I pt p
  1.2250 +	val pt = update_pbl pt p []
  1.2251 +	val pt = update_met pt p []
  1.2252 +	val pt = update_spec pt p e_spec
  1.2253 +    in (pt, (p,Pbl):pos') end;
  1.2254 +
  1.2255 +(*---------------------------------------------------------------------*)
  1.2256 +end
  1.2257 +
  1.2258 +open CalcHead;
  1.2259 +(*---------------------------------------------------------------------*)
  1.2260 +