src/Tools/isac/Specify/calchead.sml
author Walther Neuper <walther.neuper@jku.at>
Thu, 07 May 2020 14:11:03 +0200
changeset 59947 3df8a1d00a24
parent 59946 c7546066881a
child 59952 3d1c6f17edac
permissions -rw-r--r--
collect code for O_Model (+ old locations)
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@59939
    98
  val is_known : Proof.context -> string -> O_Model.T -> term -> string * O_Model.single * term list
walther@59939
    99
  val is_notyet_input : Proof.context -> I_Model.T -> term list -> O_Model.single -> ('a * (term * term)) list
walther@59939
   100
    -> string * I_Model.single
wneuper@59316
   101
  val ppc2list : 'a Model.ppc -> 'a list
walther@59943
   102
  val mk_delete: theory -> string -> I_Model.feedback -> Tactic.input
walther@59865
   103
  val mk_additem: string -> TermC.as_string -> Tactic.input
walther@59943
   104
  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
   105
  val is_error: I_Model.feedback -> bool
walther@59939
   106
  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
   107
  val nxt_specif_additem: string -> TermC.as_string -> Calc.T -> calcstate'
walther@59939
   108
  val vals_of_oris : O_Model.T -> term list
walther@59865
   109
  val specify_additem: string -> TermC.as_string -> Calc.T -> string * calcstate'
wneuper@59310
   110
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
walther@59908
   111
  val e_calcstate : Calc.T * State_Steps.T
wneuper@59265
   112
  val e_calcstate' : calcstate'
walther@59886
   113
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
walther@59805
   114
  val posterms2str : (Pos.pos' * term * 'a) list -> string
walther@59805
   115
  val postermtacs2str : (Pos.pos' * term * Tactic.input option) list -> string
wneuper@59513
   116
  val is_copy_named_idstr : string -> bool
wneuper@59513
   117
  val is_copy_named_generating_idstr : string -> bool
wneuper@59513
   118
  val is_copy_named_generating : 'a * ('b * term) -> bool
wneuper@59310
   119
  val is_copy_named : 'a * ('b * term) -> bool
walther@59945
   120
  val ori2Coritm : Model_Pattern.T -> O_Model.single -> I_Model.single
walther@59942
   121
  val matc : theory -> (string * (term * term)) list -> term list -> O_Model.preori list -> O_Model.preori list
walther@59945
   122
  val mtc : theory -> Model_Pattern.single -> term -> O_Model.preori option
walther@59945
   123
  val cpy_nam : Model_Pattern.T -> O_Model.preori list -> Model_Pattern.single -> O_Model.preori
walther@59939
   124
  datatype additm = Add of I_Model.single | Err of string
walther@59939
   125
  val appl_add: Proof.context -> string -> O_Model.T ->
walther@59939
   126
    I_Model.T -> (string * (term * term)) list -> TermC.as_string -> additm
wneuper@59550
   127
  val seek_oridts: Proof.context -> ''a -> term * term list -> (int * 'b list * ''a * term * term list) list -> string * (int * 'b list * ''a * term * term list) * term list
walther@59886
   128
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
wneuper@59310
   129
wneuper@59307
   130
(*----- kept as hints to initial design ideas; NOT in src/, NOT in test ------------------------*)
wneuper@59307
   131
  val variants_in : term list -> int
walther@59939
   132
  val is_untouched : I_Model.single -> bool
wneuper@59307
   133
  val is_list_type : typ -> bool
walther@59943
   134
  val parse_ok : I_Model.feedback list -> bool
walther@59943
   135
  val all_dsc_in : I_Model.feedback list -> term list
wneuper@59307
   136
  val chktyps : theory -> term list * term list -> term list (* only in old tests*)
wneuper@59316
   137
  val chk_vars : term Model.ppc -> string * term list
wneuper@59316
   138
  val unbound_ppc : term Model.ppc -> term list
walther@59775
   139
  val is_complete_modspec : Calc.T -> bool
