src/Tools/isac/Specify/i-model.sml
author wneuper <Walther.Neuper@jku.at>
Sun, 27 Aug 2023 16:09:04 +0200
changeset 60740 51b4f393518d
parent 60739 78a78f428ef8
child 60741 22586d7fedb0
permissions -rw-r--r--
followup 2: delete old code
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@59961
    17
  type single
Walther@60694
    18
  type single_TEST
Walther@60467
    19
  val empty_single: single
walther@60018
    20
  type variant
walther@59960
    21
  type variants
walther@59961
    22
  type m_field
walther@59961
    23
  type descriptor
walther@59948
    24
  datatype feedback = datatype Model_Def.i_model_feedback
Walther@60694
    25
  datatype feedback_TEST = datatype Model_Def.i_model_feedback_TEST
Walther@60705
    26
  type env
walther@59998
    27
  type message
walther@59938
    28
Walther@60740
    29
  val feedback_to_string: Proof.context -> feedback -> string
Walther@60733
    30
  val feedback_TEST_to_string: Proof.context -> feedback_TEST -> string
walther@59942
    31
  val single_to_string: Proof.context -> single -> string
Walther@60694
    32
  val single_to_string_TEST: Proof.context -> single_TEST -> string
walther@59942
    33
  val to_string: Proof.context -> T -> string
Walther@60694
    34
  val to_string_TEST: Proof.context -> T_TEST -> string
Walther@60708
    35
  val feedback_OLD_to_TEST: feedback -> feedback_TEST
walther@59942
    36
Walther@60477
    37
  datatype add_single = Add of single | Err of string
walther@59958
    38
  val init: Model_Pattern.T -> T
Walther@60703
    39
  val init_TEST: O_Model.T -> Model_Pattern.T -> T_TEST
walther@59998
    40
  val check_single: Proof.context -> m_field -> O_Model.T -> T -> Model_Pattern.T ->
Walther@60477
    41
    TermC.as_string -> add_single
walther@59958
    42
  val add_single: theory -> single -> T -> T
walther@59956
    43
Walther@60477
    44
  val make_tactic: m_field -> TermC.as_string * T -> Tactic.T
Walther@60477
    45
  val descriptor: feedback -> descriptor
Walther@60705
    46
  val descriptor_TEST: feedback_TEST -> descriptor
Walther@60710
    47
  val descr_pairs_to_string: Proof.context -> (Model_Pattern.single * single_TEST) list -> string
Walther@60477
    48
  val o_model_values: feedback -> O_Model.values
Walther@60733
    49
  val variables_TEST: Model_Pattern.T -> Model_Def.i_model_TEST -> term list
walther@59998
    50
  val is_notyet_input : Proof.context -> T -> O_Model.values -> O_Model.single -> Model_Pattern.T
walther@59998
    51
    -> message * single
Walther@60477
    52
  val get_field_term: theory -> O_Model.single -> single -> m_field * TermC.as_string
walther@59956
    53
Walther@60740
    54
  val of_max_variant: Model_Pattern.T -> Model_Def.i_model_TEST ->
Walther@60740
    55
       Model_Def.i_model_TEST * Env.T * (Pre_Conds.env_subst * Pre_Conds.env_eval)
Walther@60740
    56
Walther@60477
    57
  val complete: O_Model.T -> (*Problem*)T -> (*MethodC*)T -> Model_Pattern.T -> T
walther@59988
    58
  val add: single -> T -> T
walther@59988
    59
  val complete_method: O_Model.T * Model_Pattern.T * Model_Pattern.T * T -> T * T
walther@59988
    60
  val is_error: feedback -> bool
walther@59988
    61
  val complete': Model_Pattern.T -> O_Model.single -> single
walther@59956
    62
walther@59988
    63
  val is_complete: T -> bool
Walther@60740
    64
  val is_complete_variant: int -> T_TEST-> bool
walther@59988
    65
  val to_p_model: theory -> feedback -> string
walther@59956
    66
  val eq1: ''a -> 'b * (''a * 'c) -> bool
Walther@60705
    67
Walther@60694
    68
(*from isac_test for Minisubpbl*)
Walther@60723
    69
  val ori_2itm: feedback -> term -> term list -> 'a * 'b * string * descriptor * term list -> 
Walther@60723
    70
    'a * 'b * bool * string * feedback
