src/Tools/isac/Specify/specification.sml
author Walther Neuper <walther.neuper@jku.at>
Sat, 16 May 2020 16:23:24 +0200
changeset 59988 9a6aa40d1962
parent 59987 73225ca9e0aa
child 59989 31f54ab9b2ce
permissions -rw-r--r--
shift code from Specification to I_Model, rename ids
walther@59978
     1
(* Title:  Specify/specification.sml
walther@59978
     2
   Author: Walther Neuper, Mathias Lehnfeld
neuper@37906
     3
   (c) due to copyright terms
wneuper@59265
     4
*)
wneuper@59540
     5
(* Survey on type fmz_ .. type ori .. type itm (in probl, meth) .. formal args of root-/SubProblem
wneuper@59540
     6
   and relations between respective list elements:                                       #1#
wneuper@59540
     7
                  fmz      =             [ dsc $ v......................................(dsc $ v)..]
wneuper@59540
     8
root problem on pos = ([], _)            
wneuper@59540
     9
                  fmz_     =             [(dsc, v).......................................(dsc, v)..]
wneuper@59540
    10
                  \<down>                                                                       
wneuper@59540
    11
                  oris     =             [(dsc, v)..(dsc, v),(dsc, v),(dsc, v)...........(dsc, v)..]
wneuper@59540
    12
                  \<down>                       #Given,..,#Relate  #Find     #undef............#undef   \<down>
wneuper@59540
    13
                  \<down>                                                     \<down>                 \<down>       \<down>
wneuper@59540
    14
  Specify_Problem + pbl.ppc=             [(dsc,id)..(dsc,id),(dsc,id)]  \<down>                 \<down>       \<down>
wneuper@59540
    15
                  \<down>                                                     \<down>                 \<down>       \<down>
wneuper@59540
    16
                  itms     =             [(dsc, v)..(dsc, v),(dsc, v)]  \<down>                 \<down>       \<down>
wneuper@59540
    17
  int.modelling                           +Cor ++++++++++Cor +Cor       \<down>                 \<down>       \<down>
wneuper@59540
    18
                  \<down>                                                     \<down>                 \<down>       \<down>
wneuper@59540
    19
  Specify_Method  + met.ppc=             [(dsc,id)..(dsc,id),(dsc,id)]  \<down>                 \<down>       \<down>
wneuper@59540
    20
                  \<down>                                                     \<down>                 \<down>       \<down>
wneuper@59540
    21
                  itms     =             [(dsc, v)..(dsc, v),(dsc, v),(dsc, v)..(dsc, v)] \<down>       \<down>
wneuper@59540
    22
  int.modelling                                                       +Cor ++++++Cor      \<down>       \<down>
wneuper@59540
    23
                  form.args=             [ id ..... id      , ///////  id ....... id    ] \<down>       \<down>
wneuper@59540
    24
                  env =                  [(id, v)..(id, v)  , /////// (id, v)....(id, v)] \<down>       \<down>
walther@59718
    25
  interpret code env = [(id, v)..(id, v),(id, v)..(id, v)  , /////// (id, v)....(id, v)] \<down>       \<down>
wneuper@59540
    26
    ..extends env       ^^^^^^^^^^^^^^^^   \<down>                           \<down>   \<down>              \<down>       \<down>
wneuper@59540
    27
                                   \<down>       \<down>                           \<down>   \<down>              \<down>       \<down>
wneuper@59540
    28
SubProblem on pos = ([2], _)       \<down>       \<down>                           \<down>   \<down>              \<down>       \<down>
wneuper@59540
    29
                  form.args=      [id ................................ id ]\<down>              \<down>       \<down>
wneuper@59540
    30
                  \<down>                REAL..BOOL..                            \<down>              \<down>       \<down>
wneuper@59540
    31
                  \<down>                                                        \<down>              \<down>       \<down>
wneuper@59540
    32
                  + met.ppc=      [(dsc,id).......................(dsc,id)]\<down>              \<down>       \<down>
wneuper@59540
    33
                                    \<down>                               \<down>      \<down>              \<down>       \<down>
wneuper@59540
    34
                  oris     =      [(dsc, v)...................... (dsc,   v) ........... (dsc, v)]\<down>
walther@59718
    35
  Specify_Problem, int.modelling, Specify_Method, interpret code as above                 #1#    \<down>
wneuper@59540
    36
                                                                                                  \<down>
wneuper@59540
    37
SubProblem on pos = ([3, 4], _)                                                                   \<down>
wneuper@59540
    38
                  form.args=              [id ...................... id ]                         \<down>
wneuper@59540
    39
                  \<down>                        REAL..BOOL..                                           \<down>
wneuper@59540
    40
                  + met.ppc=              [(dsc,id).............(dsc,id)]                         \<down>
wneuper@59540
    41
                  oris     =              [(dsc, v).............(dsc, v)...................(dsc, v)] 
walther@59718
    42
  Specify_Problem, int.modelling, Specify_Method, interpret code as above                    #1#
wneuper@59540
    43
wneuper@59540
    44
Notes:
wneuper@59540
    45
(1) SubProblem implements sub-function calls such, that the arguments (+ pre- + post-condition)
wneuper@59540
    46
    of the functions become concern of interactive modelling.
wneuper@59540
    47
(2) In Isac-terms, the above concerns the phases of modelling and 
wneuper@59540
    48
    and of specifying (Specify_Problem, Specify_Method),
wneuper@59540
    49
    while stepwise construction of solutions is called solving.
wneuper@59540
    50
    The latter is supported by Lucas-Interpretation of the functions' body.
wneuper@59540
    51
(3) ori list is determined by fmz_ (in root-pbl) or by args of SubProblem;
wneuper@59540
    52
    it is as complete as possible (this has been different up to now).
wneuper@59540
    53
(4) itm list is initialised by pbl-ppc | met-pps and completed (Cor) by stepwise user input.
wneuper@59550
    54
(5) model-pattern of pbl can omit technical items, e.g. boundVariable or functionName,
wneuper@59550
    55
    add them to the model-pattern of met and let this input be done automatically;
wneuper@59550
    56
    respective items must be in fmz.
wneuper@59540
    57
*)
walther@59977
    58
signature SPECIFICATION =
wneuper@59265
    59
sig
walther@59735
    60
walther@59977
    61
  type T = Specification_Def.T
neuper@37906
    62
walther@59984
    63
(*val reset: Calc.T -> Calc.T*)
walther@59984
    64
  val reset_calchead: Calc.T -> Calc.T
walther@59984
    65
(*val get: Calc.T -> T*)
walther@59984
    66
  val get_ocalhd: Calc.T -> T
walther@59984
    67
(*val complete: I_Model.T -> Pre_Conds.T -> ThyC.id * Problem.id * Method.id -> bool*)
walther@59984
    68
  val ocalhd_complete: I_Model.T -> Pre_Conds.T -> ThyC.id * Problem.id * Method.id -> bool
walther@59984
    69
(*val is_complete: Calc.T -> bool*)
walther@59984
    70
  val is_complete_mod: Calc.T -> bool
neuper@37906
    71
walther@59986
    72
(*/------- to Specify from Specification -------\* )
walther@59984
    73
(*val find_next_step': Pos.pos_ -> bool -> O_Model.T -> References.T -> I_Model.T * I_Model.T ->
walther@59984
    74
    Model_Pattern.T * Model_Pattern.T -> References.T -> Pos.pos_ * Tactic.input*)
walther@59984
    75
  val nxt_spec: Pos.pos_ -> bool -> O_Model.T -> References.T -> I_Model.T * I_Model.T ->
walther@59984
    76
    Model_Pattern.T * Model_Pattern.T -> References.T -> Pos.pos_ * Tactic.input
walther@59984
    77
(*val do_all : Calc.T -> Calc.T*)
walther@59984
    78
  val all_modspec: Calc.T -> Calc.T 
walther@59984
    79
(*val finish_phase: Calc.T -> Calc.T*)
walther@59984
    80
  val complete_mod: Calc.T -> Calc.T
walther@59984
    81
(*val nxt_add: theory -> O_Model.T -> Model_Pattern.T -> I_Model.T -> (string * string) option*)
walther@59984
    82
  val nxt_add: theory -> O_Model.T -> Model_Pattern.T -> I_Model.T -> (string * string) option
walther@59984
    83
(*val nxt_specif_additem: string -> TermC.as_string -> Calc.T -> Calc.state_post*)
walther@59984
    84
  val nxt_specif_additem: string -> TermC.as_string -> Calc.T -> Calc.state_post
walther@59984
    85
(*val specify_additem: string -> TermC.as_string -> Calc.T -> string * Calc.state_post*)
walther@59984
    86
  val specify_additem: string -> TermC.as_string -> Calc.T -> string * Calc.state_post
walther@59986
    87
( *\------- to Specify from Specification -------/*)
walther@59956
    88
walther@59984
    89
(*/------- to P_Model from Specification -------\*)
walther@59984
    90
  val ppc2list: 'a P_Model.ppc -> 'a list
walther@59943
    91
  val mk_delete: theory -> string -> I_Model.feedback -> Tactic.input
walther@59865
    92
  val mk_additem: string -> TermC.as_string -> Tactic.input
walther@59984
    93
(*\------- to P_Model from Specification -------/*)
walther@59983
    94
wneuper@59310
    95
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
walther@59981
    96
  (*NONE*)
walther@59886
    97
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
walther@59985
    98
  val get: Calc.T -> I_Model.T * O_Model.T * References.T * I_Model.T * References.T * Proof.context
walther@59957
    99
  val make: I_Model.m_field -> TermC.as_string * I_Model.T -> Tactic.T
walther@59886
   100
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
wneuper@59310
   101
wneuper@59307
   102
(*----- kept as hints to initial design ideas; NOT in src/, NOT in test ------------------------*)
walther@59984
   103
  val variants_in: term list -> int
walther@59984
   104
  val is_untouched: I_Model.single -> bool
walther@59984
   105
  val is_list_type: typ -> bool
walther@59984
   106
  val parse_ok: I_Model.feedback list -> bool
walther@59984
   107
  val all_dsc_in: I_Model.feedback list -> term list
walther@59984
   108
  val chktyps: theory -> term list * term list -> term list (* only in old tests*)
walther@59984
   109
  val is_complete_modspec: Calc.T -> bool
walther@59984
   110
  val get_formress: (string * Pos.pos' * term) list list -> Pos.pos -> Ctree.ctree list ->
walther@59846
   111
    (string * Pos.pos' * term) list
walther@59984
   112
  val get_forms: (string * Pos.pos' * term) list list -> Pos.pos -> Ctree.ctree list ->
walther@59846
   113
    (string * Pos.pos' * term) list
wneuper@59265
   114
end
neuper@37906
   115
walther@59763
   116
(**)
walther@59986
   117
structure Specification(** ): SPECIFICATION( **) =
neuper@37906
   118
struct
walther@59763
   119
(**)
walther@59957
   120
walther@59977
   121
type T = Specification_Def.T;
walther@59977
   122
wneuper@59265
   123
(* is the calchead complete ? *)
wneuper@59265
   124
fun ocalhd_complete its pre (dI, pI, mI) = 
walther@59939
   125
  foldl and_ (true, map #3 (its: I_Model.T)) andalso 
walther@59963
   126
  foldl and_ (true, map #1 (pre: Pre_Conds.T)) andalso 
walther@59903
   127
  dI <> ThyC.id_empty andalso pI <> Problem.id_empty andalso mI <> Method.id_empty
neuper@37906
   128
wneuper@59308
   129
fun ppc2list {Given = gis, Where = whs, Find = fis, With = wis, Relate = res} =
wneuper@59265
   130
  gis @ whs @ fis @ wis @ res
neuper@37906
   131
neuper@37906
   132
(* get the number of variants in a problem in 'original',
neuper@37906
   133
   assumes equal descriptions in immediate sequence    *)
neuper@37906
   134
fun variants_in ts =
wneuper@59265
   135
  let
wneuper@59265
   136
    fun eq (x, y) = head_of x = head_of y
wneuper@59265
   137
    fun cnt _ [] _ n = ([n], [])
wneuper@59265
   138
      | cnt eq (x :: xs) y n = if eq (x, y) then cnt eq xs y (n + 1) else ([n], x :: xs)
wneuper@59265
   139
    fun coll _  xs [] = xs
wneuper@59265
   140
      | coll eq  xs (y :: ys) = 
wneuper@59265
   141
        let val (n, ys') = cnt eq (y :: ys) y 0
wneuper@59265
   142
        in if ys' = [] then xs @ n else coll eq  (xs @ n) ys' end
wneuper@59265
   143
    val vts = subtract op = [1] (distinct (coll eq [] ts))
wneuper@59265
   144
  in
wneuper@59265
   145
    case vts of 
wneuper@59265
   146
      [] => 1
wneuper@59265
   147
    | [n] => n
walther@59962
   148
    | _ => raise ERROR "different variants in formalization"
wneuper@59265
   149
  end
neuper@37906
   150
wneuper@59265
   151
fun is_list_type (Type ("List.list", _)) = true
wneuper@59265
   152
  | is_list_type _ = false
walther@59943
   153
fun is_parsed (I_Model.Syn _) = false
wneuper@59265
   154
  | is_parsed _ = true
wneuper@59265
   155
fun parse_ok its = foldl and_ (true, map is_parsed its)
neuper@37906
   156
neuper@37906
   157
fun all_dsc_in itm_s =
neuper@37906
   158
  let    
walther@59943
   159
    fun d_in (I_Model.Cor ((d, _), _)) = [d]
walther@59943
   160
      | d_in (I_Model.Syn _) = []
walther@59943
   161
      | d_in (I_Model.Typ _) = []
walther@59943
   162
      | d_in (I_Model.Inc ((d,_),_)) = [d]
walther@59943
   163
      | d_in (I_Model.Sup (d,_)) = [d]
walther@59943
   164
      | d_in (I_Model.Mis (d,_)) = [d]
walther@59946
   165
      | d_in i = raise ERROR ("all_dsc_in: uncovered case with " ^ I_Model.feedback_to_string' i)
wneuper@59265
   166
  in (flat o (map d_in)) itm_s end; 
neuper@37906
   167
wneuper@59265
   168
(* get the first term in ts from ori *)
wneuper@59308
   169
fun getr_ct thy (_, _, fd, d, ts) =
walther@59953
   170
  (fd, ((UnparseC.term_in_thy thy) o Input_Descript.join) (d,[hd ts]))
neuper@37906
   171
neuper@38051
   172
(* get a term from ori, notyet input in itm.
neuper@38051
   173
   the term from ori is thrown back to a string in order to reuse
neuper@38051
   174
   machinery for immediate input by the user. *)
wneuper@59308
   175
fun geti_ct thy (_, _, _, d, ts) (_, _, _, fd, itm_) =
walther@59953
   176
  (fd, ((UnparseC.term_in_thy thy) o Input_Descript.join) (d, subtract op = (I_Model.ts_in itm_) ts))
neuper@37906
   177
neuper@37906
   178
(* in FE dsc, not dat: this is in itms ...*)
walther@59943
   179
fun is_untouched (_, _, false, _, I_Model.Inc ((_, []), _)) = true
wneuper@59265
   180
  | is_untouched _ = false
neuper@37906
   181
neuper@37906
   182
(* select an item in oris, notyet input in itms 
walther@59943
   183
   (precondition: in itms are only I_Model.Cor, I_Model.Sup, I_Model.Inc) *)
wneuper@59235
   184
(*args of nxt_add
wneuper@59235
   185
  thy : for?
wneuper@59235
   186
  oris: from formalization 'type fmz', structured for efficient access 
wneuper@59235
   187
  pbt : the problem-pattern to be matched with oris in order to get itms
wneuper@59235
   188
  itms: already input items
wneuper@59235
   189
*)
wneuper@59308
   190
fun nxt_add thy [] pbt itms = (*root (only) ori...fmz=[]*)
wneuper@59265
   191
    let
walther@59943
   192
      fun test_d d (i, _, _, _, itm_) = (d = (I_Model.d_in itm_)) andalso i <> 0
wneuper@59265
   193
      fun is_elem itms (_, (d, _)) = 
wneuper@59265
   194
        case find_first (test_d d) itms of SOME _ => true | NONE => false
wneuper@59265
   195
    in
wneuper@59265
   196
      case filter_out (is_elem itms) pbt of
walther@59953
   197
        (f, (d, _)) :: _ => SOME (f, ((UnparseC.term_in_thy thy) o Input_Descript.join) (d, []))
wneuper@59265
   198
      | _ => NONE
wneuper@59265
   199
    end
wneuper@59265
   200
  | nxt_add thy oris _ itms =
wneuper@59265
   201
    let
walther@59939
   202
      fun testr_vt v ori = member op= (#2 (ori : O_Model.single)) v andalso (#3 ori) <> "#undef"
walther@59939
   203
      fun testi_vt v itm = member op= (#2 (itm : I_Model.single)) v
walther@59939
   204
      fun test_id ids r = member op= ids (#1 (r : O_Model.single))
wneuper@59426
   205
      fun test_subset itm (_, _, _, d, ts) = 
walther@59943
   206
        (I_Model.d_in (#5 (itm: I_Model.single))) = d andalso subset op = (I_Model.ts_in (#5 itm), ts)
walther@59943
   207
      fun false_and_not_Sup (_, _, false, _, I_Model.Sup _) = false
wneuper@59265
   208
        | false_and_not_Sup (_, _, false, _, _) = true
wneuper@59265
   209
        | false_and_not_Sup _ = false
walther@59943
   210
      val v = if itms = [] then 1 else I_Model.max_vt itms
wneuper@59265
   211
      val vors = if v = 0 then oris else filter (testr_vt v) oris  (* oris..vat *)
wneuper@59265
   212
      val vits =
wneuper@59265
   213
        if v = 0
wneuper@59265
   214
        then itms                                 (* because of dsc without dat *)
wneuper@59265
   215
  	    else filter (testi_vt v) itms;                             (* itms..vat *)
wneuper@59265
   216
      val icl = filter false_and_not_Sup vits;                    (* incomplete *)
wneuper@59265
   217
    in
wneuper@59265
   218
      if icl = [] 
wneuper@59265
   219
      then case filter_out (test_id (map #1 vits)) vors of
wneuper@59265
   220
  	    [] => NONE
wneuper@59265
   221
  	  | miss => SOME (getr_ct thy (hd miss))
wneuper@59265
   222
      else
wneuper@59265
   223
        case find_first (test_subset (hd icl)) vors of
walther@59962
   224
          NONE => raise ERROR "nxt_add: types or dsc DO NOT MATCH BETWEEN fmz --- pbt"
wneuper@59265
   225
        | SOME ori => SOME (geti_ct thy ori (hd icl))
wneuper@59265
   226
    end
neuper@37906
   227
walther@59988
   228
fun mk_delete thy "#Given" itm_ = Tactic.Del_Given (I_Model.to_p_model thy itm_)
walther@59988
   229
  | mk_delete thy "#Find" itm_ = Tactic.Del_Find (I_Model.to_p_model thy itm_)
walther@59988
   230
  | mk_delete thy "#Relate" itm_ = Tactic.Del_Relation (I_Model.to_p_model thy itm_)
walther@59962
   231
  | mk_delete _ str _ = raise ERROR ("mk_delete: called with field \"" ^ str ^ "\"")
wneuper@59571
   232
fun mk_additem "#Given" ct = Tactic.Add_Given ct
wneuper@59571
   233
  | mk_additem "#Find" ct = Tactic.Add_Find ct    
wneuper@59571
   234
  | mk_additem "#Relate"ct = Tactic.Add_Relation ct
walther@59962
   235
  | mk_additem str _ = raise ERROR ("mk_additem: called with field \"" ^ str ^ "\"")
neuper@37906
   236
walther@59984
   237
(*/------- to Specify from Specification -------\*)
walther@59984
   238
(* 
walther@59984
   239
  TODO: unify with Specify.find_next_step ! ! ! USED ONLY to determine Pos.Pbl .. Pos.Met
walther@59984
   240
walther@59984
   241
   determine the next step of specification;
neuper@38051
   242
   not done here: Refine_Tacitly (otherwise *** unknown method: (..., no_met))
neuper@38051
   243
   eg. in rootpbl 'no_met': 
neuper@37906
   244
args:
neuper@38051
   245
  preok          predicates are _all_ ok (and problem matches completely)
neuper@37906
   246
  oris           immediately from formalization 
neuper@37906
   247
  (dI',pI',mI')  specification coming from author/parent-problem
neuper@37906
   248
  (pbl,          item lists specified by user
neuper@37906
   249
   met)          -"-, tacitly completed by copy_probl
neuper@37906
   250
  (dI,pI,mI)     specification explicitly done by the user
neuper@37906
   251
  (pbt, mpc)     problem type, guard of method
neuper@38051
   252
*)
walther@59969
   253
fun nxt_spec Pos.Pbl preok oris (dI', pI', mI') (pbl, met) (pbt, mpc) (dI, pI, mI) = 
walther@59969
   254
    (if dI' = ThyC.id_empty andalso dI = ThyC.id_empty then (Pos.Pbl, Tactic.Specify_Theory dI')
walther@59969
   255
     else if pI' = Problem.id_empty andalso pI = Problem.id_empty then (Pos.Pbl, Tactic.Specify_Problem pI')
wneuper@59265
   256
     else
walther@59988
   257
       case find_first (I_Model.is_error o #5) pbl of
wneuper@59265
   258
	       SOME (_, _, _, fd, itm_) =>
walther@59969
   259
	         (Pos.Pbl, mk_delete (ThyC.get_theory (if dI = ThyC.id_empty then dI' else dI)) fd itm_)
wneuper@59265
   260
	     | NONE => 
walther@59881
   261
	       (case nxt_add (ThyC.get_theory (if dI = ThyC.id_empty then dI' else dI)) oris pbt pbl of
walther@59969
   262
	          SOME (fd, ct') => (Pos.Pbl, mk_additem fd ct')
wneuper@59265
   263
	        | NONE => (*pbl-items complete*)
walther@59969
   264
	          if not preok then (Pos.Pbl, Tactic.Refine_Problem pI')
walther@59969
   265
	          else if dI = ThyC.id_empty then (Pos.Pbl, Tactic.Specify_Theory dI')
walther@59969
   266
		        else if pI = Problem.id_empty then (Pos.Pbl, Tactic.Specify_Problem pI')
walther@59969
   267
		        else if mI = Method.id_empty then (Pos.Pbl, Tactic.Specify_Method mI')
wneuper@59265
   268
		        else
walther@59988
   269
			        case find_first (I_Model.is_error o #5) met of
walther@59957
   270
			          SOME (_, _, _, fd, itm_) => (Pos.Met, mk_delete (ThyC.get_theory dI) fd itm_)
wneuper@59265
   271
			        | NONE => 
walther@59881
   272
			          (case nxt_add (ThyC.get_theory dI) oris mpc met of
walther@59957
   273
				          SOME (fd, ct') => (Pos.Met, mk_additem fd ct') (*30.8.01: pre?!?*)
walther@59957
   274
				        | NONE => (Pos.Met, Tactic.Apply_Method mI))))
walther@59969
   275
  | nxt_spec Pos.Met preok oris (dI', pI', _) (_, met) (_ ,mpc) (dI, pI, mI) = 
walther@59988
   276
    (case find_first (I_Model.is_error o #5) met of
walther@59969
   277
      SOME (_,_,_,fd,itm_) => (Pos.Met, mk_delete (ThyC.get_theory (if dI = ThyC.id_empty then dI' else dI)) fd itm_)
wneuper@59265
   278
    | NONE => 
walther@59881
   279
      case nxt_add (ThyC.get_theory (if dI = ThyC.id_empty then dI' else dI)) oris mpc met of
walther@59969
   280
	      SOME (fd,ct') => (Pos.Met, mk_additem fd ct')
wneuper@59265
   281
      | NONE => 
walther@59969
   282
	      (if dI = ThyC.id_empty then (Pos.Met, Tactic.Specify_Theory dI')
walther@59969
   283
	       else if pI = Problem.id_empty then (Pos.Met, Tactic.Specify_Problem pI')
walther@59969
   284
		     else if not preok then (Pos.Met, Tactic.Specify_Method mI)
walther@59969
   285
		     else (Pos.Met, Tactic.Apply_Method mI)))
walther@59962
   286
  | nxt_spec p _ _ _ _ _ _ = raise ERROR ("nxt_spec: uncovered case with " ^ Pos.pos_2str p)
walther@59984
   287
(*\------- to Specify from Specification -------/*)
neuper@37906
   288
wneuper@59265
   289
(* output the headline to a ppc *)
wneuper@59265
   290
fun header p_ pI mI =
walther@59969
   291
  case p_ of
walther@59969
   292
    Pos.Pbl => Test_Out.Problem (if pI = Problem.id_empty then [] else pI) 
walther@59969
   293
	| Pos.Met => Test_Out.Method mI
walther@59969
   294
	| pos => raise ERROR ("header called with "^ Pos.pos_2str pos)
neuper@37906
   295
walther@59957
   296
fun make m_field (term_as_string, i_model) =
walther@59957
   297
  case m_field of
walther@59957
   298
    "#Given" => Tactic.Add_Given' (term_as_string, i_model)
walther@59957
   299
  | "#Find" => Tactic.Add_Find' (term_as_string, i_model)
walther@59957
   300
  | "#Relate" => Tactic.Add_Relation'(term_as_string, i_model)
walther@59957
   301
  | str => raise ERROR ("specify_additem Frm, Pbl: uncovered case with " ^ str);
walther@59957
   302
fun get (pt, (p, _)) =
walther@59957
   303
  case Ctree.get_obj I pt p of
walther@59957
   304
    (Ctree.PblObj {meth, origin = (oris, (dI', pI', mI'), _), probl, spec = (dI, pI, mI), ctxt, ...})
walther@59985
   305
      => (meth, oris, (dI', pI', mI'), probl, (dI, pI, mI), ctxt)
walther@59957
   306
  | _ => raise ERROR "specify_additem: uncovered case of get_obj I pt p";
walther@59969
   307
fun specify_additem sel ct (pt, pos as (p, Pos.Met)) = 
neuper@41993
   308
      let
walther@59985
   309
        val (met, oris, (dI', pI', mI'), pbl, (dI, pI, mI), ctxt) = get (pt, pos)
walther@59957
   310
        val thy = if dI = ThyC.id_empty then ThyC.get_theory dI' else ThyC.get_theory dI
walther@59957
   311
        val cpI = if pI = Problem.id_empty then pI' else pI
walther@59957
   312
        val cmI = if mI = Method.id_empty then mI' else mI
walther@59970
   313
        val {ppc, pre, prls, ...} = Method.from_store cmI
walther@59957
   314
      in 
walther@59958
   315
        case I_Model.check_single ctxt sel oris met ppc ct of
walther@59957
   316
          I_Model.Add itm =>  (*..union old input *)
walther@59957
   317
    	      let
walther@59958
   318
              val met' = I_Model.add_single thy itm met
walther@59957
   319
    	        val (p, pt') =
walther@59969
   320
    	         case Specify_Step.add (make sel (ct, met')) (Istate_Def.Uistate, ctxt) (pt, (p, Pos.Met)) of
walther@59969
   321
    	          ((p, Pos.Met), _, _, pt') => (p, pt')
walther@59962
   322
    	        | _ => raise ERROR "specify_additem: uncovered case of generate1"
walther@59965
   323
    	        val pre' = Pre_Conds.check' thy prls pre met'
walther@59957
   324
    	        val pb = foldl and_ (true, map fst pre')
walther@59957
   325
    	        val (p_, _) =
walther@59969
   326
    	          nxt_spec Pos.Met pb oris (dI',pI',mI') (pbl,met') 
walther@59970
   327
    	            ((#ppc o Problem.from_store) cpI,ppc) (dI,pI,mI);
walther@59957
   328
    	      in 
walther@59957
   329
              ("ok", ([], [], (pt', (p, p_))))
walther@59957
   330
            end
walther@59957
   331
        | I_Model.Err msg =>
walther@59957
   332
    	      let
walther@59965
   333
              val pre' = Pre_Conds.check' thy prls pre met
walther@59957
   334
    	        val pb = foldl and_ (true, map fst pre')
walther@59957
   335
    	        val (p_, _) =
walther@59969
   336
    	          nxt_spec Pos.Met pb oris (dI',pI',mI') (pbl,met) 
walther@59970
   337
    	            ((#ppc o Problem.from_store) cpI,(#ppc o Method.from_store) cmI) (dI,pI,mI);
walther@59957
   338
    	      in
walther@59957
   339
              (msg, ([], [], (pt, (p, p_))))
walther@59957
   340
    	      end
walther@59957
   341
      end
walther@59957
   342
  | specify_additem sel ct (pt, pos as (p, _(*Frm, Pbl*))) =
walther@59957
   343
      let
walther@59985
   344
        val (met, oris, (dI', pI', mI'), pbl, (dI, pI, mI), ctxt) = get (pt, pos)
walther@59881
   345
        val thy = if dI = ThyC.id_empty then ThyC.get_theory dI' else ThyC.get_theory dI
walther@59903
   346
        val cpI = if pI = Problem.id_empty then pI' else pI
walther@59903
   347
        val cmI = if mI = Method.id_empty then mI' else mI
walther@59970
   348
        val {ppc, where_, prls, ...} = Problem.from_store cpI
neuper@41993
   349
      in
walther@59958
   350
        case I_Model.check_single ctxt sel oris pbl ppc ct of
walther@59956
   351
          I_Model.Add itm => (*..union old input *)
neuper@41993
   352
	          let
walther@59958
   353
	            val pbl' = I_Model.add_single thy itm pbl
walther@59810
   354
	            val (p, pt') =
walther@59957
   355
	              case Specify_Step.add (make sel (ct, pbl')) (Istate_Def.Uistate, ctxt) (pt, (p, Pos.Pbl)) of
walther@59969
   356
  	              ((p, Pos.Pbl), _, _, pt') => (p, pt')
walther@59957
   357
  	            | _ => raise ERROR "specify_additem: uncovered case of Specify_Step.add"
walther@59957
   358
(* only for getting final p_ ..*)
walther@59965
   359
	            val pre = Pre_Conds.check' thy prls where_ pbl';
walther@59957
   360
	            val pb = foldl and_ (true, map fst pre);
walther@59957
   361
	            val (p_, _) =
walther@59970
   362
	              nxt_spec Pos.Pbl pb oris (dI',pI',mI') (pbl',met) (ppc, (#ppc o Method.from_store) cmI) (dI, pI, mI);
wneuper@59265
   363
	          in
walther@59806
   364
              ("ok", ([], [], (pt', (p, p_))))
neuper@41993
   365
            end
walther@59956
   366
        | I_Model.Err msg =>
neuper@41993
   367
	          let
walther@59965
   368
              val pre = Pre_Conds.check' thy prls where_ pbl
neuper@41993
   369
	            val pb = foldl and_ (true, map fst pre)
walther@59957
   370
	            val (p_, _(*Tactic.input*)) =
walther@59957
   371
	              nxt_spec Pos.Pbl pb oris (dI', pI', mI') (pbl, met) 
walther@59970
   372
	                (ppc, (#ppc o Method.from_store) cmI) (dI, pI, mI)
walther@59806
   373
	          in
walther@59806
   374
            (msg, ([], [], (pt, (p, p_))))
walther@59806
   375
	          end
wneuper@59265
   376
      end
neuper@37906
   377
neuper@41994
   378
(*FIXME.WN110515 declare_constraints for ct (without dsc) into PblObj{ctxt, ...}
neuper@41994
   379
  -- for input from scratch*)
walther@59969
   380
fun nxt_specif_additem sel ct (ptp as (pt, (p, Pos.Pbl))) = 
wneuper@59265
   381
    let
walther@59957
   382
      val (oris, dI', pI', dI, pI, pbl, ctxt) = case Ctree.get_obj I pt p of
walther@59957
   383
        Ctree.PblObj {origin = (oris, (dI', pI', _), _), spec = (dI, pI, _), probl = pbl, ctxt, ...} =>
wneuper@59265
   384
           (oris, dI', pI', dI, pI, pbl, ctxt)
walther@59962
   385
      | _ => raise ERROR "specify (Specify_Theory': uncovered case get_obj"
walther@59881
   386
      val thy = if dI = ThyC.id_empty then ThyC.get_theory dI' else ThyC.get_theory dI;
walther@59903
   387
      val cpI = if pI = Problem.id_empty then pI' else pI;
wneuper@59265
   388
    in
walther@59970
   389
      case I_Model.check_single ctxt sel oris pbl ((#ppc o Problem.from_store) cpI) ct of
walther@59956
   390
	      I_Model.Add itm (*..union old input *) =>
wneuper@59265
   391
	        let
walther@59958
   392
	          val pbl' = I_Model.add_single thy itm pbl
wneuper@59265
   393
	          val (tac, tac_) = case sel of
wneuper@59571
   394
		          "#Given" => (Tactic.Add_Given    ct, Tactic.Add_Given'   (ct, pbl'))
wneuper@59571
   395
		        | "#Find"  => (Tactic.Add_Find     ct, Tactic.Add_Find'    (ct, pbl'))
wneuper@59571
   396
		        | "#Relate"=> (Tactic.Add_Relation ct, Tactic.Add_Relation'(ct, pbl'))
walther@59962
   397
		        | sel => raise ERROR ("nxt_specif_additem: uncovered case of" ^ sel)
walther@59810
   398
		        val (p, c, pt') =
walther@59969
   399
		          case Specify_Step.add tac_ (Istate_Def.Uistate, ctxt) (pt, (p, Pos.Pbl)) of
walther@59969
   400
  		          ((p, Pos.Pbl), c, _, pt') =>  (p, c, pt')
walther@59962
   401
  		        | _ => raise ERROR "nxt_specif_additem: uncovered case generate1"
wneuper@59265
   402
	        in
walther@59969
   403
	          ([(tac, tac_, ((p, Pos.Pbl), (Istate_Def.Uistate, ctxt)))], c, (pt', (p, Pos.Pbl)))
wneuper@59265
   404
          end	       
walther@59956
   405
	    | I_Model.Err msg => (*TODO.WN03 pass error-msgs to the frontend..
wneuper@59265
   406
                     FIXME ..and dont abuse a tactic for that purpose*)
walther@59879
   407
	        ([(Tactic.Tac msg, Tactic.Tac_ (ThyC.get_theory "Isac_Knowledge", msg,msg,msg),
walther@59957
   408
	          (Pos.e_pos', (Istate_Def.empty, ContextC.empty)))], [], ptp) 
wneuper@59265
   409
    end
walther@59969
   410
  | nxt_specif_additem sel ct (ptp as (pt, (p, Pos.Met))) = 
wneuper@59265
   411
    let
walther@59957
   412
      val (oris, dI', mI', dI, mI, met, ctxt) = case Ctree.get_obj I pt p of
walther@59957
   413
        Ctree.PblObj {origin = (oris, (dI', _, mI'), _), spec = (dI, _, mI), meth = met,ctxt, ...} =>
wneuper@59265
   414
           (oris, dI', mI', dI, mI, met, ctxt)
walther@59962
   415
      | _ => raise ERROR "nxt_specif_additem Met: uncovered case get_obj"
walther@59881
   416
      val thy = if dI = ThyC.id_empty then ThyC.get_theory dI' else ThyC.get_theory dI;
walther@59903
   417
      val cmI = if mI = Method.id_empty then mI' else mI;
wneuper@59265
   418
    in 
walther@59970
   419
      case I_Model.check_single ctxt sel oris met ((#ppc o Method.from_store) cmI) ct of
walther@59956
   420
        I_Model.Add itm (*..union old input *) =>
wneuper@59265
   421
	        let
walther@59958
   422
	          val met' = I_Model.add_single thy itm met;
wneuper@59265
   423
	          val (tac,tac_) = case sel of
wneuper@59571
   424
		          "#Given" => (Tactic.Add_Given    ct, Tactic.Add_Given'   (ct, met'))
wneuper@59571
   425
		        | "#Find"  => (Tactic.Add_Find     ct, Tactic.Add_Find'    (ct, met'))
wneuper@59571
   426
		        | "#Relate"=> (Tactic.Add_Relation ct, Tactic.Add_Relation'(ct, met'))
walther@59962
   427
		        | sel => raise ERROR ("nxt_specif_additem Met: uncovered case of" ^ sel)
walther@59810
   428
	          val (p, c, pt') =
walther@59969
   429
	            case Specify_Step.add tac_ (Istate_Def.Uistate, ctxt) (pt, (p, Pos.Met)) of
walther@59969
   430
  	            ((p, Pos.Met), c, _, pt') => (p, c, pt')
walther@59962
   431
  		        | _ => raise ERROR "nxt_specif_additem: uncovered case generate1 (WARNING WHY ?)"
wneuper@59265
   432
	        in
walther@59969
   433
	          ([(tac, tac_, ((p, Pos.Met), (Istate_Def.Uistate, ctxt)))], c, (pt', (p, Pos.Met)))
wneuper@59265
   434
	        end
walther@59956
   435
      | I_Model.Err _ => ([(*tacis*)], [], ptp) (*nxt_me collects tacis until not hide; here just no progress*)
wneuper@59265
   436
    end
walther@59962
   437
  | nxt_specif_additem _ _ (_, p) = raise ERROR ("nxt_specif_additem not impl. for" ^ Pos.pos'2str p)
neuper@41994
   438
wneuper@59265
   439
(* fun tag_form thy (formal, given) = Thm.global_cterm_of thy
wneuper@59265
   440
	 (((head_of o Thm.term_of) given) $ (Thm.term_of formal)); WN100819 *)
neuper@37934
   441
fun tag_form thy (formal, given) =
neuper@52070
   442
 (let
neuper@52070
   443
    val gf = (head_of given) $ formal;
wneuper@59184
   444
    val _ = Thm.global_cterm_of thy gf
neuper@52070
   445
  in gf end)
walther@59962
   446
  handle _ => raise ERROR ("calchead.tag_form: " ^ UnparseC.term_in_thy thy given ^
walther@59870
   447
    " .. " ^ UnparseC.term_in_thy thy formal ^ " ..types do not match")
neuper@38053
   448
wneuper@59265
   449
fun chktyps thy (fs, gs) = map (tag_form thy) (fs ~~ gs)
neuper@37906
   450
wneuper@59265
   451
(* complete _NON_empty calc-head for autocalc (sub-)pbl from oris
wneuper@59265
   452
  + met from fmz; assumes pos on PblObj, meth = []                    *)
walther@59957
   453
fun complete_mod (pt, pos as (p, p_)) =
wneuper@59265
   454
  let
walther@59957
   455
    val _ = if p_ <> Pos.Pbl 
walther@59962
   456
	    then raise ERROR ("###complete_mod: only impl.for Pbl, called with " ^ Pos.pos'2str pos)
wneuper@59265
   457
	    else ()
walther@59957
   458
	  val (oris, ospec, probl, spec) = case Ctree.get_obj I pt p of
walther@59957
   459
	    Ctree.PblObj {origin = (oris, ospec, _), probl, spec, ...} => (oris, ospec, probl, spec)
walther@59962
   460
	  | _ => raise ERROR "complete_mod: unvered case get_obj"
walther@59983
   461
  	val (_, pI, mI) = References.select ospec spec
walther@59970
   462
  	val mpc = (#ppc o Method.from_store) mI
walther@59970
   463
  	val ppc = (#ppc o Problem.from_store) pI
walther@59988
   464
  	val (pits, mits) = I_Model.complete_method (oris, mpc, ppc, probl)
walther@59957
   465
    val pt = Ctree.update_pblppc pt p pits
walther@59957
   466
	  val pt = Ctree.update_metppc pt p mits
walther@59957
   467
  in (pt, (p, Pos.Met)) end
neuper@37906
   468
walther@59820
   469
(* do all specification in one single step:
walther@59820
   470
   complete calc-head for autocalc (sub-)pbl from oris (+ met from fmz);
walther@59820
   471
   oris and spec (incl. pbl-refinement) given from init_calc or SubProblem
walther@59820
   472
*)
walther@59671
   473
fun all_modspec (pt, (p, _)) =
wneuper@59265
   474
  let
walther@59957
   475
    val (pors, dI, pI, mI) = case Ctree.get_obj I pt p of
walther@59957
   476
      Ctree.PblObj {origin = (pors, (dI, pI, mI), _), ...}
walther@59820
   477
        => (pors, dI, pI, mI)
walther@59820
   478
    | _ => raise ERROR "all_modspec: uncovered case get_obj"
walther@59970
   479
	  val {ppc, ...} = Method.from_store mI
walther@59987
   480
    val (_, vals) = O_Model.values' pors
wneuper@59582
   481
	  val ctxt = ContextC.initialise dI vals
walther@59957
   482
    val (pt, _) = Ctree.cupdate_problem pt p ((dI, pI, mI),
walther@59988
   483
      map (I_Model.complete' ppc) pors, map (I_Model.complete' ppc) pors, ctxt)
wneuper@59265
   484
  in
walther@59957
   485
    (pt, (p, Pos.Met))
wneuper@59265
   486
  end
neuper@37906
   487
walther@59988
   488
(*/------- to I_Model from Specification -------\* )
wneuper@59265
   489
(* WN0312: use in nxt_spec, too ? what about variants ??? *)
wneuper@59308
   490
fun is_complete_mod_ [] = false
wneuper@59265
   491
  | is_complete_mod_ itms = foldl and_ (true, (map #3 itms))
walther@59988
   492
( *\------- to I_Model from Specification -------/*)
neuper@41976
   493
walther@59969
   494
fun is_complete_mod (pt, pos as (p, Pos.Pbl)) =
walther@59957
   495
    if (Ctree.is_pblobj o (Ctree.get_obj I pt)) p 
walther@59988
   496
    then (I_Model.is_complete o (Ctree.get_obj Ctree.g_pbl pt)) p
walther@59962
   497
    else raise ERROR ("is_complete_mod: called by PrfObj at " ^ Pos.pos'2str pos)
walther@59969
   498
  | is_complete_mod (pt, pos as (p, Pos.Met)) = 
walther@59957
   499
    if (Ctree.is_pblobj o (Ctree.get_obj I pt)) p 
walther@59988
   500
    then (I_Model.is_complete o (Ctree.get_obj Ctree.g_met pt)) p
walther@59962
   501
    else raise ERROR ("is_complete_mod: called by PrfObj at " ^ Pos.pos'2str pos)
neuper@37906
   502
  | is_complete_mod (_, pos) =
walther@59962
   503
    raise ERROR ("is_complete_mod called by " ^ Pos.pos'2str pos ^ " (should be Pbl or Met)")
neuper@37906
   504
walther@59986
   505
fun is_complete_modspec ptp = is_complete_mod ptp andalso References.are_complete ptp
neuper@37906
   506
walther@59969
   507
walther@59969
   508
(** get the formula from a ctree-node **)
walther@59969
   509
(*
wneuper@59265
   510
  take form+res from PblObj and 1.PrfObj and (PrfObj after PblObj)
wneuper@59265
   511
  take res from all other PrfObj's
walther@59969
   512
  Designed for interSteps, outcommented 04 in favour of calcChangedEvent
walther@59969
   513
*)
walther@59957
   514
fun formres p (Ctree.Nd (Ctree.PblObj {origin = (_, _, h), result = (r, _), ...}, _)) =
walther@59957
   515
    [("headline", (p, Pos.Frm), h), ("stepform", (p, Pos.Res), r)]
walther@59957
   516
  | formres p (Ctree.Nd (Ctree.PrfObj {form, result = (r, _), ...}, _)) = 
walther@59957
   517
    [("stepform", (p, Pos.Frm), form), ("stepform", (p, Pos.Res), r)]
walther@59962
   518
  | formres _ _ = raise ERROR "formres: uncovered definition" 
walther@59957
   519
fun form p (Ctree.Nd (Ctree.PrfObj {result = (r, _), ...}, _)) = 
walther@59957
   520
    [("stepform", (p, Pos.Res), r)]
walther@59962
   521
  | form _ _ = raise ERROR "form: uncovered definition" 
neuper@37906
   522
wneuper@59265
   523
(* assumes to take whole level, in particular hd -- for use in interSteps *)
wneuper@59265
   524
fun get_formress fs _ [] = flat fs
neuper@37906
   525
  | get_formress fs p (nd::nds) =
neuper@37906
   526
    (* start with   'form+res'       and continue with trying 'res' only*)
walther@59957
   527
    get_forms (fs @ [formres p nd]) (Pos.lev_on p) nds
wneuper@59265
   528
and get_forms fs _ [] = flat fs
neuper@37906
   529
  | get_forms fs p (nd::nds) =
walther@59957
   530
    if Ctree.is_pblnd nd
neuper@37906
   531
    (* start again with      'form+res' ///ugly repeat with Check_elementwise
neuper@37906
   532
    then get_formress (fs @ [formres p nd]) (lev_on p) nds                   *)
walther@59957
   533
    then get_forms    (fs @ [formres p nd]) (Pos.lev_on p) nds
neuper@37906
   534
    (* continue with trying 'res' only*)
walther@59957
   535
    else get_forms    (fs @ [form    p nd]) (Pos.lev_on p) nds;
neuper@37906
   536
walther@59969
   537
wneuper@59265
   538
(* get a calchead from a PblObj-node in the ctree; preconditions must be calculated *)
walther@59969
   539
fun get_ocalhd (pt, (p, Pos.Pbl)) = 
wneuper@59265
   540
    let
walther@59957
   541
	    val (ospec, hdf', spec, probl) = case Ctree.get_obj I pt p of
walther@59957
   542
	      Ctree.PblObj {origin = (_, ospec, hdf'), spec, probl,...} => (ospec, hdf', spec, probl)
walther@59962
   543
	    | _ => raise ERROR "get_ocalhd Pbl: uncovered case get_obj"
walther@59983
   544
      val {prls, where_, ...} = Problem.from_store (#2 (References.select ospec spec))
walther@59965
   545
      val pre = Pre_Conds.check' (ThyC.get_theory "Isac_Knowledge") prls where_ probl
wneuper@59265
   546
    in
walther@59969
   547
      (ocalhd_complete probl pre spec, Pos.Pbl, hdf', probl, pre, spec)
wneuper@59265
   548
    end
walther@59969
   549
  | get_ocalhd (pt, (p, Pos.Met)) = 
wneuper@59265
   550
    let
walther@59957
   551
		  val (ospec, hdf', spec, meth) = case Ctree.get_obj I pt p of
walther@59957
   552
		    Ctree.PblObj {origin = (_, ospec, hdf'), spec, meth, ...} => (ospec, hdf', spec, meth)
walther@59962
   553
		  | _ => raise ERROR "get_ocalhd Met: uncovered case get_obj"
walther@59983
   554
      val {prls, pre, ...} = Method.from_store (#3 (References.select ospec spec))
walther@59965
   555
      val pre = Pre_Conds.check' (ThyC.get_theory "Isac_Knowledge") prls pre meth
wneuper@59265
   556
    in
walther@59969
   557
      (ocalhd_complete meth pre spec, Pos.Met, hdf', meth, pre, spec)
wneuper@59265
   558
    end
walther@59962
   559
  | get_ocalhd _ = raise ERROR "get_ocalhd: uncovered definition"
neuper@37906
   560
walther@59977
   561
(* at the activeFormula set the Specification 
wneuper@59265
   562
   to empty and return a CalcHead;
wneuper@59265
   563
   the 'origin' remains (for reconstructing all that) *)
walther@59957
   564
fun reset_calchead (pt, (p,_)) = 
wneuper@59265
   565
  let
walther@59957
   566
    val () = case Ctree.get_obj I pt p of
walther@59957
   567
      Ctree.PblObj _ => ()
walther@59962
   568
    | _ => raise ERROR "reset_calchead: uncovered case get_obj"
walther@59957
   569
    val pt = Ctree.update_pbl pt p []
walther@59957
   570
    val pt = Ctree.update_met pt p []
walther@59976
   571
    val pt = Ctree.update_spec pt p References.empty
walther@59957
   572
  in (pt, (p, Pos.Pbl)) end
neuper@37906
   573
walther@59763
   574
(**)end(**)