walther@59846
   140
  val get_formress : (string * Pos.pos' * term) list list -> Pos.pos -> Ctree.ctree list ->
walther@59846
   141
    (string * Pos.pos' * term) list
walther@59846
   142
  val get_forms : (string * Pos.pos' * term) list list -> Pos.pos -> Ctree.ctree list ->
walther@59846
   143
    (string * Pos.pos' * term) list
wneuper@59265
   144
end
neuper@37906
   145
walther@59763
   146
(**)
wneuper@59265
   147
structure Chead(**): CALC_HEAD(**) =
neuper@37906
   148
struct
walther@59763
   149
(**)
wneuper@59276
   150
open Ctree
walther@59695
   151
open Pos
neuper@37906
   152
(* datatypes *)
neuper@37906
   153
wneuper@59265
   154
(* the state wich is stored after each step of calculation; it contains
neuper@38065
   155
   the calc-state and a list of [tac,istate](="tacis") to be applied next.
neuper@37906
   156
   the last_elem tacis is the first to apply to the calc-state and
neuper@37906
   157
   the (only) one shown to the front-end as the 'proposed tac'.
neuper@37906
   158
   the calc-state resulting from the application of tacis is not stored,
neuper@38065
   159
   because the tacis hold enough information for efficiently rebuilding
wneuper@59265
   160
   this state just by "fun generate "
wneuper@59265
   161
*)
neuper@37906
   162
type calcstate = 
walther@59946
   163
  Calc.T *      (* the calc-state to which the tacis could be applied *)
walther@59946
   164
  State_Steps.T (* ev. several (hidden) steps; 
walther@59946
   165
                   in REVERSE order: first tac_ to apply is last_elem *)
walther@59908
   166
val e_calcstate = ((EmptyPtree, e_pos'), [State_Steps.single_empty]);
neuper@37906
   167
neuper@37906
   168
(*the state used during one calculation within the mathengine; it contains
neuper@37906
   169
  a list of [tac,istate](="tacis") which generated the the calc-state;
neuper@37906
   170
  while this state's tacis are extended by each (internal) step,
neuper@37906
   171
  the calc-state is used for creating new nodes in the calc-tree
neuper@37906
   172
  (eg. applicable_in requires several particular nodes of the calc-tree)
neuper@37906
   173
  and then replaced by the the newly created;
neuper@37906
   174
  on leave of the mathengine the resuing calc-state is dropped anyway,
neuper@37906
   175
  because the tacis hold enought information for efficiently rebuilding
neuper@37906
   176
  this state just by "fun generate ".*)
neuper@37906
   177
type calcstate' = 
walther@59908
   178
  State_Steps.T * (* cas. several (hidden) steps;
walther@59946
   179
                       in REVERSE order: first tac_ to apply is last_elem                 *)
walther@59946
   180
  pos' list *     (* a "continuous" sequence of pos', deleted by application of taci list *)     
walther@59946
   181
  Calc.T          (* the calc-state resulting from the application of tacis               *)
walther@59908
   182
val e_calcstate' = ([State_Steps.single_empty], [e_pos'], (EmptyPtree, e_pos')) : calcstate';
neuper@37906
   183
wneuper@59265
   184
(* is the calchead complete ? *)
wneuper@59265
   185
fun ocalhd_complete its pre (dI, pI, mI) = 
walther@59939
   186
  foldl and_ (true, map #3 (its: I_Model.T)) andalso 
wneuper@59426
   187
  foldl and_ (true, map #1 (pre: (bool * term) list)) andalso 
walther@59903
   188
  dI <> ThyC.id_empty andalso pI <> Problem.id_empty andalso mI <> Method.id_empty
neuper@37906
   189
wneuper@59265
   190
(* ["BOOL (1+x=2)","REAL x"] --match_ags--> oris 
wneuper@59265
   191
   --oris2fmz_vals--> ["equality (1+x=2)","boundVariable x","solutions L"] *)
neuper@42092
   192
fun oris2fmz_vals oris =
wneuper@59308
   193
  let fun ori2fmz_vals (_, _, _, dsc, ts) = 
walther@59947
   194
	  ((UnparseC.term o O_Model.comp_dts') (dsc, ts), last_elem ts) 
walther@59868
   195
	  handle _ => error ("ori2fmz_env called with " ^ UnparseC.terms ts)
wneuper@59265
   196
  in (split_list o (map ori2fmz_vals)) oris end
neuper@37906
   197
neuper@37906
   198
(* make a term 'typeless' for comparing with another 'typeless' term;
neuper@37906
   199
   'type-less' usually is illtyped                                  *)
walther@59878
   200
fun typeless (Const (s, _)) = (Const (s, TermC.typ_empty)) 
walther@59878
   201
  | typeless (Free (s, _)) = (Free (s, TermC.typ_empty))
walther@59878
   202
  | typeless (Var (n, _)) = (Var (n, TermC.typ_empty))
neuper@37906
   203
  | typeless (Bound i) = (Bound i)
walther@59878
   204
  | typeless (Abs (s, _,t)) = Abs(s, TermC.typ_empty, typeless t)
wneuper@59265
   205
  | typeless (t1 $ t2) = (typeless t1) $ (typeless t2)
neuper@37906
   206
wneuper@59265
   207
(* to an input (d,ts) find the according ori and insert the ts)
wneuper@59265
   208
  WN.11.03: + dont take first inter<>[]                       *)
wneuper@59265
   209
fun seek_oridts ctxt sel (d, ts) [] =
bonzai@41952
   210
    ("seek_oridts: input ('" ^
walther@59947
   211
        (UnparseC.term_in_ctxt ctxt (O_Model.comp_dts (d, ts))) ^ "') not found in oris (typed)",
bonzai@41952
   212
      (0, [], sel, d, ts),
bonzai@41952
   213
      [])
wneuper@59265
   214
  | seek_oridts ctxt sel (d, ts) ((id, vat, sel', d', ts') :: oris) =
wneuper@59265
   215
    if sel = sel' andalso d = d' andalso (inter op = ts ts') <> []
wneuper@59265
   216
    then ("", (id, vat, sel, d, inter op = ts ts'), ts')
wneuper@59265
   217
    else seek_oridts ctxt sel (d, ts) oris
neuper@37906
   218
wneuper@59265
   219
(* to an input (_,ts) find the according ori and insert the ts *)
wneuper@59265
   220
fun seek_orits ctxt _ ts [] = 
walther@59870
   221
    ("seek_orits: input (_, '" ^ strs2str (map (UnparseC.term_in_ctxt ctxt) ts) ^
walther@59942
   222
    "') not found in oris (typed)", O_Model.single_empty, [])
wneuper@59308
   223
  | seek_orits ctxt sel ts ((id, vat, sel', d, ts') :: oris) =
neuper@37934
   224
    if sel = sel' andalso (inter op = ts ts') <> [] 
wneuper@59265
   225
    then
wneuper@59265
   226
      if sel = sel' 
wneuper@59308
   227
      then ("", (id, vat, sel, d, inter op = ts ts'), ts')
walther@59942
   228
      else (((strs2str' o map (UnparseC.term_in_ctxt ctxt)) ts) ^ " not for " ^ sel, O_Model.single_empty, [])
wneuper@59265
   229
    else seek_orits ctxt sel ts oris
neuper@37906
   230
wneuper@59265
   231
(* find_first item with #1 equal to id *)
wneuper@59265
   232
fun seek_ppc _ [] = NONE
walther@59939
   233
  | seek_ppc id (p :: ppc) = if id = #1 (p: I_Model.single) then SOME p else seek_ppc id ppc
neuper@37906
   234
wneuper@59308
   235
fun ppc2list {Given = gis, Where = whs, Find = fis, With = wis, Relate = res} =
wneuper@59265
   236
  gis @ whs @ fis @ wis @ res
neuper@37906
   237
neuper@37906
   238
(* get the number of variants in a problem in 'original',
neuper@37906
   239
   assumes equal descriptions in immediate sequence    *)
neuper@37906
   240
fun variants_in ts =
wneuper@59265
   241
  let
wneuper@59265
   242
    fun eq (x, y) = head_of x = head_of y
wneuper@59265
   243
    fun cnt _ [] _ n = ([n], [])
wneuper@59265
   244
      | cnt eq (x :: xs) y n = if eq (x, y) then cnt eq xs y (n + 1) else ([n], x :: xs)
wneuper@59265
   245
    fun coll _  xs [] = xs
wneuper@59265
   246
      | coll eq  xs (y :: ys) = 
wneuper@59265
   247
        let val (n, ys') = cnt eq (y :: ys) y 0
wneuper@59265
   248
        in if ys' = [] then xs @ n else coll eq  (xs @ n) ys' end
wneuper@59265
   249
    val vts = subtract op = [1] (distinct (coll eq [] ts))
wneuper@59265
   250
  in
wneuper@59265
   251
    case vts of 
wneuper@59265
   252
      [] => 1
wneuper@59265
   253
    | [n] => n
wneuper@59265
   254
    | _ => error "different variants in formalization"
wneuper@59265
   255
  end
neuper@37906
   256
wneuper@59265
   257
fun is_list_type (Type ("List.list", _)) = true
wneuper@59265
   258
  | is_list_type _ = false
walther@59943
   259
fun is_parsed (I_Model.Syn _) = false
wneuper@59265
   260
  | is_parsed _ = true
wneuper@59265
   261
fun parse_ok its = foldl and_ (true, map is_parsed its)
neuper@37906
   262
neuper@37906
   263
fun all_dsc_in itm_s =
neuper@37906
   264
  let    
walther@59943
   265
    fun d_in (I_Model.Cor ((d, _), _)) = [d]
walther@59943
   266
      | d_in (I_Model.Syn _) = []
walther@59943
   267
      | d_in (I_Model.Typ _) = []
walther@59943
   268
      | d_in (I_Model.Inc ((d,_),_)) = [d]
walther@59943
   269
      | d_in (I_Model.Sup (d,_)) = [d]
walther@59943
   270
      | d_in (I_Model.Mis (d,_)) = [d]
walther@59946
   271
      | d_in i = raise ERROR ("all_dsc_in: uncovered case with " ^ I_Model.feedback_to_string' i)
wneuper@59265
   272
  in (flat o (map d_in)) itm_s end; 
neuper@37906
   273
walther@59943
   274
fun is_error (I_Model.Cor _) = false
walther@59943
   275
  | is_error (I_Model.Sup _) = false
walther@59943
   276
  | is_error (I_Model.Inc _) = false
walther@59943
   277
  | is_error (I_Model.Mis _) = false
wneuper@59265
   278
  | is_error _ = true
neuper@37906
   279
wneuper@59265
   280
(* get the first term in ts from ori *)
wneuper@59308
   281
fun getr_ct thy (_, _, fd, d, ts) =
walther@59947
   282
  (fd, ((UnparseC.term_in_thy thy) o O_Model.comp_dts) (d,[hd ts]))
neuper@37906
   283
neuper@38051
   284
(* get a term from ori, notyet input in itm.
neuper@38051
   285
   the term from ori is thrown back to a string in order to reuse
neuper@38051
   286
   machinery for immediate input by the user. *)
wneuper@59308
   287
fun geti_ct thy (_, _, _, d, ts) (_, _, _, fd, itm_) =
walther@59947
   288
  (fd, ((UnparseC.term_in_thy thy) o O_Model.comp_dts) (d, subtract op = (I_Model.ts_in itm_) ts))
neuper@37906
   289
neuper@37906
   290
(* in FE dsc, not dat: this is in itms ...*)
walther@59943
   291
fun is_untouched (_, _, false, _, I_Model.Inc ((_, []), _)) = true
wneuper@59265
   292
  | is_untouched _ = false
neuper@37906
   293
neuper@37906
   294
(* select an item in oris, notyet input in itms 
walther@59943
   295
   (precondition: in itms are only I_Model.Cor, I_Model.Sup, I_Model.Inc) *)
wneuper@59235
   296
(*args of nxt_add
wneuper@59235
   297
  thy : for?
wneuper@59235
   298
  oris: from formalization 'type fmz', structured for efficient access 
wneuper@59235
   299
  pbt : the problem-pattern to be matched with oris in order to get itms
wneuper@59235
   300
  itms: already input items
wneuper@59235
   301
*)
wneuper@59308
   302
fun nxt_add thy [] pbt itms = (*root (only) ori...fmz=[]*)
wneuper@59265
   303
    let
walther@59943
   304
      fun test_d d (i, _, _, _, itm_) = (d = (I_Model.d_in itm_)) andalso i <> 0
wneuper@59265
   305
      fun is_elem itms (_, (d, _)) = 
wneuper@59265
   306
        case find_first (test_d d) itms of SOME _ => true | NONE => false
wneuper@59265
   307
    in
wneuper@59265
   308
      case filter_out (is_elem itms) pbt of
walther@59947
   309
        (f, (d, _)) :: _ => SOME (f, ((UnparseC.term_in_thy thy) o O_Model.comp_dts) (d, []))
wneuper@59265
   310
      | _ => NONE
wneuper@59265
   311
    end
wneuper@59265
   312
  | nxt_add thy oris _ itms =
wneuper@59265
   313
    let
walther@59939
   314
      fun testr_vt v ori = member op= (#2 (ori : O_Model.single)) v andalso (#3 ori) <> "#undef"
walther@59939
   315
      fun testi_vt v itm = member op= (#2 (itm : I_Model.single)) v
walther@59939
   316
      fun test_id ids r = member op= ids (#1 (r : O_Model.single))
wneuper@59426
   317
      fun test_subset itm (_, _, _, d, ts) = 
walther@59943
   318
        (I_Model.d_in (#5 (itm: I_Model.single))) = d andalso subset op = (I_Model.ts_in (#5 itm), ts)
walther@59943
   319
      fun false_and_not_Sup (_, _, false, _, I_Model.Sup _) = false
wneuper@59265
   320
        | false_and_not_Sup (_, _, false, _, _) = true
wneuper@59265
   321
        | false_and_not_Sup _ = false
walther@59943
   322
      val v = if itms = [] then 1 else I_Model.max_vt itms
wneuper@59265
   323
      val vors = if v = 0 then oris else filter (testr_vt v) oris  (* oris..vat *)
wneuper@59265
   324
      val vits =
wneuper@59265
   325
        if v = 0
wneuper@59265
   326
        then itms                                 (* because of dsc without dat *)
wneuper@59265
   327
  	    else filter (testi_vt v) itms;                             (* itms..vat *)
wneuper@59265
   328
      val icl = filter false_and_not_Sup vits;                    (* incomplete *)
wneuper@59265
   329
    in
wneuper@59265
   330
      if icl = [] 
wneuper@59265
   331
      then case filter_out (test_id (map #1 vits)) vors of
wneuper@59265
   332
  	    [] => NONE
wneuper@59265
   333
  	  | miss => SOME (getr_ct thy (hd miss))
wneuper@59265
   334
      else
wneuper@59265
   335
        case find_first (test_subset (hd icl)) vors of
wneuper@59265
   336
          NONE => error "nxt_add: types or dsc DO NOT MATCH BETWEEN fmz --- pbt"
wneuper@59265
   337
        | SOME ori => SOME (geti_ct thy ori (hd icl))
wneuper@59265
   338
    end
neuper@37906
   339
wneuper@59571
   340
fun mk_delete thy "#Given" itm_ = Tactic.Del_Given (Specify.itm_out thy itm_)
wneuper@59571
   341
  | mk_delete thy "#Find" itm_ = Tactic.Del_Find (Specify.itm_out thy itm_)
wneuper@59571
   342
  | mk_delete thy "#Relate" itm_ = Tactic.Del_Relation (Specify.itm_out thy itm_)
wneuper@59265
   343
  | mk_delete _ str _ = error ("mk_delete: called with field \"" ^ str ^ "\"")
wneuper@59571
   344
fun mk_additem "#Given" ct = Tactic.Add_Given ct
wneuper@59571
   345
  | mk_additem "#Find" ct = Tactic.Add_Find ct    
wneuper@59571
   346
  | mk_additem "#Relate"ct = Tactic.Add_Relation ct
wneuper@59265
   347
  | mk_additem str _ = error ("mk_additem: called with field \"" ^ str ^ "\"")
neuper@37906
   348
neuper@38051
   349
(* determine the next step of specification;
neuper@38051
   350
   not done here: Refine_Tacitly (otherwise *** unknown method: (..., no_met))
neuper@38051
   351
   eg. in rootpbl 'no_met': 
neuper@37906
   352
args:
neuper@38051
   353
  preok          predicates are _all_ ok (and problem matches completely)
neuper@37906
   354
  oris           immediately from formalization 
neuper@37906
   355
  (dI',pI',mI')  specification coming from author/parent-problem
neuper@37906
   356
  (pbl,          item lists specified by user
neuper@37906
   357
   met)          -"-, tacitly completed by copy_probl
neuper@37906
   358
  (dI,pI,mI)     specification explicitly done by the user
neuper@37906
   359
  (pbt, mpc)     problem type, guard of method
neuper@38051
   360
*)
wneuper@59308
   361
fun nxt_spec Pbl preok oris (dI', pI', mI') (pbl, met) (pbt, mpc) (dI, pI, mI) = 
walther@59879
   362
    (if dI' = ThyC.id_empty andalso dI = ThyC.id_empty then (Pbl, Tactic.Specify_Theory dI')
walther@59903
   363
     else if pI' = Problem.id_empty andalso pI = Problem.id_empty then (Pbl, Tactic.Specify_Problem pI')
wneuper@59265
   364
     else
wneuper@59308
   365
       case find_first (is_error o #5) pbl of
wneuper@59265
   366
	       SOME (_, _, _, fd, itm_) =>
walther@59881
   367
	         (Pbl, mk_delete (ThyC.get_theory (if dI = ThyC.id_empty then dI' else dI)) fd itm_)
wneuper@59265
   368
	     | NONE => 
walther@59881
   369
	       (case nxt_add (ThyC.get_theory (if dI = ThyC.id_empty then dI' else dI)) oris pbt pbl of
walther@59762
   370
	          SOME (fd, ct') => (Pbl, mk_additem fd ct')
wneuper@59265
   371
	        | NONE => (*pbl-items complete*)
wneuper@59571
   372
	          if not preok then (Pbl, Tactic.Refine_Problem pI')
walther@59879
   373
	          else if dI = ThyC.id_empty then (Pbl, Tactic.Specify_Theory dI')
walther@59903
   374
		        else if pI = Problem.id_empty then (Pbl, Tactic.Specify_Problem pI')
walther@59903
   375
		        else if mI = Method.id_empty then (Pbl, Tactic.Specify_Method mI')
wneuper@59265
   376
		        else
wneuper@59265
   377
			        case find_first (is_error o #5) met of
walther@59881
   378
			          SOME (_, _, _, fd, itm_) => (Met, mk_delete (ThyC.get_theory dI) fd itm_)
wneuper@59265
   379
			        | NONE => 
walther@59881
   380
			          (case nxt_add (ThyC.get_theory dI) oris mpc met of
wneuper@59265
   381
				          SOME (fd, ct') => (Met, mk_additem fd ct') (*30.8.01: pre?!?*)
wneuper@59571
   382
				        | NONE => (Met, Tactic.Apply_Method mI))))
wneuper@59265
   383
  | nxt_spec Met preok oris (dI', pI', _) (_, met) (_ ,mpc) (dI, pI, mI) = 
wneuper@59265
   384
    (case find_first (is_error o #5) met of
walther@59881
   385
      SOME (_,_,_,fd,itm_) => (Met, mk_delete (ThyC.get_theory (if dI = ThyC.id_empty then dI' else dI)) fd itm_)
wneuper@59265
   386
    | NONE => 
walther@59881
   387
      case nxt_add (ThyC.get_theory (if dI = ThyC.id_empty then dI' else dI)) oris mpc met of
wneuper@59265
   388
	      SOME (fd,ct') => (Met, mk_additem fd ct')
wneuper@59265
   389
      | NONE => 
walther@59879
   390
	      (if dI = ThyC.id_empty then (Met, Tactic.Specify_Theory dI')
walther@59903
   391
	       else if pI = Problem.id_empty then (Met, Tactic.Specify_Problem pI')
wneuper@59571
   392
		     else if not preok then (Met, Tactic.Specify_Method mI)
wneuper@59571
   393
		     else (Met, Tactic.Apply_Method mI)))
wneuper@59265
   394
  | nxt_spec p _ _ _ _ _ _ = error ("nxt_spec: uncovered case with " ^ pos_2str p)
neuper@37906
   395
wneuper@59265
   396
(* update the itm_ already input, all..from ori *)
wneuper@59308
   397
fun ori_2itm itm_ pid all (id, vt, fd, d, ts) = 
neuper@37906
   398
  let 
walther@59943
   399
    val ts' = union op = (I_Model.ts_in itm_) ts;
wneuper@59316
   400
    val pval = Model.pbl_ids' d ts'
wneuper@59265
   401
	  (* WN.9.5.03: FIXXXME [#0, epsilon] here would upd_penv be called for [#0, epsilon] etc *)
wneuper@59265
   402
    val complete = if eq_set op = (ts', all) then true else false
wneuper@59265
   403
  in
wneuper@59265
   404
    case itm_ of
walther@59943
   405
      (I_Model.Cor _) => 
walther@59943
   406
        (if fd = "#undef" then (id, vt, complete, fd, I_Model.Sup (d, ts')) 
walther@59943
   407
	       else (id, vt, complete, fd, I_Model.Cor ((d, ts'), (pid, pval))))
walther@59943
   408
    | (I_Model.Syn c) => error ("ori_2itm wants to overwrite " ^ c)
walther@59943
   409
    | (I_Model.Typ c) => error ("ori_2itm wants to overwrite " ^ c)
walther@59943
   410
    | (I_Model.Inc _) =>
wneuper@59265
   411
      if complete
walther@59943
   412
  	  then (id, vt, true, fd, I_Model.Cor ((d, ts'), (pid, pval)))
walther@59943
   413
  	  else (id, vt, false, fd, I_Model.Inc ((d, ts'), (pid, pval)))
walther@59943
   414
    | (I_Model.Sup (d,ts')) => (*4.9.01 lost env*)
walther@59943
   415
  	  (*if fd = "#undef" then*) (id,vt,complete,fd,I_Model.Sup(d,ts'))
walther@59943
   416
  	  (*else (id,vt,complete,fd,I_Model.Cor((d,ts'),e))*)
wneuper@59265
   417
      (* 28.1.00: not completely clear ---^^^ etc.*)
walther@59943
   418
    | (I_Model.Mis _) => (* 4.9.01: I_Model.Mis just copied *)
wneuper@59265
   419
       if complete
walther@59943
   420
  		 then (id, vt, true, fd, I_Model.Cor ((d,ts'), (pid, pval)))
walther@59943
   421
  		 else (id, vt, false, fd, I_Model.Inc ((d,ts'), (pid, pval)))
walther@59942
   422
    | i => error ("ori_2itm: uncovered case of "^ I_Model.feedback_to_string' i)
wneuper@59265
   423
  end
neuper@37906
   424
wneuper@59265
   425
fun eq1 d (_, (d', _)) = (d = d')
walther@59943
   426
fun eq3 f d (_, _, _, f', itm_) = f = f' andalso d = (I_Model.d_in itm_) 
neuper@37906
   427
neuper@37906
   428
(* 'all' ts from ori; ts is the input; (ori carries rest of info)
neuper@37906
   429
   9.01: this + ori_2itm is _VERY UNCLEAR_ ? overhead ?
neuper@37906
   430
   pval: value for problem-environment _NOT_ checked for 'inter' --
neuper@37906
   431
   -- FIXXME.WN.11.03 the generation of penv has to go to insert_ppc
neuper@37906
   432
  (as it has been done for input_icalhd+insert_ppc' in 11.03)*)
neuper@37906
   433
(*. is_input ori itms <=> 
neuper@37906
   434
    EX itm. (1) ori(field,dsc) = itm(field,dsc) & (2..4)
neuper@37906
   435
            (2) ori(ts) subset itm(ts)        --- Err "already input"       
neuper@37906
   436
	    (3) ori(ts) inter itm(ts) = empty --- new: ori(ts)
neuper@37906
   437
	    (4) -"- <> empty                  --- new: ori(ts) \\ inter .*)
wneuper@59308
   438
fun is_notyet_input ctxt itms all (i, v, f, d, ts) pbt =
neuper@37906
   439
  case find_first (eq1 d) pbt of
wneuper@59265
   440
    SOME (_, (_, pid)) =>
neuper@37906
   441
      (case find_first (eq3 f d) itms of
neuper@52070
   442
        SOME (_,_,_,_,itm_) =>
walther@59943
   443
          let val ts' = inter op = (I_Model.ts_in itm_) ts
wneuper@59265
   444
          in 
wneuper@59265
   445
            if subset op = (ts, ts') 
walther@59942
   446
            then (((strs2str' o map (UnparseC.term_in_ctxt ctxt)) ts') ^ " already input", I_Model.empty) (*2*)
wneuper@59265
   447
	          else ("", ori_2itm itm_ pid all (i,v,f,d, subtract op = ts' ts))              (*3,4*)
neuper@52070
   448
	          end
walther@59943
   449
	    | NONE => ("", ori_2itm (I_Model.Inc ((TermC.empty, []), (pid, []))) pid all (i, v, f, d, ts)))    (*1*)
walther@59943
   450
  | NONE => ("", ori_2itm (I_Model.Sup (d, ts)) TermC.empty all (i, v, f, d, ts))
neuper@37906
   451
bonzai@41949
   452
fun test_types ctxt (d,ts) =
neuper@37906
   453
  let 
walther@59947
   454
    val opt = (try O_Model.comp_dts) (d, ts)
neuper@37906
   455
    val msg = case opt of 
neuper@37926
   456
      SOME _ => "" 
walther@59870
   457
    | NONE => (UnparseC.term_in_ctxt ctxt d ^ " " ^
walther@59870
   458
	    (strs2str' o map (UnparseC.term_in_ctxt ctxt)) ts ^ " is illtyped")
wneuper@59265
   459
  in msg end
neuper@37906
   460
wneuper@59533
   461
(* is the term t input (or taken from fmz) known in oris ?
wneuper@59265
   462
   give feedback on all(?) strange input;
wneuper@59265
   463
   return _all_ terms already input to this item (e.g. valuesFor a,b) *)
bonzai@41949
   464
fun is_known ctxt sel ori t =
neuper@37906
   465
  let
wneuper@59308
   466
    val ots = (distinct o flat o (map #5)) ori
wneuper@59389
   467
    val oids = ((map (fst o dest_Free)) o distinct o flat o (map TermC.vars)) ots
walther@59947
   468
    val (d, ts) = O_Model.split_dts t
wneuper@59389
   469
    val ids = map (fst o dest_Free) ((distinct o (flat o (map TermC.vars))) ts)
wneuper@59265
   470
  in
wneuper@59265
   471
    if (subtract op = oids ids) <> []
walther@59942
   472
    then ("identifiers " ^ strs2str' (subtract op = oids ids) ^ " not in example", O_Model.single_empty, [])
wneuper@59265
   473
    else 
walther@59861
   474
	    if d = TermC.empty 
wneuper@59265
   475
	    then 
wneuper@59265
   476
	      if not (subset op = (map typeless ts, map typeless ots))
walther@59870
   477
	      then ("terms '" ^ (strs2str' o (map (UnparseC.term_in_ctxt ctxt))) ts ^
walther@59942
   478
		      "' not in example (typeless)", O_Model.single_empty, [])
wneuper@59265
   479
	      else 
wneuper@59265
   480
          (case seek_orits ctxt sel ts ori of
wneuper@59265
   481
		         ("", ori_ as (_,_,_,d,ts), all) =>
wneuper@59265
   482
		            (case test_types ctxt (d,ts) of
wneuper@59265
   483
		              "" => ("", ori_, all)
walther@59942
   484
		            | msg => (msg, O_Model.single_empty, []))
walther@59942
   485
		       | (msg, _, _) => (msg, O_Model.single_empty, []))
wneuper@59265
   486
	    else 
wneuper@59265
   487
	      if member op = (map #4 ori) d
wneuper@59265
   488
	      then seek_oridts ctxt sel (d, ts) ori
walther@59870
   489
	      else (UnparseC.term_in_ctxt ctxt d ^ " not in example", (0, [], sel, d, ts), [])
wneuper@59265
   490
  end
neuper@37906
   491
wneuper@59265
   492
neuper@37906
   493
datatype additm =
walther@59939
   494
	Add of I_Model.single   (* return-value of appl_add *)
wneuper@59308
   495
| Err of string       (* error-message            *)
neuper@37906
   496
wneuper@59265
   497
(* add an item to the model; check wrt. oris and pbt.
wneuper@59265
   498
   in contrary to oris<>[] below, this part handles user-input
wneuper@59265
   499
   extremely acceptive, i.e. accept input instead error-msg  *)
wneuper@59265
   500
fun appl_add ctxt sel [] ppc pbt ct =
wneuper@59265
   501
    let
wneuper@59265
   502
      val i = 1 + (if ppc = [] then 0 else map #1 ppc |> maxl)
wneuper@59265
   503
      in 
wneuper@59389
   504
        case TermC.parseNEW ctxt ct of
walther@59943
   505
          NONE => Add (i, [], false, sel, I_Model.Syn ct)
wneuper@59265
   506
        | SOME t =>
walther@59947
   507
            let val (d, ts) = O_Model.split_dts t
wneuper@59265
   508
            in 
walther@59861
   509
              if d = TermC.empty 
walther@59943
   510
              then Add (i, [], false, sel, I_Model.Mis (Specify.dsc_unknown, hd ts)) 
wneuper@59265
   511
              else
wneuper@59265
   512
                (case find_first (eq1 d) pbt of
walther@59943
   513
                   NONE => Add (i, [], true, sel, I_Model.Sup (d,ts))
wneuper@59265
   514
                 | SOME (f, (_, id)) =>
walther@59943
   515
                     let fun eq2 d (i, _, _, _, itm_) = d = (I_Model.d_in itm_) andalso i <> 0
wneuper@59265
   516
                     in case find_first (eq2 d) ppc of
walther@59943
   517
                       NONE => Add (i, [], true, f,I_Model.Cor ((d, ts), (id, Model.pbl_ids' d ts)))
wneuper@59265
   518
                     | SOME (i', _, _, _, itm_) => 
wneuper@59595
   519
                         if Input_Descript.is_list_dsc d 
wneuper@59265
   520
                         then 
wneuper@59265
   521
                           let
walther@59943
   522
                             val in_itm = I_Model.ts_in itm_
wneuper@59265
   523
                             val ts' = union op = ts in_itm
wneuper@59265
   524
                             val i'' = if in_itm = [] then i else i'
walther@59943
   525
                           in Add (i'', [], true, f, I_Model.Cor ((d, ts'), (id, Model.pbl_ids' d ts')))end
walther@59943
   526
                         else Add (i', [], true, f, I_Model.Cor ((d, ts), (id, Model.pbl_ids' d ts)))
wneuper@59265
   527
                     end)
wneuper@59265
   528
            end
wneuper@59265
   529
    end
wneuper@59265
   530
  | appl_add ctxt sel oris ppc pbt str =
wneuper@59389
   531
    case TermC.parseNEW ctxt str of
wneuper@59265
   532
      NONE => Err ("appl_add: syntax error in '" ^ str ^ "'")
wneuper@59265
   533
    | SOME t => 
wneuper@59265
   534
        case is_known ctxt sel oris t of
wneuper@59265
   535
          ("", ori', all) => 
wneuper@59265
   536
            (case is_notyet_input ctxt ppc all ori' pbt of
wneuper@59265
   537
               ("", itm) => Add itm
wneuper@59265
   538
             | (msg, _) => Err ("[error] appl_add: is_notyet_input: " ^ msg))
wneuper@59265
   539
        | (msg, _, _) => Err ("[error] appl_add: is_known: " ^ msg);
neuper@37906
   540
wneuper@59265
   541
(* make oris from args of the stac SubProblem and from pbt.
wneuper@59265
   542
   can this formal argument (of a model-pattern) be omitted in the arg-list
wneuper@59265
   543
   of a SubProblem ? see calcelems.sml 'type met '                        *)
wneuper@59265
   544
fun is_copy_named_idstr str =
wneuper@59265
   545
  case (rev o Symbol.explode) str of
wneuper@59540
   546
	  "'" :: _ :: "'" :: _ => true
wneuper@59540
   547
  | _ => false
wneuper@59389
   548
fun is_copy_named (_, (_, t)) = (is_copy_named_idstr o TermC.free2str) t
neuper@41973
   549
wneuper@59265
   550
(* should this formal argument (of a model-pattern) create a new identifier? *)
wneuper@59265
   551
fun is_copy_named_generating_idstr str =
wneuper@59265
   552
  if is_copy_named_idstr str
wneuper@59265
   553
  then
wneuper@59265
   554
    case (rev o Symbol.explode) str of
wneuper@59265
   555
	    "'" :: "'" :: "'" :: _ => false
wneuper@59265
   556
    | _ => true
wneuper@59265
   557
  else false
wneuper@59389
   558
fun is_copy_named_generating (_, (_, t)) = (is_copy_named_generating_idstr o TermC.free2str) t
neuper@37906
   559
wneuper@59265
   560
(* split type-wrapper from scr-arg and build part of an ori;
wneuper@59265
   561
   an type-error is reported immediately, raises an exn, 
wneuper@59265
   562
   subsequent handling of exn provides 2nd part of error message *)
wneuper@59405
   563
fun mtc thy (str, (dsc, _)) (ty $ var) =
wneuper@59265
   564
    ((Thm.global_cterm_of thy (dsc $ var);(*type check*)
wneuper@59308
   565
      SOME (([1], str, dsc, (*[var]*)
walther@59947
   566
	    O_Model.split_dts' (dsc, var))) (*:ori without leading #*))
wneuper@59265
   567
      handle e as TYPE _ => 
wneuper@59265
   568
	      (tracing (dashs 70 ^ "\n"
wneuper@59265
   569
	        ^ "*** ERROR while creating the items for the model of the ->problem\n"
wneuper@59265
   570
	        ^ "*** from the ->stac with ->typeconstructor in arglist:\n"
walther@59868
   571
	        ^ "*** item (->description ->value): " ^ UnparseC.term dsc ^ " " ^ UnparseC.term var ^ "\n"
wneuper@59389
   572
	        ^ "*** description: " ^ TermC.term_detail2str dsc
wneuper@59389
   573
	        ^ "*** value: " ^ TermC.term_detail2str var
wneuper@59389
   574
	        ^ "*** typeconstructor in script: " ^ TermC.term_detail2str ty
walther@59880
   575
	        ^ "*** checked by theory: " ^ Context.theory_name thy ^ "\n"
wneuper@59265
   576
	        ^ "*** " ^ dots 66);
wneuper@59337
   577
          writeln (@{make_string} e);
wneuper@59338
   578
          Exn.reraise e; (*raise ERROR "actual args do not match formal args";FIXXXME.WN100916*)
wneuper@59265
   579
      NONE))
walther@59868
   580
  | mtc _ _ t = error ("mtc: uncovered case with" ^ UnparseC.term t)
neuper@38010
   581
wneuper@59265
   582
(* match each pat of the model-pattern with an actual argument;
wneuper@59265
   583
   precondition: copy-named vars are filtered out            *)
wneuper@59405
   584
fun matc _ [] _ oris = oris
wneuper@59265
   585
  | matc _ pbt [] _ =
neuper@38015
   586
    (tracing (dashs 70);
walther@59945
   587
     error ("actual arg(s) missing for '" ^ Model_Pattern.to_string pbt ^ "' i.e. should be 'copy-named' by '*_._'"))
wneuper@59265
   588
  | matc thy ((p as (_, (_, t))) :: pbt) (a :: ags) oris =
wneuper@59389
   589
    (*del?..*)if (is_copy_named_idstr o TermC.free2str) t then oris
wneuper@59265
   590
    else(*..del?*)
wneuper@59265
   591
      let val opt = mtc thy p a
wneuper@59265
   592
      in
wneuper@59265
   593
        case opt of
wneuper@59265
   594
          SOME ori => matc thy pbt ags (oris @ [ori])
neuper@37926
   595
	      | NONE => [](*WN050903 skipped by exn handled in match_ags*)
wneuper@59265
   596
	 end
neuper@38011
   597
neuper@38011
   598
(* generate a new variable "x_i" name from a related given one "x"
neuper@38011
   599
   by use of oris relating "v_v'i'" (is_copy_named!) to "v_v"
neuper@38012
   600
   e.g. (v_v, x) & (v_v'i', ?) --> (v_v'i', x_i),
neuper@38012
   601
   but leave is_copy_named_generating as is, e.t. ss''' *)
wneuper@59405
   602
fun cpy_nam pbt oris (p as (field, (dsc, t))) =
neuper@37906
   603
  (if is_copy_named_generating p
neuper@37906
   604
   then (*WN051014 kept strange old code ...*)
walther@59947
   605
     let fun sel (_,_,d,ts) = O_Model.comp_ts (d, ts) 
wneuper@59389
   606
       val cy' = (implode o (drop_last_n 3) o Symbol.explode o TermC.free2str) t
wneuper@59389
   607
       val ext = (last_elem o drop_last o Symbol.explode o TermC.free2str) t
wneuper@59513
   608
       val vars' = map (Term.term_name o snd o snd) pbt (*cpy-nam filtered_out*)
wneuper@59265
   609
       val vals = map sel oris
wneuper@59513
   610
       val cy_ext = (Term.term_name o the) (assoc (vars' ~~ vals, cy')) ^ "_" ^ ext
wneuper@59389
   611
     in ([1], field, dsc, [TermC.mk_free (type_of t) cy_ext]) end
neuper@37906
   612
   else ([1], field, dsc, [t])
walther@59868
   613
	) handle _ => error ("cpy_nam: for "^ UnparseC.term t)
neuper@37906
   614
wneuper@59265
   615
(* match the actual arguments of a SubProblem with a model-pattern
neuper@37906
   616
   and create an ori list (in root-pbl created from formalization).
neuper@37906
   617
   expects ags:pats = 1:1, while copy-named are filtered out of pats;
wneuper@59540
   618
   if no 1:1 then exn raised by matc/mtc and handled at call.
wneuper@59265
   619
   copy-named pats are appended in order to get them into the model-items *)
wneuper@59405
   620
fun match_ags thy pbt ags =
wneuper@59540
   621
  let
wneuper@59540
   622
    fun flattup (i, (var, bool, str, itm_)) = (i, var, bool, str, itm_)
wneuper@59265
   623
    val pbt' = filter_out is_copy_named pbt
wneuper@59540
   624
    val cy = filter is_copy_named pbt  (* cy is NOT a (formal) argument, but the fun's result *)
wneuper@59265
   625
    val oris' = matc thy pbt' ags []
wneuper@59265
   626
    val cy' = map (cpy_nam pbt' oris') cy
walther@59947
   627
    val ors = O_Model.add_id (oris' @ cy') (*...appended in order to get into the model-items *)
wneuper@59308
   628
  in (map flattup ors) end
neuper@37906
   629
wneuper@59265
   630
(* report part of the error-msg which is not available in match_args *)
neuper@37906
   631
fun match_ags_msg pI stac ags =
wneuper@59265
   632
  let
wneuper@59269
   633
    val pats = (#ppc o Specify.get_pbt) pI
wneuper@59405
   634
    val msg = (dots 70 ^ "\n"
wneuper@59405
   635
       ^ "*** problem " ^ strs2str pI ^ " has the ...\n"
walther@59945
   636
       ^ "*** model-pattern " ^ Model_Pattern.to_string pats ^ "\n"
walther@59868
   637
       ^ "*** stac   '" ^ UnparseC.term stac ^ "' has the ...\n"
walther@59868
   638
       ^ "*** arg-list " ^ UnparseC.terms ags ^ "\n"
wneuper@59265
   639
       ^ dashs 70)
wneuper@59265
   640
	  (*WN100921 ^^^^ expect TYPE errormsg below; lost with Isa09*)
wneuper@59265
   641
  in tracing msg end
neuper@37906
   642
wneuper@59265
   643
(* get the variables out of a pbl_; FIXME.WN.0311: is_copy_named ...obscure!!! *)
neuper@37906
   644
fun vars_of_pbl_' pbl_ = 
wneuper@59265
   645
  let
wneuper@59265
   646
    fun var_of_pbl_ (_, (_, t)) = t: term
wneuper@59265
   647
  in ((map var_of_pbl_)(* o (filter_out is_copy_named)*)) pbl_ end
neuper@37906
   648
neuper@37906
   649
fun overwrite_ppc thy itm ppc =
neuper@37906
   650
  let 
wneuper@59265
   651
    fun repl _ (_, _, _, _, itm_) [] =
walther@59942
   652
        error ("overwrite_ppc: " ^ (I_Model.feedback_to_string (ThyC.to_ctxt thy) itm_) ^ " not found")
wneuper@59265
   653
      | repl ppc' itm (p :: ppc) =
wneuper@59308
   654
	      if (#1 itm) = (#1 p)
wneuper@59265
   655
	      then ppc' @ [itm] @ ppc
wneuper@59265
   656
	      else repl (ppc' @ [p]) itm ppc
wneuper@59265
   657
  in repl [] itm ppc end
neuper@37906
   658
wneuper@59265
   659
(* 10.3.00: insert the already compiled itm into model;
neuper@37906
   660
   ev. filter_out  untouched (in FE: (0,...)) item related to insert-item *)
neuper@37906
   661
fun insert_ppc thy itm ppc =
wneuper@59265
   662
  let 
walther@59943
   663
    fun eq_untouched d (0, _, _, _, itm_) = (d = I_Model.d_in itm_)
wneuper@59265
   664
      | eq_untouched _ _ = false
wneuper@59265
   665
    val ppc' = case seek_ppc (#1 itm) ppc of
wneuper@59265
   666
      SOME _ => overwrite_ppc thy itm ppc (*itm updated in is_notyet_input WN.11.03*)
wneuper@59265
   667
    | NONE => (ppc @ [itm])
walther@59943
   668
  in filter_out (eq_untouched ((I_Model.d_in o #5) itm)) ppc' end
neuper@37906
   669
walther@59943
   670
fun eq_dsc ((_, _, _, _, itm_), (_, _, _, _, iitm_)) = (I_Model.d_in itm_ = I_Model.d_in iitm_)
neuper@37906
   671
wneuper@59265
   672
(* insert_ppc = insert_ppc' for appl_add', input_icalhd 11.03,
wneuper@59265
   673
   handles superfluous items carelessly                       *)
wneuper@59265
   674
fun insert_ppc' itm itms = if member eq_dsc itms itm then itms else itms @ [itm] (* @ new itm *)
neuper@37906
   675
wneuper@59265
   676
(* output the headline to a ppc *)
wneuper@59265
   677
fun header p_ pI mI =
walther@59903
   678
  case p_ of Pbl => Generate.Problem (if pI = Problem.id_empty then [] else pI) 
wneuper@59271
   679
	   | Met => Generate.Method mI
wneuper@59265
   680
	   | pos => error ("header called with "^ pos_2str pos)
neuper@37906
   681
walther@59806
   682
fun specify_additem sel ct (pt, (p, Met)) = 
wneuper@59265
   683
    let
wneuper@59265
   684
      val (met, oris, dI', pI', mI', pbl, dI ,pI, mI, ctxt) = case get_obj I pt p of
wneuper@59265
   685
        (PblObj {meth = met, origin = (oris, (dI', pI', mI'),_), probl = pbl, spec = (dI, pI, mI), ctxt, ...})
wneuper@59265
   686
          => (met, oris, dI', pI', mI', pbl, dI ,pI, mI, ctxt)
wneuper@59265
   687
  	  | _ => error "specify_additem: uncovered case of get_obj I pt p"
walther@59881
   688
      val thy = if dI = ThyC.id_empty then ThyC.get_theory dI' else ThyC.get_theory dI
walther@59903
   689
      val cpI = if pI = Problem.id_empty then pI' else pI
walther@59903
   690
      val cmI = if mI = Method.id_empty then mI' else mI
wneuper@59269
   691
      val {ppc, pre, prls, ...} = Specify.get_met cmI
wneuper@59265
   692
    in 
wneuper@59265
   693
      case appl_add ctxt sel oris met ppc ct of
wneuper@59265
   694
        Add itm =>  (*..union old input *)
wneuper@59265
   695
  	      let
wneuper@59265
   696
            val met' = insert_ppc thy itm met
wneuper@59265
   697
            val arg = case sel of
wneuper@59571
   698
  			      "#Given" => Tactic.Add_Given'   (ct, met')
wneuper@59571
   699
  		      | "#Find"  => Tactic.Add_Find'    (ct, met')
wneuper@59571
   700
  		      | "#Relate"=> Tactic.Add_Relation'(ct, met')
wneuper@59265
   701
  		      | str => error ("specify_additem: uncovered case with " ^ str)
walther@59933
   702
  	        val (p, pt') = case Specify_Step.add arg (Istate_Def.Uistate, ctxt) (pt, (p, Met)) of
wneuper@59306
   703
  	          ((p, Met), _, _, pt') => (p, pt')
wneuper@59306
   704
  	        | _ => error "specify_additem: uncovered case of generate1"
wneuper@59308
   705
  	        val pre' = Stool.check_preconds thy prls pre met'
wneuper@59265
   706
  	        val pb = foldl and_ (true, map fst pre')
wneuper@59265
   707
  	        val (p_, nxt) =
wneuper@59265
   708
  	          nxt_spec Met pb oris (dI',pI',mI') (pbl,met') 
wneuper@59269
   709
  	            ((#ppc o Specify.get_pbt) cpI,ppc) (dI,pI,mI);
walther@59806
   710
  	      in 
walther@59806
   711
            ("ok", ([], [], (pt', (p, p_))))
wneuper@59265
   712
          end
wneuper@59265
   713
      | Err msg =>
wneuper@59265
   714
  	      let
wneuper@59308
   715
            val pre' = Stool.check_preconds thy prls pre met
wneuper@59265
   716
  	        val pb = foldl and_ (true, map fst pre')
wneuper@59265
   717
  	        val (p_, nxt) =
wneuper@59265
   718
  	          nxt_spec Met pb oris (dI',pI',mI') (pbl,met) 
wneuper@59269
   719
  	            ((#ppc o Specify.get_pbt) cpI,(#ppc o Specify.get_met) cmI) (dI,pI,mI);
walther@59806
   720
  	      in
walther@59806
   721
            (msg, ([], [], (pt, (p, p_))))
walther@59806
   722
  	      end
wneuper@59265
   723
    end
walther@59806
   724
  | specify_additem sel ct (pt, (p,_(*Frm, Pbl*))) =
neuper@41993
   725
      let
wneuper@59265
   726
        val (met, oris, dI', pI', mI', pbl, dI ,pI, mI, ctxt) = case get_obj I pt p of
wneuper@59265
   727
          (PblObj {meth = met, origin = (oris, (dI', pI', mI'),_), probl = pbl, spec = (dI, pI, mI), ctxt, ...})
wneuper@59265
   728
            => (met, oris, dI', pI', mI', pbl, dI ,pI, mI, ctxt)
wneuper@59265
   729
  	    | _ => error "specify_additem Frm, Pbl: uncovered case of get_obj I pt p"
walther@59881
   730
        val thy = if dI = ThyC.id_empty then ThyC.get_theory dI' else ThyC.get_theory dI
walther@59903
   731
        val cpI = if pI = Problem.id_empty then pI' else pI
walther@59903
   732
        val cmI = if mI = Method.id_empty then mI' else mI
wneuper@59269
   733
        val {ppc, where_, prls, ...} = Specify.get_pbt cpI
neuper@41993
   734
      in
neuper@41993
   735
        case appl_add ctxt sel oris pbl ppc ct of
wneuper@59265
   736
          Add itm => (*..union old input *)
neuper@41993
   737
	          let
neuper@41993
   738
	            val pbl' = insert_ppc thy itm pbl
wneuper@59265
   739
              val arg = case sel of
wneuper@59571
   740
  			        "#Given" => Tactic.Add_Given'   (ct, pbl')
wneuper@59571
   741
  		        | "#Find"  => Tactic.Add_Find'    (ct, pbl')
wneuper@59571
   742
  		        | "#Relate"=> Tactic.Add_Relation'(ct, pbl')
wneuper@59265
   743
  		        | str => error ("specify_additem Frm, Pbl: uncovered case with " ^ str)
walther@59810
   744
	            val (p, pt') =
walther@59933
   745
	              case Specify_Step.add arg (Istate_Def.Uistate, ctxt) (pt, (p, Pbl)) of
walther@59810
   746
  	              ((p, Pbl), _, _, pt') => (p, pt')
walther@59933
   747
  	            | _ => error "specify_additem: uncovered case of Specify_Step.add"
wneuper@59308
   748
	            val pre = Stool.check_preconds thy prls where_ pbl'
neuper@41993
   749
	            val pb = foldl and_ (true, map fst pre)
neuper@41993
   750
	            val (p_, nxt) =
wneuper@59269
   751
	              nxt_spec Pbl pb oris (dI',pI',mI') (pbl',met) (ppc, (#ppc o Specify.get_met) cmI) (dI, pI, mI)
wneuper@59265
   752
	            val ppc = if p_= Pbl then pbl' else met
wneuper@59265
   753
	          in
walther@59806
   754
              ("ok", ([], [], (pt', (p, p_))))
neuper@41993
   755
            end
neuper@41993
   756
        | Err msg =>
neuper@41993
   757
	          let
wneuper@59308
   758
              val pre = Stool.check_preconds thy prls where_ pbl
neuper@41993
   759
	            val pb = foldl and_ (true, map fst pre)
neuper@41993
   760
	            val (p_, nxt) =
wneuper@59265
   761
	              nxt_spec Pbl pb oris (dI', pI', mI') (pbl, met) 
wneuper@59269
   762
	                (ppc, (#ppc o Specify.get_met) cmI) (dI, pI, mI)
walther@59806
   763
	          in
walther@59806
   764
            (msg, ([], [], (pt, (p, p_))))
walther@59806
   765
	          end
wneuper@59265
   766
      end
neuper@37906
   767
neuper@41994
   768
(*FIXME.WN110515 declare_constraints for ct (without dsc) into PblObj{ctxt, ...}
neuper@41994
   769
  -- for input from scratch*)
neuper@38051
   770
fun nxt_specif_additem sel ct (ptp as (pt, (p, Pbl))) = 
wneuper@59265
   771
    let
wneuper@59265
   772
      val (oris, dI', pI', dI, pI, pbl, ctxt) = case get_obj I pt p of
wneuper@59265
   773
        PblObj {origin = (oris, (dI', pI', _), _), spec = (dI, pI, _), probl = pbl, ctxt, ...} =>
wneuper@59265
   774
           (oris, dI', pI', dI, pI, pbl, ctxt)
wneuper@59265
   775
      | _ => error "specify (Specify_Theory': uncovered case get_obj"
walther@59881
   776
      val thy = if dI = ThyC.id_empty then ThyC.get_theory dI' else ThyC.get_theory dI;
walther@59903
   777
      val cpI = if pI = Problem.id_empty then pI' else pI;
wneuper@59265
   778
    in
wneuper@59269
   779
      case appl_add ctxt sel oris pbl ((#ppc o Specify.get_pbt) cpI) ct of
wneuper@59265
   780
	      Add itm (*..union old input *) =>
wneuper@59265
   781
	        let
wneuper@59265
   782
	          val pbl' = insert_ppc thy itm pbl
wneuper@59265
   783
	          val (tac, tac_) = case sel of
wneuper@59571
   784
		          "#Given" => (Tactic.Add_Given    ct, Tactic.Add_Given'   (ct, pbl'))
wneuper@59571
   785
		        | "#Find"  => (Tactic.Add_Find     ct, Tactic.Add_Find'    (ct, pbl'))
wneuper@59571
   786
		        | "#Relate"=> (Tactic.Add_Relation ct, Tactic.Add_Relation'(ct, pbl'))
wneuper@59265
   787
		        | sel => error ("nxt_specif_additem: uncovered case of" ^ sel)
walther@59810
   788
		        val (p, c, pt') =
walther@59933
   789
		          case Specify_Step.add tac_ (Istate_Def.Uistate, ctxt) (pt, (p, Pbl)) of
walther@59810
   790
  		          ((p, Pbl), c, _, pt') =>  (p, c, pt')
walther@59810
   791
  		        | _ => error "nxt_specif_additem: uncovered case generate1"
wneuper@59265
   792
	        in
walther@59737
   793
	          ([(tac, tac_, ((p, Pbl), (Istate_Def.Uistate, ctxt)))], c, (pt', (p, Pbl)))
wneuper@59265
   794
          end	       
wneuper@59265
   795
	    | Err msg => (*TODO.WN03 pass error-msgs to the frontend..
wneuper@59265
   796
                     FIXME ..and dont abuse a tactic for that purpose*)
walther@59879
   797
	        ([(Tactic.Tac msg, Tactic.Tac_ (ThyC.get_theory "Isac_Knowledge", msg,msg,msg),
walther@59846
   798
	          (e_pos', (Istate_Def.empty, ContextC.empty)))], [], ptp) 
wneuper@59265
   799
    end
wneuper@59265
   800
  | nxt_specif_additem sel ct (ptp as (pt, (p, Met))) = 
wneuper@59265
   801
    let
wneuper@59265
   802
      val (oris, dI', mI', dI, mI, met, ctxt) = case get_obj I pt p of
wneuper@59265
   803
        PblObj {origin = (oris, (dI', _, mI'), _), spec = (dI, _, mI), meth = met,ctxt, ...} =>
wneuper@59265
   804
           (oris, dI', mI', dI, mI, met, ctxt)
wneuper@59265
   805
      | _ => error "nxt_specif_additem Met: uncovered case get_obj"
walther@59881
   806
      val thy = if dI = ThyC.id_empty then ThyC.get_theory dI' else ThyC.get_theory dI;
walther@59903
   807
      val cmI = if mI = Method.id_empty then mI' else mI;
wneuper@59265
   808
    in 
wneuper@59269
   809
      case appl_add ctxt sel oris met ((#ppc o Specify.get_met) cmI) ct of
wneuper@59265
   810
        Add itm (*..union old input *) =>
wneuper@59265
   811
	        let
wneuper@59265
   812
	          val met' = insert_ppc thy itm met;
wneuper@59265
   813
	          val (tac,tac_) = case sel of
wneuper@59571
   814
		          "#Given" => (Tactic.Add_Given    ct, Tactic.Add_Given'   (ct, met'))
wneuper@59571
   815
		        | "#Find"  => (Tactic.Add_Find     ct, Tactic.Add_Find'    (ct, met'))
wneuper@59571
   816
		        | "#Relate"=> (Tactic.Add_Relation ct, Tactic.Add_Relation'(ct, met'))
wneuper@59265
   817
		        | sel => error ("nxt_specif_additem Met: uncovered case of" ^ sel)
walther@59810
   818
	          val (p, c, pt') =
walther@59933
   819
	            case Specify_Step.add tac_ (Istate_Def.Uistate, ctxt) (pt, (p, Met)) of
walther@59810
   820
  	            ((p, Met), c, _, pt') => (p, c, pt')
walther@59810
   821
  		        | _ => error "nxt_specif_additem: uncovered case generate1 (WARNING WHY ?)"
wneuper@59265
   822
	        in
walther@59737
   823
	          ([(tac, tac_, ((p, Met), (Istate_Def.Uistate, ctxt)))], c, (pt', (p, Met)))
wneuper@59265
   824
	        end
wneuper@59265
   825
      | Err _ => ([(*tacis*)], [], ptp) (*nxt_me collects tacis until not hide; here just no progress*)
wneuper@59265
   826
    end
wneuper@59265
   827
  | nxt_specif_additem _ _ (_, p) = error ("nxt_specif_additem not impl. for" ^ pos'2str p)
neuper@41994
   828
wneuper@59405
   829
fun ori2Coritm pbt (i, v, f, d, ts) =
walther@59943
   830
  (i, v, true, f, I_Model.Cor ((d,ts),((snd o snd o the o (find_first (eq1 d))) pbt,ts)))
walther@59943
   831
    handle _ => (i, v, true, f, I_Model.Cor ((d, ts), (d, ts)))
wneuper@59265
   832
      (*dsc in oris, but not in pbl pat list: keep this dsc*)
neuper@37906
   833
wneuper@59265
   834
(* filter out oris which have same description in itms *)
neuper@37906
   835
fun filter_outs oris [] = oris
neuper@37906
   836
  | filter_outs oris (i::itms) = 
wneuper@59265
   837
    let
walther@59943
   838
      val ors = filter_out ((curry op = ((I_Model.d_in o #5) i)) o (#4)) oris
wneuper@59265
   839
    in
wneuper@59265
   840
      filter_outs ors itms
wneuper@59265
   841
    end
neuper@37906
   842
wneuper@59265
   843
(* filter oris which are in pbt, too *)
neuper@37906
   844
fun filter_pbt oris pbt =
wneuper@59265
   845
  let
wneuper@59265
   846
    val dscs = map (fst o snd) pbt
wneuper@59265
   847
  in
wneuper@59308
   848
    filter ((member op= dscs) o (#4)) oris
wneuper@59265
   849
  end
neuper@37906
   850
wneuper@59265
   851
(* combine itms from pbl + met and complete them wrt. pbt *)
wneuper@59265
   852
(* FIXXXME.WN031205 complete_metitms doesnt handle incorrect itms !*)
wneuper@59308
   853
fun complete_metitms oris pits mits met = 
wneuper@59265
   854
  let
walther@59943
   855
    val vat = I_Model.max_vt pits;
wneuper@59308
   856
    val itms = pits @ (filter ((member_swap op = vat) o (#2 )) mits)
wneuper@59308
   857
    val ors = filter ((member_swap op= vat) o (#2)) oris
wneuper@59265
   858
    val os = filter_outs ors itms (*WN.12.03?: does _NOT_ add itms from met ?!*)
wneuper@59265
   859
  in
wneuper@59265
   860
    itms @ (map (ori2Coritm met) os)
wneuper@59265
   861
  end
neuper@37906
   862
wneuper@59265
   863
(* complete model and guard of a calc-head *)
wneuper@59265
   864
fun complete_mod_ (oris, mpc, ppc, probl) =
wneuper@59265
   865
  let
wneuper@59308
   866
    val pits = filter_out ((curry op= false) o (#3)) probl
walther@59943
   867
    val vat = if probl = [] then 1 else I_Model.max_vt probl
wneuper@59308
   868
    val pors = filter ((member_swap op = vat) o (#2)) oris
wneuper@59265
   869
    val pors = filter_outs pors pits (*which are in pbl already*)
wneuper@59265
   870
    val pors = (filter_pbt pors ppc) (*which are in pbt, too*)
wneuper@59265
   871
    val pits = pits @ (map (ori2Coritm ppc) pors)
wneuper@59265
   872
    val mits = complete_metitms oris pits [] mpc
wneuper@59265
   873
  in (pits, mits) end
neuper@37906
   874
wneuper@59405
   875
fun some_spec (odI, opI, omI) (dI, pI, mI) =
walther@59879
   876
  (if dI = ThyC.id_empty then odI else dI,
walther@59903
   877
   if pI = Problem.id_empty then opI else pI,
walther@59903
   878
   if mI = Method.id_empty then omI else mI)
neuper@37906
   879
neuper@41982
   880
(* get the values from oris; handle the term list w.r.t. penv *)
walther@59939
   881
fun vals_of_oris (oris: O_Model.T) =
wneuper@59316
   882
  ((map (Model.mkval' o (#5))) o 
wneuper@59308
   883
    (filter ((member_swap op= 1) o (#2)))) oris
neuper@37906
   884
wneuper@59265
   885
(* fun tag_form thy (formal, given) = Thm.global_cterm_of thy
wneuper@59265
   886
	 (((head_of o Thm.term_of) given) $ (Thm.term_of formal)); WN100819 *)
neuper@37934
   887
fun tag_form thy (formal, given) =
neuper@52070
   888
 (let
neuper@52070
   889
    val gf = (head_of given) $ formal;
wneuper@59184
   890
    val _ = Thm.global_cterm_of thy gf
neuper@52070
   891
  in gf end)
walther@59870
   892
  handle _ => error ("calchead.tag_form: " ^ UnparseC.term_in_thy thy given ^
walther@59870
   893
    " .. " ^ UnparseC.term_in_thy thy formal ^ " ..types do not match")
neuper@38053
   894
wneuper@59265
   895
fun chktyps thy (fs, gs) = map (tag_form thy) (fs ~~ gs)
neuper@37906
   896
neuper@37906
   897
(* check pbltypes, announces one failure a time *)
neuper@37930
   898
fun chk_vars ctppc = 
wneuper@59265
   899
  let
wneuper@59389
   900
    val {Given = gi, Where = wh, Find = fi, Relate = re, ...} = Specify.appc flat (Specify.mappc TermC.vars ctppc)
wneuper@59265
   901
    val chked = subtract op = gi wh
wneuper@59265
   902
  in
wneuper@59265
   903
    if chked <> [] then ("wh\\gi", chked)
wneuper@59265
   904
    else
wneuper@59265
   905
      let val chked = subtract op = (union op = gi fi) re
wneuper@59265
   906
      in
wneuper@59265
   907
        if chked  <> []
wneuper@59265
   908
	      then ("re\\(gi union fi)", chked)
wneuper@59265
   909
	      else ("ok", []) 
wneuper@59265
   910
      end
wneuper@59265
   911
  end
neuper@37906
   912
neuper@37906
   913
(* check a new pbltype: variables (Free) unbound by given, find*) 
neuper@37906
   914
fun unbound_ppc ctppc =
wneuper@59265
   915
  let
wneuper@59389
   916
    val {Given = gi, Find = fi, Relate = re, ...} = Specify.appc flat (Specify.mappc TermC.vars ctppc)
wneuper@59265
   917
  in
wneuper@59265
   918
    distinct (subtract op = (union op = gi fi) re)
wneuper@59265
   919
  end
neuper@37906
   920
wneuper@59265
   921
(* complete _NON_empty calc-head for autocalc (sub-)pbl from oris
wneuper@59265
   922
  + met from fmz; assumes pos on PblObj, meth = []                    *)
wneuper@59265
   923
fun complete_mod (pt, pos as (p, p_) : pos') =
wneuper@59265
   924
  let
wneuper@59265
   925
    val _ = if p_ <> Pbl 
wneuper@59265
   926
	    then error ("###complete_mod: only impl.for Pbl, called with " ^ pos'2str pos)
wneuper@59265
   927
	    else ()
wneuper@59265
   928
	  val (oris, ospec, probl, spec) = case get_obj I pt p of
wneuper@59265
   929
	    PblObj {origin = (oris, ospec, _), probl, spec, ...} => (oris, ospec, probl, spec)
wneuper@59265
   930
	  | _ => error "complete_mod: unvered case get_obj"
wneuper@59265
   931
  	val (_, pI, mI) = some_spec ospec spec
wneuper@59269
   932
  	val mpc = (#ppc o Specify.get_met) mI
wneuper@59269
   933
  	val ppc = (#ppc o Specify.get_pbt) pI
wneuper@59265
   934
  	val (pits, mits) = complete_mod_ (oris, mpc, ppc, probl)
wneuper@59265
   935
    val pt = update_pblppc pt p pits
wneuper@59265
   936
	  val pt = update_metppc pt p mits
wneuper@59265
   937
  in (pt, (p, Met) : pos') end
neuper@37906
   938
walther@59820
   939
(* do all specification in one single step:
walther@59820
   940
   complete calc-head for autocalc (sub-)pbl from oris (+ met from fmz);
walther@59820
   941
   oris and spec (incl. pbl-refinement) given from init_calc or SubProblem
walther@59820
   942
*)
walther@59671
   943
fun all_modspec (pt, (p, _)) =
wneuper@59265
   944
  let
wneuper@59265
   945
    val (pors, dI, pI, mI) = case get_obj I pt p of
walther@59820
   946
      PblObj {origin = (pors, (dI, pI, mI), _), ...}
walther@59820
   947
        => (pors, dI, pI, mI)
walther@59820
   948
    | _ => raise ERROR "all_modspec: uncovered case get_obj"
wneuper@59269
   949
	  val {ppc, ...} = Specify.get_met mI
wneuper@59265
   950
    val (_, vals) = oris2fmz_vals pors
wneuper@59582
   951
	  val ctxt = ContextC.initialise dI vals
walther@59820
   952
    val (pt, _) = cupdate_problem pt p ((dI, pI, mI),
walther@59820
   953
      map (ori2Coritm ppc) pors, map (ori2Coritm ppc) pors, ctxt)
wneuper@59265
   954
  in
walther@59820
   955
    (pt, (p, Met))
wneuper@59265
   956
  end
neuper@37906
   957
wneuper@59265
   958
(* WN0312: use in nxt_spec, too ? what about variants ??? *)
wneuper@59308
   959
fun is_complete_mod_ [] = false
wneuper@59265
   960
  | is_complete_mod_ itms = foldl and_ (true, (map #3 itms))
neuper@41976
   961
wneuper@59265
   962
fun is_complete_mod (pt, pos as (p, Pbl) : pos') =
wneuper@59265
   963
    if (is_pblobj o (get_obj I pt)) p 
wneuper@59265
   964
    then (is_complete_mod_ o (get_obj g_pbl pt)) p
wneuper@59265
   965
    else error ("is_complete_mod: called by PrfObj at "^pos'2str pos)
neuper@37906
   966
  | is_complete_mod (pt, pos as (p, Met)) = 
wneuper@59265
   967
    if (is_pblobj o (get_obj I pt)) p 
wneuper@59265
   968
    then (is_complete_mod_ o (get_obj g_met pt)) p
wneuper@59265
   969
    else error ("is_complete_mod: called by PrfObj at "^pos'2str pos)
neuper@37906
   970
  | is_complete_mod (_, pos) =
wneuper@59265
   971
    error ("is_complete_mod called by " ^ pos'2str pos ^ " (should be Pbl or Met)")
neuper@37906
   972
wneuper@59265
   973
(* have (thy, pbl, met) _all_ been specified explicitly ? *)
wneuper@59265
   974
fun is_complete_spec (pt, pos as (p, _) : pos') = 
wneuper@59265
   975
  if (not o is_pblobj o (get_obj I pt)) p 
wneuper@59265
   976
  then error ("is_complete_spec: called by PrfObj at "^pos'2str pos)
wneuper@59265
   977
  else
wneuper@59265
   978
    let val (dI,pI,mI) = get_obj g_spec pt p
walther@59903
   979
    in dI <> ThyC.id_empty andalso pI <> Problem.id_empty andalso mI <> Method.id_empty end
neuper@37906
   980
wneuper@59265
   981
(* complete empty items in specification from origin (pbl, met ev.refined);
wneuper@59265
   982
   assumes 'is_complete_mod' *)
wneuper@59265
   983
fun complete_spec (pt, pos as (p, _) : pos') = 
wneuper@59265
   984
  let
wneuper@59265
   985
    val (ospec, spec) = case get_obj I pt p of
wneuper@59265
   986
      PblObj {origin = (_,ospec,_), spec,...} => (ospec, spec)
wneuper@59265
   987
    | _ => error "complete_spec: uncovered case get_obj"
wneuper@59265
   988
    val pt = update_spec pt p (some_spec ospec spec)
wneuper@59265
   989
  in
wneuper@59265
   990
    (pt, pos)
wneuper@59265
   991
  end
neuper@37906
   992
wneuper@59265
   993
fun is_complete_modspec ptp = is_complete_mod ptp andalso is_complete_spec ptp
neuper@37906
   994
wneuper@59265
   995
fun pt_model (PblObj {meth, spec, origin = (_, spec', hdl), ...}) Met =
wneuper@59265
   996
    let
wneuper@59265
   997
      val (_, _, metID) = get_somespec' spec spec'
walther@59903
   998
	    val pre = if metID = Method.id_empty then []
wneuper@59265
   999
	      else
wneuper@59265
  1000
	        let
wneuper@59269
  1001
	          val {prls, pre= where_, ...} = Specify.get_met metID
wneuper@59308
  1002
	          val pre = Stool.check_preconds' prls where_ meth 0
wneuper@59265
  1003
		      in pre end
wneuper@59265
  1004
	    val allcorrect = is_complete_mod_ meth andalso foldl and_ (true, (map #1 pre))
wneuper@59265
  1005
    in
wneuper@59265
  1006
      ModSpec (allcorrect, Met, hdl, meth, pre, spec)
wneuper@59265
  1007
    end
wneuper@59265
  1008
  | pt_model (PblObj {probl, spec, origin = (_, spec', hdl), ...}) _(*Frm,Pbl*) =
wneuper@59265
  1009
    let
wneuper@59265
  1010
      val (_, pI, _) = get_somespec' spec spec'
walther@59903
  1011
      val pre = if pI = Problem.id_empty then []
wneuper@59265
  1012
	      else
wneuper@59265
  1013
	        let
wneuper@59269
  1014
	          val {prls, where_, ...} = Specify.get_pbt pI
wneuper@59308
  1015
	          val pre = Stool.check_preconds' prls where_ probl 0
wneuper@59265
  1016
	        in pre end
wneuper@59265
  1017
	    val allcorrect = is_complete_mod_ probl andalso foldl and_ (true, (map #1 pre))
wneuper@59265
  1018
    in
wneuper@59265
  1019
      ModSpec (allcorrect, Pbl, hdl, probl, pre, spec)
wneuper@59265
  1020
    end
wneuper@59265
  1021
  | pt_model _ _ = error "pt_model: uncovered definition"
neuper@37906
  1022
wneuper@59265
  1023
fun pt_form (PrfObj {form, ...}) = Form form
wneuper@59265
  1024
  | pt_form (PblObj {probl, spec, origin = (_, spec', _), ...}) =
wneuper@59265
  1025
    let
wneuper@59265
  1026
      val (dI, pI, _) = get_somespec' spec spec'
wneuper@59269
  1027
      val {cas, ...} = Specify.get_pbt pI
wneuper@59265
  1028
    in case cas of
walther@59618
  1029
      NONE => Form (Auto_Prog.pblterm dI pI)
walther@59943
  1030
    | SOME t => Form (subst_atomic (I_Model.mk_env probl) t)
wneuper@59265
  1031
    end
neuper@37906
  1032
wneuper@59265
  1033
(* pt_extract returns
neuper@37906
  1034
      # the formula at pos
neuper@37906
  1035
      # the tactic applied to this formula
neuper@37906
  1036
      # the list of assumptions generated at this formula
neuper@37906
  1037
	(by application of another tac to the preceding formula !)
wneuper@59265
  1038
   pos is assumed to come from the frontend, ie. generated by moveDown.
wneuper@59265
  1039
   Notes: cannot be in ctree.sml, because ModSpec has to be calculated. 
wneuper@59265
  1040
   TODO 110417 get assumptions from ctxt !?
wneuper@59265
  1041
*)
wneuper@59265
  1042
fun pt_extract (pt, ([], Res)) =
wneuper@59265
  1043
    (* WN120512: returns Check_Postcondition WRONGLY see --- build fun is_exactly_equal *)
wneuper@59265
  1044
    let
wneuper@59265
  1045
      val (f, asm) = get_obj g_result pt []
wneuper@59265
  1046
    in (Form f, NONE, asm) end
neuper@37906
  1047
  | pt_extract (pt,(p,Res)) =
wneuper@59265
  1048
    let
wneuper@59265
  1049
      val (f, asm) = get_obj g_result pt p
wneuper@59265
  1050
      val tac =
wneuper@59265
  1051
        if last_onlev pt p
wneuper@59265
  1052
        then
wneuper@59265
  1053
          if is_pblobj' pt (lev_up p)
neuper@42437
  1054
          then
wneuper@59265
  1055
            let
wneuper@59265
  1056
              val pI = case get_obj I pt (lev_up p) of
wneuper@59265
  1057
                PblObj{spec = (_, pI, _), ...} => pI
wneuper@59265
  1058
              | _ => error "pt_extract last_onlev: uncovered case get_obj"
walther@59903
  1059
            in if pI = Problem.id_empty then NONE else SOME (Tactic.Check_Postcond pI) end
wneuper@59571
  1060
		      else SOME Tactic.End_Trans (* WN0502 TODO for other branches *)
wneuper@59265
  1061
		    else
wneuper@59265
  1062
		      let val p' = lev_on p
wneuper@59265
  1063
		      in
wneuper@59265
  1064
		        if is_pblobj' pt p'
wneuper@59265
  1065
		        then
wneuper@59265
  1066
		          let
wneuper@59265
  1067
		            val (dI ,pI) = case get_obj I pt p' of
wneuper@59265
  1068
		              PblObj{origin = (_, (dI, pI, _), _), ...} => (dI ,pI)
wneuper@59265
  1069
		            | _ => error "pt_extract \<not>last_onlev: uncovered case get_obj"
wneuper@59571
  1070
		          in SOME (Tactic.Subproblem (dI, pI)) end
wneuper@59265
  1071
		        else
wneuper@59265
  1072
		          if f = get_obj g_form pt p'
wneuper@59265
  1073
		          then SOME (get_obj g_tac pt p') (*because this Frm ~~~is not on worksheet*)
walther@59868
  1074
		          else SOME (Tactic.Take (UnparseC.term (get_obj g_form pt p')))
wneuper@59265
  1075
		      end
wneuper@59265
  1076
    in (Form f, tac, asm) end
wneuper@59265
  1077
  | pt_extract (pt, (p,p_(*Frm,Pbl*))) =
neuper@42437
  1078
      let
neuper@42437
  1079
        val ppobj = get_obj I pt p
neuper@42437
  1080
        val f = if is_pblobj ppobj then pt_model ppobj p_ else get_obj pt_form pt p
neuper@42437
  1081
        val tac = g_tac ppobj
wneuper@59265
  1082
      in (f, SOME tac, []) end
neuper@37906
  1083
wneuper@59265
  1084
(** get the formula from a ctree-node:
wneuper@59265
  1085
  take form+res from PblObj and 1.PrfObj and (PrfObj after PblObj)
wneuper@59265
  1086
  take res from all other PrfObj's
wneuper@59265
  1087
  Designed for interSteps, outcommented 04 in favour of calcChangedEvent **)
wneuper@59265
  1088
fun formres p (Nd (PblObj {origin = (_, _, h), result = (r, _), ...}, _)) =
wneuper@59265
  1089
    [("headline", (p, Frm), h), ("stepform", (p, Res), r)]
wneuper@59265
  1090
  | formres p (Nd (PrfObj {form, result = (r, _), ...}, _)) = 
wneuper@59265
  1091
    [("stepform", (p, Frm), form), ("stepform", (p, Res), r)]
wneuper@59265
  1092
  | formres _ _ = error "formres: uncovered definition" 
wneuper@59265
  1093
fun form p (Nd (PrfObj {result = (r, _), ...}, _)) = 
wneuper@59265
  1094
    [("stepform", (p, Res), r)]
wneuper@59265
  1095
  | form _ _ = error "form: uncovered definition" 
neuper@37906
  1096
wneuper@59265
  1097
(* assumes to take whole level, in particular hd -- for use in interSteps *)
wneuper@59265
  1098
fun get_formress fs _ [] = flat fs
neuper@37906
  1099
  | get_formress fs p (nd::nds) =
neuper@37906
  1100
    (* start with   'form+res'       and continue with trying 'res' only*)
neuper@37906
  1101
    get_forms (fs @ [formres p nd]) (lev_on p) nds
wneuper@59265
  1102
and get_forms fs _ [] = flat fs
neuper@37906
  1103
  | get_forms fs p (nd::nds) =
neuper@37906
  1104
    if is_pblnd nd
neuper@37906
  1105
    (* start again with      'form+res' ///ugly repeat with Check_elementwise
neuper@37906
  1106
    then get_formress (fs @ [formres p nd]) (lev_on p) nds                   *)
neuper@37906
  1107
    then get_forms    (fs @ [formres p nd]) (lev_on p) nds
neuper@37906
  1108
    (* continue with trying 'res' only*)
neuper@37906
  1109
    else get_forms    (fs @ [form    p nd]) (lev_on p) nds;
neuper@37906
  1110
wneuper@59279
  1111
(** get an 'interval' 'from' 'to' of formulae from a ctree **)
wneuper@59265
  1112
(* WN0401 this functionality belongs to ctree.sml *)
wneuper@59265
  1113
fun eq_pos' (p1, Frm) (p2, Frm) = p1 = p2
wneuper@59265
  1114
  | eq_pos' (p1, Res) (p2, Res) = p1 = p2
wneuper@59265
  1115
  | eq_pos' (p1, Pbl) (p2, p2_) =
wneuper@59265
  1116
    p1 = p2 andalso (case p2_ of Pbl => true | Met => true | _ => false)
wneuper@59265
  1117
  | eq_pos' (p1, Met) (p2, p2_) =
wneuper@59265
  1118
    p1 = p2 andalso (case p2_ of Pbl => true | Met => true | _ => false)
neuper@37906
  1119
  | eq_pos' _ _ = false;
neuper@37906
  1120
wneuper@59265
  1121
(* get an 'interval' from the ctree; 'interval' is w.r.t. the 
neuper@37906
  1122
   total ordering Position#compareTo(Position p) in the java-code
wneuper@59516
  1123
val get_interval = fn :
wneuper@59516
  1124
      pos' ->     : from is "move_up 1st-element" to return
neuper@37906
  1125
      pos' -> 	  : to the last element to be returned; from < to
wneuper@59516
  1126
      int -> 	    : level: 0 gets the flattest sub-tree possible, 999 the deepest
wneuper@59279
  1127
      ctree -> 	  : 
neuper@37906
  1128
      (pos' * 	  : of the formula
neuper@37906
  1129
       Term.term) : the formula
wneuper@59265
  1130
	  list                                                           *)
neuper@37906
  1131
fun get_interval from to level pt =
neuper@42432
  1132
  let
wneuper@59265
  1133
    fun get_inter c (from : pos') (to : pos') lev pt =
wneuper@59265
  1134
	    if eq_pos' from to orelse from = ([], Res)
neuper@42432
  1135
	    then
wneuper@59516
  1136
	      let val (f, tacopt, _) = pt_extract (pt, from)
wneuper@59265
  1137
	      in case f of
wneuper@59516
  1138
	        ModSpec (_, _, headline, _, _, _) => c @ [(from, headline, NONE)] 
wneuper@59516
  1139
	      | Form t => c @ [(from, t, tacopt)]
neuper@42432
  1140
	      end
neuper@37906
  1141
	    else 
neuper@42432
  1142
	      if lev < lev_of from
neuper@42432
  1143
	      then (get_inter c (move_dn [] pt from) to lev pt)
neuper@42432
  1144
		      handle (PTREE _(*from move_dn too far*)) => c
neuper@42432
  1145
		    else
neuper@42432
  1146
		      let
wneuper@59516
  1147
		        val (f, tacopt, _) = pt_extract (pt, from)
neuper@42432
  1148
		        val term = case f of
neuper@42432
  1149
		          ModSpec (_,_,headline,_,_,_) => headline
neuper@42432
  1150
				    | Form t => t
wneuper@59516
  1151
		      in (get_inter (c @ [(from, term, tacopt)]) (move_dn [] pt from) to lev pt)
wneuper@59516
  1152
		        handle (PTREE _(*from move_dn too far*)) => c @ [(from, term, tacopt)]
neuper@42432
  1153
		      end
wneuper@59265
  1154
  in get_inter [] from to level pt end
neuper@37906
  1155
walther@59868
  1156
fun posterm2str (pos, t, _) = "(" ^ pos'2str pos ^ ", " ^ UnparseC.term t ^ ")"
wneuper@59265
  1157
fun posterms2str pfs = (strs2str' o (map (curry op ^ "\n")) o (map posterm2str)) pfs
neuper@37906
  1158
wneuper@59516
  1159
fun postermtac2str (pos, t, SOME tac) =
walther@59868
  1160
    pos'2str pos ^ ", " ^ UnparseC.term t ^ "\n" ^ indent 10 ^ Tactic.input_to_string tac
wneuper@59516
  1161
  | postermtac2str (pos, t, NONE) =
walther@59868
  1162
    pos'2str pos ^ ", " ^ UnparseC.term t
wneuper@59516
  1163
fun postermtacs2str pfts = (strs2str' o (map (curry op ^ "\n")) o (map postermtac2str)) pfts
wneuper@59516
  1164
wneuper@59265
  1165
(* WN050225 omits the last step, if pt is incomplete *)
wneuper@59265
  1166
fun show_pt pt = tracing (posterms2str (get_interval ([], Frm) ([], Res) 99999 pt))
wneuper@59516
  1167
fun show_pt_tac pt = tracing (postermtacs2str (get_interval ([], Frm) ([], Res) 99999 pt))
neuper@37906
  1168
wneuper@59265
  1169
(* get a calchead from a PblObj-node in the ctree; preconditions must be calculated *)
wneuper@59265
  1170
fun get_ocalhd (pt, (p, Pbl) : pos') = 
wneuper@59265
  1171
    let
wneuper@59265
  1172
	    val (ospec, hdf', spec, probl) = case get_obj I pt p of
wneuper@59265
  1173
	      PblObj {origin = (_, ospec, hdf'), spec, probl,...} => (ospec, hdf', spec, probl)
wneuper@59265
  1174
	    | _ => error "get_ocalhd Pbl: uncovered case get_obj"
wneuper@59269
  1175
      val {prls, where_, ...} = Specify.get_pbt (#2 (some_spec ospec spec))
walther@59881
  1176
      val pre = Stool.check_preconds (ThyC.get_theory "Isac_Knowledge") prls where_ probl
wneuper@59265
  1177
    in
wneuper@59265
  1178
      (ocalhd_complete probl pre spec, Pbl, hdf', probl, pre, spec) : ocalhd
wneuper@59265
  1179
    end
wneuper@59265
  1180
  | get_ocalhd (pt, (p, Met)) = 
wneuper@59265
  1181
    let
wneuper@59265
  1182
		  val (ospec, hdf', spec, meth) = case get_obj I pt p of
wneuper@59265
  1183
		    PblObj {origin = (_, ospec, hdf'), spec, meth, ...} => (ospec, hdf', spec, meth)
wneuper@59265
  1184
		  | _ => error "get_ocalhd Met: uncovered case get_obj"
wneuper@59269
  1185
      val {prls, pre, ...} = Specify.get_met (#3 (some_spec ospec spec))
walther@59881
  1186
      val pre = Stool.check_preconds (ThyC.get_theory "Isac_Knowledge") prls pre meth
wneuper@59265
  1187
    in
wneuper@59265
  1188
      (ocalhd_complete meth pre spec, Met, hdf', meth, pre, spec)
wneuper@59265
  1189
    end
wneuper@59265
  1190
  | get_ocalhd _ = error "get_ocalhd: uncovered definition"
neuper@37906
  1191
wneuper@59265
  1192
(* at the activeFormula set the Model, the Guard and the Specification 
wneuper@59265
  1193
   to empty and return a CalcHead;
wneuper@59265
  1194
   the 'origin' remains (for reconstructing all that) *)
wneuper@59265
  1195
fun reset_calchead (pt, (p,_) : pos') = 
wneuper@59265
  1196
  let
wneuper@59265
  1197
    val () = case get_obj I pt p of
wneuper@59265
  1198
      PblObj _ => ()
wneuper@59265
  1199
    | _ => error "reset_calchead: uncovered case get_obj"
wneuper@59265
  1200
    val pt = update_pbl pt p []
wneuper@59265
  1201
    val pt = update_met pt p []
walther@59902
  1202
    val pt = update_spec pt p Spec.empty
wneuper@59265
  1203
  in (pt, (p, Pbl) : pos') end
neuper@37906
  1204
walther@59763
  1205
(**)end(**)