src/Tools/isac/Specify/i-model.sml
author wneuper <Walther.Neuper@jku.at>
Mon, 11 Dec 2023 17:26:30 +0100
changeset 60782 e797d1bdfe37
parent 60780 91b105cf194a
permissions -rw-r--r--
eliminate the intermediate *_POS
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@60467
    12
  val empty: T
Walther@60747
    13
walther@59961
    14
  type single
Walther@60467
    15
  val empty_single: single
Walther@60782
    16
  val is_empty_single: single -> bool
Walther@60747
    17
walther@60018
    18
  type variant
walther@59960
    19
  type variants
walther@59961
    20
  type m_field
walther@59961
    21
  type descriptor
Walther@60762
    22
  type values
Walther@60747
    23
walther@59948
    24
  datatype feedback = datatype Model_Def.i_model_feedback
Walther@60782
    25
  val feedback_empty: Model_Def.i_model_feedback
Walther@60747
    26
Walther@60705
    27
  type env
walther@59998
    28
  type message
walther@59938
    29
Walther@60782
    30
  val single_to_string: Proof.context -> single -> string
Walther@60782
    31
  val to_string: Proof.context -> T -> string
Walther@60763
    32
Walther@60782
    33
  datatype add_single = Add of single | Err of string
Walther@60782
    34
  val init: Proof.context -> O_Model.T -> Model_Pattern.T -> T
Walther@60782
    35
  val check_single: Proof.context -> m_field -> O_Model.T -> T -> Model_Pattern.T ->
Walther@60477
    36
    TermC.as_string -> add_single
Walther@60782
    37
  val add_single: theory -> single -> T -> T
walther@59956
    38
Walther@60782
    39
  val make_tactic: m_field -> TermC.as_string * T -> Tactic.T
Walther@60772
    40
Walther@60782
    41
  val descriptor: feedback -> descriptor
Walther@60782
    42
  val get_values: T -> values list
Walther@60782
    43
  val feedb_values: feedback -> values
Walther@60782
    44
  val order_by_patt:  Model_Pattern.T -> T ->T
Walther@60782
    45
  val descr_pairs_to_string: Proof.context -> (Model_Pattern.single * single) list -> string
Walther@60782
    46
  val variables: Model_Pattern.T -> Model_Def.i_model -> term list
Walther@60782
    47
  val is_notyet_input: Proof.context -> T -> O_Model.values -> O_Model.single ->
Walther@60782
    48
    Model_Pattern.T -> message * single
walther@59956
    49
Walther@60782
    50
  val fill_from_o: O_Model.T -> single -> single option
Walther@60763
    51
Walther@60782
    52
  val add_other: variant -> T -> single -> single
Walther@60782
    53
  val fill_method: O_Model.T -> T * T-> Model_Pattern.T -> T
Walther@60782
    54
  val s_make_complete: Proof.context ->  O_Model.T -> T * T -> Problem.id * MethodC.id ->
Walther@60782
    55
    T * T
Walther@60782
    56
  val s_are_complete: Proof.context -> O_Model.T -> T * T -> Problem.id * MethodC.id -> bool
Walther@60747
    57
Walther@60782
    58
  val is_error: feedback -> bool
Walther@60782
    59
  val to_p_model: theory -> feedback -> string
Walther@60767
    60
Walther@60756
    61
(*/----- from isac_test for Minisubpbl*)
Walther@60782
    62
  val msg: variants -> feedback -> string
Walther@60782
    63
  val transfer_terms: O_Model.single -> single
Walther@60751
    64
Walther@60782
    65
  val feedback_to_string: Proof.context -> feedback -> string
Walther@60763
    66
  val descr_vals_to_string: Proof.context -> descriptor * values -> string
Walther@60782
    67
  val feedb_args_to_string: Proof.context -> feedback -> string
Walther@60741
    68
Walther@60782
    69
  val single_from_o: feedback -> descriptor -> Model_Def.values -> O_Model.single -> single
Walther@60782
    70
  val seek_ppc: int -> single list -> single option
Walther@60782
    71
  val overwrite_ppc: theory -> single -> T -> T
Walther@60756
    72
(*\----- from isac_test for Minisubpbl*)
Walther@60723
    73
