src/Tools/isac/Specify/i-model.sml
author Walther Neuper <walther.neuper@jku.at>
Mon, 11 May 2020 11:22:46 +0200
changeset 59961 d9b2994fcce2
parent 59960 d0637de46bfa
child 59962 6a59d252345d
permissions -rw-r--r--
cover types from struct. *_Def
walther@59938
     1
(* Title:  Specify/i-model.sml
walther@59938
     2
   Author: Walther Neuper 110226
walther@59938
     3
   (c) due to copyright terms
walther@59938
     4
 *)
walther@59938
     5
walther@59938
     6
signature MODEL_FOR_INTERACTION =
walther@59938
     7
sig
walther@59942
     8
(**)
walther@59961
     9
  type T
walther@59961
    10
  type single
walther@59948
    11
  val empty: single 
walther@59960
    12
  type variants
walther@59961
    13
  type m_field
walther@59961
    14
  type descriptor
walther@59948
    15
  datatype feedback = datatype Model_Def.i_model_feedback
walther@59938
    16
walther@59942
    17
  val feedback_to_string': feedback -> string
walther@59942
    18
  val feedback_to_string: Proof.context -> feedback -> string
walther@59942
    19
  val single_to_string: Proof.context -> single -> string
walther@59942
    20
  val to_string: Proof.context -> T -> string
walther@59942
    21
walther@59958
    22
walther@59956
    23
  datatype additm = Add of single | Err of string
walther@59958
    24
  val init: Model_Pattern.T -> T
walther@59958
    25
  val check_single: Proof.context -> m_field -> O_Model.T ->
walther@59956
    26
    T -> Model_Pattern.T -> TermC.as_string -> additm
walther@59958
    27
  val add_single: theory -> single -> T -> T
walther@59956
    28
walther@59956
    29
  val d_in: feedback -> term
walther@59956
    30
  val ts_in: feedback -> term list
walther@59956
    31
  val vars_of: T -> term list
walther@59956
    32
  val max_vt: T -> int
walther@59958
    33
  val is_known: Proof.context -> string -> O_Model.T -> term -> string * O_Model.single * term list
walther@59958
    34
  val is_notyet_input : Proof.context -> T -> term list -> O_Model.single -> ('a * (term * term)) list
walther@59958
    35
    -> string * single
walther@59958
    36
  val dsc_unknown: term
walther@59948
    37
walther@59956
    38
  val mk_env: T -> (term * term) list
walther@59956
    39
  val penvval_in: feedback -> term list
walther@59956
    40
walther@59956
    41
  val pbl_ids': term -> term list -> term list
walther@59956
    42
walther@59956
    43
walther@59938
    44
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
walther@59956
    45
  val dts2str: term * (term list) -> string
walther@59956
    46
  val eq1: ''a -> 'b * (''a * 'c) -> bool
walther@59938
    47
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
walther@59958
    48
  val vts_in: T -> int list
walther@59956
    49
  val seek_oridts: Proof.context -> ''a -> term * term list -> (int * 'b list * ''a * term * term list) list -> string * (int * 'b list * ''a * term * term list) * term list
walther@59938
    50
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
walther@59938
    51
end
walther@59938
    52
walther@59942
    53
(**)
walther@59938
    54
structure I_Model(**) : MODEL_FOR_INTERACTION(**) =
walther@59938
    55
struct
walther@59942
    56
(**)
walther@59955
    57
walther@59958
    58
(** data types **)
walther@59958
    59
walther@59940
    60
type variants =  Model_Def.variants;
walther@59952
    61
type m_field = Model_Def.m_field;
walther@59952
    62
type descriptor = Model_Def.descriptor;
walther@59938
    63
walther@59940
    64
type T = Model_Def.i_model_single list;
walther@59940
    65
datatype feedback = datatype Model_Def.i_model_feedback;
walther@59940
    66
type single = Model_Def.i_model_single;
walther@59940
    67