Walther@60723
    71
  val seek_ppc: int -> T -> single option
Walther@60723
    72
  val overwrite_ppc: theory -> single -> T -> T
Walther@60723
    73
Walther@60694
    74
\<^isac_test>\<open>
Walther@60694
    75
  (**)
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@59938
    92
walther@59940
    93
type T = Model_Def.i_model_single list;
Walther@60702
    94
(* for developing input from PIDE, we use T_TEST with these ideas:
Walther@60702
    95
  (1) the new structure is as close to old T, because we want to preserve the old tests
Walther@60702
    96
  (2) after development (with *_TEST) of essential parts of the Specification's semantics,
Walther@60702
    97
      we adapt the old tests to the new T_TEST
Walther@60702
    98
  (3) together with adaption of the tests we remove the *_TEST
Walther@60702
    99
*)
Walther@60694
   100
type T_TEST = Model_Def.i_model_single_TEST list;
walther@59940
   101
datatype feedback = datatype Model_Def.i_model_feedback;
Walther@60694
   102
datatype feedback_TEST = datatype Model_Def.i_model_feedback_TEST;
walther@59940
   103
type single = Model_Def.i_model_single;
Walther@60694
   104
type single_TEST = Model_Def.i_model_single_TEST;
Walther@60467
   105
val empty_single = Model_Def.i_model_empty;
Walther@60467
   106
val empty = []: T;
Walther@60704
   107
val empty_TEST = []: T_TEST;
Walther@60733
   108
Walther@60740
   109
type env = Env.T
Walther@60740
   110
Walther@60733
   111
Walther@60706
   112
fun feedback_OLD_to_TEST (Cor ((d, ts), penv)) = (Model_Def.Cor_TEST ((d, ts), penv)) 
Walther@60706
   113
  | feedback_OLD_to_TEST (Syn c) = (Model_Def.Syn_TEST c)
Walther@60706
   114
  | feedback_OLD_to_TEST (Typ c) = (Model_Def.Syn_TEST c)
Walther@60706
   115
  | feedback_OLD_to_TEST (Inc ((d, ts), penv)) = (Model_Def.Inc_TEST ((d, ts), penv))
Walther@60706
   116
  | feedback_OLD_to_TEST (Sup (d, ts)) = (Model_Def.Sup_TEST (d, ts))
Walther@60706
   117
  | feedback_OLD_to_TEST (Mis (d, pid)) = Model_Def.Syn_TEST ((UnparseC.term (ContextC.for_ERROR ()) d) ^ " " ^
Walther@60706
   118
      (UnparseC.term (ContextC.for_ERROR ()) pid))
Walther@60706
   119
  | feedback_OLD_to_TEST (Par s) = (Model_Def.Syn_TEST s)
Walther@60706
   120
fun OLD_to_TEST i_old =
Walther@60706
   121
  map (fn (a, b, c, d, e) => (a, b, c, d, (feedback_OLD_to_TEST e, Position.none))) i_old
Walther@60705
   122
Walther@60714
   123
fun feedback_TEST_to_OLD (Model_Def.Cor_TEST ((d, ts), penv)) = (Cor ((d, ts), penv))
Walther@60714
   124
  | feedback_TEST_to_OLD (Model_Def.Syn_TEST c) = (Syn c)
Walther@60714
   125
  | feedback_TEST_to_OLD (Model_Def.Inc_TEST ((d, ts), penv)) = (Inc ((d, ts), penv))
Walther@60714
   126
  | feedback_TEST_to_OLD (Model_Def.Sup_TEST (d, ts)) = (Sup (d, ts))
Walther@60714
   127
fun TEST_to_OLD i_model = 
Walther@60714
   128
  map (fn (a, b, c, d, (e, _)) => (a, b, c, d, feedback_TEST_to_OLD e)) i_model
Walther@60714
   129
walther@59998
   130
type message = string;
walther@59938
   131
Walther@60739
   132
fun feedback_to_string ctxt (Cor ((d, ts), _)) = 
Walther@60739
   133
    "Cor " ^ UnparseC.term  ctxt (Input_Descript.join (d, ts)) ^ " , pen2str"
walther@59942
   134
  | feedback_to_string _ (Syn c) = "Syn " ^ c
walther@59942
   135
  | feedback_to_string _ (Typ c) = "Typ " ^ c
