src/Tools/isac/Interpret/mstools.sml
author Walther Neuper <wneuper@ist.tugraz.at>
Mon, 07 Dec 2015 10:17:08 +0100
changeset 59184 831fa972f73b
parent 52114 10790ed771a7
child 59186 d9c3e373f8f5
permissions -rw-r--r--
sabelle2014-->15: cterm_of-->Thm.global_cterm_of
     1 (* Types and tools for 'modeling' und 'specifying' to be used in
     2    modspec.sml. The types are separated from calchead.sml into this file,
     3    because some of them are stored in the calc-tree, and thus are required
     4    _before_ ctree.sml. 
     5    author: Walther Neuper, Mathias Lehnfeld
     6    (c) due to copyright terms
     7 
     8 12345678901234567890123456789012345678901234567890123456789012345678901234567890
     9         10        20        30        40        50        60        70        80
    10 *)
    11 
    12 signature SPECIFY_TOOLS =
    13   sig
    14     type envv
    15     datatype
    16       item =
    17           Correct of cterm'
    18         | False of cterm'
    19         | Incompl of cterm'
    20         | Missing of cterm'
    21         | Superfl of string
    22         | SyntaxE of string
    23         | TypeE of string
    24     val item2str : item -> string
    25     type itm
    26     val itm2str_ : Proof.context -> itm -> string
    27     datatype
    28       itm_ =
    29           Cor of (term * term list) * (term * term list)
    30         | Inc of (term * term list) * (term * term list)
    31         | Mis of term * term
    32         | Par of cterm'
    33         | Sup of term * term list
    34         | Syn of cterm'
    35         | Typ of cterm'
    36     val itm_2str : itm_ -> string
    37     val itm_2str_ : Proof.context -> itm_ -> string
    38     val itms2str_ : Proof.context -> itm list -> string
    39     type 'a ppc
    40     val ppc2str :
    41        {Find: string list, With: string list, Given: string list,
    42          Where: string list, Relate: string list} -> string
    43     datatype
    44       match =
    45           Matches of pblID * item ppc
    46         | NoMatch of pblID * item ppc
    47     val match2str : match -> string
    48     datatype
    49       match_ =
    50           Match_ of pblID * (itm list * (bool * term) list)
    51         | NoMatch_
    52     val matchs2str : match list -> string
    53     type ori
    54     val ori2str : ori -> string
    55     val oris2str : ori list -> string
    56     type preori
    57     val preori2str : preori -> string
    58     val preoris2str : preori list -> string
    59     type penv
    60     (* val penv2str_ : Proof.context -> penv -> string *)
    61     type vats
    62     (*----------------------------------------------------------------------*)
    63     val all_ts_in : itm_ list -> term list
    64     val check_preconds :
    65        'a ->
    66        rls ->
    67        term list -> itm list -> (bool * term) list
    68     val check_preconds' :
    69        rls ->
    70        term list ->
    71        itm list -> 'a -> (bool * term) list
    72    (* val chkpre2item : rls -> term -> bool * item  *)
    73     val pres2str : (bool * term) list -> string
    74    (* val evalprecond : rls -> term -> bool * term  *)
    75    (* val cnt : itm list -> int -> int * int *)
    76     val comp_dts : term * term list -> term
    77     val comp_dts' : term * term list -> term
    78     val comp_dts'' : term * term list -> string
    79     val comp_ts : term * term list -> term
    80     val d_in : itm_ -> term
    81     val de_item : item -> cterm'
    82     val declare_constraints : string -> Proof.context -> Proof.context
    83     val declare_constraints' : term list -> Proof.context -> Proof.context
    84     val dest_list : term * term list -> term list (* for testing *)
    85     val dest_list' : term -> term list
    86     val dts2str : term * term list -> string
    87     val e_itm : itm
    88   (*  val e_listBool : term  *)
    89   (*  val e_listReal : term  *)
    90     val e_ori : ori
    91     val e_ori_ : ori
    92     val empty_ppc : item ppc
    93    (* val empty_ppc_ct' : cterm' ppc *)
    94    (* val getval : term * term list -> term * term *)
    95    (*val head_precond :
    96        domID * pblID * 'a ->
    97        term option ->
    98        rls ->
    99        term list ->
   100        itm list -> 'b -> term * (bool * term) list*)
   101    (* val init_item : string -> item *)
   102    (* val is_matches : match -> bool *)
   103    (* val is_matches_ : match_ -> bool *)
   104     val is_var : term -> bool
   105    (* val item_ppc :
   106        string ppc -> item ppc  *)
   107     val itemppc2str : item ppc -> string
   108    (* val matches_pblID : match -> pblID *)
   109     val max2 : ('a * int) list -> 'a * int
   110     val max_vt : itm list -> int
   111     val mk_e : itm_ -> (term * term) list
   112     val mk_en : int -> itm -> (term * term) list
   113     val mk_env : itm list -> (term * term) list
   114     val mkval : 'a -> term list -> term
   115     val mkval' : term list -> term
   116    (* val pblID_of_match : match -> pblID *)
   117     val pbl_ids : Proof.context -> term -> term -> term list
   118     val pbl_ids' : term -> term list -> term list
   119    (* val pen2str : theory -> term * term list -> string *)
   120     val penvval_in : itm_ -> term list
   121     val refined : match list -> pblID
   122     val refined_ :
   123        match_ list -> match_ option
   124   (*  val refined_IDitms :
   125        match list -> match option  *)
   126     val split_dts : term -> term * term list
   127     val split_dts' : term * term -> term list
   128   (*  val take_apart : term -> term list  *)
   129   (*  val take_apart_inv : term list -> term *)
   130     val ts_in : itm_ -> term list
   131    (* val unique : term  *)
   132     val untouched : itm list -> bool
   133     val upd :
   134        Proof.context ->
   135        (''a * (''b * term list) list) list ->
   136        term ->
   137        ''b * term -> ''a -> ''a * (''b * term list) list
   138     val upd_envv :
   139        Proof.context ->
   140        envv ->
   141        vats ->
   142        term -> term -> term -> envv
   143     val upd_penv :
   144        Proof.context ->
   145        (''a * term list) list ->
   146        term -> ''a * term -> (''a * term list) list
   147    (* val upds_envv :
   148        Proof.context ->
   149        envv ->
   150        (vats * term * term * term) list ->
   151        envv                         *)
   152     val vts_cnt : int list -> itm list -> (int * int) list
   153     val vts_in : itm list -> int list
   154    (* val w_itms2str_ : Proof.context -> itm list -> unit *)
   155     val get_assumptions : Proof.context -> term list
   156     val insert_assumptions : term list -> Proof.context -> Proof.context
   157     val transfer_asms_from_to : Proof.context -> Proof.context -> Proof.context
   158     val from_subpbl_to_caller : Proof.context -> term -> Proof.context -> Proof.context
   159  
   160   end
   161 
   162 (*----------------------------------------------------------*)
   163 structure SpecifyTools : SPECIFY_TOOLS =
   164 struct
   165 (*----------------------------------------------------------*)
   166 val script_parse = the o (@{theory Script} |> thy2ctxt |> parseNEW);
   167 val e_listReal = script_parse "[]::(real list)";
   168 val e_listBool = script_parse "[]::(bool list)";
   169 
   170 (*.take list-term apart w.r.t. handling elementwise input.*)
   171 fun take_apart t =
   172     let val elems = isalist2list t
   173     in map ((list2isalist (type_of (hd elems))) o single) elems end;
   174 (*val t = str2term "[a, b]";
   175 > val ts = take_apart t; tracing (terms2str ts);
   176 ["[a]","[b]"] 
   177 
   178 > t = (take_apart_inv o take_apart) t;
   179 true*)
   180 fun take_apart_inv ts =
   181     let val elems = (flat o (map isalist2list)) ts;
   182     in list2isalist (type_of (hd elems)) elems end;
   183 (*val ts = [str2term "[a]", str2term "[b]"];
   184 > val t = take_apart_inv ts; term2str t;
   185 "[a, b]"
   186 
   187 ts = (take_apart o take_apart_inv) ts;
   188 true*)
   189 
   190 
   191 
   192 
   193 (*.revert split_dts only for ts; compare comp_dts.*)
   194 fun comp_ts (d, ts) = 
   195     if is_list_dsc d
   196     then if is_list (hd ts)
   197 	 then if is_unl d
   198 	      then (hd ts)            (*e.g. someList [1,3,2]*)
   199 	      else (take_apart_inv ts) 
   200 	 (*             SML[ [a], [b] ]SML --> [a,b]             *)
   201 	 else (hd ts) (*a variable or metavariable for a list*)
   202     else (hd ts);
   203 (*.revert split_.
   204  WN050903 we do NOT know which is from subtheory, description or term;
   205  typecheck thus may lead to TYPE-error 'unknown constant';
   206  solution: typecheck with (Thy_Info.get_theory "Isac"); i.e. arg 'thy' superfluous*)
   207 (*fun comp_dts thy (d,[]) = 
   208     Thm.global_cterm_of (*(sign_of o assoc_thy) "Isac"*)
   209 	     (Thy_Info.get_theory "Isac")
   210 	     (*comp_dts:FIXXME stay with term for efficiency !!!*)
   211 	     (if is_reall_dsc d then (d $ e_listReal)
   212 	      else if is_booll_dsc d then (d $ e_listBool)
   213 	      else d)
   214   | comp_dts (d,ts) =
   215     (Thm.global_cterm_of (*(sign_of o assoc_thy) "Isac"*)
   216 	      (Thy_Info.get_theory "Isac")
   217 	      (*comp_dts:FIXXME stay with term for efficiency !!*)
   218 	      (d $ (comp_ts (d, ts)))
   219        handle _ => error ("comp_dts: "^(term2str d)^
   220 				" $ "^(term2str (hd ts))));*)
   221 fun comp_dts (d,[]) = 
   222 	     (if is_reall_dsc d then (d $ e_listReal)
   223 	      else if is_booll_dsc d then (d $ e_listBool)
   224 	      else d)
   225   | comp_dts (d,ts) =
   226 	      (d $ (comp_ts (d, ts)))
   227        handle _ => error ("comp_dts: "^(term2str d)^
   228 				" $ "^(term2str (hd ts))); 
   229 (*25.8.03*)
   230 fun comp_dts' (d,[]) = 
   231     if is_reall_dsc d then (d $ e_listReal)
   232     else if is_booll_dsc d then (d $ e_listBool)
   233     else d
   234   | comp_dts' (d,ts) = (d $ (comp_ts (d, ts)))
   235        handle _ => error ("comp_dts': "^(term2str d)^
   236 				" $ "^(term2str (hd ts))); 
   237 (*val t = str2term "maximum A"; 
   238 > val (d,ts) = split_dts thy t; comp_dts thy (d,ts);
   239 val it = "maximum A" : cterm
   240 > val t = str2term "fixedValues [r=Arbfix]"; 
   241 > val (d,ts) = split_dts thy t; comp_dts thy (d,ts);
   242 "fixedValues [r = Arbfix]"
   243 > val t = str2term "valuesFor [a]"; 
   244 > val (d,ts) = split_dts thy t; comp_dts thy (d,ts);
   245 "valuesFor [a]"
   246 > val t = str2term "valuesFor [a,b]"; 
   247 > val (d,ts) = split_dts thy t; comp_dts thy (d,ts);
   248 "valuesFor [a, b]"
   249 > val t = str2term "relations [A=a*b, (a/2)^^^2 + (b/2)^^^2 = r^^^2]"; 
   250 > val (d,ts) = split_dts thy t; comp_dts thy (d,ts);
   251 relations [A = a * b, (a / 2) ^^^ 2 + (b / 2) ^^^ 2 = r ^^^ 2]"
   252 > val t = str2term "boundVariable a";
   253 > val (d,ts) = split_dts thy t; comp_dts thy (d,ts);
   254 "boundVariable a"
   255 > val t = str2term "interval {x::real. 0 <= x & x <= 2*r}"; 
   256 > val (d,ts) = split_dts thy t; comp_dts thy (d,ts);
   257 "interval {x. 0 <= x & x <= 2 * r}"
   258 
   259 > val t = str2term "equality (sqrt(9+4*x)=sqrt x + sqrt(5+x))"; 
   260 > val (d,ts) = split_dts thy t; comp_dts thy (d,ts);
   261 "equality (sqrt (9 + 4 * x) = sqrt x + sqrt (5 + x))"
   262 > val t = str2term "solveFor x"; 
   263 > val (d,ts) = split_dts thy t; comp_dts thy (d,ts);
   264 "solveFor x"
   265 > val t = str2term "errorBound (eps=0)"; 
   266 > val (d,ts) = split_dts thy t; comp_dts thy (d,ts);
   267 "errorBound (eps = 0)"
   268 > val t = str2term "solutions L";
   269 > val (d,ts) = split_dts thy t; comp_dts thy (d,ts);
   270 "solutions L"
   271 
   272 before 6.5.03:
   273 > val t = (term_of o the o (parse thy)) "testdscforlist [#1]";
   274 > val (d,ts) = split_dts t;
   275 > comp_dts thy (d,ts);
   276 val it = "testdscforlist [#1]" : cterm
   277 
   278 > val t = (term_of o the o (parse thy)) "(A::real)";
   279 > val (d,ts) = split_dts t;
   280 val d = Const ("empty","empty") : term
   281 val ts = [Free ("A","RealDef.real")] : term list
   282 > val t = (term_of o the o (parse thy)) "[R=(R::real)]";
   283 > val (d,ts) = split_dts t;
   284 val d = Const ("empty","empty") : term
   285 val ts = [Const # $ Free # $ Free (#,#)] : term list
   286 > val t = (term_of o the o (parse thy)) "[#1,#2]";
   287 > val (d,ts) = split_dts t;
   288 val ts = [Free ("#1","'a"),Free ("#2","'a")] : NOT WANTED
   289 *)
   290 
   291 (*for input_icalhd 11.03*)
   292 fun comp_dts'' (d,[]) = 
   293     if is_reall_dsc d then term2str (d $ e_listReal)
   294     else if is_booll_dsc d then term2str (d $ e_listBool)
   295     else term2str d
   296   | comp_dts'' (d,ts) = term2str (d $ (comp_ts (d, ts)))
   297        handle _ => error ("comp_dts'': "^(term2str d)^
   298 				" $ "^(term2str (hd ts))); 
   299 
   300 
   301 
   302 
   303 
   304 
   305 (* this may decompose an object-language isa-list;
   306    use only, if description is not available, eg. not input ?WN:14.5.03 ??!?*)
   307 fun dest_list' t = if is_list t then isalist2list t  else [t];
   308 
   309 (*fun is_metavar (Free (str, _)) =
   310     if (last_elem o Symbol.explode) str = "_" then true else false
   311   | is_metavar _ = false;*)
   312 fun is_var (Free _) = true
   313   | is_var _ = false;
   314 
   315 (*.special handling for lists. ?WN:14.5.03 ??!?*)
   316 fun dest_list (d,ts) = 
   317   let fun dest t = 
   318     if is_list_dsc d andalso not (is_unl d) 
   319       andalso not (is_var t) (*..for pbt*)
   320       then isalist2list t  else [t]
   321   in (flat o (map dest)) ts end;
   322 
   323 
   324 (*.decompose an input into description, terms (ev. elems of lists),
   325     and the value for the problem-environment; inv to comp_dts .*)
   326 (*WN.8.6.03: corrected with minimal effort,
   327 fn : theory -> term ->
   328      term *       description
   329      term list *  lists decomposed for elementwise input
   330      term list    pbl_ids not _HERE_: dont know which list-elems input*)
   331 fun split_dts (t as d $ arg) =
   332       if is_dsc d then
   333         if is_list_dsc d andalso is_list arg andalso is_unl d |> not then
   334           (d, take_apart arg) else
   335           (d, [arg]) else
   336         (e_term, dest_list' t)
   337   | split_dts t =
   338       let val t' as (h, _) = strip_comb t;
   339       in if is_dsc h then
   340         (h, dest_list t') else
   341         (e_term, dest_list' t)
   342       end;
   343 
   344 (* tests see fun comp_dts 
   345 
   346 > val t = str2term "someList []";
   347 > val (_,ts) = split_dts thy t; tracing (terms2str ts);
   348 ["[]"]
   349 > val t = str2term "valuesFor []";
   350 > val (_,ts) = split_dts thy t; tracing (terms2str ts);
   351 ["[]"]*)
   352 
   353 (*.version returning ts only.*)
   354 fun split_dts' (d, arg) = 
   355     if is_dsc d
   356     then if is_list_dsc d
   357 	 then if is_list arg
   358 	      then if is_unl d
   359 		   then ([arg])                 (*e.g. someList [1,3,2]*)
   360 		   else (take_apart arg)(*[a,b] --> SML[ [a], [b] ]SML*)
   361 	      else ([arg])      (*a variable or metavariable for a list*)
   362 	 else ([arg])
   363     else (dest_list' arg(*9.01 ???*))
   364   | split_dts' (d, t) = (*either dsc or term; 14.5.03 only copied*)
   365   let val (h,argl) = strip_comb t
   366   in if (not o is_dsc) h then (dest_list' t)
   367      else (dest_list (h,argl))
   368   end;
   369 
   370 
   371 
   372 
   373 
   374 (*27.8.01: problem-environment
   375 WN.6.5.03: FIXXME reconsider if penv is worth the effort --
   376            -- just rerun a whole expl with num/var may show the same ?!
   377 WN.9.5.03: penv-concept stalled, immediately generate script env !
   378            but [#0, epsilon] only outcommented for eventual reconsideration  
   379 *)
   380 type penv = (term     (*err_*)
   381 	     * (term list)  (*[#0, epsilon] 9.5.03 outcommented*)
   382 	     ) list;
   383 fun pen2str ctxt (t, ts) =
   384     pair2str (term_to_string'  ctxt t, 
   385       (strs2str' o map (term_to_string'  ctxt)) ts);
   386 fun penv2str_ thy (penv:penv) = (strs2str' o (map (pen2str thy))) penv;
   387 
   388 (*
   389   9.5.03: still unused, but left for eventual future development*)
   390 type envv = (int * penv) list; (*over variants*)
   391 
   392 (*. 14.9.01: not used after putting penv-values into itm_
   393       make the result of split_* a value of problem-environment .*)
   394 fun mkval dsc [] = error "mkval called with []"
   395   | mkval dsc [t] = t
   396   | mkval dsc ts = list2isalist ((type_of o hd) ts) ts;
   397 (*WN.12.12.03*)
   398 fun mkval' x = mkval e_term x;
   399 
   400 
   401 
   402 (*. get the constant value from a penv .*)
   403 fun getval (id, values) = 
   404     case values of
   405 	[] => error ("penv_value: no values in '" ^ term2str id)
   406       | [v] => (id, v)
   407       | (v1::v2::_) => (case v1 of 
   408 			     Const ("Script.Arbfix",_) => (id, v2)
   409 			   | _ => (id, v1));
   410 (*
   411   val e_ = (term_of o the o (parse thy)) "e_::bool";
   412   val ev = (term_of o the o (parse thy)) "#4 + #3 * x^^^#2 = #0";
   413   val v_ = (term_of o the o (parse thy)) "v_";
   414   val vv = (term_of o the o (parse thy)) "x";
   415   val r_ = (term_of o the o (parse thy)) "err_::bool";
   416   val rv1 = (term_of o the o (parse thy)) "#0";
   417   val rv2 = (term_of o the o (parse thy)) "eps";
   418 
   419   val penv = [(e_,[ev]),(v_,[vv]),(r_,[rv2,rv2])]:penv;
   420   map getval penv;
   421 [(Free ("e_","bool"),
   422   Const (#,#) $ (# $ # $ (# $ #)) $ Free ("#0","RealDef.real")),
   423  (Free ("v_","RealDef.real"),Free ("x","RealDef.real")),
   424  (Free ("err_","bool"),Free ("#0","RealDef.real"))] : (term * term) list      
   425 *)
   426 
   427 
   428 (*==========================================================================
   429 23.3.02 TODO: ideas on redesign of type itm_,type item,type ori,type item ppc
   430 (1) kinds of itms:
   431   (1.1) untouched: for modeling only dsc displayed(impossible after match_itms)
   432         =(presently) Mis (? should be Inc initially, and Mis after match_itms?)
   433   (1.2)  Syn,Typ,Sup: not related to oris
   434     Syn, Typ (presently) should be accepted in appl_add (instead Error')
   435     Sup      (presently) should be accepted in appl_add (instead Error')
   436          _could_ be w.r.t current vat (and then _is_ related to vat
   437     Mis should _not_ be  made Inc ((presently, by appl_add & match_itms)
   438 - dsc in itm_ is timeconsuming -- keep id for respective queries ?
   439 - order of items in ppc should be stable w.r.t order of itms
   440 
   441 - stepwise input of itms --- match_itms (in one go) ..not coordinated
   442   - unify code
   443   - match_itms / match_itms_oris ..2 versions ?!
   444     (fast, for refine / slow, for modeling)
   445 
   446 - clarify: efficiency <--> simplicity !!!
   447   ?: shift dsc itm_ -> itm | discard int in ori,itm | take int instead dsc 
   448     | take int for perserving order of item ppc in itms 
   449     | make all(!?) handling of itms stable against reordering(?)
   450     | field in ori ?? (not from fmz!) -- meant for efficiency (not doc!???)
   451       -"- "#undef" ?= not touched ?= (id,..)
   452 -----------------------------------------------------------------
   453 27.3.02:
   454 def: type pbt = (field, (dsc, pid)) *** design considerations ***
   455 
   456 (1) fmz + pbt -> oris
   457 (2) input + oris -> itm
   458 (3) match_itms      : schnell(?) f"ur refine
   459     match_itms_oris : r"uckmeldung f"ur item ppc
   460 
   461 (1.1) in oris fehlt daher pid: (i,v,f,d,ts,pid)
   462 ---------- ^^^^^ --- dh. pbt meist als argument zu viel !!!
   463 
   464 (3.1) abwarten, wie das matchen mehr unterschiedlicher pbt's sich macht;
   465       wenn Problem pbt v"ollig neue, dann w"are eigentlich n"otig ????:
   466       (a) (_,_,d1,ts,_):ori + pbt -> (i,vt,d2,ts,pid)  dh.vt neu  ????
   467       (b) 
   468 ==========================================================================*)
   469 
   470 (*the internal representation of a models' item
   471 
   472   4.9.01: not consistent:
   473   after Init_Proof 'Inc', but after copy_probl 'Mis' - for same situation
   474   (involves 'is_error');
   475   bool in itm really necessary ???*)
   476 datatype itm_ = 
   477     Cor of (term *              (* description *)
   478 	    (term list)) *      (* for list: elem-wise input *) 
   479 	   (*split_dts <-> comp_dts*)
   480 	   (term * (term list)) (* elem of penv *)
   481 	 (*9.5.03:  ---- is already for script -- penv delayed to future*)
   482   | Syn of cterm'
   483   | Typ of cterm'
   484   | Inc of (term * (term list))	* (term * (term list)) (*lists,
   485 				+ init_pbl WN.11.03 FIXXME: empty penv .. bad
   486                                 init_pbl should return Mis !!!*)
   487   | Sup of (term * (term list)) (* user-input not found in pbt(+?oris?11.03)*)
   488   | Mis of (term * term)        (* after re-specification pbt-item not found 
   489                                    in pbl: only dsc, pid_*)
   490   | Par of cterm';  (*internal state from fun parsitm*)
   491 
   492 type vats = int list;      (*variants in formalizations*)
   493 
   494 (*.data-type for working on pbl/met-ppc: 
   495    in pbl initially holds descriptions (only) for user guidance.*)
   496 type itm = 
   497   int *        (* id  =0 .. untouched - descript (only) from init 
   498 		  23.3.02: seems to correspond to ori (fun insert_ppc)
   499 		           <> maintain order in item ppc?*)
   500   vats *       (* variants - copy from ori *)
   501   bool *       (* input on this item is not/complete *)
   502   string *     (* #Given | #Find | #Relate *)
   503   itm_;        (*  *)
   504 (* use"ME/sequent.sml";
   505    *)
   506 val e_itm = (0,[],false,"e_itm",Syn"e_itm"):itm;
   507 (*in CalcTree/Subproblem an 'untouched' model is created
   508   FIXME.WN.9.03 model should be filled to 'untouched' by Model/Refine_Problem*)
   509 fun untouched (itms: itm list) = 
   510     foldl and_ (true ,map ((curry op= 0) o #1) itms);
   511 (*> untouched [];
   512 val it = true : bool
   513 > untouched [e_itm];
   514 val it = true : bool
   515 > untouched [e_itm, (1,[],false,"e_itm",Syn "e_itm")];
   516 val it = false : bool*)
   517 
   518 
   519 
   520 
   521 
   522 (* find most frequent variant v in itms *)
   523 
   524 fun vts_in itms = (distinct o flat o (map #2)) (itms:itm list);
   525 
   526 fun cnt itms v = (v,(length o (filter (curry op= v)) o 
   527 		     flat o (map #2)) (itms:itm list));
   528 fun vts_cnt vts itms = map (cnt itms) vts;
   529 fun max2 [] = error "max2 of []"
   530   | max2 (y::ys) =
   531   let fun mx (a,x) [] = (a,x)
   532 	| mx (a,x) ((b,y)::ys) = 
   533     if x < y then mx (b,y) ys else mx (a,x) ys;
   534 in mx y ys end;
   535 
   536 (*. find the variant with most items already input .*)
   537 fun max_vt itms = 
   538     let val vts = (vts_cnt (vts_in itms)) itms;
   539     in if vts = [] then 0 else (fst o max2) vts end;
   540 
   541 
   542 (* TODO ev. make more efficient by avoiding flat *)
   543 fun mk_e (Cor (_, iv)) = [getval iv]
   544   | mk_e (Syn _) = []
   545   | mk_e (Typ _) = [] 
   546   | mk_e (Inc (_, iv)) = [getval iv]
   547   | mk_e (Sup _) = []
   548   | mk_e (Mis _) = [];
   549 fun mk_en vt ((i,vts,b,f,itm_):itm) =
   550     if member op = vts vt then mk_e itm_ else [];
   551 (*. extract the environment from an item list; 
   552     takes the variant with most items .*)
   553 fun mk_env itms = 
   554     let val vt = max_vt itms
   555     in (flat o (map (mk_en vt))) itms end;
   556 
   557 
   558 
   559 (*. example as provided by an author, complete w.r.t. pbt specified 
   560     not touched by any user action                                 .*)
   561 type ori = (int *      (* id: 10.3.00ff impl. only <>0 .. touched 
   562 			  21.3.02: insert_ppc needs it ! ?:purpose maintain
   563 				   order in item ppc ???*)
   564 	    vats *     (* variants 21.3.02: related to pbt..discard ?*)
   565 	    string *   (* #Given | #Find | #Relate 21.3.02: discard ?*)
   566 	    term *     (* description *)
   567 	    term list  (* isalist2list t | [t] *)
   568 	    );
   569 val e_ori_ = (0,[],"",e_term,[e_term]):ori;
   570 val e_ori = (0,[],"",e_term,[e_term]):ori;
   571 
   572 fun ori2str ((i,vs,fi,t,ts):ori) = 
   573     "("^(string_of_int i)^", "^((strs2str o (map string_of_int)) vs)^", "^fi^","^
   574     (term2str t)^", "^((strs2str o (map term2str)) ts)^")";
   575 val oris2str = 
   576     let (*val s = !show_types
   577 	val _ = show_types:= true*)
   578 	val str = (strs2str' o (map (linefeed o ori2str)))
   579 	(*val _ = show_types:= s*)
   580     in str end;
   581 
   582 (*.an or without leading integer.*)
   583 type preori = (vats *  
   584 	       string *   
   585 	       term *     
   586 	       term list);
   587 fun preori2str ((vs,fi,t,ts):preori) = 
   588     "("^((strs2str o (map string_of_int)) vs)^", "^fi^", "^
   589     (term2str t)^", "^((strs2str o (map term2str)) ts)^")";
   590 val preoris2str = (strs2str' o (map (linefeed o preori2str)));
   591 
   592 (*. given the input value (from split_dts)
   593     make the value in a problem-env according to description-type .*)
   594 (*28.8.01: .nam and .una impl. properly, others copied .. TODO*)
   595 fun pbl_ids ctxt (Const(_,Type("fun",[_,Type("Tools.nam",_)]))) v =
   596     if is_list v 
   597     then [v]         (*eg. [r=Arbfix]*)
   598     else (case v of  (*eg. eps=#0*)
   599 	      (Const ("HOL.eq",_) $ l $ r) => [r,l]
   600 	    | _ => error ("pbl_ids Tools.nam: no equality " ^ term_to_string'  ctxt v))
   601   | pbl_ids ctxt (Const(_,Type("fun",[_,Type("Tools.una",_)]))) v = [v]
   602   | pbl_ids ctxt (Const(_,Type("fun",[_,Type("Tools.unl",_)]))) v = [v]
   603   | pbl_ids ctxt (Const(_,Type("fun",[_,Type("Tools.str",_)]))) v = [v]
   604   | pbl_ids ctxt (Const(_,Type("fun",[_,Type("Tools.toreal",_)]))) v = [v] 
   605   | pbl_ids ctxt (Const(_,Type("fun",[_,Type("Tools.toreall",_)])))v = [v] 
   606   | pbl_ids ctxt (Const(_,Type("fun",[_,Type("Tools.unknown",_)])))v = [v] 
   607   | pbl_ids ctxt _ v = 
   608     error ("pbl_ids: not implemented for " ^ term2str v);
   609 (*
   610 val t as t1 $ t2 = str2term "antiDerivativeName M_b";
   611 pbl_ids ctxt t1 t2;
   612 
   613   val t = (term_of o the o (parse thy)) "fixedValues [r=Arbfix]";
   614   val (d,argl) = strip_comb t;
   615   is_dsc d;                      (*see split_dts*)
   616   dest_list (d,argl);
   617   val (_ $ v) = t;
   618   is_list v;
   619   pbl_ids ctxt d v;
   620 [Const ("List.list.Cons","[bool, bool List.list] => bool List.list") $
   621        (Const # $ Free # $ Const (#,#)) $ Const ("List.list.Nil","bool List..
   622 
   623   val (dsc,vl) = (split_dts o term_of o the o (parse thy)) "solveFor x";
   624 val dsc = Const ("Descript.solveFor","RealDef.real => Tools.una") : term
   625 val vl = Free ("x","RealDef.real") : term 
   626 
   627   val (dsc,id) = (split_did o term_of o the o (parse thy)) "solveFor v_";
   628   pbl_ids ctxt dsc vl;
   629 val it = [Free ("x","RealDef.real")] : term list
   630    
   631   val (dsc,vl) = (split_dts o term_of o the o(parse thy))
   632 		       "errorBound (eps=#0)";
   633   val (dsc,id) = (split_did o term_of o the o(parse thy)) "errorBound err_";
   634   pbl_ids ctxt dsc vl;
   635 val it = [Free ("#0","RealDef.real"),Free ("eps","RealDef.real")] : term list     *)
   636 
   637 (*. given an already input itm, ((14.9.01: no difference to pbl_ids jet!!))
   638     make the value in a problem-env according to description-type .*)
   639 (*28.8.01: .nam and .una impl. properly, others copied .. TODO*)
   640 fun pbl_ids' (Const(_,Type("fun",[_,Type("Tools.nam",_)]))) vs =
   641     (case vs of 
   642 	 [] => error ("pbl_ids' Tools.nam called with []")
   643        | [t] => (case t of  (*eg. eps=#0*)
   644 		     (Const ("HOL.eq",_) $ l $ r) => [r,l]
   645 		   | _ => 
   646                      error ("pbl_ids' Tools.nam: no equality " ^ term2str t))
   647        | vs' => vs (*14.9.01: ???TODO *))
   648   | pbl_ids' (Const(_,Type("fun",[_,Type("Tools.una",_)]))) vs = vs
   649   | pbl_ids' (Const(_,Type("fun",[_,Type("Tools.unl",_)]))) vs = vs
   650   | pbl_ids' (Const(_,Type("fun",[_,Type("Tools.str",_)]))) vs = vs
   651   | pbl_ids' (Const(_,Type("fun",[_,Type("Tools.toreal",_)]))) vs = vs 
   652   | pbl_ids' (Const(_,Type("fun",[_,Type("Tools.toreall",_)])))vs = vs 
   653   | pbl_ids' (Const(_,Type("fun",[_,Type("Tools.unknown",_)])))vs = vs 
   654   | pbl_ids'  _ vs = 
   655     error ("pbl_ids': not implemented for " ^ terms2str vs);
   656 (*9.5.03 penv postponed: pbl_ids'*)
   657 fun pbl_ids' d vs = [comp_ts (d, vs)];
   658 
   659 
   660 (*14.9.01: not used after putting values for penv into itm_
   661   WN.5.5.03: used in upd .. upd_envv*)
   662 fun upd_penv ctxt penv dsc (id, vl) =
   663 (tracing"### upd_penv used !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
   664  tracing"### upd_penv used !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
   665  tracing"### upd_penv used !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
   666   overwrite (penv, (id, pbl_ids ctxt dsc vl))
   667 );
   668 (* 
   669   val penv = [];
   670   val (dsc,vl) = (split_did o term_of o the o (parse thy)) "solveFor x";
   671   val (dsc,id) = (split_did o term_of o the o (parse thy)) "solveFor v_";
   672   val penv = upd_penv thy penv dsc (id, vl);
   673 [(Free ("v_","RealDef.real"),
   674   [Const (#,#) $ Free (#,#) $ Free ("#0","RealDef.real")])]
   675 : (term * term list) list                                                     
   676 
   677   val (dsc,vl) = (split_did o term_of o the o(parse thy))"errorBound (eps=#0)";
   678   val (dsc,id) = (split_did o term_of o the o(parse thy))"errorBound err_";
   679   upd_penv thy penv dsc (id, vl);
   680 [(Free ("v_","RealDef.real"),
   681   [Const (#,#) $ Free (#,#) $ Free ("#0","RealDef.real")]),
   682  (Free ("err_","bool"),
   683   [Free ("#0","RealDef.real"),Free ("eps","RealDef.real")])]
   684 : (term * term list) list    ^.........!!!!
   685 *)
   686 
   687 (*WN.9.5.03: not reconsidered; looks strange !!!*)
   688 fun upd thy envv dsc (id, vl) i =
   689     let val penv = case assoc (envv, i) of
   690 		       SOME e => e
   691 		     | NONE => [];
   692         val penv' = upd_penv thy penv dsc (id, vl);
   693     in (i, penv') end;
   694 (*
   695   val i = 2;
   696   val envv = [(1,[]:penv),(2,[]:penv),(3,[]:penv)]:envv;
   697   val (dsc,vl) = (split_did o term_of o the o(parse thy))"boundVariable b";
   698   val (dsc,id) = (split_did o term_of o the o(parse thy))"boundVariable v_";
   699   upd thy envv dsc (id, vl) i;
   700 val it = (2,[(Free ("v_","RealDef.real"),[Free ("b","RealDef.real")])])
   701   : int * (term * term list) list*)
   702 
   703 
   704 (*14.9.01: not used after putting pre-penv into itm_*)
   705 fun upd_envv thy (envv:envv) (vats:vats) dsc id vl  =
   706     let val vats = if length vats = 0 
   707 		   then (*unknown id to _all_ variants*)
   708 		       if length envv = 0 then [1]
   709 		       else (intsto o length) envv 
   710 		   else vats
   711 	fun isin vats (i,_) = member op = vats i;
   712 	val envs_notin_vat = filter_out (isin vats) envv;
   713     in ((map (upd thy envv dsc (id, vl)) vats) @ envs_notin_vat):envv end;
   714 (*
   715   val envv = [(1,[]:penv),(2,[]:penv),(3,[]:penv)]:envv;
   716  
   717   val vats = [2] 
   718   val (dsc,vl) = (split_did o term_of o the o(parse thy))"boundVariable b";
   719   val (dsc,id) = (split_did o term_of o the o(parse thy))"boundVariable v_";
   720   val envv = upd_envv thy envv vats dsc id vl;
   721 val envv = [(2,[(Free ("v_","RealDef.real"),[Free ("b","RealDef.real")])])]
   722   : (int * (term * term list) list) list
   723 
   724   val vats = [1,2,3];
   725   val (dsc,vl) = (split_did o term_of o the o(parse thy))"maximum A";
   726   val (dsc,id) = (split_did o term_of o the o(parse thy))"maximum m_";
   727   upd_envv thy envv vats dsc id vl;
   728 [(1,[(Free ("m_","bool"),[Free ("A","bool")])]),
   729  (2,
   730   [(Free ("v_","RealDef.real"),[Free ("b","RealDef.real")]),
   731    (Free ("m_","bool"),[Free ("A","bool")])]),
   732  (3,[(Free ("m_","bool"),[Free ("A","bool")])])]
   733 : (int * (term * term list) list) list
   734 
   735 
   736   val env = []:envv;
   737   val (d,ts) = (split_dts o term_of o the o (parse thy))
   738 		   "fixedValues [r=Arbfix]";
   739   val (_,id) = (split_did o term_of o the o (parse thy))"fixedValues fix_";
   740   val vats = [1,2,3];
   741   val env = upd_envv thy env vats d id (mkval ts);
   742 *)
   743 
   744 (*. update envv by folding from a list of arguments .*)
   745 fun upds_envv thy envv [] = envv
   746   | upds_envv thy envv ((vs, dsc, id, vl)::ps) = 
   747     upds_envv thy (upd_envv thy envv vs dsc id vl) ps;
   748 (* eval test-maximum.sml until Specify_Method ...
   749   val PblObj{probl=(_,pbl),origin=(_,(_,_,mI),_),...} = get_obj I pt [];
   750   val met = (#ppc o get_met) mI;
   751 
   752   val envv = [];
   753   val eargs = flat eargs;
   754   val (vs, dsc, id, vl) = hd eargs;
   755   val envv = upds_envv thy envv [(vs, dsc, id, vl)];
   756 
   757   val (vs, dsc, id, vl) = hd (tl eargs);
   758   val envv = upds_envv thy envv [(vs, dsc, id, vl)];
   759 
   760   val (vs, dsc, id, vl) = hd (tl (tl eargs));
   761   val envv = upds_envv thy envv [(vs, dsc, id, vl)];
   762 
   763   val (vs, dsc, id, vl) = hd (tl (tl (tl eargs)));
   764   val envv = upds_envv thy envv [(vs, dsc, id, vl)];
   765 [(1,
   766   [(Free ("fix_","bool List.list"),[Const (#,#),Free (#,#)]),
   767    (Free ("m_","bool"),[Free (#,#)]),
   768    (Free ("vs_","bool List.list"),[# $ # $ Const #]),
   769    (Free ("rs_","bool List.list"),[# $ # $ (# $ #)])]),
   770  (2,
   771   [(Free ("fix_","bool List.list"),[Const (#,#),Free (#,#)]),
   772    (Free ("m_","bool"),[Free (#,#)]),
   773    (Free ("vs_","bool List.list"),[# $ # $ Const #]),
   774    (Free ("rs_","bool List.list"),[# $ # $ (# $ #)])]),
   775  (3,
   776   [(Free ("fix_","bool List.list"),[Const (#,#),Free (#,#)]),
   777    (Free ("m_","bool"),[Free (#,#)]),
   778    (Free ("vs_","bool List.list"),[# $ # $ Const #])])] : envv *)
   779 
   780 (*for _output_ of the items of a Model*)
   781 datatype item = 
   782     Correct of cterm' (*labels a correct formula (type cterm')*)
   783   | SyntaxE of string (**)
   784   | TypeE   of string (**)
   785   | False   of cterm' (*WN050618 notexistent in itm_: only used in Where*)
   786   | Incompl of cterm' (**)
   787   | Superfl of string (**)
   788   | Missing of cterm';
   789 fun item2str (Correct  s) ="Correct " ^ s
   790   | item2str (SyntaxE  s) ="SyntaxE " ^ s
   791   | item2str (TypeE    s) ="TypeE " ^ s
   792   | item2str (False    s) ="False " ^ s
   793   | item2str (Incompl  s) ="Incompl " ^ s
   794   | item2str (Superfl  s) ="Superfl " ^ s
   795   | item2str (Missing  s) ="Missing " ^ s;
   796 (*make string for error-msgs*)
   797 fun itm_2str_ ctxt (Cor ((d,ts), penv)) = 
   798     "Cor " ^ term_to_string'  ctxt (comp_dts (d,ts)) ^ " ," ^ pen2str ctxt penv
   799   | itm_2str_ ctxt (Syn c) = "Syn " ^ c
   800   | itm_2str_ ctxt (Typ c) = "Typ " ^ c
   801   | itm_2str_ ctxt (Inc ((d,ts), penv)) = 
   802     "Inc " ^ term_to_string'  ctxt (comp_dts (d,ts)) ^ " ," ^ pen2str ctxt penv
   803   | itm_2str_ ctxt (Sup (d,ts)) = 
   804     "Sup " ^ term_to_string'  ctxt (comp_dts (d,ts))
   805   | itm_2str_ ctxt (Mis (d,pid)) = 
   806     "Mis "^ term_to_string'  ctxt d ^ " " ^ term_to_string'  ctxt pid
   807   | itm_2str_ ctxt (Par s) = "Trm "^s;
   808 fun itm_2str t = itm_2str_ (thy2ctxt' "Isac") t;
   809 fun itm2str_ ctxt ((i,is,b,s,itm_):itm) = 
   810     "("^(string_of_int i)^" ,"^(ints2str' is)^" ,"^(bool2str b)^" ,"^
   811     s^" ,"^(itm_2str_ ctxt itm_)^")";
   812 fun itms2str_ ctxt itms = strs2str' (map (linefeed o (itm2str_ ctxt)) itms);
   813 fun w_itms2str_ ctxt itms = tracing (itms2str_ ctxt itms);
   814 
   815 fun init_item str = SyntaxE str;
   816 
   817 
   818 
   819 
   820 type 'a ppc = 
   821     {Given : 'a list,
   822      Where: 'a list,
   823      Find  : 'a list,
   824      With : 'a list,
   825      Relate: 'a list};
   826 fun ppc2str {Given=Given,Where=Where,Find=Find,With=With,Relate=Relate}=
   827     ("{Given =" ^ (strs2str Given ) ^
   828      ",Where=" ^ (strs2str Where) ^
   829      ",Find  =" ^ (strs2str Find  ) ^
   830      ",With =" ^ (strs2str With ) ^
   831      ",Relate=" ^ (strs2str Relate) ^ "}");
   832 
   833 
   834 
   835 
   836 fun item_ppc ({Given = gi,Where= wh,
   837 		 Find = fi,With = wi,Relate= re}: string ppc) =
   838   {Given = map init_item gi,Where= map init_item wh,
   839    Find = map init_item fi,With = map init_item wi,
   840    Relate= map init_item re}:item ppc;
   841 fun itemppc2str ({Given=Given,Where=Where,
   842 		 Find=Find,With=With,Relate=Relate}:item ppc)=
   843     ("{Given =" ^ ((strs2str' o (map item2str))	 Given ) ^
   844      ",Where=" ^ ((strs2str' o (map item2str))	 Where) ^
   845      ",Find  =" ^ ((strs2str' o (map item2str))	 Find  ) ^
   846      ",With =" ^ ((strs2str' o (map item2str))	 With ) ^
   847      ",Relate=" ^ ((strs2str' o (map item2str))	 Relate) ^ "}");
   848 
   849 fun de_item (Correct x) = x
   850   | de_item (SyntaxE x) = x
   851   | de_item (TypeE   x) = x
   852   | de_item (False   x) = x
   853   | de_item (Incompl x) = x
   854   | de_item (Superfl x) = x
   855   | de_item (Missing x) = x;
   856 val empty_ppc ={Given = [],
   857 		Where= [],
   858 		Find  = [], 
   859 		With = [],
   860 		Relate= []}:item ppc;
   861 val empty_ppc_ct' ={Given = [],
   862 		Where = [],
   863 		Find  = [], 
   864 		With  = [],
   865 		Relate= []}:cterm' ppc;
   866 
   867 
   868 datatype match = 
   869   Matches of pblID * item ppc
   870 | NoMatch of pblID * item ppc;
   871 fun match2str (Matches (pI, ppc)) = 
   872     "Matches ("^(strs2str pI)^", "^(itemppc2str ppc)^")"
   873   | match2str(NoMatch (pI, ppc)) = 
   874     "NoMatch ("^(strs2str pI)^", "^(itemppc2str ppc)^")";
   875 fun matchs2str ms = (strs2str o (map match2str)) ms;
   876 fun pblID_of_match (Matches (pI,_)) = pI
   877   | pblID_of_match (NoMatch (pI,_)) = pI;
   878 
   879 (*10.03 for Refine_Problem*)
   880 datatype match_ = 
   881   Match_ of pblID * ((itm list) * ((bool * term) list))
   882 | NoMatch_;
   883 
   884 (*. the refined pbt is the last_element Matches in the list .*)
   885 fun is_matches (Matches _) = true
   886   | is_matches _ = false;
   887 fun matches_pblID (Matches (pI,_)) = pI;
   888 fun refined ms = ((matches_pblID o the o (find_first is_matches) o rev) ms)
   889     handle _ => []:pblID;
   890 fun refined_IDitms ms = ((find_first is_matches) o rev) ms;
   891 
   892 (*. the refined pbt is the last_element Matches in the list,
   893     for Refine_Problem, tryrefine .*)
   894 fun is_matches_ (Match_ _) = true
   895   | is_matches_ _ = false;
   896 fun refined_ ms = ((find_first is_matches_) o rev) ms;
   897 
   898 
   899 fun ts_in (Cor ((_,ts),_)) = ts
   900   | ts_in (Syn  (c)) = []
   901   | ts_in (Typ  (c)) = []
   902   | ts_in (Inc ((_,ts),_)) = ts
   903   | ts_in (Sup (_,ts)) = ts
   904   | ts_in (Mis _) = [];
   905 (*WN050629 unused*)
   906 fun all_ts_in itm_s = (flat o (map ts_in)) itm_s;
   907 val unique = (term_of o the o (parse (Thy_Info.get_theory "Real"))) "UnIqE_tErM";
   908 fun d_in (Cor ((d,_),_)) = d
   909   | d_in (Syn  (c)) = (tracing("*** d_in: Syn ("^c^")"); unique)
   910   | d_in (Typ  (c)) = (tracing("*** d_in: Typ ("^c^")"); unique)
   911   | d_in (Inc ((d,_),_)) = d
   912   | d_in (Sup (d,_)) = d
   913   | d_in (Mis (d,_)) = d;
   914 
   915 fun dts2str (d,ts) = pair2str (term2str d, terms2str ts);
   916 fun penvval_in (Cor ((d,_),(_,ts))) = [comp_ts (d,ts)]
   917   | penvval_in (Syn  (c)) = (tracing("*** penvval_in: Syn ("^c^")"); [])
   918   | penvval_in (Typ  (c)) = (tracing("*** penvval_in: Typ ("^c^")"); [])
   919   | penvval_in (Inc (_,(_,ts))) = ts
   920   | penvval_in (Sup dts) = (tracing("*** penvval_in: Sup "^(dts2str dts)); [])
   921   | penvval_in (Mis (d,t)) = (tracing("*** penvval_in: Mis "^
   922 				      (pair2str(term2str d, term2str t))); []);
   923 
   924 
   925 (*. check a predicate labelled with indication of incomplete substitution;
   926 rls ->    (*for eval_true*)
   927 bool * 	  (*have _all_ variables(Free) from the model-pattern 
   928             been substituted by a value from the pattern's environment ?*)
   929 term (*the precondition*)
   930 ->
   931 bool * 	  (*has the precondition evaluated to true*)
   932 term (*the precondition (for map)*)
   933 .*)
   934 fun evalprecond prls (false, pre) = 
   935   (*NOT ALL Free's have been substituted, eg. because of incomplete model*)
   936     (false, pre)
   937   | evalprecond prls (true, pre) =
   938 (* val (prls, pre) = (prls, hd pres');
   939    val (prls, pre) = (prls, hd (tl pres'));
   940    *)
   941     if eval_true (assoc_thy "Isac") (*for Pattern.match   *)
   942 		 [pre] prls             (*pre parsed, prls.thy*)
   943     then (true , pre)
   944     else (false , pre);
   945 
   946 fun pre2str (b, t) = pair2str(bool2str b, term2str t);
   947 fun pres2str pres = strs2str' (map (linefeed o pre2str) pres);
   948 
   949 (* check preconditions, return true if all true *)
   950 fun check_preconds' _ [] _ _ = []  (*empty preconditions are true*)
   951   | check_preconds' prls pres pbl _(*FIXME.WN0308 mvat re-introduce*) =
   952     let val env = mk_env pbl;
   953         val pres' = map (subst_atomic_all env) pres;
   954     in map (evalprecond prls) pres' end;
   955 
   956 fun check_preconds thy prls pres pbl = 
   957     check_preconds' prls pres pbl (max_vt pbl);
   958 
   959 fun declare_constraints' ts ctxt =
   960       fold Variable.declare_constraints (flat (map vars ts)) ctxt;
   961 (*WN110613 fun declare_constraints' shall replace fun declare_constraints*)
   962 fun declare_constraints t ctxt =
   963     let
   964       fun get_vars ((v,T)::vs) = (case raw_explode v |> Library.read_int of
   965               (_, _::_) => (Free (v,T)::get_vars vs)
   966             | (_, []  ) => get_vars vs) (*filter out nums as long as 
   967                                           we have Free ("123",_)*)
   968         | get_vars [] = [];
   969       val ts = Term.add_frees (Syntax.read_term ctxt t) [] |> get_vars;
   970     in fold Variable.declare_constraints ts ctxt end;
   971 
   972 structure Context_Data = Proof_Data
   973   (type T = term list
   974    fun init _ = []);
   975 fun get_assumptions ctxt = Context_Data.get ctxt
   976 fun insert_assumptions asms = Context_Data.map (fn xs => distinct (asms @ xs))
   977 
   978 (* transfer assumptions from one to another ctxt.
   979    does NOT respect scope: in a calculation identifiers are unique.
   980    but environments are scoped as usual in Luacs-interpretation.
   981    WN110520 redo (1) take declare_constraints (2) with combinators*)
   982 fun transfer_asms_from_to from_ctxt to_ctxt =
   983   let
   984     val to_vars = get_assumptions to_ctxt |> map vars |> flat
   985     fun transfer [] to_ctxt = to_ctxt
   986         | transfer (from_asm::fas) to_ctxt =
   987             if inter op = (vars from_asm) to_vars = []
   988             then transfer fas to_ctxt
   989             else transfer fas (insert_assumptions [from_asm] to_ctxt)
   990   in transfer (get_assumptions from_ctxt) to_ctxt end
   991 
   992 (* exported from a subproblem to the context of the calling method:
   993    # 'scrval': the result of script interpretation and
   994    # those assumptions in the subproblem wich contain a variable known
   995      in the calling method. *)
   996 fun from_subpbl_to_caller sub_ctxt scrval caller_ctxt =
   997   let val caller_ctxt = (scrval |> dest_list' |> insert_assumptions) caller_ctxt
   998   in transfer_asms_from_to sub_ctxt caller_ctxt end;
   999 
  1000 (*----------------------------------------------------------*)
  1001 end
  1002 open SpecifyTools;
  1003 (*----------------------------------------------------------*)
  1004