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