Walther@60694
    74
\<^isac_test>\<open>
Walther@60772
    75
  (*copy "from isac_test for Minisubpbl" here*)
Walther@60723
    76
Walther@60694
    77
\<close>
Walther@60694
    78
walther@59938
    79
end
walther@59938
    80
walther@59942
    81
(**)
Walther@60694
    82
structure I_Model(**) : INTERACTION_MODEL(**) =
walther@59938
    83
struct
walther@59942
    84
(**)
walther@59955
    85
walther@59958
    86
(** data types **)
walther@59958
    87
walther@60018
    88
type variant =  Model_Def.variant;
walther@59940
    89
type variants =  Model_Def.variants;
walther@59952
    90
type m_field = Model_Def.m_field;
walther@59952
    91
type descriptor = Model_Def.descriptor;
Walther@60766
    92
type values = Model_Def.values
walther@59938
    93
walther@59940
    94
type T = Model_Def.i_model_single list;
walther@59940
    95
datatype feedback = datatype Model_Def.i_model_feedback;
Walther@60782
    96
val feedback_empty = Model_Def.feedback_empty
walther@59940
    97
type single = Model_Def.i_model_single;
Walther@60467
    98
val empty_single = Model_Def.i_model_empty;
Walther@60782
    99
fun is_empty_single (0, [], false, "i_model_empty", _) = true
Walther@60782
   100
  | is_empty_single _ = false
Walther@60747
   101
Walther@60467
   102
val empty = []: T;
Walther@60733
   103
Walther@60740
   104
type env = Env.T
Walther@60740
   105
walther@59998
   106
type message = string;
Walther@60782
   107
fun feedback_to_string ctxt (Cor (d, ts)) = 
Walther@60782
   108
    "Cor " ^ UnparseC.term ctxt (Input_Descript.join (d, ts)) ^ " , pen2str"
Walther@60782
   109
  | feedback_to_string _ (Syn c) =
Walther@60782
   110
    "Syn " ^ c
Walther@60782
   111
  | feedback_to_string ctxt (Inc (d, [])) = 
Walther@60782
   112
    "Inc " ^ UnparseC.term ctxt (Input_Descript.join (d, [])) ^ " " ^ 
Walther@60733
   113
      Model_Pattern.empty_for d
Walther@60782
   114
  | feedback_to_string ctxt (Inc (d, ts)) =
Walther@60782
   115
    "Inc " ^ UnparseC.term ctxt (Input_Descript.join (d, ts)) ^ " , pen2str"
Walther@60782
   116
  | feedback_to_string ctxt (Sup (d, ts)) = 
Walther@60782
   117
    "Sup " ^ UnparseC.term ctxt (Input_Descript.join (d, ts))
walther@59942
   118
Walther@60766
   119
fun descr_vals_to_string ctxt (descr, values) =
Walther@60769
   120
  UnparseC.term ctxt (descr $ Model_Def.values_to_present values)
Walther@60763
   121
Walther@60782
   122
(*prepare for presentation to user; thus Syn does NOT raise an exn*)
Walther@60782
   123
fun feedb_args_to_string ctxt (Cor (descr, values)) =
Walther@60769
   124
    UnparseC.term ctxt (descr $ Model_Def.values_to_present values)
Walther@60782
   125
  | feedb_args_to_string _ (Syn str) = str
Walther@60782
   126
  | feedb_args_to_string ctxt (Inc (descr, [])) =
Walther@60766
   127
    UnparseC.term ctxt descr ^ Model_Pattern.empty_for descr
Walther@60782
   128
  | feedb_args_to_string ctxt (Inc (descr, values)) =
Walther@60769
   129
    UnparseC.term ctxt (descr $ Model_Def.values_to_present values)
Walther@60782
   130
  | feedb_args_to_string ctxt (Sup (descr, values)) =
Walther@60769
   131
    UnparseC.term ctxt (descr $ Model_Def.values_to_present values)
Walther@60763
   132
Walther@60782
   133
fun single_to_string ctxt (i, is, b, s, (itm_, _(*Position.T*))) = 
Walther@60694
   134
  "(" ^ string_of_int i ^ ", " ^ ints2str' is ^ ", " ^ bool2str b ^ " ," ^
