src/Tools/isac/Specify/i-model.sml
author wneuper <walther.neuper@jku.at>
Tue, 20 Jul 2021 14:37:56 +0200
changeset 60339 0d22a6bf1fc6
parent 60335 7701598a2182
child 60340 0ee698b0a703
permissions -rw-r--r--
//reduce the number of TermC.parse*; "//"means: tests broken .

broken tests are outcommented with "reduce the number of TermC.parse*"
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@59938
     8
signature MODEL_FOR_INTERACTION =
walther@59938
     9
sig
walther@59969
    10
walther@59961
    11
  type T
walther@59961
    12
  type single
walther@59948
    13
  val empty: single 
walther@60018
    14
  type variant
walther@59960
    15
  type variants
walther@59961
    16
  type m_field
walther@59961
    17
  type descriptor
walther@59948
    18
  datatype feedback = datatype Model_Def.i_model_feedback
walther@59998
    19
  type message
walther@59938
    20
walther@59942
    21
  val feedback_to_string': feedback -> string
walther@59942
    22
  val feedback_to_string: Proof.context -> feedback -> string
walther@59942
    23
  val single_to_string: Proof.context -> single -> string
walther@59942
    24
  val to_string: Proof.context -> T -> string
walther@59942
    25
walther@59998
    26
(*/------- rename -------\*)
walther@59956
    27
  datatype additm = Add of single | Err of string
walther@59998
    28
(*\------- rename -------/*)
walther@60126
    29
  val initPIDE: Problem.id -> T
walther@59958
    30
  val init: Model_Pattern.T -> T
walther@59998
    31
(*val check_single: Proof.context -> *)
walther@59998
    32
  val check_single: Proof.context -> m_field -> O_Model.T -> T -> Model_Pattern.T ->
walther@59998
    33
    TermC.as_string -> additm
walther@59958
    34
  val add_single: theory -> single -> T -> T
walther@59956
    35
walther@59998
    36
(*/------- rename -------\*)
walther@59998
    37
(*val make_tactic: m_field -> TermC.as_string * T -> Tactic.T*)
walther@59992
    38
  val get_tac: m_field -> TermC.as_string * T -> Tactic.T
walther@59998
    39
(*val d_in: feedback -> descriptor*)
walther@59998
    40
  val d_in: feedback -> descriptor
walther@59998
    41
(*val ts_in: feedback -> O_Model.values*)
walther@59998
    42
  val ts_in: feedback -> O_Model.values
walther@59998
    43
(*val vars_of: T -> term list*)
walther@59956
    44
  val vars_of: T -> term list
walther@59998
    45
(*val max_vt: T -> int*)
walther@59956
    46
  val max_vt: T -> int
walther@59998
    47
(*val is_notyet_input : Proof.context -> T -> O_Model.values -> O_Model.single -> Model_Pattern.T
walther@59998
    48
    -> message * single*)
walther@59998
    49
  val is_notyet_input : Proof.context -> T -> O_Model.values -> O_Model.single -> Model_Pattern.T
walther@59998
    50
    -> message * single
walther@59998
    51
(*val dsc_unknown: term*)
walther@59958
    52
  val dsc_unknown: term
walther@59998
    53
(*val geti_ct: theory -> O_Model.single -> single -> m_field * UnparseC.term_as_string*)
walther@59992
    54
  val geti_ct: theory -> O_Model.single -> single -> m_field * UnparseC.term_as_string
walther@59998
    55
