src/Tools/isac/Interpret/calchead.sml
author Walther Neuper <neuper@ist.tugraz.at>
Tue, 28 Sep 2010 09:06:56 +0200
branchisac-update-Isa09-2
changeset 38031 460c24a6a6ba
parent 38015 67ba02dffacc
child 38050 4c52ad406c20
permissions -rw-r--r--
tuned error and writeln

# raise error --> error
# writeln in atomtyp, atomty, atomt and xmlsrc
neuper@37906
     1
(* Specify-phase: specifying and modeling a problem or a subproblem. The
neuper@37906
     2
   most important types are declared in mstools.sml.
neuper@37906
     3
   author: Walther Neuper
neuper@37906
     4
   991122
neuper@37906
     5
   (c) due to copyright terms
neuper@37906
     6
neuper@37906
     7
use"ME/calchead.sml";
neuper@37906
     8
use"calchead.sml";
neuper@37934
     9
12345678901234567890123456789012345678901234567890123456789012345678901234567890
neuper@37934
    10
        10        20        30        40        50        60        70        80
neuper@37906
    11
*)
neuper@37906
    12
neuper@37906
    13
(* TODO interne Funktionen aus sig entfernen *)
neuper@37906
    14
signature CALC_HEAD =
neuper@37906
    15
  sig
neuper@37906
    16
    datatype additm = Add of SpecifyTools.itm | Err of string
neuper@37906
    17
    val all_dsc_in : SpecifyTools.itm_ list -> Term.term list
neuper@37906
    18
    val all_modspec : ptree * pos' -> ptree * pos'
neuper@37906
    19
    datatype appl = Appl of tac_ | Notappl of string
neuper@37906
    20
    val appl_add :
neuper@37931
    21
       theory ->
neuper@37906
    22
       string ->
neuper@37906
    23
       SpecifyTools.ori list ->
neuper@37906
    24
       SpecifyTools.itm list ->
neuper@37906
    25
       (string * (Term.term * Term.term)) list -> cterm' -> additm
neuper@37906
    26
    type calcstate
neuper@37906
    27
    type calcstate'
neuper@37934
    28
    val chk_vars : term ppc -> string * Term.term list
neuper@37906
    29
    val chktyp :
neuper@37934
    30
       theory -> int * term list * term list -> term
neuper@37906
    31
    val chktyps :
neuper@37934
    32
       theory -> term list * term list -> term list
neuper@37906
    33
    val complete_metitms :
neuper@37906
    34
   SpecifyTools.ori list ->
neuper@37906
    35
   SpecifyTools.itm list ->
neuper@37906
    36
   SpecifyTools.itm list -> pat list -> SpecifyTools.itm list
neuper@37906
    37
    val complete_mod_ : ori list * pat list * pat list * itm list ->
neuper@37906
    38
			itm list * itm list
neuper@37906
    39
    val complete_mod : ptree * pos' -> ptree * (pos * pos_)
neuper@37906
    40
    val complete_spec : ptree * pos' -> ptree * pos'
neuper@37906
    41
    val cpy_nam :
neuper@37906
    42
       pat list -> preori list -> pat -> preori
neuper@37906
    43
    val e_calcstate : calcstate
neuper@37906
    44
    val e_calcstate' : calcstate'
neuper@37906
    45
    val eq1 : ''a -> 'b * (''a * 'c) -> bool
neuper@37906
    46
    val eq3 :
neuper@37906
    47
       ''a -> Term.term -> 'b * 'c * 'd * ''a * SpecifyTools.itm_ -> bool
neuper@37906
    48
    val eq4 : ''a -> 'b * ''a list * 'c * 'd * 'e -> bool
neuper@37906
    49
    val eq5 :
neuper@37906
    50
       'a * 'b * 'c * 'd * SpecifyTools.itm_ ->
neuper@37906
    51
       'e * 'f * 'g * Term.term * 'h -> bool
neuper@37906
    52
    val eq_dsc : SpecifyTools.itm * SpecifyTools.itm -> bool
neuper@37906
    53
    val eq_pos' : ''a * pos_ -> ''a * pos_ -> bool
neuper@37931
    54
    val f_mout : theory -> mout -> Term.term
neuper@37906
    55
    val filter_outs :
neuper@37906
    56
       SpecifyTools.ori list ->
neuper@37906
    57
       SpecifyTools.itm list -> SpecifyTools.ori list
neuper@37906
    58
    val filter_pbt :
neuper@37906
    59
       SpecifyTools.ori list ->
neuper@37906
    60
       ('a * (Term.term * 'b)) list -> SpecifyTools.ori list
neuper@37906
    61
    val foldl1 : ('a * 'a -> 'a) -> 'a list -> 'a
neuper@37906
    62
    val foldr1 : ('a * 'a -> 'a) -> 'a list -> 'a
neuper@37906
    63
    val form : 'a -> ptree -> (string * ('a * pos_) * Term.term) list
neuper@37906
    64
    val formres : 'a -> ptree -> (string * ('a * pos_) * Term.term) list
neuper@37906
    65
    val gen_ins' : ('a * 'a -> bool) -> 'a * 'a list -> 'a list
neuper@37906
    66
    val get_formress :
neuper@37906
    67
       (string * (pos * pos_) * Term.term) list list ->
neuper@37906
    68
       pos -> ptree list -> (string * (pos * pos_) * Term.term) list
neuper@37906
    69
    val get_forms :
neuper@37906
    70
       (string * (pos * pos_) * Term.term) list list ->
neuper@37906
    71
       posel list -> ptree list -> (string * (pos * pos_) * Term.term) list
neuper@37906
    72
    val get_interval : pos' -> pos' -> int -> ptree -> (pos' * term) list
neuper@37906
    73
    val get_ocalhd : ptree * pos' -> ocalhd
neuper@37906
    74
    val get_spec_form : tac_ -> pos' -> ptree -> mout
neuper@37906
    75
    val geti_ct :
neuper@37931
    76
       theory ->
neuper@37906
    77
       SpecifyTools.ori -> SpecifyTools.itm -> string * cterm'
neuper@37931
    78
    val getr_ct : theory -> SpecifyTools.ori -> string * cterm'
neuper@37906
    79
    val has_list_type : Term.term -> bool
neuper@37906
    80
    val header : pos_ -> pblID -> metID -> pblmet
neuper@37906
    81
    val insert_ppc :
neuper@37931
    82
       theory ->
neuper@37906
    83
       int * SpecifyTools.vats * bool * string * SpecifyTools.itm_ ->
neuper@37906
    84
       SpecifyTools.itm list -> SpecifyTools.itm list
neuper@37906
    85
    val insert_ppc' :
neuper@37906
    86
       SpecifyTools.itm -> SpecifyTools.itm list -> SpecifyTools.itm list
neuper@37906
    87
    val is_complete_mod : ptree * pos' -> bool
neuper@37906
    88
    val is_complete_mod_ : SpecifyTools.itm list -> bool
neuper@37906
    89
    val is_complete_modspec : ptree * pos' -> bool
neuper@37906
    90
    val is_complete_spec : ptree * pos' -> bool
neuper@37906
    91
    val is_copy_named : 'a * ('b * Term.term) -> bool
neuper@37906
    92
    val is_copy_named_idstr : string -> bool
neuper@37906
    93
    val is_error : SpecifyTools.itm_ -> bool
neuper@37906
    94
    val is_field_correct : ''a -> ''b -> (''a * ''b list) list -> bool
neuper@37906
    95
    val is_known :
neuper@37931
    96
       theory ->
neuper@37906
    97
       string ->
neuper@37906
    98
       SpecifyTools.ori list ->
neuper@37906
    99
       Term.term -> string * SpecifyTools.ori * Term.term list
neuper@37906
   100
    val is_list_type : Term.typ -> bool
neuper@37906
   101
    val is_notyet_input :
neuper@37931
   102
       theory ->
neuper@37906
   103
       SpecifyTools.itm list ->
neuper@37906
   104
       Term.term list ->
neuper@37906
   105
       SpecifyTools.ori ->