Walther@60739
   136
  | feedback_to_string ctxt (Inc ((d, ts), _)) = 
Walther@60739
   137
    "Inc " ^ UnparseC.term  ctxt (Input_Descript.join (d, ts)) ^ " , pen2str"
walther@59942
   138
  | feedback_to_string ctxt (Sup (d, ts)) = 
Walther@60675
   139
    "Sup " ^ UnparseC.term  ctxt (Input_Descript.join (d, ts))
walther@59942
   140
  | feedback_to_string ctxt (Mis (d, pid)) = 
Walther@60698
   141
    "Mis " ^ UnparseC.term  ctxt d ^ " " ^ UnparseC.term ctxt pid
walther@59942
   142
  | feedback_to_string _ (Par s) = "Trm "^s;
Walther@60698
   143
Walther@60733
   144
(**)
Walther@60739
   145
fun feedback_TEST_to_string ctxt (Cor_TEST ((d, ts), _)) = 
Walther@60733
   146
    "Cor_TEST " ^ UnparseC.term ctxt (Input_Descript.join (d, ts)) ^ " , pen2str"
Walther@60733
   147
  | feedback_TEST_to_string _ (Syn_TEST c) =
Walther@60733
   148
    "Syn_TEST " ^ c
Walther@60733
   149
  | feedback_TEST_to_string ctxt (Inc_TEST ((d, []), _)) = 
Walther@60733
   150
    "Inc_TEST " ^ UnparseC.term ctxt (Input_Descript.join (d, [])) ^ " " ^ 
Walther@60733
   151
      Model_Pattern.empty_for d
Walther@60739
   152
  | feedback_TEST_to_string ctxt (Inc_TEST ((d, ts), _)) =
Walther@60733
   153
    "Inc_TEST " ^ UnparseC.term ctxt (Input_Descript.join (d, ts)) ^ " , pen2str"
Walther@60733
   154
  | feedback_TEST_to_string ctxt (Sup_TEST (d, ts)) = 
Walther@60733
   155
    "Sup_TEST " ^ UnparseC.term ctxt (Input_Descript.join (d, ts))
walther@59942
   156
walther@59942
   157
fun single_to_string ctxt (i, is, b, s, itm_) = 
walther@59942
   158
  "(" ^ string_of_int i ^ " ," ^ ints2str' is ^ " ," ^ bool2str b ^ " ," ^
walther@59942
   159
  s ^ " ," ^ feedback_to_string ctxt itm_ ^ ")";
Walther@60702
   160
fun single_to_string_TEST ctxt (i, is, b, s, (itm_, _(*Position.T*))) = 
Walther@60694
   161
  "(" ^ string_of_int i ^ ", " ^ ints2str' is ^ ", " ^ bool2str b ^ " ," ^
Walther@60733
   162
  s ^ ", (" ^ feedback_TEST_to_string ctxt itm_ ^ ", Position.T))";
Walther@60694
   163
walther@59942
   164
fun to_string ctxt itms = strs2str' (map (linefeed o (single_to_string ctxt)) itms);
Walther@60694
   165
fun to_string_TEST ctxt itms = strs2str' (map (linefeed o (single_to_string_TEST ctxt)) itms);
walther@59942
   166
walther@59958
   167
Walther@60694
   168
(** make a Tactic.T **)
Walther@60694
   169
Walther@60477
   170
fun make_tactic m_field (term_as_string, i_model) =
walther@59992
   171
  case m_field of
walther@59992
   172
    "#Given" => Tactic.Add_Given' (term_as_string, i_model)
walther@59992
   173
  | "#Find" => Tactic.Add_Find' (term_as_string, i_model)
walther@59992
   174
  | "#Relate" => Tactic.Add_Relation'(term_as_string, i_model)
walther@59992
   175
  | str => raise ERROR ("specify_additem Frm, Pbl: uncovered case with " ^ str);
walther@59992
   176
walther@59992
   177
walther@59958
   178
(** initialise a model **)
walther@59958
   179
walther@59958
   180
fun init pbt = 
walther@59958
   181
  let
walther@59958
   182
    fun pbt2itm (f, (d, _)) = (0, [], false, f, Inc ((d, []), (TermC.empty, [])))
walther@59958
   183
  in map pbt2itm pbt end
Walther@60556
   184
