src/Tools/isac/Specify/i-model.sml
author wneuper <Walther.Neuper@jku.at>
Thu, 16 Nov 2023 08:15:46 +0100
changeset 60763 2121f1a39a64
parent 60762 f10bbfb2b3bb
child 60764 f82fd40eb400
permissions -rw-r--r--
prepare 14: improved item_to_add

emergency-CS:
* no code cleanup
* ERROR in test/../biegelinie-3.sml outcommented
walther@59938
     1
(* Title:  Specify/i-model.sml
walther@59938
     2
   Author: Walther Neuper 110226
walther@59938
     3
   (c) due to copyright terms
walther@59998
     4
walther@60004
     5
\<open>I_Model\<close> serves students' interactive modelling and gives feedback in the specify-phase.
walther@59998
     6
*)
walther@59938
     7
Walther@60694
     8
signature INTERACTION_MODEL =
walther@59938
     9
sig
walther@59969
    10
walther@59961
    11
  type T
Walther@60695
    12
  type T_TEST
Walther@60708
    13
  val OLD_to_TEST: T -> T_TEST
Walther@60714
    14
  val TEST_to_OLD: T_TEST -> T
Walther@60467
    15
  val empty: T
Walther@60704
    16
  val empty_TEST: T_TEST
Walther@60747
    17
walther@59961
    18
  type single
Walther@60694
    19
  type single_TEST
Walther@60467
    20
  val empty_single: single
Walther@60747
    21
  val empty_single_TEST: single_TEST
Walther@60747
    22
  val is_empty_single_TEST: single_TEST -> bool
Walther@60747
    23
walther@60018
    24
  type variant
walther@59960
    25
  type variants
walther@59961
    26
  type m_field
walther@59961
    27
  type descriptor
Walther@60762
    28
  type values
Walther@60747
    29
walther@59948
    30
  datatype feedback = datatype Model_Def.i_model_feedback
Walther@60694
    31
  datatype feedback_TEST = datatype Model_Def.i_model_feedback_TEST
Walther@60747
    32
  val feedback_empty_TEST: Model_Def.i_model_feedback_TEST
Walther@60747
    33
Walther@60705
    34
  type env
walther@59998
    35
  type message
walther@59938
    36
walther@59942
    37
  val single_to_string: Proof.context -> single -> string
Walther@60694
    38
  val single_to_string_TEST: Proof.context -> single_TEST -> string
Walther@60763
    39
  val single_to_string_TEST': single_TEST -> string
walther@59942
    40
  val to_string: Proof.context -> T -> string
Walther@60694
    41
  val to_string_TEST: Proof.context -> T_TEST -> string
Walther@60763
    42
Walther@60708
    43
  val feedback_OLD_to_TEST: feedback -> feedback_TEST
walther@59942
    44
Walther@60477
    45
  datatype add_single = Add of single | Err of string
walther@59958
    46
  val init: Model_Pattern.T -> T
Walther@60703
    47
  val init_TEST: O_Model.T -> Model_Pattern.T -> T_TEST
walther@59998
    48
  val check_single: Proof.context -> m_field -> O_Model.T -> T -> Model_Pattern.T ->
Walther@60477
    49
    TermC.as_string -> add_single
walther@59958
    50
  val add_single: theory -> single -> T -> T
walther@59956
    51
Walther@60477
    52
  val make_tactic: m_field -> TermC.as_string * T -> Tactic.T
Walther@60477
    53
  val descriptor: feedback -> descriptor
Walther@60705
    54
  val descriptor_TEST: feedback_TEST -> descriptor
Walther@60762
    55
  val values: feedback -> values option
Walther@60763
    56
  val o_model_values: feedback -> values
Walther@60763
    57
  val values_TEST': feedback_TEST -> values
Walther@60710
    58
  val descr_pairs_to_string: Proof.context -> (Model_Pattern.single * single_TEST) list -> string
Walther@60741
    59
  val variables: Model_Pattern.T -> Model_Def.i_model_TEST -> term list
walther@59998
    60
  val is_notyet_input : Proof.context -> T -> O_Model.values -> O_Model.single -> Model_Pattern.T
walther@59998
    61
    -> message * single
Walther@60477
    62
  val get_field_term: theory -> O_Model.single -> single -> m_field * TermC.as_string
walther@59956
    63
Walther@60751
    64
  val add: single -> T -> T
Walther@60763
    65
(*OLD* )
Walther@60763
    66
  val handle_atom: values -> values
Walther@60763
    67
( *---*)
Walther@60763
    68
  val unpack_values: descriptor * values -> values
Walther@60763
    69
  val fill_from_o: O_Model.T -> single_TEST -> single_TEST option
Walther@60763
    70
(*NEW*)
Walther@60763
    71
Walther@60763
    72
Walther@60760
    73
  val add_other: variant -> T_TEST -> single_TEST -> single_TEST
Walther@60760
    74
  val fill_method: O_Model.T -> T_TEST * T_TEST-> Model_Pattern.T -> T_TEST
Walther@60757
    75
  val s_make_complete: Proof.context ->  O_Model.T -> T_TEST * T_TEST -> Problem.id * MethodC.id ->
Walther@60751
    76
    T_TEST * T_TEST
Walther@60757
    77
  val s_are_complete: Proof.context -> O_Model.T -> T_TEST * T_TEST -> Problem.id * MethodC.id -> bool
Walther@60747
    78
walther@59988
    79
  val is_error: feedback -> bool
walther@59988
    80
  val to_p_model: theory -> feedback -> string
Walther@60756
    81
(*/----- from isac_test for Minisubpbl*)
Walther@60751
    82
  val msg: variants -> feedback_TEST -> string
Walther@60751
    83
  val transfer_terms: O_Model.single -> single_TEST
Walther@60751
    84
Walther@60746
    85
  val eq1: ''a -> 'b * (''a * 'c) -> bool
Walther@60741
    86
  val feedback_to_string: Proof.context -> feedback -> string