neuper@37906
   106
       ('a * (Term.term * Term.term)) list -> string * SpecifyTools.itm
neuper@37906
   107
    val is_parsed : SpecifyTools.itm_ -> bool
neuper@37906
   108
    val is_untouched : SpecifyTools.itm -> bool
neuper@37906
   109
    val matc :
neuper@37931
   110
       theory ->
neuper@37906
   111
       pat list ->
neuper@37906
   112
       Term.term list ->
neuper@37906
   113
       (int list * string * Term.term * Term.term list) list ->
neuper@37906
   114
       (int list * string * Term.term * Term.term list) list
neuper@37906
   115
    val match_ags :
neuper@37931
   116
       theory -> pat list -> Term.term list -> SpecifyTools.ori list
neuper@37906
   117
    val maxl : int list -> int
neuper@37906
   118
    val match_ags_msg : string list -> Term.term -> Term.term list -> unit
neuper@37906
   119
    val memI : ''a list -> ''a -> bool
neuper@37906
   120
    val mk_additem : string -> cterm' -> tac
neuper@37931
   121
    val mk_delete : theory -> string -> SpecifyTools.itm_ -> tac
neuper@37906
   122
    val mtc :
neuper@37931
   123
       theory -> pat -> Term.term -> SpecifyTools.preori option
neuper@37906
   124
    val nxt_add :
neuper@37931
   125
       theory ->
neuper@37906
   126
       SpecifyTools.ori list ->
neuper@37906
   127
       (string * (Term.term * 'a)) list ->
neuper@37931
   128
       SpecifyTools.itm list -> (string * cterm') option
neuper@37906
   129
    val nxt_model_pbl : tac_ -> ptree * (int list * pos_) -> tac_
neuper@37906
   130
    val nxt_spec :
neuper@37906
   131
       pos_ ->
neuper@37906
   132
       bool ->
neuper@37906
   133
       SpecifyTools.ori list ->
neuper@37906
   134
       spec ->
neuper@37906
   135
       SpecifyTools.itm list * SpecifyTools.itm list ->
neuper@37906
   136
       (string * (Term.term * 'a)) list * (string * (Term.term * 'b)) list ->
neuper@37906
   137
       spec -> pos_ * tac
neuper@37906
   138
    val nxt_specif : tac -> ptree * (int list * pos_) -> calcstate'
neuper@37906
   139
    val nxt_specif_additem :
neuper@37906
   140
       string -> cterm' -> ptree * (int list * pos_) -> calcstate'
neuper@37906
   141
    val nxt_specify_init_calc : fmz -> calcstate
neuper@37906
   142
    val ocalhd_complete :
neuper@37906
   143
       SpecifyTools.itm list ->
neuper@37906
   144
       (bool * Term.term) list -> domID * pblID * metID -> bool
neuper@37906
   145
    val ori2Coritm :
neuper@37906
   146
	pat list -> ori -> itm
neuper@37906
   147
    val ori_2itm :
neuper@37906
   148
       'a ->
neuper@37906
   149
       SpecifyTools.itm_ ->
neuper@37906
   150
       Term.term -> Term.term list -> SpecifyTools.ori -> SpecifyTools.itm
neuper@37906
   151
    val overwrite_ppc :
neuper@37931
   152
       theory ->
neuper@37906
   153
       int * SpecifyTools.vats * bool * string * SpecifyTools.itm_ ->
neuper@37906
   154
       SpecifyTools.itm list ->
neuper@37906
   155
       (int * SpecifyTools.vats * bool * string * SpecifyTools.itm_) list
neuper@37906
   156
    val parse_ok : SpecifyTools.itm_ list -> bool
neuper@37906
   157
    val posform2str : pos' * ptform -> string
neuper@37906
   158
    val posforms2str : (pos' * ptform) list -> string
neuper@37906
   159
    val posterms2str : (pos' * term) list -> string (*tests only*)
neuper@37906
   160
    val ppc135list : 'a SpecifyTools.ppc -> 'a list
neuper@37906
   161
    val ppc2list : 'a SpecifyTools.ppc -> 'a list
neuper@37906
   162
    val pt_extract :
neuper@37906
   163
       ptree * (int list * pos_) ->
neuper@37931
   164
       ptform * tac option * Term.term list
neuper@37906
   165
    val pt_form : ppobj -> ptform
neuper@37906
   166
    val pt_model : ppobj -> pos_ -> ptform
neuper@37906
   167
    val reset_calchead : ptree * pos' -> ptree * pos'
neuper@37906
   168
    val seek_oridts :
neuper@37931
   169
       theory ->
neuper@37906
   170
       string ->
neuper@37906
   171
       Term.term * Term.term list ->
neuper@37906
   172
       (int * SpecifyTools.vats * string * Term.term * Term.term list) list
neuper@37906
   173
       -> string * SpecifyTools.ori * Term.term list
neuper@37906
   174
    val seek_orits :
neuper@37931
   175
       theory ->
neuper@37906
   176
       string ->
neuper@37906
   177
       Term.term list ->
neuper@37906
   178
       (int * SpecifyTools.vats * string * Term.term * Term.term list) list
neuper@37906
   179
       -> string * SpecifyTools.ori * Term.term list
neuper@37906
   180
    val seek_ppc :
neuper@37931
   181
       int -> SpecifyTools.itm list -> SpecifyTools.itm option
neuper@37906
   182
    val show_pt : ptree -> unit
neuper@37906
   183
    val some_spec : spec -> spec -> spec
neuper@37906
   184
    val specify :
neuper@37906
   185
       tac_ ->
neuper@37906
   186
       pos' ->
neuper@37906
   187
       cid ->
neuper@37906
   188
       ptree ->
neuper@37906
   189
       (posel list * pos_) * ((posel list * pos_) * istate) * mout * tac *
neuper@37906
   190
       safe * ptree
neuper@37906
   191
    val specify_additem :
neuper@37906
   192
       string ->
neuper@37906
   193
       cterm' * 'a ->
neuper@37906
   194
       int list * pos_ ->
neuper@37906
   195
       'b ->
neuper@37906
   196
       ptree ->
neuper@37906
   197
       (pos * pos_) * ((pos * pos_) * istate) * mout * tac * safe * ptree
neuper@37934
   198
    val tag_form : theory -> term * term -> term
neuper@37931
   199
    val test_types : theory -> Term.term * Term.term list -> string
neuper@37906
   200
    val typeless : Term.term -> Term.term
neuper@37934
   201
    val unbound_ppc : term SpecifyTools.ppc -> Term.term list
neuper@37906
   202
    val vals_of_oris : SpecifyTools.ori list -> Term.term list
neuper@37906
   203
    val variants_in : Term.term list -> int
neuper@37906
   204
    val vars_of_pbl_ : ('a * ('b * Term.term)) list -> Term.term list
neuper@37906
   205
    val vars_of_pbl_' : ('a * ('b * Term.term)) list -> Term.term list
neuper@37906
   206
  end
neuper@37906
   207
 
neuper@37906
   208
neuper@37906
   209
neuper@37906
   210
neuper@37906
   211
neuper@37906
   212
(*---------------------------------------------------------------------*)
neuper@37906
   213
structure CalcHead (**): CALC_HEAD(**) =
neuper@37906
   214
neuper@37906
   215
struct
neuper@37906
   216
(*---------------------------------------------------------------------*)
neuper@37906
   217
neuper@37906
   218
(* datatypes *)
neuper@37906
   219
neuper@37906
   220
(*.the state wich is stored after each step of calculation; it contains
neuper@37906
   221
   the calc-state and a list of [tac,istate](="tacis") to be applied.
neuper@37906
   222
   the last_elem tacis is the first to apply to the calc-state and
neuper@37906
   223
   the (only) one shown to the front-end as the 'proposed tac'.
neuper@37906
   224
   the calc-state resulting from the application of tacis is not stored,
neuper@37906
   225
   because the tacis hold enought information for efficiently rebuilding
neuper@37906
   226
   this state just by "fun generate ".*)
neuper@37906
   227
type calcstate = 
neuper@37906
   228
     (ptree * pos') *    (*the calc-state to which the tacis could be applied*)
neuper@37906
   229
     (taci list);        (*ev. several (hidden) steps; 
neuper@37906
   230
                           in REVERSE order: first tac_ to apply is last_elem*)
neuper@37906
   231
val e_calcstate = ((EmptyPtree, e_pos'), [e_taci]):calcstate;
neuper@37906
   232
neuper@37906
   233
(*the state used during one calculation within the mathengine; it contains
neuper@37906
   234
  a list of [tac,istate](="tacis") which generated the the calc-state;
neuper@37906
   235
  while this state's tacis are extended by each (internal) step,
neuper@37906
   236
  the calc-state is used for creating new nodes in the calc-tree
neuper@37906
   237
  (eg. applicable_in requires several particular nodes of the calc-tree)
neuper@37906
   238
  and then replaced by the the newly created;
neuper@37906
   239
  on leave of the mathengine the resuing calc-state is dropped anyway,
neuper@37906
   240
  because the tacis hold enought information for efficiently rebuilding
neuper@37906
   241
  this state just by "fun generate ".*)
neuper@37906
   242
type calcstate' = 
neuper@37906
   243
     taci list *        (*cas. several (hidden) steps; 
neuper@37906
   244
                          in REVERSE order: first tac_ to apply is last_elem*)
neuper@37906
   245
     pos' list *        (*a "continuous" sequence of pos',
neuper@37906
   246
			 deleted by application of taci list*)     
neuper@37906
   247
     (ptree * pos');    (*the calc-state resulting from the application of tacis*)
neuper@37906
   248
val e_calcstate' = ([e_taci], [e_pos'], (EmptyPtree, e_pos')):calcstate';
neuper@37906
   249
neuper@37906
   250
(*FIXXXME.WN020430 intermediate hack for fun ass_up*)
neuper@37906
   251
fun f_mout thy (Form' (FormKF (_,_,_,_,f))) = (term_of o the o (parse thy)) f
neuper@38031
   252
  | f_mout thy _ = error "f_mout: not called with formula";
neuper@37906
   253
neuper@37906
   254
neuper@37906
   255
(*.is the calchead complete ?.*)
neuper@37906
   256
fun ocalhd_complete (its: itm list) (pre: (bool * term) list) (dI,pI,mI) = 
neuper@37906
   257
    foldl and_ (true, map #3 its) andalso 
neuper@37906
   258
    foldl and_ (true, map #1 pre) andalso 
neuper@37906
   259
    dI<>e_domID andalso pI<>e_pblID andalso mI<>e_metID;
neuper@37906
   260
neuper@37906
   261
neuper@37906
   262
(* make a term 'typeless' for comparing with another 'typeless' term;
neuper@37906
   263
   'type-less' usually is illtyped                                  *)
neuper@37906
   264
fun typeless (Const(s,_)) = (Const(s,e_type)) 
neuper@37906
   265
  | typeless (Free(s,_)) = (Free(s,e_type))
neuper@37906
   266
  | typeless (Var(n,_)) = (Var(n,e_type))
neuper@37906
   267
  | typeless (Bound i) = (Bound i)
neuper@37906
   268
  | typeless (Abs(s,_,t)) = Abs(s,e_type, typeless t)
neuper@37906
   269
  | typeless (t1 $ t2) = (typeless t1) $ (typeless t2);
neuper@37906
   270
(*
neuper@37926
   271
> val (SOME ct) = parse thy "max_relation (A=#2*a*b - a^^^#2)";
neuper@37906
   272
> val (_,t1) = split_dsc_t hs (term_of ct);
neuper@37926
   273
> val (SOME ct) = parse thy "A=#2*a*b - a^^^#2";
neuper@37906
   274
> val (_,t2) = split_dsc_t hs (term_of ct);
neuper@37906
   275
> typeless t1 = typeless t2;
neuper@37906
   276
val it = true : bool
neuper@37906
   277
*)
neuper@37906
   278
neuper@37906
   279
neuper@37906
   280
neuper@37906
   281
(*.to an input (d,ts) find the according ori and insert the ts.*)
neuper@37906
   282
(*WN.11.03: + dont take first inter<>[]*)
neuper@37906
   283
fun seek_oridts thy sel (d,ts) [] = 
neuper@37934
   284
  ("'"^(Syntax.string_of_term (thy2ctxt thy) (comp_dts thy (d,ts)))^
neuper@37906
   285
   "' not found (typed)", (0,[],sel,d,ts):ori, [])
neuper@37906
   286
  (* val (id,vat,sel',d',ts')::oris = ori;
neuper@37906
   287
     val (id,vat,sel',d',ts') = ori;
neuper@37906
   288
     *)
neuper@37906
   289
  | seek_oridts thy sel (d,ts) ((id,vat,sel',d',ts')::(oris:ori list)) =
neuper@37934
   290
    if sel = sel' andalso d=d' andalso (inter op = ts ts') <> [] 
neuper@37934
   291
    then if sel = sel' 
neuper@37934
   292
	 then ("", 
neuper@37934
   293
               (id,vat,sel,d, inter op = ts ts'):ori, 
neuper@37934
   294
               ts')
neuper@37934
   295
	 else ((Syntax.string_of_term (thy2ctxt thy) (comp_dts thy (d,ts))) 
neuper@37934
   296
               ^ " not for " ^ sel, 
neuper@37934
   297
               e_ori_, 
neuper@37934
   298
               [])
neuper@37906
   299
    else seek_oridts thy sel (d,ts) oris;
neuper@37906
   300
neuper@37906
   301
(*.to an input (_,ts) find the according ori and insert the ts.*)
neuper@37906
   302
fun seek_orits thy sel ts [] = 
neuper@37906
   303
  ("'"^
neuper@37934
   304
   (strs2str (map (Syntax.string_of_term (thy2ctxt thy)) ts))^
neuper@37906
   305
   "' not found (typed)", e_ori_, [])
neuper@37906
   306
  | seek_orits thy sel ts ((id,vat,sel',d,ts')::(oris:ori list)) =
neuper@37934
   307
    if sel = sel' andalso (inter op = ts ts') <> [] 
neuper@37906
   308
      then if sel = sel' 
neuper@37934
   309
	   then ("",
neuper@37934
   310
                 (id,vat,sel,d, inter op = ts ts'):ori, 
neuper@37934
   311
                 ts')
neuper@37934
   312
	   else (((strs2str' o map (Syntax.string_of_term (thy2ctxt thy))) ts)
neuper@37934
   313
                 ^ " not for "^sel, 
neuper@37934
   314
                 e_ori_, 
neuper@37934
   315
                 [])
neuper@37906
   316
    else seek_orits thy sel ts oris;
neuper@37906
   317
(* false
neuper@37906
   318
> val ((id,vat,sel',d,ts')::(ori':ori)) = ori;
neuper@37906
   319
> seek_orits thy sel ts [(id,vat,sel',d,ts')];
neuper@37906
   320
uncaught exception TYPE
neuper@37906
   321
> seek_orits thy sel ts [];
neuper@37906
   322
uncaught exception TYPE
neuper@37906
   323
*)
neuper@37906
   324
neuper@37906
   325
(*find_first item with #1 equal to id*)
neuper@37926
   326
fun seek_ppc id [] = NONE
neuper@37906
   327
  | seek_ppc id (p::(ppc:itm list)) =
neuper@37926
   328
    if id = #1 p then SOME p else seek_ppc id ppc;
neuper@37906
   329
neuper@37906
   330
neuper@37906
   331
neuper@37906
   332
(*---------------------------------------------(3) nach ptyps.sml 23.3.02*)
neuper@37906
   333
neuper@37906
   334
neuper@37906
   335
datatype appl = Appl of tac_ | Notappl of string;
neuper@37906
   336
neuper@37906
   337
fun ppc2list ({Given=gis,Where=whs,Find=fis,
neuper@37906
   338
	       With=wis,Relate=res}: 'a ppc) =
neuper@37906
   339
  gis @ whs @ fis @ wis @ res;
neuper@37906
   340
fun ppc135list ({Given=gis,Find=fis,Relate=res,...}: 'a ppc) =
neuper@37906
   341
  gis @ fis @ res;
neuper@37906
   342
neuper@37906
   343
neuper@37906
   344
neuper@37906
   345
neuper@37906
   346
(* get the number of variants in a problem in 'original',
neuper@37906
   347
   assumes equal descriptions in immediate sequence    *)
neuper@37906
   348
fun variants_in ts =
neuper@37906
   349
  let fun eq(x,y) = head_of x = head_of y;
neuper@37906
   350
    fun cnt eq [] y n = ([n],[])
neuper@37906
   351
      | cnt eq (x::xs) y n = if eq(x,y) then cnt eq xs y (n+1)
neuper@37906
   352
			     else ([n], x::xs);
neuper@37906
   353
    fun coll eq  xs [] = xs
neuper@37906
   354
      | coll eq  xs (y::ys) = 
neuper@37906
   355
      let val (n,ys') = cnt eq (y::ys) y 0;
neuper@37906
   356
      in if ys' = [] then xs @ n else coll eq  (xs @ n) ys' end;
neuper@37934
   357
    val vts = subtract op = [1] (distinct (coll eq [] ts));
neuper@37906
   358
  in case vts of [] => 1 | [n] => n
neuper@37906
   359
      | _ => error "different variants in formalization" end;
neuper@37906
   360
(*
neuper@37906
   361
> cnt (op=) [2,2,2,4,5,5,5,5,5] 2 0;
neuper@37906
   362
val it = ([3],[4,5,5,5,5,5]) : int list * int list
neuper@37906
   363
> coll (op=) [] [1,2,2,2,4,5,5,5,5,5];
neuper@37906
   364
val it = [1,3,1,5] : int list
neuper@37906
   365
*)
neuper@37906
   366
neuper@37906
   367
fun is_list_type (Type("List.list",_)) = true
neuper@37906
   368
  | is_list_type _ = false;
neuper@37906
   369
(* fun destr (Type(str,sort)) = (str,sort);
neuper@37926
   370
> val (SOME ct) = parse thy "lll::real list";
neuper@37906
   371
> val ty = (#T o rep_cterm) ct;
neuper@37906
   372
> is_list_type ty;
neuper@37906
   373
val it = true : bool 
neuper@37906
   374
> destr ty;
neuper@37906
   375
val it = ("List.list",["RealDef.real"]) : string * typ list
neuper@37906
   376
> atomty ((#t o rep_cterm) ct);
neuper@37906
   377
*** -------------
neuper@37906
   378
*** Free ( lll, real list)
neuper@37906
   379
val it = () : unit
neuper@37906
   380
 
neuper@37926
   381
> val (SOME ct) = parse thy "[lll::real]";
neuper@37906
   382
> val ty = (#T o rep_cterm) ct;
neuper@37906
   383
> is_list_type ty;
neuper@37906
   384
val it = true : bool 
neuper@37906
   385
> destr ty;
neuper@37906
   386
val it = ("List.list",["'a"]) : string * typ list
neuper@37906
   387
> atomty ((#t o rep_cterm) ct);
neuper@37906
   388
*** -------------
neuper@37906
   389
*** Const ( List.list.Cons, [real, real list] => real list)
neuper@37906
   390
***   Free ( lll, real)
neuper@37906
   391
***   Const ( List.list.Nil, real list) 
neuper@37906
   392
neuper@37926
   393
> val (SOME ct) = parse thy "lll";
neuper@37906
   394
> val ty = (#T o rep_cterm) ct;
neuper@37906
   395
> is_list_type ty;
neuper@37906
   396
val it = false : bool  *)
neuper@37906
   397
neuper@37906
   398
neuper@37906
   399
fun has_list_type (Free(_,T)) = is_list_type T
neuper@37906
   400
  | has_list_type _ = false;
neuper@37906
   401
(*
neuper@37926
   402
> val (SOME ct) = parse thy "lll::real list";
neuper@37906
   403
> has_list_type (term_of ct);
neuper@37906
   404
val it = true : bool
neuper@37926
   405
> val (SOME ct) = parse thy "[lll::real]";
neuper@37906
   406
> has_list_type (term_of ct);
neuper@37906
   407
val it = false : bool *)
neuper@37906
   408
neuper@37906
   409
fun is_parsed (Syn _) = false
neuper@37906
   410
  | is_parsed _ = true;
neuper@37906
   411
fun parse_ok its = foldl and_ (true, map is_parsed its);
neuper@37906
   412
neuper@37906
   413
fun all_dsc_in itm_s =
neuper@37906
   414
  let    
neuper@37906
   415
    fun d_in (Cor ((d,_),_)) = [d]
neuper@37906
   416
      | d_in (Syn c) = []
neuper@37906
   417
      | d_in (Typ c) = []
neuper@37906
   418
      | d_in (Inc ((d,_),_)) = [d]
neuper@37906
   419
      | d_in (Sup (d,_)) = [d]
neuper@37906
   420
      | d_in (Mis (d,_)) = [d];
neuper@37906
   421
  in (flat o (map d_in)) itm_s end;  
neuper@37906
   422
neuper@37906
   423
(* 30.1.00 ---
neuper@37906
   424
fun is_Syn (Syn _) = true
neuper@37906
   425
  | is_Syn (Typ _) = true
neuper@37906
   426
  | is_Syn _ = false;
neuper@37906
   427
 --- *)
neuper@37906
   428
fun is_error (Cor (_,ts)) = false
neuper@37906
   429
  | is_error (Sup (_,ts)) = false
neuper@37906
   430
  | is_error (Inc (_,ts)) = false
neuper@37906
   431
  | is_error (Mis (_,ts)) = false
neuper@37906
   432
  | is_error _ = true;
neuper@37906
   433
neuper@37906
   434
(* 30.1.00 ---
neuper@37906
   435
fun ct_in (Syn (c)) = c
neuper@37906
   436
  | ct_in (Typ (c)) = c
neuper@38031
   437
  | ct_in _ = error "ct_in called for Cor .. Sup";
neuper@37906
   438
 --- *)
neuper@37906
   439
neuper@37906
   440
(*#############################################################*)
neuper@37906
   441
(*#############################################################*)
neuper@37906
   442
(* vvv--- aus nnewcode.sml am 30.1.00 ---vvv *)
neuper@37906
   443
neuper@37906
   444
neuper@37906
   445
(* testdaten besorgen:
neuper@37906
   446
   use"test-coil-kernel.sml";
neuper@37906
   447
   val (PblObj{origin=(oris,_,_),meth={ppc=itms,...},...}) = 
neuper@37906
   448
        get_obj I pt p;
neuper@37906
   449
  *)
neuper@37906
   450
neuper@37906
   451
(* given oris, ppc, 
neuper@37906
   452
   variant V: oris union ppc => int, id ID: oris union ppc => int
neuper@37906
   453
neuper@37906
   454
   ppc is_complete == 
neuper@37906
   455
     EX vt:V. ALL r:oris --> EX i:ppc. ID r = ID i  &  complete i
neuper@37906
   456
neuper@37906
   457
   and
neuper@37906
   458
     @vt = max sum(i : ppc) V i
neuper@37906
   459
*)
neuper@37906
   460
neuper@37906
   461
neuper@37906
   462
neuper@37906
   463
(*
neuper@37906
   464
> ((vts_cnt (vts_in itms))) itms;
neuper@37906
   465
neuper@37906
   466
neuper@37906
   467
neuper@37906
   468
---^^--test 10.3.
neuper@37906
   469
> val vts = vts_in itms;
neuper@37906
   470
val vts = [1,2,3] : int list
neuper@37906
   471
> val nvts = vts_cnt vts itms;
neuper@37906
   472
val nvts = [(1,6),(2,5),(3,7)] : (int * int) list
neuper@37906
   473
> val mx = max2 nvts;
neuper@37906
   474
val mx = (3,7) : int * int
neuper@37906
   475
> val v = max_vt itms;
neuper@37906
   476
val v = 3 : int
neuper@37906
   477
--------------------------
neuper@37906
   478
> 
neuper@37906
   479
*)
neuper@37906
   480
neuper@37906
   481
(*.get the first term in ts from ori.*)
neuper@37906
   482
(* val (_,_,fd,d,ts) = hd miss;
neuper@37906
   483
   *)
neuper@37906
   484
fun getr_ct thy ((_,_,fd,d,ts):ori) =
neuper@37934
   485
  (fd, ((Syntax.string_of_term (thy2ctxt thy)) o 
neuper@37934
   486
        (comp_dts thy)) (d,[hd ts]):cterm');
neuper@37906
   487
(* val t = comp_dts thy (d,[hd ts]);
neuper@37906
   488
   *)
neuper@37906
   489
neuper@37906
   490
(* get a term from ori, notyet input in itm *)
neuper@37906
   491
fun geti_ct thy ((_,_,_,d,ts):ori) ((_,_,_,fd,itm_):itm) =  
neuper@37934
   492
  (fd, ((Syntax.string_of_term (thy2ctxt thy)) o (comp_dts thy)) 
neuper@37934
   493
           (d, subtract op = (ts_in itm_) ts):cterm');
neuper@37906
   494
(* test-maximum.sml fmy <> [], Init_Proof ...
neuper@37906
   495
   val (_,_,_,d,ts) = ori; val (_,_,_,fd,itm_) = hd icl;
neuper@37906
   496
   val d' $ ts' = (term_of o the o (parse thy)) "fixedValues [r=Arbfix]";
neuper@37906
   497
   atomty d;
neuper@37906
   498
   atomty d';
neuper@37906
   499
   atomty (hd ts);
neuper@37906
   500
   atomty ts';
neuper@37938
   501
   cterm_of thy (d $ (hd ts));
neuper@37938
   502
   cterm_of thy (d' $ ts');
neuper@37906
   503
neuper@37906
   504
   comp_dts thy (d,ts);
neuper@37906
   505
   *)
neuper@37906
   506
neuper@37906
   507
neuper@37906
   508
(* in FE dsc, not dat: this is in itms ...*)
neuper@37906
   509
fun is_untouched ((_,_,false,_,Inc((_,[]),_)):itm) = true
neuper@37906
   510
  | is_untouched _ = false;
neuper@37906
   511
neuper@37906
   512
neuper@37906
   513
(* select an item in oris, notyet input in itms 
neuper@37906
   514
   (precondition: in itms are only Cor, Sup, Inc) *)
neuper@37936
   515
local infix mem;
neuper@37934
   516
fun x mem [] = false
neuper@37934
   517
  | x mem (y :: ys) = x = y orelse x mem ys;
neuper@37934
   518
in 
neuper@37906
   519
fun nxt_add thy ([]:ori list) pbt itms = (*root (only) ori...fmz=[]*)
neuper@37906
   520
  let
neuper@37906
   521
    fun test_d d ((i,_,_,_,itm_):itm) = (d = (d_in itm_)) andalso i<>0; 
neuper@37906
   522
    fun is_elem itms (f,(d,t)) = 
neuper@37906
   523
      case find_first (test_d d) itms of 
neuper@37926
   524
	SOME _ => true | NONE => false;
neuper@37906
   525
  in case filter_out (is_elem itms) pbt of
neuper@37906
   526
(* val ((f,(d,_))::itms) = filter_out (is_elem itms) pbt;
neuper@37906
   527
   *)
neuper@37906
   528
    (f,(d,_))::itms => 
neuper@37934
   529
      SOME (f:string, ((Syntax.string_of_term (thy2ctxt thy)) o comp_dts thy) (d,[]):cterm')
neuper@37926
   530
  | _ => NONE end
neuper@37906
   531
neuper@37906
   532
(* val (thy,itms) = (assoc_thy (if dI=e_domID then dI' else dI),pbl);
neuper@37906
   533
   *)
neuper@37906
   534
  | nxt_add thy oris pbt itms =
neuper@37906
   535
  let
neuper@37906
   536
    fun testr_vt v ori = (curry (op mem) v) (#2 (ori:ori))
neuper@37906
   537
      andalso (#3 ori) <>"#undef";
neuper@37906
   538
    fun testi_vt v itm = (curry (op mem) v) (#2 (itm:itm));
neuper@37906
   539
    fun test_id ids r = curry (op mem) (#1 (r:ori)) ids;
neuper@37906
   540
(* val itm = hd icl; val (_,_,_,d,ts) = v6;
neuper@37906
   541
   *)
neuper@37906
   542
    fun test_subset (itm:itm) ((_,_,_,d,ts):ori) = 
neuper@37934
   543
	(d_in (#5 itm)) = d andalso subset op = (ts_in (#5 itm), ts);
neuper@37906
   544
    fun false_and_not_Sup((i,v,false,f,Sup _):itm) = false
neuper@37906
   545
      | false_and_not_Sup (i,v,false,f, _) = true
neuper@37906
   546
      | false_and_not_Sup  _ = false;
neuper@37906
   547
neuper@37906
   548
    val v = if itms = [] then 1 else max_vt itms;
neuper@37906
   549
    val vors = if v = 0 then oris else filter (testr_vt v) oris;(*oris..vat*)
neuper@37906
   550
    val vits = if v = 0 then itms (*because of dsc without dat*)
neuper@37906
   551
	       else filter (testi_vt v) itms;                   (*itms..vat*)
neuper@37906
   552
    val icl = filter false_and_not_Sup vits; (* incomplete *)
neuper@37906
   553
  in if icl = [] 
neuper@37906
   554
     then case filter_out (test_id (map #1 vits)) vors of
neuper@37926
   555
	      [] => NONE
neuper@37906
   556
	    (* val miss = filter_out (test_id (map #1 vits)) vors;
neuper@37906
   557
	       *)
neuper@37926
   558
	    | miss => SOME (getr_ct thy (hd miss))
neuper@37906
   559
     else
neuper@37906
   560
	 case find_first (test_subset (hd icl)) vors of
neuper@37926
   561
	     (* val SOME ori = find_first (test_subset (hd icl)) vors;
neuper@37906
   562
	      *)
neuper@38031
   563
	     NONE => error "nxt_add: EX itm. not(dat(itm)<=dat(ori))"
neuper@37926
   564
	   | SOME ori => SOME (geti_ct thy ori (hd icl))
neuper@37934
   565
  end
neuper@37934
   566
end;
neuper@37906
   567
neuper@37906
   568
neuper@37906
   569
neuper@37906
   570
fun mk_delete thy "#Given"  itm_ = Del_Given   (itm_out thy itm_)
neuper@37906
   571
  | mk_delete thy "#Find"   itm_ = Del_Find    (itm_out thy itm_)
neuper@37906
   572
  | mk_delete thy "#Relate" itm_ = Del_Relation(itm_out thy itm_)
neuper@37906
   573
  | mk_delete thy str _ = 
neuper@38031
   574
  error ("mk_delete: called with field '"^str^"'");
neuper@37906
   575
fun mk_additem "#Given" ct = Add_Given ct
neuper@37906
   576
  | mk_additem "#Find"  ct = Add_Find ct    
neuper@37906
   577
  | mk_additem "#Relate"ct = Add_Relation ct
neuper@37906
   578
  | mk_additem str _ = 
neuper@38031
   579
  error ("mk_additem: called with field '"^str^"'");
neuper@37906
   580
neuper@37906
   581
neuper@37906
   582
neuper@37906
   583
neuper@37906
   584
neuper@37906
   585
(* find the next tac in specify (except nxt_model_pbl)
neuper@37906
   586
   4.00.: TODO: do not return a pos !!!
neuper@37906
   587
          (sind from DG comes the _OLD_ writepos)*)
neuper@37906
   588
(* 
neuper@37906
   589
> val (pbl,pbt,mpc) =(pbl',get_pbt cpI,(#ppc o get_met) cmI);
neuper@37906
   590
> val (dI,pI,mI) = empty_spec;
neuper@37906
   591
> nxt_spec Pbl (oris:ori list) ((dI',pI',mI'):spec(*original*))
neuper@37906
   592
  ((pbl:itm list), (met:itm list)) (pbt,mpc) ((dI,pI,mI):spec);
neuper@37906
   593
neuper@37906
   594
at Init_Proof:
neuper@37906
   595
> val met = [];val (pbt,mpc) = (get_pbt pI',(#ppc o get_met) mI');
neuper@37906
   596
> val (dI,pI,mI) = empty_spec;
neuper@37906
   597
> nxt_spec Pbl (oris:ori list) ((dI',pI',mI'):spec(*original*))
neuper@37906
   598
  ((pbl:itm list), (met:itm list)) (pbt,mpc) ((dI,pI,mI):spec);
neuper@37906
   599
  *)
neuper@37906
   600
neuper@37906
   601
(*. determine the next step of specification;
neuper@37906
   602
    not done here: Refine_Tacitly (otherwise *** unknown method: (..., no_met))
neuper@37906
   603
eg. in rootpbl 'no_met': 
neuper@37906
   604
args:
neuper@37906
   605
  preok          predicates are _all_ ok, or problem matches completely
neuper@37906
   606
  oris           immediately from formalization 
neuper@37906
   607
  (dI',pI',mI')  specification coming from author/parent-problem
neuper@37906
   608
  (pbl,          item lists specified by user
neuper@37906
   609
   met)          -"-, tacitly completed by copy_probl
neuper@37906
   610
  (dI,pI,mI)     specification explicitly done by the user
neuper@37906
   611
  (pbt, mpc)     problem type, guard of method
neuper@37906
   612
.*)
neuper@37906
   613
(* val (preok,pbl,pbt,mpc)=(pb,pbl',(#ppc o get_pbt) cpI,(#ppc o get_met) cmI);
neuper@37906
   614
   val (preok,pbl,pbt,mpc)=(pb,pbl',ppc,(#ppc o get_met) cmI);
neuper@37906
   615
   val (Pbl, preok, oris, (dI',pI',mI'), (pbl,met), (pbt,mpc), (dI,pI,mI)) =
neuper@37906
   616
       (p_, pb, oris, (dI',pI',mI'), (probl,meth), 
neuper@37906
   617
	(ppc, (#ppc o get_met) cmI), (dI,pI,mI));
neuper@37906
   618
   *)
neuper@37906
   619
fun nxt_spec Pbl preok (oris:ori list) ((dI',pI',mI'):spec)
neuper@37906
   620
  ((pbl:itm list), (met:itm list)) (pbt,mpc) ((dI,pI,mI):spec) = 
neuper@38015
   621
  ((*tracing"### nxt_spec Pbl";*)
neuper@37906
   622
   if dI'=e_domID andalso dI=e_domID then (Pbl, Specify_Theory dI')
neuper@37906
   623
   else if pI'=e_pblID andalso pI=e_pblID then (Pbl, Specify_Problem pI')
neuper@37906
   624
	else case find_first (is_error o #5) (pbl:itm list) of
neuper@37926
   625
	  SOME (_,_,_,fd,itm_) => 
neuper@37906
   626
	      (Pbl, mk_delete 
neuper@37906
   627
	       (assoc_thy (if dI=e_domID then dI' else dI)) fd itm_)
neuper@37926
   628
	| NONE => 
neuper@38015
   629
	    ((*tracing"### nxt_spec is_error NONE";*)
neuper@37906
   630
	     case nxt_add (assoc_thy (if dI=e_domID then dI' else dI)) 
neuper@37906
   631
		 oris pbt pbl of
neuper@37926
   632
(* val SOME (fd,ct') = nxt_add (assoc_thy (if dI=e_domID then dI' else dI)) 
neuper@37906
   633
                       oris pbt pbl;
neuper@37906
   634
  *)
neuper@38015
   635
	       SOME (fd,ct') => ((*tracing"### nxt_spec nxt_add SOME";*)
neuper@37906
   636
				 (Pbl, mk_additem fd ct'))
neuper@37926
   637
	     | NONE => (*pbl-items complete*)
neuper@37906
   638
	       if not preok then (Pbl, Refine_Problem pI')
neuper@37906
   639
	       else
neuper@37906
   640
		 if dI = e_domID then (Pbl, Specify_Theory dI')
neuper@37906
   641
		 else if pI = e_pblID then (Pbl, Specify_Problem pI')
neuper@37906
   642
		      else if mI = e_metID then (Pbl, Specify_Method mI')
neuper@37906
   643
			   else
neuper@37906
   644
			     case find_first (is_error o #5) met of
neuper@37926
   645
			       SOME (_,_,_,fd,itm_) => 
neuper@37906
   646
				   (Met, mk_delete (assoc_thy dI) fd itm_)
neuper@37926
   647
			     | NONE => 
neuper@37906
   648
				 (case nxt_add (assoc_thy dI) oris mpc met of
neuper@37926
   649
				      SOME (fd,ct') => (*30.8.01: pre?!?*)
neuper@37906
   650
				      (Met, mk_additem fd ct')
neuper@37926
   651
				    | NONE => 
neuper@37906
   652
				      ((*Solv 3.4.00*)Met, Apply_Method mI))))
neuper@37906
   653
(* val preok=pb; val (pbl, met) = (pbl,met');
neuper@37906
   654
   val (pbt,mpc)=((#ppc o get_pbt) cpI,(#ppc o get_met) cmI);
neuper@37906
   655
   val (Met, preok, oris, (dI',pI',mI'), (pbl,met), (pbt,mpc), (dI,pI,mI)) =
neuper@37906
   656
       (p_, pb, oris, (dI',pI',mI'), (probl,meth), 
neuper@37906
   657
	(ppc, (#ppc o get_met) cmI), (dI,pI,mI));
neuper@37906
   658
   *)
neuper@37906
   659
  | nxt_spec Met preok oris (dI',pI',mI') (pbl, met) (pbt,mpc) (dI,pI,mI) = 
neuper@38015
   660
  ((*tracing"### nxt_spec Met"; *)
neuper@37906
   661
   case find_first (is_error o #5) met of
neuper@37926
   662
     SOME (_,_,_,fd,itm_) => 
neuper@37906
   663
	 (Met, mk_delete (assoc_thy (if dI=e_domID then dI' else dI)) fd itm_)
neuper@37926
   664
   | NONE => 
neuper@37906
   665
       case nxt_add (assoc_thy (if dI=e_domID then dI' else dI))oris mpc met of
neuper@37926
   666
	 SOME (fd,ct') => (Met, mk_additem fd ct')
neuper@37926
   667
       | NONE => 
neuper@38015
   668
	   ((*tracing"### nxt_spec Met: nxt_add NONE";*)
neuper@37906
   669
	    if dI = e_domID then (Met, Specify_Theory dI')
neuper@37906
   670
	    else if pI = e_pblID then (Met, Specify_Problem pI')
neuper@37906
   671
		 else if not preok then (Met, Specify_Method mI)
neuper@37906
   672
		      else (Met, Apply_Method mI)));
neuper@37906
   673
	  
neuper@37906
   674
(* di_ pI_ mI_ pos_
neuper@37906
   675
val itms = [(1,[1],true,"#Find",Cor(e_term,[e_term])):itm,
neuper@37906
   676
	    (2,[2],true,"#Find",Syn("empty"))];
neuper@37906
   677
*)
neuper@37906
   678
neuper@37906
   679
neuper@37906
   680
(* ^^^--- aus nnewcode.sml am 30.1.00 ---^^^ *)
neuper@37906
   681
(*#############################################################*)
neuper@37906
   682
(*#############################################################*)
neuper@37906
   683
(* vvv--- aus nnewcode.sml vor 29.1.00 ---vvv *)
neuper@37906
   684
neuper@37906
   685
(*3.3.--
neuper@37906
   686
fun update_itm (cl,d,ts) ((id,vt,_,sl,Cor (_,_)):itm) = 
neuper@37906
   687
  (id,vt,cl,sl,Cor (d,ts)):itm
neuper@37906
   688
  | update_itm (cl,d,ts) (id,vt,_,sl,Syn (_)) =   
neuper@38031
   689
  error ("update_itm "^((Syntax.string_of_term (thy2ctxt thy)) (comp_dts thy (d,ts)))^
neuper@37906
   690
	       " not not for Syn (s:cterm')")
neuper@37906
   691
  | update_itm (cl,d,ts) (id,vt,_,sl,Typ (_)) = 
neuper@38031
   692
  error ("update_itm "^((Syntax.string_of_term (thy2ctxt thy)) (comp_dts thy (d,ts)))^
neuper@37906
   693
	       " not not for Typ (s:cterm')")
neuper@37906
   694
  | update_itm (cl,d,ts) (id,vt,_,sl,Fal (_,_)) =
neuper@37906
   695
  (id,vt,cl,sl,Fal (d,ts))
neuper@37906
   696
  | update_itm (cl,d,ts) (id,vt,_,sl,Inc (_,_)) =
neuper@37906
   697
  (id,vt,cl,sl,Inc (d,ts))
neuper@37906
   698
  | update_itm (cl,d,ts) (id,vt,_,sl,Sup (_,_)) =
neuper@37906
   699
  (id,vt,cl,sl,Sup (d,ts));
neuper@37906
   700
*)
neuper@37906
   701
neuper@37906
   702
neuper@37906
   703
neuper@37906
   704
neuper@37906
   705
fun is_field_correct sel d dscpbt =
neuper@37906
   706
  case assoc (dscpbt, sel) of
neuper@37926
   707
    NONE => false
neuper@37934
   708
  | SOME ds => member op = ds d;
neuper@37906
   709
neuper@37906
   710
(*. update the itm_ already input, all..from ori .*)
neuper@37906
   711
(* val (id,vt,fd,d,ts) = (i,v,f,d,ts\\ts');
neuper@37906
   712
   *)
neuper@37906
   713
fun ori_2itm thy itm_ pid all ((id,vt,fd,d,ts):ori) = 
neuper@37906
   714
  let 
neuper@37930
   715
    val ts' = union op = (ts_in itm_) ts;
neuper@37906
   716
    val pval = pbl_ids' thy d ts'
neuper@37906
   717
	(*WN.9.5.03: FIXXXME [#0, epsilon]
neuper@37906
   718
	  here would upd_penv be called for [#0, epsilon] etc. *)
neuper@37934
   719
    val complete = if eq_set op = (ts', all) then true else false;
neuper@37906
   720
  in case itm_ of
neuper@37906
   721
    (Cor _) => 
neuper@37906
   722
	(if fd = "#undef" then (id,vt,complete,fd,Sup(d,ts')) 
neuper@37906
   723
	 else (id,vt,complete,fd,Cor((d,ts'),(pid, pval)))):itm
neuper@38031
   724
  | (Syn c)     => error ("ori_2itm wants to overwrite "^c)
neuper@38031
   725
  | (Typ c)     => error ("ori_2itm wants to overwrite "^c)
neuper@37906
   726
  | (Inc _) => if complete
neuper@37906
   727
	       then (id,vt,true ,fd, Cor ((d,ts'),(pid, pval)))
neuper@37906
   728
	       else (id,vt,false,fd, Inc ((d,ts'),(pid, pval)))
neuper@37906
   729
  | (Sup ((*_,_*)d,ts')) => (*4.9.01 lost env*)
neuper@37906
   730
	 (*if fd = "#undef" then*) (id,vt,complete,fd,Sup(d,ts'))
neuper@37906
   731
	 (*else (id,vt,complete,fd,Cor((d,ts'),e))*)
neuper@37906
   732
(* 28.1.00: not completely clear ---^^^ etc.*)
neuper@37906
   733
(* 4.9.01: Mis just copied---vvv *)
neuper@37906
   734
  | (Mis _) => if complete
neuper@37906
   735
		     then (id,vt,true ,fd, Cor ((d,ts'),(pid, pval)))
neuper@37906
   736
		     else (id,vt,false,fd, Inc ((d,ts'),(pid, pval)))
neuper@37906
   737
  end;
neuper@37906
   738
neuper@37906
   739
neuper@37906
   740
fun eq1 d (_,(d',_)) = (d = d');
neuper@37906
   741
fun eq3 f d (_,_,_,f',itm_) = f = f' andalso d = (d_in itm_); 
neuper@37906
   742
neuper@37906
   743
neuper@37906
   744
(* 'all' ts from ori; ts is the input; (ori carries rest of info)
neuper@37906
   745
   9.01: this + ori_2itm is _VERY UNCLEAR_ ? overhead ?
neuper@37906
   746
   pval: value for problem-environment _NOT_ checked for 'inter' --
neuper@37906
   747
   -- FIXXME.WN.11.03 the generation of penv has to go to insert_ppc
neuper@37906
   748
  (as it has been done for input_icalhd+insert_ppc' in 11.03)*)
neuper@37906
   749
(*. is_input ori itms <=> 
neuper@37906
   750
    EX itm. (1) ori(field,dsc) = itm(field,dsc) & (2..4)
neuper@37906
   751
            (2) ori(ts) subset itm(ts)        --- Err "already input"       
neuper@37906
   752
	    (3) ori(ts) inter itm(ts) = empty --- new: ori(ts)
neuper@37906
   753
	    (4) -"- <> empty                  --- new: ori(ts) \\ inter .*)
neuper@37906
   754
(* val(itms,(i,v,f,d,ts)) = (ppc,ori');
neuper@37906
   755
   *)
neuper@37906
   756
fun is_notyet_input thy (itms:itm list) all ((i,v,f,d,ts):ori) pbt =
neuper@37906
   757
  case find_first (eq1 d) pbt of
neuper@37926
   758
      SOME (_,(_,pid)) =>(* val SOME (_,(_,pid)) = find_first (eq1 d) pbt;
neuper@37926
   759
                            val SOME (_,_,_,_,itm_)=find_first (eq3 f d) itms;
neuper@37906
   760
			   *)
neuper@37906
   761
      (case find_first (eq3 f d) itms of
neuper@37926
   762
	   SOME (_,_,_,_,itm_) =>
neuper@37906
   763
	   let 
neuper@37934
   764
	       val ts' = inter op = (ts_in itm_) ts;
neuper@37934
   765
	   in if subset op = (ts, ts') 
neuper@37906
   766
	      then (((strs2str' o 
neuper@37934
   767
		      map (Syntax.string_of_term (thy2ctxt thy))) ts')^
neuper@37906
   768
		    " already input", e_itm)                            (*2*)
neuper@37934
   769
	      else ("", 
neuper@37934
   770
                    ori_2itm thy itm_ pid all (i,v,f,d,
neuper@37934
   771
                                               subtract op = ts' ts))   (*3,4*)
neuper@37906
   772
	   end
neuper@37926
   773
	 | NONE => ("", ori_2itm thy (Inc ((e_term,[]),(pid,[]))) 
neuper@37906
   774
				 pid all (i,v,f,d,ts))                  (*1*)
neuper@37906
   775
	)
neuper@37926
   776
    | NONE => ("", ori_2itm thy (Sup (d,ts)) 
neuper@37906
   777
			      e_term all (i,v,f,d,ts));
neuper@37906
   778
neuper@37906
   779
fun test_types thy (d,ts) =
neuper@37906
   780
  let 
neuper@37906
   781
    val s = !show_types; val _ = show_types:= true;
neuper@37906
   782
    val opt = (try (comp_dts thy)) (d,ts);
neuper@37906
   783
    val msg = case opt of 
neuper@37926
   784
      SOME _ => "" 
neuper@37934
   785
    | NONE => ((Syntax.string_of_term (thy2ctxt thy) d)^" "^
neuper@37934
   786
	     ((strs2str' o map (Syntax.string_of_term (thy2ctxt thy))) ts)
neuper@37934
   787
	     ^ " is illtyped");
neuper@37906
   788
    val _ = show_types:= s
neuper@37906
   789
  in msg end;
neuper@37906
   790
neuper@37906
   791
neuper@37906
   792
neuper@38031
   793
fun maxl [] = error "maxl of []"
neuper@37906
   794
  | maxl (y::ys) =
neuper@37906
   795
  let fun mx x [] = x
neuper@37906
   796
	| mx x (y::ys) = if x < (y:int) then mx y ys else mx x ys
neuper@37906
   797
  in mx y ys end;
neuper@37906
   798
neuper@37906
   799
neuper@37906
   800
(*. is the input term t known in oris ? 
neuper@37906
   801
    give feedback on all(?) strange input;
neuper@37906
   802
    return _all_ terms already input to this item (e.g. valuesFor a,b) .*)
neuper@37906
   803
(*WN.11.03: from lists*)
neuper@37906
   804
fun is_known thy sel ori t =
neuper@37906
   805
(* val (ori,t)=(oris,term_of ct);
neuper@37906
   806
   *)
neuper@37906
   807
  let
neuper@37906
   808
    val ots = (distinct o flat o (map #5)) (ori:ori list);
neuper@37906
   809
    val oids = ((map (fst o dest_Free)) o distinct o 
neuper@37906
   810
		flat o (map vars)) ots;
neuper@37906
   811
    val (d,ts(*,pval*)) = split_dts thy t;
neuper@37906
   812
    val ids = map (fst o dest_Free) 
neuper@37906
   813
      ((distinct o (flat o (map vars))) ts);
neuper@37934
   814
  in if (subtract op = oids ids) <> []
neuper@37934
   815
     then (("identifiers "^(strs2str' (subtract op = oids ids))^
neuper@37906
   816
	    " not in example"), e_ori_, [])
neuper@37906
   817
     else 
neuper@37906
   818
	 if d = e_term 
neuper@37906
   819
	 then 
neuper@37934
   820
	     if not (subset op = (map typeless ts, map typeless ots))
neuper@37906
   821
	     then (("terms '"^
neuper@37934
   822
		    ((strs2str' o (map (Syntax.string_of_term 
neuper@37934
   823
					    (thy2ctxt thy)))) ts)^
neuper@37906
   824
		    "' not in example (typeless)"), e_ori_, [])
neuper@37906
   825
	     else (case seek_orits thy sel ts ori of
neuper@37906
   826
		       ("", ori_ as (_,_,_,d,ts), all) =>
neuper@37906
   827
		       (case test_types thy (d,ts) of
neuper@37906
   828
			    "" => ("", ori_, all)
neuper@37906
   829
			  | msg => (msg, e_ori_, []))
neuper@37906
   830
		     | (msg,_,_) => (msg, e_ori_, []))
neuper@37906
   831
	 else 
neuper@37934
   832
	     if member op = (map #4 ori) d
neuper@37906
   833
	     then seek_oridts thy sel (d,ts) ori
neuper@37934
   834
	     else ((Syntax.string_of_term (thy2ctxt thy) d)^
neuper@37906
   835
		   (*" not in example", e_ori_, []) ///11.11.03*)
neuper@37906
   836
		   " not in example", (0,[],sel,d,ts), [])
neuper@37906
   837
  end;
neuper@37906
   838
neuper@37906
   839
neuper@37906
   840
(*. for return-value of appl_add .*)
neuper@37906
   841
datatype additm =
neuper@37906
   842
	 Add of itm
neuper@37906
   843
       | Err of string;    (*error-message*)
neuper@37906
   844
neuper@37906
   845
neuper@37906
   846
(*. add an item; check wrt. oris and pbt .*)
neuper@37906
   847
neuper@37906
   848
(* in contrary to oris<>[] below, this part handles user-input
neuper@37906
   849
   extremely acceptive, i.e. accept input instead error-msg *)
neuper@37906
   850
fun appl_add thy sel ([]:ori list) ppc pbt ct' =
neuper@37906
   851
(* val (ppc,pbt,ct',env) = (pbl, (#ppc o get_pbt) cpI, ct, []:envv);
neuper@37906
   852
   !!!! 28.8.01: env tested _minimally_ !!!
neuper@37906
   853
   *)
neuper@37906
   854
  let 
neuper@37906
   855
    val i = 1 + (if ppc=[] then 0 else maxl (map #1 ppc));
neuper@37906
   856
  in case parse thy ct' of (*should be done in applicable_in 4.00.FIXME*)
neuper@37926
   857
    NONE => Add (i,[],false,sel,Syn ct')
neuper@37926
   858
(* val (SOME ct) = parse thy ct';
neuper@37906
   859
   *)
neuper@37926
   860
  | SOME ct =>
neuper@37906
   861
      let
neuper@37906
   862
	val (d,ts(*,pval*)) = split_dts thy (term_of ct);
neuper@37906
   863
      in if d = e_term 
neuper@37906
   864
	 then Add (i,[],false,sel,Mis (dsc_unknown,hd ts(*24.3.02*)))
neuper@37906
   865
      
neuper@37906
   866
	 else  
neuper@37906
   867
	   (case find_first (eq1 d) pbt of
neuper@37926
   868
	     NONE => Add (i,[],true,sel,Sup ((d,ts)))
neuper@37926
   869
	   | SOME (f,(_,id)) =>
neuper@37926
   870
(* val SOME (f,(_,id)) = find_first (eq1 d) pbt;
neuper@37906
   871
   *)
neuper@37906
   872
	       let
neuper@37906
   873
		 fun eq2 d ((i,_,_,_,itm_):itm) = 
neuper@37906
   874
		     (d = (d_in itm_)) andalso i<>0;
neuper@37906
   875
	       in case find_first (eq2 d) ppc of 
neuper@37926
   876
		 NONE => Add (i,[],true,f, Cor ((d,ts), (id, (*pval*)
neuper@37906
   877
							 pbl_ids' thy d ts)))
neuper@37926
   878
	       | SOME (i',_,_,_,itm_) => 
neuper@37926
   879
(* val SOME (i',_,_,_,itm_) = find_first (eq2 d) ppc;
neuper@37926
   880
   val NONE = find_first (eq2 d) ppc;
neuper@37906
   881
   *)
neuper@37906
   882
		   if is_list_dsc d
neuper@37930
   883
		   then let val ts = union op = ts (ts_in itm_) 
neuper@37906
   884
			in Add (if ts_in itm_ = [] then i else i',
neuper@37906
   885
				 [],true,f,Cor ((d, ts), (id, (*pval*)
neuper@37906
   886
							  pbl_ids' thy d ts)))
neuper@37906
   887
			end
neuper@37906
   888
		   else Add (i',[],true,f,Cor ((d,ts),(id, (*pval*)
neuper@37906
   889
						       pbl_ids' thy d ts)))
neuper@37906
   890
	       end
neuper@37906
   891
	   )
neuper@37906
   892
      end
neuper@37906
   893
  end
neuper@37906
   894
(*. add ct to ppc .*)
neuper@37906
   895
(*FIXXME: accept items as Sup, Syn here, too (like appl_add..oris=[] above)*)
neuper@37906
   896
(* val (ppc,pbt) = (pbl, ppc);
neuper@37906
   897
   val (ppc,pbt) = (met, (#ppc o get_met) cmI);
neuper@37906
   898
neuper@37906
   899
   val (ppc,pbt) = (pbl, (#ppc o get_pbt) cpI);
neuper@37906
   900
   *)
neuper@37906
   901
  | appl_add thy sel oris ppc pbt(*only for upd_envv*) ct = 
neuper@37906
   902
  let
neuper@37906
   903
    val ctopt = parse thy ct;
neuper@37906
   904
  in case ctopt of
neuper@37926
   905
    NONE => Err ("syntax error in "^ct)
neuper@37926
   906
  | SOME ct =>(* val SOME ct = ctopt;
neuper@37906
   907
		 val (msg,ori',all) = is_known thy sel oris (term_of ct);
neuper@37906
   908
		 val (msg,itm) = is_notyet_input thy ppc all ori' pbt;
neuper@37906
   909
		*) 
neuper@37906
   910
    (case is_known thy sel oris (term_of ct) of
neuper@37906
   911
	 ("",ori'(*ts='ct'*), all) => 
neuper@37906
   912
	 (case is_notyet_input thy ppc all ori' pbt of
neuper@37906
   913
	      ("",itm)  => Add itm
neuper@37906
   914
	    | (msg,_) => Err msg)
neuper@37906
   915
       | (msg,_,_) => Err msg)
neuper@37906
   916
  end;
neuper@37906
   917
(* 
neuper@37906
   918
> val (msg,itm) = is_notyet_input thy ppc all ori';
neuper@37906
   919
val itm = (12,[3],false,"#Relate",Cor (Const #,[#,#])) : itm
neuper@37906
   920
> val itm_ = #5 itm;
neuper@37906
   921
> val ts = ts_in itm_;
neuper@37906
   922
> map (atomty) ts; 
neuper@37906
   923
*)
neuper@37906
   924
neuper@37906
   925
(** make oris from args of the stac SubProblem and from pbt **)
neuper@37906
   926
neuper@37906
   927
(*.can this formal argument (of a model-pattern) be omitted in the arg-list
neuper@37906
   928
   of a SubProblem ? see ME/ptyps.sml 'type met '.*)
neuper@37906
   929
fun is_copy_named_idstr str =
neuper@37906
   930
    case (rev o explode) str of
neuper@38011
   931
	"'":: _ ::"'"::_ => (tracing ((strs2str o (rev o explode)) 
neuper@38011
   932
                                  "is_copy_named_idstr: " ^ str ^ " T"); true)
neuper@38011
   933
      | _ => (tracing ((strs2str o (rev o explode)) 
neuper@38011
   934
                                  "is_copy_named_idstr: " ^ str ^ " F"); false);
neuper@38010
   935
fun is_copy_named (_, (_, t)) = (is_copy_named_idstr o free2str) t;
neuper@38010
   936
neuper@37906
   937
(*.should this formal argument (of a model-pattern) create a new identifier?.*)
neuper@37906
   938
fun is_copy_named_generating_idstr str =
neuper@37906
   939
    if is_copy_named_idstr str
neuper@37906
   940
    then case (rev o explode) str of
neuper@38010
   941
	"'"::"'"::"'"::_ => false
neuper@37906
   942
      | _ => true
neuper@37906
   943
    else false;
neuper@38010
   944
fun is_copy_named_generating (_, (_, t)) = 
neuper@37906
   945
    (is_copy_named_generating_idstr o free2str) t;
neuper@37906
   946
neuper@37906
   947
(*.split type-wrapper from scr-arg and build part of an ori;
neuper@37906
   948
   an type-error is reported immediately, raises an exn, 
neuper@37906
   949
   subsequent handling of exn provides 2nd part of error message.*)
neuper@37934
   950
fun mtc thy ((str, (dsc, _)):pat) (ty $ var) =
neuper@37934
   951
    (* val (thy, (str, (dsc, _)), (ty $ var)) =
neuper@37934
   952
	   (thy,  p,               a);
neuper@37934
   953
       *)
neuper@37938
   954
    (cterm_of thy (dsc $ var);(*type check*)
neuper@37934
   955
     SOME ((([1], str, dsc, (*[var]*)
neuper@37934
   956
	    split_dts' (dsc, var))): preori)(*:ori without leading #*))
neuper@38011
   957
    handle e as TYPE _ => 
neuper@38015
   958
	   (tracing (dashs 70 ^ "\n"
neuper@38011
   959
	^"*** ERROR while creating the items for the model of the ->problem\n"
neuper@38011
   960
	^"*** from the ->stac with ->typeconstructor in arglist:\n"
neuper@38011
   961
	^"*** item (->description ->value): "^term2str dsc^" "^term2str var^"\n"
neuper@38011
   962
	^"*** description: "^(term_detail2str dsc)
neuper@38011
   963
	^"*** value: "^(term_detail2str var)
neuper@38011
   964
	^"*** typeconstructor in script: "^(term_detail2str ty)
neuper@38011
   965
	^"*** checked by theory: "^(theory2str thy)^"\n"
neuper@38011
   966
	^"*** " ^ dots 66);
neuper@38011
   967
	    OldGoals.print_exn e; (*raises exn again*)
neuper@38011
   968
	(*raise ERROR "actual args do not match formal args";FIXXXME.WN100916*)
neuper@38011
   969
	NONE);
neuper@37906
   970
neuper@37906
   971
(*.match each pat of the model-pattern with an actual argument;
neuper@37906
   972
   precondition: copy-named vars are filtered out.*)
neuper@37906
   973
fun matc thy ([]:pat list)  _  (oris:preori list) = oris
neuper@37906
   974
  | matc thy pbt [] _ =
neuper@38015
   975
    (tracing (dashs 70);
neuper@38031
   976
     error ("actual arg(s) missing for '"^pats2str pbt
neuper@37906
   977
		 ^"' i.e. should be 'copy-named' by '*_._'"))
neuper@37906
   978
  | matc thy ((p as (s,(d,t)))::pbt) (a::ags) oris =
neuper@37906
   979
    (* val (thy, ((p as (s,(d,t)))::pbt), (a::ags), oris) =
neuper@37906
   980
	   (thy,  pbt',                    ags,     []);
neuper@37906
   981
       (*recursion..*)
neuper@37906
   982
       val (thy, ((p as (s,(d,t)))::pbt), (a::ags), oris) =
neuper@37906
   983
	   (thy,  pbt,                     ags,    (oris @ [ori]));
neuper@37906
   984
       *)
neuper@37906
   985
    (*del?..*)if (is_copy_named_idstr o free2str) t then oris
neuper@37906
   986
    else(*..del?*) let val opt = mtc thy p a;  
neuper@37906
   987
	 in case opt of
neuper@37926
   988
		(* val SOME ori = mtc thy p a;
neuper@37906
   989
		   *)
neuper@37926
   990
		SOME ori => matc thy pbt ags (oris @ [ori])
neuper@37926
   991
	      | NONE => [](*WN050903 skipped by exn handled in match_ags*)
neuper@37906
   992
	 end; 
neuper@37906
   993
(* run subp-rooteq.sml until Init_Proof before ...
neuper@37906
   994
> val Nd (PblObj {origin=(oris,_,_),...},_) = pt;(*from test/subp-rooteq.sml*)
neuper@37906
   995
> fun xxxfortest (_,a,b,c,d) = (a,b,c,d);val oris = map xxxfortest oris;
neuper@37906
   996
neuper@37906
   997
 other vars as in mtc ..
neuper@37906
   998
> matc thy (drop_last pbt) ags [];
neuper@37906
   999
val it = ([[1],"#Given",Const #,[#]),(0,[#],"#Given",Const #,[#])],2)*)
neuper@37906
  1000
neuper@37906
  1001
neuper@38011
  1002
(* generate a new variable "x_i" name from a related given one "x"
neuper@38011
  1003
   by use of oris relating "v_i_" (is_copy_named!) to "v_"
neuper@38011
  1004
   e.g. (v_, x) & (v_i_, ?) --> (v_i_, x_i) *)
neuper@38011
  1005
neuper@38011
  1006
(* generate a new variable "x_i" name from a related given one "x"
neuper@38011
  1007
   by use of oris relating "v_v'i'" (is_copy_named!) to "v_v"
neuper@38012
  1008
   e.g. (v_v, x) & (v_v'i', ?) --> (v_v'i', x_i),
neuper@38012
  1009
   but leave is_copy_named_generating as is, e.t. ss''' *)
neuper@37906
  1010
fun cpy_nam (pbt:pat list) (oris:preori list) (p as (field,(dsc,t)):pat) =
neuper@37906
  1011
  (if is_copy_named_generating p
neuper@37906
  1012
   then (*WN051014 kept strange old code ...*)
neuper@37906
  1013
       let fun sel (_,_,d,ts) = comp_ts (d, ts) 
neuper@38011
  1014
	   val cy' = (implode o (drop_last_n 3) o explode o free2str) t
neuper@37906
  1015
	   val ext = (last_elem o drop_last o explode o free2str) t
neuper@38011
  1016
	   val vars' = map (free2str o snd o snd) pbt (*cpy-nam filtered_out*)
neuper@37906
  1017
	   val vals = map sel oris
neuper@37906
  1018
	   val cy_ext = (free2str o the) (assoc (vars'~~vals, cy'))^"_"^ext
neuper@37906
  1019
       in ([1], field, dsc, [mk_free (type_of t) cy_ext]):preori end
neuper@37906
  1020
   else ([1], field, dsc, [t])
neuper@37906
  1021
	)
neuper@38031
  1022
  handle _ => error ("cpy_nam: for "^(term2str t));
neuper@37906
  1023
neuper@37906
  1024
(*.match the actual arguments of a SubProblem with a model-pattern
neuper@37906
  1025
   and create an ori list (in root-pbl created from formalization).
neuper@37906
  1026
   expects ags:pats = 1:1, while copy-named are filtered out of pats;
neuper@38011
  1027
   if no 1:1 the exn raised by matc/mtc and handled at call.
neuper@37906
  1028
   copy-named pats are appended in order to get them into the model-items.*)
neuper@37906
  1029
fun match_ags thy (pbt:pat list) ags =
neuper@37906
  1030
    let fun flattup (i,(var,bool,str,itm_)) = (i,var,bool,str,itm_);
neuper@37906
  1031
	val pbt' = filter_out is_copy_named pbt;
neuper@37906
  1032
	val cy = filter is_copy_named pbt;
neuper@37906
  1033
	val oris' = matc thy pbt' ags [];
neuper@37906
  1034
	val cy' = map (cpy_nam pbt' oris') cy;
neuper@37906
  1035
	val ors = add_id (oris' @ cy'); 
neuper@38011
  1036
    (*appended in order to get ^^^^^ into the model-items*)
neuper@37906
  1037
    in (map flattup ors):ori list end;
neuper@37906
  1038
neuper@37906
  1039
(*.report part of the error-msg which is not available in match_args.*)
neuper@37906
  1040
fun match_ags_msg pI stac ags =
neuper@37906
  1041
    let val s = !show_types
neuper@37906
  1042
	val _ = show_types:= true
neuper@37906
  1043
	val pats = (#ppc o get_pbt) pI
neuper@37906
  1044
	val msg = (dots 70^"\n"
neuper@37906
  1045
		 ^"*** problem "^strs2str pI^" has the ...\n"
neuper@37906
  1046
		 ^"*** model-pattern "^pats2str pats^"\n"
neuper@37906
  1047
		 ^"*** stac   '"^term2str stac^"' has the ...\n"
neuper@37906
  1048
		 ^"*** arg-list "^terms2str ags^"\n"
neuper@37906
  1049
		 ^dashs 70)
neuper@38011
  1050
	(*WN100921 ^^^^ expect TYPE errormsg below; lost with Isa09*)
neuper@37906
  1051
	val _ = show_types:= s
neuper@38015
  1052
    in tracing msg end;
neuper@37906
  1053
neuper@37906
  1054
neuper@37906
  1055
(*get the variables out of a pbl_; FIXME.WN.0311: is_copy_named ...obscure!!!*)
neuper@37906
  1056
fun vars_of_pbl_ pbl_ = 
neuper@37906
  1057
    let fun var_of_pbl_ (gfr,(dsc,t)) = t
neuper@37906
  1058
    in ((map var_of_pbl_) o (filter_out is_copy_named)) pbl_ end;
neuper@37906
  1059
fun vars_of_pbl_' pbl_ = 
neuper@37906
  1060
    let fun var_of_pbl_ (gfr,(dsc,t)) = t:term
neuper@37906
  1061
    in ((map var_of_pbl_)(* o (filter_out is_copy_named)*)) pbl_ end;
neuper@37906
  1062
neuper@37906
  1063
fun overwrite_ppc thy itm ppc =
neuper@37906
  1064
  let 
neuper@37906
  1065
    fun repl ppc' (_,_,_,_,itm_) [] =
neuper@38031
  1066
      error ("overwrite_ppc: " ^ (itm_2str_ (thy2ctxt thy) itm_) ^ 
neuper@37934
  1067
                   " not found")
neuper@37906
  1068
      | repl ppc' itm (p::ppc) =
neuper@37906
  1069
	if (#1 itm) = (#1 (p:itm)) then ppc' @ [itm] @ ppc
neuper@37906
  1070
	else repl (ppc' @ [p]) itm ppc
neuper@37906
  1071
  in repl [] itm ppc end;
neuper@37906
  1072
neuper@37906
  1073
(*10.3.00: insert the already compiled itm into model;
neuper@37906
  1074
   ev. filter_out  untouched (in FE: (0,...)) item related to insert-item *)
neuper@37906
  1075
(* val ppc=pbl;
neuper@37906
  1076
   *)
neuper@37906
  1077
fun insert_ppc thy itm ppc =
neuper@37906
  1078
    let 
neuper@37906
  1079
	fun eq_untouched d ((0,_,_,_,itm_):itm) = (d = d_in itm_)
neuper@37906
  1080
	  | eq_untouched _ _ = false;
neuper@37906
  1081
	    val ppc' = 
neuper@37906
  1082
		(
neuper@38015
  1083
		 (*tracing("### insert_ppc: itm= "^(itm2str_ itm));*)       
neuper@37906
  1084
		 case seek_ppc (#1 itm) ppc of
neuper@37926
  1085
		     (* val SOME xxx = seek_ppc (#1 itm) ppc;
neuper@37906
  1086
		        *)
neuper@37926
  1087
		     SOME _ => (*itm updated in is_notyet_input WN.11.03*)
neuper@37906
  1088
		     overwrite_ppc thy itm ppc
neuper@37926
  1089
		   | NONE => (ppc @ [itm]));
neuper@37906
  1090
    in filter_out (eq_untouched ((d_in o #5) itm)) ppc' end;
neuper@37906
  1091
neuper@37906
  1092
(*from Isabelle/src/Pure/library.ML, _appends_ a new element*)
neuper@37906
  1093
fun gen_ins' eq (x, xs) = if gen_mem eq (x, xs) then xs else xs @ [x];
neuper@37906
  1094
neuper@37906
  1095
fun eq_dsc ((_,_,_,_,itm_):itm, (_,_,_,_,iitm_):itm) = 
neuper@37906
  1096
    (d_in itm_) = (d_in iitm_);
neuper@37906
  1097
(*insert_ppc = insert_ppc' for appl_add', input_icalhd 11.03,
neuper@37906
  1098
    handles superfluous items carelessly*)
neuper@37906
  1099
fun insert_ppc' itm itms = gen_ins' eq_dsc (itm, itms);
neuper@37906
  1100
(* val eee = op=;
neuper@37906
  1101
 > gen_ins' eee (4,[1,3,5,7]);
neuper@37906
  1102
val it = [1, 3, 5, 7, 4] : int list*)
neuper@37906
  1103
neuper@37906
  1104
neuper@37906
  1105
(*. output the headline to a ppc .*)
neuper@37906
  1106
fun header p_ pI mI =
neuper@37906
  1107
    case p_ of Pbl => Problem (if pI = e_pblID then [] else pI) 
neuper@37906
  1108
	     | Met => Method mI
neuper@38031
  1109
	     | pos => error ("header called with "^ pos_2str pos);
neuper@37906
  1110
neuper@37906
  1111
neuper@37906
  1112
neuper@37906
  1113
(* test-printouts ---
neuper@38015
  1114
val _=tracing("### insert_ppc: (d,ts)="^((Syntax.string_of_term (thy2ctxt thy))(comp_dts thy(d,ts))));
neuper@38015
  1115
 val _=tracing("### insert_ppc: pts= "^
neuper@37934
  1116
(strs2str' o map (Syntax.string_of_term (thy2ctxt thy))) pts);
neuper@37906
  1117
neuper@37906
  1118
neuper@37906
  1119
 val sel = "#Given"; val Add_Given' ct = m;
neuper@37906
  1120
neuper@37906
  1121
 val sel = "#Find"; val Add_Find' (ct,_) = m; 
neuper@37906
  1122
 val (p,_) = p;
neuper@37906
  1123
 val (_,_,f,nxt',_,pt')= specify_additem sel (ct,[]) (p,Pbl(*!!!!!!!*)) c pt;
neuper@37906
  1124
--------------
neuper@37906
  1125
 val sel = "#Given"; val Add_Given' (ct,_) = nxt; val (p,_) = p;
neuper@37906
  1126
  *)
neuper@37906
  1127
fun specify_additem sel (ct,_) (p,Met) c pt = 
neuper@37906
  1128
    let
neuper@37906
  1129
      val (PblObj{meth=met,origin=(oris,(dI',pI',mI'),_),
neuper@37906
  1130
		  probl=pbl,spec=(dI,pI,mI),...}) = get_obj I pt p;
neuper@37906
  1131
      val thy = if dI = e_domID then assoc_thy dI' else assoc_thy dI;
neuper@37906
  1132
    (*val ppt = if pI = e_pblID then get_pbt pI' else get_pbt pI;*)
neuper@37906
  1133
      val cpI = if pI = e_pblID then pI' else pI;
neuper@37906
  1134
      val cmI = if mI = e_metID then mI' else mI;
neuper@37906
  1135
      val {ppc,pre,prls,...} = get_met cmI
neuper@37906
  1136
    in case appl_add thy sel oris met ppc ct of
neuper@37906
  1137
      Add itm (*..union old input *) =>
neuper@37906
  1138
	let (* val Add itm = appl_add thy sel oris met (#ppc (get_met cmI)) ct;
neuper@37906
  1139
               *)
neuper@37906
  1140
	  val met' = insert_ppc thy itm met;
neuper@37906
  1141
	  (*val pt' = update_met pt p met';*)
neuper@37906
  1142
	  val ((p,Met),_,_,pt') = 
neuper@37906
  1143
	      generate1 thy (case sel of
neuper@37906
  1144
				 "#Given" => Add_Given' (ct, met')
neuper@37906
  1145
			       | "#Find"  => Add_Find'  (ct, met')
neuper@37906
  1146
			       | "#Relate"=> Add_Relation'(ct, met')) 
neuper@37906
  1147
			Uistate (p,Met) pt
neuper@37906
  1148
	  val pre' = check_preconds thy prls pre met'
neuper@37906
  1149
	  val pb = foldl and_ (true, map fst pre')
neuper@38015
  1150
	  (*val _=tracing("@@@ specify_additem: Met Add before nxt_spec")*)
neuper@37906
  1151
	  val (p_,nxt) =
neuper@37906
  1152
	    nxt_spec Met pb oris (dI',pI',mI') (pbl,met') 
neuper@37906
  1153
	    ((#ppc o get_pbt) cpI,ppc) (dI,pI,mI);
neuper@37906
  1154
	in ((p,p_), ((p,p_),Uistate),
neuper@37906
  1155
	    Form' (PpcKF (0,EdUndef,(length p),Nundef,
neuper@37906
  1156
			  (Method cmI, itms2itemppc thy met' pre'))),
neuper@37906
  1157
	    nxt,Safe,pt') end
neuper@37906
  1158
    | Err msg =>
neuper@37906
  1159
	  let val pre' = check_preconds thy prls pre met
neuper@37906
  1160
	      val pb = foldl and_ (true, map fst pre')
neuper@38015
  1161
	    (*val _=tracing("@@@ specify_additem: Met Err before nxt_spec")*)
neuper@37906
  1162
	      val (p_,nxt) =
neuper@37906
  1163
	    nxt_spec Met pb oris (dI',pI',mI') (pbl,met) 
neuper@37906
  1164
	    ((#ppc o get_pbt) cpI,(#ppc o get_met) cmI) (dI,pI,mI);
neuper@37906
  1165
	  in ((p,p_), ((p,p_),Uistate), Error' (Error_ msg), nxt, Safe,pt) end
neuper@37906
  1166
    end
neuper@37906
  1167
(* val (p,_) = p;
neuper@37906
  1168
   *)
neuper@37906
  1169
| specify_additem sel (ct,_) (p,_(*Frm, Pbl*)) c pt = 
neuper@37906
  1170
    let
neuper@37906
  1171
      val (PblObj{meth=met,origin=(oris,(dI',pI',mI'),_),
neuper@37906
  1172
		  probl=pbl,spec=(dI,pI,mI),...}) = get_obj I pt p;
neuper@37906
  1173
      val thy = if dI = e_domID then assoc_thy dI' else assoc_thy dI;
neuper@37906
  1174
      val cpI = if pI = e_pblID then pI' else pI;
neuper@37906
  1175
      val cmI = if mI = e_metID then mI' else mI;
neuper@37906
  1176
      val {ppc,where_,prls,...} = get_pbt cpI;
neuper@37906
  1177
    in case appl_add thy sel oris pbl ppc ct of
neuper@37906
  1178
      Add itm (*..union old input *) =>
neuper@37906
  1179
      (* val Add itm = appl_add thy sel oris pbl ppc ct;
neuper@37906
  1180
         *)
neuper@37906
  1181
	let
neuper@38015
  1182
	    (*val _= tracing("###specify_additem: itm= "^(itm2str_ itm));*)
neuper@37906
  1183
	  val pbl' = insert_ppc thy itm pbl
neuper@37906
  1184
	  val ((p,Pbl),_,_,pt') = 
neuper@37906
  1185
	      generate1 thy (case sel of
neuper@37906
  1186
				 "#Given" => Add_Given' (ct, pbl')
neuper@37906
  1187
			       | "#Find"  => Add_Find'  (ct, pbl')
neuper@37906
  1188
			       | "#Relate"=> Add_Relation'(ct, pbl')) 
neuper@37906
  1189
			Uistate (p,Pbl) pt
neuper@37906
  1190
	  val pre = check_preconds thy prls where_ pbl'
neuper@37906
  1191
	  val pb = foldl and_ (true, map fst pre)
neuper@38015
  1192
	(*val _=tracing("@@@ specify_additem: Pbl Add before nxt_spec")*)
neuper@37906
  1193
	  val (p_,nxt) =
neuper@37906
  1194
	    nxt_spec Pbl pb oris (dI',pI',mI') (pbl',met) 
neuper@37906
  1195
		     (ppc,(#ppc o get_met) cmI) (dI,pI,mI);
neuper@37906
  1196
	  val ppc = if p_= Pbl then pbl' else met;
neuper@37906
  1197
	in ((p,p_), ((p,p_),Uistate),
neuper@37906
  1198
	    Form' (PpcKF (0,EdUndef,(length p),Nundef,
neuper@37906
  1199
			  (header p_ pI cmI,
neuper@37906
  1200
			   itms2itemppc thy ppc pre))), nxt,Safe,pt') end
neuper@37906
  1201
neuper@37906
  1202
    | Err msg =>
neuper@37906
  1203
	  let val pre = check_preconds thy prls where_ pbl
neuper@37906
  1204
	      val pb = foldl and_ (true, map fst pre)
neuper@38015
  1205
	    (*val _=tracing("@@@ specify_additem: Pbl Err before nxt_spec")*)
neuper@37906
  1206
	      val (p_,nxt) =
neuper@37906
  1207
	    nxt_spec Pbl pb oris (dI',pI',mI') (pbl,met) 
neuper@37906
  1208
	    (ppc,(#ppc o get_met) cmI) (dI,pI,mI);
neuper@37906
  1209
	  in ((p,p_), ((p,p_),Uistate), Error' (Error_ msg), nxt, Safe,pt) end
neuper@37906
  1210
    end;
neuper@37906
  1211
(* val sel = "#Find"; val (p,_) = p; val Add_Find' ct = nxt;
neuper@37906
  1212
   val (_,_,f,nxt',_,pt')= specify_additem sel ct (p,Met) c pt;
neuper@37906
  1213
  *)
neuper@37906
  1214
neuper@37906
  1215
(* ori
neuper@37906
  1216
val (msg,itm) = appl_add thy sel oris ppc ct;
neuper@37906
  1217
val (Cor(d,ts)) = #5 itm;
neuper@37906
  1218
map (atomty) ts;
neuper@37906
  1219
neuper@37906
  1220
pre
neuper@37906
  1221
*)
neuper@37906
  1222
neuper@37906
  1223
neuper@37906
  1224
(* val Init_Proof' (fmz,(dI',pI',mI')) = m;
neuper@37906
  1225
   specify (Init_Proof' (fmz,(dI',pI',mI'))) e_pos' [] EmptyPtree;
neuper@37906
  1226
   *)
neuper@37906
  1227
fun specify (Init_Proof' (fmz,(dI',pI',mI'))) (_:pos') (_:cid) (_:ptree)= 
neuper@37906
  1228
  let          (* either """"""""""""""" all empty or complete *)
neuper@37906
  1229
    val thy = assoc_thy dI';
neuper@37906
  1230
    val oris = if dI' = e_domID orelse pI' = e_pblID then ([]:ori list)
neuper@37906
  1231
	       else prep_ori fmz thy ((#ppc o get_pbt) pI');
neuper@37906
  1232
    val (pt,c) = cappend_problem e_ptree [] e_istate (fmz,(dI',pI',mI'))
neuper@37906
  1233
				 (oris,(dI',pI',mI'),e_term);
neuper@37906
  1234
    val {ppc,prls,where_,...} = get_pbt pI'
neuper@37906
  1235
    (*val pbl = init_pbl ppc;  WN.9.03: done in Model/Refine_Problem
neuper@37906
  1236
    val pt = update_pbl pt [] pbl;
neuper@37906
  1237
    val pre = check_preconds thy prls where_ pbl
neuper@37906
  1238
    val pb = foldl and_ (true, map fst pre)*)
neuper@37906
  1239
    val (pbl, pre, pb) = ([], [], false)
neuper@37906
  1240
  in case mI' of
neuper@37906
  1241
	 ["no_met"] => 
neuper@37906
  1242
	 (([],Pbl), (([],Pbl),Uistate),
neuper@37906
  1243
	  Form' (PpcKF (0,EdUndef,(length []),Nundef,
neuper@37906
  1244
			(Problem [], itms2itemppc (assoc_thy dI') pbl pre))),
neuper@37906
  1245
	  Refine_Tacitly pI', Safe,pt)
neuper@37906
  1246
       | _ => 
neuper@37906
  1247
	 (([],Pbl), (([],Pbl),Uistate),
neuper@37906
  1248
	  Form' (PpcKF (0,EdUndef,(length []),Nundef,
neuper@37906
  1249
			(Problem [], itms2itemppc (assoc_thy dI') pbl pre))),
neuper@37906
  1250
	  Model_Problem,
neuper@37906
  1251
	  Safe,pt)
neuper@37906
  1252
  end
neuper@37906
  1253
  (*ONLY for STARTING modeling phase*)
neuper@37906
  1254
  | specify (Model_Problem' (_,pbl,met)) (pos as (p,p_)) c pt =
neuper@37906
  1255
  let (* val (Model_Problem' (_,pbl), pos as (p,p_)) = (m, (p,p_));
neuper@37906
  1256
         *)
neuper@37906
  1257
    val (PblObj{origin=(oris,(dI',pI',mI'),_), spec=(dI,_,_),...}) = 
neuper@37906
  1258
	get_obj I pt p
neuper@37906
  1259
    val thy' = if dI = e_domID then dI' else dI
neuper@37906
  1260
    val thy = assoc_thy thy'
neuper@37906
  1261
    val {ppc,prls,where_,...} = get_pbt pI'
neuper@37906
  1262
    val pre = check_preconds thy prls where_ pbl
neuper@37906
  1263
    val pb = foldl and_ (true, map fst pre)
neuper@37906
  1264
    val ((p,_),_,_,pt) = 
neuper@37906
  1265
	generate1 thy (Model_Problem'([],pbl,met)) Uistate pos pt
neuper@37906
  1266
    val (_,nxt) = nxt_spec Pbl pb oris (dI',pI',mI') (pbl,met) 
neuper@37906
  1267
		(ppc,(#ppc o get_met) mI') (dI',pI',mI');
neuper@37906
  1268
  in ((p,Pbl), ((p,p_),Uistate),
neuper@37906
  1269
      Form' (PpcKF (0,EdUndef,(length p),Nundef,
neuper@37906
  1270
		    (Problem pI', itms2itemppc (assoc_thy dI') pbl pre))),
neuper@37906
  1271
      nxt, Safe, pt) end
neuper@37906
  1272
neuper@37906
  1273
(*. called only if no_met is specified .*)     
neuper@37906
  1274
  | specify (Refine_Tacitly' (pI,pIre,_,_,_)) (pos as (p,_)) c pt =
neuper@37906
  1275
  let (* val Refine_Tacitly' (pI,pIre,_,_,_) = m;
neuper@37906
  1276
         *)
neuper@37906
  1277
    val (PblObj{origin=(oris,(dI',pI',mI'),_), meth=met, ...}) = 
neuper@37906
  1278
	get_obj I pt p;
neuper@37906
  1279
    val {prls,met,ppc,thy,where_,...} = get_pbt pIre
neuper@37906
  1280
    (*val pbl = init_pbl ppc --- Model_Problem recognizes probl=[]*)
neuper@37906
  1281
    (*val pt = update_pbl pt p pbl;
neuper@37906
  1282
    val pt = update_orispec pt p 
neuper@37906
  1283
		(string_of_thy thy, pIre, 
neuper@37906
  1284
		 if length met = 0 then e_metID else hd met);*)
neuper@37906
  1285
    val (domID, metID) = (string_of_thy thy, 
neuper@37906
  1286
		      if length met = 0 then e_metID else hd met)
neuper@37906
  1287
    val ((p,_),_,_,pt) = 
neuper@37906
  1288
	generate1 thy (Refine_Tacitly'(pI,pIre,domID,metID,(*pbl*)[])) 
neuper@37906
  1289
		  Uistate pos pt
neuper@37906
  1290
    (*val pre = check_preconds thy prls where_ pbl
neuper@37906
  1291
    val pb = foldl and_ (true, map fst pre)*)
neuper@37906
  1292
    val (pbl, pre, pb) = ([], [], false)
neuper@37906
  1293
  in ((p,Pbl), (pos,Uistate),
neuper@37906
  1294
      Form' (PpcKF (0,EdUndef,(length p),Nundef,
neuper@37906
  1295
		    (Problem pIre, itms2itemppc (assoc_thy dI') pbl pre))),
neuper@37906
  1296
      Model_Problem, Safe, pt) end
neuper@37906
  1297
neuper@37906
  1298
  | specify (Refine_Problem' (rfd as (pI,_))) pos c pt =
neuper@37906
  1299
    let val (pos,_,_,pt) = generate1 (assoc_thy "Isac.thy") 
neuper@37906
  1300
				     (Refine_Problem' rfd) Uistate pos pt
neuper@37906
  1301
    in (pos(*p,Pbl*), (pos(*p,Pbl*),Uistate), Problems (RefinedKF rfd), 
neuper@37906
  1302
	Model_Problem, Safe, pt) end
neuper@37906
  1303
neuper@37906
  1304
(* val (Specify_Problem' (pI, (ok, (itms, pre)))) = nxt; val (p,_) = p;
neuper@37906
  1305
   val (Specify_Problem' (pI, (ok, (itms, pre)))) = m; val (p,_) = p;
neuper@37906
  1306
   *)
neuper@37906
  1307
  | specify (Specify_Problem' (pI, (ok, (itms, pre)))) (pos as (p,_)) c pt =
neuper@37906
  1308
  let val (PblObj {origin=(oris,(dI',pI',mI'),_), spec=(dI,_,mI), 
neuper@37906
  1309
		   meth=met, ...}) = get_obj I pt p;
neuper@37906
  1310
    (*val pt = update_pbl pt p itms;
neuper@37906
  1311
    val pt = update_pblID pt p pI;*)
neuper@37934
  1312
    val thy = assoc_thy dI
neuper@37906
  1313
    val ((p,Pbl),_,_,pt)= 
neuper@37906
  1314
	generate1 thy (Specify_Problem' (pI, (ok, (itms, pre)))) Uistate pos pt
neuper@37906
  1315
    val dI'' = assoc_thy (if dI=e_domID then dI' else dI);
neuper@37906
  1316
    val mI'' = if mI=e_metID then mI' else mI;
neuper@38015
  1317
  (*val _=tracing("@@@ specify (Specify_Problem) before nxt_spec")*)
neuper@37906
  1318
    val (_,nxt) = nxt_spec Pbl ok oris (dI',pI',mI') (itms, met) 
neuper@37906
  1319
		((#ppc o get_pbt) pI,(#ppc o get_met) mI'') (dI,pI,mI);
neuper@37906
  1320
  in ((p,Pbl), (pos,Uistate),
neuper@37906
  1321
      Form' (PpcKF (0,EdUndef,(length p),Nundef,
neuper@37906
  1322
		    (Problem pI, itms2itemppc dI'' itms pre))),
neuper@37906
  1323
      nxt, Safe, pt) end    
neuper@37906
  1324
(* val Specify_Method' mID = nxt; val (p,_) = p;
neuper@37906
  1325
   val Specify_Method' mID = m;
neuper@37906
  1326
   specify (Specify_Method' mID) (p,p_) c pt;
neuper@37906
  1327
   *)
neuper@37906
  1328
  | specify (Specify_Method' (mID,_,_)) (pos as (p,_)) c pt =
neuper@37906
  1329
  let val (PblObj {origin=(oris,(dI',pI',mI'),_), probl=pbl, spec=(dI,pI,mI), 
neuper@37906
  1330
		   meth=met, ...}) = get_obj I pt p;
neuper@37906
  1331
    val {ppc,pre,prls,...} = get_met mID
neuper@37906
  1332
    val thy = assoc_thy dI
neuper@37906
  1333
    val oris = add_field' thy ppc oris;
neuper@37906
  1334
    (*val pt = update_oris pt p oris; 20.3.02: repl. "#undef"*)
neuper@37906
  1335
    val dI'' = if dI=e_domID then dI' else dI;
neuper@37906
  1336
    val pI'' = if pI = e_pblID then pI' else pI;
neuper@37906
  1337
    val met = if met=[] then pbl else met;
neuper@37906
  1338
    val (ok, (itms, pre')) = match_itms_oris thy met (ppc,pre,prls ) oris;
neuper@37906
  1339
    (*val pt = update_met pt p itms;
neuper@37906
  1340
    val pt = update_metID pt p mID*)
neuper@37906
  1341
    val (pos,_,_,pt)= 
neuper@37906
  1342
	generate1 thy (Specify_Method' (mID, oris, itms)) Uistate pos pt
neuper@38015
  1343
    (*val _=tracing("@@@ specify (Specify_Method) before nxt_spec")*)
neuper@37906
  1344
    val (_,nxt) = nxt_spec Met (*ok*)true oris (dI',pI',mI') (pbl, itms) 
neuper@37906
  1345
		((#ppc o get_pbt) pI'',ppc) (dI'',pI'',mID);
neuper@37906
  1346
  in (pos, (pos,Uistate),
neuper@37906
  1347
      Form' (PpcKF (0,EdUndef,(length p),Nundef,
neuper@37906
  1348
		    (Method mID, itms2itemppc (assoc_thy dI'') itms pre'))),
neuper@37906
  1349
      nxt, Safe, pt) end    
neuper@37906
  1350
(* val Add_Find' ct = nxt; val sel = "#Find"; 
neuper@37906
  1351
   *)
neuper@37906
  1352
  | specify (Add_Given' ct) p c pt = specify_additem "#Given" ct p c pt
neuper@37906
  1353
  | specify (Add_Find'  ct) p c pt = specify_additem "#Find"  ct p c pt
neuper@37906
  1354
  | specify (Add_Relation' ct) p c pt=specify_additem"#Relate"ct p c pt
neuper@37906
  1355
(* val Specify_Theory' domID = m;
neuper@37906
  1356
   val (Specify_Theory' domID, (p,p_)) = (m, pos);
neuper@37906
  1357
   *)
neuper@37906
  1358
  | specify (Specify_Theory' domID) (pos as (p,p_)) c pt =
neuper@37906
  1359
    let val p_ = case p_ of Met => Met | _ => Pbl
neuper@37906
  1360
      val thy = assoc_thy domID;
neuper@37906
  1361
      val (PblObj{origin=(oris,(dI',pI',mI'),_), meth=met,
neuper@37906
  1362
		  probl=pbl, spec=(dI,pI,mI),...}) = get_obj I pt p;
neuper@37906
  1363
      val mppc = case p_ of Met => met | _ => pbl;
neuper@37906
  1364
      val cpI = if pI = e_pblID then pI' else pI;
neuper@37906
  1365
      val {prls=per,ppc,where_=pwh,...} = get_pbt cpI
neuper@37906
  1366
      val cmI = if mI = e_metID then mI' else mI;
neuper@37906
  1367
      val {prls=mer,ppc=mpc,pre=mwh,...} = get_met cmI
neuper@37906
  1368
      val pre = 
neuper@37906
  1369
	  case p_ of
neuper@37906
  1370
	      Met => (check_preconds thy mer mwh met)
neuper@37906
  1371
	    | _ => (check_preconds thy per pwh pbl)
neuper@37906
  1372
      val pb = foldl and_ (true, map fst pre)
neuper@37906
  1373
    in if domID = dI
neuper@37906
  1374
       then let 
neuper@38015
  1375
	 (*val _=tracing("@@@ specify (Specify_Theory) THEN before nxt_spec")*)
neuper@37906
  1376
           val (p_,nxt) = nxt_spec p_ pb oris (dI',pI',mI') 
neuper@37906
  1377
				   (pbl,met) (ppc,mpc) (dI,pI,mI);
neuper@37906
  1378
	      in ((p,p_), (pos,Uistate), 
neuper@37906
  1379
		  Form'(PpcKF (0,EdUndef,(length p), Nundef,
neuper@37906
  1380
			       (header p_ pI cmI, itms2itemppc thy mppc pre))),
neuper@37906
  1381
		  nxt,Safe,pt) end
neuper@37906
  1382
       else (*FIXME: check ppc wrt. (new!) domID ..? still parsable?*)
neuper@37906
  1383
	 let 
neuper@37906
  1384
	   (*val pt = update_domID pt p domID;11.8.03*)
neuper@37906
  1385
	   val ((p,p_),_,_,pt) = generate1 thy (Specify_Theory' domID) 
neuper@37906
  1386
					   Uistate (p,p_) pt
neuper@38015
  1387
	 (*val _=tracing("@@@ specify (Specify_Theory) ELSE before nxt_spec")*)
neuper@37906
  1388
	   val (p_,nxt) = nxt_spec p_ pb oris (dI',pI',mI') (pbl,met) 
neuper@37906
  1389
				   (ppc,mpc) (domID,pI,mI);
neuper@37906
  1390
	 in ((p,p_), (pos,Uistate), 
neuper@37906
  1391
	     Form' (PpcKF (0, EdUndef, (length p),Nundef,
neuper@37906
  1392
			   (header p_ pI cmI, itms2itemppc thy mppc pre))),
neuper@37906
  1393
	     nxt, Safe,pt) end
neuper@37906
  1394
    end
neuper@37906
  1395
(* itms2itemppc thy [](*mpc*) pre
neuper@37906
  1396
   *)
neuper@37906
  1397
  | specify m' _ _ _ = 
neuper@38031
  1398
    error ("specify: not impl. for "^tac_2str m');
neuper@37906
  1399
neuper@37906
  1400
(* val (sel, Add_Given ct, ptp as (pt,(p,Pbl))) = ("#Given", tac, ptp);
neuper@37906
  1401
   val (sel, Add_Find  ct, ptp as (pt,(p,Pbl))) = ("#Find", tac, ptp);
neuper@37906
  1402
   *)
neuper@37906
  1403
fun nxt_specif_additem sel ct (ptp as (pt,(p,Pbl))) = 
neuper@37906
  1404
    let
neuper@37906
  1405
      val (PblObj{meth=met,origin=(oris,(dI',pI',_),_),
neuper@37906
  1406
		  probl=pbl,spec=(dI,pI,_),...}) = get_obj I pt p;
neuper@37906
  1407
      val thy = if dI = e_domID then assoc_thy dI' else assoc_thy dI;
neuper@37906
  1408
      val cpI = if pI = e_pblID then pI' else pI;
neuper@37906
  1409
    in case appl_add thy sel oris pbl ((#ppc o get_pbt) cpI) ct of
neuper@37906
  1410
	   Add itm (*..union old input *) =>
neuper@37906
  1411
(* val Add itm = appl_add thy sel oris pbl ppc ct;
neuper@37906
  1412
   *)
neuper@37906
  1413
	   let
neuper@38015
  1414
	       (*val _=tracing("###nxt_specif_additem: itm= "^(itm2str_ itm));*)
neuper@37906
  1415
	       val pbl' = insert_ppc thy itm pbl
neuper@37906
  1416
	       val (tac,tac_) = 
neuper@37906
  1417
		   case sel of
neuper@37906
  1418
		       "#Given" => (Add_Given    ct, Add_Given'   (ct, pbl'))
neuper@37906
  1419
		     | "#Find"  => (Add_Find     ct, Add_Find'    (ct, pbl'))
neuper@37906
  1420
		     | "#Relate"=> (Add_Relation ct, Add_Relation'(ct, pbl'))
neuper@37906
  1421
	       val ((p,Pbl),c,_,pt') = 
neuper@37906
  1422
		   generate1 thy tac_ Uistate (p,Pbl) pt
neuper@37906
  1423
	   in ([(tac,tac_,((p,Pbl),Uistate))], c, (pt',(p,Pbl))):calcstate' end
neuper@37906
  1424
	       
neuper@37906
  1425
	 | Err msg => 
neuper@37906
  1426
	   (*TODO.WN03 pass error-msgs to the frontend..
neuper@37906
  1427
             FIXME ..and dont abuse a tactic for that purpose*)
neuper@37906
  1428
	   ([(Tac msg,
neuper@37934
  1429
	      Tac_ (theory "Pure", msg,msg,msg),
neuper@37906
  1430
	      (e_pos', e_istate))], [], ptp) 
neuper@37906
  1431
    end
neuper@37906
  1432
neuper@37906
  1433
(* val sel = "#Find"; val (p,_) = p; val Add_Find' ct = nxt;
neuper@37906
  1434
   val (_,_,f,nxt',_,pt')= nxt_specif_additem sel ct (p,Met) c pt;
neuper@37906
  1435
  *)
neuper@37906
  1436
  | nxt_specif_additem sel ct (ptp as (pt,(p,Met))) = 
neuper@37906
  1437
    let
neuper@37906
  1438
      val (PblObj{meth=met,origin=(oris,(dI',pI',mI'),_),
neuper@37906
  1439
		  probl=pbl,spec=(dI,pI,mI),...}) = get_obj I pt p;
neuper@37906
  1440
      val thy = if dI = e_domID then assoc_thy dI' else assoc_thy dI;
neuper@37906
  1441
      val cmI = if mI = e_metID then mI' else mI;
neuper@37906
  1442
    in case appl_add thy sel oris met ((#ppc o get_met) cmI) ct of
neuper@37906
  1443
      Add itm (*..union old input *) =>
neuper@37906
  1444
	let (* val Add itm = appl_add thy sel oris met (#ppc (get_met cmI)) ct;
neuper@37906
  1445
               *)
neuper@37906
  1446
	  val met' = insert_ppc thy itm met;
neuper@37906
  1447
	  val (tac,tac_) = 
neuper@37906
  1448
	      case sel of
neuper@37906
  1449
		  "#Given" => (Add_Given    ct, Add_Given'   (ct, met'))
neuper@37906
  1450
		| "#Find"  => (Add_Find     ct, Add_Find'    (ct, met'))
neuper@37906
  1451
		| "#Relate"=> (Add_Relation ct, Add_Relation'(ct, met'))
neuper@37906
  1452
	  val ((p,Met),c,_,pt') = 
neuper@37906
  1453
	      generate1 thy tac_ Uistate (p,Met) pt
neuper@37906
  1454
	in ([(tac,tac_,((p,Met), Uistate))], c, (pt',(p,Met))) end
neuper@37906
  1455
neuper@37906
  1456
    | Err msg => ([(*tacis*)], [], ptp) 
neuper@37906
  1457
    (*nxt_me collects tacis until not hide; here just no progress*)
neuper@37906
  1458
    end;
neuper@37906
  1459
neuper@37906
  1460
(* ori
neuper@37906
  1461
val (msg,itm) = appl_add thy sel oris ppc ct;
neuper@37906
  1462
val (Cor(d,ts)) = #5 itm;
neuper@37906
  1463
map (atomty) ts;
neuper@37906
  1464
neuper@37906
  1465
pre
neuper@37906
  1466
*)
neuper@37906
  1467
fun ori2Coritm pbt ((i,v,f,d,ts):ori) =
neuper@37906
  1468
    (i,v,true,f, Cor ((d,ts),(((snd o snd o the o (find_first (eq1 d))) pbt) 
neuper@38031
  1469
			      handle _ => error ("ori2Coritm: dsc "^
neuper@37906
  1470
						term2str d^
neuper@37906
  1471
						"in ori, but not in pbt")
neuper@37906
  1472
			      ,ts))):itm;
neuper@37906
  1473
fun ori2Coritm (pbt:pat list) ((i,v,f,d,ts):ori) =
neuper@37906
  1474
    ((i,v,true,f, Cor ((d,ts),((snd o snd o the o 
neuper@37906
  1475
			       (find_first (eq1 d))) pbt,ts))):itm)
neuper@37906
  1476
    handle _ => (*dsc in oris, but not in pbl pat list: keep this dsc*)
neuper@37906
  1477
    ((i,v,true,f, Cor ((d,ts),(d,ts))):itm);
neuper@37906
  1478
neuper@37906
  1479
neuper@37906
  1480
(*filter out oris which have same description in itms*)
neuper@37906
  1481
fun filter_outs oris [] = oris
neuper@37906
  1482
  | filter_outs oris (i::itms) = 
neuper@37906
  1483
    let val ors = filter_out ((curry op= ((d_in o #5) (i:itm))) o 
neuper@37906
  1484
			      (#4:ori -> term)) oris;
neuper@37906
  1485
    in filter_outs ors itms end;
neuper@37906
  1486
neuper@37934
  1487
fun memI a b = member op = a b;
neuper@37906
  1488
(*filter oris which are in pbt, too*)
neuper@37906
  1489
fun filter_pbt oris pbt =
neuper@37906
  1490
    let val dscs = map (fst o snd) pbt
neuper@37906
  1491
    in filter ((memI dscs) o (#4: ori -> term)) oris end;
neuper@37906
  1492
neuper@37906
  1493
(*.combine itms from pbl + met and complete them wrt. pbt.*)
neuper@37906
  1494
(*FIXXXME.WN031205 complete_metitms doesnt handle incorrect itms !*)
neuper@37936
  1495
local infix mem;
neuper@37934
  1496
fun x mem [] = false
neuper@37934
  1497
  | x mem (y :: ys) = x = y orelse x mem ys;
neuper@37934
  1498
in 
neuper@37906
  1499
fun complete_metitms (oris:ori list) (pits:itm list) (mits:itm list) met = 
neuper@37906
  1500
(* val met = (#ppc o get_met) ["DiffApp","max_by_calculus"];
neuper@37906
  1501
   *)
neuper@37906
  1502
    let val vat = max_vt pits;
neuper@37906
  1503
        val itms = pits @ 
neuper@37906
  1504
		   (filter ((curry (op mem) vat) o (#2:itm -> int list)) mits);
neuper@37906
  1505
	val ors = filter ((curry (op mem) vat) o (#2:ori -> int list)) oris;
neuper@37906
  1506
        val os = filter_outs ors itms;
neuper@37906
  1507
    (*WN.12.03?: does _NOT_ add itms from met ?!*)
neuper@37934
  1508
    in itms @ (map (ori2Coritm met) os) end
neuper@37934
  1509
end;
neuper@37906
  1510
neuper@37906
  1511
neuper@37906
  1512
neuper@37906
  1513
(*.complete model and guard of a calc-head .*)
neuper@37936
  1514
local infix mem;
neuper@37934
  1515
fun x mem [] = false
neuper@37934
  1516
  | x mem (y :: ys) = x = y orelse x mem ys;
neuper@37934
  1517
in 
neuper@37906
  1518
fun complete_mod_ (oris, mpc, ppc, probl) =
neuper@37906
  1519
    let	val pits = filter_out ((curry op= false) o (#3: itm -> bool)) probl
neuper@37906
  1520
	val vat = if probl = [] then 1 else max_vt probl
neuper@37906
  1521
	val pors = filter ((curry (op mem) vat) o (#2:ori -> int list)) oris
neuper@37906
  1522
	val pors = filter_outs pors pits (*which are in pbl already*)
neuper@37906
  1523
        val pors = (filter_pbt pors ppc) (*which are in pbt, too*)
neuper@37906
  1524
neuper@37906
  1525
	val pits = pits @ (map (ori2Coritm ppc) pors)
neuper@37906
  1526
	val mits = complete_metitms oris pits [] mpc
neuper@37934
  1527
    in (pits, mits) end
neuper@37934
  1528
end;
neuper@37906
  1529
neuper@37906
  1530
fun some_spec ((odI, opI, omI):spec) ((dI, pI, mI):spec) =
neuper@37906
  1531
    (if dI = e_domID then odI else dI,
neuper@37906
  1532
     if pI = e_pblID then opI else pI,
neuper@37906
  1533
     if mI = e_metID then omI else mI):spec;
neuper@37906
  1534
neuper@37906
  1535
neuper@37906
  1536
(*.find a next applicable tac (for calcstate) and update ptree
neuper@37906
  1537
 (for ev. finding several more tacs due to hide).*)
neuper@37906
  1538
(*FIXXXME: unify ... fun nxt_specif = nxt_spec + applicable_in + specify !!*)
neuper@37906
  1539
(*WN.24.10.03        ~~~~~~~~~~~~~~   -> tac     -> tac_      -> -"- as arg*)
neuper@37906
  1540
(*WN.24.10.03        fun nxt_solv   = ...................................??*)
neuper@37906
  1541
fun nxt_specif (tac as Model_Problem) (pt, pos as (p,p_)) =
neuper@37906
  1542
  let
neuper@37906
  1543
    val (PblObj{origin=(oris,ospec,_),probl,spec,...}) = get_obj I pt p
neuper@37906
  1544
    val (dI,pI,mI) = some_spec ospec spec
neuper@37934
  1545
    val thy = assoc_thy dI
neuper@37906
  1546
    val mpc = (#ppc o get_met) mI (*just for reuse complete_mod_*)
neuper@37906
  1547
    val {cas,ppc,...} = get_pbt pI
neuper@37906
  1548
    val pbl = init_pbl ppc (*fill in descriptions*)
neuper@37906
  1549
    (*--------------if you think, this should be done by the Dialog 
neuper@37906
  1550
     in the java front-end, search there for WN060225-modelProblem----*)
neuper@37926
  1551
    val (pbl,met) = case cas of NONE => (pbl,[])
neuper@37906
  1552
			    | _ => complete_mod_ (oris, mpc, ppc, probl)
neuper@37906
  1553
    (*----------------------------------------------------------------*)
neuper@37906
  1554
    val tac_ = Model_Problem' (pI, pbl, met)
neuper@37906
  1555
    val (pos,c,_,pt) = generate1 thy tac_ Uistate pos pt
neuper@37906
  1556
  in ([(tac,tac_, (pos, Uistate))], c, (pt,pos)):calcstate' end
neuper@37906
  1557
neuper@37906
  1558
(* val Add_Find ct = tac;
neuper@37906
  1559
   *)
neuper@37906
  1560
  | nxt_specif (Add_Given ct) ptp = nxt_specif_additem "#Given" ct ptp
neuper@37906
  1561
  | nxt_specif (Add_Find  ct) ptp = nxt_specif_additem "#Find"  ct ptp
neuper@37906
  1562
  | nxt_specif (Add_Relation ct) ptp = nxt_specif_additem"#Relate" ct ptp
neuper@37906
  1563
neuper@37906
  1564
(*. called only if no_met is specified .*)     
neuper@37906
  1565
  | nxt_specif (Refine_Tacitly pI) (ptp as (pt, pos as (p,_))) =
neuper@37906
  1566
    let val (PblObj {origin = (oris, (dI,_,_),_), ...}) = get_obj I pt p
neuper@37906
  1567
	val opt = refine_ori oris pI
neuper@37906
  1568
    in case opt of
neuper@37926
  1569
	   SOME pI' => 
neuper@37906
  1570
	   let val {met,ppc,...} = get_pbt pI'
neuper@37906
  1571
	       val pbl = init_pbl ppc
neuper@37906
  1572
	       (*val pt = update_pbl pt p pbl ..done by Model_Problem*)
neuper@37906
  1573
	       val mI = if length met = 0 then e_metID else hd met
neuper@37934
  1574
               val thy = assoc_thy dI
neuper@37906
  1575
	       val (pos,c,_,pt) = 
neuper@37906
  1576
		   generate1 thy (Refine_Tacitly' (pI,pI',dI,mI,(*pbl*)[])) 
neuper@37906
  1577
			     Uistate pos pt
neuper@37906
  1578
	   in ([(Refine_Tacitly pI, Refine_Tacitly' (pI,pI',dI,mI,(*pbl*)[]),
neuper@37906
  1579
		 (pos, Uistate))], c, (pt,pos)) end
neuper@37926
  1580
	 | NONE => ([], [], ptp)
neuper@37906
  1581
    end
neuper@37906
  1582
neuper@37906
  1583
  | nxt_specif (Refine_Problem pI) (ptp as (pt, pos as (p,_))) =
neuper@37906
  1584
    let val (PblObj {origin=(_,(dI,_,_),_),spec=(dI',_,_),
neuper@37906
  1585
		     probl, ...}) = get_obj I pt p
neuper@37906
  1586
	val thy = if dI' = e_domID then dI else dI'
neuper@37906
  1587
    in case refine_pbl (assoc_thy thy) pI probl of
neuper@37926
  1588
	   NONE => ([], [], ptp)
neuper@37926
  1589
	 | SOME (rfd as (pI',_)) => 
neuper@37906
  1590
	   let val (pos,c,_,pt) = 
neuper@37906
  1591
		   generate1 (assoc_thy thy) 
neuper@37906
  1592
			     (Refine_Problem' rfd) Uistate pos pt
neuper@37906
  1593
	    in ([(Refine_Problem pI, Refine_Problem' rfd,
neuper@37906
  1594
			    (pos, Uistate))], c, (pt,pos)) end
neuper@37906
  1595
    end
neuper@37906
  1596
neuper@37906
  1597
  | nxt_specif (Specify_Problem pI) (pt, pos as (p,_)) =
neuper@37906
  1598
    let val (PblObj {origin=(oris,(dI,_,_),_),spec=(dI',pI',_),
neuper@37906
  1599
		     probl, ...}) = get_obj I pt p;
neuper@37906
  1600
	val thy = assoc_thy (if dI' = e_domID then dI else dI');
neuper@37906
  1601
        val {ppc,where_,prls,...} = get_pbt pI
neuper@37906
  1602
	val pbl as (_,(itms,_)) = 
neuper@37906
  1603
	    if pI'=e_pblID andalso pI=e_pblID
neuper@37906
  1604
	    then (false, (init_pbl ppc, []))
neuper@37906
  1605
	    else match_itms_oris thy probl (ppc,where_,prls) oris(*FIXXXXXME?*)
neuper@37906
  1606
	(*FIXXXME~~~~~~~~~~~~~~~: take pbl and compare with new pI WN.8.03*)
neuper@37906
  1607
	val ((p,Pbl),c,_,pt)= 
neuper@37906
  1608
	    generate1 thy (Specify_Problem' (pI, pbl)) Uistate pos pt
neuper@37906
  1609
    in ([(Specify_Problem pI, Specify_Problem' (pI, pbl),
neuper@37906
  1610
		    (pos,Uistate))], c, (pt,pos)) end
neuper@37906
  1611
neuper@37906
  1612
  (*transfers oris (not required in pbl) to met-model for script-env
neuper@37906
  1613
    FIXME.WN.8.03: application of several mIDs to SAME model?*)
neuper@37906
  1614
  | nxt_specif (Specify_Method mID) (ptp as (pt, pos as (p,_))) = 
neuper@37906
  1615
  let val (PblObj {origin=(oris,(dI',pI',mI'),_), probl=pbl, spec=(dI,pI,mI), 
neuper@37906
  1616
		   meth=met, ...}) = get_obj I pt p;
neuper@37906
  1617
    val {ppc,pre,prls,...} = get_met mID
neuper@37906
  1618
    val thy = assoc_thy dI
neuper@37906
  1619
    val oris = add_field' thy ppc oris;
neuper@37906
  1620
    val dI'' = if dI=e_domID then dI' else dI;
neuper@37906
  1621
    val pI'' = if pI = e_pblID then pI' else pI;
neuper@37906
  1622
    val met = if met=[] then pbl else met;(*WN0602 what if more itms in met?*)
neuper@37906
  1623
    val (ok, (itms, pre')) = match_itms_oris thy met (ppc,pre,prls ) oris;
neuper@37906
  1624
    val (pos,c,_,pt)= 
neuper@37906
  1625
	generate1 thy (Specify_Method' (mID, oris, itms)) Uistate pos pt
neuper@37906
  1626
  in ([(Specify_Method mID, Specify_Method' (mID, oris, itms),
neuper@37906
  1627
		  (pos,Uistate))], c, (pt,pos)) end    
neuper@37906
  1628
neuper@37906
  1629
  | nxt_specif (Specify_Theory dI) (pt, pos as (p,Pbl)) =
neuper@37906
  1630
    let val (dI',_,_) = get_obj g_spec pt p
neuper@37906
  1631
	val (pos,c,_,pt) = 
neuper@37906
  1632
	    generate1 (assoc_thy "Isac.thy") (Specify_Theory' dI) 
neuper@37906
  1633
		      Uistate pos pt
neuper@37906
  1634
    in  (*FIXXXME: check if pbl can still be parsed*)
neuper@37906
  1635
	([(Specify_Theory dI, Specify_Theory' dI, (pos,Uistate))], c,
neuper@37906
  1636
	 (pt, pos)) end
neuper@37906
  1637
neuper@37906
  1638
  | nxt_specif (Specify_Theory dI) (pt, pos as (p,Met)) =
neuper@37906
  1639
    let val (dI',_,_) = get_obj g_spec pt p
neuper@37906
  1640
	val (pos,c,_,pt) = 
neuper@37906
  1641
	    generate1 (assoc_thy "Isac.thy") (Specify_Theory' dI) 
neuper@37906
  1642
		      Uistate pos pt
neuper@37906
  1643
    in  (*FIXXXME: check if met can still be parsed*)
neuper@37906
  1644
	([(Specify_Theory dI, Specify_Theory' dI, (pos,Uistate))], c,
neuper@37906
  1645
	 (pt, pos)) end
neuper@37906
  1646
neuper@37906
  1647
  | nxt_specif m' _ = 
neuper@38031
  1648
    error ("nxt_specif: not impl. for "^tac2str m');
neuper@37906
  1649
neuper@37906
  1650
(*.get the values from oris; handle the term list w.r.t. penv.*)
neuper@37934
  1651
neuper@37936
  1652
local infix mem;
neuper@37934
  1653
fun x mem [] = false
neuper@37934
  1654
  | x mem (y :: ys) = x = y orelse x mem ys;
neuper@37934
  1655
in 
neuper@37906
  1656
fun vals_of_oris oris =
neuper@37906
  1657
    ((map (mkval' o (#5:ori -> term list))) o 
neuper@37934
  1658
     (filter ((curry (op mem) 1) o (#2:ori -> int list)))) oris
neuper@37934
  1659
end;
neuper@37906
  1660
neuper@37906
  1661
neuper@37906
  1662
neuper@37906
  1663
(*.create a calc-tree with oris via an cas.refined pbl.*)
neuper@37906
  1664
fun nxt_specify_init_calc (([],(dI,pI,mI)): fmz) =
neuper@37906
  1665
(* val ([],(dI,pI,mI)) = (fmz, sp);
neuper@37906
  1666
   *)
neuper@37906
  1667
    if pI <> [] then (*comes from pbl-browser*)
neuper@37906
  1668
	let val {cas,met,ppc,thy,...} = get_pbt pI
neuper@37906
  1669
	    val dI = if dI = "" then theory2theory' thy else dI
neuper@37906
  1670
	    val thy = assoc_thy dI
neuper@37906
  1671
	    val mI = if mI = [] then hd met else mI
neuper@37926
  1672
	    val hdl = case cas of NONE => pblterm dI pI | SOME t => t
neuper@37906
  1673
	    val (pt,_) = cappend_problem e_ptree [] e_istate ([], (dI,pI,mI))
neuper@37906
  1674
					 ([], (dI,pI,mI), hdl)
neuper@37906
  1675
	    val pt = update_spec pt [] (dI,pI,mI)
neuper@37906
  1676
	    val pits = init_pbl' ppc
neuper@37906
  1677
	    val pt = update_pbl pt [] pits
neuper@37906
  1678
	in ((pt,([],Pbl)), []): calcstate end
neuper@37906
  1679
    else if mI <> [] then (*comes from met-browser*)
neuper@37906
  1680
	let val {ppc,...} = get_met mI
neuper@37906
  1681
	    val dI = if dI = "" then "Isac.thy" else dI
neuper@37906
  1682
	    val thy = assoc_thy dI
neuper@37906
  1683
	    val (pt,_) = cappend_problem e_ptree [] e_istate ([], (dI,pI,mI))
neuper@37906
  1684
					 ([], (dI,pI,mI), e_term(*FIXME met*))
neuper@37906
  1685
	    val pt = update_spec pt [] (dI,pI,mI)
neuper@37906
  1686
	    val mits = init_pbl' ppc
neuper@37906
  1687
	    val pt = update_met pt [] mits
neuper@37906
  1688
	in ((pt,([],Met)), []) end
neuper@37906
  1689
    else (*completely new example*)
neuper@37906
  1690
	let val (pt,_) = cappend_problem e_ptree [] e_istate ([], e_spec)
neuper@37906
  1691
					 ([], e_spec, e_term)
neuper@37906
  1692
	in ((pt,([],Pbl)), []) end
neuper@37906
  1693
(* val (fmz, (dI,pI,mI)) = (fmz, sp);
neuper@37906
  1694
   *)
neuper@37906
  1695
  | nxt_specify_init_calc (fmz:fmz_,(dI,pI,mI):spec) = 
neuper@37906
  1696
    let            (* either """"""""""""""" all empty or complete *)
neuper@37906
  1697
	val thy = assoc_thy dI
neuper@37906
  1698
	val (pI, pors, mI) = 
neuper@37906
  1699
	    if mI = ["no_met"] 
neuper@37906
  1700
	    then let val pors = prep_ori fmz thy ((#ppc o get_pbt) pI)
neuper@37906
  1701
		     val pI' = refine_ori' pors pI;
neuper@37906
  1702
		 in (pI', pors (*refinement over models with diff.prec only*), 
neuper@37906
  1703
		     (hd o #met o get_pbt) pI') end
neuper@37906
  1704
	    else (pI, prep_ori fmz thy ((#ppc o get_pbt) pI), mI)
neuper@37906
  1705
	val {cas,ppc,thy=thy',...} = get_pbt pI (*take dI from _refined_ pbl*)
neuper@37906
  1706
	val dI = theory2theory' (maxthy thy thy');
neuper@37906
  1707
	val hdl = case cas of
neuper@37926
  1708
		      NONE => pblterm dI pI
neuper@37926
  1709
		    | SOME t => subst_atomic ((vars_of_pbl_' ppc) 
neuper@37906
  1710
						  ~~~ vals_of_oris pors) t
neuper@37906
  1711
    val (pt,_) = cappend_problem e_ptree [] e_istate (fmz,(dI,pI,mI))
neuper@37906
  1712
				 (pors,(dI,pI,mI),hdl)
neuper@37906
  1713
    (*val pbl = init_pbl ppc  WN.9.03: done by Model/Refine_Problem
neuper@37906
  1714
    val pt = update_pbl pt [] pbl*)
neuper@37906
  1715
  in ((pt,([],Pbl)), fst3 (nxt_specif Model_Problem (pt, ([],Pbl))))
neuper@37906
  1716
  end;
neuper@37906
  1717
neuper@37906
  1718
neuper@37906
  1719
neuper@37906
  1720
(*18.12.99*)
neuper@37906
  1721
fun get_spec_form (m:tac_) ((p,p_):pos') (pt:ptree) = 
neuper@37906
  1722
(*  case appl_spec p pt m of           /// 19.1.00
neuper@37906
  1723
    Notappl e => Error' (Error_ e)
neuper@37906
  1724
  | Appl => 
neuper@37906
  1725
*)    let val (_,_,f,_,_,_) = specify m (p,p_) [] pt
neuper@37906
  1726
      in f end;
neuper@37906
  1727
neuper@37906
  1728
neuper@37938
  1729
(*fun tag_form thy (formal, given) = cterm_of thy
neuper@37934
  1730
	      (((head_of o term_of) given) $ (term_of formal)); WN100819*)
neuper@37934
  1731
fun tag_form thy (formal, given) =
neuper@37934
  1732
    (let val gf = (head_of given) $ formal;
neuper@37938
  1733
         val _ = cterm_of thy gf
neuper@37934
  1734
     in gf end)
neuper@38031
  1735
    handle _ => error ("calchead.tag_form: " ^ 
neuper@37934
  1736
                             Syntax.string_of_term (thy2ctxt thy) given ^
neuper@37934
  1737
                             " .. " ^
neuper@37934
  1738
                             Syntax.string_of_term (thy2ctxt thy) formal ^
neuper@37934
  1739
                         " ..types do not match");
neuper@37906
  1740
(* val formal = (the o (parse thy)) "[R::real]";
neuper@37906
  1741
> val given = (the o (parse thy)) "fixed_values (cs::real list)";
neuper@37906
  1742
> tag_form thy (formal, given);
neuper@37906
  1743
val it = "fixed_values [R]" : cterm
neuper@37906
  1744
*)
neuper@37906
  1745
fun chktyp thy (n, fs, gs) = 
neuper@38015
  1746
  ((tracing o (Syntax.string_of_term (thy2ctxt thy)) o (nth n)) fs;
neuper@38015
  1747
   (tracing o (Syntax.string_of_term (thy2ctxt thy)) o (nth n)) gs;
neuper@37906
  1748
   tag_form thy (nth n fs, nth n gs));
neuper@37906
  1749
neuper@37906
  1750
fun chktyps thy (fs, gs) = map (tag_form thy) (fs ~~ gs);
neuper@37906
  1751
neuper@37906
  1752
(* #####################################################
neuper@37906
  1753
   find the failing item:
neuper@37906
  1754
> val n = 2;
neuper@37906
  1755
> val tag__form = chktyp (n,formals,givens);
neuper@37906
  1756
> (type_of o term_of o (nth n)) formals; 
neuper@37906
  1757
> (type_of o term_of o (nth n)) givens;
neuper@37906
  1758
> atomty ((term_of o (nth n)) formals);
neuper@37906
  1759
> atomty ((term_of o (nth n)) givens);
neuper@37906
  1760
> atomty (term_of tag__form);
neuper@37906
  1761
> use_thy"isa-98-1-HOL-plus/knowl-base/DiffAppl";
neuper@37906
  1762
 ##################################################### *)
neuper@37906
  1763
neuper@37906
  1764
(* #####################################################
neuper@37906
  1765
   testdata setup
neuper@37906
  1766
val origin = ["sqrt(9+4*x)=sqrt x + sqrt(5+x)","x::rat","(+0)"];
neuper@37906
  1767
val formals = map (the o (parse thy)) origin;
neuper@37906
  1768
neuper@37906
  1769
val given  = ["equation (lhs=rhs)",
neuper@37906
  1770
	     "bound_variable bdv",   (* TODO type *) 
neuper@37906
  1771
	     "error_bound apx"];
neuper@37906
  1772
val where_ = ["e is_root_equation_in bdv",
neuper@37906
  1773
	      "bdv is_var",
neuper@37906
  1774
	      "apx is_const_expr"];
neuper@37906
  1775
val find   = ["L::rat set"];
neuper@37906
  1776
val with_  = ["L = {bdv. || ((%x. lhs) bdv) - ((%x. rhs) bdv) || < apx}"];
neuper@37906
  1777
val chkpbl = map (the o (parse thy)) (given @ where_ @ find @ with_);
neuper@37906
  1778
val givens = map (the o (parse thy)) given;
neuper@37906
  1779
neuper@37906
  1780
val tag__forms = chktyps (formals, givens);
neuper@37906
  1781
map ((atomty) o term_of) tag__forms;
neuper@37906
  1782
 ##################################################### *)
neuper@37906
  1783
neuper@37906
  1784
neuper@37906
  1785
(* check pbltypes, announces one failure a time *)
neuper@37930
  1786
(*fun chk_vars ctppc = 
neuper@37906
  1787
  let val {Given=gi,Where=wh,Find=fi,With=wi,Relate=re} = 
neuper@37906
  1788
    appc flat (mappc (vars o term_of) ctppc)
neuper@37906
  1789
  in if (wh\\gi) <> [] then ("wh\\gi",wh\\gi)
neuper@37906
  1790
     else if (re\\(gi union fi)) <> [] 
neuper@37906
  1791
	    then ("re\\(gi union fi)",re\\(gi union fi))
neuper@37930
  1792
	  else ("ok",[]) end;*)
neuper@37930
  1793
fun chk_vars ctppc = 
neuper@37930
  1794
  let val {Given=gi,Where=wh,Find=fi,With=wi,Relate=re} = 
neuper@37934
  1795
          appc flat (mappc vars ctppc)
neuper@37932
  1796
      val chked = subtract op = gi wh
neuper@37932
  1797
  in if chked <> [] then ("wh\\gi", chked)
neuper@37930
  1798
     else let val chked = subtract op = (union op = gi fi) re
neuper@37932
  1799
          in if chked  <> []
neuper@37932
  1800
	     then ("re\\(gi union fi)", chked)
neuper@37932
  1801
	     else ("ok", []) 
neuper@37932
  1802
          end
neuper@37932
  1803
  end;
neuper@37906
  1804
neuper@37906
  1805
(* check a new pbltype: variables (Free) unbound by given, find*) 
neuper@37906
  1806
fun unbound_ppc ctppc =
neuper@37906
  1807
  let val {Given=gi,Find=fi,Relate=re,...} = 
neuper@37934
  1808
    appc flat (mappc vars ctppc)
neuper@37930
  1809
  in distinct (*re\\(gi union fi)*) 
neuper@37930
  1810
              (subtract op = (union op = gi fi) re) end;
neuper@37906
  1811
(*
neuper@37906
  1812
> val org = {Given=["[R=(R::real)]"],Where=[],
neuper@37906
  1813
	   Find=["[A::real]"],With=[],
neuper@37906
  1814
	   Relate=["[A=#2*a*b - a^^^#2, (a//#2)^^^#2 + (b//#2)^^^#2 = R^^^#2]"]
neuper@37906
  1815
	   }:string ppc;
neuper@37906
  1816
> val ctppc = mappc (the o (parse thy)) org;
neuper@37906
  1817
> unbound_ppc ctppc;
neuper@37906
  1818
val it = [("a","RealDef.real"),("b","RealDef.real")] : (string * typ) list
neuper@37906
  1819
*)
neuper@37906
  1820
neuper@37906
  1821
neuper@37906
  1822
(* f, a binary operator, is nested rightassociative *)
neuper@37906
  1823
fun foldr1 f xs =
neuper@37906
  1824
  let
neuper@37906
  1825
    fun fld f (x::[]) = x
neuper@37906
  1826
      | fld f (x::x'::[]) = f (x',x)
neuper@37906
  1827
      | fld f (x::x'::xs) = f (fld f (x'::xs),x);
neuper@37906
  1828
  in ((fld f) o rev) xs end;
neuper@37906
  1829
(*
neuper@37926
  1830
> val (SOME ct) = parse thy "[a=b,c=d,e=f]";
neuper@37938
  1831
> val ces = map (cterm_of thy) (isalist2list (term_of ct));
neuper@37906
  1832
> val conj = foldr1 HOLogic.mk_conj (isalist2list (term_of ct));
neuper@37938
  1833
> cterm_of thy conj;
neuper@37906
  1834
val it = "(a = b & c = d) & e = f" : cterm
neuper@37906
  1835
*)
neuper@37906
  1836
neuper@37906
  1837
(* f, a binary operator, is nested leftassociative *)
neuper@37906
  1838
fun foldl1 f (x::[]) = x
neuper@37906
  1839
  | foldl1 f (x::x'::[]) = f (x,x')
neuper@37906
  1840
  | foldl1 f (x::x'::xs) = f (x,foldl1 f (x'::xs));
neuper@37906
  1841
(*
neuper@37926
  1842
> val (SOME ct) = parse thy "[a=b,c=d,e=f,g=h]";
neuper@37938
  1843
> val ces = map (cterm_of thy) (isalist2list (term_of ct));
neuper@37906
  1844
> val conj = foldl1 HOLogic.mk_conj (isalist2list (term_of ct));
neuper@37938
  1845
> cterm_of thy conj;
neuper@37906
  1846
val it = "a = b & c = d & e = f & g = h" : cterm
neuper@37906
  1847
*)
neuper@37906
  1848
neuper@37906
  1849
neuper@37906
  1850
(* called only once, if a Subproblem has been located in the script*)
neuper@37906
  1851
fun nxt_model_pbl (Subproblem'((_,pblID,metID),_,_,_,_)) ptp =
neuper@37906
  1852
(* val (Subproblem'((_,pblID,metID),_,_,_,_),ptp) = (m', (pt,(p,p_)));
neuper@37906
  1853
   *)
neuper@37906
  1854
    (case metID of
neuper@37906
  1855
	 ["no_met"] => 
neuper@37906
  1856
	 (snd3 o hd o fst3) (nxt_specif (Refine_Tacitly pblID) ptp)
neuper@37906
  1857
       | _ => (snd3 o hd o fst3) (nxt_specif Model_Problem ptp))
neuper@37906
  1858
  (*all stored in tac_ itms     ^^^^^^^^^^*)
neuper@37906
  1859
  | nxt_model_pbl tac_ _ = 
neuper@38031
  1860
    error ("nxt_model_pbl: called by tac= "^tac_2str tac_);
neuper@37906
  1861
(* run subp_rooteq.sml ''
neuper@37906
  1862
   until nxt=("Subproblem",Subproblem ("SqRoot.thy",["univariate","equation"]))
neuper@37906
  1863
> val (_, (Subproblem'((_,pblID,metID),_,_,_,_),_,_,_,_,_)) =
neuper@37906
  1864
      (last_elem o drop_last) ets'';
neuper@37906
  1865
> val mst = (last_elem o drop_last) ets'';
neuper@37906
  1866
> nxt_model_pbl mst;
neuper@37906
  1867
val it = Refine_Tacitly ["univariate","equation"] : tac
neuper@37906
  1868
*)
neuper@37906
  1869
neuper@37906
  1870
(*fun eq1 d (_,(d',_)) = (d = d'); ---modspec.sml*)
neuper@37934
  1871
fun eq4 v (_,vts,_,_,_) = member op = vts v;
neuper@37906
  1872
fun eq5 (_,_,_,_,itm_) (_,_,_,d,_) = d_in itm_ = d;
neuper@37906
  1873
neuper@37906
  1874
 
neuper@37906
  1875
neuper@37906
  1876
(*
neuper@38015
  1877
  tracing (oris2str pors);
neuper@37906
  1878
neuper@38015
  1879
  tracing (itms2str_ thy pits);
neuper@38015
  1880
  tracing (itms2str_ thy mits);
neuper@37906
  1881
   *)
neuper@37906
  1882
neuper@37906
  1883
neuper@37906
  1884
(*.complete _NON_empty calc-head for autocalc (sub-)pbl from oris
neuper@37906
  1885
  + met from fmz; assumes pos on PblObj, meth = [].*)
neuper@37906
  1886
fun complete_mod (pt, pos as (p, p_):pos') =
neuper@37906
  1887
(* val (pt, (p, _)) = (pt, p);
neuper@37906
  1888
   val (pt, (p, _)) = (pt, pos);
neuper@37906
  1889
   *)
neuper@37906
  1890
    let val _= if p_ <> Pbl 
neuper@38015
  1891
	       then tracing("###complete_mod: only impl.for Pbl, called with "^
neuper@37906
  1892
			    pos'2str pos) else ()
neuper@37906
  1893
	val (PblObj{origin=(oris, ospec, hdl), probl, spec,...}) =
neuper@37906
  1894
	    get_obj I pt p
neuper@37906
  1895
	val (dI,pI,mI) = some_spec ospec spec
neuper@37906
  1896
	val mpc = (#ppc o get_met) mI
neuper@37906
  1897
	val ppc = (#ppc o get_pbt) pI
neuper@37906
  1898
	val (pits, mits) = complete_mod_ (oris, mpc, ppc, probl)
neuper@37906
  1899
        val pt = update_pblppc pt p pits
neuper@37906
  1900
	val pt = update_metppc pt p mits
neuper@37906
  1901
    in (pt, (p,Met):pos') end
neuper@37906
  1902
;
neuper@37906
  1903
(*| complete_mod (pt, pos as (p, Met):pos') =
neuper@38031
  1904
    error ("###complete_mod: only impl.for Pbl, called with "^
neuper@37906
  1905
		 pos'2str pos);*)
neuper@37906
  1906
neuper@37906
  1907
(*.complete _EMPTY_ calc-head for autocalc (sub-)pbl from oris(+met from fmz);
neuper@37906
  1908
   oris and spec (incl. pbl-refinement) given from init_calc or SubProblem .*)
neuper@37906
  1909
fun all_modspec (pt, (p,_):pos') =
neuper@37906
  1910
(* val (pt, (p,_)) = ptp;
neuper@37906
  1911
   *)
neuper@37906
  1912
    let val (PblObj{fmz=(fmz_,_), origin=(pors, spec as (dI,pI,mI), hdl),
neuper@37906
  1913
		    ...}) = get_obj I pt p;
neuper@37906
  1914
	val thy = assoc_thy dI;
neuper@37906
  1915
	val {ppc,...} = get_met mI;
neuper@37906
  1916
	val mors = prep_ori fmz_ thy ppc;
neuper@37906
  1917
        val pt = update_pblppc pt p (map (ori2Coritm ppc) pors);
neuper@37906
  1918
	val pt = update_metppc pt p (map (ori2Coritm ppc) mors);
neuper@37906
  1919
	val pt = update_spec pt p (dI,pI,mI);
neuper@37906
  1920
    in (pt, (p,Met): pos') end;
neuper@37906
  1921
neuper@37906
  1922
(*WN.12.03: use in nxt_spec, too ? what about variants ???*)
neuper@37906
  1923
fun is_complete_mod_ ([]: itm list) = false
neuper@37906
  1924
  | is_complete_mod_ itms = 
neuper@37906
  1925
    foldl and_ (true, (map #3 itms));
neuper@37906
  1926
fun is_complete_mod (pt, pos as (p, Pbl): pos') =
neuper@37906
  1927
    if (is_pblobj o (get_obj I pt)) p 
neuper@37906
  1928
    then (is_complete_mod_ o (get_obj g_pbl pt)) p
neuper@38031
  1929
    else error ("is_complete_mod: called by PrfObj at "^pos'2str pos)
neuper@37906
  1930
  | is_complete_mod (pt, pos as (p, Met)) = 
neuper@37906
  1931
    if (is_pblobj o (get_obj I pt)) p 
neuper@37906
  1932
    then (is_complete_mod_ o (get_obj g_met pt)) p
neuper@38031
  1933
    else error ("is_complete_mod: called by PrfObj at "^pos'2str pos)
neuper@37906
  1934
  | is_complete_mod (_, pos) =
neuper@38031
  1935
    error ("is_complete_mod called by "^pos'2str pos^
neuper@37906
  1936
		 " (should be Pbl or Met)");
neuper@37906
  1937
neuper@37906
  1938
(*.have (thy, pbl, met) _all_ been specified explicitly ?.*)
neuper@37906
  1939
fun is_complete_spec (pt, pos as (p,_): pos') = 
neuper@37906
  1940
    if (not o is_pblobj o (get_obj I pt)) p 
neuper@38031
  1941
    then error ("is_complete_spec: called by PrfObj at "^pos'2str pos)
neuper@37906
  1942
    else let val (dI,pI,mI) = get_obj g_spec pt p
neuper@37906
  1943
	 in dI<>e_domID andalso pI<>e_pblID andalso mI<>e_metID end;
neuper@37906
  1944
(*.complete empty items in specification from origin (pbl, met ev.refined);
neuper@37906
  1945
  assumes 'is_complete_mod'.*)
neuper@37906
  1946
fun complete_spec (pt, pos as (p,_): pos') = 
neuper@37906
  1947
    let val PblObj {origin = (_,ospec,_), spec,...} = get_obj I pt p
neuper@37906
  1948
	val pt = update_spec pt p (some_spec ospec spec)
neuper@37906
  1949
    in (pt, pos) end;
neuper@37906
  1950
neuper@37906
  1951
fun is_complete_modspec ptp = 
neuper@37906
  1952
    is_complete_mod ptp andalso is_complete_spec ptp;
neuper@37906
  1953
neuper@37906
  1954
neuper@37906
  1955
neuper@37906
  1956
neuper@37906
  1957
fun pt_model (PblObj {meth,spec,origin=(_,spec',hdl),...}) Met =
neuper@37906
  1958
(* val ((PblObj {meth,spec,origin=(_,spec',hdl),...}), Met) = (ppobj, p_);
neuper@37906
  1959
   *)
neuper@37906
  1960
    let val (_,_,metID) = get_somespec' spec spec'
neuper@37906
  1961
	val pre = 
neuper@37906
  1962
	    if metID = e_metID then []
neuper@37906
  1963
	    else let val {prls,pre=where_,...} = get_met metID
neuper@37906
  1964
		     val pre = check_preconds' prls where_ meth 0
neuper@37906
  1965
		 in pre end
neuper@37906
  1966
	val allcorrect = is_complete_mod_ meth
neuper@37906
  1967
			 andalso foldl and_ (true, (map #1 pre))
neuper@37906
  1968
    in ModSpec (allcorrect, Met, hdl, meth, pre, spec) end
neuper@37906
  1969
  | pt_model (PblObj {probl,spec,origin=(_,spec',hdl),...}) _(*Frm,Pbl*) =
neuper@37906
  1970
(* val ((PblObj {probl,spec,origin=(_,spec',hdl),...}),_) = (ppobj, p_);
neuper@37906
  1971
   *)
neuper@37906
  1972
    let val (_,pI,_) = get_somespec' spec spec'
neuper@37906
  1973
	val pre =
neuper@37906
  1974
	    if pI = e_pblID then []
neuper@37906
  1975
	    else let val {prls,where_,cas,...} = get_pbt pI
neuper@37906
  1976
		     val pre = check_preconds' prls where_ probl 0
neuper@37906
  1977
		 in pre end
neuper@37906
  1978
	val allcorrect = is_complete_mod_ probl
neuper@37906
  1979
			 andalso foldl and_ (true, (map #1 pre))
neuper@37906
  1980
    in ModSpec (allcorrect, Pbl, hdl, probl, pre, spec) end;
neuper@37906
  1981
neuper@37906
  1982
neuper@37906
  1983
fun pt_form (PrfObj {form,...}) = Form form
neuper@37906
  1984
  | pt_form (PblObj {probl,spec,origin=(_,spec',_),...}) =
neuper@37906
  1985
    let val (dI, pI, _) = get_somespec' spec spec'
neuper@37906
  1986
	val {cas,...} = get_pbt pI
neuper@37906
  1987
    in case cas of
neuper@37926
  1988
	   NONE => Form (pblterm dI pI)
neuper@37926
  1989
	 | SOME t => Form (subst_atomic (mk_env probl) t)
neuper@37906
  1990
    end;
neuper@37906
  1991
(*vvv takes the tac _generating_ the formula=result, asm ok....
neuper@37906
  1992
fun pt_result (PrfObj {result=(t,asm), tac,...}) = 
neuper@37906
  1993
    (Form t, 
neuper@37926
  1994
     if null asm then NONE else SOME asm, 
neuper@37926
  1995
     SOME tac)
neuper@37906
  1996
  | pt_result (PblObj {result=(t,asm), origin = (_,ospec,_), spec,...}) =
neuper@37906
  1997
    let val (_,_,metID) = some_spec ospec spec
neuper@37906
  1998
    in (Form t, 
neuper@37926
  1999
	if null asm then NONE else SOME asm, 
neuper@37926
  2000
	if metID = e_metID then NONE else SOME (Apply_Method metID)) end;
neuper@37906
  2001
-------------------------------------------------------------------------*)
neuper@37906
  2002
neuper@37906
  2003
neuper@37906
  2004
(*.pt_extract returns
neuper@37906
  2005
      # the formula at pos
neuper@37906
  2006
      # the tactic applied to this formula
neuper@37906
  2007
      # the list of assumptions generated at this formula
neuper@37906
  2008
	(by application of another tac to the preceding formula !)
neuper@37906
  2009
   pos is assumed to come from the frontend, ie. generated by moveDown.*)
neuper@37906
  2010
(*cannot be in ctree.sml, because ModSpec has to be calculated*)
neuper@37906
  2011
fun pt_extract (pt,([],Res)) =
neuper@37906
  2012
(* val (pt,([],Res)) = ptp;
neuper@37906
  2013
   *)
neuper@37906
  2014
    let val (f, asm) = get_obj g_result pt []
neuper@37926
  2015
    in (Form f, NONE, asm) end
neuper@37906
  2016
(* val p = [3,2];
neuper@37906
  2017
   *)
neuper@37906
  2018
  | pt_extract (pt,(p,Res)) =
neuper@37906
  2019
(* val (pt,(p,Res)) = ptp;
neuper@37906
  2020
   *)
neuper@37906
  2021
    let val (f, asm) = get_obj g_result pt p
neuper@37906
  2022
	val tac = if last_onlev pt p
neuper@37906
  2023
		  then if is_pblobj' pt (lev_up p)
neuper@37906
  2024
		       then let val (PblObj{spec=(_,pI,_),...}) = 
neuper@37906
  2025
				    get_obj I pt (lev_up p)
neuper@37926
  2026
			    in if pI = e_pblID then NONE 
neuper@37926
  2027
			       else SOME (Check_Postcond pI) end
neuper@37926
  2028
		       else SOME End_Trans (*WN0502 TODO for other branches*)
neuper@37906
  2029
		  else let val p' = lev_on p
neuper@37906
  2030
		       in if is_pblobj' pt p'
neuper@37906
  2031
			  then let val (PblObj{origin = (_,(dI,pI,_),_),...}) =
neuper@37906
  2032
				       get_obj I pt p'
neuper@37926
  2033
			       in SOME (Subproblem (dI, pI)) end
neuper@37906
  2034
			  else if f = get_obj g_form pt p'
neuper@37926
  2035
			  then SOME (get_obj g_tac pt p')
neuper@37906
  2036
			  (*because this Frm          ~~~is not on worksheet*)
neuper@37926
  2037
			  else SOME (Take (term2str (get_obj g_form pt p')))
neuper@37906
  2038
		       end
neuper@37906
  2039
    in (Form f, tac, asm) end
neuper@37906
  2040
	
neuper@37906
  2041
  | pt_extract (pt, pos as (p,p_(*Frm,Pbl*))) =
neuper@37906
  2042
(* val (pt, pos as (p,p_(*Frm,Pbl*))) = ptp;
neuper@37906
  2043
   val (pt, pos as (p,p_(*Frm,Pbl*))) = (pt, p);
neuper@37906
  2044
   *)
neuper@37906
  2045
    let val ppobj = get_obj I pt p
neuper@37906
  2046
	val f = if is_pblobj ppobj then pt_model ppobj p_
neuper@37906
  2047
		else get_obj pt_form pt p
neuper@37906
  2048
	val tac = g_tac ppobj
neuper@37926
  2049
    in (f, SOME tac, []) end;
neuper@37906
  2050
neuper@37906
  2051
neuper@37906
  2052
(**. get the formula from a ctree-node:
neuper@37906
  2053
 take form+res from PblObj and 1.PrfObj and (PrfObj after PblObj)
neuper@37906
  2054
 take res from all other PrfObj's .**)
neuper@37906
  2055
(*designed for interSteps, outcommented 04 in favour of calcChangedEvent*)
neuper@37906
  2056
fun formres p (Nd (PblObj {origin = (_,_, h), result = (r, _),...}, _)) =
neuper@37906
  2057
    [("headline", (p, Frm), h), 
neuper@37906
  2058
     ("stepform", (p, Res), r)]
neuper@37906
  2059
  | formres p (Nd (PrfObj {form, result = (r, _),...}, _)) = 
neuper@37906
  2060
    [("stepform", (p, Frm), form), 
neuper@37906
  2061
     ("stepform", (p, Res), r)];
neuper@37906
  2062
neuper@37906
  2063
fun form p (Nd (PrfObj {result = (r, _),...}, _)) = 
neuper@37906
  2064
    [("stepform", (p, Res), r)]
neuper@37906
  2065
neuper@37906
  2066
(*assumes to take whole level, in particular hd -- for use in interSteps*)
neuper@37906
  2067
fun get_formress fs p [] = flat fs
neuper@37906
  2068
  | get_formress fs p (nd::nds) =
neuper@37906
  2069
    (* start with   'form+res'       and continue with trying 'res' only*)
neuper@37906
  2070
    get_forms (fs @ [formres p nd]) (lev_on p) nds
neuper@37906
  2071
and get_forms fs p [] = flat fs
neuper@37906
  2072
  | get_forms fs p (nd::nds) =
neuper@37906
  2073
    if is_pblnd nd
neuper@37906
  2074
    (* start again with      'form+res' ///ugly repeat with Check_elementwise
neuper@37906
  2075
    then get_formress (fs @ [formres p nd]) (lev_on p) nds                   *)
neuper@37906
  2076
    then get_forms    (fs @ [formres p nd]) (lev_on p) nds
neuper@37906
  2077
    (* continue with trying 'res' only*)
neuper@37906
  2078
    else get_forms    (fs @ [form    p nd]) (lev_on p) nds;
neuper@37906
  2079
neuper@37906
  2080
(**.get an 'interval' 'from' 'to' of formulae from a ptree.**)
neuper@37906
  2081
(*WN050219 made robust against _'to' below or after Complete nodes
neuper@37906
  2082
	   by handling exn caused by move_dn*)
neuper@37906
  2083
(*WN0401 this functionality belongs to ctree.sml, 
neuper@37906
  2084
but fetching a calc_head requires calculations defined in modspec.sml
neuper@37906
  2085
transfer to ME/me.sml !!!
neuper@37906
  2086
WN051224 ^^^ doesnt hold any longer, since only the headline of a calc_head
neuper@37906
  2087
is returned !!!!!!!!!!!!!
neuper@37906
  2088
*)
neuper@37906
  2089
fun eq_pos' (p1,Frm) (p2,Frm) = p1 = p2
neuper@37906
  2090
  | eq_pos' (p1,Res) (p2,Res) = p1 = p2
neuper@37906
  2091
  | eq_pos' (p1,Pbl) (p2,p2_) = p1 = p2 andalso (case p2_ of
neuper@37906
  2092
						     Pbl => true
neuper@37906
  2093
						   | Met => true
neuper@37906
  2094
						   | _ => false)
neuper@37906
  2095
  | eq_pos' (p1,Met) (p2,p2_) = p1 = p2 andalso (case p2_ of
neuper@37906
  2096
						     Pbl => true
neuper@37906
  2097
						   | Met => true
neuper@37906
  2098
						   | _ => false)
neuper@37906
  2099
  | eq_pos' _ _ = false;
neuper@37906
  2100
neuper@37906
  2101
(*.get an 'interval' from the ctree; 'interval' is w.r.t. the 
neuper@37906
  2102
   total ordering Position#compareTo(Position p) in the java-code
neuper@37906
  2103
val get_interval = fn
neuper@37906
  2104
    : pos' ->     : from is "move_up 1st-element" to return
neuper@37906
  2105
      pos' -> 	  : to the last element to be returned; from < to
neuper@37906
  2106
      int -> 	  : level: 0 gets the flattest sub-tree possible
neuper@37906
  2107
			   >999 gets the deepest sub-tree possible
neuper@37906
  2108
      ptree -> 	  : 
neuper@37906
  2109
      (pos' * 	  : of the formula
neuper@37906
  2110
       Term.term) : the formula
neuper@37906
  2111
	  list
neuper@37906
  2112
.*)
neuper@37906
  2113
fun get_interval from to level pt =
neuper@37906
  2114
(* val (from,level) = (f,lev);
neuper@37906
  2115
   val (from, to, level) = (([3, 2, 1], Res), ([],Res), 9999);
neuper@37906
  2116
   *)
neuper@37906
  2117
    let fun get_inter c (from:pos') (to:pos') lev pt =
neuper@37906
  2118
(* val (c, from, to, lev) = ([], from, to, level);
neuper@37906
  2119
   ------for recursion.......
neuper@37906
  2120
   val (c, from:pos', to:pos') = (c @ [(from, f)], move_dn [] pt from, to);
neuper@37906
  2121
   *)
neuper@37906
  2122
	    if eq_pos' from to orelse from = ([],Res)
neuper@37906
  2123
	    (*orelse ... avoids Exception- PTREE "end of calculation" raised,
neuper@37906
  2124
	     if 'to' has values NOT generated by move_dn, see systest/me.sml
neuper@37906
  2125
             TODO.WN0501: introduce an order on pos' and check "from > to"..
neuper@37906
  2126
             ...there is an order in Java! 
neuper@37906
  2127
             WN051224 the hack got worse with returning term instead ptform*)
neuper@37906
  2128
	    then let val (f,_,_) = pt_extract (pt, from)
neuper@37906
  2129
		 in case f of
neuper@37906
  2130
			ModSpec (_,_,headline,_,_,_) => c @ [(from, headline)] 
neuper@37906
  2131
		      | Form t => c @ [(from, t)]
neuper@37906
  2132
		 end
neuper@37906
  2133
	    else 
neuper@37906
  2134
		if lev < lev_of from
neuper@37906
  2135
		then (get_inter c (move_dn [] pt from) to lev pt)
neuper@37906
  2136
		     handle (PTREE _(*from move_dn too far*)) => c
neuper@37906
  2137
		else let val (f,_,_) = pt_extract (pt, from)
neuper@37906
  2138
			 val term = case f of
neuper@37906
  2139
					ModSpec (_,_,headline,_,_,_)=> headline
neuper@37906
  2140
				      | Form t => t
neuper@37906
  2141
		     in (get_inter (c @ [(from, term)]) 
neuper@37906
  2142
				   (move_dn [] pt from) to lev pt)
neuper@37906
  2143
			handle (PTREE _(*from move_dn too far*)) 
neuper@37906
  2144
			       => c @ [(from, term)] end
neuper@37906
  2145
    in get_inter [] from to level pt end;
neuper@37906
  2146
neuper@37906
  2147
(*for tests*)
neuper@37906
  2148
fun posform2str (pos:pos', form) =
neuper@37906
  2149
    "("^ pos'2str pos ^", "^
neuper@37906
  2150
    (case form of 
neuper@37906
  2151
	 Form f => term2str f
neuper@37906
  2152
       | ModSpec c => term2str (#3 c(*the headline*)))
neuper@37906
  2153
    ^")";
neuper@37906
  2154
fun posforms2str pfs = (strs2str' o (map (curry op ^ "\n")) o 
neuper@37906
  2155
			(map posform2str)) pfs;
neuper@37906
  2156
fun posterm2str (pos:pos', t) =
neuper@37906
  2157
    "("^ pos'2str pos ^", "^term2str t^")";
neuper@37906
  2158
fun posterms2str pfs = (strs2str' o (map (curry op ^ "\n")) o 
neuper@37906
  2159
			(map posterm2str)) pfs;
neuper@37906
  2160
neuper@37906
  2161
neuper@37906
  2162
(*WN050225 omits the last step, if pt is incomplete*)
neuper@37906
  2163
fun show_pt pt = 
neuper@38015
  2164
    tracing (posterms2str (get_interval ([],Frm) ([],Res) 99999 pt));
neuper@37906
  2165
neuper@37906
  2166
(*.get a calchead from a PblObj-node in the ctree; 
neuper@37906
  2167
   preconditions must be calculated.*)
neuper@37906
  2168
fun get_ocalhd (pt, pos' as (p,Pbl):pos') = 
neuper@37906
  2169
    let val PblObj {origin = (oris, ospec, hdf'), spec, probl,...} = 
neuper@37906
  2170
	    get_obj I pt p
neuper@37906
  2171
	val {prls,where_,...} = get_pbt (#2 (some_spec ospec spec))
neuper@37906
  2172
	val pre = check_preconds (assoc_thy"Isac.thy") prls where_ probl
neuper@37906
  2173
    in (ocalhd_complete probl pre spec, Pbl, hdf', probl, pre, spec):ocalhd end
neuper@37906
  2174
| get_ocalhd (pt, pos' as (p,Met):pos') = 
neuper@37906
  2175
    let val PblObj {fmz = fmz as (fmz_,_), origin = (oris, ospec, hdf'), 
neuper@37906
  2176
		    spec, meth,...} = 
neuper@37906
  2177
	    get_obj I pt p
neuper@37906
  2178
	val {prls,pre,...} = get_met (#3 (some_spec ospec spec))
neuper@37906
  2179
	val pre = check_preconds (assoc_thy"Isac.thy") prls pre meth
neuper@37906
  2180
    in (ocalhd_complete meth pre spec, Met, hdf', meth, pre, spec):ocalhd end;
neuper@37906
  2181
neuper@37906
  2182
(*.at the activeFormula set the Model, the Guard and the Specification 
neuper@37906
  2183
   to empty and return a CalcHead;
neuper@37906
  2184
   the 'origin' remains (for reconstructing all that).*)
neuper@37906
  2185
fun reset_calchead (pt, pos' as (p,_):pos') = 
neuper@37906
  2186
    let val PblObj {origin = (_, _, hdf'),...} = get_obj I pt p
neuper@37906
  2187
	val pt = update_pbl pt p []
neuper@37906
  2188
	val pt = update_met pt p []
neuper@37906
  2189
	val pt = update_spec pt p e_spec
neuper@37906
  2190
    in (pt, (p,Pbl):pos') end;
neuper@37906
  2191
neuper@37906
  2192
(*---------------------------------------------------------------------*)
neuper@37906
  2193
end
neuper@37906
  2194
neuper@37906
  2195
open CalcHead;
neuper@37906
  2196
(*---------------------------------------------------------------------*)
neuper@37906
  2197