src/Tools/isac/MathEngBasic/model.sml
author Walther Neuper <walther.neuper@jku.at>
Sat, 26 Oct 2019 13:03:16 +0200
changeset 59674 3da177a07c3e
parent 59662 src/Tools/isac/Specify/model.sml@e3713aaf2735
child 59680 2796db5c718c
permissions -rw-r--r--
separate common base for Specify and Interpret

note: much to disentangle between Specify -- MathEngBasic -- CalcElements
wneuper@59316
     1
(* Title: Model for (sub-)calculations.
wneuper@59316
     2
          Various representations: item and ppc for frontend, itm_ and itm for internal functions.
wneuper@59316
     3
          The former are related to structure Specify, the latter to structure Chead --
wneuper@59316
     4
          -- apt to re-arrangement of structures
wneuper@59316
     5
   Author: Walther Neuper 170207
wneuper@59316
     6
   (c) due to copyright terms
wneuper@59316
     7
*)
wneuper@59316
     8
wneuper@59316
     9
signature MODEL =
wneuper@59316
    10
sig
wneuper@59316
    11
  type ori
wneuper@59316
    12
  val oris2str : ori list -> string
wneuper@59316
    13
  val e_ori : ori
wneuper@59316
    14
  datatype item
wneuper@59416
    15
  = Correct of Rule.cterm' | False of Rule.cterm' | Incompl of Rule.cterm' | Missing of Rule.cterm' | Superfl of string
wneuper@59316
    16
     | SyntaxE of string | TypeE of string
wneuper@59316
    17
  datatype itm_ = Cor of (term * (term list)) * (term * (term list))
wneuper@59416
    18
  | Syn of Rule.cterm' | Typ of Rule.cterm' | Inc of (term * (term list))	* (term * (term list))
wneuper@59416
    19
  | Sup of (term * (term list)) | Mis of (term * term) | Par of Rule.cterm'
wneuper@59316
    20
  val itm_2str : itm_ -> string
wneuper@59316
    21
  val itm_2str_ : Proof.context -> itm_ -> string
wneuper@59316
    22
  type itm
wneuper@59316
    23
  val itm2str_ : Proof.context -> itm -> string
wneuper@59316
    24
  val itms2str_ : Proof.context -> itm list -> string
wneuper@59316
    25
  val e_itm : itm 
wneuper@59316
    26
  type 'a ppc
wneuper@59316
    27
  val empty_ppc : item ppc
wneuper@59316
    28
  val ppc2str : {Find: string list, Given: string list, Relate: string list, Where: string list,
wneuper@59316
    29
    With: string list} -> string
wneuper@59316
    30
  val itemppc2str : item ppc -> string
wneuper@59316
    31
wneuper@59316
    32
  type vats
wneuper@59316
    33
  val comp_dts : term * term list -> term
wneuper@59316
    34
  val comp_dts' : term * term list -> term
wneuper@59316
    35
  val comp_dts'' : term * term list -> string
wneuper@59316
    36
  val comp_ts : term * term list -> term
wneuper@59316
    37
  val split_dts : term -> term * term list
wneuper@59316
    38
  val split_dts' : term * term -> term list
wneuper@59316
    39
  val pbl_ids' : term -> term list -> term list
wneuper@59316
    40
  val mkval' : term list -> term
wneuper@59316
    41
wneuper@59316
    42
  val d_in : itm_ -> term
wneuper@59316
    43
  val ts_in : itm_ -> term list
wneuper@59316
    44
  val penvval_in : itm_ -> term list
wneuper@59316
    45
  val mk_env : itm list -> (term * term) list (* close to Chead.all_dsc_in, Chead.is_error, etc *)
wneuper@59582
    46
  val vars_of : itm list -> term list
wneuper@59316
    47
  val max_vt : itm list -> int
wneuper@59316
    48
wneuper@59316
    49
(* ---- for tests only: shifted from below to remove the Warning "unused" at fun.def. --------- *)
wneuper@59316
    50
  type penv
wneuper@59316
    51
  val penv2str_ : Proof.context -> penv -> string  (* NONE *)
wneuper@59316
    52
  type preori
wneuper@59316
    53
  val preoris2str : preori list -> string
walther@59662
    54
(*/-------------------------------------------------------- ! aktivate for Test_Isac BEGIN ---\* )
wneuper@59316
    55
  (* NONE *)
walther@59662
    56
