src/Tools/isac/Specify/o-model.sml
author wneuper <walther.neuper@jku.at>
Thu, 29 Apr 2021 12:43:50 +0200
changeset 60268 637f20154de6
parent 60265 9c9d61fed195
child 60442 57fecc591856
permissions -rw-r--r--
eliminate warnings from src/*, part 2
     1 (* Title:  Specify/o-model.sml
     2    Author: Walther Neuper 110226
     3    (c) due to copyright terms
     4 
     5 An \<open>O_Model\<close> holds \<open>O\<close>riginal data, \<open>descriptor\<close> and \<open>values\<close> parsed from \<open>Formalise.model\<close>
     6 selected with respect to a \<open>Model_Pattern\<close> and combined with respective \<open>m_field\<close>s.
     7 \<open>complete_for\<close> a \<open>Model_Pattern\<close> is done by \<open>Tactic.Model_Problem\<close>
     8 (with defaults from \<open>Formalise\<close>refs), by \<open>Tactic.Specify_Problem\<close> and \<open>Tactic.Specify_Method\<close>
     9 
    10 \<open>O_Model\<close> serves efficiency of student's interactive modelling via \<open>I_Model\<close>.
    11 
    12 \<open>O_Model\<close> marks elements with  \<open>m_field\<close> \<open>#undef\<close>, which are not required by the \<open>Model_Pattern\<close>
    13 of the \<open>Problem\<close> or the \<open>MethodC\<close> .. TODO redes: \<open>m_field\<close> probably different in root..Subproblem
    14 
    15 TODO: revise with an example with more than 1 variant.
    16     + consider: drop m_field ?
    17 *)
    18 
    19 signature ORIGINAL_MODEL =
    20 sig
    21   type T
    22   type single
    23   type variant
    24   type variants
    25   type m_field
    26   type descriptor
    27   type values
    28   type message
    29   val to_string: T -> string
    30   val single_to_string: single -> string
    31   val single_empty: single
    32 
    33 (*val init: theory -> Formalise.model -> Model_Pattern.T -> T ..TODO*)
    34   val init: Formalise.model -> theory -> Model_Pattern.T -> T
    35   val values : T -> term list
    36   val values': T -> Formalise.model * term list
    37   val complete_for: Model_Pattern.T -> T -> T * Proof.context -> T * Proof.context
    38 (*/------- rename -------\*)
    39 (*val seek_oridts: Proof.context -> ?/////? -> descriptor * values -> T -> message * single * values*)
    40   val seek_oridts: Proof.context -> m_field -> descriptor * values -> T -> message * single * values
    41 (*val single_for_values:!!! values -> Proof.context -> m_field -> T -> message * single * values*)
    42   val seek_orits: Proof.context -> m_field -> values -> T -> message * single * values
    43 (*val ?contains:!!! term -> Proof.context -> m_field -> T -> message * single * values*)
    44   val is_known: Proof.context -> m_field -> T -> term -> message * single * values
    45 (*val typeless: term -> term*)
    46   val typeless: term -> term
    47 (*val test_types: Proof.context -> descriptor * values -> string*)
    48   val test_types: Proof.context -> descriptor * values -> string
    49 
    50 (*put add_id into a new auxiliary fun, see ONLY call..*)
    51   val add_id: 'a list -> (int * 'a) list
    52   type preori
    53 (*val cpy_nam: Model_Pattern.T -> preori list -> Model_Pattern.single -> preori*)
    54   val cpy_nam: Model_Pattern.T -> preori list -> Model_Pattern.single -> preori
    55   val is_copy_named: Model_Pattern.single -> bool
    56   val is_copy_named_idstr: string -> bool
    57 \<^isac_test>\<open>
    58   val is_copy_named_generating_idstr: string -> bool
    59 \<close>
    60   val is_copy_named_generating: Model_Pattern.single -> bool
    61 
    62 \<^isac_test>\<open>
    63   val preoris2str : preori list -> string
    64 \<close>
    65   val getr_ct: theory -> single -> m_field * UnparseC.term_as_string
    66 
    67 \<^isac_test>\<open>
    68   val add_field: theory -> Model_Pattern.T -> descriptor * values -> m_field * descriptor * values
    69   val add_variants: ('a * ''b * 'c) list -> (int * ('a * ''b * 'c)) list
    70 (*val max: variants -> int*)
    71   val max: variants -> int
    72 (*val coll_variants: ('a * ''b) list -> ('a list * ''b) list*)
    73   val coll_variants: ('a * ''b) list -> ('a list * ''b) list
    74 (*val replace_0: int -> int list -> int list*)
    75   val replace_0: int -> int list -> int list
    76   val flattup: 'a * ('b * ('c * 'd * 'e)) -> 'a * 'b * 'c * 'd * 'e
    77 (*val mark: ('a * 'a -> bool) -> 'a list -> (int * 'a) list*)
    78   val mark: ('a * 'a -> bool) -> 'a list -> (int * 'a) list
    79 (*\------- rename -------/*)
    80 \<close>
    81 end
    82 
    83 (**)
    84 structure O_Model(**) : ORIGINAL_MODEL(**) =
    85 struct
    86 (**)
    87 
    88 (** types **)
    89 
    90 type variant =  Model_Def.variant;
    91 type variants =  Model_Def.variants;
    92 type m_field = Model_Def.m_field;
    93 type descriptor = Model_Def.descriptor;
    94 type values = Model_Def.values;
    95 type message = string;
    96 
    97 type T = Model_Def.o_model;
    98 type single = Model_Def.o_model_single
    99 val single_empty = Model_Def.o_model_empty;
   100 val single_to_string = Model_Def.o_model_single_to_string;
   101 val to_string = Model_Def.o_model_to_string;
   102 
   103 (* O_Model.single without leading integer *)
   104 type preori = (variants * m_field * term * term list);
   105 \<^isac_test>\<open>
   106 fun preori2str (vs, fi, t, ts) = 
   107   "(" ^ (strs2str o (map string_of_int)) vs ^ ", " ^ fi ^ ", " ^
   108   UnparseC.term t ^ ", " ^ (strs2str o (map UnparseC.term)) ts ^ ")";
   109 val preoris2str = (strs2str' o (map (linefeed o preori2str)));
   110 \<close>
   111 
   112 (* get the first term in ts from ori *)
   113 fun getr_ct thy (_, _, fd, d, ts) =
   114   (fd, ((UnparseC.term_in_thy thy) o Input_Descript.join) (d,[hd ts]))
   115 
   116 
   117 (** initialise O_Model **)
   118 
   119 (* compare d and dsc in pbt and transfer field to pre-ori *)
   120 fun add_field (_: theory) pbt (d,ts) = 
   121   let fun eq d pt = (d = (fst o snd) pt);
   122   in case filter (eq d) pbt of
   123        [(fi, (_, _))] => (fi, d, ts)
   124      | [] => ("#undef", d, ts)   (*may come with met.ppc*)
   125      | _ => raise ERROR ("add_field: " ^ UnparseC.term d ^ " more than once in pbt")
   126   end;
   127 
   128 (*
   129   mark an element with the position within a plateau;
   130   a plateau with length 1 is marked with 0
   131 *)
   132 fun mark _ [] = raise ERROR "mark []"
   133   | mark eq xs =
   134     let
   135       fun mar xx _ [x] n = xx @ [(if n = 1 then 0 else n, x)]
   136         | mar _ _ [] _ = raise ERROR "mark []"
   137         | mar xx eq (x:: x' :: xs) n = 
   138         if eq(x, x') then mar (xx @ [(n, x)]) eq (x' :: xs) (n + 1)
   139         else mar (xx @ [(if n = 1 then 0 else n, x)]) eq (x' :: xs) 1;
   140     in mar [] eq xs 1 end;
   141 
   142 (*
   143   assumes equal descriptions to be in adjacent 'plateaus',
   144   items at a certain position within the plateaus form a variant;
   145   length = 1 ... marked with 0: covers all variants
   146 *)
   147 fun add_variants fdts = 
   148   let 
   149     fun eq (a, b) = curry op= (snd3 a) (snd3 b);
   150   in mark eq fdts end;
   151 
   152 fun max [] = raise ERROR "max of []"
   153   | max (y :: ys) =
   154   let fun mx x [] = x
   155 	| mx x (y :: ys) = if x < y then mx y ys else mx x ys;
   156 in mx y ys end;
   157 
   158 fun coll_variants (((v,x) :: vxs)) =
   159     let
   160       fun col xs (vs, x) [] = xs @ [(vs, x)]
   161         | col xs (vs, x) ((v', x') :: vxs') = 
   162         if x = x' then col xs (vs @ [v'], x') vxs'
   163         else col (xs @ [(vs, x)]) ([v'], x') vxs';
   164     in col [] ([v], x) vxs end
   165   | coll_variants _ = raise ERROR "coll_variants: called with []";
   166 
   167 fun replace_0 vm [0] = intsto vm
   168   | replace_0 _ vs = vs;
   169 
   170 fun add_id [] = raise ERROR "O_Model.add_id []"
   171   | add_id xs =
   172     let
   173       fun add _ [] = []
   174         | add n (x :: xs) = (n, x) :: add (n + 1) xs;
   175     in add 1 xs end;
   176 
   177 fun flattup (a, (b, (c, d, e))) = (a, b, c, d, e);
   178 
   179 fun init [] _ _ = []
   180   | init fmz thy pbt =
   181     let
   182       val model =
   183         (map (fn str => str
   184           |> TermC.parseNEW'' thy
   185           |> Input_Descript.split
   186           |> add_field thy pbt) fmz)
   187         |> add_variants;
   188       val maxv = model |> map fst |> max;
   189       val maxv = if maxv = 0 then 1 else maxv;
   190       val model' = model
   191         |> coll_variants
   192         |> map (replace_0 maxv |> apfst)
   193         |> add_id
   194         |> map flattup;
   195     in model' end;
   196 
   197 
   198 (** get the values **)
   199 
   200 fun mkval _(*dsc*) [] = raise ERROR "mkval called with []"
   201   | mkval _ [t] = t
   202   | mkval _ ts = TermC.list2isalist ((type_of o hd) ts) ts;
   203 fun mkval' x = mkval TermC.empty x;
   204 (*TODO: unify with values'*)
   205 fun values (oris:T) =
   206   ((map (mkval' o (#5))) o (filter ((member_swap op= 1) o (#2)))) oris
   207 
   208 
   209 (** complete wrt. Model_Pattern.T by use of root's O_Model.T **)
   210 
   211 fun complete_for m_patt root_model (o_model, ctxt) =
   212   let
   213     val  missing = m_patt |> filter_out
   214       (fn (_, (descriptor, _)) => (member op = (map #4 o_model) descriptor))
   215     val add = (root_model
   216       |> filter
   217         (fn (_, _, _, descriptor, _) => (member op = (map (fst o snd) missing)) descriptor))
   218   in
   219     ((o_model @ add)
   220       |> map (fn (a, b, _, descr, e) =>
   221                case Model_Pattern.get_field descr m_patt of
   222                  SOME m_field => (a, b, m_field, descr, e)
   223                | NONE => (a, b, "#undef", descr, e))
   224       |> map (fn (_, b, c, d, e) => (b, c, d, e))      (* for correct enumeration *)
   225       |> add_id                                        (* for correct enumeration *)
   226       |> map (fn (a, (b, c, d, e)) => (a, b, c, d, e)), (* for correct enumeration *)
   227     ctxt |> ContextC.add_constraints (add |> values |> TermC.vars'))
   228   end
   229 
   230 
   231 (** ? ? ? **)
   232 
   233 (* make oris from args of the stac SubProblem and from pbt.
   234    can this formal argument (of a model-pattern) be omitted in the arg-list
   235    of a SubProblem ? see calcelems.sml 'type met '                        *)
   236 fun is_copy_named_idstr str =
   237   case (rev o Symbol.explode) str of
   238 	  "'" :: _ :: "'" :: _ => true
   239   | _ => false
   240 fun is_copy_named (_, (_, t)) = (is_copy_named_idstr o TermC.free2str) t
   241 
   242 (* should this formal argument (of a model-pattern) create a new identifier? *)
   243 fun is_copy_named_generating_idstr str =
   244   if is_copy_named_idstr str
   245   then
   246     case (rev o Symbol.explode) str of
   247 	    "'" :: "'" :: "'" :: _ => false
   248     | _ => true
   249   else false
   250 fun is_copy_named_generating (_, (_, t)) = (is_copy_named_generating_idstr o TermC.free2str) t
   251 
   252 (* generate a new variable "x_i" name from a related given one "x"
   253    by use of oris relating "v_v'i'" (is_copy_named!) to "v_v"
   254    e.g. (v_v, x) & (v_v'i', ?) --> (v_v'i', x_i),
   255    but leave is_copy_named_generating as is, e.t. ss''' *)
   256 fun cpy_nam pbt oris (p as (field, (dsc, t))) =
   257   case \<^try>\<open>
   258     if is_copy_named_generating p
   259     then (*WN051014 kept strange old code ...*)
   260       let fun sel (_,_,d,ts) = Input_Descript.join'''' (d, ts) 
   261         val cy' = (implode o (drop_last_n 3) o Symbol.explode o TermC.free2str) t
   262         val ext = (last_elem o drop_last o Symbol.explode o TermC.free2str) t
   263         val vars' = map (Term.term_name o snd o snd) pbt (*cpy-nam filtered_out*)
   264         val vals = map sel oris
   265         val cy_ext = (Term.term_name o the) (assoc (vars' ~~ vals, cy')) ^ "_" ^ ext
   266       in ([1], field, dsc, [TermC.mk_free (type_of t) cy_ext]) end
   267     else ([1], field, dsc, [t])
   268 	\<close> of
   269     SOME x => x
   270   | NONE => raise ERROR ("cpy_nam: for "^ UnparseC.term t)
   271 
   272 (* ["BOOL (1+x=2)", "REAL x"] --match_ags--> oris 
   273    --values'--> ["equality (1+x=2)", "boundVariable x", "solutions L"] *)
   274 (*TODO: unify with values*)
   275 fun values' oris =
   276   let
   277     fun ori2fmz_vals (_, _, _, dsc, ts) = 
   278       case \<^try>\<open>((UnparseC.term o Input_Descript.join') (dsc, ts), last_elem ts)\<close> of
   279         SOME x => x
   280       | NONE => raise ERROR ("ori2fmz_env called with " ^ UnparseC.terms ts)
   281   in (split_list o (map ori2fmz_vals)) oris end
   282 
   283 
   284 (** tools for I_Model **)
   285 
   286 fun seek_oridts ctxt m_field (descript, vals) [] =
   287     ("seek_oridts: input ('" ^
   288         (UnparseC.term_in_ctxt ctxt (Input_Descript.join (descript, vals))) ^
   289           "') not found in oris (typed)", (0, [], m_field, descript, vals), [])
   290   | seek_oridts ctxt sel (d, ts) ((id, vat, sel', d', ts') :: oris) =
   291     if sel = sel' andalso d = d' andalso (inter op = ts ts') <> []
   292     then ("", (id, vat, sel, d, inter op = ts ts'), ts')
   293     else seek_oridts ctxt sel (d, ts) oris
   294 
   295 (* to an input (_ $ values) find the according O_Model.single and insert the values *)
   296 fun seek_orits ctxt _ vals [] = 
   297     ("seek_orits: input (_, '" ^ strs2str (map (UnparseC.term_in_ctxt ctxt) vals) ^
   298       "') not found in oris (typed)", single_empty, [])
   299   | seek_orits ctxt m_field vals ((id, vat, m_field', descr, vals') :: oris) =
   300     if m_field = m_field' andalso (inter op = vals vals') <> [] 
   301     then
   302       if m_field = m_field' 
   303       then ("", (id, vat, m_field, descr, inter op = vals vals'), vals')
   304       else ((strs2str' o map (UnparseC.term_in_ctxt ctxt)) vals ^
   305         " not for " ^ m_field, single_empty, [])
   306     else seek_orits ctxt m_field vals oris
   307 
   308 fun test_types ctxt (d,ts) =
   309   let 
   310     val opt = (try Input_Descript.join) (d, ts)
   311     val msg = case opt of 
   312       SOME _ => "" 
   313     | NONE => (UnparseC.term_in_ctxt ctxt d ^ " " ^
   314 	    (strs2str' o map (UnparseC.term_in_ctxt ctxt)) ts ^ " is illtyped")
   315   in msg end
   316 
   317 (* make a term 'typeless' for comparing with another 'typeless' term;
   318    'type-less' usually is illtyped                                  *)
   319 fun typeless (Const (s, _)) = (Const (s, TermC.typ_empty)) 
   320   | typeless (Free (s, _)) = (Free (s, TermC.typ_empty))
   321   | typeless (Var (n, _)) = (Var (n, TermC.typ_empty))
   322   | typeless (Bound i) = (Bound i)
   323   | typeless (Abs (s, _,t)) = Abs(s, TermC.typ_empty, typeless t)
   324   | typeless (t1 $ t2) = (typeless t1) $ (typeless t2)
   325 
   326 (* is the term t input (or taken from fmz) known in O_Model ?
   327    give feedback on all(?) strange input;
   328    return _all_ terms already input to this item (e.g. valuesFor a,b) *)
   329 fun is_known ctxt sel ori t =
   330   let
   331     val ots = ((distinct op =) o flat o (map #5)) ori
   332     val oids = ((map (fst o dest_Free)) o (distinct op =) o flat o (map TermC.vars)) ots
   333     val (d, ts) = Input_Descript.split t
   334     val ids = map (fst o dest_Free) (((distinct op =) o (flat o (map TermC.vars))) ts)
   335   in
   336     if (subtract op = oids ids) <> []
   337     then ("identifiers " ^ strs2str' (subtract op = oids ids) ^ " not in example", single_empty, [])
   338     else 
   339 	    if d = TermC.empty
   340 	    then 
   341 	      if not (subset op = (map typeless ts, map typeless ots))
   342 	      then ("terms '" ^ (strs2str' o (map (UnparseC.term_in_ctxt ctxt))) ts ^
   343 		      "' not in example (typeless)", single_empty, [])
   344 	      else 
   345           (case seek_orits ctxt sel ts(*..values*) ori of
   346 		         ("", ori_ as (_, _, _, d, ts), all) =>
   347 		            (case test_types ctxt (d,ts) of
   348 		              "" => ("", ori_, all)
   349 		            | msg => (msg, single_empty, []))
   350 		       | (msg, _, _) => (msg, single_empty, []))
   351 	    else 
   352 	      if member op = (map #4 ori) d
   353 	      then seek_oridts ctxt sel (d, ts) ori
   354 	      else (UnparseC.term_in_ctxt ctxt d ^ " not in example", (0, [], sel, d, ts), [])
   355   end
   356 
   357 (**)end(**);