Walther@60782
   135
  s ^ ", (" ^ feedback_to_string ctxt itm_ ^ ", Position.T))";
Walther@60782
   136
fun to_string ctxt itms = strs2str' (map (linefeed o (single_to_string ctxt)) itms);
walther@59942
   137
walther@59958
   138
Walther@60694
   139
(** make a Tactic.T **)
Walther@60694
   140
Walther@60477
   141
fun make_tactic m_field (term_as_string, i_model) =
walther@59992
   142
  case m_field of
walther@59992
   143
    "#Given" => Tactic.Add_Given' (term_as_string, i_model)
walther@59992
   144
  | "#Find" => Tactic.Add_Find' (term_as_string, i_model)
walther@59992
   145
  | "#Relate" => Tactic.Add_Relation'(term_as_string, i_model)
walther@59992
   146
  | str => raise ERROR ("specify_additem Frm, Pbl: uncovered case with " ^ str);
walther@59992
   147
walther@59992
   148
walther@59958
   149
(** initialise a model **)
walther@59958
   150
Walther@60702
   151
(*
Walther@60770
   152
  NEW design decision:
Walther@60705
   153
* Now the Model in Specification is intialised such that the placement of items can be
Walther@60702
   154
  maximally stable during interactive input to the Specification.
Walther@60702
   155
* Template.show provides the initial output to the user and thus determines what will be parsed
Walther@60702
   156
  by Outer_Syntax later during interaction.
Walther@60705
   157
* The relation between O_Model.T and I_Model.T becomes much simpler.
Walther@60702
   158
*)
Walther@60702
   159
(**)
Walther@60770
   160
fun patt_to_item ctxt o_model (_, (descriptor, _)) =
Walther@60702
   161
  case find_first (fn (_, _, _, desc, _) => desc = descriptor) o_model of
Walther@60770
   162
    NONE => raise ERROR ("I_Model.patt_to_item NONE for " ^ UnparseC.term ctxt descriptor)
Walther@60702
   163
  | SOME (_, variants, m_field, descr, _) => (variants, false, m_field,
Walther@60782
   164
    (Inc (descr, []), Position.none))
Walther@60782
   165
fun init ctxt o_model model_patt =
Walther@60690
   166
  let
Walther@60770
   167
    val pre_items = map (patt_to_item ctxt o_model) model_patt
Walther@60702
   168
  in
Walther@60702
   169
    O_Model.add_enumerate pre_items |> map (fn (a, (b, c, d, e)) => (a, b, c, d, e))
Walther@60702
   170
  end
walther@59943
   171
walther@59943
   172
Walther@60664
   173
val unique = Syntax.read_term\<^context> "UnIqE_tErM";
Walther@60770
   174
(*DANGEROUS: do NOT use "UnIqE_tErM" *)
Walther@60782
   175
fun descriptor (Cor (d ,_)) = d
Walther@60782
   176
  | descriptor (Syn _) = ((*tracing ("*** descriptor: Syn ("^c^")");*) unique)
Walther@60782
   177
  | descriptor (Inc (d, _)) = d
Walther@60782
   178
  | descriptor (Sup (d, _)) = d
Walther@60782
   179
fun feedb_values (Cor (_, ts)) = ts
Walther@60782
   180
  | feedb_values (Syn _) = raise ERROR "feedb_values NOT for Syn"
Walther@60782
   181
  | feedb_values (Inc (_, ts)) = ts
Walther@60782
   182
  | feedb_values (Sup (_, ts)) = ts
Walther@60777
   183
Walther@60777
   184
(*assumption: i_model has filtered max_vnt*)
Walther@60777
   185
local
Walther@60777
   186
 fun order_by_pa i_model (_, (descr, _ )) =
Walther@60782
   187
   case find_first (fn (_, _, _, _, (feedb, _)) => descr = descriptor feedb) i_model of
Walther@60777
   188
     SOME i_single => [i_single]
Walther@60777
   189
   | NONE => []
Walther@60777
   190
in
Walther@60777
   191
  fun order_by_patt model_patt i_model = model_patt |> map (order_by_pa i_model) |> flat
Walther@60777
   192
end
Walther@60782
   193