(*val pbl_ids': term -> term list -> term list*)
walther@59988
    56
  val pbl_ids': term -> term list -> term list
walther@59948
    57
walther@59956
    58
  val mk_env: T -> (term * term) list
walther@59956
    59
  val penvval_in: feedback -> term list
walther@59988
    60
(*\------- rename -------/*)
walther@59956
    61
walther@59988
    62
  val complete: O_Model.T -> T -> T -> Model_Pattern.T -> T
walther@59988
    63
  val add: single -> T -> T
walther@59998
    64
(*val complete_method: O_Model.T -> Model_Pattern.T * Model_Pattern.T * T -> T * T*)
walther@59988
    65
  val complete_method: O_Model.T * Model_Pattern.T * Model_Pattern.T * T -> T * T
walther@59988
    66
  val is_error: feedback -> bool
walther@59988
    67
  val complete': Model_Pattern.T -> O_Model.single -> single
walther@59956
    68
walther@59988
    69
  val is_complete: T -> bool
walther@59988
    70
  val to_p_model: theory -> feedback -> string
walther@59988
    71
(*/------- rename -------\*)
walther@59998
    72
(*val dts2str: term * (term list) -> string*)
walther@59956
    73
  val dts2str: term * (term list) -> string
walther@59956
    74
  val eq1: ''a -> 'b * (''a * 'c) -> bool
walther@59988
    75
(*\------- rename -------/*)
walther@59988
    76
(*/------- rename -------\*)
wenzelm@60223
    77
\<^isac_test>\<open>
walther@59958
    78
  val vts_in: T -> int list
wenzelm@60223
    79
\<close>
walther@59938
    80
end
walther@59938
    81
walther@59942
    82
(**)
walther@60265
    83
structure I_Model(**) : MODEL_FOR_INTERACTION(**) =
walther@59938
    84
struct
walther@59942
    85
(**)
walther@59955
    86
walther@59958
    87
(** data types **)
walther@59958
    88
walther@60018
    89
type variant =  Model_Def.variant;
walther@59940
    90
type variants =  Model_Def.variants;
walther@59952
    91
type m_field = Model_Def.m_field;
walther@59952
    92
type descriptor = Model_Def.descriptor;
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@59940
    96
type single = Model_Def.i_model_single;
walther@59940
    97
val empty = Model_Def.i_model_empty;
walther@59998
    98
type message = string;
walther@59938
    99
walther@59942
   100
