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