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