Walther@60763
    87
(*OLD* )
Walther@60763
    88
  val feedback_NEW_to_string: Proof.context -> feedback_TEST -> string
Walther@60763
    89
( *---*)
Walther@60741
    90
  val feedback_TEST_to_string: Proof.context -> feedback_TEST -> string
Walther@60763
    91
  val descr_vals_to_string: Proof.context -> descriptor * values -> string
Walther@60763
    92
  val feedb_args_to_string: Proof.context -> feedback_TEST -> string
Walther@60763
    93
(*NEW*)
Walther@60741
    94
Walther@60763
    95
  val ori_2itm: feedback -> descriptor -> Model_Pattern.values -> O_Model.single -> single
Walther@60723
    96
  val seek_ppc: int -> T -> single option
Walther@60723
    97
  val overwrite_ppc: theory -> single -> T -> T
Walther@60756
    98
(*\----- from isac_test for Minisubpbl*)
Walther@60723
    99
Walther@60694
   100
\<^isac_test>\<open>
Walther@60694
   101
  (**)
Walther@60723
   102
Walther@60694
   103
\<close>
Walther@60694
   104
walther@59938
   105
end
walther@59938
   106
walther@59942
   107
(**)
Walther@60694
   108
structure I_Model(**) : INTERACTION_MODEL(**) =
walther@59938
   109
struct
walther@59942
   110
(**)
walther@59955
   111
walther@59958
   112
(** data types **)
walther@59958
   113
walther@60018
   114
type variant =  Model_Def.variant;
walther@59940
   115
type variants =  Model_Def.variants;
walther@59952
   116
type m_field = Model_Def.m_field;
walther@59952
   117
type descriptor = Model_Def.descriptor;
Walther@60763
   118
type values = Model_Pattern.values
walther@59938
   119
walther@59940
   120
type T = Model_Def.i_model_single list;
Walther@60702
   121
(* for developing input from PIDE, we use T_TEST with these ideas:
Walther@60702
   122
  (1) the new structure is as close to old T, because we want to preserve the old tests
Walther@60702
   123
  (2) after development (with *_TEST) of essential parts of the Specification's semantics,
Walther@60702
   124
      we adapt the old tests to the new T_TEST
Walther@60702
   125
  (3) together with adaption of the tests we remove the *_TEST
Walther@60702
   126
*)
Walther@60694
   127
type T_TEST = Model_Def.i_model_single_TEST list;
walther@59940
   128
datatype feedback = datatype Model_Def.i_model_feedback;
Walther@60694
   129
datatype feedback_TEST = datatype Model_Def.i_model_feedback_TEST;
Walther@60747
   130
val feedback_empty_TEST = Model_Def.feedback_empty_TEST
walther@59940
   131
type single = Model_Def.i_model_single;
Walther@60694
   132
type single_TEST = Model_Def.i_model_single_TEST;
Walther@60467
   133
val empty_single = Model_Def.i_model_empty;
Walther@60747
   134
val empty_single_TEST = Model_Def.i_model_empty_TEST;
Walther@60747
   135
fun is_empty_single_TEST (0, [], false, "i_model_empty", _) = true
Walther@60747
   136
  | is_empty_single_TEST _ = false
Walther@60747
   137
Walther@60467
   138
val empty = []: T;
Walther@60704
   139
val empty_TEST = []: T_TEST;
Walther@60733
   140
Walther@60740
   141
type env = Env.T
Walther@60740
   142
Walther@60733
   143
Walther@60756
   144
fun feedback_OLD_to_TEST (Cor ((d, ts), _)) = (Model_Def.Cor_TEST (d, ts))
Walther@60706
   145
  | feedback_OLD_to_TEST (Syn c) = (Model_Def.Syn_TEST c)
Walther@60706
   146
  | feedback_OLD_to_TEST (Typ c) = (Model_Def.Syn_TEST c)
Walther@60756
   147
  | feedback_OLD_to_TEST (Inc ((d, ts), _)) = (Model_Def.Inc_TEST (d, ts))
Walther@60706
   148
  | feedback_OLD_to_TEST (Sup (d, ts)) = (Model_Def.Sup_TEST (d, ts))
Walther@60706
   149
  | feedback_OLD_to_TEST (Mis (d, pid)) = Model_Def.Syn_TEST ((UnparseC.term (ContextC.for_ERROR ()) d) ^ " " ^
Walther@60706
   150
      (UnparseC.term (ContextC.for_ERROR ()) pid))
Walther@60706
   151
  | feedback_OLD_to_TEST (Par s) = (Model_Def.Syn_TEST s)
Walther@60706
   152
fun OLD_to_TEST i_old =
Walther@60706
   153
  map (fn (a, b, c, d, e) => (a, b, c, d, (feedback_OLD_to_TEST e, Position.none))) i_old
Walther@60705
   154
Walther@60750
   155
fun feedback_TEST_to_OLD (Model_Def.Cor_TEST (d, ts)) = (Cor ((d, ts), (TermC.empty, [])))
Walther@60714
   156
  | feedback_TEST_to_OLD (Model_Def.Syn_TEST c) = (Syn c)
Walther@60750
   157
  | feedback_TEST_to_OLD (Model_Def.Inc_TEST (d, ts)) = (Inc ((d, ts), (TermC.empty, [])))
Walther@60714
   158
  | feedback_TEST_to_OLD (Model_Def.Sup_TEST (d, ts)) = (Sup (d, ts))
Walther@60714
   159
fun TEST_to_OLD i_model = 
Walther@60714
   160
  map (fn (a, b, c, d, (e, _)) => (a, b, c, d, feedback_TEST_to_OLD e)) i_model
Walther@60714
   161
walther@59998
   162
type message = string;
walther@59938
   163
Walther@60739
   164
fun feedback_to_string ctxt (Cor ((d, ts), _)) = 
Walther@60739
   165
    "Cor " ^ UnparseC.term  ctxt (Input_Descript.join (d, ts)) ^ " , pen2str"