val empty = Model_Def.i_model_empty;
walther@59938
    68
walther@59942
    69
fun pen2str ctxt (t, ts) =
walther@59942
    70
  pair2str (UnparseC.term_in_ctxt ctxt t, (strs2str' o map (UnparseC.term_in_ctxt  ctxt)) ts);
walther@59948
    71
walther@59942
    72
fun feedback_to_string ctxt (Cor ((d, ts), penv)) = 
walther@59953
    73
    "Cor " ^ UnparseC.term_in_ctxt  ctxt (Input_Descript.join (d, ts)) ^ " ," ^ pen2str ctxt penv
walther@59942
    74
  | feedback_to_string _ (Syn c) = "Syn " ^ c
walther@59942
    75
  | feedback_to_string _ (Typ c) = "Typ " ^ c
walther@59942
    76
  | feedback_to_string ctxt (Inc ((d, ts), penv)) = 
walther@59953
    77
    "Inc " ^ UnparseC.term_in_ctxt  ctxt (Input_Descript.join (d, ts)) ^ " ," ^ pen2str ctxt penv
walther@59942
    78
  | feedback_to_string ctxt (Sup (d, ts)) = 
walther@59953
    79
    "Sup " ^ UnparseC.term_in_ctxt  ctxt (Input_Descript.join (d, ts))
walther@59942
    80
  | feedback_to_string ctxt (Mis (d, pid)) = 
walther@59942
    81
    "Mis "^ UnparseC.term_in_ctxt  ctxt d ^ " " ^ UnparseC.term_in_ctxt  ctxt pid
walther@59942
    82
  | feedback_to_string _ (Par s) = "Trm "^s;
walther@59942
    83
fun feedback_to_string' t = feedback_to_string (ThyC.id_to_ctxt "Isac_Knowledge") t;
walther@59942
    84
walther@59942
    85
fun single_to_string ctxt (i, is, b, s, itm_) = 
walther@59942
    86
  "(" ^ string_of_int i ^ " ," ^ ints2str' is ^ " ," ^ bool2str b ^ " ," ^
walther@59942
    87
  s ^ " ," ^ feedback_to_string ctxt itm_ ^ ")";
walther@59942
    88
fun to_string ctxt itms = strs2str' (map (linefeed o (single_to_string ctxt)) itms);
walther@59942
    89
walther@59958
    90
walther@59958
    91
(** initialise a model **)
walther@59958
    92
walther@59958
    93
fun init pbt = 
walther@59958
    94
  let
walther@59958
    95
    fun pbt2itm (f, (d, _)) = (0, [], false, f, Inc ((d, []), (TermC.empty, [])))
walther@59958
    96
  in map pbt2itm pbt end
walther@59958
    97
walther@59958
    98
walther@59958
    99
(** check input on a model **)
walther@59942
   100
walther@59943
   101
(* find most frequent variant v in itms *)
walther@59943
   102
fun vts_in itms = (distinct o flat o (map #2)) (itms: T);
walther@59943
   103
walther@59943
   104
fun cnt itms v = (v, (length o (filter (curry op = v)) o flat o (map #2)) itms);
walther@59943
   105
fun vts_cnt vts itms = map (cnt itms) vts;
walther@59943
   106
fun max2 [] = error "max2 of []"
walther@59943
   107
  | max2 (y :: ys) =
walther@59943
   108
    let
walther@59943
   109
      fun mx (a,x) [] = (a,x)
walther@59943
   110
  	    | mx (a, x) ((b,y) :: ys) = if x < y then mx (b, y) ys else mx (a, x) ys;
walther@59943
   111
    in mx y ys end;
walther@59943
   112
walther@59943
   113
(* get the constant value from a penv *)
walther@59943
   114
fun getval (id, values) = 
walther@59943
   115
  case values of
walther@59943
   116
	  [] => error ("penv_value: no values in '" ^ UnparseC.term id)
walther@59943
   117
  | [v] => (id, v)
walther@59943
   118
  | (v1 :: v2 :: _) => (case v1 of 
walther@59943
   119
	      Const ("Program.Arbfix",_) => (id, v2)
walther@59943
   120
	    | _ => (id, v1));
walther@59943
   121
walther@59943
   122
(* find the variant with most items already input *)
walther@59943
   123
fun max_vt itms = 
walther@59943
   124
    let val vts = (vts_cnt (vts_in itms)) itms;
walther@59943
   125
    in if vts = [] then 0 else (fst o max2) vts end;
walther@59943
   126
walther@59943
   127
(* TODO ev. make more efficient by avoiding flat *)
walther@59943
   128
fun mk_e (Cor (_, iv)) = [getval iv]
walther@59943
   129
  | mk_e (Syn _) = []
walther@59943
   130
  | mk_e (Typ _) = [] 
walther@59943
   131
  | mk_e (Inc (_, iv)) = [getval iv]
walther@59943
   132
  | mk_e (Sup _) = []
walther@59943
   133
  | mk_e (Mis _) = []
walther@59943
   134
  | mk_e  _ = error "mk_e: uncovered case in fun.def.";
walther@59943
   135
fun mk_en vt (_, vts, _, _, itm_) = if member op = vts vt then mk_e itm_ else [];
walther@59943
   136
walther@59943
   137
(* extract the environment from an item list; takes the variant with most items *)
walther@59943
   138
fun mk_env itms = 
walther@59943
   139
  let val vt = max_vt itms
walther@59943
   140
  in (flat o (map (mk_en vt))) itms end;
walther@59943
   141
(**)
walther@59943
   142
fun ts_in (Cor ((_, ts), _)) = ts
walther@59943
   143
  | ts_in (Syn _) = []
walther@59943
   144
  | ts_in (Typ _) = []
walther@59943
   145
  | ts_in (Inc ((_, ts), _)) = ts
walther@59943
   146
  | ts_in (Sup (_, ts)) = ts
walther@59943
   147
  | ts_in (Mis _) = []
walther@59943
   148
  | ts_in _ = error "ts_in: uncovered case in fun.def.";
walther@59943
   149
walther@59943
   150
val unique = (Thm.term_of o the o (TermC.parse @{theory "Real"} )) "UnIqE_tErM";
walther@59943
   151
fun d_in (Cor ((d ,_), _)) = d
walther@59956
   152
  | d_in (Syn _) = ((*tracing ("*** d_in: Syn ("^c^")");*) unique)
walther@59956
   153
  | d_in (Typ _) = ((*tracing ("*** d_in: Typ ("^c^")");*) unique)
walther@59943
   154
  | d_in (Inc ((d, _), _)) = d
walther@59943
   155
  | d_in (Sup (d, _)) = d
walther@59943
   156
  | d_in (Mis (d, _)) = d
walther@59943
   157
  | d_in _ = raise ERROR "d_in: uncovered case in fun.def.";
walther@59943
   158
walther@59943
   159
fun dts2str (d, ts) = pair2str (UnparseC.term d, UnparseC.terms ts);
walther@59956
   160
fun penvval_in (Cor ((d, _), (_, ts))) = [Input_Descript.join'''' (d, ts)]
walther@59956
   161
  | penvval_in (Syn  (_)) = ((*tracing("*** penvval_in: Syn ("^c^")");*) [])
walther@59956
   162
  | penvval_in (Typ  (_)) = ((*tracing("*** penvval_in: Typ ("^c^")");*) [])
walther@59943
   163
  | penvval_in (Inc (_, (_, ts))) = ts
walther@59956
   164
  | penvval_in (Sup _) = ((*tracing ("*** penvval_in: Sup " ^ dts2str dts);*) [])
walther@59956
   165
  | penvval_in (Mis (_, _)) = ((*tracing ("*** penvval_in: Mis " ^
walther@59943
   166
			pair2str(UnparseC.term d, UnparseC.term t));*) [])
walther@59943
   167
	| penvval_in _ = error "penvval_in: uncovered case in fun.def.";
walther@59943
   168
walther@59943
   169
fun vars_of itms = itms |> mk_env |> map snd
walther@59943
   170
walther@59956
   171
fun seek_oridts ctxt sel (d, ts) [] =
walther@59956
   172
    ("seek_oridts: input ('" ^
walther@59956
   173
        (UnparseC.term_in_ctxt ctxt (Input_Descript.join (d, ts))) ^ "') not found in oris (typed)",
walther@59956
   174
      (0, [], sel, d, ts),
walther@59956
   175
      [])
walther@59956
   176
  | seek_oridts ctxt sel (d, ts) ((id, vat, sel', d', ts') :: oris) =
walther@59956
   177
    if sel = sel' andalso d = d' andalso (inter op = ts ts') <> []
walther@59956
   178
    then ("", (id, vat, sel, d, inter op = ts ts'), ts')
walther@59956
   179
    else seek_oridts ctxt sel (d, ts) oris
walther@59956
   180
walther@59956
   181
fun test_types ctxt (d,ts) =
walther@59956
   182
  let 
walther@59956
   183
    val opt = (try Input_Descript.join) (d, ts)
walther@59956
   184
    val msg = case opt of 
walther@59956
   185
      SOME _ => "" 
walther@59956
   186
    | NONE => (UnparseC.term_in_ctxt ctxt d ^ " " ^
walther@59956
   187
	    (strs2str' o map (UnparseC.term_in_ctxt ctxt)) ts ^ " is illtyped")
walther@59956
   188
  in msg end
walther@59956
   189
walther@59956
   190
(* to an input (_,ts) find the according ori and insert the ts *)
walther@59956
   191
fun seek_orits ctxt _ ts [] = 
walther@59956
   192
    ("seek_orits: input (_, '" ^ strs2str (map (UnparseC.term_in_ctxt ctxt) ts) ^
walther@59956
   193
    "') not found in oris (typed)", O_Model.single_empty, [])
walther@59956
   194
  | seek_orits ctxt sel ts ((id, vat, sel', d, ts') :: oris) =
walther@59956
   195
    if sel = sel' andalso (inter op = ts ts') <> [] 
walther@59956
   196
    then
walther@59956
   197
      if sel = sel' 
walther@59956
   198
      then ("", (id, vat, sel, d, inter op = ts ts'), ts')
walther@59956
   199
      else (((strs2str' o map (UnparseC.term_in_ctxt ctxt)) ts) ^ " not for " ^ sel, O_Model.single_empty, [])
walther@59956
   200
    else seek_orits ctxt sel ts oris
walther@59956
   201
walther@59956
   202
(* make a term 'typeless' for comparing with another 'typeless' term;
walther@59956
   203
   'type-less' usually is illtyped                                  *)
walther@59956
   204
fun typeless (Const (s, _)) = (Const (s, TermC.typ_empty)) 
walther@59956
   205
  | typeless (Free (s, _)) = (Free (s, TermC.typ_empty))
walther@59956
   206
  | typeless (Var (n, _)) = (Var (n, TermC.typ_empty))
walther@59956
   207
  | typeless (Bound i) = (Bound i)
walther@59956
   208
  | typeless (Abs (s, _,t)) = Abs(s, TermC.typ_empty, typeless t)
walther@59956
   209
  | typeless (t1 $ t2) = (typeless t1) $ (typeless t2)
walther@59956
   210
walther@59956
   211
(* is the term t input (or taken from fmz) known in oris ?
walther@59956
   212
   give feedback on all(?) strange input;
walther@59956
   213
   return _all_ terms already input to this item (e.g. valuesFor a,b) *)
walther@59956
   214
fun is_known ctxt sel ori t =
walther@59956
   215
  let
walther@59956
   216
    val ots = (distinct o flat o (map #5)) ori
walther@59956
   217
    val oids = ((map (fst o dest_Free)) o distinct o flat o (map TermC.vars)) ots
walther@59956
   218
    val (d, ts) = Input_Descript.split t
walther@59956
   219
    val ids = map (fst o dest_Free) ((distinct o (flat o (map TermC.vars))) ts)
walther@59956
   220
  in
walther@59956
   221
    if (subtract op = oids ids) <> []
walther@59956
   222
    then ("identifiers " ^ strs2str' (subtract op = oids ids) ^ " not in example", O_Model.single_empty, [])
walther@59956
   223
    else 
walther@59956
   224
	    if d = TermC.empty 
walther@59956
   225
	    then 
walther@59956
   226
	      if not (subset op = (map typeless ts, map typeless ots))
walther@59956
   227
	      then ("terms '" ^ (strs2str' o (map (UnparseC.term_in_ctxt ctxt))) ts ^
walther@59956
   228
		      "' not in example (typeless)", O_Model.single_empty, [])
walther@59956
   229
	      else 
walther@59956
   230
          (case seek_orits ctxt sel ts ori of
walther@59956
   231
		         ("", ori_ as (_,_,_,d,ts), all) =>
walther@59956
   232
		            (case test_types ctxt (d,ts) of
walther@59956
   233
		              "" => ("", ori_, all)
walther@59956
   234
		            | msg => (msg, O_Model.single_empty, []))
walther@59956
   235
		       | (msg, _, _) => (msg, O_Model.single_empty, []))
walther@59956
   236
	    else 
walther@59956
   237
	      if member op = (map #4 ori) d
walther@59956
   238
	      then seek_oridts ctxt sel (d, ts) ori
walther@59956
   239
	      else (UnparseC.term_in_ctxt ctxt d ^ " not in example", (0, [], sel, d, ts), [])
walther@59956
   240
  end
walther@59956
   241
walther@59956
   242
val dsc_unknown = (Thm.term_of o the o (TermC.parseold @{theory})) "unknown::'a => unknow";
walther@59956
   243
walther@59956
   244
(* 9.5.03 penv postponed: pbl_ids' *)
walther@59956
   245
fun pbl_ids' d vs = [Input_Descript.join'''' (d, vs)];
walther@59956
   246
walther@59956
   247
(* update the itm_ already input, all..from ori *)
walther@59956
   248
fun ori_2itm itm_ pid all (id, vt, fd, d, ts) = 
walther@59956
   249
  let 
walther@59956
   250
    val ts' = union op = (ts_in itm_) ts;
walther@59956
   251
    val pval =pbl_ids' d ts'
walther@59956
   252
	  (* WN.9.5.03: FIXXXME [#0, epsilon] here would upd_penv be called for [#0, epsilon] etc *)
walther@59956
   253
    val complete = if eq_set op = (ts', all) then true else false
walther@59956
   254
  in
walther@59956
   255
    case itm_ of
walther@59956
   256
      (Cor _) => 
walther@59956
   257
        (if fd = "#undef" then (id, vt, complete, fd, Sup (d, ts')) 
walther@59956
   258
	       else (id, vt, complete, fd, Cor ((d, ts'), (pid, pval))))
walther@59956
   259
    | (Syn c) => error ("ori_2itm wants to overwrite " ^ c)
walther@59956
   260
    | (Typ c) => error ("ori_2itm wants to overwrite " ^ c)
walther@59956
   261
    | (Inc _) =>
walther@59956
   262
      if complete
walther@59956
   263
  	  then (id, vt, true, fd, Cor ((d, ts'), (pid, pval)))
walther@59956
   264
  	  else (id, vt, false, fd, Inc ((d, ts'), (pid, pval)))
walther@59956
   265
    | (Sup (d,ts')) => (*4.9.01 lost env*)
walther@59956
   266
  	  (*if fd = "#undef" then*) (id,vt,complete,fd,Sup(d,ts'))
walther@59956
   267
  	  (*else (id,vt,complete,fd,Cor((d,ts'),e))*)
walther@59956
   268
      (* 28.1.00: not completely clear ---^^^ etc.*)
walther@59956
   269
    | (Mis _) => (* 4.9.01: Mis just copied *)
walther@59956
   270
       if complete
walther@59956
   271
  		 then (id, vt, true, fd, Cor ((d,ts'), (pid, pval)))
walther@59956
   272
  		 else (id, vt, false, fd, Inc ((d,ts'), (pid, pval)))
walther@59956
   273
    | i => error ("ori_2itm: uncovered case of "^ feedback_to_string' i)
walther@59956
   274
  end
walther@59956
   275
walther@59956
   276
fun eq1 d (_, (d', _)) = (d = d')
walther@59956
   277
fun eq3 f d (_, _, _, f', itm_) = f = f' andalso d = (d_in itm_) 
walther@59956
   278
walther@59956
   279
fun is_notyet_input ctxt itms all (i, v, f, d, ts) pbt =
walther@59956
   280
  case find_first (eq1 d) pbt of
walther@59956
   281
    SOME (_, (_, pid)) =>
walther@59956
   282
      (case find_first (eq3 f d) itms of
walther@59956
   283
        SOME (_,_,_,_,itm_) =>
walther@59956
   284
          let val ts' = inter op = (ts_in itm_) ts
walther@59956
   285
          in 
walther@59956
   286
            if subset op = (ts, ts') 
walther@59956
   287
            then (((strs2str' o map (UnparseC.term_in_ctxt ctxt)) ts') ^ " already input", empty) (*2*)
walther@59956
   288
	          else ("", ori_2itm itm_ pid all (i,v,f,d, subtract op = ts' ts))              (*3,4*)
walther@59956
   289
	          end
walther@59956
   290
	    | NONE => ("", ori_2itm (Inc ((TermC.empty, []), (pid, []))) pid all (i, v, f, d, ts)))    (*1*)
walther@59956
   291
  | NONE => ("", ori_2itm (Sup (d, ts)) TermC.empty all (i, v, f, d, ts))
walther@59956
   292
walther@59956
   293
datatype additm =
walther@59958
   294
	Add of single   (* return-value of check_single *)
walther@59956
   295
| Err of string       (* error-message            *)
walther@59956
   296
walther@59956
   297
(*
walther@59956
   298
  Create feedback for input of TermC.as_string to m_field;
walther@59956
   299
  check w.r.t. O_Model.T and Model_Pattern.T.
walther@59956
   300
  In case of O_Model.T = [] (i.e. no data for user-guidance from Formalise.T)
walther@59958
   301
  check_single is extremely permissive.
walther@59956
   302
*)
walther@59958
   303
fun check_single ctxt m_field [] i_model m_patt ct =
walther@59956
   304
      let
walther@59956
   305
        val i = 1 + (if i_model = [] then 0 else map #1 i_model |> maxl)
walther@59956
   306
      in 
walther@59956
   307
        case TermC.parseNEW ctxt ct of
walther@59956
   308
          NONE => Add (i, [], false, m_field, Syn ct)
walther@59956
   309
        | SOME t =>
walther@59956
   310
            let
walther@59956
   311
              val (d, ts) = Input_Descript.split t
walther@59956
   312
            in 
walther@59956
   313
              if d = TermC.empty then
walther@59956
   314
                Add (i, [], false, m_field, Mis (dsc_unknown, hd ts)) 
walther@59956
   315
              else
walther@59956
   316
                (case find_first (eq1 d) m_patt of
walther@59956
   317
                  NONE => Add (i, [], true, m_field, Sup (d,ts))
walther@59956
   318
                | SOME (f, (_, id)) =>
walther@59956
   319
                    let
walther@59956
   320
                      fun eq2 d (i, _, _, _, itm_) = d = (d_in itm_) andalso i <> 0
walther@59956
   321
                    in
walther@59956
   322
                      case find_first (eq2 d) i_model of
walther@59956
   323
                        NONE => Add (i, [], true, f,Cor ((d, ts), (id, pbl_ids' d ts)))
walther@59956
   324
                      | SOME (i', _, _, _, itm_) => 
walther@59956
   325
                          if Input_Descript.for_list d then 
walther@59956
   326
                            let
walther@59956
   327
                              val in_itm = ts_in itm_
walther@59956
   328
                              val ts' = union op = ts in_itm
walther@59956
   329
                              val i'' = if in_itm = [] then i else i'
walther@59956
   330
                            in Add (i'', [], true, f, Cor ((d, ts'), (id, pbl_ids' d ts')))end
walther@59956
   331
                          else Add (i', [], true, f, Cor ((d, ts), (id, pbl_ids' d ts)))
walther@59956
   332
                    end)
walther@59956
   333
            end
walther@59956
   334
      end
walther@59958
   335
  | check_single ctxt m_field o_model i_model m_patt str =
walther@59956
   336
      case TermC.parseNEW ctxt str of
walther@59958
   337
        NONE => Err ("check_single: syntax error in '" ^ str ^ "'")
walther@59956
   338
      | SOME t => 
walther@59956
   339
          case is_known ctxt m_field o_model t of
walther@59956
   340
            ("", ori', all) => 
walther@59956
   341
              (case is_notyet_input ctxt i_model all ori' m_patt of
walther@59956
   342
                 ("", itm) => Add itm
walther@59958
   343
               | (msg, _) => Err ("ERROR I_Model.check_single: is_notyet_input: " ^ msg))
walther@59958
   344
          | (msg, _, _) => Err ("ERROR I_Model.check_single: is_known: " ^ msg);
walther@59958
   345
walther@59958
   346
walther@59958
   347
(** add input **)
walther@59958
   348
walther@59958
   349
fun overwrite_ppc thy itm ppc =
walther@59958
   350
  let 
walther@59958
   351
    fun repl _ (_, _, _, _, itm_) [] =
walther@59958
   352
        error ("overwrite_ppc: " ^ (feedback_to_string (ThyC.to_ctxt thy) itm_) ^ " not found")
walther@59958
   353
      | repl ppc' itm (p :: ppc) =
walther@59958
   354
	      if (#1 itm) = (#1 p)
walther@59958
   355
	      then ppc' @ [itm] @ ppc
walther@59958
   356
	      else repl (ppc' @ [p]) itm ppc
walther@59958
   357
  in repl [] itm ppc end
walther@59958
   358
walther@59958
   359
(* find_first item with #1 equal to id *)
walther@59958
   360
fun seek_ppc _ [] = NONE
walther@59958
   361
  | seek_ppc id (p :: ppc) = if id = #1 (p: single) then SOME p else seek_ppc id ppc
walther@59958
   362
walther@59958
   363
(* 10.3.00: insert the already compiled itm into model;
walther@59958
   364
   ev. filter_out  untouched (in FE: (0,...)) item related to insert-item *)
walther@59958
   365
fun add_single thy itm ppc =
walther@59958
   366
  let 
walther@59958
   367
    fun eq_untouched d (0, _, _, _, itm_) = (d = d_in itm_)
walther@59958
   368
      | eq_untouched _ _ = false
walther@59958
   369
    val ppc' = case seek_ppc (#1 itm) ppc of
walther@59958
   370
      SOME _ => overwrite_ppc thy itm ppc (*itm updated in is_notyet_input WN.11.03*)
walther@59958
   371
    | NONE => (ppc @ [itm])
walther@59958
   372
  in filter_out (eq_untouched ((d_in o #5) itm)) ppc' end
walther@59956
   373
walther@59938
   374
(**)end(**);