fun feedb_vals (Cor (_, ts)) = [ts]
Walther@60782
   194
  | feedb_vals (Syn _) = []
Walther@60782
   195
  | feedb_vals (Inc (_, ts)) = [ts]
Walther@60782
   196
  | feedb_vals (Sup (_, ts)) = [ts]
Walther@60777
   197
fun get_values i_model =
Walther@60777
   198
  map (fn (_, _, _, _, (feedb, _)) => feedb_vals feedb) i_model
Walther@60777
   199
  |> flat
Walther@60762
   200
Walther@60710
   201
fun descr_pairs_to_string ctxt equal_descr_pairs =
Walther@60782
   202
(map (fn (a, b) => pair (Model_Pattern.single_to_string ctxt a) (single_to_string ctxt b)
Walther@60710
   203
    |> pair2str) equal_descr_pairs)
Walther@60710
   204
  |> strs2str'
Walther@60710
   205
Walther@60741
   206
fun variables model_patt i_model =
Walther@60782
   207
  Pre_Conds.environment model_patt i_model
Walther@60733
   208
  |> map snd
walther@59943
   209
Walther@60740
   210
(*update the itm_ already input, all..from ori*)
Walther@60782
   211
fun single_from_o (feedb:feedback) _ all (id, vt, fd, d, ts) = 
walther@59956
   212
  let 
Walther@60777
   213
    val ts' = union op = (feedb_values feedb) ts;