fun pen2str ctxt (t, ts) =
walther@59942
   101
  pair2str (UnparseC.term_in_ctxt ctxt t, (strs2str' o map (UnparseC.term_in_ctxt  ctxt)) ts);
walther@59948
   102
walther@59942
   103
fun feedback_to_string ctxt (Cor ((d, ts), penv)) = 
walther@59953
   104
    "Cor " ^ UnparseC.term_in_ctxt  ctxt (Input_Descript.join (d, ts)) ^ " ," ^ pen2str ctxt penv
walther@59942
   105
  | feedback_to_string _ (Syn c) = "Syn " ^ c
walther@59942
   106
  | feedback_to_string _ (Typ c) = "Typ " ^ c
walther@59942
   107
  | feedback_to_string ctxt (Inc ((d, ts), penv)) = 
walther@59953
   108
    "Inc " ^ UnparseC.term_in_ctxt  ctxt (Input_Descript.join (d, ts)) ^ " ," ^ pen2str ctxt penv
walther@59942
   109
  | feedback_to_string ctxt (Sup (d, ts)) = 
walther@59953
   110
    "Sup " ^ UnparseC.term_in_ctxt  ctxt (Input_Descript.join (d, ts))
walther@59942
   111
  | feedback_to_string ctxt (Mis (d, pid)) = 
walther@59942
   112
    "Mis "^ UnparseC.term_in_ctxt  ctxt d ^ " " ^ UnparseC.term_in_ctxt  ctxt pid
walther@59942
   113
  | feedback_to_string _ (Par s) = "Trm "^s;
walther@59942
   114
fun feedback_to_string' t = feedback_to_string (ThyC.id_to_ctxt "Isac_Knowledge") t;
walther@59942
   115
walther@59942
   116
fun single_to_string ctxt (i, is, b, s, itm_) = 
walther@59942
   117
  "(" ^ string_of_int i ^ " ," ^ ints2str' is ^ " ," ^ bool2str b ^ " ," ^
walther@59942
   118
  s ^ " ," ^ feedback_to_string ctxt itm_ ^ ")";
walther@59942
   119
fun to_string ctxt itms = strs2str' (map (linefeed o (single_to_string ctxt)) itms);
walther@59942
   120
walther@59958
   121
walther@59992
   122
fun get_tac m_field (term_as_string, i_model) =
walther@59992
   123
  case m_field of
walther@59992
   124
    "#Given" => Tactic.Add_Given' (term_as_string, i_model)
walther@59992
   125
  | "#Find" => Tactic.Add_Find' (term_as_string, i_model)
walther@59992
   126
  | "#Relate" => Tactic.Add_Relation'(term_as_string, i_model)
walther@59992
   127
  | str => raise ERROR ("specify_additem Frm, Pbl: uncovered case with " ^ str);
walther@59992
   128
walther@59992
   129
walther@59958
   130
(** initialise a model **)
walther@59958
   131
walther@59958
   132
fun init pbt = 
walther@59958
   133
  let
walther@59958
   134
    fun pbt2itm (f, (d, _)) = (0, [], false, f, Inc ((d, []), (TermC.empty, [])))
walther@59958
   135
  in map pbt2itm pbt end
walther@59958
   136
walther@60126
   137
fun initPIDE pbl_id = pbl_id |> Problem.from_store |> #ppc |> init
walther@60126
   138
walther@59958
   139
walther@59958
   140
(** check input on a model **)
walther@59942
   141
walther@59943
   142
(* find most frequent variant v in itms *)
walther@60017
   143
fun vts_in itms = ((distinct op =) o flat o (map #2)) (itms: T);
walther@59943
   144
walther@59943
   145
fun cnt itms v = (v, (length o (filter (curry op = v)) o flat o (map #2)) itms);
walther@59943
   146
fun vts_cnt vts itms = map (cnt itms) vts;
walther@59962
   147
fun max2 [] = raise ERROR "max2 of []"
walther@59943
   148
  | max2 (y :: ys) =
walther@59943
   149
    let
walther@59943
   150
      fun mx (a,x) [] = (a,x)
walther@59943
   151
  	    | mx (a, x) ((b,y) :: ys) = if x < y then mx (b, y) ys else mx (a, x) ys;
walther@59943
   152
    in mx y ys end;
walther@59943
   153
walther@59943
   154
(* get the constant value from a penv *)
walther@59943
   155
fun getval (id, values) = 
walther@59943
   156
  case values of
walther@59962
   157
	  [] => raise ERROR ("penv_value: no values in '" ^ UnparseC.term id)
walther@59943
   158
  | [v] => (id, v)
walther@59943
   159
  | (v1 :: v2 :: _) => (case v1 of 
walther@60335
   160
	      Const (\<^const_name>\<open>Program.Arbfix\<close>, _) => (id, v2)
walther@59943
   161
	    | _ => (id, v1));
walther@59943
   162
walther@59943
   163
(* find the variant with most items already input *)
walther@59943
   164
fun max_vt itms = 
walther@59943
   165
    let val vts = (vts_cnt (vts_in itms)) itms;
walther@59943
   166
    in if vts = [] then 0 else (fst o max2) vts end;
walther@59943
   167
walther@59943
   168
(* TODO ev. make more efficient by avoiding flat *)
walther@59943
   169
fun mk_e (Cor (_, iv)) = [getval iv]
walther@59943
   170
  | mk_e (Syn _) = []
walther@59943
   171
  | mk_e (Typ _) = [] 
walther@59943
   172
  | mk_e (Inc (_, iv)) = [getval iv]
walther@59943
   173
  | mk_e (Sup _) = []
walther@59943
   174
  | mk_e (Mis _) = []
walther@59962
   175
  | mk_e  _ = raise ERROR "mk_e: uncovered case in fun.def.";
walther@59943
   176
fun mk_en vt (_, vts, _, _, itm_) = if member op = vts vt then mk_e itm_ else [];
walther@59943
   177
walther@59943
   178
(* extract the environment from an item list; takes the variant with most items *)
walther@59943
   179
fun mk_env itms = 
walther@59943
   180
  let val vt = max_vt itms
walther@59943
   181
  in (flat o (map (mk_en vt))) itms end;
walther@59943
   182
(**)
walther@59943
   183
fun ts_in (Cor ((_, ts), _)) = ts
walther@59943
   184
  | ts_in (Syn _) = []
walther@59943
   185
  | ts_in (Typ _) = []
walther@59943
   186
  | ts_in (Inc ((_, ts), _)) = ts
walther@59943
   187
  | ts_in (Sup (_, ts)) = ts
walther@59943
   188
  | ts_in (Mis _) = []
walther@59962
   189
  | ts_in _ = raise ERROR "ts_in: uncovered case in fun.def.";
walther@59943
   190
walther@60339
   191
val unique = TermC.parseNEW'' @{theory "Real"} "UnIqE_tErM";
walther@59943
   192
fun d_in (Cor ((d ,_), _)) = d
walther@59956
   193
  | d_in (Syn _) = ((*tracing ("*** d_in: Syn ("^c^")");*) unique)
walther@59956
   194
  | d_in (Typ _) = ((*tracing ("*** d_in: Typ ("^c^")");*) unique)
walther@59943
   195
  | d_in (Inc ((d, _), _)) = d
walther@59943
   196
  | d_in (Sup (d, _)) = d
walther@59943
   197
  | d_in (Mis (d, _)) = d
walther@59943
   198
  | d_in _ = raise ERROR "d_in: uncovered case in fun.def.";
walther@59943
   199
walther@59943
   200
fun dts2str (d, ts) = pair2str (UnparseC.term d, UnparseC.terms ts);
walther@59956
   201
fun penvval_in (Cor ((d, _), (_, ts))) = [Input_Descript.join'''' (d, ts)]
walther@59956
   202
  | penvval_in (Syn  (_)) = ((*tracing("*** penvval_in: Syn ("^c^")");*) [])
walther@59956
   203
  | penvval_in (Typ  (_)) = ((*tracing("*** penvval_in: Typ ("^c^")");*) [])
walther@59943
   204
  | penvval_in (Inc (_, (_, ts))) = ts
walther@59956
   205
  | penvval_in (Sup _) = ((*tracing ("*** penvval_in: Sup " ^ dts2str dts);*) [])
walther@59956
   206
  | penvval_in (Mis (_, _)) = ((*tracing ("*** penvval_in: Mis " ^
walther@59943
   207
			pair2str(UnparseC.term d, UnparseC.term t));*) [])
walther@59962
   208
	| penvval_in _ = raise ERROR "penvval_in: uncovered case in fun.def.";
walther@59943
   209
walther@59943
   210
fun vars_of itms = itms |> mk_env |> map snd
walther@59943
   211
walther@60339
   212
val dsc_unknown = TermC.parseNEW'' @{theory} "unknown::'a => unknow";
walther@59956
   213
walther@59992
   214
(* get a term from ori, notyet input in itm.
walther@59992
   215
   the term from ori is thrown back to a string in order to reuse
walther@59992
   216
   machinery for immediate input by the user. *)
walther@59992
   217
fun geti_ct thy (_, _, _, d, ts) (_, _, _, fd, itm_) =
walther@59992
   218
  (fd, ((UnparseC.term_in_thy thy) o Input_Descript.join) (d, subtract op = (ts_in itm_) ts))
walther@59992
   219
walther@59956
   220
(* 9.5.03 penv postponed: pbl_ids' *)
walther@59956
   221
fun pbl_ids' d vs = [Input_Descript.join'''' (d, vs)];
walther@59956
   222
walther@59956
   223
(* update the itm_ already input, all..from ori *)
walther@59956
   224
fun ori_2itm itm_ pid all (id, vt, fd, d, ts) = 
walther@59956
   225
  let 
walther@59956
   226
    val ts' = union op = (ts_in itm_) ts;
walther@59956
   227
    val pval =pbl_ids' d ts'
walther@59956
   228
	  (* WN.9.5.03: FIXXXME [#0, epsilon] here would upd_penv be called for [#0, epsilon] etc *)
walther@59956
   229
    val complete = if eq_set op = (ts', all) then true else false
walther@59956
   230
  in
walther@59956
   231
    case itm_ of
walther@59956
   232
      (Cor _) => 
walther@59956
   233
        (if fd = "#undef" then (id, vt, complete, fd, Sup (d, ts')) 
walther@59956
   234
	       else (id, vt, complete, fd, Cor ((d, ts'), (pid, pval))))
walther@59962
   235
    | (Syn c) => raise ERROR ("ori_2itm wants to overwrite " ^ c)
walther@59962
   236
    | (Typ c) => raise ERROR ("ori_2itm wants to overwrite " ^ c)
walther@59956
   237
    | (Inc _) =>
walther@59956
   238
      if complete
walther@59956
   239
  	  then (id, vt, true, fd, Cor ((d, ts'), (pid, pval)))
walther@59956
   240
  	  else (id, vt, false, fd, Inc ((d, ts'), (pid, pval)))
walther@59956
   241
    | (Sup (d,ts')) => (*4.9.01 lost env*)
walther@59956
   242
  	  (*if fd = "#undef" then*) (id,vt,complete,fd,Sup(d,ts'))
walther@59956
   243
  	  (*else (id,vt,complete,fd,Cor((d,ts'),e))*)
walther@59956
   244
      (* 28.1.00: not completely clear ---^^^ etc.*)
walther@59956
   245
    | (Mis _) => (* 4.9.01: Mis just copied *)
walther@59956
   246
       if complete
walther@59956
   247
  		 then (id, vt, true, fd, Cor ((d,ts'), (pid, pval)))
walther@59956
   248
  		 else (id, vt, false, fd, Inc ((d,ts'), (pid, pval)))
walther@59998
   249
    | i => raise ERROR ("ori_2itm: uncovered case of " ^ feedback_to_string' i)
walther@59956
   250
  end
walther@59956
   251
walther@59956
   252
fun eq1 d (_, (d', _)) = (d = d')
walther@59956
   253
fun eq3 f d (_, _, _, f', itm_) = f = f' andalso d = (d_in itm_) 
walther@59956
   254
walther@59956
   255
fun is_notyet_input ctxt itms all (i, v, f, d, ts) pbt =
walther@59956
   256
  case find_first (eq1 d) pbt of
walther@59956
   257
    SOME (_, (_, pid)) =>
walther@59956
   258
      (case find_first (eq3 f d) itms of
walther@59998
   259
        SOME (_, _, _, _, itm_) =>
walther@59956
   260
          let val ts' = inter op = (ts_in itm_) ts
walther@59956
   261
          in 
walther@59956
   262
            if subset op = (ts, ts') 
walther@59998
   263
            then (((strs2str' o map (UnparseC.term_in_ctxt ctxt)) ts') ^ " already input", empty)
walther@59998
   264
	          else ("", ori_2itm itm_ pid all (i, v, f, d, subtract op = ts' ts))
walther@59956
   265
	          end
walther@59998
   266
	    | NONE => ("", ori_2itm (Inc ((TermC.empty, []), (pid, []))) pid all (i, v, f, d, ts)))
walther@59956
   267
  | NONE => ("", ori_2itm (Sup (d, ts)) TermC.empty all (i, v, f, d, ts))
walther@59956
   268
walther@59956
   269
datatype additm =
walther@59958
   270
	Add of single   (* return-value of check_single *)
walther@59998
   271
| Err of string   (* error-message                *)
walther@59956
   272
walther@59956
   273
(*
walther@59956
   274
  Create feedback for input of TermC.as_string to m_field;
walther@59956
   275
  check w.r.t. O_Model.T and Model_Pattern.T.
walther@59998
   276
  In case of O_Model.T = [] (i.e. no data for user-guidance in Formalise.T)
walther@59958
   277
  check_single is extremely permissive.
walther@59956
   278
*)
walther@59958
   279
fun check_single ctxt m_field [] i_model m_patt ct =
walther@59956
   280
      let
walther@59956
   281
        val i = 1 + (if i_model = [] then 0 else map #1 i_model |> maxl)
walther@59956
   282
      in 
walther@59956
   283
        case TermC.parseNEW ctxt ct of
walther@59956
   284
          NONE => Add (i, [], false, m_field, Syn ct)
walther@59956
   285
        | SOME t =>
walther@59956
   286
            let
walther@59956
   287
              val (d, ts) = Input_Descript.split t
walther@59956
   288
            in 
walther@59956
   289
              if d = TermC.empty then
walther@59956
   290
                Add (i, [], false, m_field, Mis (dsc_unknown, hd ts)) 
walther@59956
   291
              else
walther@59956
   292
                (case find_first (eq1 d) m_patt of
walther@59956
   293
                  NONE => Add (i, [], true, m_field, Sup (d,ts))
walther@59956
   294
                | SOME (f, (_, id)) =>
walther@59956
   295
                    let
walther@59956
   296
                      fun eq2 d (i, _, _, _, itm_) = d = (d_in itm_) andalso i <> 0
walther@59956
   297
                    in
walther@59956
   298
                      case find_first (eq2 d) i_model of
walther@59956
   299
                        NONE => Add (i, [], true, f,Cor ((d, ts), (id, pbl_ids' d ts)))
walther@59956
   300
                      | SOME (i', _, _, _, itm_) => 
walther@59956
   301
                          if Input_Descript.for_list d then 
walther@59956
   302
                            let
walther@59956
   303
                              val in_itm = ts_in itm_
walther@59956
   304
                              val ts' = union op = ts in_itm
walther@59956
   305
                              val i'' = if in_itm = [] then i else i'
walther@59956
   306
                            in Add (i'', [], true, f, Cor ((d, ts'), (id, pbl_ids' d ts')))end
walther@59956
   307
                          else Add (i', [], true, f, Cor ((d, ts), (id, pbl_ids' d ts)))
walther@59956
   308
                    end)
walther@59956
   309
            end
walther@59956
   310
      end
walther@60154
   311
      (* for MethodC:  #undef  completed vvvvv vvvvvv term_as_string *)
walther@60011
   312
  | check_single ctxt m_field o_model i_model m_patt str =
walther@59956
   313
      case TermC.parseNEW ctxt str of
walther@60011
   314
         NONE => Err ("ERROR I_Model.check_single: syntax error in \"" ^ str ^ "\"")
walther@60011
   315
      | SOME (t as (descriptor $ _)) => 
walther@60011
   316
        (case Model_Pattern.get_field descriptor m_patt of
walther@60011
   317
          NONE => Err ("ERROR I_Model.check_single: unknown descriptor \"" ^
walther@60011
   318
            UnparseC.term_in_ctxt ctxt descriptor ^ "\"")
walther@60011
   319
        | SOME m_field' => 
walther@60011
   320
          if m_field <> m_field' then
walther@60011
   321
            Err ("ERROR I_Model.check_single: \"" ^ UnparseC.term_in_ctxt ctxt t ^ "\"" ^
walther@60011
   322
             "\" not for field \"" ^ m_field ^ "\"")
walther@60011
   323
          else
walther@60011
   324
            case O_Model.is_known ctxt m_field o_model t of
walther@60011
   325
              ("", ori', all) => 
walther@60011
   326
                (case is_notyet_input ctxt i_model all ori' m_patt of
walther@60011
   327
                   ("", itm) => Add itm
walther@60011
   328
                 | (msg, _) => Err ("ERROR I_Model.check_single: is_notyet_input: " ^ msg))
walther@60011
   329
            | (msg, _, _) => Err ("ERROR I_Model.check_single: is_known: " ^ msg))
walther@60011
   330
      | SOME t => Err ("ERROR I_Model.check_single: impossible for input \"" ^
walther@60011
   331
          UnparseC.term_in_ctxt ctxt t ^ "\"");
walther@60011
   332
      
walther@59958
   333
walther@59958
   334
(** add input **)
walther@59958
   335
walther@59958
   336
fun overwrite_ppc thy itm ppc =
walther@59958
   337
  let 
walther@59958
   338
    fun repl _ (_, _, _, _, itm_) [] =
walther@59962
   339
        raise ERROR ("overwrite_ppc: " ^ (feedback_to_string (ThyC.to_ctxt thy) itm_) ^ " not found")
walther@59958
   340
      | repl ppc' itm (p :: ppc) =
walther@59958
   341
	      if (#1 itm) = (#1 p)
walther@59958
   342
	      then ppc' @ [itm] @ ppc
walther@59958
   343
	      else repl (ppc' @ [p]) itm ppc
walther@59958
   344
  in repl [] itm ppc end
walther@59958
   345
walther@59958
   346
(* find_first item with #1 equal to id *)
walther@59958
   347
fun seek_ppc _ [] = NONE
walther@59958
   348
  | seek_ppc id (p :: ppc) = if id = #1 (p: single) then SOME p else seek_ppc id ppc
walther@59958
   349
walther@59958
   350
(* 10.3.00: insert the already compiled itm into model;
walther@59958
   351
   ev. filter_out  untouched (in FE: (0,...)) item related to insert-item *)
walther@59958
   352
fun add_single thy itm ppc =
walther@59958
   353
  let 
walther@59958
   354
    fun eq_untouched d (0, _, _, _, itm_) = (d = d_in itm_)
walther@59958
   355
      | eq_untouched _ _ = false
walther@59958
   356
    val ppc' = case seek_ppc (#1 itm) ppc of
walther@59958
   357
      SOME _ => overwrite_ppc thy itm ppc (*itm updated in is_notyet_input WN.11.03*)
walther@59958
   358
    | NONE => (ppc @ [itm])
walther@59958
   359
  in filter_out (eq_untouched ((d_in o #5) itm)) ppc' end
walther@59956
   360
walther@59988
   361
fun complete' pbt (i, v, f, d, ts) =
walther@60265
   362
  case \<^try>\<open> (i, v, true, f, Cor ((d, ts), ((snd o snd o the o (find_first (eq1 d))) pbt, ts)))\<close> of
walther@60265
   363
    SOME x => x
walther@60265
   364
  | NONE => (i, v, true, f, Cor ((d, ts), (d, ts)))
walther@60265
   365
    (*descriptor in O_Model, but not in Problem_Pattern.T, thus keep this descriptor*)
walther@59988
   366
walther@59988
   367
(* filter out oris which have same description in itms *)
walther@59988
   368
fun filter_outs oris [] = oris
walther@59988
   369
  | filter_outs oris (i::itms) = 
walther@59988
   370
    let
walther@59988
   371
      val ors = filter_out ((curry op = ((d_in o #5) i)) o (#4)) oris
walther@59988
   372
    in
walther@59988
   373
      filter_outs ors itms
walther@59988
   374
    end
walther@59988
   375
walther@59988
   376
(* filter oris which are in pbt, too *)
walther@59988
   377
fun filter_pbt oris pbt =
walther@59988
   378
  let
walther@59988
   379
    val dscs = map (fst o snd) pbt
walther@59988
   380
  in
walther@59988
   381
    filter ((member op= dscs) o (#4)) oris
walther@59988
   382
  end
walther@59988
   383
walther@59988
   384
fun is_error (Cor _) = false
walther@59988
   385
  | is_error (Sup _) = false
walther@59988
   386
  | is_error (Inc _) = false
walther@59988
   387
  | is_error (Mis _) = false
walther@59988
   388
  | is_error _ = true
walther@59988
   389
walther@59988
   390
(*create output-string for itm_*)
walther@59988
   391
fun to_p_model _ (Cor ((d, ts), _)) = UnparseC.term (Input_Descript.join (d, ts))
walther@59988
   392
  | to_p_model _ (Syn c) = c
walther@59988
   393
  | to_p_model _ (Typ c) = c
walther@59988
   394
  | to_p_model _ (Inc ((d, ts), _)) = UnparseC.term (Input_Descript.join (d, ts))
walther@59988
   395
  | to_p_model _ (Sup (d, ts)) = UnparseC.term (Input_Descript.join (d, ts))
walther@59988
   396
  | to_p_model _ (Mis (d, pid)) = UnparseC.term d ^ " " ^ UnparseC.term pid
walther@59988
   397
  | to_p_model _ _ = raise ERROR "to_p_model: uncovered definition"
walther@59988
   398
walther@59988
   399
fun eq_dsc ((_, _, _, _, itm_), (_, _, _, _, iitm_)) = (d_in itm_ = d_in iitm_)
walther@59988
   400
walther@59988
   401
(* insert_ppc = add for appl_add', input_icalhd 11.03,
walther@59988
   402
   handles superfluous items carelessly                       *)
walther@59988
   403
fun add itm itms = if member eq_dsc itms itm then itms else itms @ [itm] (* @ new itm *)
walther@59988
   404
walther@59988
   405
(* combine itms from pbl + met and complete them wrt. pbt *)
walther@59988
   406
(* FIXXXME.WN031205 complete doesnt handle incorrect itms !*)
walther@59988
   407
fun complete oris pits mits met = 
walther@59988
   408
  let
walther@59988
   409
    val vat = max_vt pits;
walther@59988
   410
    val itms = pits @ (filter ((member_swap op = vat) o (#2 )) mits)
walther@59988
   411
    val ors = filter ((member_swap op= vat) o (#2)) oris
walther@59988
   412
    val os = filter_outs ors itms (*WN.12.03?: does _NOT_ add itms from met ?!*)
walther@59988
   413
  in
walther@59988
   414
    itms @ (map (complete' met) os)
walther@59988
   415
  end
walther@59988
   416
walther@59988
   417
(* complete model and guard of a calc-head *)
walther@59988
   418
fun complete_method (oris, mpc, ppc, probl) =
walther@59988
   419
  let
walther@59988
   420
    val pits = filter_out ((curry op= false) o (#3)) probl
walther@59988
   421
    val vat = if probl = [] then 1 else max_vt probl
walther@59988
   422
    val pors = filter ((member_swap op = vat) o (#2)) oris
walther@59988
   423
    val pors = filter_outs pors pits (*which are in pbl already*)
walther@59988
   424
    val pors = (filter_pbt pors ppc) (*which are in pbt, too*)
walther@59988
   425
    val pits = pits @ (map (complete' ppc) pors)
walther@59988
   426
    val mits = complete oris pits [] mpc
walther@59988
   427
  in (pits, mits) end
walther@59988
   428
walther@59988
   429
(* WN0312: use in nxt_spec, too ? what about variants ??? *)
walther@59998
   430
fun is_complete ([]: T) = false
walther@59988
   431
  | is_complete itms = foldl and_ (true, (map #3 itms))
walther@59986
   432
walther@60126
   433
(**)end(**);