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