src/Tools/isac/Specify/i-model.sml
author wneuper <Walther.Neuper@jku.at>
Fri, 01 Dec 2023 06:08:22 +0100
changeset 60771 1b072aab8f4e
parent 60770 365758b39d90
child 60772 ac0317936138
permissions -rw-r--r--
PIDE turn 13: rename ALL(?) code already handling Position.T from *_TEST to *_POS

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