Walther@60702
   185
(*
Walther@60702
   186
  Design decision:
Walther@60705
   187
* Now the Model in Specification is intialised such that the placement of items can be
Walther@60702
   188
  maximally stable during interactive input to the Specification.
Walther@60702
   189
* Template.show provides the initial output to the user and thus determines what will be parsed
Walther@60702
   190
  by Outer_Syntax later during interaction.
Walther@60705
   191
* The relation between O_Model.T and I_Model.T becomes much simpler.
Walther@60702
   192
*)
Walther@60702
   193
(**)
Walther@60702
   194
fun pat_to_item o_model (_, (descriptor, _)) =
Walther@60702
   195
  case find_first (fn (_, _, _, desc, _) => desc = descriptor) o_model of
Walther@60702
   196
    NONE => raise ERROR "I_Model.pat_to_item_TEST with NONE"
Walther@60702
   197
  | SOME (_, variants, m_field, descr, _) => (variants, false, m_field,
Walther@60714
   198
    (Inc_TEST ((descr, []), (descr, [])), Position.none))
Walther@60702
   199
fun init_TEST o_model model_patt =
Walther@60690
   200
  let
Walther@60702
   201
    val pre_items = map (pat_to_item o_model) model_patt
Walther@60702
   202
  in
Walther@60702
   203
    O_Model.add_enumerate pre_items |> map (fn (a, (b, c, d, e)) => (a, b, c, d, e))
Walther@60702
   204
  end
walther@59943
   205
Walther@60705
   206
(*/---------------- old code -----------------------------------------------------------------\*)
Walther@60477
   207
fun o_model_values (Cor ((_, ts), _)) = ts
Walther@60477
   208
  | o_model_values (Syn _) = []
Walther@60477
   209
  | o_model_values (Typ _) = []
Walther@60477
   210
  | o_model_values (Inc ((_, ts), _)) = ts
Walther@60477
   211
  | o_model_values (Sup (_, ts)) = ts
Walther@60477
   212
  | o_model_values (Mis _) = []
Walther@60477
   213
  | o_model_values _ = raise ERROR "o_model_values: uncovered case in fun.def.";
walther@59943
   214
Walther@60664
   215
val unique = Syntax.read_term\<^context> "UnIqE_tErM";
Walther@60477
   216
fun descriptor (Cor ((d ,_), _)) = d
Walther@60477
   217
  | descriptor (Syn _) = ((*tracing ("*** descriptor: Syn ("^c^")");*) unique)
Walther@60477
   218
  | descriptor (Typ _) = ((*tracing ("*** descriptor: Typ ("^c^")");*) unique)
Walther@60477
   219
  | descriptor (Inc ((d, _), _)) = d
Walther@60477
   220
  | descriptor (Sup (d, _)) = d
Walther@60477
   221
  | descriptor (Mis (d, _)) = d
Walther@60477
   222
  | descriptor _ = raise ERROR "descriptor: uncovered case in fun.def.";
Walther@60705
   223
fun descriptor_TEST (Cor_TEST ((d ,_), _)) = d
Walther@60705
   224
  | descriptor_TEST (Syn_TEST _) = ((*tracing ("*** descriptor: Syn ("^c^")");*) unique)
Walther@60705
   225
  | descriptor_TEST (Inc_TEST ((d, _), _)) = d
Walther@60705
   226
  | descriptor_TEST (Sup_TEST (d, _)) = d
walther@59943
   227
Walther@60710
   228
fun descr_pairs_to_string ctxt equal_descr_pairs =
Walther@60710
   229
(map (fn (a, b) => pair (Model_Pattern.pat2str ctxt a) (single_to_string_TEST ctxt b)
Walther@60710
   230
    |> pair2str) equal_descr_pairs)
Walther@60710
   231
  |> strs2str'
Walther@60710
   232
Walther@60733
   233
fun variables_TEST model_patt i_model =
Walther@60733
   234
  Pre_Conds.environment_TEST model_patt i_model
Walther@60733
   235
  |> map snd
walther@59943
   236
Walther@60664
   237
val unknown_descriptor = Syntax.read_term\<^context> "unknown::'a => unknow";
walther@59956
   238
Walther@60670
   239
(* get a term from O_Model, notyet input in I_Model.
Walther@60670
   240
   the term from O_Model is thrown back to a string in order to reuse
walther@59992
   241
   machinery for immediate input by the user. *)