walther@59942
   166
  | feedback_to_string _ (Syn c) = "Syn " ^ c
walther@59942
   167
  | feedback_to_string _ (Typ c) = "Typ " ^ c
Walther@60739
   168
  | feedback_to_string ctxt (Inc ((d, ts), _)) = 
Walther@60739
   169
    "Inc " ^ UnparseC.term  ctxt (Input_Descript.join (d, ts)) ^ " , pen2str"
walther@59942
   170
  | feedback_to_string ctxt (Sup (d, ts)) = 
Walther@60675
   171
    "Sup " ^ UnparseC.term  ctxt (Input_Descript.join (d, ts))
walther@59942
   172
  | feedback_to_string ctxt (Mis (d, pid)) = 
Walther@60698
   173
    "Mis " ^ UnparseC.term  ctxt d ^ " " ^ UnparseC.term ctxt pid
walther@59942
   174
  | feedback_to_string _ (Par s) = "Trm "^s;
Walther@60698
   175
Walther@60750
   176
fun feedback_TEST_to_string ctxt (Cor_TEST (d, ts)) = 
Walther@60733
   177
    "Cor_TEST " ^ UnparseC.term ctxt (Input_Descript.join (d, ts)) ^ " , pen2str"
Walther@60733
   178
  | feedback_TEST_to_string _ (Syn_TEST c) =
Walther@60733
   179
    "Syn_TEST " ^ c
Walther@60750
   180
  | feedback_TEST_to_string ctxt (Inc_TEST (d, [])) = 
Walther@60733
   181
    "Inc_TEST " ^ UnparseC.term ctxt (Input_Descript.join (d, [])) ^ " " ^ 
Walther@60733
   182
      Model_Pattern.empty_for d
Walther@60750
   183
  | feedback_TEST_to_string ctxt (Inc_TEST (d, ts)) =
Walther@60733
   184
    "Inc_TEST " ^ UnparseC.term ctxt (Input_Descript.join (d, ts)) ^ " , pen2str"
Walther@60733
   185
  | feedback_TEST_to_string ctxt (Sup_TEST (d, ts)) = 
Walther@60733
   186
    "Sup_TEST " ^ UnparseC.term ctxt (Input_Descript.join (d, ts))
walther@59942
   187
Walther@60763
   188
fun descr_vals_to_string ctxt (descr, values) =
Walther@60763
   189
    if Pre_Conds.is_list_descr descr
Walther@60763
   190
    then if Pre_Conds.is_NObrack_list descr values
Walther@60763
   191
      then UnparseC.term ctxt descr ^ " " ^ UnparseC.terms_NObrack ctxt values
Walther@60763
   192
      else UnparseC.term ctxt descr ^ " " ^ UnparseC.terms(*_NObrack*) ctxt values(* ^ "]"*)
Walther@60763
   193
    else if TermC.is_atom (hd values)
Walther@60763
   194
      then UnparseC.term ctxt descr ^ " " ^ UnparseC.term ctxt (hd values)
Walther@60763
   195
      else UnparseC.term ctxt descr ^ " (" ^ UnparseC.term ctxt (hd values) ^ ")"
Walther@60763
   196
Walther@60763
   197
(*prepare for presentation to user; thus Syn_TEST does NOT raise an exn*)
Walther@60763
   198
fun feedb_args_to_string ctxt (Cor_TEST (descr, values)) = descr_vals_to_string ctxt (descr, values)
Walther@60763
   199
  | feedb_args_to_string _ (Syn_TEST str) = str
Walther@60763
   200
  | feedb_args_to_string ctxt (Inc_TEST (descr, [])) =
Walther@60763
   201
    UnparseC.term ctxt descr ^ Model_Pattern.empty_for descr
Walther@60763
   202
  | feedb_args_to_string ctxt (Inc_TEST (descr, values)) = descr_vals_to_string ctxt (descr, values)
Walther@60763
   203
  | feedb_args_to_string ctxt (Sup_TEST (descr, values)) = descr_vals_to_string ctxt (descr, values)
Walther@60763
   204
walther@59942
   205
fun single_to_string ctxt (i, is, b, s, itm_) = 
walther@59942
   206
  "(" ^ string_of_int i ^ " ," ^ ints2str' is ^ " ," ^ bool2str b ^ " ," ^
walther@59942
   207
  s ^ " ," ^ feedback_to_string ctxt itm_ ^ ")";
Walther@60702
   208
fun single_to_string_TEST ctxt (i, is, b, s, (itm_, _(*Position.T*))) = 
Walther@60694
   209
  "(" ^ string_of_int i ^ ", " ^ ints2str' is ^ ", " ^ bool2str b ^ " ," ^
Walther@60733
   210
  s ^ ", (" ^ feedback_TEST_to_string ctxt itm_ ^ ", Position.T))";
Walther@60763
   211
fun single_to_string_TEST' (i, is, b, s, (itm_, _(*Position.T*))) = 
Walther@60763
   212
  "(" ^ string_of_int i ^ ", " ^ ints2str' is ^ ", " ^ bool2str b ^ " ," ^
Walther@60763
   213
  s ^ ", (" ^ feedback_TEST_to_string (ContextC.for_ERROR ()) itm_ ^ ", Position.T))";
Walther@60694
   214
walther@59942
   215
fun to_string ctxt itms = strs2str' (map (linefeed o (single_to_string ctxt)) itms);
Walther@60694
   216
fun to_string_TEST ctxt itms = strs2str' (map (linefeed o (single_to_string_TEST ctxt)) itms);
walther@59942
   217
walther@59958
   218
Walther@60694
   219
(** make a Tactic.T **)
Walther@60694
   220
Walther@60477
   221
fun make_tactic m_field (term_as_string, i_model) =
walther@59992
   222
  case m_field of
walther@59992
   223
    "#Given" => Tactic.Add_Given' (term_as_string, i_model)
walther@59992
   224
  | "#Find" => Tactic.Add_Find' (term_as_string, i_model)