walther@59956
   214
    val complete = if eq_set op = (ts', all) then true else false
walther@59956
   215
  in
Walther@60772
   216
    case feedb of
Walther@60782
   217
      Cor _ => if fd = "#undef"
Walther@60782
   218
      then (id, vt, complete, fd, (Sup (d, ts'), Position.none))
Walther@60782
   219
	    else (id, vt, complete, fd, (Cor (d, ts'), Position.none))
Walther@60782
   220
    | Inc _ => if complete
Walther@60782
   221
  	  then (id, vt, true,  fd, (Cor (d, ts'), Position.none))
Walther@60782
   222
  	  else (id, vt, false, fd, (Inc (d, ts'), Position.none))
Walther@60782
   223
    | Sup (d, ts') =>
Walther@60782
   224
  	  (id, vt, complete, fd, (Sup (d, ts'), Position.none))
Walther@60782
   225
    | i => raise ERROR ("single_from_o: uncovered case of " ^ feedback_to_string (ContextC.for_ERROR ()) i)
walther@59956
   226
  end
walther@59956
   227
Walther@60740
   228
Walther@60740
   229
(** find next step **)
Walther@60740
   230
Walther@60772
   231
(*old code kept for test/*)
walther@59956
   232
fun is_notyet_input ctxt itms all (i, v, f, d, ts) pbt =
Walther@60772
   233
  case find_first (fn (_, (d', _)) => d = d') pbt of
walther@59956
   234
    SOME (_, (_, pid)) =>
Walther@60772
   235
      (case find_first (fn (_, _, _, f', (feedb, _)) =>
Walther@60782
   236
          f = f' andalso d = (descriptor feedb)) itms of
Walther@60772
   237
        SOME (_, _, _, _, (feedb, _)) =>
Walther@60773
   238
          let
Walther@60777
   239
            val ts' = inter op = (feedb_values feedb) ts
Walther@60772
   240
          in
walther@59956
   241
            if subset op = (ts, ts') 
Walther@60782
   242
            then (((strs2str' o map (UnparseC.term ctxt)) ts') ^ " already input", empty_single)
Walther@60778
   243
	          else ("", single_from_o feedb pid all (i, v, f, d, subtract op = ts' ts))
Walther@60772
   244
	        end
Walther@60782
   245
	    | NONE => ("", single_from_o (Inc (TermC.empty, [])) pid all (i, v, f, d, ts)))
Walther@60782
   246
  | NONE => ("", single_from_o (Sup (d, ts)) TermC.empty all (i, v, f, d, ts))
walther@59956
   247
Walther@60477
   248
datatype add_single =
Walther@60782
   249
	Add of single   (* return-value of check_single *)
walther@59998
   250
| Err of string   (* error-message                *)
walther@59956
   251
walther@59956
   252
(*
walther@59956
   253
  Create feedback for input of TermC.as_string to m_field;
walther@59956
   254
  check w.r.t. O_Model.T and Model_Pattern.T.
walther@59998
   255
  In case of O_Model.T = [] (i.e. no data for user-guidance in Formalise.T)
walther@59958
   256
  check_single is extremely permissive.
walther@59956
   257
*)
Walther@60658
   258
(*will come directly from PIDE -----------------vvvvvvvvvvv
Walther@60658
   259
  in case t comes from Step.specify_do_next -----------vvv = Position.none*)
Walther@60658
   260
fun check_single ctxt m_field [] i_model m_patt (ct(*, pos*)) =
Walther@60658
   261
    let
Walther@60658
   262
      val i = 1 + (if i_model = [] then 0 else map #1 i_model |> maxl)
Walther@60661
   263
    (*/------------ replace by ParseC.term_position -----------\*)
Walther@60661
   264
      val t = Syntax.read_term ctxt ct
Walther@60658
   265
        handle ERROR msg => error (msg (*^ Position.here pos*))
Walther@60661
   266
    (*\------------ replace by ParseC.term_position -----------/*)
Walther@60658
   267
        (*NONE => Add (i, [], false, m_field, Syn ct)*)
Walther@60658
   268
      val (d, ts) = Input_Descript.split t
Walther@60772
   269
    in
Walther@60772
   270
      (*if d = TermC.empty then .. *)
Walther@60772
   271
        (case find_first (fn (_, (d', _)) => d = d') m_patt of
Walther@60782
   272
          NONE => Add (i, [], true, m_field, (Sup (d,ts), Position.none))
Walther@60782
   273
        | SOME (f, (_, _)) =>
Walther@60782
   274
          case find_first (fn (i, _, _, _, (feedb, _)) => d = (descriptor feedb) andalso i <> 0) i_model of
Walther@60772
   275
            NONE =>
Walther@60782
   276
              Add (i, [], true, f, (Cor (d, ts), Position.none))
Walther@60778
   277
          | SOME (i', _, _, _, (itm_, _)) => 
Walther@60772
   278
            if Input_Descript.for_list d then 
Walther@60772
   279
              let
Walther@60778
   280
                val in_itm = feedb_values itm_
Walther@60772
   281
                val ts' = union op = ts in_itm
Walther@60772
   282
                val i'' = if in_itm = [] then i else i'
Walther@60782
   283
              in Add (i'', [], true, f, (Cor (d, ts'), Position.none)) end
Walther@60782
   284
            else Add (i', [], true, f, (Cor (d, ts), Position.none)))
Walther@60658
   285
    end
Walther@60658
   286
    (*will come directly from PIDE ----------------------vvvvvvvvvvv*)
Walther@60658
   287
  | check_single ctxt m_field o_model i_model m_patt (str(*, pos*)) =
Walther@60658
   288
    let
Walther@60659
   289
      val (t as (descriptor $ _)) = Syntax.read_term ctxt str
Walther@60740
   290
        handle ERROR msg => error (msg (*TODO: ^ Position.here pp*))
Walther@60659
   291
        (*old code: NONE => Err ("ERROR I_Model.check_single: syntax error in \"" ^ str ^ "\"")*)
Walther@60658
   292
    in 
Walther@60658
   293
        case Model_Pattern.get_field descriptor m_patt of
Walther@60658
   294
          NONE => Err ("ERROR I_Model.check_single: unknown descriptor \"" ^
Walther@60675
   295
            UnparseC.term ctxt descriptor ^ "\"")
Walther@60658
   296
        | SOME m_field' => 
Walther@60658
   297
          if m_field <> m_field' then
Walther@60675
   298
            Err ("ERROR I_Model.check_single: \"" ^ UnparseC.term ctxt t ^ "\"" ^
Walther@60658
   299
             "\" not for field \"" ^ m_field ^ "\"")
Walther@60658
   300
          else
Walther@60658
   301
            case O_Model.contains ctxt m_field o_model t of
Walther@60658
   302
              ("", ori', all) => 
Walther@60778
   303
                (case is_notyet_input ctxt i_model all ori' m_patt of
Walther@60658
   304
                   ("", itm) => Add itm
Walther@60658
   305
                 | (msg, _) => Err ("ERROR I_Model.check_single: is_notyet_input: " ^ msg))
Walther@60658
   306
            | (msg, _, _) => Err ("ERROR I_Model.check_single: contains: " ^ msg)
Walther@60658
   307
    end
Walther@60658
   308
     
walther@59958
   309
walther@59958
   310
(** add input **)
walther@59958
   311
Walther@60586
   312
fun overwrite_ppc thy itm model =
walther@59958
   313
  let 
Walther@60773
   314
    fun repl _ (_, _, _, _, (itm_, _)) [] =
Walther@60782
   315
        raise ERROR ("overwrite_ppc: " ^ feedback_to_string (Proof_Context.init_global thy) itm_
walther@60360
   316
          ^ " not found")
Walther@60586
   317
      | repl model' itm (p :: model) =
walther@59958
   318
	      if (#1 itm) = (#1 p)
Walther@60586
   319
	      then model' @ [itm] @ model
Walther@60586
   320
	      else repl (model' @ [p]) itm model
Walther@60586
   321
  in repl [] itm model end
walther@59958
   322
Walther@60740
   323
(*find_first item with #1 equal to id*)
walther@59958
   324
fun seek_ppc _ [] = NONE
Walther@60782
   325
  | seek_ppc id (p :: model) = if id = #1 (p: single) then SOME p else seek_ppc id model
walther@59958
   326
Walther@60763
   327
(* 10.3.00: insert the parsed itm into model;
walther@59958
   328
   ev. filter_out  untouched (in FE: (0,...)) item related to insert-item *)
Walther@60586
   329
fun add_single thy itm model =
walther@59958
   330
  let 
Walther@60782
   331
    fun eq_untouched d (0, _, _, _, (itm_, _)) = (d = descriptor itm_)
walther@59958
   332
      | eq_untouched _ _ = false
Walther@60586
   333
    val model' = case seek_ppc (#1 itm) model of
Walther@60586
   334
      SOME _ => overwrite_ppc thy itm model (*itm updated in is_notyet_input WN.11.03*)
Walther@60586
   335
    | NONE => (model @ [itm])
Walther@60782
   336
  in filter_out (eq_untouched ((descriptor o #1 o #5) itm)) model' end
walther@59956
   337
Walther@60740
   338
Walther@60747
   339
(** complete I_Model.T **)
Walther@60747
   340
Walther@60756
   341
fun s_are_complete _ _ ([], _) _ = false
Walther@60756
   342
  | s_are_complete _ _ (_, []) _ = false
Walther@60756
   343
  | s_are_complete ctxt o_model (pbl_imod, met_imod) (pbl_id, met_id) =
Walther@60756
   344
  let
Walther@60756
   345
    val pbl_max_vnts = Model_Def.max_variants o_model pbl_imod
Walther@60756
   346
    val met_max_vnts = Model_Def.max_variants o_model met_imod
Walther@60756
   347
    val max_vnts = inter op= pbl_max_vnts met_max_vnts
Walther@60756
   348
    val max_vnt = if max_vnts = []
Walther@60756
   349
      then raise ERROR "I_Model.s_are_complete: request user to review met_imod"
Walther@60756
   350
      else hd max_vnts
Walther@60747
   351
Walther@60756
   352
    val (pbl_imod', met_imod') = (
Walther@60756
   353
      filter (fn (_, vnts, _, _, _) => member_swap op = max_vnt vnts) pbl_imod,
Walther@60756
   354
      filter (fn (_, vnts, _, _, _) => member_swap op = max_vnt vnts) met_imod)
Walther@60747
   355
Walther@60756
   356
    val (pbl_check, _) = Pre_Conds.check_internal ctxt pbl_imod' (Pos.Pbl, pbl_id)
Walther@60756
   357
    val (met_check, _) = Pre_Conds.check_internal ctxt met_imod' (Pos.Met, met_id)
Walther@60756
   358
  in
Walther@60756
   359
    pbl_check andalso met_check
Walther@60756
   360
  end
walther@59988
   361
Walther@60782
   362
fun is_error (Cor _) = false
Walther@60782
   363
  | is_error (Sup _) = false
Walther@60782
   364
  | is_error (Inc _) = false
Walther@60782
   365
  | is_error (Syn _) = true
walther@59988
   366
Walther@60740
   367
(*create output-string for itm*)
Walther@60782
   368
fun to_p_model thy (Cor (d, ts)) = UnparseC.term_in_thy thy (Input_Descript.join (d, ts))
Walther@60782
   369
  | to_p_model _ (Syn c) = c
Walther@60782
   370
  | to_p_model thy (Inc (d, ts)) = UnparseC.term_in_thy thy (Input_Descript.join (d, ts))
Walther@60782
   371
  | to_p_model thy (Sup (d, ts)) = UnparseC.term_in_thy thy (Input_Descript.join (d, ts))
walther@59988
   372
Walther@60766
   373
fun fill_from_o o_model (i, vnts, bool, _, (feedb, pos)) = 
Walther@60766
   374
  let
Walther@60766
   375
    val (m_field, all_values) =
Walther@60767
   376
      case find_first (fn (_, _, _, descr', _) => Model_Def.descriptor_exists descr' feedb) o_model of
Walther@60766
   377
        SOME (_, _, m_field, _, ts) =>  (m_field, ts)
Walther@60766
   378
      | NONE => raise ERROR "I_Model.fill_from_o does NOT find a descriptor in O_Model"
Walther@60767
   379
    val descr = Model_Def.get_descr feedb (*i_single has been filtered appropriately*)
Walther@60766
   380
  in
Walther@60772
   381
(*---------------vvvvvvvvvvvvv MV if TermC.is_list all_value-----*)
Walther@60766
   382
    if Model_Def.is_list_descr descr
Walther@60766
   383
    then
Walther@60766
   384
      let
Walther@60777
   385
        val already_input = feedb |> feedb_values
Walther@60766
   386
        val miss = subtract op= already_input all_values (*"[[c], [c_2], [c_3], [c_4]]"*)
Walther@60766
   387
        val ts = already_input @ [hd miss]
Walther@60766
   388
      in
Walther@60766
   389
        if length all_values = length ts
Walther@60782
   390
        then SOME (i, vnts, bool, m_field, (Cor (descr, [Model_Def.values_to_present ts]), pos))
Walther@60782
   391
        else SOME (i, vnts, bool, m_field, (Inc (descr, [Model_Def.values_to_present ts]), pos))
Walther@60766
   392
      end
Walther@60782
   393
    else SOME (i, vnts, bool, m_field, (Cor (descr, all_values(*only 1 term*)), pos))
Walther@60766
   394
  end
walther@59988
   395
Walther@60760
   396
(*
Walther@60782
   397
  in case there is an item in i2_model(= met) with Sup, 
Walther@60782
   398
  find_first an appropriate (variant, descriptor) item in i1_model(= pbl) and add it instead Sup,
Walther@60760
   399
  otherwise keep the items of i2_model.
Walther@60760
   400
*)
Walther@60782
   401
fun add_other max_vnt i1_model (i2, _, bool2, m_field2, (Sup (descr2, ts2), pos2)) =
Walther@60767
   402
    (case find_first (fn (_, vnts1, _, _, (feedb1, _)) => case Model_Def.get_dscr_opt feedb1 of
Walther@60760
   403
          NONE => false
Walther@60760
   404
        | SOME descr1 => descr1 = descr2 andalso Model_Def.member_vnt vnts1 max_vnt) i1_model of
Walther@60760
   405
      NONE =>
Walther@60782
   406
        (i2, [max_vnt], bool2, m_field2, (Sup (descr2, ts2), pos2)) (*the present in i2_model*)
Walther@60772
   407
    | SOME i1_single => i1_single)                     (*shift the item from i1_model to i2_model*)
Walther@60772
   408
  | add_other _ _ i2_single = i2_single                    (*keep all the other items in i2_model*)
Walther@60760
   409
Walther@60770
   410
(*fill method from items already input*)
Walther@60760
   411
fun fill_method o_model (pbl_imod, met_imod) met_patt =
Walther@60760
   412
  let
Walther@60760
   413
    val pbl_max_vnts = Model_Def.max_variants o_model pbl_imod
Walther@60760
   414
    (*probably pbl/met_imod = [], so take met_patt; if empty return Sup*)
Walther@60760
   415
    val i_from_met = map (fn (_, (descr, _)) => (*order from met_patt*)
Walther@60760
   416
      Pre_Conds.get_descr_vnt descr pbl_max_vnts met_imod) met_patt (*\<longrightarrow> right order for args*)
Walther@60760
   417
Walther@60760
   418
    val met_max_vnts = Model_Def.max_variants o_model i_from_met;
Walther@60760
   419
    val max_vnt = hd (inter op= pbl_max_vnts met_max_vnts);
Walther@60760
   420
    (*add items from pbl_imod (without overwriting existing items in met_imod)*)
Walther@60760
   421
  in
Walther@60760
   422
    map (add_other max_vnt pbl_imod) i_from_met
Walther@60760
   423
  end 
Walther@60760
   424
Walther@60755
   425
fun msg vnts feedb = "get_descr_vnt' returns NONE: i.e. it does not find an item of o_model with\n" ^
Walther@60755
   426
  "variants " ^ ints2str' vnts ^ " and descriptor " ^
Walther@60767
   427
  (feedb |> Model_Def.get_dscr_opt |> the |> UnparseC.term (ContextC.for_ERROR ()))
Walther@60755
   428
fun transfer_terms (i, vnts, m_field, descr, ts) =
Walther@60782
   429
  (i, vnts, true, m_field, (Cor (descr, ts), Position.none))
Walther@60757
   430
fun s_make_complete ctxt o_model (pbl_imod, met_imod) (pbl_id, met_id) =
Walther@60751
   431
  let
Walther@60757
   432
    val {model = pbl_patt, ...} = Problem.from_store ctxt pbl_id;
Walther@60757
   433
    val {model = met_patt, ...} = MethodC.from_store ctxt met_id;
Walther@60752
   434
    val pbl_max_vnts = Model_Def.max_variants o_model pbl_imod;
Walther@60751
   435
    val i_from_pbl = map (fn (_, (descr, _)) =>
Walther@60751
   436
      Pre_Conds.get_descr_vnt descr pbl_max_vnts pbl_imod) pbl_patt
Walther@60751
   437
    val pbl_from_o_model = map ((fn i_single as (_, _, _, _, (feedb, _)) =>
Walther@60782
   438
      if is_empty_single i_single
Walther@60751
   439
      then
Walther@60751
   440
        case Pre_Conds.get_descr_vnt' feedb pbl_max_vnts o_model of
Walther@60751
   441
            [] => raise ERROR (msg pbl_max_vnts feedb)
Walther@60751
   442
          | o_singles => map transfer_terms o_singles
Walther@60751
   443
      else [i_single (*fetched before from pbl_imod*)])) i_from_pbl |> flat
Walther@60751
   444
Walther@60751
   445
    val i_from_met = map (fn (_, (descr, _)) =>
Walther@60752
   446
      Pre_Conds.get_descr_vnt descr pbl_max_vnts met_imod) met_patt
Walther@60752
   447
    val met_max_vnts = Model_Def.max_variants o_model i_from_met;
Walther@60752
   448
    val max_vnt = hd met_max_vnts (*need only one for solve-phase*)
Walther@60751
   449
Walther@60751
   450
    val met_from_pbl = map ((fn i_single as (_, _, _, _, (feedb, _)) =>
Walther@60782
   451
      if is_empty_single i_single
Walther@60751
   452
      then
Walther@60752
   453
        case Pre_Conds.get_descr_vnt' feedb [max_vnt] o_model of
Walther@60752
   454
            [] => raise ERROR (msg [max_vnt] feedb)
Walther@60751
   455
          | o_singles => map transfer_terms o_singles
Walther@60751
   456
      else [i_single (*fetched before from met_imod*)])) i_from_met |> flat
Walther@60751
   457
  in
Walther@60752
   458
    (filter (fn (_, vnts', _, _, _) => member op = vnts' max_vnt) pbl_from_o_model,
Walther@60752
   459
      met_from_pbl)
Walther@60751
   460
  end
Walther@60751
   461
walther@60126
   462
(**)end(**);