Walther@60477
   242
fun get_field_term thy (_, _, _, d, ts) (_, _, _, fd, itm_) =
Walther@60477
   243
  (fd, ((UnparseC.term_in_thy thy) o Input_Descript.join) (d, subtract op = (o_model_values itm_) ts))
walther@59992
   244
Walther@60740
   245
(*update the itm_ already input, all..from ori*)
walther@59956
   246
fun ori_2itm itm_ pid all (id, vt, fd, d, ts) = 
walther@59956
   247
  let 
Walther@60477
   248
    val ts' = union op = (o_model_values itm_) ts;
Walther@60478
   249
    val pval = [Input_Descript.join'''' (d, ts')]
Walther@60740
   250
	  (*WN.9.5.03: FIXXXME [#0, epsilon] here would upd_penv be called for [#0, epsilon] etc*)
walther@59956
   251
    val complete = if eq_set op = (ts', all) then true else false
walther@59956
   252
  in
walther@59956
   253
    case itm_ of
walther@59956
   254
      (Cor _) => 
walther@59956
   255
        (if fd = "#undef" then (id, vt, complete, fd, Sup (d, ts')) 
walther@59956
   256
	       else (id, vt, complete, fd, Cor ((d, ts'), (pid, pval))))
walther@59962
   257
    | (Syn c) => raise ERROR ("ori_2itm wants to overwrite " ^ c)
walther@59962
   258
    | (Typ c) => raise ERROR ("ori_2itm wants to overwrite " ^ c)
walther@59956
   259
    | (Inc _) =>
walther@59956
   260
      if complete
walther@59956
   261
  	  then (id, vt, true, fd, Cor ((d, ts'), (pid, pval)))
walther@59956
   262
  	  else (id, vt, false, fd, Inc ((d, ts'), (pid, pval)))
walther@59956
   263
    | (Sup (d,ts')) => (*4.9.01 lost env*)
walther@59956
   264
  	  (*if fd = "#undef" then*) (id,vt,complete,fd,Sup(d,ts'))
walther@59956
   265
  	  (*else (id,vt,complete,fd,Cor((d,ts'),e))*)
walther@59956
   266
      (* 28.1.00: not completely clear ---^^^ etc.*)
walther@59956
   267
    | (Mis _) => (* 4.9.01: Mis just copied *)
walther@59956
   268
       if complete
walther@59956
   269
  		 then (id, vt, true, fd, Cor ((d,ts'), (pid, pval)))
walther@59956
   270
  		 else (id, vt, false, fd, Inc ((d,ts'), (pid, pval)))
Walther@60733
   271
    | i => raise ERROR ("ori_2itm: uncovered case of " ^ feedback_to_string (ContextC.for_ERROR ()) i)
walther@59956
   272
  end
walther@59956
   273
Walther@60740
   274
Walther@60740
   275
(** find next step **)
Walther@60740
   276
walther@59956
   277
fun eq1 d (_, (d', _)) = (d = d')
Walther@60477
   278
fun eq3 f d (_, _, _, f', itm_) = f = f' andalso d = (descriptor itm_) 
walther@59956
   279
walther@59956
   280
fun is_notyet_input ctxt itms all (i, v, f, d, ts) pbt =
walther@59956
   281
  case find_first (eq1 d) pbt of
walther@59956
   282
    SOME (_, (_, pid)) =>
walther@59956
   283
      (case find_first (eq3 f d) itms of
walther@59998
   284
        SOME (_, _, _, _, itm_) =>
Walther@60477
   285
          let val ts' = inter op = (o_model_values itm_) ts
walther@59956
   286
          in 
walther@59956
   287
            if subset op = (ts, ts') 
Walther@60675
   288
            then (((strs2str' o map (UnparseC.term ctxt)) ts') ^ " already input", empty_single)
walther@59998
   289
	          else ("", ori_2itm itm_ pid all (i, v, f, d, subtract op = ts' ts))
walther@59956
   290
	          end
walther@59998
   291
	    | NONE => ("", ori_2itm (Inc ((TermC.empty, []), (pid, []))) pid all (i, v, f, d, ts)))
walther@59956
   292
  | NONE => ("", ori_2itm (Sup (d, ts)) TermC.empty all (i, v, f, d, ts))
walther@59956
   293
Walther@60477
   294
datatype add_single =
walther@59958
   295
	Add of single   (* return-value of check_single *)
walther@59998
   296
| Err of string   (* error-message                *)
walther@59956
   297
walther@59956
   298
(*
walther@59956
   299
  Create feedback for input of TermC.as_string to m_field;
walther@59956
   300
  check w.r.t. O_Model.T and Model_Pattern.T.
walther@59998
   301
  In case of O_Model.T = [] (i.e. no data for user-guidance in Formalise.T)
walther@59958
   302
  check_single is extremely permissive.
walther@59956
   303
*)
Walther@60658
   304
(*will come directly from PIDE -----------------vvvvvvvvvvv
Walther@60658
   305
  in case t comes from Step.specify_do_next -----------vvv = Position.none*)
Walther@60658
   306
fun check_single ctxt m_field [] i_model m_patt (ct(*, pos*)) =
Walther@60658
   307
    let
Walther@60658
   308
      val i = 1 + (if i_model = [] then 0 else map #1 i_model |> maxl)
Walther@60661
   309
    (*/------------ replace by ParseC.term_position -----------\*)
Walther@60661
   310
      val t = Syntax.read_term ctxt ct
Walther@60658
   311
        handle ERROR msg => error (msg (*^ Position.here pos*))
Walther@60661
   312
    (*\------------ replace by ParseC.term_position -----------/*)
Walther@60658
   313
        (*NONE => Add (i, [], false, m_field, Syn ct)*)
Walther@60658
   314
      val (d, ts) = Input_Descript.split t
Walther@60658
   315
    in 
Walther@60658
   316
      if d = TermC.empty then
Walther@60658
   317
        Add (i, [], false, m_field, Mis (unknown_descriptor, hd ts)) 
Walther@60658
   318
      else
Walther@60658
   319
        (case find_first (eq1 d) m_patt of
Walther@60658
   320
          NONE => Add (i, [], true, m_field, Sup (d,ts))
Walther@60658
   321
        | SOME (f, (_, id)) =>
Walther@60658
   322
            let
Walther@60658
   323
              fun eq2 d (i, _, _, _, itm_) = d = (descriptor itm_) andalso i <> 0
Walther@60658
   324
            in
Walther@60658
   325
              case find_first (eq2 d) i_model of
Walther@60658
   326
                NONE => Add (i, [], true, f,Cor ((d, ts), (id, [Input_Descript.join'''' (d, ts)])))
Walther@60658
   327
              | SOME (i', _, _, _, itm_) => 
Walther@60658
   328
                  if Input_Descript.for_list d then 
Walther@60658
   329
                    let
Walther@60658
   330
                      val in_itm = o_model_values itm_
Walther@60658
   331
                      val ts' = union op = ts in_itm
Walther@60658
   332
                      val i'' = if in_itm = [] then i else i'
Walther@60658
   333
                    in Add (i'', [], true, f, Cor ((d, ts'), (id, [Input_Descript.join'''' (d, ts')])))end
Walther@60658
   334
                  else Add (i', [], true, f, Cor ((d, ts), (id, [Input_Descript.join'''' (d, ts)])))
Walther@60658
   335
            end)
Walther@60658
   336
    end
Walther@60740
   337
    (*for MethodC:   #undef  completed vvvvv vvvvvv term_as_string*)
Walther@60658
   338
    (*will come directly from PIDE ----------------------vvvvvvvvvvv*)
Walther@60658
   339
  | check_single ctxt m_field o_model i_model m_patt (str(*, pos*)) =
Walther@60658
   340
    let
Walther@60659
   341
      val (t as (descriptor $ _)) = Syntax.read_term ctxt str
Walther@60740
   342
        handle ERROR msg => error (msg (*TODO: ^ Position.here pp*))
Walther@60659
   343
        (*old code: NONE => Err ("ERROR I_Model.check_single: syntax error in \"" ^ str ^ "\"")*)
Walther@60658
   344
    in 
Walther@60658
   345
        case Model_Pattern.get_field descriptor m_patt of
Walther@60658
   346
          NONE => Err ("ERROR I_Model.check_single: unknown descriptor \"" ^
Walther@60675
   347
            UnparseC.term ctxt descriptor ^ "\"")
Walther@60658
   348
        | SOME m_field' => 
Walther@60658
   349
          if m_field <> m_field' then
Walther@60675
   350
            Err ("ERROR I_Model.check_single: \"" ^ UnparseC.term ctxt t ^ "\"" ^
Walther@60658
   351
             "\" not for field \"" ^ m_field ^ "\"")
Walther@60658
   352
          else
Walther@60658
   353
            case O_Model.contains ctxt m_field o_model t of
Walther@60658
   354
              ("", ori', all) => 
Walther@60658
   355
                (case is_notyet_input ctxt i_model all ori' m_patt of
Walther@60658
   356
                   ("", itm) => Add itm
Walther@60658
   357
                 | (msg, _) => Err ("ERROR I_Model.check_single: is_notyet_input: " ^ msg))
Walther@60658
   358
            | (msg, _, _) => Err ("ERROR I_Model.check_single: contains: " ^ msg)
Walther@60658
   359
    end
Walther@60658
   360
     
walther@59958
   361
walther@59958
   362
(** add input **)
walther@59958
   363
Walther@60586
   364
fun overwrite_ppc thy itm model =
walther@59958
   365
  let 
walther@59958
   366
    fun repl _ (_, _, _, _, itm_) [] =
walther@60360
   367
        raise ERROR ("overwrite_ppc: " ^ feedback_to_string (Proof_Context.init_global thy) itm_
walther@60360
   368
          ^ " not found")
Walther@60586
   369
      | repl model' itm (p :: model) =
walther@59958
   370
	      if (#1 itm) = (#1 p)
Walther@60586
   371
	      then model' @ [itm] @ model
Walther@60586
   372
	      else repl (model' @ [p]) itm model
Walther@60586
   373
  in repl [] itm model end
walther@59958
   374
Walther@60740
   375
(*find_first item with #1 equal to id*)
walther@59958
   376
fun seek_ppc _ [] = NONE
Walther@60586
   377
  | seek_ppc id (p :: model) = if id = #1 (p: single) then SOME p else seek_ppc id model
walther@59958
   378
walther@59958
   379
(* 10.3.00: insert the already compiled itm into model;
walther@59958
   380
   ev. filter_out  untouched (in FE: (0,...)) item related to insert-item *)
Walther@60586
   381
fun add_single thy itm model =
walther@59958
   382
  let 
Walther@60477
   383
    fun eq_untouched d (0, _, _, _, itm_) = (d = descriptor itm_)
walther@59958
   384
      | eq_untouched _ _ = false
Walther@60586
   385
    val model' = case seek_ppc (#1 itm) model of
Walther@60586
   386
      SOME _ => overwrite_ppc thy itm model (*itm updated in is_notyet_input WN.11.03*)
Walther@60586
   387
    | NONE => (model @ [itm])
Walther@60586
   388
  in filter_out (eq_untouched ((descriptor o #5) itm)) model' end
walther@59956
   389
Walther@60740
   390
Walther@60740
   391
(** I_Model.is_complete **)
Walther@60740
   392
walther@59988
   393
fun complete' pbt (i, v, f, d, ts) =
Walther@60654
   394
  case \<^try>\<open> (i, v, true, f, Cor ((d, ts),
Walther@60654
   395
      ((find_first ((fn descriptor => (fn (_, (d, _)) => descriptor = d)) d)) pbt |> the |> snd |> snd, ts)
Walther@60654
   396
    ))\<close> of
walther@60265
   397
    SOME x => x
walther@60265
   398
  | NONE => (i, v, true, f, Cor ((d, ts), (d, ts)))
walther@59988
   399
Walther@60740
   400
(*filter out oris which have same description in itms*)
walther@59988
   401
fun filter_outs oris [] = oris
walther@59988
   402
  | filter_outs oris (i::itms) = 
walther@59988
   403
    let
Walther@60477
   404
      val ors = filter_out ((curry op = ((descriptor o #5) i)) o (#4)) oris
walther@59988
   405
    in
walther@59988
   406
      filter_outs ors itms
walther@59988
   407
    end
walther@59988
   408
Walther@60740
   409
(*filter oris which are in pbt, too*)
walther@59988
   410
fun filter_pbt oris pbt =
walther@59988
   411
  let
walther@59988
   412
    val dscs = map (fst o snd) pbt
walther@59988
   413
  in
walther@59988
   414
    filter ((member op= dscs) o (#4)) oris
walther@59988
   415
  end
walther@59988
   416
walther@59988
   417
fun is_error (Cor _) = false
walther@59988
   418
  | is_error (Sup _) = false
walther@59988
   419
  | is_error (Inc _) = false
walther@59988
   420
  | is_error (Mis _) = false
walther@59988
   421
  | is_error _ = true
walther@59988
   422
Walther@60740
   423
(*create output-string for itm*)
Walther@60674
   424
fun to_p_model thy (Cor ((d, ts), _)) = UnparseC.term_in_thy thy (Input_Descript.join (d, ts))
walther@59988
   425
  | to_p_model _ (Syn c) = c
walther@59988
   426
  | to_p_model _ (Typ c) = c
Walther@60673
   427
  | to_p_model thy (Inc ((d, ts), _)) = UnparseC.term_in_thy thy (Input_Descript.join (d, ts))
Walther@60673
   428
  | to_p_model thy (Sup (d, ts)) = UnparseC.term_in_thy thy (Input_Descript.join (d, ts))
Walther@60674
   429
  | to_p_model thy (Mis (d, pid)) = UnparseC.term_in_thy thy d ^ " " ^ UnparseC.term_in_thy thy pid
walther@59988
   430
  | to_p_model _ _ = raise ERROR "to_p_model: uncovered definition"
walther@59988
   431
Walther@60477
   432
fun eq_dsc ((_, _, _, _, itm_), (_, _, _, _, iitm_)) = (descriptor itm_ = descriptor iitm_)
walther@59988
   433
walther@59988
   434
(* insert_ppc = add for appl_add', input_icalhd 11.03,
walther@59988
   435
   handles superfluous items carelessly                       *)
walther@59988
   436
fun add itm itms = if member eq_dsc itms itm then itms else itms @ [itm] (* @ new itm *)
walther@59988
   437
Walther@60740
   438
(*combine itms from pbl + met and complete them wrt. pbt*)
walther@59988
   439
(* FIXXXME.WN031205 complete doesnt handle incorrect itms !*)
walther@59988
   440
fun complete oris pits mits met = 
walther@59988
   441
  let
Walther@60705
   442
    val vat = Pre_Conds.max_variant pits;
walther@59988
   443
    val itms = pits @ (filter ((member_swap op = vat) o (#2 )) mits)
walther@59988
   444
    val ors = filter ((member_swap op= vat) o (#2)) oris
walther@59988
   445
    val os = filter_outs ors itms (*WN.12.03?: does _NOT_ add itms from met ?!*)
walther@59988
   446
  in
walther@59988
   447
    itms @ (map (complete' met) os)
walther@59988
   448
  end
walther@59988
   449
Walther@60740
   450
(*complete model and guard of a calc-head*)
Walther@60586
   451
fun complete_method (oris, mpc, model, probl) =
walther@59988
   452
  let
walther@59988
   453
    val pits = filter_out ((curry op= false) o (#3)) probl
Walther@60705
   454
    val vat = if probl = [] then 1 else Pre_Conds.max_variant probl
walther@59988
   455
    val pors = filter ((member_swap op = vat) o (#2)) oris
walther@59988
   456
    val pors = filter_outs pors pits (*which are in pbl already*)
Walther@60586
   457
    val pors = (filter_pbt pors model) (*which are in pbt, too*)
Walther@60586
   458
    val pits = pits @ (map (complete' model) pors)
walther@59988
   459
    val mits = complete oris pits [] mpc
walther@59988
   460
  in (pits, mits) end
walther@59988
   461
Walther@60740
   462
(*TODO: also check if all elements are I_Model.Cor*)
walther@59998
   463
fun is_complete ([]: T) = false
walther@59988
   464
  | is_complete itms = foldl and_ (true, (map #3 itms))
walther@59986
   465
Walther@60740
   466
(*for PIDE*)
Walther@60705
   467
fun is_complete_variant no_model_items i_model =
Walther@60705
   468
  no_model_items = length (filter (fn (_, _, _, _, (Cor_TEST _, _)) =>  true | _ => false) i_model)
Walther@60705
   469
Walther@60710
   470
val of_max_variant = Pre_Conds.of_max_variant
Walther@60710
   471
walther@60126
   472
(**)end(**);