walther@59992
   225
  | "#Relate" => Tactic.Add_Relation'(term_as_string, i_model)
walther@59992
   226
  | str => raise ERROR ("specify_additem Frm, Pbl: uncovered case with " ^ str);
walther@59992
   227
walther@59992
   228
walther@59958
   229
(** initialise a model **)
walther@59958
   230
walther@59958
   231
fun init pbt = 
walther@59958
   232
  let
walther@59958
   233
    fun pbt2itm (f, (d, _)) = (0, [], false, f, Inc ((d, []), (TermC.empty, [])))
walther@59958
   234
  in map pbt2itm pbt end
Walther@60556
   235
Walther@60702
   236
(*
Walther@60702
   237
  Design decision:
Walther@60705
   238
* Now the Model in Specification is intialised such that the placement of items can be
Walther@60702
   239
  maximally stable during interactive input to the Specification.
Walther@60702
   240
* Template.show provides the initial output to the user and thus determines what will be parsed
Walther@60702
   241
  by Outer_Syntax later during interaction.
Walther@60705
   242
* The relation between O_Model.T and I_Model.T becomes much simpler.
Walther@60702
   243
*)
Walther@60702
   244
(**)
Walther@60702
   245
fun pat_to_item o_model (_, (descriptor, _)) =
Walther@60702
   246
  case find_first (fn (_, _, _, desc, _) => desc = descriptor) o_model of
Walther@60702
   247
    NONE => raise ERROR "I_Model.pat_to_item_TEST with NONE"
Walther@60702
   248
  | SOME (_, variants, m_field, descr, _) => (variants, false, m_field,
Walther@60750
   249
    (Inc_TEST (descr, []), Position.none))
Walther@60702
   250
fun init_TEST o_model model_patt =
Walther@60690
   251
  let
Walther@60702
   252
    val pre_items = map (pat_to_item o_model) model_patt
Walther@60702
   253
  in
Walther@60702
   254
    O_Model.add_enumerate pre_items |> map (fn (a, (b, c, d, e)) => (a, b, c, d, e))
Walther@60702
   255
  end
walther@59943
   256
walther@59943
   257
Walther@60664
   258
val unique = Syntax.read_term\<^context> "UnIqE_tErM";
Walther@60477
   259
fun descriptor (Cor ((d ,_), _)) = d
Walther@60477
   260
  | descriptor (Syn _) = ((*tracing ("*** descriptor: Syn ("^c^")");*) unique)
Walther@60477
   261
  | descriptor (Typ _) = ((*tracing ("*** descriptor: Typ ("^c^")");*) unique)
Walther@60477
   262
  | descriptor (Inc ((d, _), _)) = d
Walther@60477
   263
  | descriptor (Sup (d, _)) = d
Walther@60477
   264
  | descriptor (Mis (d, _)) = d
Walther@60477
   265
  | descriptor _ = raise ERROR "descriptor: uncovered case in fun.def.";
Walther@60750
   266
fun descriptor_TEST (Cor_TEST (d ,_)) = d
Walther@60705
   267
  | descriptor_TEST (Syn_TEST _) = ((*tracing ("*** descriptor: Syn ("^c^")");*) unique)
Walther@60750
   268
  | descriptor_TEST (Inc_TEST (d, _)) = d
Walther@60705
   269
  | descriptor_TEST (Sup_TEST (d, _)) = d
walther@59943
   270
Walther@60762
   271
fun values (Cor ((_ , ts), _)) = SOME ts
Walther@60762
   272
  | values (Syn _) = NONE
Walther@60762
   273
  | values (Typ _) = NONE
Walther@60762
   274
  | values (Inc ((_, ts), _)) = SOME ts
Walther@60762
   275
  | values (Sup (_, ts)) = SOME ts
Walther@60762
   276
  | values (Mis (_, t)) = SOME [t]
Walther@60762
   277
  | values _ = raise ERROR "descriptor: uncovered case in fun.def.";
Walther@60763
   278
fun o_model_values (Cor ((_, ts), _)) = ts
Walther@60763
   279
  | o_model_values (Syn _) = []
Walther@60763
   280
  | o_model_values (Typ _) = []
Walther@60763
   281
  | o_model_values (Inc ((_, ts), _)) = ts
Walther@60763
   282
  | o_model_values (Sup (_, ts)) = ts
Walther@60763
   283
  | o_model_values (Mis _) = []
Walther@60763
   284
  | o_model_values _ = raise ERROR "o_model_values: uncovered case in fun.def.";
Walther@60763
   285
fun values_TEST' (Cor_TEST (_, ts)) = ts
Walther@60763
   286
  | values_TEST' (Syn_TEST _) = raise ERROR "values_TEST' NOT for Syn_TEST"
Walther@60763
   287
  | values_TEST' (Inc_TEST (_, ts)) = ts
Walther@60763
   288
  | values_TEST' (Sup_TEST (_, ts)) = ts
Walther@60762
   289
Walther@60710
   290
fun descr_pairs_to_string ctxt equal_descr_pairs =
Walther@60710
   291
(map (fn (a, b) => pair (Model_Pattern.pat2str ctxt a) (single_to_string_TEST ctxt b)
Walther@60710
   292
    |> pair2str) equal_descr_pairs)
Walther@60710
   293
  |> strs2str'
Walther@60710
   294
Walther@60741
   295
fun variables model_patt i_model =
Walther@60733
   296
  Pre_Conds.environment_TEST model_patt i_model
Walther@60733
   297
  |> map snd
walther@59943
   298
Walther@60664
   299
val unknown_descriptor = Syntax.read_term\<^context> "unknown::'a => unknow";
walther@59956
   300
Walther@60670
   301
(* get a term from O_Model, notyet input in I_Model.
Walther@60670
   302
   the term from O_Model is thrown back to a string in order to reuse
walther@59992
   303
   machinery for immediate input by the user. *)
Walther@60477
   304
