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