src/Tools/isac/Specify/specification.sml
author Walther Neuper <walther.neuper@jku.at>
Thu, 14 May 2020 13:48:45 +0200
changeset 59978 660ed21464d2
parent 59977 src/Tools/isac/Specify/calchead.sml@e635534c5f63
child 59981 dc34eff67648
permissions -rw-r--r--
rename to Specification, contiued
walther@59978
     1
(* Title:  Specify/specification.sml
walther@59978
     2
   Author: Walther Neuper, Mathias Lehnfeld
neuper@37906
     3
   (c) due to copyright terms
wneuper@59265
     4
*)
wneuper@59540
     5
(* Survey on type fmz_ .. type ori .. type itm (in probl, meth) .. formal args of root-/SubProblem
wneuper@59540
     6
   and relations between respective list elements:                                       #1#
wneuper@59540
     7
                  fmz      =             [ dsc $ v......................................(dsc $ v)..]
wneuper@59540
     8
root problem on pos = ([], _)            
wneuper@59540
     9
                  fmz_     =             [(dsc, v).......................................(dsc, v)..]
wneuper@59540
    10
                  \<down>                                                                       
wneuper@59540
    11
                  oris     =             [(dsc, v)..(dsc, v),(dsc, v),(dsc, v)...........(dsc, v)..]
wneuper@59540
    12
                  \<down>                       #Given,..,#Relate  #Find     #undef............#undef   \<down>
wneuper@59540
    13
                  \<down>                                                     \<down>                 \<down>       \<down>
wneuper@59540
    14
  Specify_Problem + pbl.ppc=             [(dsc,id)..(dsc,id),(dsc,id)]  \<down>                 \<down>       \<down>
wneuper@59540
    15
                  \<down>                                                     \<down>                 \<down>       \<down>
wneuper@59540
    16
                  itms     =             [(dsc, v)..(dsc, v),(dsc, v)]  \<down>                 \<down>       \<down>
wneuper@59540
    17
  int.modelling                           +Cor ++++++++++Cor +Cor       \<down>                 \<down>       \<down>
wneuper@59540
    18
                  \<down>                                                     \<down>                 \<down>       \<down>
wneuper@59540
    19
  Specify_Method  + met.ppc=             [(dsc,id)..(dsc,id),(dsc,id)]  \<down>                 \<down>       \<down>
wneuper@59540
    20
                  \<down>                                                     \<down>                 \<down>       \<down>
wneuper@59540
    21
                  itms     =             [(dsc, v)..(dsc, v),(dsc, v),(dsc, v)..(dsc, v)] \<down>       \<down>
wneuper@59540
    22
  int.modelling                                                       +Cor ++++++Cor      \<down>       \<down>
wneuper@59540
    23
                  form.args=             [ id ..... id      , ///////  id ....... id    ] \<down>       \<down>
wneuper@59540
    24
                  env =                  [(id, v)..(id, v)  , /////// (id, v)....(id, v)] \<down>       \<down>
walther@59718
    25
  interpret code env = [(id, v)..(id, v),(id, v)..(id, v)  , /////// (id, v)....(id, v)] \<down>       \<down>
wneuper@59540
    26
    ..extends env       ^^^^^^^^^^^^^^^^   \<down>                           \<down>   \<down>              \<down>       \<down>
wneuper@59540
    27
                                   \<down>       \<down>                           \<down>   \<down>              \<down>       \<down>
wneuper@59540
    28
SubProblem on pos = ([2], _)       \<down>       \<down>                           \<down>   \<down>              \<down>       \<down>
wneuper@59540
    29
                  form.args=      [id ................................ id ]\<down>              \<down>       \<down>
wneuper@59540
    30
                  \<down>                REAL..BOOL..                            \<down>              \<down>       \<down>
wneuper@59540
    31
                  \<down>                                                        \<down>              \<down>       \<down>
wneuper@59540
    32
                  + met.ppc=      [(dsc,id).......................(dsc,id)]\<down>              \<down>       \<down>
wneuper@59540
    33
                                    \<down>                               \<down>      \<down>              \<down>       \<down>
wneuper@59540
    34
                  oris     =      [(dsc, v)...................... (dsc,   v) ........... (dsc, v)]\<down>
walther@59718
    35
  Specify_Problem, int.modelling, Specify_Method, interpret code as above                 #1#    \<down>
wneuper@59540
    36
                                                                                                  \<down>
wneuper@59540
    37
SubProblem on pos = ([3, 4], _)                                                                   \<down>
wneuper@59540
    38
                  form.args=              [id ...................... id ]                         \<down>
wneuper@59540
    39
                  \<down>                        REAL..BOOL..                                           \<down>
wneuper@59540
    40
                  + met.ppc=              [(dsc,id).............(dsc,id)]                         \<down>
wneuper@59540
    41
                  oris     =              [(dsc, v).............(dsc, v)...................(dsc, v)] 
walther@59718
    42
  Specify_Problem, int.modelling, Specify_Method, interpret code as above                    #1#
wneuper@59540
    43
wneuper@59540
    44
Notes:
wneuper@59540
    45
(1) SubProblem implements sub-function calls such, that the arguments (+ pre- + post-condition)
wneuper@59540
    46
    of the functions become concern of interactive modelling.
wneuper@59540
    47
(2) In Isac-terms, the above concerns the phases of modelling and 
wneuper@59540
    48
    and of specifying (Specify_Problem, Specify_Method),
wneuper@59540
    49
    while stepwise construction of solutions is called solving.
wneuper@59540
    50
    The latter is supported by Lucas-Interpretation of the functions' body.
wneuper@59540
    51
(3) ori list is determined by fmz_ (in root-pbl) or by args of SubProblem;
wneuper@59540
    52
    it is as complete as possible (this has been different up to now).
wneuper@59540
    53
(4) itm list is initialised by pbl-ppc | met-pps and completed (Cor) by stepwise user input.
wneuper@59550
    54
(5) model-pattern of pbl can omit technical items, e.g. boundVariable or functionName,
wneuper@59550
    55
    add them to the model-pattern of met and let this input be done automatically;
wneuper@59550
    56
    respective items must be in fmz.
wneuper@59540
    57
#1# fmz contains items, which are stored in oris of the root(!)-problem.
wneuper@59540
    58
    This allows to specify methods, which require more input as anticipated
wneuper@59540
    59
    in writing partial_functions: such an item can be fetched from the root-problem's oris.
wneuper@59540
    60
    A candidate for this situation is "errorBound" in case an equation cannot be solved symbolically
wneuper@59540
    61
    and thus is solved numerically.
wneuper@59540
    62
#2# TODO
wneuper@59540
    63
*)
walther@59977
    64
signature SPECIFICATION =
wneuper@59265
    65
sig
wneuper@59265
    66
  type calcstate
wneuper@59265
    67
  type calcstate'
walther@59735
    68
walther@59977
    69
  type T = Specification_Def.T
walther@59976
    70
  val nxt_spec : Pos.pos_ -> bool -> O_Model.T -> References.T -> I_Model.T * I_Model.T ->
walther@59976
    71
    (string * (term * 'a)) list * (string * (term * 'b)) list -> References.T -> Pos.pos_ * Tactic.input
neuper@37906
    72
walther@59775
    73
  val reset_calchead : Calc.T -> Calc.T
walther@59977
    74
  val get_ocalhd : Calc.T -> T
walther@59963
    75
  val ocalhd_complete : I_Model.T -> Pre_Conds.T -> ThyC.id * Problem.id * Method.id -> bool
walther@59775
    76
  val all_modspec : Calc.T -> Calc.T 
neuper@37906
    77
walther@59945
    78
  val complete_metitms : O_Model.T -> I_Model.T -> I_Model.T -> Model_Pattern.T -> I_Model.T
walther@59939
    79
  val insert_ppc' : I_Model.single -> I_Model.T -> I_Model.T
neuper@37906
    80
walther@59775
    81
  val complete_mod : Calc.T -> Calc.T
walther@59775
    82
  val is_complete_mod : Calc.T -> bool
walther@59775
    83
  val complete_spec : Calc.T -> Calc.T
walther@59775
    84
  val is_complete_spec : Calc.T -> bool
walther@59976
    85
  val some_spec : References.T -> References.T -> References.T
wneuper@59265
    86
  (* these could go to Ctree ..*)
wneuper@59279
    87
  val show_pt : Ctree.ctree -> unit
wneuper@59517
    88
  val show_pt_tac : Ctree.ctree -> unit
walther@59775
    89
  val pt_extract : Calc.T -> Ctree.ptform * Tactic.input option * term list 
walther@59846
    90
  val get_interval : Pos.pos' -> Pos.pos' -> int -> Ctree.ctree -> (Pos.pos' * term * Tactic.input option) list
neuper@37906
    91
walther@59945
    92
  val match_ags : theory -> Model_Pattern.T -> term list -> O_Model.T
walther@59903
    93
  val match_ags_msg : Problem.id -> term -> term list -> unit
walther@59939
    94
  val oris2fmz_vals : O_Model.T -> string list * term list
walther@59960
    95
  val vars_of_pbl_' : Model_Pattern.T -> term list
walther@59956
    96
walther@59959
    97
  val ppc2list : 'a P_Model.ppc -> 'a list
walther@59974
    98
walther@59974
    99
  val itm_out : theory -> I_Model.feedback -> string
walther@59943
   100
  val mk_delete: theory -> string -> I_Model.feedback -> Tactic.input
walther@59865
   101
  val mk_additem: string -> TermC.as_string -> Tactic.input
walther@59960
   102
  val nxt_add: theory -> O_Model.T -> Model_Pattern.T -> I_Model.T -> (string * string) option
walther@59943
   103
  val is_error: I_Model.feedback -> bool
walther@59977
   104
  val complete_mod_: O_Model.T * Model_Pattern.T * Model_Pattern.T * I_Model.T ->
walther@59977
   105
    I_Model.T * I_Model.T
walther@59865
   106
  val nxt_specif_additem: string -> TermC.as_string -> Calc.T -> calcstate'
walther@59865
   107
  val specify_additem: string -> TermC.as_string -> Calc.T -> string * calcstate'
wneuper@59310
   108
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
walther@59908
   109
  val e_calcstate : Calc.T * State_Steps.T
wneuper@59265
   110
  val e_calcstate' : calcstate'
walther@59886
   111
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
walther@59957
   112
  val posterms2str: (Pos.pos' * term * 'a) list -> string
walther@59957
   113
  val postermtacs2str: (Pos.pos' * term * Tactic.input option) list -> string
walther@59957
   114
  val is_copy_named_idstr: string -> bool
walther@59957
   115
  val is_copy_named_generating_idstr: string -> bool
walther@59960
   116
  val is_copy_named_generating: Model_Pattern.single -> bool
walther@59960
   117
  val is_copy_named: Model_Pattern.single -> bool
walther@59957
   118
  val ori2Coritm: Model_Pattern.T -> O_Model.single -> I_Model.single
walther@59960
   119
  val matc: theory -> Model_Pattern.T -> term list -> O_Model.preori list -> O_Model.preori list
walther@59957
   120
  val mtc: theory -> Model_Pattern.single -> term -> O_Model.preori option
walther@59957
   121
  val cpy_nam: Model_Pattern.T -> O_Model.preori list -> Model_Pattern.single -> O_Model.preori
walther@59976
   122
  val get: Calc.T -> I_Model.T * O_Model.T * ThyC.id * References.id * References.id *
walther@59976
   123
        I_Model.T * ThyC.id * References.id * References.id * Proof.context
walther@59957
   124
  val make: I_Model.m_field -> TermC.as_string * I_Model.T -> Tactic.T
walther@59886
   125
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
wneuper@59310
   126
wneuper@59307
   127
(*----- kept as hints to initial design ideas; NOT in src/, NOT in test ------------------------*)
wneuper@59307
   128
  val variants_in : term list -> int
walther@59939
   129
  val is_untouched : I_Model.single -> bool
wneuper@59307
   130
  val is_list_type : typ -> bool
walther@59943
   131
  val parse_ok : I_Model.feedback list -> bool
walther@59943
   132
  val all_dsc_in : I_Model.feedback list -> term list
wneuper@59307
   133
  val chktyps : theory -> term list * term list -> term list (* only in old tests*)
walther@59775
   134
  val is_complete_modspec : Calc.T -> bool
walther@59846
   135
  val get_formress : (string * Pos.pos' * term) list list -> Pos.pos -> Ctree.ctree list ->
walther@59846
   136
    (string * Pos.pos' * term) list
walther@59846
   137
  val get_forms : (string * Pos.pos' * term) list list -> Pos.pos -> Ctree.ctree list ->
walther@59846
   138
    (string * Pos.pos' * term) list
wneuper@59265
   139
end
neuper@37906
   140
walther@59763
   141
(**)
walther@59977
   142
structure Specification(**): SPECIFICATION(**) =
neuper@37906
   143
struct
walther@59763
   144
(**)
walther@59957
   145
walther@59957
   146
(** datatypes **)
neuper@37906
   147
wneuper@59265
   148
(* the state wich is stored after each step of calculation; it contains
neuper@38065
   149
   the calc-state and a list of [tac,istate](="tacis") to be applied next.
neuper@37906
   150
   the last_elem tacis is the first to apply to the calc-state and
neuper@37906
   151
   the (only) one shown to the front-end as the 'proposed tac'.
neuper@37906
   152
   the calc-state resulting from the application of tacis is not stored,
neuper@38065
   153
   because the tacis hold enough information for efficiently rebuilding
wneuper@59265
   154
   this state just by "fun generate "
wneuper@59265
   155
*)
neuper@37906
   156
type calcstate = 
walther@59946
   157
  Calc.T *      (* the calc-state to which the tacis could be applied *)
walther@59946
   158
  State_Steps.T (* ev. several (hidden) steps; 
walther@59946
   159
                   in REVERSE order: first tac_ to apply is last_elem *)
walther@59957
   160
val e_calcstate = ((Ctree.EmptyPtree, Pos.e_pos'), [State_Steps.single_empty]);
neuper@37906
   161
neuper@37906
   162
(*the state used during one calculation within the mathengine; it contains
neuper@37906
   163
  a list of [tac,istate](="tacis") which generated the the calc-state;
neuper@37906
   164
  while this state's tacis are extended by each (internal) step,
neuper@37906
   165
  the calc-state is used for creating new nodes in the calc-tree
neuper@37906
   166
  (eg. applicable_in requires several particular nodes of the calc-tree)
neuper@37906
   167
  and then replaced by the the newly created;
neuper@37906
   168
  on leave of the mathengine the resuing calc-state is dropped anyway,
neuper@37906
   169
  because the tacis hold enought information for efficiently rebuilding
neuper@37906
   170
  this state just by "fun generate ".*)
neuper@37906
   171
type calcstate' = 
walther@59908
   172
  State_Steps.T * (* cas. several (hidden) steps;
walther@59946
   173
                       in REVERSE order: first tac_ to apply is last_elem                 *)
walther@59957
   174
  Pos.pos' list *     (* a "continuous" sequence of pos', deleted by application of taci list *)     
walther@59946
   175
  Calc.T          (* the calc-state resulting from the application of tacis               *)
walther@59957
   176
val e_calcstate' = ([State_Steps.single_empty], [Pos.e_pos'], (Ctree.EmptyPtree, Pos.e_pos')) : calcstate';
neuper@37906
   177
walther@59977
   178
type T = Specification_Def.T;
walther@59977
   179
wneuper@59265
   180
(* is the calchead complete ? *)
wneuper@59265
   181
fun ocalhd_complete its pre (dI, pI, mI) = 
walther@59939
   182
  foldl and_ (true, map #3 (its: I_Model.T)) andalso 
walther@59963
   183
  foldl and_ (true, map #1 (pre: Pre_Conds.T)) andalso 
walther@59903
   184
  dI <> ThyC.id_empty andalso pI <> Problem.id_empty andalso mI <> Method.id_empty
neuper@37906
   185
wneuper@59265
   186
(* ["BOOL (1+x=2)","REAL x"] --match_ags--> oris 
wneuper@59265
   187
   --oris2fmz_vals--> ["equality (1+x=2)","boundVariable x","solutions L"] *)
neuper@42092
   188
fun oris2fmz_vals oris =
wneuper@59308
   189
  let fun ori2fmz_vals (_, _, _, dsc, ts) = 
walther@59953
   190
	  ((UnparseC.term o Input_Descript.join') (dsc, ts), last_elem ts) 
walther@59962
   191
	  handle _ => raise ERROR ("ori2fmz_env called with " ^ UnparseC.terms ts)
wneuper@59265
   192
  in (split_list o (map ori2fmz_vals)) oris end
neuper@37906
   193
wneuper@59308
   194
fun ppc2list {Given = gis, Where = whs, Find = fis, With = wis, Relate = res} =
wneuper@59265
   195
  gis @ whs @ fis @ wis @ res
neuper@37906
   196
neuper@37906
   197
(* get the number of variants in a problem in 'original',
neuper@37906
   198
   assumes equal descriptions in immediate sequence    *)
neuper@37906
   199
fun variants_in ts =
wneuper@59265
   200
  let
wneuper@59265
   201
    fun eq (x, y) = head_of x = head_of y
wneuper@59265
   202
    fun cnt _ [] _ n = ([n], [])
wneuper@59265
   203
      | cnt eq (x :: xs) y n = if eq (x, y) then cnt eq xs y (n + 1) else ([n], x :: xs)
wneuper@59265
   204
    fun coll _  xs [] = xs
wneuper@59265
   205
      | coll eq  xs (y :: ys) = 
wneuper@59265
   206
        let val (n, ys') = cnt eq (y :: ys) y 0
wneuper@59265
   207
        in if ys' = [] then xs @ n else coll eq  (xs @ n) ys' end
wneuper@59265
   208
    val vts = subtract op = [1] (distinct (coll eq [] ts))
wneuper@59265
   209
  in
wneuper@59265
   210
    case vts of 
wneuper@59265
   211
      [] => 1
wneuper@59265
   212
    | [n] => n
walther@59962
   213
    | _ => raise ERROR "different variants in formalization"
wneuper@59265
   214
  end
neuper@37906
   215
wneuper@59265
   216
fun is_list_type (Type ("List.list", _)) = true
wneuper@59265
   217
  | is_list_type _ = false
walther@59943
   218
fun is_parsed (I_Model.Syn _) = false
wneuper@59265
   219
  | is_parsed _ = true
wneuper@59265
   220
fun parse_ok its = foldl and_ (true, map is_parsed its)
neuper@37906
   221
neuper@37906
   222
fun all_dsc_in itm_s =
neuper@37906
   223
  let    
walther@59943
   224
    fun d_in (I_Model.Cor ((d, _), _)) = [d]
walther@59943
   225
      | d_in (I_Model.Syn _) = []
walther@59943
   226
      | d_in (I_Model.Typ _) = []
walther@59943
   227
      | d_in (I_Model.Inc ((d,_),_)) = [d]
walther@59943
   228
      | d_in (I_Model.Sup (d,_)) = [d]
walther@59943
   229
      | d_in (I_Model.Mis (d,_)) = [d]
walther@59946
   230
      | d_in i = raise ERROR ("all_dsc_in: uncovered case with " ^ I_Model.feedback_to_string' i)
wneuper@59265
   231
  in (flat o (map d_in)) itm_s end; 
neuper@37906
   232
walther@59943
   233
fun is_error (I_Model.Cor _) = false
walther@59943
   234
  | is_error (I_Model.Sup _) = false
walther@59943
   235
  | is_error (I_Model.Inc _) = false
walther@59943
   236
  | is_error (I_Model.Mis _) = false
wneuper@59265
   237
  | is_error _ = true
neuper@37906
   238
wneuper@59265
   239
(* get the first term in ts from ori *)
wneuper@59308
   240
fun getr_ct thy (_, _, fd, d, ts) =
walther@59953
   241
  (fd, ((UnparseC.term_in_thy thy) o Input_Descript.join) (d,[hd ts]))
neuper@37906
   242
neuper@38051
   243
(* get a term from ori, notyet input in itm.
neuper@38051
   244
   the term from ori is thrown back to a string in order to reuse
neuper@38051
   245
   machinery for immediate input by the user. *)
wneuper@59308
   246
fun geti_ct thy (_, _, _, d, ts) (_, _, _, fd, itm_) =
walther@59953
   247
  (fd, ((UnparseC.term_in_thy thy) o Input_Descript.join) (d, subtract op = (I_Model.ts_in itm_) ts))
neuper@37906
   248
neuper@37906
   249
(* in FE dsc, not dat: this is in itms ...*)
walther@59943
   250
fun is_untouched (_, _, false, _, I_Model.Inc ((_, []), _)) = true
wneuper@59265
   251
  | is_untouched _ = false
neuper@37906
   252
neuper@37906
   253
(* select an item in oris, notyet input in itms 
walther@59943
   254
   (precondition: in itms are only I_Model.Cor, I_Model.Sup, I_Model.Inc) *)
wneuper@59235
   255
(*args of nxt_add
wneuper@59235
   256
  thy : for?
wneuper@59235
   257
  oris: from formalization 'type fmz', structured for efficient access 
wneuper@59235
   258
  pbt : the problem-pattern to be matched with oris in order to get itms
wneuper@59235
   259
  itms: already input items
wneuper@59235
   260
*)
wneuper@59308
   261
fun nxt_add thy [] pbt itms = (*root (only) ori...fmz=[]*)
wneuper@59265
   262
    let
walther@59943
   263
      fun test_d d (i, _, _, _, itm_) = (d = (I_Model.d_in itm_)) andalso i <> 0
wneuper@59265
   264
      fun is_elem itms (_, (d, _)) = 
wneuper@59265
   265
        case find_first (test_d d) itms of SOME _ => true | NONE => false
wneuper@59265
   266
    in
wneuper@59265
   267
      case filter_out (is_elem itms) pbt of
walther@59953
   268
        (f, (d, _)) :: _ => SOME (f, ((UnparseC.term_in_thy thy) o Input_Descript.join) (d, []))
wneuper@59265
   269
      | _ => NONE
wneuper@59265
   270
    end
wneuper@59265
   271
  | nxt_add thy oris _ itms =
wneuper@59265
   272
    let
walther@59939
   273
      fun testr_vt v ori = member op= (#2 (ori : O_Model.single)) v andalso (#3 ori) <> "#undef"
walther@59939
   274
      fun testi_vt v itm = member op= (#2 (itm : I_Model.single)) v
walther@59939
   275
      fun test_id ids r = member op= ids (#1 (r : O_Model.single))
wneuper@59426
   276
      fun test_subset itm (_, _, _, d, ts) = 
walther@59943
   277
        (I_Model.d_in (#5 (itm: I_Model.single))) = d andalso subset op = (I_Model.ts_in (#5 itm), ts)
walther@59943
   278
      fun false_and_not_Sup (_, _, false, _, I_Model.Sup _) = false
wneuper@59265
   279
        | false_and_not_Sup (_, _, false, _, _) = true
wneuper@59265
   280
        | false_and_not_Sup _ = false
walther@59943
   281
      val v = if itms = [] then 1 else I_Model.max_vt itms
wneuper@59265
   282
      val vors = if v = 0 then oris else filter (testr_vt v) oris  (* oris..vat *)
wneuper@59265
   283
      val vits =
wneuper@59265
   284
        if v = 0
wneuper@59265
   285
        then itms                                 (* because of dsc without dat *)
wneuper@59265
   286
  	    else filter (testi_vt v) itms;                             (* itms..vat *)
wneuper@59265
   287
      val icl = filter false_and_not_Sup vits;                    (* incomplete *)
wneuper@59265
   288
    in
wneuper@59265
   289
      if icl = [] 
wneuper@59265
   290
      then case filter_out (test_id (map #1 vits)) vors of
wneuper@59265
   291
  	    [] => NONE
wneuper@59265
   292
  	  | miss => SOME (getr_ct thy (hd miss))
wneuper@59265
   293
      else
wneuper@59265
   294
        case find_first (test_subset (hd icl)) vors of
walther@59962
   295
          NONE => raise ERROR "nxt_add: types or dsc DO NOT MATCH BETWEEN fmz --- pbt"
wneuper@59265
   296
        | SOME ori => SOME (geti_ct thy ori (hd icl))
wneuper@59265
   297
    end
neuper@37906
   298
walther@59974
   299
(*create output-string for itm_*)
walther@59974
   300
fun itm_out _ (I_Model.Cor ((d, ts), _)) = UnparseC.term (Input_Descript.join (d, ts))
walther@59974
   301
  | itm_out _ (I_Model.Syn c) = c
walther@59974
   302
  | itm_out _ (I_Model.Typ c) = c
walther@59974
   303
  | itm_out _ (I_Model.Inc ((d, ts), _)) = UnparseC.term (Input_Descript.join (d, ts))
walther@59974
   304
  | itm_out _ (I_Model.Sup (d, ts)) = UnparseC.term (Input_Descript.join (d, ts))
walther@59974
   305
  | itm_out _ (I_Model.Mis (d, pid)) = UnparseC.term d ^ " " ^ UnparseC.term pid
walther@59974
   306
  | itm_out _ _ = raise ERROR "itm_out: uncovered definition"
walther@59974
   307
walther@59974
   308
fun mk_delete thy "#Given" itm_ = Tactic.Del_Given (itm_out thy itm_)
walther@59974
   309
  | mk_delete thy "#Find" itm_ = Tactic.Del_Find (itm_out thy itm_)
walther@59974
   310
  | mk_delete thy "#Relate" itm_ = Tactic.Del_Relation (itm_out thy itm_)
walther@59962
   311
  | mk_delete _ str _ = raise ERROR ("mk_delete: called with field \"" ^ str ^ "\"")
wneuper@59571
   312
fun mk_additem "#Given" ct = Tactic.Add_Given ct
wneuper@59571
   313
  | mk_additem "#Find" ct = Tactic.Add_Find ct    
wneuper@59571
   314
  | mk_additem "#Relate"ct = Tactic.Add_Relation ct
walther@59962
   315
  | mk_additem str _ = raise ERROR ("mk_additem: called with field \"" ^ str ^ "\"")
neuper@37906
   316
neuper@38051
   317
(* determine the next step of specification;
neuper@38051
   318
   not done here: Refine_Tacitly (otherwise *** unknown method: (..., no_met))
neuper@38051
   319
   eg. in rootpbl 'no_met': 
neuper@37906
   320
args:
neuper@38051
   321
  preok          predicates are _all_ ok (and problem matches completely)
neuper@37906
   322
  oris           immediately from formalization 
neuper@37906
   323
  (dI',pI',mI')  specification coming from author/parent-problem
neuper@37906
   324
  (pbl,          item lists specified by user
neuper@37906
   325
   met)          -"-, tacitly completed by copy_probl
neuper@37906
   326
  (dI,pI,mI)     specification explicitly done by the user
neuper@37906
   327
  (pbt, mpc)     problem type, guard of method
neuper@38051
   328
*)
walther@59969
   329
fun nxt_spec Pos.Pbl preok oris (dI', pI', mI') (pbl, met) (pbt, mpc) (dI, pI, mI) = 
walther@59969
   330
    (if dI' = ThyC.id_empty andalso dI = ThyC.id_empty then (Pos.Pbl, Tactic.Specify_Theory dI')
walther@59969
   331
     else if pI' = Problem.id_empty andalso pI = Problem.id_empty then (Pos.Pbl, Tactic.Specify_Problem pI')
wneuper@59265
   332
     else
wneuper@59308
   333
       case find_first (is_error o #5) pbl of
wneuper@59265
   334
	       SOME (_, _, _, fd, itm_) =>
walther@59969
   335
	         (Pos.Pbl, mk_delete (ThyC.get_theory (if dI = ThyC.id_empty then dI' else dI)) fd itm_)
wneuper@59265
   336
	     | NONE => 
walther@59881
   337
	       (case nxt_add (ThyC.get_theory (if dI = ThyC.id_empty then dI' else dI)) oris pbt pbl of
walther@59969
   338
	          SOME (fd, ct') => (Pos.Pbl, mk_additem fd ct')
wneuper@59265
   339
	        | NONE => (*pbl-items complete*)
walther@59969
   340
	          if not preok then (Pos.Pbl, Tactic.Refine_Problem pI')
walther@59969
   341
	          else if dI = ThyC.id_empty then (Pos.Pbl, Tactic.Specify_Theory dI')
walther@59969
   342
		        else if pI = Problem.id_empty then (Pos.Pbl, Tactic.Specify_Problem pI')
walther@59969
   343
		        else if mI = Method.id_empty then (Pos.Pbl, Tactic.Specify_Method mI')
wneuper@59265
   344
		        else
wneuper@59265
   345
			        case find_first (is_error o #5) met of
walther@59957
   346
			          SOME (_, _, _, fd, itm_) => (Pos.Met, mk_delete (ThyC.get_theory dI) fd itm_)
wneuper@59265
   347
			        | NONE => 
walther@59881
   348
			          (case nxt_add (ThyC.get_theory dI) oris mpc met of
walther@59957
   349
				          SOME (fd, ct') => (Pos.Met, mk_additem fd ct') (*30.8.01: pre?!?*)
walther@59957
   350
				        | NONE => (Pos.Met, Tactic.Apply_Method mI))))
walther@59969
   351
  | nxt_spec Pos.Met preok oris (dI', pI', _) (_, met) (_ ,mpc) (dI, pI, mI) = 
wneuper@59265
   352
    (case find_first (is_error o #5) met of
walther@59969
   353
      SOME (_,_,_,fd,itm_) => (Pos.Met, mk_delete (ThyC.get_theory (if dI = ThyC.id_empty then dI' else dI)) fd itm_)
wneuper@59265
   354
    | NONE => 
walther@59881
   355
      case nxt_add (ThyC.get_theory (if dI = ThyC.id_empty then dI' else dI)) oris mpc met of
walther@59969
   356
	      SOME (fd,ct') => (Pos.Met, mk_additem fd ct')
wneuper@59265
   357
      | NONE => 
walther@59969
   358
	      (if dI = ThyC.id_empty then (Pos.Met, Tactic.Specify_Theory dI')
walther@59969
   359
	       else if pI = Problem.id_empty then (Pos.Met, Tactic.Specify_Problem pI')
walther@59969
   360
		     else if not preok then (Pos.Met, Tactic.Specify_Method mI)
walther@59969
   361
		     else (Pos.Met, Tactic.Apply_Method mI)))
walther@59962
   362
  | nxt_spec p _ _ _ _ _ _ = raise ERROR ("nxt_spec: uncovered case with " ^ Pos.pos_2str p)
neuper@37906
   363
wneuper@59265
   364
(* make oris from args of the stac SubProblem and from pbt.
wneuper@59265
   365
   can this formal argument (of a model-pattern) be omitted in the arg-list
wneuper@59265
   366
   of a SubProblem ? see calcelems.sml 'type met '                        *)
wneuper@59265
   367
fun is_copy_named_idstr str =
wneuper@59265
   368
  case (rev o Symbol.explode) str of
wneuper@59540
   369
	  "'" :: _ :: "'" :: _ => true
wneuper@59540
   370
  | _ => false
wneuper@59389
   371
fun is_copy_named (_, (_, t)) = (is_copy_named_idstr o TermC.free2str) t
neuper@41973
   372
wneuper@59265
   373
(* should this formal argument (of a model-pattern) create a new identifier? *)
wneuper@59265
   374
fun is_copy_named_generating_idstr str =
wneuper@59265
   375
  if is_copy_named_idstr str
wneuper@59265
   376
  then
wneuper@59265
   377
    case (rev o Symbol.explode) str of
wneuper@59265
   378
	    "'" :: "'" :: "'" :: _ => false
wneuper@59265
   379
    | _ => true
wneuper@59265
   380
  else false
wneuper@59389
   381
fun is_copy_named_generating (_, (_, t)) = (is_copy_named_generating_idstr o TermC.free2str) t
neuper@37906
   382
wneuper@59265
   383
(* split type-wrapper from scr-arg and build part of an ori;
wneuper@59265
   384
   an type-error is reported immediately, raises an exn, 
wneuper@59265
   385
   subsequent handling of exn provides 2nd part of error message *)
wneuper@59405
   386
fun mtc thy (str, (dsc, _)) (ty $ var) =
wneuper@59265
   387
    ((Thm.global_cterm_of thy (dsc $ var);(*type check*)
wneuper@59308
   388
      SOME (([1], str, dsc, (*[var]*)
walther@59953
   389
	    Input_Descript.split' (dsc, var))) (*:ori without leading #*))
wneuper@59265
   390
      handle e as TYPE _ => 
wneuper@59265
   391
	      (tracing (dashs 70 ^ "\n"
wneuper@59265
   392
	        ^ "*** ERROR while creating the items for the model of the ->problem\n"
wneuper@59265
   393
	        ^ "*** from the ->stac with ->typeconstructor in arglist:\n"
walther@59868
   394
	        ^ "*** item (->description ->value): " ^ UnparseC.term dsc ^ " " ^ UnparseC.term var ^ "\n"
wneuper@59389
   395
	        ^ "*** description: " ^ TermC.term_detail2str dsc
wneuper@59389
   396
	        ^ "*** value: " ^ TermC.term_detail2str var
wneuper@59389
   397
	        ^ "*** typeconstructor in script: " ^ TermC.term_detail2str ty
walther@59880
   398
	        ^ "*** checked by theory: " ^ Context.theory_name thy ^ "\n"
wneuper@59265
   399
	        ^ "*** " ^ dots 66);
wneuper@59337
   400
          writeln (@{make_string} e);
wneuper@59338
   401
          Exn.reraise e; (*raise ERROR "actual args do not match formal args";FIXXXME.WN100916*)
wneuper@59265
   402
      NONE))
walther@59962
   403
  | mtc _ _ t = raise ERROR ("mtc: uncovered case with" ^ UnparseC.term t)
neuper@38010
   404
wneuper@59265
   405
(* match each pat of the model-pattern with an actual argument;
wneuper@59265
   406
   precondition: copy-named vars are filtered out            *)
wneuper@59405
   407
fun matc _ [] _ oris = oris
wneuper@59265
   408
  | matc _ pbt [] _ =
neuper@38015
   409
    (tracing (dashs 70);
walther@59962
   410
     raise ERROR ("actual arg(s) missing for '" ^ Model_Pattern.to_string pbt ^ "' i.e. should be 'copy-named' by '*_._'"))
wneuper@59265
   411
  | matc thy ((p as (_, (_, t))) :: pbt) (a :: ags) oris =
wneuper@59389
   412
    (*del?..*)if (is_copy_named_idstr o TermC.free2str) t then oris
wneuper@59265
   413
    else(*..del?*)
wneuper@59265
   414
      let val opt = mtc thy p a
wneuper@59265
   415
      in
wneuper@59265
   416
        case opt of
wneuper@59265
   417
          SOME ori => matc thy pbt ags (oris @ [ori])
neuper@37926
   418
	      | NONE => [](*WN050903 skipped by exn handled in match_ags*)
wneuper@59265
   419
	 end
neuper@38011
   420
neuper@38011
   421
(* generate a new variable "x_i" name from a related given one "x"
neuper@38011
   422
   by use of oris relating "v_v'i'" (is_copy_named!) to "v_v"
neuper@38012
   423
   e.g. (v_v, x) & (v_v'i', ?) --> (v_v'i', x_i),
neuper@38012
   424
   but leave is_copy_named_generating as is, e.t. ss''' *)
wneuper@59405
   425
fun cpy_nam pbt oris (p as (field, (dsc, t))) =
neuper@37906
   426
  (if is_copy_named_generating p
neuper@37906
   427
   then (*WN051014 kept strange old code ...*)
walther@59953
   428
     let fun sel (_,_,d,ts) = Input_Descript.join'''' (d, ts) 
wneuper@59389
   429
       val cy' = (implode o (drop_last_n 3) o Symbol.explode o TermC.free2str) t
wneuper@59389
   430
       val ext = (last_elem o drop_last o Symbol.explode o TermC.free2str) t
wneuper@59513
   431
       val vars' = map (Term.term_name o snd o snd) pbt (*cpy-nam filtered_out*)
wneuper@59265
   432
       val vals = map sel oris
wneuper@59513
   433
       val cy_ext = (Term.term_name o the) (assoc (vars' ~~ vals, cy')) ^ "_" ^ ext
wneuper@59389
   434
     in ([1], field, dsc, [TermC.mk_free (type_of t) cy_ext]) end
neuper@37906
   435
   else ([1], field, dsc, [t])
walther@59962
   436
	) handle _ => raise ERROR ("cpy_nam: for "^ UnparseC.term t)
neuper@37906
   437
wneuper@59265
   438
(* match the actual arguments of a SubProblem with a model-pattern
neuper@37906
   439
   and create an ori list (in root-pbl created from formalization).
neuper@37906
   440
   expects ags:pats = 1:1, while copy-named are filtered out of pats;
wneuper@59540
   441
   if no 1:1 then exn raised by matc/mtc and handled at call.
wneuper@59265
   442
   copy-named pats are appended in order to get them into the model-items *)
wneuper@59405
   443
fun match_ags thy pbt ags =
wneuper@59540
   444
  let
wneuper@59540
   445
    fun flattup (i, (var, bool, str, itm_)) = (i, var, bool, str, itm_)
wneuper@59265
   446
    val pbt' = filter_out is_copy_named pbt
wneuper@59540
   447
    val cy = filter is_copy_named pbt  (* cy is NOT a (formal) argument, but the fun's result *)
wneuper@59265
   448
    val oris' = matc thy pbt' ags []
wneuper@59265
   449
    val cy' = map (cpy_nam pbt' oris') cy
walther@59947
   450
    val ors = O_Model.add_id (oris' @ cy') (*...appended in order to get into the model-items *)
wneuper@59308
   451
  in (map flattup ors) end
neuper@37906
   452
wneuper@59265
   453
(* report part of the error-msg which is not available in match_args *)
neuper@37906
   454
fun match_ags_msg pI stac ags =
wneuper@59265
   455
  let
walther@59970
   456
    val pats = (#ppc o Problem.from_store) pI
wneuper@59405
   457
    val msg = (dots 70 ^ "\n"
wneuper@59405
   458
       ^ "*** problem " ^ strs2str pI ^ " has the ...\n"
walther@59945
   459
       ^ "*** model-pattern " ^ Model_Pattern.to_string pats ^ "\n"
walther@59868
   460
       ^ "*** stac   '" ^ UnparseC.term stac ^ "' has the ...\n"
walther@59868
   461
       ^ "*** arg-list " ^ UnparseC.terms ags ^ "\n"
wneuper@59265
   462
       ^ dashs 70)
wneuper@59265
   463
	  (*WN100921 ^^^^ expect TYPE errormsg below; lost with Isa09*)
wneuper@59265
   464
  in tracing msg end
neuper@37906
   465
wneuper@59265
   466
(* get the variables out of a pbl_; FIXME.WN.0311: is_copy_named ...obscure!!! *)
neuper@37906
   467
fun vars_of_pbl_' pbl_ = 
wneuper@59265
   468
  let
wneuper@59265
   469
    fun var_of_pbl_ (_, (_, t)) = t: term
wneuper@59265
   470
  in ((map var_of_pbl_)(* o (filter_out is_copy_named)*)) pbl_ end
neuper@37906
   471
walther@59943
   472
fun eq_dsc ((_, _, _, _, itm_), (_, _, _, _, iitm_)) = (I_Model.d_in itm_ = I_Model.d_in iitm_)
neuper@37906
   473
wneuper@59265
   474
(* insert_ppc = insert_ppc' for appl_add', input_icalhd 11.03,
wneuper@59265
   475
   handles superfluous items carelessly                       *)
wneuper@59265
   476
fun insert_ppc' itm itms = if member eq_dsc itms itm then itms else itms @ [itm] (* @ new itm *)
neuper@37906
   477
wneuper@59265
   478
(* output the headline to a ppc *)
wneuper@59265
   479
fun header p_ pI mI =
walther@59969
   480
  case p_ of
walther@59969
   481
    Pos.Pbl => Test_Out.Problem (if pI = Problem.id_empty then [] else pI) 
walther@59969
   482
	| Pos.Met => Test_Out.Method mI
walther@59969
   483
	| pos => raise ERROR ("header called with "^ Pos.pos_2str pos)
neuper@37906
   484
walther@59957
   485
fun make m_field (term_as_string, i_model) =
walther@59957
   486
  case m_field of
walther@59957
   487
    "#Given" => Tactic.Add_Given' (term_as_string, i_model)
walther@59957
   488
  | "#Find" => Tactic.Add_Find' (term_as_string, i_model)
walther@59957
   489
  | "#Relate" => Tactic.Add_Relation'(term_as_string, i_model)
walther@59957
   490
  | str => raise ERROR ("specify_additem Frm, Pbl: uncovered case with " ^ str);
walther@59957
   491
fun get (pt, (p, _)) =
walther@59957
   492
  case Ctree.get_obj I pt p of
walther@59957
   493
    (Ctree.PblObj {meth, origin = (oris, (dI', pI', mI'), _), probl, spec = (dI, pI, mI), ctxt, ...})
walther@59957
   494
      => (meth, oris, dI', pI', mI', probl, dI, pI, mI, ctxt)
walther@59957
   495
  | _ => raise ERROR "specify_additem: uncovered case of get_obj I pt p";
walther@59969
   496
fun specify_additem sel ct (pt, pos as (p, Pos.Met)) = 
neuper@41993
   497
      let
walther@59957
   498
        val (met, oris, dI', pI', mI', pbl, dI, pI, mI, ctxt) = get (pt, pos)
walther@59957
   499
        val thy = if dI = ThyC.id_empty then ThyC.get_theory dI' else ThyC.get_theory dI
walther@59957
   500
        val cpI = if pI = Problem.id_empty then pI' else pI
walther@59957
   501
        val cmI = if mI = Method.id_empty then mI' else mI
walther@59970
   502
        val {ppc, pre, prls, ...} = Method.from_store cmI
walther@59957
   503
      in 
walther@59958
   504
        case I_Model.check_single ctxt sel oris met ppc ct of
walther@59957
   505
          I_Model.Add itm =>  (*..union old input *)
walther@59957
   506
    	      let
walther@59958
   507
              val met' = I_Model.add_single thy itm met
walther@59957
   508
    	        val (p, pt') =
walther@59969
   509
    	         case Specify_Step.add (make sel (ct, met')) (Istate_Def.Uistate, ctxt) (pt, (p, Pos.Met)) of
walther@59969
   510
    	          ((p, Pos.Met), _, _, pt') => (p, pt')
walther@59962
   511
    	        | _ => raise ERROR "specify_additem: uncovered case of generate1"
walther@59965
   512
    	        val pre' = Pre_Conds.check' thy prls pre met'
walther@59957
   513
    	        val pb = foldl and_ (true, map fst pre')
walther@59957
   514
    	        val (p_, _) =
walther@59969
   515
    	          nxt_spec Pos.Met pb oris (dI',pI',mI') (pbl,met') 
walther@59970
   516
    	            ((#ppc o Problem.from_store) cpI,ppc) (dI,pI,mI);
walther@59957
   517
    	      in 
walther@59957
   518
              ("ok", ([], [], (pt', (p, p_))))
walther@59957
   519
            end
walther@59957
   520
        | I_Model.Err msg =>
walther@59957
   521
    	      let
walther@59965
   522
              val pre' = Pre_Conds.check' thy prls pre met
walther@59957
   523
    	        val pb = foldl and_ (true, map fst pre')
walther@59957
   524
    	        val (p_, _) =
walther@59969
   525
    	          nxt_spec Pos.Met pb oris (dI',pI',mI') (pbl,met) 
walther@59970
   526
    	            ((#ppc o Problem.from_store) cpI,(#ppc o Method.from_store) cmI) (dI,pI,mI);
walther@59957
   527
    	      in
walther@59957
   528
              (msg, ([], [], (pt, (p, p_))))
walther@59957
   529
    	      end
walther@59957
   530
      end
walther@59957
   531
  | specify_additem sel ct (pt, pos as (p, _(*Frm, Pbl*))) =
walther@59957
   532
      let
walther@59957
   533
        val (met, oris, dI', pI', mI', pbl, dI, pI, mI, ctxt) = get (pt, pos)
walther@59881
   534
        val thy = if dI = ThyC.id_empty then ThyC.get_theory dI' else ThyC.get_theory dI
walther@59903
   535
        val cpI = if pI = Problem.id_empty then pI' else pI
walther@59903
   536
        val cmI = if mI = Method.id_empty then mI' else mI
walther@59970
   537
        val {ppc, where_, prls, ...} = Problem.from_store cpI
neuper@41993
   538
      in
walther@59958
   539
        case I_Model.check_single ctxt sel oris pbl ppc ct of
walther@59956
   540
          I_Model.Add itm => (*..union old input *)
neuper@41993
   541
	          let
walther@59958
   542
	            val pbl' = I_Model.add_single thy itm pbl
walther@59810
   543
	            val (p, pt') =
walther@59957
   544
	              case Specify_Step.add (make sel (ct, pbl')) (Istate_Def.Uistate, ctxt) (pt, (p, Pos.Pbl)) of
walther@59969
   545
  	              ((p, Pos.Pbl), _, _, pt') => (p, pt')
walther@59957
   546
  	            | _ => raise ERROR "specify_additem: uncovered case of Specify_Step.add"
walther@59957
   547
(* only for getting final p_ ..*)
walther@59965
   548
	            val pre = Pre_Conds.check' thy prls where_ pbl';
walther@59957
   549
	            val pb = foldl and_ (true, map fst pre);
walther@59957
   550
	            val (p_, _) =
walther@59970
   551
	              nxt_spec Pos.Pbl pb oris (dI',pI',mI') (pbl',met) (ppc, (#ppc o Method.from_store) cmI) (dI, pI, mI);
wneuper@59265
   552
	          in
walther@59806
   553
              ("ok", ([], [], (pt', (p, p_))))
neuper@41993
   554
            end
walther@59956
   555
        | I_Model.Err msg =>
neuper@41993
   556
	          let
walther@59965
   557
              val pre = Pre_Conds.check' thy prls where_ pbl
neuper@41993
   558
	            val pb = foldl and_ (true, map fst pre)
walther@59957
   559
	            val (p_, _(*Tactic.input*)) =
walther@59957
   560
	              nxt_spec Pos.Pbl pb oris (dI', pI', mI') (pbl, met) 
walther@59970
   561
	                (ppc, (#ppc o Method.from_store) cmI) (dI, pI, mI)
walther@59806
   562
	          in
walther@59806
   563
            (msg, ([], [], (pt, (p, p_))))
walther@59806
   564
	          end
wneuper@59265
   565
      end
neuper@37906
   566
neuper@41994
   567
(*FIXME.WN110515 declare_constraints for ct (without dsc) into PblObj{ctxt, ...}
neuper@41994
   568
  -- for input from scratch*)
walther@59969
   569
fun nxt_specif_additem sel ct (ptp as (pt, (p, Pos.Pbl))) = 
wneuper@59265
   570
    let
walther@59957
   571
      val (oris, dI', pI', dI, pI, pbl, ctxt) = case Ctree.get_obj I pt p of
walther@59957
   572
        Ctree.PblObj {origin = (oris, (dI', pI', _), _), spec = (dI, pI, _), probl = pbl, ctxt, ...} =>
wneuper@59265
   573
           (oris, dI', pI', dI, pI, pbl, ctxt)
walther@59962
   574
      | _ => raise ERROR "specify (Specify_Theory': uncovered case get_obj"
walther@59881
   575
      val thy = if dI = ThyC.id_empty then ThyC.get_theory dI' else ThyC.get_theory dI;
walther@59903
   576
      val cpI = if pI = Problem.id_empty then pI' else pI;
wneuper@59265
   577
    in
walther@59970
   578
      case I_Model.check_single ctxt sel oris pbl ((#ppc o Problem.from_store) cpI) ct of
walther@59956
   579
	      I_Model.Add itm (*..union old input *) =>
wneuper@59265
   580
	        let
walther@59958
   581
	          val pbl' = I_Model.add_single thy itm pbl
wneuper@59265
   582
	          val (tac, tac_) = case sel of
wneuper@59571
   583
		          "#Given" => (Tactic.Add_Given    ct, Tactic.Add_Given'   (ct, pbl'))
wneuper@59571
   584
		        | "#Find"  => (Tactic.Add_Find     ct, Tactic.Add_Find'    (ct, pbl'))
wneuper@59571
   585
		        | "#Relate"=> (Tactic.Add_Relation ct, Tactic.Add_Relation'(ct, pbl'))
walther@59962
   586
		        | sel => raise ERROR ("nxt_specif_additem: uncovered case of" ^ sel)
walther@59810
   587
		        val (p, c, pt') =
walther@59969
   588
		          case Specify_Step.add tac_ (Istate_Def.Uistate, ctxt) (pt, (p, Pos.Pbl)) of
walther@59969
   589
  		          ((p, Pos.Pbl), c, _, pt') =>  (p, c, pt')
walther@59962
   590
  		        | _ => raise ERROR "nxt_specif_additem: uncovered case generate1"
wneuper@59265
   591
	        in
walther@59969
   592
	          ([(tac, tac_, ((p, Pos.Pbl), (Istate_Def.Uistate, ctxt)))], c, (pt', (p, Pos.Pbl)))
wneuper@59265
   593
          end	       
walther@59956
   594
	    | I_Model.Err msg => (*TODO.WN03 pass error-msgs to the frontend..
wneuper@59265
   595
                     FIXME ..and dont abuse a tactic for that purpose*)
walther@59879
   596
	        ([(Tactic.Tac msg, Tactic.Tac_ (ThyC.get_theory "Isac_Knowledge", msg,msg,msg),
walther@59957
   597
	          (Pos.e_pos', (Istate_Def.empty, ContextC.empty)))], [], ptp) 
wneuper@59265
   598
    end
walther@59969
   599
  | nxt_specif_additem sel ct (ptp as (pt, (p, Pos.Met))) = 
wneuper@59265
   600
    let
walther@59957
   601
      val (oris, dI', mI', dI, mI, met, ctxt) = case Ctree.get_obj I pt p of
walther@59957
   602
        Ctree.PblObj {origin = (oris, (dI', _, mI'), _), spec = (dI, _, mI), meth = met,ctxt, ...} =>
wneuper@59265
   603
           (oris, dI', mI', dI, mI, met, ctxt)
walther@59962
   604
      | _ => raise ERROR "nxt_specif_additem Met: uncovered case get_obj"
walther@59881
   605
      val thy = if dI = ThyC.id_empty then ThyC.get_theory dI' else ThyC.get_theory dI;
walther@59903
   606
      val cmI = if mI = Method.id_empty then mI' else mI;
wneuper@59265
   607
    in 
walther@59970
   608
      case I_Model.check_single ctxt sel oris met ((#ppc o Method.from_store) cmI) ct of
walther@59956
   609
        I_Model.Add itm (*..union old input *) =>
wneuper@59265
   610
	        let
walther@59958
   611
	          val met' = I_Model.add_single thy itm met;
wneuper@59265
   612
	          val (tac,tac_) = case sel of
wneuper@59571
   613
		          "#Given" => (Tactic.Add_Given    ct, Tactic.Add_Given'   (ct, met'))
wneuper@59571
   614
		        | "#Find"  => (Tactic.Add_Find     ct, Tactic.Add_Find'    (ct, met'))
wneuper@59571
   615
		        | "#Relate"=> (Tactic.Add_Relation ct, Tactic.Add_Relation'(ct, met'))
walther@59962
   616
		        | sel => raise ERROR ("nxt_specif_additem Met: uncovered case of" ^ sel)
walther@59810
   617
	          val (p, c, pt') =
walther@59969
   618
	            case Specify_Step.add tac_ (Istate_Def.Uistate, ctxt) (pt, (p, Pos.Met)) of
walther@59969
   619
  	            ((p, Pos.Met), c, _, pt') => (p, c, pt')
walther@59962
   620
  		        | _ => raise ERROR "nxt_specif_additem: uncovered case generate1 (WARNING WHY ?)"
wneuper@59265
   621
	        in
walther@59969
   622
	          ([(tac, tac_, ((p, Pos.Met), (Istate_Def.Uistate, ctxt)))], c, (pt', (p, Pos.Met)))
wneuper@59265
   623
	        end
walther@59956
   624
      | I_Model.Err _ => ([(*tacis*)], [], ptp) (*nxt_me collects tacis until not hide; here just no progress*)
wneuper@59265
   625
    end
walther@59962
   626
  | nxt_specif_additem _ _ (_, p) = raise ERROR ("nxt_specif_additem not impl. for" ^ Pos.pos'2str p)
neuper@41994
   627
wneuper@59405
   628
fun ori2Coritm pbt (i, v, f, d, ts) =
walther@59969
   629
  (i, v, true, f, I_Model.Cor ((d, ts), ((snd o snd o the o (find_first (I_Model.eq1 d))) pbt, ts)))
walther@59943
   630
    handle _ => (i, v, true, f, I_Model.Cor ((d, ts), (d, ts)))
wneuper@59265
   631
      (*dsc in oris, but not in pbl pat list: keep this dsc*)
neuper@37906
   632
wneuper@59265
   633
(* filter out oris which have same description in itms *)
neuper@37906
   634
fun filter_outs oris [] = oris
neuper@37906
   635
  | filter_outs oris (i::itms) = 
wneuper@59265
   636
    let
walther@59943
   637
      val ors = filter_out ((curry op = ((I_Model.d_in o #5) i)) o (#4)) oris
wneuper@59265
   638
    in
wneuper@59265
   639
      filter_outs ors itms
wneuper@59265
   640
    end
neuper@37906
   641
wneuper@59265
   642
(* filter oris which are in pbt, too *)
neuper@37906
   643
fun filter_pbt oris pbt =
wneuper@59265
   644
  let
wneuper@59265
   645
    val dscs = map (fst o snd) pbt
wneuper@59265
   646
  in
wneuper@59308
   647
    filter ((member op= dscs) o (#4)) oris
wneuper@59265
   648
  end
neuper@37906
   649
wneuper@59265
   650
(* combine itms from pbl + met and complete them wrt. pbt *)
wneuper@59265
   651
(* FIXXXME.WN031205 complete_metitms doesnt handle incorrect itms !*)
wneuper@59308
   652
fun complete_metitms oris pits mits met = 
wneuper@59265
   653
  let
walther@59943
   654
    val vat = I_Model.max_vt pits;
wneuper@59308
   655
    val itms = pits @ (filter ((member_swap op = vat) o (#2 )) mits)
wneuper@59308
   656
    val ors = filter ((member_swap op= vat) o (#2)) oris
wneuper@59265
   657
    val os = filter_outs ors itms (*WN.12.03?: does _NOT_ add itms from met ?!*)
wneuper@59265
   658
  in
wneuper@59265
   659
    itms @ (map (ori2Coritm met) os)
wneuper@59265
   660
  end
neuper@37906
   661
wneuper@59265
   662
(* complete model and guard of a calc-head *)
wneuper@59265
   663
fun complete_mod_ (oris, mpc, ppc, probl) =
wneuper@59265
   664
  let
wneuper@59308
   665
    val pits = filter_out ((curry op= false) o (#3)) probl
walther@59943
   666
    val vat = if probl = [] then 1 else I_Model.max_vt probl
wneuper@59308
   667
    val pors = filter ((member_swap op = vat) o (#2)) oris
wneuper@59265
   668
    val pors = filter_outs pors pits (*which are in pbl already*)
wneuper@59265
   669
    val pors = (filter_pbt pors ppc) (*which are in pbt, too*)
wneuper@59265
   670
    val pits = pits @ (map (ori2Coritm ppc) pors)
wneuper@59265
   671
    val mits = complete_metitms oris pits [] mpc
wneuper@59265
   672
  in (pits, mits) end
neuper@37906
   673
wneuper@59405
   674
fun some_spec (odI, opI, omI) (dI, pI, mI) =
walther@59879
   675
  (if dI = ThyC.id_empty then odI else dI,
walther@59903
   676
   if pI = Problem.id_empty then opI else pI,
walther@59903
   677
   if mI = Method.id_empty then omI else mI)
neuper@37906
   678
wneuper@59265
   679
(* fun tag_form thy (formal, given) = Thm.global_cterm_of thy
wneuper@59265
   680
	 (((head_of o Thm.term_of) given) $ (Thm.term_of formal)); WN100819 *)
neuper@37934
   681
fun tag_form thy (formal, given) =
neuper@52070
   682
 (let
neuper@52070
   683
    val gf = (head_of given) $ formal;
wneuper@59184
   684
    val _ = Thm.global_cterm_of thy gf
neuper@52070
   685
  in gf end)
walther@59962
   686
  handle _ => raise ERROR ("calchead.tag_form: " ^ UnparseC.term_in_thy thy given ^
walther@59870
   687
    " .. " ^ UnparseC.term_in_thy thy formal ^ " ..types do not match")
neuper@38053
   688
wneuper@59265
   689
fun chktyps thy (fs, gs) = map (tag_form thy) (fs ~~ gs)
neuper@37906
   690
wneuper@59265
   691
(* complete _NON_empty calc-head for autocalc (sub-)pbl from oris
wneuper@59265
   692
  + met from fmz; assumes pos on PblObj, meth = []                    *)
walther@59957
   693
fun complete_mod (pt, pos as (p, p_)) =
wneuper@59265
   694
  let
walther@59957
   695
    val _ = if p_ <> Pos.Pbl 
walther@59962
   696
	    then raise ERROR ("###complete_mod: only impl.for Pbl, called with " ^ Pos.pos'2str pos)
wneuper@59265
   697
	    else ()
walther@59957
   698
	  val (oris, ospec, probl, spec) = case Ctree.get_obj I pt p of
walther@59957
   699
	    Ctree.PblObj {origin = (oris, ospec, _), probl, spec, ...} => (oris, ospec, probl, spec)
walther@59962
   700
	  | _ => raise ERROR "complete_mod: unvered case get_obj"
wneuper@59265
   701
  	val (_, pI, mI) = some_spec ospec spec
walther@59970
   702
  	val mpc = (#ppc o Method.from_store) mI
walther@59970
   703
  	val ppc = (#ppc o Problem.from_store) pI
wneuper@59265
   704
  	val (pits, mits) = complete_mod_ (oris, mpc, ppc, probl)
walther@59957
   705
    val pt = Ctree.update_pblppc pt p pits
walther@59957
   706
	  val pt = Ctree.update_metppc pt p mits
walther@59957
   707
  in (pt, (p, Pos.Met)) end
neuper@37906
   708
walther@59820
   709
(* do all specification in one single step:
walther@59820
   710
   complete calc-head for autocalc (sub-)pbl from oris (+ met from fmz);
walther@59820
   711
   oris and spec (incl. pbl-refinement) given from init_calc or SubProblem
walther@59820
   712
*)
walther@59671
   713
fun all_modspec (pt, (p, _)) =
wneuper@59265
   714
  let
walther@59957
   715
    val (pors, dI, pI, mI) = case Ctree.get_obj I pt p of
walther@59957
   716
      Ctree.PblObj {origin = (pors, (dI, pI, mI), _), ...}
walther@59820
   717
        => (pors, dI, pI, mI)
walther@59820
   718
    | _ => raise ERROR "all_modspec: uncovered case get_obj"
walther@59970
   719
	  val {ppc, ...} = Method.from_store mI
wneuper@59265
   720
    val (_, vals) = oris2fmz_vals pors
wneuper@59582
   721
	  val ctxt = ContextC.initialise dI vals
walther@59957
   722
    val (pt, _) = Ctree.cupdate_problem pt p ((dI, pI, mI),
walther@59820
   723
      map (ori2Coritm ppc) pors, map (ori2Coritm ppc) pors, ctxt)
wneuper@59265
   724
  in
walther@59957
   725
    (pt, (p, Pos.Met))
wneuper@59265
   726
  end
neuper@37906
   727
wneuper@59265
   728
(* WN0312: use in nxt_spec, too ? what about variants ??? *)
wneuper@59308
   729
fun is_complete_mod_ [] = false
wneuper@59265
   730
  | is_complete_mod_ itms = foldl and_ (true, (map #3 itms))
neuper@41976
   731
walther@59969
   732
fun is_complete_mod (pt, pos as (p, Pos.Pbl)) =
walther@59957
   733
    if (Ctree.is_pblobj o (Ctree.get_obj I pt)) p 
walther@59957
   734
    then (is_complete_mod_ o (Ctree.get_obj Ctree.g_pbl pt)) p
walther@59962
   735
    else raise ERROR ("is_complete_mod: called by PrfObj at " ^ Pos.pos'2str pos)
walther@59969
   736
  | is_complete_mod (pt, pos as (p, Pos.Met)) = 
walther@59957
   737
    if (Ctree.is_pblobj o (Ctree.get_obj I pt)) p 
walther@59957
   738
    then (is_complete_mod_ o (Ctree.get_obj Ctree.g_met pt)) p
walther@59962
   739
    else raise ERROR ("is_complete_mod: called by PrfObj at " ^ Pos.pos'2str pos)
neuper@37906
   740
  | is_complete_mod (_, pos) =
walther@59962
   741
    raise ERROR ("is_complete_mod called by " ^ Pos.pos'2str pos ^ " (should be Pbl or Met)")
neuper@37906
   742
wneuper@59265
   743
(* have (thy, pbl, met) _all_ been specified explicitly ? *)
walther@59957
   744
fun is_complete_spec (pt, pos as (p, _)) = 
walther@59957
   745
  if (not o Ctree.is_pblobj o (Ctree.get_obj I pt)) p 
walther@59962
   746
  then raise ERROR ("is_complete_spec: called by PrfObj at " ^ Pos.pos'2str pos)
wneuper@59265
   747
  else
walther@59957
   748
    let val (dI,pI,mI) = Ctree.get_obj Ctree.g_spec pt p
walther@59903
   749
    in dI <> ThyC.id_empty andalso pI <> Problem.id_empty andalso mI <> Method.id_empty end
neuper@37906
   750
wneuper@59265
   751
(* complete empty items in specification from origin (pbl, met ev.refined);
wneuper@59265
   752
   assumes 'is_complete_mod' *)
walther@59957
   753
fun complete_spec (pt, pos as (p, _)) = 
wneuper@59265
   754
  let
walther@59957
   755
    val (ospec, spec) = case Ctree.get_obj I pt p of
walther@59957
   756
      Ctree.PblObj {origin = (_,ospec,_), spec,...} => (ospec, spec)
walther@59962
   757
    | _ => raise ERROR "complete_spec: uncovered case get_obj"
walther@59957
   758
    val pt = Ctree.update_spec pt p (some_spec ospec spec)
wneuper@59265
   759
  in
wneuper@59265
   760
    (pt, pos)
wneuper@59265
   761
  end
neuper@37906
   762
wneuper@59265
   763
fun is_complete_modspec ptp = is_complete_mod ptp andalso is_complete_spec ptp
neuper@37906
   764
walther@59969
   765
fun pt_model (Ctree.PblObj {meth, spec, origin = (_, spec', hdl), ...}) Pos.Met =
wneuper@59265
   766
    let
walther@59957
   767
      val (_, _, metID) = Ctree.get_somespec' spec spec'
walther@59903
   768
	    val pre = if metID = Method.id_empty then []
wneuper@59265
   769
	      else
wneuper@59265
   770
	        let
walther@59970
   771
	          val {prls, pre= where_, ...} = Method.from_store metID
walther@59960
   772
	          val pre = Pre_Conds.check prls where_ meth 0
wneuper@59265
   773
		      in pre end
wneuper@59265
   774
	    val allcorrect = is_complete_mod_ meth andalso foldl and_ (true, (map #1 pre))
wneuper@59265
   775
    in
walther@59969
   776
      Ctree.ModSpec (allcorrect, Pos.Met, hdl, meth, pre, spec)
wneuper@59265
   777
    end
walther@59957
   778
  | pt_model (Ctree.PblObj {probl, spec, origin = (_, spec', hdl), ...}) _(*Frm,Pbl*) =
wneuper@59265
   779
    let
walther@59957
   780
      val (_, pI, _) = Ctree.get_somespec' spec spec'
walther@59903
   781
      val pre = if pI = Problem.id_empty then []
wneuper@59265
   782
	      else
wneuper@59265
   783
	        let
walther@59970
   784
	          val {prls, where_, ...} = Problem.from_store pI
walther@59960
   785
	          val pre = Pre_Conds.check prls where_ probl 0
wneuper@59265
   786
	        in pre end
wneuper@59265
   787
	    val allcorrect = is_complete_mod_ probl andalso foldl and_ (true, (map #1 pre))
wneuper@59265
   788
    in
walther@59957
   789
      Ctree.ModSpec (allcorrect, Pos.Pbl, hdl, probl, pre, spec)
wneuper@59265
   790
    end
walther@59962
   791
  | pt_model _ _ = raise ERROR "pt_model: uncovered definition"
neuper@37906
   792
walther@59957
   793
fun pt_form (Ctree.PrfObj {form, ...}) = Ctree.Form form
walther@59957
   794
  | pt_form (Ctree.PblObj {probl, spec, origin = (_, spec', _), ...}) =
wneuper@59265
   795
    let
walther@59957
   796
      val (dI, pI, _) = Ctree.get_somespec' spec spec'
walther@59970
   797
      val {cas, ...} = Problem.from_store pI
wneuper@59265
   798
    in case cas of
walther@59957
   799
      NONE => Ctree.Form (Auto_Prog.pblterm dI pI)
walther@59957
   800
    | SOME t => Ctree.Form (subst_atomic (I_Model.mk_env probl) t)
wneuper@59265
   801
    end
neuper@37906
   802
wneuper@59265
   803
(* pt_extract returns
neuper@37906
   804
      # the formula at pos
neuper@37906
   805
      # the tactic applied to this formula
neuper@37906
   806
      # the list of assumptions generated at this formula
neuper@37906
   807
	(by application of another tac to the preceding formula !)
wneuper@59265
   808
   pos is assumed to come from the frontend, ie. generated by moveDown.
wneuper@59265
   809
   Notes: cannot be in ctree.sml, because ModSpec has to be calculated. 
wneuper@59265
   810
   TODO 110417 get assumptions from ctxt !?
wneuper@59265
   811
*)
walther@59969
   812
fun pt_extract (pt, ([], Pos.Res)) =
wneuper@59265
   813
    (* WN120512: returns Check_Postcondition WRONGLY see --- build fun is_exactly_equal *)
wneuper@59265
   814
    let
walther@59957
   815
      val (f, asm) = Ctree.get_obj Ctree.g_result pt []
walther@59957
   816
    in (Ctree.Form f, NONE, asm) end
walther@59969
   817
  | pt_extract (pt, (p, Pos.Res)) =
wneuper@59265
   818
    let
walther@59957
   819
      val (f, asm) = Ctree.get_obj Ctree.g_result pt p
wneuper@59265
   820
      val tac =
walther@59957
   821
        if Ctree.last_onlev pt p
wneuper@59265
   822
        then
walther@59957
   823
          if Ctree.is_pblobj' pt (Pos.lev_up p)
neuper@42437
   824
          then
wneuper@59265
   825
            let
walther@59957
   826
              val pI = case Ctree.get_obj I pt (Pos.lev_up p) of
walther@59957
   827
                Ctree.PblObj{spec = (_, pI, _), ...} => pI
walther@59962
   828
              | _ => raise ERROR "pt_extract last_onlev: uncovered case get_obj"
walther@59903
   829
            in if pI = Problem.id_empty then NONE else SOME (Tactic.Check_Postcond pI) end
wneuper@59571
   830
		      else SOME Tactic.End_Trans (* WN0502 TODO for other branches *)
wneuper@59265
   831
		    else
walther@59957
   832
		      let val p' = Pos.lev_on p
wneuper@59265
   833
		      in
walther@59957
   834
		        if Ctree.is_pblobj' pt p'
wneuper@59265
   835
		        then
wneuper@59265
   836
		          let
walther@59957
   837
		            val (dI , pI) = case Ctree.get_obj I pt p' of
walther@59957
   838
		              Ctree.PblObj{origin = (_, (dI, pI, _), _), ...} => (dI ,pI)
walther@59962
   839
		            | _ => raise ERROR "pt_extract \<not>last_onlev: uncovered case get_obj"
wneuper@59571
   840
		          in SOME (Tactic.Subproblem (dI, pI)) end
wneuper@59265
   841
		        else
walther@59957
   842
		          if f = Ctree.get_obj Ctree.g_form pt p'
walther@59957
   843
		          then SOME (Ctree.get_obj Ctree.g_tac pt p') (*because this Frm ~~~is not on worksheet*)
walther@59957
   844
		          else SOME (Tactic.Take (UnparseC.term (Ctree.get_obj Ctree.g_form pt p')))
wneuper@59265
   845
		      end
walther@59957
   846
    in (Ctree.Form f, tac, asm) end
walther@59969
   847
  | pt_extract (pt, (p, p_(*Frm,Pbl*))) =
neuper@42437
   848
      let
walther@59957
   849
        val ppobj = Ctree.get_obj I pt p
walther@59957
   850
        val f = if Ctree.is_pblobj ppobj then pt_model ppobj p_ else Ctree.get_obj pt_form pt p
walther@59957
   851
        val tac = Ctree.g_tac ppobj
wneuper@59265
   852
      in (f, SOME tac, []) end
neuper@37906
   853
walther@59969
   854
walther@59969
   855
(** get the formula from a ctree-node **)
walther@59969
   856
(*
wneuper@59265
   857
  take form+res from PblObj and 1.PrfObj and (PrfObj after PblObj)
wneuper@59265
   858
  take res from all other PrfObj's
walther@59969
   859
  Designed for interSteps, outcommented 04 in favour of calcChangedEvent
walther@59969
   860
*)
walther@59957
   861
fun formres p (Ctree.Nd (Ctree.PblObj {origin = (_, _, h), result = (r, _), ...}, _)) =
walther@59957
   862
    [("headline", (p, Pos.Frm), h), ("stepform", (p, Pos.Res), r)]
walther@59957
   863
  | formres p (Ctree.Nd (Ctree.PrfObj {form, result = (r, _), ...}, _)) = 
walther@59957
   864
    [("stepform", (p, Pos.Frm), form), ("stepform", (p, Pos.Res), r)]
walther@59962
   865
  | formres _ _ = raise ERROR "formres: uncovered definition" 
walther@59957
   866
fun form p (Ctree.Nd (Ctree.PrfObj {result = (r, _), ...}, _)) = 
walther@59957
   867
    [("stepform", (p, Pos.Res), r)]
walther@59962
   868
  | form _ _ = raise ERROR "form: uncovered definition" 
neuper@37906
   869
wneuper@59265
   870
(* assumes to take whole level, in particular hd -- for use in interSteps *)
wneuper@59265
   871
fun get_formress fs _ [] = flat fs
neuper@37906
   872
  | get_formress fs p (nd::nds) =
neuper@37906
   873
    (* start with   'form+res'       and continue with trying 'res' only*)
walther@59957
   874
    get_forms (fs @ [formres p nd]) (Pos.lev_on p) nds
wneuper@59265
   875
and get_forms fs _ [] = flat fs
neuper@37906
   876
  | get_forms fs p (nd::nds) =
walther@59957
   877
    if Ctree.is_pblnd nd
neuper@37906
   878
    (* start again with      'form+res' ///ugly repeat with Check_elementwise
neuper@37906
   879
    then get_formress (fs @ [formres p nd]) (lev_on p) nds                   *)
walther@59957
   880
    then get_forms    (fs @ [formres p nd]) (Pos.lev_on p) nds
neuper@37906
   881
    (* continue with trying 'res' only*)
walther@59957
   882
    else get_forms    (fs @ [form    p nd]) (Pos.lev_on p) nds;
neuper@37906
   883
walther@59969
   884
wneuper@59279
   885
(** get an 'interval' 'from' 'to' of formulae from a ctree **)
walther@59969
   886
wneuper@59265
   887
(* WN0401 this functionality belongs to ctree.sml *)
walther@59969
   888
fun eq_pos' (p1, Pos.Frm) (p2, Pos.Frm) = p1 = p2
walther@59969
   889
  | eq_pos' (p1, Pos.Res) (p2, Pos.Res) = p1 = p2
walther@59969
   890
  | eq_pos' (p1, Pos.Pbl) (p2, p2_) =
walther@59969
   891
    p1 = p2 andalso (case p2_ of Pos.Pbl => true | Pos.Met => true | _ => false)
walther@59969
   892
  | eq_pos' (p1, Pos.Met) (p2, p2_) =
walther@59969
   893
    p1 = p2 andalso (case p2_ of Pos.Pbl => true | Pos.Met => true | _ => false)
neuper@37906
   894
  | eq_pos' _ _ = false;
neuper@37906
   895
wneuper@59265
   896
(* get an 'interval' from the ctree; 'interval' is w.r.t. the 
neuper@37906
   897
   total ordering Position#compareTo(Position p) in the java-code
wneuper@59516
   898
val get_interval = fn :
wneuper@59516
   899
      pos' ->     : from is "move_up 1st-element" to return
neuper@37906
   900
      pos' -> 	  : to the last element to be returned; from < to
wneuper@59516
   901
      int -> 	    : level: 0 gets the flattest sub-tree possible, 999 the deepest
wneuper@59279
   902
      ctree -> 	  : 
neuper@37906
   903
      (pos' * 	  : of the formula
neuper@37906
   904
       Term.term) : the formula
wneuper@59265
   905
	  list                                                           *)
neuper@37906
   906
fun get_interval from to level pt =
neuper@42432
   907
  let
walther@59957
   908
    fun get_inter c (from) (to) lev pt =
walther@59957
   909
	    if eq_pos' from to orelse from = ([], Pos.Res)
neuper@42432
   910
	    then
wneuper@59516
   911
	      let val (f, tacopt, _) = pt_extract (pt, from)
wneuper@59265
   912
	      in case f of
walther@59957
   913
	        Ctree.ModSpec (_, _, headline, _, _, _) => c @ [(from, headline, NONE)] 
walther@59957
   914
	      | Ctree.Form t => c @ [(from, t, tacopt)]
neuper@42432
   915
	      end
neuper@37906
   916
	    else 
walther@59957
   917
	      if lev < Pos.lev_of from
walther@59957
   918
	      then (get_inter c (Ctree.move_dn [] pt from) to lev pt)
walther@59957
   919
		      handle (Ctree.PTREE _(*from move_dn too far*)) => c
neuper@42432
   920
		    else
neuper@42432
   921
		      let
wneuper@59516
   922
		        val (f, tacopt, _) = pt_extract (pt, from)
neuper@42432
   923
		        val term = case f of
walther@59957
   924
		          Ctree.ModSpec (_,_,headline,_,_,_) => headline
walther@59957
   925
				    | Ctree.Form t => t
walther@59957
   926
		      in (get_inter (c @ [(from, term, tacopt)]) (Ctree.move_dn [] pt from) to lev pt)
walther@59957
   927
		        handle (Ctree.PTREE _(*from move_dn too far*)) => c @ [(from, term, tacopt)]
neuper@42432
   928
		      end
wneuper@59265
   929
  in get_inter [] from to level pt end
neuper@37906
   930
walther@59957
   931
fun posterm2str (pos, t, _) = "(" ^ Pos.pos'2str pos ^ ", " ^ UnparseC.term t ^ ")"
wneuper@59265
   932
fun posterms2str pfs = (strs2str' o (map (curry op ^ "\n")) o (map posterm2str)) pfs
neuper@37906
   933
wneuper@59516
   934
fun postermtac2str (pos, t, SOME tac) =
walther@59957
   935
      Pos.pos'2str pos ^ ", " ^ UnparseC.term t ^ "\n" ^ indent 10 ^ Tactic.input_to_string tac
wneuper@59516
   936
  | postermtac2str (pos, t, NONE) =
walther@59957
   937
      Pos.pos'2str pos ^ ", " ^ UnparseC.term t
wneuper@59516
   938
fun postermtacs2str pfts = (strs2str' o (map (curry op ^ "\n")) o (map postermtac2str)) pfts
wneuper@59516
   939
wneuper@59265
   940
(* WN050225 omits the last step, if pt is incomplete *)
walther@59957
   941
fun show_pt pt = tracing (posterms2str (get_interval ([], Pos.Frm) ([], Pos.Res) 99999 pt))
walther@59957
   942
fun show_pt_tac pt = tracing (postermtacs2str (get_interval ([], Pos.Frm) ([], Pos.Res) 99999 pt))
neuper@37906
   943
wneuper@59265
   944
(* get a calchead from a PblObj-node in the ctree; preconditions must be calculated *)
walther@59969
   945
fun get_ocalhd (pt, (p, Pos.Pbl)) = 
wneuper@59265
   946
    let
walther@59957
   947
	    val (ospec, hdf', spec, probl) = case Ctree.get_obj I pt p of
walther@59957
   948
	      Ctree.PblObj {origin = (_, ospec, hdf'), spec, probl,...} => (ospec, hdf', spec, probl)
walther@59962
   949
	    | _ => raise ERROR "get_ocalhd Pbl: uncovered case get_obj"
walther@59970
   950
      val {prls, where_, ...} = Problem.from_store (#2 (some_spec ospec spec))
walther@59965
   951
      val pre = Pre_Conds.check' (ThyC.get_theory "Isac_Knowledge") prls where_ probl
wneuper@59265
   952
    in
walther@59969
   953
      (ocalhd_complete probl pre spec, Pos.Pbl, hdf', probl, pre, spec)
wneuper@59265
   954
    end
walther@59969
   955
  | get_ocalhd (pt, (p, Pos.Met)) = 
wneuper@59265
   956
    let
walther@59957
   957
		  val (ospec, hdf', spec, meth) = case Ctree.get_obj I pt p of
walther@59957
   958
		    Ctree.PblObj {origin = (_, ospec, hdf'), spec, meth, ...} => (ospec, hdf', spec, meth)
walther@59962
   959
		  | _ => raise ERROR "get_ocalhd Met: uncovered case get_obj"
walther@59970
   960
      val {prls, pre, ...} = Method.from_store (#3 (some_spec ospec spec))
walther@59965
   961
      val pre = Pre_Conds.check' (ThyC.get_theory "Isac_Knowledge") prls pre meth
wneuper@59265
   962
    in
walther@59969
   963
      (ocalhd_complete meth pre spec, Pos.Met, hdf', meth, pre, spec)
wneuper@59265
   964
    end
walther@59962
   965
  | get_ocalhd _ = raise ERROR "get_ocalhd: uncovered definition"
neuper@37906
   966
walther@59977
   967
(* at the activeFormula set the Specification 
wneuper@59265
   968
   to empty and return a CalcHead;
wneuper@59265
   969
   the 'origin' remains (for reconstructing all that) *)
walther@59957
   970
fun reset_calchead (pt, (p,_)) = 
wneuper@59265
   971
  let
walther@59957
   972
    val () = case Ctree.get_obj I pt p of
walther@59957
   973
      Ctree.PblObj _ => ()
walther@59962
   974
    | _ => raise ERROR "reset_calchead: uncovered case get_obj"
walther@59957
   975
    val pt = Ctree.update_pbl pt p []
walther@59957
   976
    val pt = Ctree.update_met pt p []
walther@59976
   977
    val pt = Ctree.update_spec pt p References.empty
walther@59957
   978
  in (pt, (p, Pos.Pbl)) end
neuper@37906
   979
walther@59763
   980
(**)end(**)