fun get_field_term thy (_, _, _, d, ts) (_, _, _, fd, itm_) =
Walther@60477
   305
  (fd, ((UnparseC.term_in_thy thy) o Input_Descript.join) (d, subtract op = (o_model_values itm_) ts))
walther@59992
   306
Walther@60740
   307
(*update the itm_ already input, all..from ori*)
walther@59956
   308
fun ori_2itm itm_ pid all (id, vt, fd, d, ts) = 
walther@59956
   309
  let 
Walther@60477
   310
    val ts' = union op = (o_model_values itm_) ts;
Walther@60478
   311
    val pval = [Input_Descript.join'''' (d, ts')]
Walther@60740
   312
	  (*WN.9.5.03: FIXXXME [#0, epsilon] here would upd_penv be called for [#0, epsilon] etc*)
walther@59956
   313
    val complete = if eq_set op = (ts', all) then true else false
walther@59956
   314
  in
walther@59956
   315
    case itm_ of
walther@59956
   316
      (Cor _) => 
walther@59956
   317
        (if fd = "#undef" then (id, vt, complete, fd, Sup (d, ts')) 
walther@59956
   318
	       else (id, vt, complete, fd, Cor ((d, ts'), (pid, pval))))
walther@59962
   319
    | (Syn c) => raise ERROR ("ori_2itm wants to overwrite " ^ c)
walther@59962
   320
    | (Typ c) => raise ERROR ("ori_2itm wants to overwrite " ^ c)
walther@59956
   321
    | (Inc _) =>
walther@59956
   322
      if complete
walther@59956
   323
  	  then (id, vt, true, fd, Cor ((d, ts'), (pid, pval)))
walther@59956
   324
  	  else (id, vt, false, fd, Inc ((d, ts'), (pid, pval)))
walther@59956
   325
    | (Sup (d,ts')) => (*4.9.01 lost env*)
walther@59956
   326
  	  (*if fd = "#undef" then*) (id,vt,complete,fd,Sup(d,ts'))
walther@59956
   327
  	  (*else (id,vt,complete,fd,Cor((d,ts'),e))*)
walther@59956
   328
      (* 28.1.00: not completely clear ---^^^ etc.*)
walther@59956
   329
    | (Mis _) => (* 4.9.01: Mis just copied *)
walther@59956
   330
       if complete
walther@59956
   331
  		 then (id, vt, true, fd, Cor ((d,ts'), (pid, pval)))
walther@59956
   332
  		 else (id, vt, false, fd, Inc ((d,ts'), (pid, pval)))
Walther@60733
   333
    | i => raise ERROR ("ori_2itm: uncovered case of " ^ feedback_to_string (ContextC.for_ERROR ()) i)
walther@59956
   334
  end
walther@59956
   335
Walther@60740
   336
Walther@60740
   337
(** find next step **)
Walther@60740
   338
walther@59956
   339
fun eq1 d (_, (d', _)) = (d = d')
Walther@60477
   340
fun eq3 f d (_, _, _, f', itm_) = f = f' andalso d = (descriptor itm_) 
walther@59956
   341
walther@59956
   342
fun is_notyet_input ctxt itms all (i, v, f, d, ts) pbt =
walther@59956
   343
  case find_first (eq1 d) pbt of
walther@59956
   344
    SOME (_, (_, pid)) =>
walther@59956
   345
      (case find_first (eq3 f d) itms of
walther@59998
   346
        SOME (_, _, _, _, itm_) =>
Walther@60477
   347
          let val ts' = inter op = (o_model_values itm_) ts
walther@59956
   348
          in 
walther@59956
   349
            if subset op = (ts, ts') 
Walther@60675
   350
            then (((strs2str' o map (UnparseC.term ctxt)) ts') ^ " already input", empty_single)
walther@59998
   351
	          else ("", ori_2itm itm_ pid all (i, v, f, d, subtract op = ts' ts))
walther@59956
   352
	          end
walther@59998
   353
	    | NONE => ("", ori_2itm (Inc ((TermC.empty, []), (pid, []))) pid all (i, v, f, d, ts)))
walther@59956
   354
  | NONE => ("", ori_2itm (Sup (d, ts)) TermC.empty all (i, v, f, d, ts))
walther@59956
   355
Walther@60477
   356
datatype add_single =
walther@59958
   357
	Add of single   (* return-value of check_single *)
walther@59998
   358
| Err of string   (* error-message                *)
walther@59956
   359
walther@59956
   360
(*
walther@59956
   361
  Create feedback for input of TermC.as_string to m_field;
walther@59956
   362
  check w.r.t. O_Model.T and Model_Pattern.T.
walther@59998
   363
  In case of O_Model.T = [] (i.e. no data for user-guidance in Formalise.T)
walther@59958
   364
  check_single is extremely permissive.
walther@59956
   365
*)
Walther@60658
   366
(*will come directly from PIDE -----------------vvvvvvvvvvv
Walther@60658
   367
  in case t comes from Step.specify_do_next -----------vvv = Position.none*)
Walther@60658
   368
fun check_single ctxt m_field [] i_model m_patt (ct(*, pos*)) =
Walther@60658
   369
    let
Walther@60658
   370
      val i = 1 + (if i_model = [] then 0 else map #1 i_model |> maxl)
Walther@60661
   371
    (*/------------ replace by ParseC.term_position -----------\*)
Walther@60661
   372
      val t = Syntax.read_term ctxt ct
Walther@60658
   373
        handle ERROR msg => error (msg (*^ Position.here pos*))
Walther@60661
   374
    (*\------------ replace by ParseC.term_position -----------/*)
Walther@60658
   375
        (*NONE => Add (i, [], false, m_field, Syn ct)*)
Walther@60658
   376
      val (d, ts) = Input_Descript.split t
Walther@60658
   377
    in 
Walther@60658
   378
      if d = TermC.empty then
Walther@60658
   379
        Add (i, [], false, m_field, Mis (unknown_descriptor, hd ts)) 
Walther@60658
   380
      else
Walther@60658
   381
        (case find_first (eq1 d) m_patt of
Walther@60658
   382
          NONE => Add (i, [], true, m_field, Sup (d,ts))
Walther@60658
   383
        | SOME (f, (_, id)) =>
Walther@60658
   384
            let
Walther@60658
   385
              fun eq2 d (i, _, _, _, itm_) = d = (descriptor itm_) andalso i <> 0
Walther@60658
   386
            in
Walther@60658
   387
              case find_first (eq2 d) i_model of
Walther@60658
   388
                NONE => Add (i, [], true, f,Cor ((d, ts), (id, [Input_Descript.join'''' (d, ts)])))
Walther@60658
   389
              | SOME (i', _, _, _, itm_) => 
Walther@60658
   390
                  if Input_Descript.for_list d then 
Walther@60658
   391
                    let
Walther@60658
   392
                      val in_itm = o_model_values itm_
Walther@60658
   393
                      val ts' = union op = ts in_itm
Walther@60658
   394
                      val i'' = if in_itm = [] then i else i'
Walther@60658
   395
                    in Add (i'', [], true, f, Cor ((d, ts'), (id, [Input_Descript.join'''' (d, ts')])))end
Walther@60658
   396
                  else Add (i', [], true, f, Cor ((d, ts), (id, [Input_Descript.join'''' (d, ts)])))
Walther@60658
   397
            end)
Walther@60658
   398
    end
Walther@60658
   399
    (*will come directly from PIDE ----------------------vvvvvvvvvvv*)
Walther@60658
   400
  | check_single ctxt m_field o_model i_model m_patt (str(*, pos*)) =
Walther@60658
   401
    let
Walther@60659
   402
      val (t as (descriptor $ _)) = Syntax.read_term ctxt str
Walther@60740
   403
        handle ERROR msg => error (msg (*TODO: ^ Position.here pp*))
Walther@60659
   404
        (*old code: NONE => Err ("ERROR I_Model.check_single: syntax error in \"" ^ str ^ "\"")*)
Walther@60658
   405
    in 
Walther@60658
   406
        case Model_Pattern.get_field descriptor m_patt of
Walther@60658
   407
          NONE => Err ("ERROR I_Model.check_single: unknown descriptor \"" ^
Walther@60675
   408
            UnparseC.term ctxt descriptor ^ "\"")
Walther@60658
   409
        | SOME m_field' => 
Walther@60658
   410
          if m_field <> m_field' then
Walther@60675
   411
            Err ("ERROR I_Model.check_single: \"" ^ UnparseC.term ctxt t ^ "\"" ^
Walther@60658
   412
             "\" not for field \"" ^ m_field ^ "\"")
Walther@60658
   413
          else
Walther@60658
   414
            case O_Model.contains ctxt m_field o_model t of
Walther@60658
   415
              ("", ori', all) => 
Walther@60658
   416
                (case is_notyet_input ctxt i_model all ori' m_patt of
Walther@60658
   417
                   ("", itm) => Add itm
Walther@60658
   418
                 | (msg, _) => Err ("ERROR I_Model.check_single: is_notyet_input: " ^ msg))
Walther@60658
   419
            | (msg, _, _) => Err ("ERROR I_Model.check_single: contains: " ^ msg)
Walther@60658
   420
    end
Walther@60658
   421
     
walther@59958
   422
walther@59958
   423
(** add input **)
walther@59958
   424
Walther@60586
   425
fun overwrite_ppc thy itm model =
walther@59958
   426
  let 
walther@59958
   427
    fun repl _ (_, _, _, _, itm_) [] =
walther@60360
   428
        raise ERROR ("overwrite_ppc: " ^ feedback_to_string (Proof_Context.init_global thy) itm_
walther@60360
   429
          ^ " not found")
Walther@60586
   430
      | repl model' itm (p :: model) =
walther@59958
   431
	      if (#1 itm) = (#1 p)
Walther@60586
   432
	      then model' @ [itm] @ model
Walther@60586
   433
	      else repl (model' @ [p]) itm model
Walther@60586
   434
  in repl [] itm model end
walther@59958
   435
Walther@60740
   436
(*find_first item with #1 equal to id*)
walther@59958
   437
fun seek_ppc _ [] = NONE
Walther@60586
   438
  | seek_ppc id (p :: model) = if id = #1 (p: single) then SOME p else seek_ppc id model
walther@59958
   439
Walther@60763
   440
(* 10.3.00: insert the parsed itm into model;
walther@59958
   441
   ev. filter_out  untouched (in FE: (0,...)) item related to insert-item *)
Walther@60586
   442
fun add_single thy itm model =
walther@59958
   443
  let 
Walther@60477
   444
    fun eq_untouched d (0, _, _, _, itm_) = (d = descriptor itm_)
walther@59958
   445
      | eq_untouched _ _ = false
Walther@60586
   446
    val model' = case seek_ppc (#1 itm) model of
Walther@60586
   447
      SOME _ => overwrite_ppc thy itm model (*itm updated in is_notyet_input WN.11.03*)
Walther@60586
   448
    | NONE => (model @ [itm])
Walther@60586
   449
  in filter_out (eq_untouched ((descriptor o #5) itm)) model' end
walther@59956
   450
Walther@60740
   451
Walther@60747
   452
(** complete I_Model.T **)
Walther@60747
   453
Walther@60756
   454
fun s_are_complete _ _ ([], _) _ = false
Walther@60756
   455
  | s_are_complete _ _ (_, []) _ = false
Walther@60756
   456
  | s_are_complete ctxt o_model (pbl_imod, met_imod) (pbl_id, met_id) =
Walther@60756
   457
  let
Walther@60756
   458
    val pbl_max_vnts = Model_Def.max_variants o_model pbl_imod
Walther@60756
   459
    val met_max_vnts = Model_Def.max_variants o_model met_imod
Walther@60756
   460
    val max_vnts = inter op= pbl_max_vnts met_max_vnts
Walther@60756
   461
    val max_vnt = if max_vnts = []
Walther@60756
   462
      then raise ERROR "I_Model.s_are_complete: request user to review met_imod"
Walther@60756
   463
      else hd max_vnts
Walther@60747
   464
Walther@60756
   465
    val (pbl_imod', met_imod') = (
Walther@60756
   466
      filter (fn (_, vnts, _, _, _) => member_swap op = max_vnt vnts) pbl_imod,
Walther@60756
   467
      filter (fn (_, vnts, _, _, _) => member_swap op = max_vnt vnts) met_imod)
Walther@60747
   468
Walther@60756
   469
    val (pbl_check, _) = Pre_Conds.check_internal ctxt pbl_imod' (Pos.Pbl, pbl_id)
Walther@60756
   470
    val (met_check, _) = Pre_Conds.check_internal ctxt met_imod' (Pos.Met, met_id)
Walther@60756
   471
  in
Walther@60756
   472
    pbl_check andalso met_check
Walther@60756
   473
  end
walther@59988
   474
walther@59988
   475
fun is_error (Cor _) = false
walther@59988
   476
  | is_error (Sup _) = false
walther@59988
   477
  | is_error (Inc _) = false
walther@59988
   478
  | is_error (Mis _) = false
walther@59988
   479
  | is_error _ = true
walther@59988
   480
Walther@60763
   481
(*OLD*)
Walther@60740
   482
(*create output-string for itm*)
Walther@60674
   483
fun to_p_model thy (Cor ((d, ts), _)) = UnparseC.term_in_thy thy (Input_Descript.join (d, ts))
walther@59988
   484
  | to_p_model _ (Syn c) = c
walther@59988
   485
  | to_p_model _ (Typ c) = c
Walther@60673
   486
  | to_p_model thy (Inc ((d, ts), _)) = UnparseC.term_in_thy thy (Input_Descript.join (d, ts))
Walther@60673
   487
  | to_p_model thy (Sup (d, ts)) = UnparseC.term_in_thy thy (Input_Descript.join (d, ts))
Walther@60674
   488
  | to_p_model thy (Mis (d, pid)) = UnparseC.term_in_thy thy d ^ " " ^ UnparseC.term_in_thy thy pid
walther@59988
   489
  | to_p_model _ _ = raise ERROR "to_p_model: uncovered definition"
Walther@60763
   490
(*---*)
Walther@60763
   491
(*see feedback_NEW_to_string, descr_vals_to_string*)
Walther@60763
   492
(*NEW*)
walther@59988
   493
Walther@60763
   494
(*OLD* )
Walther@60763
   495
fun handle_atom [t] = [t]
Walther@60763
   496
  | handle_atom ts = [TermC.list2isalist (type_of (hd ts)) ts]
Walther@60763
   497
( *---*)
Walther@60763
   498
(*
Walther@60763
   499
  atoms like Solutions (L::bool list);
Walther@60763
   500
  precondition: arguments are of type list; according to type of descriptor
Walther@60763
   501
*)
Walther@60763
   502
fun handle_atom [t] = [t]
Walther@60763
   503
  | handle_atom ts = ts |> map TermC.isalist2list |> flat
Walther@60763
   504
(*NEW*)
Walther@60763
   505
Walther@60763
   506
(*OLD*)
Walther@60763
   507
(*---*)
Walther@60763
   508
fun unpack_values (descr, [t]) =
Walther@60763
   509
    if Pre_Conds.is_list_descr descr
Walther@60763
   510
    then if Pre_Conds.is_NObrack_list descr [(*TODO drop this argument*)]
Walther@60763
   511
      then [t]
Walther@60763
   512
      else t |> TermC.isalist2list
Walther@60763
   513
    else [t]
Walther@60763
   514
  | unpack_values (_, ts) = ts |> map TermC.isalist2list |> flat
Walther@60763
   515
(*NEW*)
Walther@60763
   516
Walther@60763
   517
(*OLD*)
Walther@60477
   518
fun eq_dsc ((_, _, _, _, itm_), (_, _, _, _, iitm_)) = (descriptor itm_ = descriptor iitm_)
walther@59988
   519
(* insert_ppc = add for appl_add', input_icalhd 11.03,
walther@59988
   520
   handles superfluous items carelessly                       *)
Walther@60763
   521
(*---*)
walther@59988
   522
fun add itm itms = if member eq_dsc itms itm then itms else itms @ [itm] (* @ new itm *)
Walther@60763
   523
fun fill_from_o o_model (i, vnts, bool, _, (feedb, pos)) = 
Walther@60763
   524
  let
Walther@60763
   525
    val (m_field, all_value) =
Walther@60763
   526
      case find_first (fn (_, _, _, descr', _) => Pre_Conds.descriptor_exists descr' feedb) o_model of
Walther@60763
   527
        SOME (_, _, m_field, _, ts) =>  (m_field, ts)
Walther@60763
   528
      | NONE => raise ERROR "I_Model.fill_from_o does NOT find a descriptor in O_Model"
Walther@60763
   529
    val descr = Pre_Conds.get_dscr'' feedb (*i_single has been filtered appropriately*)
Walther@60763
   530
  in
Walther@60763
   531
    if Pre_Conds.is_list_descr descr
Walther@60763
   532
    then
Walther@60763
   533
      let
Walther@60763
   534
        val all_value' = unpack_values (descr, all_value)
Walther@60763
   535
        val already_input = feedb |> values_TEST' |> map TermC.isalist2list |> flat
Walther@60763
   536
        val miss = subtract op= already_input all_value'
Walther@60763
   537
        val present = already_input @ [hd miss]
Walther@60763
   538
      in
Walther@60763
   539
        if length all_value' = length present
Walther@60763
   540
        then SOME (i, vnts, bool, m_field, (Cor_TEST (descr, present), pos))
Walther@60763
   541
        else SOME (i, vnts, bool, m_field, (Inc_TEST (descr, present), pos))
Walther@60763
   542
      end
Walther@60763
   543
    else SOME (i, vnts, bool, m_field, (Cor_TEST (descr, all_value), pos))
Walther@60763
   544
  end
Walther@60763
   545
(*NEW*)
walther@59988
   546
Walther@60760
   547
(*
Walther@60760
   548
  in case there is an item in i2_model(= met) with Sup_TEST, 
Walther@60760
   549
  find_first an appropriate (variant, descriptor) item in i1_model(= pbl) and add it instead Sup_TEST,
Walther@60760
   550
  otherwise keep the items of i2_model.
Walther@60760
   551
*)
Walther@60760
   552
fun add_other max_vnt i1_model (i2, _, bool2, m_field2, (Sup_TEST (descr2, ts2), pos2)) =
Walther@60760
   553
    (case find_first (fn (_, vnts1, _, _, (feedb1, _)) => case Pre_Conds.get_dscr' feedb1 of
Walther@60760
   554
          NONE => false
Walther@60760
   555
        | SOME descr1 => descr1 = descr2 andalso Model_Def.member_vnt vnts1 max_vnt) i1_model of
Walther@60760
   556
      NONE =>
Walther@60760
   557
        (i2, [max_vnt], bool2, m_field2, (Sup_TEST (descr2, ts2), pos2)) (*the present in i2_model*)
Walther@60760
   558
    | SOME i1_single => i1_single)                      (*shift the item from i1_model to i2_model*)
Walther@60760
   559
  | add_other _ _ i2_single = i2_single                     (*keep all the other items in i2_model*)
Walther@60760
   560
Walther@60760
   561
fun fill_method o_model (pbl_imod, met_imod) met_patt =
Walther@60760
   562
  let
Walther@60760
   563
    val pbl_max_vnts = Model_Def.max_variants o_model pbl_imod
Walther@60760
   564
    (*probably pbl/met_imod = [], so take met_patt; if empty return Sup*)
Walther@60760
   565
    val i_from_met = map (fn (_, (descr, _)) => (*order from met_patt*)
Walther@60760
   566
      Pre_Conds.get_descr_vnt descr pbl_max_vnts met_imod) met_patt (*\<longrightarrow> right order for args*)
Walther@60760
   567
Walther@60760
   568
    val met_max_vnts = Model_Def.max_variants o_model i_from_met;
Walther@60760
   569
    val max_vnt = hd (inter op= pbl_max_vnts met_max_vnts);
Walther@60760
   570
    (*add items from pbl_imod (without overwriting existing items in met_imod)*)
Walther@60760
   571
  in
Walther@60760
   572
    map (add_other max_vnt pbl_imod) i_from_met
Walther@60760
   573
  end 
Walther@60760
   574
Walther@60755
   575
fun msg vnts feedb = "get_descr_vnt' returns NONE: i.e. it does not find an item of o_model with\n" ^
Walther@60755
   576
  "variants " ^ ints2str' vnts ^ " and descriptor " ^
Walther@60755
   577
  (feedb |> Pre_Conds.get_dscr' |> the |> UnparseC.term (ContextC.for_ERROR ()))
Walther@60755
   578
fun transfer_terms (i, vnts, m_field, descr, ts) =
Walther@60755
   579
  (i, vnts, true, m_field, (Cor_TEST (descr, ts), Position.none))
Walther@60757
   580
fun s_make_complete ctxt o_model (pbl_imod, met_imod) (pbl_id, met_id) =
Walther@60751
   581
  let
Walther@60757
   582
    val {model = pbl_patt, ...} = Problem.from_store ctxt pbl_id;
Walther@60757
   583
    val {model = met_patt, ...} = MethodC.from_store ctxt met_id;
Walther@60752
   584
    val pbl_max_vnts = Model_Def.max_variants o_model pbl_imod;
Walther@60751
   585
    val i_from_pbl = map (fn (_, (descr, _)) =>
Walther@60751
   586
      Pre_Conds.get_descr_vnt descr pbl_max_vnts pbl_imod) pbl_patt
Walther@60751
   587
    val pbl_from_o_model = map ((fn i_single as (_, _, _, _, (feedb, _)) =>
Walther@60751
   588
      if is_empty_single_TEST i_single
Walther@60751
   589
      then
Walther@60751
   590
        case Pre_Conds.get_descr_vnt' feedb pbl_max_vnts o_model of
Walther@60751
   591
            [] => raise ERROR (msg pbl_max_vnts feedb)
Walther@60751
   592
          | o_singles => map transfer_terms o_singles
Walther@60751
   593
      else [i_single (*fetched before from pbl_imod*)])) i_from_pbl |> flat
Walther@60751
   594
Walther@60751
   595
    val i_from_met = map (fn (_, (descr, _)) =>
Walther@60752
   596
      Pre_Conds.get_descr_vnt descr pbl_max_vnts met_imod) met_patt
Walther@60752
   597
    val met_max_vnts = Model_Def.max_variants o_model i_from_met;
Walther@60752
   598
    val max_vnt = hd met_max_vnts (*need only one for solve-phase*)
Walther@60751
   599
Walther@60751
   600
    val met_from_pbl = map ((fn i_single as (_, _, _, _, (feedb, _)) =>
Walther@60751
   601
      if is_empty_single_TEST i_single
Walther@60751
   602
      then
Walther@60752
   603
        case Pre_Conds.get_descr_vnt' feedb [max_vnt] o_model of
Walther@60752
   604
            [] => raise ERROR (msg [max_vnt] feedb)
Walther@60751
   605
          | o_singles => map transfer_terms o_singles
Walther@60751
   606
      else [i_single (*fetched before from met_imod*)])) i_from_met |> flat
Walther@60751
   607
  in
Walther@60752
   608
    (filter (fn (_, vnts', _, _, _) => member op = vnts' max_vnt) pbl_from_o_model,
Walther@60752
   609
      met_from_pbl)
Walther@60751
   610
  end
Walther@60751
   611
walther@60126
   612
(**)end(**);