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