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