( *\--- ! aktivate for Test_Isac END ----------------------------------------------------------/*)
wneuper@59316
    57
wneuper@59316
    58
(*----- unused code, kept as hints to design ideas ---------------------------------------------*)
wneuper@59316
    59
  val untouched : itm list -> bool
wneuper@59316
    60
  type envv
wneuper@59316
    61
  val upds_envv : Proof.context -> envv -> (vats * term * term * term) list -> envv
wneuper@59316
    62
  val item_ppc : string ppc -> item ppc
wneuper@59316
    63
  val all_ts_in : itm_ list -> term list
wneuper@59316
    64
  val pres2str : (bool * term) list -> string
wneuper@59316
    65
end
wneuper@59316
    66
wneuper@59316
    67
structure Model(**) : MODEL(**) =
wneuper@59316
    68
struct
wneuper@59316
    69
(*==========================================================================
wneuper@59316
    70
23.3.02 TODO: ideas on redesign of type itm_,type item,type ori,type item ppc
wneuper@59316
    71
(1) kinds of itms:
wneuper@59316
    72
  (1.1) untouched: for modeling only dsc displayed(impossible after match_itms)
wneuper@59316
    73
        =(presently) Mis (? should be Inc initially, and Mis after match_itms?)
wneuper@59316
    74
  (1.2)  Syn,Typ,Sup: not related to oris
wneuper@59316
    75
    Syn, Typ (presently) should be accepted in appl_add (instead Error')
wneuper@59316
    76
    Sup      (presently) should be accepted in appl_add (instead Error')
wneuper@59316
    77
         _could_ be w.r.t current vat (and then _is_ related to vat
wneuper@59316
    78
    Mis should _not_ be  made Inc ((presently, by appl_add & match_itms)
wneuper@59316
    79
- dsc in itm_ is timeconsuming -- keep id for respective queries ?
wneuper@59316
    80
- order of items in ppc should be stable w.r.t order of itms
wneuper@59316
    81
wneuper@59316
    82
- stepwise input of itms --- match_itms (in one go) ..not coordinated
wneuper@59316
    83
  - unify code
wneuper@59316
    84
  - match_itms / match_itms_oris ..2 versions ?!
wneuper@59316
    85
    (fast, for refine / slow, for modeling)
wneuper@59316
    86
wneuper@59316
    87
- clarify: efficiency <--> simplicity !!!
wneuper@59316
    88
  ?: shift dsc itm_ -> itm | discard int in ori,itm | take int instead dsc 
wneuper@59316
    89
    | take int for perserving order of item ppc in itms 
wneuper@59316
    90
    | make all(!?) handling of itms stable against reordering(?)
wneuper@59316
    91
    | field in ori ?? (not from fmz!) -- meant for efficiency (not doc!???)
wneuper@59316
    92
      -"- "#undef" ?= not touched ?= (id,..)
wneuper@59316
    93
-----------------------------------------------------------------
wneuper@59316
    94
27.3.02:
wneuper@59316
    95
def: type pbt = (field, (dsc, pid)) *** design considerations ***
wneuper@59316
    96
wneuper@59316
    97
(1) fmz + pbt -> oris
wneuper@59316
    98
(2) input + oris -> itm
wneuper@59316
    99
(3) match_itms      : schnell(?) f"ur refine
wneuper@59316
   100
    match_itms_oris : r"uckmeldung f"ur item ppc
wneuper@59316
   101
wneuper@59316
   102
(1.1) in oris fehlt daher pid: (i,v,f,d,ts,pid)
wneuper@59316
   103
---------- ^^^^^ --- dh. pbt meist als argument zu viel !!!
wneuper@59316
   104
wneuper@59316
   105
(3.1) abwarten, wie das matchen mehr unterschiedlicher pbt's sich macht;
wneuper@59316
   106
      wenn Problem pbt v"ollig neue, dann w"are eigentlich n"otig ????:
wneuper@59316
   107
      (a) (_,_,d1,ts,_):ori + pbt -> (i,vt,d2,ts,pid)  dh.vt neu  ????
wneuper@59316
   108
      (b) 
wneuper@59316
   109
==========================================================================*)
wneuper@59316
   110
walther@59603
   111
val script_parse = the o (@{theory ProgLang} |> Rule.thy2ctxt |> TermC.parseNEW);
wneuper@59316
   112
val e_listReal = script_parse "[]::(real list)";
wneuper@59316
   113
val e_listBool = script_parse "[]::(bool list)";
wneuper@59316
   114
wneuper@59316
   115
(* take list-term apart w.r.t. handling elementwise input: @{term "[a, b]"} \<rightarrow> ["[a]","[b]"] *)
wneuper@59316
   116
fun take_apart t =
wneuper@59389
   117
  let val elems = TermC.isalist2list t
wneuper@59389
   118
  in map ((TermC.list2isalist (type_of (hd elems))) o single) elems end;
wneuper@59316
   119
fun take_apart_inv ts = (* t = (take_apart_inv o take_apart) t *)
wneuper@59389
   120
  let val elems = (flat o (map TermC.isalist2list)) ts;
wneuper@59389
   121
  in TermC.list2isalist (type_of (hd elems)) elems end;
wneuper@59316
   122
wneuper@59316
   123
fun is_var (Free _) = true
wneuper@59316
   124
  | is_var _ = false;
wneuper@59316
   125
wneuper@59316
   126
(* special handling for lists. ?WN:14.5.03 ??!? *)
wneuper@59316
   127
fun dest_list (d, ts) = 
wneuper@59316
   128
  let fun dest t = 
wneuper@59595
   129
    if Input_Descript.is_list_dsc d andalso not (Input_Descript.is_unl d) andalso not (is_var t) (*..for pbt*)
wneuper@59389
   130
    then TermC.isalist2list t
wneuper@59316
   131
    else [t]
wneuper@59316
   132
  in (flat o (map dest)) ts end;
wneuper@59316
   133
wneuper@59316
   134
(* revert split_dts only for ts; compare comp_dts *)
wneuper@59316
   135
fun comp_ts (d, ts) = 
wneuper@59595
   136
  if Input_Descript.is_list_dsc d
wneuper@59389
   137
  then if TermC.is_list (hd ts)
wneuper@59595
   138
	  then if Input_Descript.is_unl d
wneuper@59316
   139
	    then (hd ts)             (* e.g. someList [1,3,2] *)
wneuper@59316
   140
	    else (take_apart_inv ts) (* [ [a], [b] ] -> [a,b] *)
wneuper@59316
   141
	  else (hd ts)               (* a variable or metavariable for a list *)
wneuper@59316
   142
  else (hd ts);
wneuper@59316
   143
fun comp_dts (d, []) = 
wneuper@59595
   144
  	if Input_Descript.is_reall_dsc d
wneuper@59316
   145
  	then (d $ e_listReal)
wneuper@59595
   146
  	else if Input_Descript.is_booll_dsc d then (d $ e_listBool) else d
wneuper@59316
   147
  | comp_dts (d, ts) = (d $ (comp_ts (d, ts)))
wneuper@59416
   148
    handle _ => error ("comp_dts: " ^ Rule.term2str d ^ " $ " ^ Rule.term2str (hd ts)); 
wneuper@59316
   149
fun comp_dts' (d, []) = 
wneuper@59595
   150
    if Input_Descript.is_reall_dsc d
wneuper@59316
   151
    then (d $ e_listReal)
wneuper@59595
   152
    else if Input_Descript.is_booll_dsc d then (d $ e_listBool) else d
wneuper@59316
   153
  | comp_dts' (d, ts) = (d $ (comp_ts (d, ts)))
wneuper@59416
   154
    handle _ => error ("comp_dts': " ^ Rule.term2str d ^ " $ " ^ Rule.term2str (hd ts)); 
wneuper@59316
   155
fun comp_dts'' (d, []) = 
wneuper@59595
   156
    if Input_Descript.is_reall_dsc d
wneuper@59416
   157
    then Rule.term2str (d $ e_listReal)
wneuper@59595
   158
    else if Input_Descript.is_booll_dsc d
wneuper@59416
   159
      then Rule.term2str (d $ e_listBool)
wneuper@59416
   160
      else Rule.term2str d
wneuper@59416
   161
  | comp_dts'' (d, ts) = Rule.term2str (d $ (comp_ts (d, ts)))
wneuper@59416
   162
    handle _ => error ("comp_dts'': " ^ Rule.term2str d ^ " $ " ^ Rule.term2str (hd ts)); 
wneuper@59316
   163
wneuper@59316
   164
(* decompose an input into description, terms (ev. elems of lists),
wneuper@59316
   165
    and the value for the problem-environment; inv to comp_dts   *)
wneuper@59316
   166
fun split_dts (t as d $ arg) =
wneuper@59595
   167
    if Input_Descript.is_dsc d
wneuper@59595
   168
    then if Input_Descript.is_list_dsc d andalso TermC.is_list arg andalso Input_Descript.is_unl d |> not
wneuper@59316
   169
      then (d, take_apart arg)
wneuper@59316
   170
      else (d, [arg])
wneuper@59577
   171
    else (Rule.e_term, TermC.dest_list' t)
wneuper@59316
   172
  | split_dts t =
wneuper@59316
   173
    let val t' as (h, _) = strip_comb t;
wneuper@59316
   174
    in
wneuper@59595
   175
      if Input_Descript.is_dsc h
wneuper@59316
   176
      then (h, dest_list t')
wneuper@59577
   177
      else (Rule.e_term, TermC.dest_list' t)
wneuper@59316
   178
    end;
wneuper@59316
   179
(* version returning ts only *)
wneuper@59316
   180
fun split_dts' (d, arg) = 
wneuper@59595
   181
    if Input_Descript.is_dsc d
wneuper@59595
   182
    then if Input_Descript.is_list_dsc d
wneuper@59389
   183
      then if TermC.is_list arg
wneuper@59595
   184
	      then if Input_Descript.is_unl d
wneuper@59316
   185
	        then ([arg])           (* e.g. someList [1,3,2]                 *)
wneuper@59316
   186
		      else (take_apart arg)  (* [a,b] --> SML[ [a], [b] ]SML          *)
wneuper@59316
   187
	      else ([arg])             (* a variable or metavariable for a list *)
wneuper@59316
   188
	     else ([arg])
wneuper@59577
   189
    else (TermC.dest_list' arg)
wneuper@59316
   190
(* WN170204: Warning "redundant"
wneuper@59316
   191
  | split_dts' (d, t) =          (*either dsc or term; 14.5.03 only copied*)
wneuper@59316
   192
    let val (h,argl) = strip_comb t
wneuper@59316
   193
    in
wneuper@59316
   194
      if (not o is_dsc) h
wneuper@59316
   195
      then (dest_list' t)
wneuper@59316
   196
      else (dest_list (h,argl))
wneuper@59316
   197
    end;*)
wneuper@59316
   198
(* revert split_:
wneuper@59316
   199
 WN050903 we do NOT know which is from subtheory, description or term;
wneuper@59316
   200
 typecheck thus may lead to TYPE-error 'unknown constant';
wneuper@59592
   201
 solution: typecheck with (Thy_Info_get_theory "Isac_Knowledge"); i.e. arg 'thy' superfluous*)
wneuper@59316
   202
(*fun comp_dts thy (d,[]) = 
wneuper@59592
   203
    Thm.global_cterm_of (*(sign_of o assoc_thy) "Isac_Knowledge"*)
wneuper@59592
   204
	     (Thy_Info_get_theory "Isac_Knowledge")
wneuper@59316
   205
	     (*comp_dts:FIXXME stay with term for efficiency !!!*)
wneuper@59316
   206
	     (if is_reall_dsc d then (d $ e_listReal)
wneuper@59316
   207
	      else if is_booll_dsc d then (d $ e_listBool)
wneuper@59316
   208
	      else d)
wneuper@59316
   209
  | comp_dts (d,ts) =
wneuper@59592
   210
    (Thm.global_cterm_of (*(sign_of o assoc_thy) "Isac_Knowledge"*)
wneuper@59592
   211
	      (Thy_Info_get_theory "Isac_Knowledge")
wneuper@59316
   212
	      (*comp_dts:FIXXME stay with term for efficiency !!*)
wneuper@59316
   213
	      (d $ (comp_ts (d, ts)))
wneuper@59316
   214
       handle _ => error ("comp_dts: "^(term2str d)^
wneuper@59316
   215
				" $ "^(term2str (hd ts))));*)
wneuper@59316
   216
wneuper@59316
   217
(* 27.8.01: problem-environment
wneuper@59316
   218
WN.6.5.03: FIXXME reconsider if penv is worth the effort --
wneuper@59316
   219
           -- just rerun a whole expl with num/var may show the same ?!
wneuper@59316
   220
WN.9.5.03: penv-concept stalled, immediately generate script env !
wneuper@59316
   221
           but [#0, epsilon] only outcommented for eventual reconsideration  *)
wneuper@59316
   222
type penv = (* problem-environment *)
wneuper@59316
   223
  (term           (* err_                              *)
wneuper@59316
   224
	 * (term list)  (* [#0, epsilon] 9.5.03 outcommented *)
wneuper@59316
   225
	) list;
wneuper@59316
   226
fun pen2str ctxt (t, ts) =
wneuper@59416
   227
  pair2str (Rule.term_to_string' ctxt t, (strs2str' o map (Rule.term_to_string'  ctxt)) ts);
wneuper@59316
   228
fun penv2str_ thy penv = (strs2str' o (map (pen2str thy))) penv;
wneuper@59316
   229
wneuper@59316
   230
(* get the constant value from a penv *)
wneuper@59316
   231
fun getval (id, values) = 
wneuper@59316
   232
  case values of
wneuper@59416
   233
	  [] => error ("penv_value: no values in '" ^ Rule.term2str id)
wneuper@59316
   234
  | [v] => (id, v)
wneuper@59316
   235
  | (v1 :: v2 :: _) => (case v1 of 
walther@59620
   236
	      Const ("Program.Arbfix",_) => (id, v2)
wneuper@59316
   237
	    | _ => (id, v1));
wneuper@59316
   238
wneuper@59316
   239
(* 9.5.03: still unused, but left for eventual future development *)
wneuper@59316
   240
type envv = (int * penv) list; (* over variants *)
wneuper@59316
   241
wneuper@59316
   242
(* 14.9.01: not used after putting penv-values into itm_
wneuper@59316
   243
   make the result of split_* a value of problem-environment *)
wneuper@59316
   244
fun mkval _(*dsc*) [] = error "mkval called with []"
wneuper@59316
   245
  | mkval _ [t] = t
wneuper@59389
   246
  | mkval _ ts = TermC.list2isalist ((type_of o hd) ts) ts;
wneuper@59416
   247
fun mkval' x = mkval Rule.e_term x;
wneuper@59316
   248
wneuper@59316
   249
(* the internal representation of a models' item
wneuper@59316
   250
  4.9.01: not consistent:
wneuper@59316
   251
  after Init_Proof 'Inc', but after copy_probl 'Mis' - for same situation
wneuper@59316
   252
  (involves 'is_error');
wneuper@59316
   253
  bool in itm really necessary ???*)
wneuper@59316
   254
datatype itm_ = 
wneuper@59316
   255
  Cor of (term *              (* description                                                     *)
wneuper@59316
   256
    (term list)) *            (* for list: elem-wise input                                       *) 
wneuper@59316
   257
   (term * (term list))       (* elem of penv ---- penv delayed to future                        *)
wneuper@59416
   258
| Syn of Rule.cterm'
wneuper@59416
   259
| Typ of Rule.cterm'
wneuper@59316
   260
| Inc of (term * (term list))	* (term * (term list)) (*lists,
wneuper@59316
   261
			+ init_pbl WN.11.03 FIXXME: empty penv .. bad; init_pbl should return Mis !!!              *)
wneuper@59316
   262
| Sup of (term * (term list)) (* user-input not found in pbt(+?oris?11.03)*)
wneuper@59316
   263
| Mis of (term * term)        (* after re-specification pbt-item not found in pbl: only dsc, pid_*)
wneuper@59416
   264
| Par of Rule.cterm';              (* internal state from fun parsitm                                 *)
wneuper@59316
   265
wneuper@59316
   266
type vats = int list; (* variants in formalizations *)
wneuper@59316
   267
wneuper@59316
   268
(* data-type for working on pbl/met-ppc:
wneuper@59316
   269
  in pbl initially holds descriptions (only) for user guidance *)
wneuper@59316
   270
type itm = 
wneuper@59316
   271
  int *        (* id  =0 .. untouched - descript (only) from init 
wneuper@59316
   272
		              seems to correspond to ori (fun insert_ppc) <> maintain order in item ppc?   *)
wneuper@59316
   273
  vats *       (* variants - copy from ori                                                     *)
wneuper@59316
   274
  bool *       (* input on this item is not/complete                                           *)
wneuper@59316
   275
  string *     (* #Given | #Find | #Relate                                                     *)
wneuper@59316
   276
  itm_;        (*                                                                              *)
wneuper@59316
   277
val e_itm = (0, [], false, "e_itm", Syn "e_itm");
wneuper@59316
   278
wneuper@59316
   279
(* in CalcTree/Subproblem an 'untouched' model is created
wneuper@59316
   280
  FIXME.WN.9.03 model should be filled to 'untouched' by Model/Refine_Problem*)
wneuper@59316
   281
fun untouched itms = foldl and_ (true , map ((curry op = 0) o (#1 : itm -> int)) itms);
wneuper@59316
   282
wneuper@59316
   283
(* find most frequent variant v in itms *)
wneuper@59316
   284
fun vts_in itms = (distinct o flat o (map #2)) (itms:itm list);
wneuper@59316
   285
wneuper@59316
   286
fun cnt itms v = (v, (length o (filter (curry op = v)) o flat o (map #2)) itms);
wneuper@59316
   287
fun vts_cnt vts itms = map (cnt itms) vts;
wneuper@59316
   288
fun max2 [] = error "max2 of []"
wneuper@59316
   289
  | max2 (y :: ys) =
wneuper@59316
   290
    let
wneuper@59316
   291
      fun mx (a,x) [] = (a,x)
wneuper@59316
   292
  	    | mx (a, x) ((b,y) :: ys) = if x < y then mx (b, y) ys else mx (a, x) ys;
wneuper@59316
   293
    in mx y ys end;
wneuper@59316
   294
wneuper@59316
   295
(* find the variant with most items already input *)
wneuper@59316
   296
fun max_vt itms = 
wneuper@59316
   297
    let val vts = (vts_cnt (vts_in itms)) itms;
wneuper@59316
   298
    in if vts = [] then 0 else (fst o max2) vts end;
wneuper@59316
   299
wneuper@59316
   300
(* TODO ev. make more efficient by avoiding flat *)
wneuper@59316
   301
fun mk_e (Cor (_, iv)) = [getval iv]
wneuper@59316
   302
  | mk_e (Syn _) = []
wneuper@59316
   303
  | mk_e (Typ _) = [] 
wneuper@59316
   304
  | mk_e (Inc (_, iv)) = [getval iv]
wneuper@59316
   305
  | mk_e (Sup _) = []
wneuper@59316
   306
  | mk_e (Mis _) = []
wneuper@59316
   307
  | mk_e  _ = error "mk_e: uncovered case in fun.def.";
wneuper@59316
   308
fun mk_en vt (_, vts, _, _, itm_) = if member op = vts vt then mk_e itm_ else [];
wneuper@59316
   309
wneuper@59316
   310
(* extract the environment from an item list; takes the variant with most items *)
wneuper@59316
   311
fun mk_env itms = 
wneuper@59316
   312
  let val vt = max_vt itms
wneuper@59316
   313
  in (flat o (map (mk_en vt))) itms end;
wneuper@59316
   314
wneuper@59316
   315
(* example as provided by an author, complete w.r.t. pbt specified 
wneuper@59316
   316
   not touched by any user action                                 *)
wneuper@59316
   317
type ori =
wneuper@59316
   318
  (int *     (* id: 10.3.00ff impl. only <>0 .. touched 
wneuper@59316
   319
			          21.3.02: insert_ppc needs it ! ?:purpose maintain order in item ppc ??? *)
wneuper@59316
   320
	vats *     (* variants 21.3.02: related to pbt..discard ?                             *)
wneuper@59316
   321
	string *   (* #Given | #Find | #Relate 21.3.02: discard ?                             *)
wneuper@59316
   322
	term *     (* description                                                             *)
wneuper@59316
   323
	term list  (* isalist2list t | [t]                                                    *)
wneuper@59316
   324
	);
wneuper@59416
   325
val e_ori = (0, [], "", Rule.e_term, [Rule.e_term]) : ori;
wneuper@59316
   326
wneuper@59316
   327
fun ori2str (i, vs, fi, t, ts) = 
wneuper@59316
   328
  "(" ^ string_of_int i ^ ", " ^ (strs2str o (map string_of_int)) vs ^ ", " ^ fi ^ "," ^
wneuper@59416
   329
  Rule.term2str t ^ ", " ^ (strs2str o (map Rule.term2str)) ts ^ ")";
wneuper@59405
   330
val oris2str = strs2str' o (map (Celem.linefeed o ori2str));
wneuper@59316
   331
wneuper@59316
   332
(* an or without leading integer *)
wneuper@59316
   333
type preori = (vats * string * term * term list);
wneuper@59316
   334
fun preori2str (vs, fi, t, ts) = 
wneuper@59316
   335
  "(" ^ (strs2str o (map string_of_int)) vs ^ ", " ^ fi ^ ", " ^
wneuper@59416
   336
  Rule.term2str t ^ ", " ^ (strs2str o (map Rule.term2str)) ts ^ ")";
wneuper@59405
   337
val preoris2str = (strs2str' o (map (Celem.linefeed o preori2str)));
wneuper@59316
   338
wneuper@59316
   339
(* 9.5.03 penv postponed: pbl_ids' *)
wneuper@59316
   340
fun pbl_ids' d vs = [comp_ts (d, vs)];
wneuper@59316
   341
wneuper@59316
   342
(* 14.9.01: not used after putting values for penv into itm_
wneuper@59316
   343
  WN.5.5.03: used in upd .. upd_envv *)
wneuper@59316
   344
fun upd_penv ctxt penv dsc (id, vl) =
wneuper@59316
   345
(tracing"### upd_penv used !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
wneuper@59316
   346
 tracing"### upd_penv used !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
wneuper@59316
   347
 tracing"### upd_penv used !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
wneuper@59599
   348
  overwrite (penv, (id, Input_Descript.pbl_ids ctxt dsc vl))
wneuper@59316
   349
);
wneuper@59316
   350
wneuper@59316
   351
(* WN.9.5.03: not reconsidered; looks strange !!!*)
wneuper@59316
   352
fun upd thy envv dsc (id, vl) i =
wneuper@59316
   353
    let val penv = case assoc (envv, i) of
wneuper@59316
   354
		       SOME e => e
wneuper@59316
   355
		     | NONE => [];
wneuper@59316
   356
        val penv' = upd_penv thy penv dsc (id, vl);
wneuper@59316
   357
    in (i, penv') end;
wneuper@59316
   358
wneuper@59316
   359
(* 14.9.01: not used after putting pre-penv into itm_*)
wneuper@59316
   360
fun upd_envv thy envv vats dsc id vl  =
wneuper@59316
   361
    let val vats = if length vats = 0 
wneuper@59316
   362
		   then (*unknown id to _all_ variants*)
wneuper@59316
   363
		       if length envv = 0 then [1]
wneuper@59316
   364
		       else (intsto o length) envv 
wneuper@59316
   365
		   else vats
wneuper@59316
   366
	fun isin vats (i, _) = member op = vats i;
wneuper@59316
   367
	val envs_notin_vat = filter_out (isin vats) envv;
wneuper@59316
   368
    in (map (upd thy envv dsc (id, vl)) vats) @ envs_notin_vat end;
wneuper@59316
   369
wneuper@59316
   370
(* update envv by folding from a list of arguments *)
wneuper@59316
   371
fun upds_envv _ envv [] = envv
wneuper@59316
   372
  | upds_envv thy envv ((vs, dsc, id, vl) :: ps) = 
wneuper@59316
   373
    upds_envv thy (upd_envv thy envv vs dsc id vl) ps;
wneuper@59316
   374
wneuper@59316
   375
(* for _output_ of the items of a Model *)
wneuper@59316
   376
datatype item = 
wneuper@59416
   377
    Correct of Rule.cterm' (* labels a correct formula (type cterm') *)
wneuper@59316
   378
  | SyntaxE of string (**)
wneuper@59316
   379
  | TypeE   of string (**)
wneuper@59416
   380
  | False   of Rule.cterm' (* WN050618 notexistent in itm_: only used in Where *)
wneuper@59416
   381
  | Incompl of Rule.cterm' (**)
wneuper@59316
   382
  | Superfl of string (**)
wneuper@59416
   383
  | Missing of Rule.cterm';
wneuper@59316
   384
fun item2str (Correct  s) ="Correct " ^ s
wneuper@59316
   385
  | item2str (SyntaxE  s) ="SyntaxE " ^ s
wneuper@59316
   386
  | item2str (TypeE    s) ="TypeE " ^ s
wneuper@59316
   387
  | item2str (False    s) ="False " ^ s
wneuper@59316
   388
  | item2str (Incompl  s) ="Incompl " ^ s
wneuper@59316
   389
  | item2str (Superfl  s) ="Superfl " ^ s
wneuper@59316
   390
  | item2str (Missing  s) ="Missing " ^ s;
wneuper@59316
   391
(*make string for error-msgs*)
wneuper@59316
   392
fun itm_2str_ ctxt (Cor ((d, ts), penv)) = 
wneuper@59416
   393
    "Cor " ^ Rule.term_to_string'  ctxt (comp_dts (d, ts)) ^ " ," ^ pen2str ctxt penv
wneuper@59316
   394
  | itm_2str_ _ (Syn c) = "Syn " ^ c
wneuper@59316
   395
  | itm_2str_ _ (Typ c) = "Typ " ^ c
wneuper@59316
   396
  | itm_2str_ ctxt (Inc ((d, ts), penv)) = 
wneuper@59416
   397
    "Inc " ^ Rule.term_to_string'  ctxt (comp_dts (d, ts)) ^ " ," ^ pen2str ctxt penv
wneuper@59316
   398
  | itm_2str_ ctxt (Sup (d, ts)) = 
wneuper@59416
   399
    "Sup " ^ Rule.term_to_string'  ctxt (comp_dts (d, ts))
wneuper@59316
   400
  | itm_2str_ ctxt (Mis (d, pid)) = 
wneuper@59416
   401
    "Mis "^ Rule.term_to_string'  ctxt d ^ " " ^ Rule.term_to_string'  ctxt pid
wneuper@59316
   402
  | itm_2str_ _ (Par s) = "Trm "^s;
wneuper@59592
   403
fun itm_2str t = itm_2str_ (Rule.thy2ctxt' "Isac_Knowledge") t;
wneuper@59316
   404
fun itm2str_ ctxt ((i, is, b, s, itm_):itm) = 
wneuper@59316
   405
  "(" ^ string_of_int i ^ " ," ^ ints2str' is ^ " ," ^ bool2str b ^ " ," ^
wneuper@59316
   406
  s ^ " ," ^ itm_2str_ ctxt itm_ ^ ")";
wneuper@59405
   407
fun itms2str_ ctxt itms = strs2str' (map (Celem.linefeed o (itm2str_ ctxt)) itms);
wneuper@59316
   408
fun init_item str = SyntaxE str;
wneuper@59316
   409
wneuper@59316
   410
type 'a ppc = 
wneuper@59316
   411
  {Given : 'a list, Where: 'a list, Find  : 'a list, With : 'a list, Relate: 'a list};
wneuper@59316
   412
fun ppc2str {Given = Given, Where = Where, Find = Find, With = With, Relate = Relate}=
wneuper@59316
   413
  "{Given =" ^ strs2str Given ^ ",Where=" ^ strs2str Where ^ ",Find  =" ^ strs2str Find ^
wneuper@59316
   414
  ",With =" ^ strs2str With ^ ",Relate=" ^ strs2str Relate ^ "}";
wneuper@59316
   415
wneuper@59316
   416
fun item_ppc {Given = gi, Where= wh, Find = fi, With = wi, Relate= re} =
wneuper@59316
   417
  {Given = map init_item gi, Where= map init_item wh, Find = map init_item fi,
wneuper@59316
   418
    With = map init_item wi, Relate= map init_item re};
wneuper@59316
   419
fun itemppc2str ({Given=Given,Where=Where,
wneuper@59316
   420
		 Find=Find,With=With,Relate=Relate}:item ppc)=
wneuper@59316
   421
    ("{Given =" ^ ((strs2str' o (map item2str))	 Given ) ^
wneuper@59316
   422
     ",Where=" ^ ((strs2str' o (map item2str))	 Where) ^
wneuper@59316
   423
     ",Find  =" ^ ((strs2str' o (map item2str))	 Find  ) ^
wneuper@59316
   424
     ",With =" ^ ((strs2str' o (map item2str))	 With ) ^
wneuper@59316
   425
     ",Relate=" ^ ((strs2str' o (map item2str))	 Relate) ^ "}");
wneuper@59316
   426
wneuper@59316
   427
val empty_ppc = {Given = [], Where= [], Find  = [], With = [], Relate= []};
wneuper@59316
   428
wneuper@59316
   429
fun ts_in (Cor ((_, ts), _)) = ts
wneuper@59316
   430
  | ts_in (Syn _) = []
wneuper@59316
   431
  | ts_in (Typ _) = []
wneuper@59316
   432
  | ts_in (Inc ((_, ts), _)) = ts
wneuper@59316
   433
  | ts_in (Sup (_, ts)) = ts
wneuper@59316
   434
  | ts_in (Mis _) = []
wneuper@59316
   435
  | ts_in _ = error "ts_in: uncovered case in fun.def.";
wneuper@59316
   436
(*WN050629 unused*)
wneuper@59316
   437
fun all_ts_in itm_s = (flat o (map ts_in)) itm_s;
wneuper@59389
   438
val unique = (Thm.term_of o the o (TermC.parse @{theory "Real"} )) "UnIqE_tErM";
wneuper@59316
   439
fun d_in (Cor ((d ,_), _)) = d
wneuper@59316
   440
  | d_in (Syn c) = (tracing ("*** d_in: Syn ("^c^")"); unique)
wneuper@59316
   441
  | d_in (Typ c) = (tracing ("*** d_in: Typ ("^c^")"); unique)
wneuper@59316
   442
  | d_in (Inc ((d, _), _)) = d
wneuper@59316
   443
  | d_in (Sup (d, _)) = d
wneuper@59316
   444
  | d_in (Mis (d, _)) = d
wneuper@59316
   445
  | d_in _ = error "d_in: uncovered case in fun.def.";
wneuper@59316
   446
wneuper@59416
   447
fun dts2str (d, ts) = pair2str (Rule.term2str d, Rule.terms2str ts);
wneuper@59316
   448
fun penvval_in (Cor ((d, _), (_, ts))) = [comp_ts (d,ts)]
wneuper@59316
   449
  | penvval_in (Syn  (c)) = (tracing("*** penvval_in: Syn ("^c^")"); [])
wneuper@59316
   450
  | penvval_in (Typ  (c)) = (tracing("*** penvval_in: Typ ("^c^")"); [])
wneuper@59316
   451
  | penvval_in (Inc (_, (_, ts))) = ts
wneuper@59316
   452
  | penvval_in (Sup dts) = (tracing ("*** penvval_in: Sup "^(dts2str dts)); [])
wneuper@59316
   453
  | penvval_in (Mis (d, t)) = (tracing ("*** penvval_in: Mis " ^
wneuper@59416
   454
			pair2str(Rule.term2str d, Rule.term2str t)); [])
wneuper@59316
   455
	| penvval_in _ = error "penvval_in: uncovered case in fun.def.";
wneuper@59316
   456
wneuper@59316
   457
(* check a predicate labelled with indication of incomplete substitution;
wneuper@59316
   458
  rls ->    (* for eval_true                                               *)
wneuper@59316
   459
  bool * 	  (* have _all_ variables(Free) from the model-pattern 
wneuper@59316
   460
               been substituted by a value from the pattern's environment ?*)
wneuper@59316
   461
  term ->   (* the precondition                                            *)
wneuper@59316
   462
  bool * 	  (* has the precondition evaluated to true                      *)
wneuper@59316
   463
  term      (* the precondition (for map)                                  *)
wneuper@59316
   464
*)
wneuper@59416
   465
fun pre2str (b, t) = pair2str(bool2str b, Rule.term2str t);
wneuper@59405
   466
fun pres2str pres = strs2str' (map (Celem.linefeed o pre2str) pres);
wneuper@59316
   467
wneuper@59582
   468
fun vars_of itms = itms |> mk_env |> map snd
wneuper@59582
   469
wneuper@59316
   470
end;